]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
charon-tkm: Add get_other_esa_id function to TKM kernel SAD
authorAdrian-Ken Rueegsegger <ken@codelabs.ch>
Wed, 8 Apr 2015 16:54:54 +0000 (18:54 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 4 May 2015 16:07:50 +0000 (18:07 +0200)
The function gets the ESA id for another entry associated with the same
security policy as the specified ESA.

src/charon-tkm/src/tkm/tkm_kernel_sad.c
src/charon-tkm/src/tkm/tkm_kernel_sad.h
src/charon-tkm/tests/kernel_sad_tests.c

index 3394b58af79c07fce0bdaf56826a0b936fddbbd2..619ebeeb61d8ec6c0ef258df9e440ce4a0d7b647 100644 (file)
@@ -126,6 +126,26 @@ static bool sad_entry_match_dst(sad_entry_t * const entry,
                   entry->proto == *proto;
 }
 
+/**
+ * Find a list entry with given esa id.
+ */
+static bool sad_entry_match_esa_id(sad_entry_t * const entry,
+                                                                  const esa_id_type * const esa_id)
+{
+       return entry->esa_id == *esa_id;
+}
+
+/**
+ * Find a list entry with given reqid and different esa id.
+ */
+static bool sad_entry_match_other_esa(sad_entry_t * const entry,
+                                                                         const esa_id_type * const esa_id,
+                                                                         const u_int32_t * const reqid)
+{
+       return entry->reqid  == *reqid &&
+                  entry->esa_id != *esa_id;
+}
+
 /**
  * Compare two SAD entries for equality.
  */
@@ -209,6 +229,42 @@ METHOD(tkm_kernel_sad_t, get_esa_id, esa_id_type,
        return id;
 }
 
+METHOD(tkm_kernel_sad_t, get_other_esa_id, esa_id_type,
+       private_tkm_kernel_sad_t * const this, const esa_id_type esa_id)
+{
+       esa_id_type id = 0;
+       sad_entry_t *entry = NULL;
+       u_int32_t reqid;
+       status_t res;
+
+       this->mutex->lock(this->mutex);
+       res = this->data->find_first(this->data,
+                                                                (linked_list_match_t)sad_entry_match_esa_id,
+                                                                (void**)&entry, &esa_id);
+       if (res == SUCCESS && entry)
+       {
+               reqid = entry->reqid;
+       }
+       else
+       {
+               DBG3(DBG_KNL, "no SAD entry found for ESA id %llu", esa_id);
+               this->mutex->unlock(this->mutex);
+               return id;
+       }
+
+       res = this->data->find_first(this->data,
+                                                                (linked_list_match_t)sad_entry_match_other_esa,
+                                                                (void**)&entry, &esa_id, &reqid);
+       if (res == SUCCESS && entry)
+       {
+               id = entry->esa_id;
+               DBG3(DBG_KNL, "returning ESA id %llu of other SAD entry with reqid %u",
+                        id, reqid);
+       }
+       this->mutex->unlock(this->mutex);
+       return id;
+}
+
 METHOD(tkm_kernel_sad_t, get_dst_host, host_t *,
        private_tkm_kernel_sad_t * const this, const u_int32_t reqid,
        const u_int32_t spi, const u_int8_t proto)
@@ -289,6 +345,7 @@ tkm_kernel_sad_t *tkm_kernel_sad_create()
                .public = {
                        .insert = _insert,
                        .get_esa_id = _get_esa_id,
+                       .get_other_esa_id = _get_other_esa_id,
                        .get_dst_host = _get_dst_host,
                        .remove = __remove,
                        .destroy = _destroy,
index 38b19dd01bc857357e24a3b3e381db8260064415..fd4830805289f07fc90e71ade22c2336a65b2f98 100644 (file)
@@ -62,6 +62,16 @@ struct tkm_kernel_sad_t {
                                 const host_t * const src, const host_t * const dst,
                                 const u_int32_t spi, const u_int8_t proto);
 
+       /**
+        * Get ESA id for entry associated with same security policy as the
+        * specified ESA.
+        *
+        * @param esa_id                id of ESA identifying the security policy
+        * @return                              ESA id of entry if found, 0 otherwise
+        */
+       esa_id_type (*get_other_esa_id)(tkm_kernel_sad_t * const this,
+                                                                       const esa_id_type esa_id);
+
        /**
         * Get destination host for entry with given parameters.
         *
index b9ab3cb5e926cc9a963a561a8ec4215ef91d77e1..91ccdd4dd239c4b5df6df274025fd2f6cbb86f54 100644 (file)
@@ -81,6 +81,37 @@ START_TEST(test_get_esa_id_nonexistent)
 }
 END_TEST
 
+START_TEST(test_get_other_esa_id)
+{
+       host_t *addr = host_create_from_string("127.0.0.1", 1024);
+       tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
+       fail_unless(sad->insert(sad, 23, 54, addr, addr, 42, 50),
+                               "Error inserting SAD entry");
+       fail_unless(sad->insert(sad, 24, 54, addr, addr, 42, 50),
+                               "Error inserting SAD entry");
+       fail_unless(sad->get_other_esa_id(sad, 23) == 24,
+                               "Error getting other esa id");
+       sad->destroy(sad);
+       addr->destroy(addr);
+}
+END_TEST
+
+START_TEST(test_get_other_esa_id_nonexistent)
+{
+       host_t *addr = host_create_from_string("127.0.0.1", 1024);
+       tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
+       fail_unless(sad->get_other_esa_id(sad, 1) == 0,
+                               "Got other esa id for nonexistent SAD entry");
+       fail_unless(sad->insert(sad, 23, 54, addr, addr, 42, 50),
+                               "Error inserting SAD entry");
+       fail_unless(sad->get_other_esa_id(sad, 23) == 0,
+                               "Got own esa id");
+
+       sad->destroy(sad);
+       addr->destroy(addr);
+}
+END_TEST
+
 START_TEST(test_get_dst_host)
 {
        host_t *addr = host_create_from_string("127.0.0.1", 1024);
@@ -151,6 +182,11 @@ Suite *make_kernel_sad_tests()
        tcase_add_test(tc, test_get_esa_id_nonexistent);
        suite_add_tcase(s, tc);
 
+       tc = tcase_create("get_other_esa_id");
+       tcase_add_test(tc, test_get_other_esa_id);
+       tcase_add_test(tc, test_get_other_esa_id_nonexistent);
+       suite_add_tcase(s, tc);
+
        tc = tcase_create("get_dst_host");
        tcase_add_test(tc, test_get_dst_host);
        tcase_add_test(tc, test_get_dst_host_nonexistent);