Extended IKE header for IKEv1 support
authorMartin Willi <martin@revosec.ch>
Tue, 15 Nov 2011 12:53:56 +0000 (13:53 +0100)
committerMartin Willi <martin@revosec.ch>
Tue, 20 Mar 2012 16:30:39 +0000 (17:30 +0100)
src/libcharon/encoding/message.c
src/libcharon/encoding/payloads/ike_header.c
src/libcharon/encoding/payloads/ike_header.h
src/libcharon/network/receiver.c

index 2b53992..fa724e4 100644 (file)
@@ -1120,9 +1120,8 @@ METHOD(message_t, generate, status_t,
                DBG2(DBG_ENC, "not encrypting payloads");
        }
 
-       ike_header = ike_header_create();
-       ike_header->set_maj_version(ike_header, this->major_version);
-       ike_header->set_min_version(ike_header, this->minor_version);
+       ike_header = ike_header_create_version(this->major_version,
+                                                                                  this->minor_version);
        ike_header->set_exchange_type(ike_header, this->exchange_type);
        ike_header->set_message_id(ike_header, this->message_id);
        ike_header->set_response_flag(ike_header, !this->is_request);
@@ -1567,8 +1566,8 @@ message_t *message_create_from_packet(packet_t *packet)
                        .get_packet_data = _get_packet_data,
                        .destroy = _destroy,
                },
-               .major_version = IKE_MAJOR_VERSION,
-               .minor_version = IKE_MINOR_VERSION,
+               .major_version = IKEV2_MAJOR_VERSION,
+               .minor_version = IKEV2_MINOR_VERSION,
                .exchange_type = EXCHANGE_TYPE_UNDEFINED,
                .is_request = TRUE,
                .first_payload = NO_PAYLOAD,
index 24d22f3..556e422 100644 (file)
@@ -81,12 +81,27 @@ struct private_ike_header_t {
                 * TRUE, if this is a response, FALSE if its a Request.
                 */
                bool response;
+
+               /**
+                * TRUE, if the packet is encrypted (IKEv1).
+                */
+               bool encryption;
+
+               /**
+                * TRUE, if the commit flag is set (IKEv1).
+                */
+               bool commit;
+
+               /**
+                * TRUE, if the auth only flag is set (IKEv1).
+                */
+               bool authonly;
        } flags;
 
        /**
         * Reserved bits of IKE header
         */
-       bool reserved[5];
+       bool reserved[2];
 
        /**
         * Associated Message-ID.
@@ -99,9 +114,14 @@ struct private_ike_header_t {
        u_int32_t length;
 };
 
-ENUM_BEGIN(exchange_type_names, EXCHANGE_TYPE_UNDEFINED, EXCHANGE_TYPE_UNDEFINED,
-       "EXCHANGE_TYPE_UNDEFINED");
-ENUM_NEXT(exchange_type_names, IKE_SA_INIT, IKE_SESSION_RESUME, EXCHANGE_TYPE_UNDEFINED,
+ENUM_BEGIN(exchange_type_names, ID_PROT, INFORMATIONAL_V1,
+       "ID_PROT",
+       "AUTH_ONLY",
+       "AGGRESSIVE",
+       "INFORMATIONAL_V1");
+ENUM_NEXT(exchange_type_names, QUICK_MODE, IKE_SESSION_RESUME, INFORMATIONAL_V1,
+       "QUICK_MODE",
+       "NEW_GROUP_MODE",
        "IKE_SA_INIT",
        "IKE_AUTH",
        "CREATE_CHILD_SA",
@@ -110,13 +130,18 @@ ENUM_NEXT(exchange_type_names, IKE_SA_INIT, IKE_SESSION_RESUME, EXCHANGE_TYPE_UN
 #ifdef ME
 ENUM_NEXT(exchange_type_names, ME_CONNECT, ME_CONNECT, IKE_SESSION_RESUME,
        "ME_CONNECT");
-ENUM_END(exchange_type_names, ME_CONNECT);
+ENUM_NEXT(exchange_type_names, EXCHANGE_TYPE_UNDEFINED,
+                                                          EXCHANGE_TYPE_UNDEFINED, ME_CONNECT,
+       "EXCHANGE_TYPE_UNDEFINED");
 #else
-ENUM_END(exchange_type_names, IKE_SESSION_RESUME);
+ENUM_NEXT(exchange_type_names, EXCHANGE_TYPE_UNDEFINED,
+                                                          EXCHANGE_TYPE_UNDEFINED, IKE_SESSION_RESUME,
+       "EXCHANGE_TYPE_UNDEFINED");
 #endif /* ME */
+ENUM_END(exchange_type_names, EXCHANGE_TYPE_UNDEFINED);
 
 /**
- * Encoding rules to parse or generate a IKEv2-Header.
+ * Encoding rules to parse or generate a IKE-Header.
  *
  * The defined offsets are the positions in a object of type
  * ike_header_t.
@@ -137,21 +162,19 @@ encoding_rule_t ike_header_encodings[] = {
        /* 2 Bit reserved bits */
        { RESERVED_BIT, offsetof(private_ike_header_t, reserved[0])             },
        { RESERVED_BIT, offsetof(private_ike_header_t, reserved[1])             },
-       /* 3 Bit flags, stored in the fields response, version and initiator */
+       /* 6 flags  */
        { FLAG,                 offsetof(private_ike_header_t, flags.response)  },
        { FLAG,                 offsetof(private_ike_header_t, flags.version)   },
        { FLAG,                 offsetof(private_ike_header_t, flags.initiator) },
-       /* 3 Bit reserved bits */
-       { RESERVED_BIT, offsetof(private_ike_header_t, reserved[2])             },
-       { RESERVED_BIT, offsetof(private_ike_header_t, reserved[3])             },
-       { RESERVED_BIT, offsetof(private_ike_header_t, reserved[4])             },
+       { FLAG,                 offsetof(private_ike_header_t, flags.authonly)  },
+       { FLAG,                 offsetof(private_ike_header_t, flags.commit)    },
+       { FLAG,                 offsetof(private_ike_header_t, flags.encryption)},
        /* 4 Byte message id, stored in the field message_id */
        { U_INT_32,             offsetof(private_ike_header_t, message_id)              },
        /* 4 Byte length fied, stored in the field length */
        { HEADER_LENGTH,offsetof(private_ike_header_t, length)                  },
 };
 
-
 /*                           1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -172,26 +195,51 @@ encoding_rule_t ike_header_encodings[] = {
 METHOD(payload_t, verify, status_t,
        private_ike_header_t *this)
 {
-       if ((this->exchange_type < IKE_SA_INIT) ||
-               ((this->exchange_type > INFORMATIONAL)
+       switch (this->exchange_type)
+       {
+               case ID_PROT:
+               case AGGRESSIVE:
+                       if (this->message_id != 0)
+                       {
+                               return FAILED;
+                       }
+                       /* fall */
+               case AUTH_ONLY:
+               case INFORMATIONAL_V1:
+               case QUICK_MODE:
+               case NEW_GROUP_MODE:
+                       if (this->maj_version != IKEV1_MAJOR_VERSION)
+                       {
+                               return FAILED;
+                       }
+                       break;
+               case IKE_SA_INIT:
+               case IKE_AUTH:
+               case CREATE_CHILD_SA:
+               case INFORMATIONAL:
+               case IKE_SESSION_RESUME:
 #ifdef ME
-                       && (this->exchange_type != ME_CONNECT)
+               case ME_CONNECT:
 #endif /* ME */
-               ))
-       {
-               /* unsupported exchange type */
-               return FAILED;
+                       if (this->maj_version != IKEV2_MAJOR_VERSION)
+                       {
+                               return FAILED;
+                       }
+                       break;
+               default:
+                       /* unsupported exchange type */
+                       return FAILED;
        }
-       if (this->initiator_spi == 0
+       if (this->initiator_spi == 0)
+       {
 #ifdef ME
-               /* we allow zero spi for INFORMATIONAL exchanges,
-                * to allow connectivity checks */
-               && this->exchange_type != INFORMATIONAL
+               if (this->exchange_type != INFORMATIONAL)
+                       /* we allow zero spi for INFORMATIONAL exchanges,
+                        * to allow connectivity checks */
 #endif /* ME */
-               )
-       {
-               /* initiator spi not set */
-               return FAILED;
+               {
+                       return FAILED;
+               }
        }
        return SUCCESS;
 }
@@ -311,6 +359,43 @@ METHOD(ike_header_t, set_initiator_flag, void,
        this->flags.initiator = initiator;
 }
 
+METHOD(ike_header_t, get_encryption_flag, bool,
+       private_ike_header_t *this)
+{
+       return this->flags.encryption;
+}
+
+METHOD(ike_header_t, set_encryption_flag, void,
+       private_ike_header_t *this, bool encryption)
+{
+       this->flags.encryption = encryption;
+}
+
+
+METHOD(ike_header_t, get_commit_flag, bool,
+       private_ike_header_t *this)
+{
+       return this->flags.commit;
+}
+
+METHOD(ike_header_t, set_commit_flag, void,
+       private_ike_header_t *this, bool commit)
+{
+       this->flags.commit = commit;
+}
+
+METHOD(ike_header_t, get_authonly_flag, bool,
+       private_ike_header_t *this)
+{
+       return this->flags.authonly;
+}
+
+METHOD(ike_header_t, set_authonly_flag, void,
+       private_ike_header_t *this, bool authonly)
+{
+       this->flags.authonly = authonly;
+}
+
 METHOD(ike_header_t, get_exchange_type, u_int8_t,
        private_ike_header_t *this)
 {
@@ -373,21 +458,35 @@ ike_header_t *ike_header_create()
                        .set_version_flag = _set_version_flag,
                        .get_initiator_flag = _get_initiator_flag,
                        .set_initiator_flag = _set_initiator_flag,
+                       .get_encryption_flag = _get_encryption_flag,
+                       .set_encryption_flag = _set_encryption_flag,
+                       .get_commit_flag = _get_commit_flag,
+                       .set_commit_flag = _set_commit_flag,
+                       .get_authonly_flag = _get_authonly_flag,
+                       .set_authonly_flag = _set_authonly_flag,
                        .get_exchange_type = _get_exchange_type,
                        .set_exchange_type = _set_exchange_type,
                        .get_message_id = _get_message_id,
                        .set_message_id = _set_message_id,
                        .destroy = _destroy,
                },
-               .maj_version = IKE_MAJOR_VERSION,
-               .min_version = IKE_MINOR_VERSION,
-               .exchange_type = EXCHANGE_TYPE_UNDEFINED,
-               .flags = {
-                       .initiator = TRUE,
-                       .version = HIGHER_VERSION_SUPPORTED_FLAG,
-               },
                .length = IKE_HEADER_LENGTH,
+               .exchange_type = EXCHANGE_TYPE_UNDEFINED,
        );
 
        return &this->public;
 }
+
+/*
+ * Described in header.
+ */
+ike_header_t *ike_header_create_version(int major, int minor)
+{
+       ike_header_t *this = ike_header_create();
+
+       this->set_maj_version(this, major);
+       this->set_min_version(this, minor);
+       this->set_initiator_flag(this, TRUE);
+
+       return this;
+}
index 5579a49..aa456de 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2007 Tobias Brunner
- * Copyright (C) 2005-2006 Martin Willi
+ * Copyright (C) 2005-2011 Martin Willi
  * Copyright (C) 2005 Jan Hutter
  * Hochschule fuer Technik Rapperswil
  *
@@ -30,19 +30,24 @@ typedef struct ike_header_t ike_header_t;
 #include <encoding/payloads/payload.h>
 
 /**
- * Major Version of IKEv2.
+ * Major Version of IKEv1 we implement.
  */
-#define IKE_MAJOR_VERSION 2
+#define IKEV1_MAJOR_VERSION 1
 
 /**
- * Minor Version of IKEv2.
+ * Minor Version of IKEv2 we implement.
  */
-#define IKE_MINOR_VERSION 0
+#define IKEV1_MINOR_VERSION 0
 
 /**
- * Flag in IKEv2-Header. Always 0.
+ * Major Version of IKEv2 we implement.
  */
-#define HIGHER_VERSION_SUPPORTED_FLAG 0
+#define IKEV2_MAJOR_VERSION 2
+
+/**
+ * Minor Version of IKEv2 we implement.
+ */
+#define IKEV2_MINOR_VERSION 0
 
 /**
  * Length of IKE Header in Bytes.
@@ -57,9 +62,34 @@ typedef struct ike_header_t ike_header_t;
 enum exchange_type_t{
 
        /**
-        * EXCHANGE_TYPE_UNDEFINED. In private space, since not a official message type.
+        * Identity Protection (Main mode).
         */
-       EXCHANGE_TYPE_UNDEFINED = 255,
+       ID_PROT = 2,
+
+       /**
+        * Authentication Only.
+        */
+       AUTH_ONLY = 3,
+
+       /**
+        * Aggresive (Aggressive mode)
+        */
+       AGGRESSIVE = 4,
+
+       /**
+        * Informational in IKEv1
+        */
+       INFORMATIONAL_V1 = 5,
+
+       /**
+        * Quick Mode
+        */
+       QUICK_MODE = 32,
+
+       /**
+        * New Group Mode
+        */
+       NEW_GROUP_MODE = 33,
 
        /**
         * IKE_SA_INIT.
@@ -77,7 +107,7 @@ enum exchange_type_t{
        CREATE_CHILD_SA = 36,
 
        /**
-        * INFORMATIONAL.
+        * INFORMATIONAL in IKEv2.
         */
        INFORMATIONAL = 37,
 
@@ -91,6 +121,11 @@ enum exchange_type_t{
         */
        ME_CONNECT = 240
 #endif /* ME */
+
+       /**
+        * Undefined exchange type, in private space.
+        */
+       EXCHANGE_TYPE_UNDEFINED = 255,
 };
 
 /**
@@ -99,14 +134,10 @@ enum exchange_type_t{
 extern enum_name_t *exchange_type_names;
 
 /**
- * An object of this type represents an IKEv2 header and is used to
- * generate and parse IKEv2 headers.
- *
- * The header format of an IKEv2-Message is compatible to the
- * ISAKMP-Header format to allow implementations supporting
- * both versions of the IKE-protocol.
+ * An object of this type represents an IKE header of either IKEv1 or IKEv2.
  */
 struct ike_header_t {
+
        /**
         * The payload_t interface.
         */
@@ -211,6 +242,48 @@ struct ike_header_t {
        void (*set_initiator_flag) (ike_header_t *this, bool initiator);
 
        /**
+        * Get the encryption flag.
+        *
+        * @return                              encryption flag
+        */
+       bool (*get_encryption_flag) (ike_header_t *this);
+
+       /**
+        * Set the encryption flag.
+        *
+        * @param encryption            encryption flag
+        */
+       void (*set_encryption_flag) (ike_header_t *this, bool encryption);
+
+       /**
+        * Get the commit flag.
+        *
+        * @return                              commit flag
+        */
+       bool (*get_commit_flag) (ike_header_t *this);
+
+       /**
+        * Set the commit flag.
+        *
+        * @param commit                commit flag
+        */
+       void (*set_commit_flag) (ike_header_t *this, bool commit);
+
+       /**
+        * Get the authentication only flag.
+        *
+        * @return                              authonly flag
+        */
+       bool (*get_authonly_flag) (ike_header_t *this);
+
+       /**
+        * Set the authentication only flag.
+        *
+        * @param authonly              authonly flag
+        */
+       void (*set_authonly_flag) (ike_header_t *this, bool authonly);
+
+       /**
         * Get the exchange type.
         *
         * @return                              exchange type
@@ -245,10 +318,17 @@ struct ike_header_t {
 };
 
 /**
- * Create an ike_header_t object
+ * Create an empty ike_header_t object.
  *
  * @return ike_header_t object
  */
 ike_header_t *ike_header_create(void);
 
+/**
+ * Create an ike_header_t object for a specific major/minor version
+ *
+ * @return ike_header_t object
+ */
+ike_header_t *ike_header_create_version(int major, int minor);
+
 #endif /** IKE_HEADER_H_ @}*/
index 2887595..ec30fca 100644 (file)
@@ -371,7 +371,7 @@ static job_requeue_t receive_packets(private_receiver_t *this)
        }
 
        /* check IKE major version */
-       if (message->get_major_version(message) != IKE_MAJOR_VERSION)
+       if (message->get_major_version(message) != IKEV2_MAJOR_VERSION)
        {
                DBG1(DBG_NET, "received unsupported IKE version %d.%d from %H, "
                         "sending INVALID_MAJOR_VERSION", message->get_major_version(message),