make DHCP debug messages consistent
[strongswan.git] / src / libcharon / plugins / dhcp / dhcp_socket.c
index 8921228..f5bfe2a 100644 (file)
@@ -123,7 +123,9 @@ typedef enum {
  * Some DHCP options used
  */
 typedef enum {
+       DHCP_DNS_SERVER = 6,
        DHCP_HOST_NAME = 12,
+       DHCP_NBNS_SERVER = 44,
        DHCP_REQUESTED_IP = 50,
        DHCP_MESSAGE_TYPE = 53,
        DHCP_SERVER_ID = 54,
@@ -144,15 +146,6 @@ typedef enum {
        DHCP_RELEASE = 7,
        DHCP_INFORM = 8,
 } dhcp_message_type_t;
-
-/**
- * DHCP parameters in the DHCP_PARAM_REQ_LIST option
- */
-typedef enum {
-       DHCP_ROUTER = 3,
-       DHCP_DNS_SERVER = 6,
-} dhcp_parameter_t;
-
 /**
  * DHCP option encoding, a TLV
  */
@@ -251,13 +244,6 @@ static int prepare_dhcp(private_dhcp_socket_t *this,
        memcpy(option->data, chunk.ptr, option->len);
        optlen += sizeof(dhcp_option_t) + option->len;
 
-       option = (dhcp_option_t*)&dhcp->options[optlen];
-       option->type = DHCP_PARAM_REQ_LIST;
-       option->len = 2;
-       option->data[0] = DHCP_ROUTER;
-       option->data[1] = DHCP_DNS_SERVER;
-       optlen += sizeof(dhcp_option_t) + option->len;
-
        return optlen;
 }
 
@@ -286,6 +272,7 @@ static bool send_dhcp(private_dhcp_socket_t *this,
 static bool discover(private_dhcp_socket_t *this,
                                         dhcp_transaction_t *transaction)
 {
+       dhcp_option_t *option;
        dhcp_t dhcp;
        int optlen;
 
@@ -293,6 +280,13 @@ static bool discover(private_dhcp_socket_t *this,
 
        DBG1(DBG_CFG, "sending DHCP DISCOVER to %H", this->dst);
 
+       option = (dhcp_option_t*)&dhcp.options[optlen];
+       option->type = DHCP_PARAM_REQ_LIST;
+       option->len = 2;
+       option->data[0] = DHCP_DNS_SERVER;
+       option->data[1] = DHCP_NBNS_SERVER;
+       optlen += sizeof(dhcp_option_t) + option->len;
+
        dhcp.options[optlen++] = DHCP_OPTEND;
 
        if (!send_dhcp(this, transaction, &dhcp, optlen))
@@ -339,6 +333,13 @@ static bool request(private_dhcp_socket_t *this,
        memcpy(option->data, chunk.ptr, min(chunk.len, option->len));
        optlen += sizeof(dhcp_option_t) + option->len;
 
+       option = (dhcp_option_t*)&dhcp.options[optlen];
+       option->type = DHCP_PARAM_REQ_LIST;
+       option->len = 2;
+       option->data[0] = DHCP_DNS_SERVER;
+       option->data[1] = DHCP_NBNS_SERVER;
+       optlen += sizeof(dhcp_option_t) + option->len;
+
        dhcp.options[optlen++] = DHCP_OPTEND;
 
        if (!send_dhcp(this, transaction, &dhcp, optlen))
@@ -376,7 +377,7 @@ METHOD(dhcp_socket_t, enroll, dhcp_transaction_t*,
        {       /* no OFFER received */
                this->mutex->unlock(this->mutex);
                transaction->destroy(transaction);
-               DBG1(DBG_CFG, "DHCP disover timed out");
+               DBG1(DBG_CFG, "DHCP DISCOVER timed out");
                return NULL;
        }
 
@@ -394,7 +395,7 @@ METHOD(dhcp_socket_t, enroll, dhcp_transaction_t*,
        {       /* no ACK received */
                this->mutex->unlock(this->mutex);
                transaction->destroy(transaction);
-               DBG1(DBG_CFG, "DHCP request timed out");
+               DBG1(DBG_CFG, "DHCP REQUEST timed out");
                return NULL;
        }
        this->mutex->unlock(this->mutex);
@@ -402,12 +403,50 @@ METHOD(dhcp_socket_t, enroll, dhcp_transaction_t*,
        return transaction;
 }
 
+METHOD(dhcp_socket_t, release, void,
+       private_dhcp_socket_t *this, dhcp_transaction_t *transaction)
+{
+       dhcp_option_t *option;
+       dhcp_t dhcp;
+       host_t *release, *server;
+       chunk_t chunk;
+       int optlen;
+
+       optlen = prepare_dhcp(this, transaction, DHCP_RELEASE, &dhcp);
+
+       release = transaction->get_address(transaction);
+       server = transaction->get_server(transaction);
+       if (!release || !server)
+       {
+               return;
+       }
+       DBG1(DBG_CFG, "sending DHCP RELEASE for %H to %H", release, server);
+
+       chunk = release->get_address(release);
+       memcpy(&dhcp.client_address, chunk.ptr,
+                  min(chunk.len, sizeof(dhcp.client_address)));
+
+       option = (dhcp_option_t*)&dhcp.options[optlen];
+       option->type = DHCP_SERVER_ID;
+       option->len = 4;
+       chunk = server->get_address(server);
+       memcpy(option->data, chunk.ptr, min(chunk.len, option->len));
+       optlen += sizeof(dhcp_option_t) + option->len;
+
+       dhcp.options[optlen++] = DHCP_OPTEND;
+
+       if (!send_dhcp(this, transaction, &dhcp, optlen))
+       {
+               DBG1(DBG_CFG, "sending DHCP RELEASE failed: %s", strerror(errno));
+       }
+}
+
 /**
  * Handle a DHCP OFFER
  */
 static void handle_offer(private_dhcp_socket_t *this, dhcp_t *dhcp, int optlen)
 {
-       dhcp_transaction_t *transaction;
+       dhcp_transaction_t *transaction = NULL;
        enumerator_t *enumerator;
        host_t *offer, *server;
 
@@ -415,7 +454,6 @@ static void handle_offer(private_dhcp_socket_t *this, dhcp_t *dhcp, int optlen)
                                        chunk_from_thing(dhcp->your_address), 0);
        server = host_create_from_chunk(AF_INET,
                                        chunk_from_thing(dhcp->server_address), DHCP_SERVER_PORT);
-       DBG1(DBG_CFG, "received DHCP OFFER %H from %H", offer, server);
 
        this->mutex->lock(this->mutex);
        enumerator = this->discover->create_enumerator(this->discover);
@@ -423,6 +461,7 @@ static void handle_offer(private_dhcp_socket_t *this, dhcp_t *dhcp, int optlen)
        {
                if (transaction->get_id(transaction) == dhcp->transaction_id)
                {
+                       DBG1(DBG_CFG, "received DHCP OFFER %H from %H", offer, server);
                        this->discover->remove_at(this->discover, enumerator);
                        this->request->insert_last(this->request, transaction);
                        transaction->set_address(transaction, offer->clone(offer));
@@ -431,6 +470,34 @@ static void handle_offer(private_dhcp_socket_t *this, dhcp_t *dhcp, int optlen)
                }
        }
        enumerator->destroy(enumerator);
+
+       if (transaction)
+       {
+               int optsize, optpos = 0, pos;
+               dhcp_option_t *option;
+
+               while (optlen > sizeof(dhcp_option_t))
+               {
+                       option = (dhcp_option_t*)&dhcp->options[optpos];
+                       optsize = sizeof(dhcp_option_t) + option->len;
+                       if (option->type == DHCP_OPTEND || optlen < optsize)
+                       {
+                               break;
+                       }
+                       if (option->type == DHCP_DNS_SERVER ||
+                               option->type == DHCP_NBNS_SERVER)
+                       {
+                               for (pos = 0; pos + 4 <= option->len; pos += 4)
+                               {
+                                       transaction->add_attribute(transaction, option->type ==
+                                               DHCP_DNS_SERVER ? INTERNAL_IP4_DNS : INTERNAL_IP4_NBNS,
+                                               chunk_create((char*)&option->data[pos], 4));
+                               }
+                       }
+                       optlen -= optsize;
+                       optpos += optsize;
+               }
+       }
        this->mutex->unlock(this->mutex);
        this->condvar->broadcast(this->condvar);
        offer->destroy(offer);
@@ -448,7 +515,6 @@ static void handle_ack(private_dhcp_socket_t *this, dhcp_t *dhcp, int optlen)
 
        offer = host_create_from_chunk(AF_INET,
                                                chunk_from_thing(dhcp->your_address), 0);
-       DBG1(DBG_CFG, "received DHCP ACK for %H", offer);
 
        this->mutex->lock(this->mutex);
        enumerator = this->request->create_enumerator(this->request);
@@ -456,6 +522,7 @@ static void handle_ack(private_dhcp_socket_t *this, dhcp_t *dhcp, int optlen)
        {
                if (transaction->get_id(transaction) == dhcp->transaction_id)
                {
+                       DBG1(DBG_CFG, "received DHCP ACK for %H", offer);
                        this->request->remove_at(this->request, enumerator);
                        this->completed->insert_last(this->completed, transaction);
                        break;
@@ -565,13 +632,15 @@ dhcp_socket_t *dhcp_socket_create()
        struct sock_filter dhcp_filter_code[] = {
                BPF_STMT(BPF_LD+BPF_B+BPF_ABS,
                                 offsetof(struct iphdr, protocol)),
-               BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_UDP, 0, 14),
+               BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_UDP, 0, 16),
                BPF_STMT(BPF_LD+BPF_H+BPF_ABS, sizeof(struct iphdr) +
                                 offsetof(struct udphdr, source)),
-               BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, DHCP_SERVER_PORT, 0, 12),
+               BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, DHCP_SERVER_PORT, 0, 14),
                BPF_STMT(BPF_LD+BPF_H+BPF_ABS, sizeof(struct iphdr) +
                                 offsetof(struct udphdr, dest)),
-               BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, DHCP_CLIENT_PORT, 0, 10),
+               BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, DHCP_CLIENT_PORT, 0, 2),
+               BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, DHCP_SERVER_PORT, 0, 1),
+               BPF_JUMP(BPF_JMP+BPF_JA, 0, 0, 10),
                BPF_STMT(BPF_LD+BPF_B+BPF_ABS, sizeof(struct iphdr) +
                                 sizeof(struct udphdr) + offsetof(dhcp_t, opcode)),
                BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, BOOTREPLY, 0, 8),
@@ -596,6 +665,7 @@ dhcp_socket_t *dhcp_socket_create()
        INIT(this,
                .public = {
                        .enroll = _enroll,
+                       .release = _release,
                        .destroy = _destroy,
                },
                .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),