[hipl-commit] [trunk] Rev 4004: Moved one function from pfkeyapi.c to pfkeysadb.c because it was borrowed from racoon.

  • From: Miika Komu <miika@xxxxxx>
  • To: hipl-commit@xxxxxxxxxxxxx
  • Date: Thu, 18 Mar 2010 15:02:45 +0200

Committer: Miika Komu <miika@xxxxxx>
Date: 18/03/2010 at 15:02:45
Revision: 4004
Revision-id: miika@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Branch nick: trunk

Log:
  Moved one function from pfkeyapi.c to pfkeysadb.c because it was borrowed 
from racoon.
  All borrowed functionality should be located in lib/tool.

Modified:
  A  lib/tool/pfkeysadb.c
  A  lib/tool/pfkeysadb.h
  M  Makefile.am
  M  hipd/pfkeyapi.c

=== modified file 'Makefile.am'
--- Makefile.am 2010-03-17 18:46:11 +0000
+++ Makefile.am 2010-03-18 13:02:44 +0000
@@ -302,7 +302,9 @@
 DISTCLEANFILES = lib/ipsec/policy_token.h
 
 # don't use libipsec, but xfrmapi
-if !HIP_PFKEY
+if HIP_PFKEY
+lib_tool_libhiptool_la_SOURCES += lib/tool/pfkeysadb.c
+else
 lib_tool_libhiptool_la_SOURCES += lib/tool/xfrmapi.c
 endif # HIPPFKEY
 

=== modified file 'hipd/pfkeyapi.c'
--- hipd/pfkeyapi.c     2010-03-18 12:44:51 +0000
+++ hipd/pfkeyapi.c     2010-03-18 13:02:44 +0000
@@ -22,7 +22,7 @@
 #include "pfkeyapi.h"
 #include "lib/core/hip_udp.h"
 #include "lib/core/keylen.h"
-
+#include "lib/tool/pfkeysadb.h"
 
 // FIXME: This must be turned to BEET when BEET will be supported by pfkey as 
well
 #define HIP_IPSEC_DEFAULT_MODE IPSEC_MODE_BEET
@@ -287,83 +287,6 @@
 }
 
 /**
- * This function fills in policy0 and policylen0 according to the given 
parameters
- * The full implementation can be found in racoon.
- *
- * @param policy0 the IPsec policy
- * @param policylen0 length of the policy
- * @param direction IPSEC_DIR_INBOUND | IPSEC_DIR_OUTBOUND
- * @param src the source address for the policy
- * @param dst the destination address for the policy
- * @param mode the IPsec mode
- * @param cmd add or delete
- */
-static int getsadbpolicy(caddr_t *policy0, int *policylen0, int direction,
-                         struct sockaddr *src, struct sockaddr *dst, u_int 
mode, int cmd)
-{
-    struct sadb_x_policy *xpl;
-    struct sadb_x_ipsecrequest *xisr;
-    caddr_t policy, p;
-    int policylen;
-    int xisrlen, src_len, dst_len;
-
-    HIP_DEBUG("\n");
-    /* get policy buffer size */
-    policylen = sizeof(struct sadb_x_policy);
-    if (cmd != SADB_X_SPDDELETE) {
-        xisrlen    = sizeof(*xisr);
-        xisrlen   += (sysdep_sa_len(src) + sysdep_sa_len(dst));
-        policylen += PFKEY_ALIGN8(xisrlen);
-    }
-
-    /* make policy structure */
-    policy = malloc(policylen);
-    if (!policy) {
-        HIP_ERROR("Cannot allocate memory for policy\n");
-        return -ENOMEM;
-    }
-
-    xpl                        = (struct sadb_x_policy *) policy;
-    xpl->sadb_x_policy_len     = PFKEY_UNIT64(policylen);
-    xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
-    xpl->sadb_x_policy_type    = IPSEC_POLICY_IPSEC;
-    xpl->sadb_x_policy_dir     = direction;
-    xpl->sadb_x_policy_id      = 0;
-
-    //xpl->sadb_x_policy_priority = PRIORITY_DEFAULT;
-
-    if (cmd == SADB_X_SPDDELETE) {
-        goto end;
-    }
-
-    xisr                            = (struct sadb_x_ipsecrequest *) (xpl + 1);
-
-    xisr->sadb_x_ipsecrequest_proto = SADB_SATYPE_ESP;
-    xisr->sadb_x_ipsecrequest_mode  = mode;
-    xisr->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
-    xisr->sadb_x_ipsecrequest_reqid = 0;
-    p                               = (caddr_t) (xisr + 1);
-
-    xisrlen                         = sizeof(*xisr);
-
-    src_len                         = sysdep_sa_len(src);
-    dst_len                         = sysdep_sa_len(dst);
-    xisrlen                        += src_len + dst_len;
-
-    memcpy(p, src, src_len);
-    p                              += src_len;
-
-    memcpy(p, dst, dst_len);
-    p                              += dst_len;
-
-    xisr->sadb_x_ipsecrequest_len   = PFKEY_ALIGN8(xisrlen);
-end:
-    *policy0                        = policy;
-    *policylen0                     = policylen;
-    return 0;
-}
-
-/**
  * modify an IPsec policy using PFKEY
  *
  * @param so the PF_KEY socket

=== added file 'lib/tool/pfkeysadb.c'
--- lib/tool/pfkeysadb.c        1970-01-01 00:00:00 +0000
+++ lib/tool/pfkeysadb.c        2010-03-18 13:02:44 +0000
@@ -0,0 +1,102 @@
+/**
+ * @file
+ *
+ * Distributed under <a href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>
+ *
+ * @brief this file contains IPsec management functionality borrowed from 
Racoon
+ */
+/* required for caddr_t */
+#define _BSD_SOURCE
+
+#include "config.h"
+
+#include <lib/ipsec/pfkeyv2.h>
+#include <linux/ipsec.h>
+
+#include "config.h"
+#include "hipd/pfkeyapi.h"
+#include "lib/ipsec/libpfkey.h"
+#include "lib/core/hip_udp.h"
+#include "lib/core/keylen.h"
+#include "lib/core/debug.h"
+#include "pfkeysadb.h"
+
+#ifdef CONFIG_HIP_PFKEY
+/**
+ * This function fills in policy0 and policylen0 according to the given 
parameters
+ * The full implementation can be found in racoon.
+ *
+ * @param policy0 the IPsec policy
+ * @param policylen0 length of the policy
+ * @param direction IPSEC_DIR_INBOUND | IPSEC_DIR_OUTBOUND
+ * @param src the source address for the policy
+ * @param dst the destination address for the policy
+ * @param mode the IPsec mode
+ * @param cmd add or delete
+ */
+int getsadbpolicy(caddr_t *policy0, int *policylen0, int direction,
+                  struct sockaddr *src, struct sockaddr *dst, u_int mode, int 
cmd)
+{
+    struct sadb_x_policy *xpl;
+    struct sadb_x_ipsecrequest *xisr;
+    caddr_t policy, p;
+    int policylen;
+    int xisrlen, src_len, dst_len;
+
+    HIP_DEBUG("\n");
+    /* get policy buffer size */
+    policylen = sizeof(struct sadb_x_policy);
+    if (cmd != SADB_X_SPDDELETE) {
+        xisrlen    = sizeof(*xisr);
+        xisrlen   += (sysdep_sa_len(src) + sysdep_sa_len(dst));
+        policylen += PFKEY_ALIGN8(xisrlen);
+    }
+
+    /* make policy structure */
+    policy = malloc(policylen);
+    if (!policy) {
+        HIP_ERROR("Cannot allocate memory for policy\n");
+        return -ENOMEM;
+    }
+
+    xpl                        = (struct sadb_x_policy *) policy;
+    xpl->sadb_x_policy_len     = PFKEY_UNIT64(policylen);
+    xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
+    xpl->sadb_x_policy_type    = IPSEC_POLICY_IPSEC;
+    xpl->sadb_x_policy_dir     = direction;
+    xpl->sadb_x_policy_id      = 0;
+
+    //xpl->sadb_x_policy_priority = PRIORITY_DEFAULT;
+
+    if (cmd == SADB_X_SPDDELETE) {
+        goto end;
+    }
+
+    xisr                            = (struct sadb_x_ipsecrequest *) (xpl + 1);
+
+    xisr->sadb_x_ipsecrequest_proto = SADB_SATYPE_ESP;
+    xisr->sadb_x_ipsecrequest_mode  = mode;
+    xisr->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
+    xisr->sadb_x_ipsecrequest_reqid = 0;
+    p                               = (caddr_t) (xisr + 1);
+
+    xisrlen                         = sizeof(*xisr);
+
+    src_len                         = sysdep_sa_len(src);
+    dst_len                         = sysdep_sa_len(dst);
+    xisrlen                        += src_len + dst_len;
+
+    memcpy(p, src, src_len);
+    p                              += src_len;
+
+    memcpy(p, dst, dst_len);
+    p                              += dst_len;
+
+    xisr->sadb_x_ipsecrequest_len   = PFKEY_ALIGN8(xisrlen);
+end:
+    *policy0                        = policy;
+    *policylen0                     = policylen;
+    return 0;
+}
+
+#endif /* CONFIG_HIP_PFKEY */

=== added file 'lib/tool/pfkeysadb.h'
--- lib/tool/pfkeysadb.h        1970-01-01 00:00:00 +0000
+++ lib/tool/pfkeysadb.h        2010-03-18 13:02:44 +0000
@@ -0,0 +1,7 @@
+#ifndef HIP_LIB_TOOL_PFKEYSADB_H
+#define HIP_LIB_TOOL_PFKEYSADB_H
+
+int getsadbpolicy(caddr_t *policy0, int *policylen0, int direction,
+                  struct sockaddr *src, struct sockaddr *dst, u_int mode, int 
cmd);
+
+#endif /* HIP_LIB_TOOL_PFKEYSADB_H */

Other related posts:

  • » [hipl-commit] [trunk] Rev 4004: Moved one function from pfkeyapi.c to pfkeysadb.c because it was borrowed from racoon. - Miika Komu