- fixed bad bugs in kernel interface
[strongswan.git] / Source / charon / threads / kernel_interface.c
index c42a091..679cf69 100644 (file)
@@ -35,7 +35,6 @@
 #include "kernel_interface.h"
 
 #include <daemon.h>
-#include <utils/allocator.h>
 #include <utils/linked_list.h>
 
 
@@ -124,7 +123,11 @@ struct private_kernel_interface_t {
         */
        int socket;
        
+       /**
+        * Process id of kernel thread
+        */
        pid_t pid;
+       
        /**
         * Sequence number for messages.
         */
@@ -151,6 +154,11 @@ struct private_kernel_interface_t {
        pthread_cond_t condvar;
        
        /**
+        * Logger for XFRM stuff
+        */
+       logger_t *logger;
+       
+       /**
         * Function for the thread, receives messages.
         */
        void (*receive_messages) (private_kernel_interface_t *this);
@@ -197,6 +205,7 @@ mapping_t kernel_integrity_algs_m[] = {
        {MAPPING_END, NULL}
 };
 
+
 /**
  * Implementation of kernel_interface_t.get_spi.
  */
@@ -208,6 +217,9 @@ static status_t get_spi(private_kernel_interface_t *this,
        netlink_message_t request, *response;
        status_t status = SUCCESS;
        
+       
+       this->logger->log(this->logger, CONTROL|LEVEL2, "getting spi");
+       
        memset(&request, 0, sizeof(request));
        request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.spi)));
        request.hdr.nlmsg_flags = NLM_F_REQUEST;
@@ -216,30 +228,37 @@ static status_t get_spi(private_kernel_interface_t *this,
        request.spi.info.id.daddr = dest->get_xfrm_addr(dest);
        request.spi.info.mode = TRUE; /* tunnel mode */
        request.spi.info.reqid = reqid;
-       request.spi.info.id.proto = (protocol == ESP) ? KERNEL_ESP : KERNEL_AH;
+       request.spi.info.id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
        request.spi.info.family = PF_INET;
        request.spi.min = 0xc0000000;
        request.spi.max = 0xcFFFFFFF;
        
        if (this->send_message(this, &request, &response) != SUCCESS)
        {
-               status = FAILED;
+               this->logger->log(this->logger, ERROR, "netlink communication failed");
+               return FAILED;
        }
        else if (response->hdr.nlmsg_type == NLMSG_ERROR)
        {
+               this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_ALLOCSPI got an error: %s",
+                                                 strerror(-response->e.error));
                status = FAILED;
        }
        else if (response->hdr.nlmsg_type != XFRM_MSG_NEWSA)
        {
+               this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_ALLOCSPI got a unknown reply");
                status = FAILED;
        }
        else if (response->hdr.nlmsg_len < NLMSG_LENGTH(sizeof(response->sa)))
        {
+               this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_ALLOCSPI got an invalid reply");
                status = FAILED;
        }
-       
-       *spi = response->sa.id.spi;
-       allocator_free(response);
+       else
+       {
+               *spi = response->sa.id.spi;
+       }
+       free(response);
        
        return status;
 }
@@ -261,7 +280,9 @@ static status_t add_sa(     private_kernel_interface_t *this,
 {
        netlink_message_t request, *response;
        memset(&request, 0, sizeof(request));
-       status_t status;
+       status_t status = SUCCESS;
+       
+       this->logger->log(this->logger, CONTROL|LEVEL2, "adding SA");
        
        request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
        request.hdr.nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
@@ -270,7 +291,7 @@ static status_t add_sa(     private_kernel_interface_t *this,
        request.sa.id.daddr = other->get_xfrm_addr(other);
        
        request.sa.id.spi = spi;
-       request.sa.id.proto = (protocol == ESP) ? KERNEL_ESP : KERNEL_AH;
+       request.sa.id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
        request.sa.family = me->get_family(me);
        request.sa.mode = TRUE; /* tunnel mode */
        request.sa.replay_window = 32;
@@ -318,19 +339,23 @@ static status_t add_sa(   private_kernel_interface_t *this,
        
        if (this->send_message(this, &request, &response) != SUCCESS)
        {
-               status = FAILED;
+               this->logger->log(this->logger, ERROR, "netlink communication failed");
+               return FAILED;
        }
        else if (response->hdr.nlmsg_type != NLMSG_ERROR)
        {
+               this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_NEWSA not acknowledged");
                status = FAILED;
        }
        else if (response->e.error)
        {
+               this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_NEWSA got error %s",
+                                                 strerror(-response->e.error));
                status = FAILED;
        }
        
-       allocator_free(response);
-       return SUCCESS;
+       free(response);
+       return status;
 }
 
 static status_t del_sa(        private_kernel_interface_t *this,
@@ -340,7 +365,9 @@ static status_t del_sa(     private_kernel_interface_t *this,
 {
        netlink_message_t request, *response;
        memset(&request, 0, sizeof(request));
-       status_t status;
+       status_t status = SUCCESS;
+       
+       this->logger->log(this->logger, CONTROL|LEVEL2, "deleting SA");
        
        request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
        request.hdr.nlmsg_type = XFRM_MSG_DELSA;
@@ -348,14 +375,14 @@ static status_t del_sa(   private_kernel_interface_t *this,
        request.sa_id.daddr = dst->get_xfrm_addr(dst);
        
        request.sa_id.spi = spi;
-       request.sa_id.proto = (protocol == ESP) ? KERNEL_ESP : KERNEL_AH;
+       request.sa_id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
        request.sa_id.family = dst->get_family(dst);
        
        request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.sa_id)));
        
        if (this->send_message(this, &request, &response) != SUCCESS)
        {
-               status = FAILED;
+               return FAILED;
        }
        else if (response->hdr.nlmsg_type != NLMSG_ERROR)
        {
@@ -366,8 +393,8 @@ static status_t del_sa(     private_kernel_interface_t *this,
                status = FAILED;
        }
        
-       allocator_free(response);
-       return SUCCESS;
+       free(response);
+       return status;
 }
 
 /**
@@ -384,6 +411,8 @@ static status_t add_policy(private_kernel_interface_t *this,
        netlink_message_t request, *response;
        status_t status = SUCCESS;
        
+       this->logger->log(this->logger, CONTROL|LEVEL2, "adding policy");
+       
        memset(&request, 0, sizeof(request));
        request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
 
@@ -447,18 +476,22 @@ static status_t add_policy(private_kernel_interface_t *this,
        
        if (this->send_message(this, &request, &response) != SUCCESS)
        {
-               status = FAILED;
+               this->logger->log(this->logger, ERROR, "netlink communication failed");
+               return FAILED;
        }
        else if (response->hdr.nlmsg_type != NLMSG_ERROR)
        {
+               this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_NEWPOLICY not acknowledged");
                status = FAILED;
        }
        else if (response->e.error)
        {
+               this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_NEWPOLICY got error %s",
+                                                 strerror(-response->e.error));
                status = FAILED;
        }
        
-       allocator_free(response);
+       free(response);
        return status;
 }
 
@@ -474,6 +507,9 @@ static status_t del_policy(private_kernel_interface_t *this,
        netlink_message_t request, *response;
        status_t status = SUCCESS;
        
+       
+       this->logger->log(this->logger, CONTROL|LEVEL2, "deleting policy");
+       
        memset(&request, 0, sizeof(request));
        request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
 
@@ -495,7 +531,7 @@ static status_t del_policy(private_kernel_interface_t *this,
        
        if (this->send_message(this, &request, &response) != SUCCESS)
        {
-               status = FAILED;
+               return FAILED;
        }
        else if (response->hdr.nlmsg_type != NLMSG_ERROR)
        {
@@ -506,7 +542,7 @@ static status_t del_policy(private_kernel_interface_t *this,
                status = FAILED;
        }
        
-       allocator_free(response);
+       free(response);
        return status;
 }
 
@@ -623,7 +659,7 @@ static void receive_messages(private_kernel_interface_t *this)
                else
                {
                        /* add response to queue */                     
-                       listed_response = allocator_alloc(sizeof(response));
+                       listed_response = malloc(sizeof(response));
                        memcpy(listed_response, &response, sizeof(response));
 
                        pthread_mutex_lock(&(this->mutex));
@@ -645,7 +681,7 @@ static void destroy(private_kernel_interface_t *this)
        pthread_join(this->thread, NULL);
        close(this->socket);
        this->responses->destroy(this->responses);
-       allocator_free(this);
+       free(this);
 }
 
 /*
@@ -653,7 +689,7 @@ static void destroy(private_kernel_interface_t *this)
  */
 kernel_interface_t *kernel_interface_create()
 {
-       private_kernel_interface_t *this = allocator_alloc_thing(private_kernel_interface_t);
+       private_kernel_interface_t *this = malloc_thing(private_kernel_interface_t);
        
        /* public functions */
        this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,u_int32_t,u_int32_t*))get_spi;
@@ -669,23 +705,25 @@ kernel_interface_t *kernel_interface_create()
        this->send_message = send_message;
        this->pid = getpid();
        this->responses = linked_list_create();
+       this->logger = logger_manager->get_logger(logger_manager, XFRM);
        pthread_mutex_init(&(this->mutex),NULL);
        pthread_cond_init(&(this->condvar),NULL);
        this->seq = 0;
        this->socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_XFRM);
        if (this->socket <= 0)
        {
-               allocator_free(this);
+               this->responses->destroy(this->responses);
+               free(this);
                charon->kill(charon, "Unable to create netlink socket");        
        }
        
        if (pthread_create(&(this->thread), NULL, (void*(*)(void*))this->receive_messages, this) != 0)
        {
+               this->responses->destroy(this->responses);
                close(this->socket);
-               allocator_free(this);
+               free(this);
                charon->kill(charon, "Unable to create netlink thread");
        }
        
-       charon->logger_manager->enable_logger_level(charon->logger_manager, TESTER, FULL);
        return (&this->public);
 }