(no commit message)
[strongswan.git] / Source / charon / threads / kernel_interface.c
index 54fe4d1..4fba85c 100644 (file)
@@ -8,6 +8,9 @@
 /*
  * Copyright (C) 2005 Jan Hutter, Martin Willi
  * Hochschule fuer Technik Rapperswil
+ * Copyright (C) 2003 Herbert Xu.
+ * 
+ * Contains modified parts from pluto.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -39,6 +42,9 @@
 
 typedef struct netlink_message_t netlink_message_t;
 
+/**
+ * Representation of ANY netlink message used
+ */
 struct netlink_message_t {
        
        /**
@@ -58,6 +64,9 @@ struct netlink_message_t {
 
 typedef struct netlink_algo_t netlink_algo_t;
 
+/**
+ * Add length and type to xfrm_algo
+ */
 struct netlink_algo_t {
        u_int16_t length;
        u_int16_t type;
@@ -80,7 +89,6 @@ struct private_kernel_interface_t {
         * Netlink communication socket.
         */
        int socket;
-       int bc_socket;
 
        pid_t pid;
        /**
@@ -107,7 +115,6 @@ struct private_kernel_interface_t {
         * Condvar allows signaling of threads waiting for a reply.
         */
        pthread_cond_t condvar;
-       logger_t *logger;
        
        /**
         * Function for the thread, receives messages.
@@ -115,7 +122,7 @@ struct private_kernel_interface_t {
        void (*receive_messages) (private_kernel_interface_t *this);
        
        /**
-        * Sends a netlink_message_t down to the kernel.
+        * Sends a netlink_message_t down to the kernel and wait for reply.
         */
        status_t (*send_message) (private_kernel_interface_t *this, netlink_message_t *request, netlink_message_t **response);
 };
@@ -123,11 +130,11 @@ struct private_kernel_interface_t {
 mapping_t kernel_encryption_algs_m[] = {
        {ENCR_DES_IV64, ""},
        {ENCR_DES, "des"},
-       {ENCR_3DES, "3des"},
+       {ENCR_3DES, "des3_ede"},
        {ENCR_RC5, ""},
-       {ENCR_IDEA, ""},
-       {ENCR_CAST, ""},
-       {ENCR_BLOWFISH, ""},
+       {ENCR_IDEA, "idea"},
+       {ENCR_CAST, "cast128"},
+       {ENCR_BLOWFISH, "blowfish"},
        {ENCR_3IDEA, ""},
        {ENCR_DES_IV32, ""},
        {ENCR_NULL, ""},
@@ -188,21 +195,19 @@ static status_t get_spi(private_kernel_interface_t *this, host_t *src, host_t *d
 }
 
 static status_t add_sa(        private_kernel_interface_t *this,
-                                               host_t *me, 
-                                               host_t *other, 
-                                               u_int32_t spi, 
-                                               int protocol, 
+                                               host_t *me,
+                                               host_t *other,
+                                               u_int32_t spi,
+                                               int protocol,
                                                bool tunnel_mode,
                                                encryption_algorithm_t enc_alg,
-                                               size_t enc_size,
-                                               chunk_t enc_key,
+                                               chunk_t encryption_key,
                                                integrity_algorithm_t int_alg,
-                                               size_t int_size,
-                                               chunk_t int_key,
+                                               chunk_t integrity_key,
                                                bool replace)
 {
     netlink_message_t request, *response;
-    
+       POS;
     memset(&request, 0, sizeof(request));
     
     request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
@@ -229,11 +234,11 @@ static status_t add_sa(   private_kernel_interface_t *this,
                netlink_algo_t *nla = (netlink_algo_t*)(((u_int8_t*)&request) + request.hdr.nlmsg_len);
        
        nla->type = XFRMA_ALG_CRYPT;
-       nla->length = sizeof(netlink_algo_t) + enc_size;
-               nla->algo.alg_key_len = enc_size * 8;
+               nla->length = sizeof(netlink_algo_t) + encryption_key.len;
+               nla->algo.alg_key_len = encryption_key.len * 8;
                
                strcpy(nla->algo.alg_name, mapping_find(kernel_encryption_algs_m, enc_alg));
-               memcpy(nla->algo.alg_key, enc_key.ptr, enc_key.len);
+               memcpy(nla->algo.alg_key, encryption_key.ptr, encryption_key.len);
 
                request.hdr.nlmsg_len += nla->length;
     }
@@ -243,10 +248,10 @@ static status_t add_sa(   private_kernel_interface_t *this,
                netlink_algo_t *nla = (netlink_algo_t*)(((u_int8_t*)&request) + request.hdr.nlmsg_len);
                
                nla->type = XFRMA_ALG_AUTH;
-       nla->length = sizeof(netlink_algo_t) + int_size;
-               nla->algo.alg_key_len = int_size * 8;
+               nla->length = sizeof(netlink_algo_t) + integrity_key.len;
+               nla->algo.alg_key_len = integrity_key.len * 8;
                strcpy(nla->algo.alg_name, mapping_find(kernel_integrity_algs_m, int_alg));
-               memcpy(nla->algo.alg_key, int_key.ptr, int_key.len);
+               memcpy(nla->algo.alg_key, integrity_key.ptr, integrity_key.len);
 
                request.hdr.nlmsg_len += nla->length;
     }
@@ -258,20 +263,12 @@ static status_t add_sa(   private_kernel_interface_t *this,
        allocator_free(response);
        return FAILED;  
     }
-
-       printf("received message type: %d\n", response->hdr.nlmsg_type);
-       
-       if (response->hdr.nlmsg_type == NLMSG_ERROR)
-       {
-               printf("received error: %s\n", strerror(-response->e.error));
-       }
        
     allocator_free(response);
     return SUCCESS;
 }
 
 
-
 static status_t send_message(private_kernel_interface_t *this, netlink_message_t *request, netlink_message_t **response)
 {
        size_t length;
@@ -311,17 +308,17 @@ static status_t send_message(private_kernel_interface_t *this, netlink_message_t
                        if (listed_response->hdr.nlmsg_seq == request->hdr.nlmsg_seq)
                        {
                                /* matches our request, this is the reply */
-                                       *response = listed_response;
-                                       found = TRUE;
-                                       break;
-                               }
-                       }
-                       iterator->destroy(iterator);
-                       
-                       if (found)
-                       {
-                               break;  
-                       }
+                               *response = listed_response;
+                               found = TRUE;
+                               break;
+                       }
+               }
+               iterator->destroy(iterator);
+               
+               if (found)
+               {
+                       break;  
+               }
                /* we should time out, if something goes wrong */
                pthread_cond_wait(&(this->condvar), &(this->mutex));
        }
@@ -363,14 +360,14 @@ static void receive_messages(private_kernel_interface_t *this)
                        }
                        if (addr.nl_pid != 0)
                        {
-                               /* not interested, try another one */
+                               /* not from kernel. not interested, try another one */
                            continue;
                        }
                        break;
                }
                
                /* got a valid message.
-                * requests are handled on or own, 
+                * requests are handled on our own, 
                 * responses are listed for the requesters
                 */
                if (response.hdr.nlmsg_flags & NLM_F_REQUEST)
@@ -386,7 +383,7 @@ static void receive_messages(private_kernel_interface_t *this)
                        pthread_mutex_lock(&(this->mutex));
                        this->responses->insert_last(this->responses, (void*)listed_response);
                        pthread_mutex_unlock(&(this->mutex));
-                       /* signal LEVEL3 waiting threads */
+                       /* signal ALL waiting threads */
                        pthread_cond_broadcast(&(this->condvar));
                }
                /* get the next one */
@@ -417,7 +414,7 @@ kernel_interface_t *kernel_interface_create()
        /* public functions */
        this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,bool,u_int32_t*))get_spi;
        
-       this->public.add_sa  = (status_t(*)(kernel_interface_t *,host_t*,host_t*,u_int32_t,int,bool,encryption_algorithm_t,size_t,chunk_t,integrity_algorithm_t,size_t,chunk_t,bool))add_sa;
+       this->public.add_sa  = (status_t(*)(kernel_interface_t *,host_t*,host_t*,u_int32_t,int,bool,encryption_algorithm_t,chunk_t,integrity_algorithm_t,chunk_t,bool))add_sa;
        
        
        this->public.destroy = (void(*)(kernel_interface_t*)) destroy;
@@ -444,7 +441,6 @@ kernel_interface_t *kernel_interface_create()
                charon->kill(charon, "Unable to create netlink thread");
        }
        
-       this->logger = charon->logger_manager->create_logger(charon->logger_manager, TESTER, NULL);
        charon->logger_manager->enable_logger_level(charon->logger_manager, TESTER, FULL);
        return (&this->public);
 }