[systemd-devel] [PATCH v2 20/26] dhcp: Handle received DHCP Offer message

Patrik Flykt patrik.flykt at linux.intel.com
Sun Nov 24 23:13:36 PST 2013


Create a function for handling the full IP, UDP and DHCP packet
and tie it to the main loop. Verify IP and UDP headers and checksum.
Creat a new lease structure with using the values supplied in the
DHCP message. Free the lease structure when client is stopped.

Split out socket handling into a creation and a sending part. As a
result modify the test code.
---
v2: - previous 21/28
    - fix unitialized fd to -1
    - use betoh* and friends (also in previous patches) and ENOMEM
    - a DHCPLease is stored for future use if values were obtained and only
      removed on error, thus no _cleanup_free_ here

 src/dhcp/client.c           |  204 ++++++++++++++++++++++++++++++++++++++++++-
 src/dhcp/internal.h         |    7 +-
 src/dhcp/network.c          |   33 ++++---
 src/dhcp/protocol.h         |    6 ++
 src/dhcp/test-dhcp-client.c |   26 +++++-
 5 files changed, 257 insertions(+), 19 deletions(-)

diff --git a/src/dhcp/client.c b/src/dhcp/client.c
index 7af0075..10afb11 100644
--- a/src/dhcp/client.c
+++ b/src/dhcp/client.c
@@ -32,17 +32,31 @@
 
 #define DHCP_CLIENT_MIN_OPTIONS_SIZE            312
 
+struct DHCPLease {
+        uint32_t lifetime;
+        uint32_t address;
+        uint32_t server_address;
+        uint32_t subnet_mask;
+        uint32_t router;
+};
+
+typedef struct DHCPLease DHCPLease;
+
 struct DHCPClient {
         DHCPState state;
         sd_event *event;
         sd_event_source *timeout_resend;
         int index;
+        int fd;
+        union sockaddr_union link;
+        sd_event_source *receive_message;
         uint8_t *req_opts;
         size_t req_opts_size;
         uint32_t last_addr;
         struct ether_addr mac_addr;
         uint32_t xid;
         usec_t start_time;
+        DHCPLease *lease;
 };
 
 static const uint8_t default_req_opts[] = {
@@ -127,6 +141,13 @@ static int client_stop(DHCPClient *client, int error)
         assert_return(client->state != DHCP_STATE_INIT &&
                       client->state != DHCP_STATE_INIT_REBOOT, -EALREADY);
 
+        if (client->fd >= 0)
+                close(client->fd);
+        client->fd = -1;
+
+        client->receive_message =
+                sd_event_source_unref(client->receive_message);
+
         client->timeout_resend = sd_event_source_unref(client->timeout_resend);
 
         switch (client->state) {
@@ -134,6 +155,7 @@ static int client_stop(DHCPClient *client, int error)
         case DHCP_STATE_INIT:
         case DHCP_STATE_SELECTING:
 
+                client->start_time = 0;
                 client->state = DHCP_STATE_INIT;
                 break;
 
@@ -147,6 +169,11 @@ static int client_stop(DHCPClient *client, int error)
                 break;
         }
 
+        if (client->lease) {
+                free(client->lease);
+                client->lease = NULL;
+        }
+
         return 0;
 }
 
@@ -277,9 +304,10 @@ static int client_send_discover(DHCPClient *client, uint16_t secs)
         discover->ip.check = client_checksum(&discover->ip,
                                              sizeof(discover->ip));
 
-        err = dhcp_network_send_raw_packet(client->index, discover, len);
+        err = dhcp_network_send_raw_socket(client->fd, &client->link,
+                                           discover, len);
 
-        return 0;
+        return err;
 }
 
 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
@@ -331,6 +359,163 @@ error:
         return 0;
 }
 
+static int client_parse_offer(uint8_t code, uint8_t len, uint8_t *option,
+                              void *user_data)
+{
+        DHCPLease *lease = user_data;
+        be32_t val;
+
+        switch(code) {
+
+        case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
+                if (len == 4) {
+                        memcpy(&val, option, 4);
+                        lease->lifetime = be32toh(val);
+                }
+
+                break;
+
+        case DHCP_OPTION_SERVER_IDENTIFIER:
+                if (len >= 4)
+                        memcpy(&lease->server_address, option, 4);
+
+                break;
+
+        case DHCP_OPTION_SUBNET_MASK:
+                if (len >= 4)
+                        memcpy(&lease->subnet_mask, option, 4);
+
+                break;
+
+        case DHCP_OPTION_ROUTER:
+                if (len >= 4)
+                        memcpy(&lease->router, option, 4);
+
+                break;
+        }
+
+        return 0;
+}
+
+static int client_receive_offer(DHCPClient *client,
+                                DHCPPacket *offer, int len)
+{
+        int hdrlen;
+        DHCPLease *lease;
+
+        if (len < (DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE))
+                return -EINVAL;
+
+        hdrlen = offer->ip.ihl * 4;
+        if (hdrlen < 20 || hdrlen > len || client_checksum(&offer->ip,
+                                                           hdrlen))
+                return -EINVAL;
+
+        offer->ip.check = offer->udp.len;
+        offer->ip.ttl = 0;
+
+        if (hdrlen + be16toh(offer->udp.len) > len ||
+            client_checksum(&offer->ip.ttl, be16toh(offer->udp.len) + 12))
+                return -EINVAL;
+
+        if (be16toh(offer->udp.source) != DHCP_PORT_SERVER ||
+            be16toh(offer->udp.dest) != DHCP_PORT_CLIENT)
+                return -EINVAL;
+
+        if (offer->dhcp.op != BOOTREPLY)
+                return -EINVAL;
+
+        if (be32toh(offer->dhcp.xid) != client->xid)
+                return -EINVAL;
+
+        if (memcmp(&offer->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet,
+                    ETHER_ADDR_LEN))
+                return -EINVAL;
+
+        lease = new0(DHCPLease, 1);
+        if (!lease)
+                return -ENOMEM;
+
+        len = len - DHCP_IP_UDP_SIZE;
+        if (dhcp_option_parse(&offer->dhcp, len, client_parse_offer,
+                              lease) != DHCP_OFFER)
+                goto error;
+
+        lease->address = offer->dhcp.yiaddr;
+
+        if (lease->address == INADDR_ANY ||
+            lease->server_address == INADDR_ANY ||
+            lease->subnet_mask == INADDR_ANY ||
+            lease->lifetime == 0)
+                goto error;
+
+        client->lease = lease;
+
+        return 0;
+
+error:
+        free(lease);
+
+        return -ENOMSG;
+}
+
+static int client_receive_raw_message(sd_event_source *s, int fd,
+                                      uint32_t revents, void *userdata)
+{
+        DHCPClient *client = userdata;
+        int len, buflen;
+        uint8_t *buf;
+        uint8_t tmp;
+        DHCPPacket *message;
+
+        buflen = sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE;
+        buf = malloc0(buflen);
+        if (!buf) {
+                read(fd, &tmp, 1);
+                return 0;
+        }
+
+        len = read(fd, buf, buflen);
+        if (len < 0)
+                goto error;
+
+        message = (DHCPPacket *)buf;
+
+        switch (client->state) {
+        case DHCP_STATE_SELECTING:
+
+                if (client_receive_offer(client, message, len) >= 0) {
+
+                        close(client->fd);
+                        client->fd = -1;
+                        client->receive_message =
+                                sd_event_source_unref(client->receive_message);
+
+                        client->timeout_resend =
+                                sd_event_source_unref(client->timeout_resend);
+
+                        client->state = DHCP_STATE_REQUESTING;
+                }
+
+                break;
+
+        case DHCP_STATE_INIT:
+        case DHCP_STATE_INIT_REBOOT:
+        case DHCP_STATE_REBOOTING:
+        case DHCP_STATE_REQUESTING:
+        case DHCP_STATE_BOUND:
+        case DHCP_STATE_RENEWING:
+        case DHCP_STATE_REBINDING:
+
+                break;
+        }
+
+error:
+        free(buf);
+
+        return 0;
+}
+
 int sd_dhcp_client_start(DHCPClient *client)
 {
         int err;
@@ -342,6 +527,20 @@ int sd_dhcp_client_start(DHCPClient *client)
 
         client->xid = random_u();
 
+        client->fd = dhcp_network_bind_raw_socket(client->index,
+                                                  &client->link);
+
+        if (client->fd < 0) {
+                err = client->fd;
+                goto error;
+        }
+
+        err = sd_event_add_io(client->event, client->fd, EPOLLIN,
+                              client_receive_raw_message, client,
+                              &client->receive_message);
+        if (err < 0)
+                goto error;
+
         err = sd_event_add_monotonic(client->event, now(CLOCK_MONOTONIC), 0,
                                      client_timeout_resend, client,
                                      &client->timeout_resend);
@@ -374,6 +573,7 @@ DHCPClient *sd_dhcp_client_new(sd_event *event)
         client->event = event;
         client->state = DHCP_STATE_INIT;
         client->index = -1;
+        client->fd = -1;
 
         client->req_opts_size = ELEMENTSOF(default_req_opts);
 
diff --git a/src/dhcp/internal.h b/src/dhcp/internal.h
index 6f414e3..8423ac1 100644
--- a/src/dhcp/internal.h
+++ b/src/dhcp/internal.h
@@ -22,10 +22,15 @@
 ***/
 
 #include <stdint.h>
+#include <linux/if_packet.h>
+
+#include "socket-util.h"
 
 #include "protocol.h"
 
-int dhcp_network_send_raw_packet(int index, void *packet, int len);
+int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link);
+int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
+                                 void *packet, int len);
 
 int dhcp_option_append(uint8_t **buf, int *buflen, uint8_t code,
                        uint8_t optlen, const void *optval);
diff --git a/src/dhcp/network.c b/src/dhcp/network.c
index 3b2d2c3..2faebce 100644
--- a/src/dhcp/network.c
+++ b/src/dhcp/network.c
@@ -30,26 +30,35 @@
 
 #include "internal.h"
 
-int dhcp_network_send_raw_packet(int index, void *packet, int len)
+int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
 {
-        _cleanup_close_ int s;
-        union sockaddr_union link = {};
+        int s;
 
         s = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
         if (s < 0)
                 return -errno;
 
-        link.ll.sll_family = AF_PACKET;
-        link.ll.sll_protocol = htons(ETH_P_IP);
-        link.ll.sll_ifindex =  index;
-        link.ll.sll_halen = ETH_ALEN;
-        memset(&link.ll.sll_addr, 0xff, ETH_ALEN);
+        link->ll.sll_family = AF_PACKET;
+        link->ll.sll_protocol = htons(ETH_P_IP);
+        link->ll.sll_ifindex =  index;
+        link->ll.sll_halen = ETH_ALEN;
+        memset(link->ll.sll_addr, 0xff, ETH_ALEN);
 
-        if (bind(s, &link.sa, sizeof(link.ll)) < 0)
+        if (bind(s, &link->sa, sizeof(link->ll)) < 0) {
+                close(s);
                 return -errno;
+        }
 
-        if (sendto(s, packet, len, 0, &link.sa, sizeof(link.ll)) < 0)
-                return -errno;
+        return s;
+}
+
+int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
+                                 void *packet, int len)
+{
+        int err = 0;
+
+        if (sendto(s, packet, len, 0, &link->sa, sizeof(link->ll)) < 0)
+                err = -errno;
 
-        return 0;
+        return err;
 }
diff --git a/src/dhcp/protocol.h b/src/dhcp/protocol.h
index fa077fb..95c4f4e 100644
--- a/src/dhcp/protocol.h
+++ b/src/dhcp/protocol.h
@@ -55,6 +55,10 @@ struct DHCPPacket {
 
 typedef struct DHCPPacket DHCPPacket;
 
+#define DHCP_IP_SIZE            (int32_t)(sizeof(struct iphdr))
+#define DHCP_IP_UDP_SIZE        (int32_t)(sizeof(struct udphdr) + DHCP_IP_SIZE)
+#define DHCP_MESSAGE_SIZE       (int32_t)(sizeof(DHCPMessage))
+
 enum {
         DHCP_PORT_SERVER                        = 67,
         DHCP_PORT_CLIENT                        = 68,
@@ -102,8 +106,10 @@ enum {
         DHCP_OPTION_DOMAIN_NAME                 = 15,
         DHCP_OPTION_NTP_SERVER                  = 42,
         DHCP_OPTION_REQUESTED_IP_ADDRESS        = 50,
+        DHCP_OPTION_IP_ADDRESS_LEASE_TIME       = 51,
         DHCP_OPTION_OVERLOAD                    = 52,
         DHCP_OPTION_MESSAGE_TYPE                = 53,
+        DHCP_OPTION_SERVER_IDENTIFIER           = 54,
         DHCP_OPTION_PARAMETER_REQUEST_LIST      = 55,
         DHCP_OPTION_CLIENT_IDENTIFIER           = 61,
         DHCP_OPTION_END                         = 255,
diff --git a/src/dhcp/test-dhcp-client.c b/src/dhcp/test-dhcp-client.c
index 5a4715e..8e576d9 100644
--- a/src/dhcp/test-dhcp-client.c
+++ b/src/dhcp/test-dhcp-client.c
@@ -23,8 +23,12 @@
 #include <assert.h>
 #include <errno.h>
 #include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
 
 #include "util.h"
+#include "socket-util.h"
 
 #include "protocol.h"
 #include "internal.h"
@@ -34,6 +38,8 @@ static struct ether_addr mac_addr = {
         .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
 };
 
+static int test_fd[2];
+
 static void test_request_basic(sd_event *e)
 {
         DHCPClient *client;
@@ -125,14 +131,15 @@ static int check_options(uint8_t code, uint8_t len, uint8_t *option,
         return 0;
 }
 
-int dhcp_network_send_raw_packet(int index, void *packet, int len)
+int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
+                                 void *packet, int len)
 {
         int size;
         DHCPPacket *discover;
         uint16_t ip_check, udp_check;
         int res;
 
-        assert(index == 42);
+        assert(s > 0);
         assert(packet);
 
         size = sizeof(DHCPPacket) + 4;
@@ -146,8 +153,8 @@ int dhcp_network_send_raw_packet(int index, void *packet, int len)
         assert(discover->ip.protocol == IPPROTO_UDP);
         assert(discover->ip.saddr == INADDR_ANY);
         assert(discover->ip.daddr == INADDR_BROADCAST);
-        assert(discover->udp.source == ntohs(DHCP_PORT_CLIENT));
-        assert(discover->udp.dest == ntohs(DHCP_PORT_SERVER));
+        assert(discover->udp.source == be16toh(DHCP_PORT_CLIENT));
+        assert(discover->udp.dest == be16toh(DHCP_PORT_SERVER));
 
         ip_check = discover->ip.check;
 
@@ -172,6 +179,14 @@ int dhcp_network_send_raw_packet(int index, void *packet, int len)
         return 575;
 }
 
+int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
+{
+        if (socketpair(AF_UNIX, SOCK_STREAM, 0, test_fd) < 0)
+                return -errno;
+
+        return test_fd[0];
+}
+
 static void test_discover_message(sd_event *e)
 {
         DHCPClient *client;
@@ -188,6 +203,9 @@ static void test_discover_message(sd_event *e)
         res = sd_dhcp_client_start(client);
 
         assert(res == 575 || res == 0);
+
+        close(test_fd[0]);
+        close(test_fd[1]);
 }
 
 int main(int argc, char *argv[])
-- 
1.7.10.4



More information about the systemd-devel mailing list