[systemd-devel] [PATCH 12/28] dhcp: Add DHCP discover sending

Patrik Flykt patrik.flykt at linux.intel.com
Wed Nov 13 13:22:40 PST 2013


On starting the client, use the supplied interface mac address and create
a transaction id. Puzzle together an IP/UDP/DHCP Discover message, compute
checksums and send it out as a raw packet.

Create an additional function that constructs default options common to
all DHCP messages.

Set the DHCP Client ID option as noticed by Grant Erickson in ConnMan
commit b18d9798b3a0ae46ed87d6d2be8d5a474bf3ab1e:

   "Some Internet gateways and Wi-Fi access points are unhappy when the
    DHCPv4 client-id option (61) is missing and will refuse to issue a
    DHCP lease."
---
 src/dhcp/client.c   |  163 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/dhcp/client.h   |    3 +
 src/dhcp/protocol.h |    4 ++
 3 files changed, 170 insertions(+)

diff --git a/src/dhcp/client.c b/src/dhcp/client.c
index 172ddd9..1d4d957 100644
--- a/src/dhcp/client.c
+++ b/src/dhcp/client.c
@@ -21,19 +21,25 @@
 #include <errno.h>
 #include <string.h>
 #include <stdio.h>
+#include <net/ethernet.h>
 
 #include "util.h"
 #include "list.h"
 
 #include "protocol.h"
+#include "internal.h"
 #include "client.h"
 
+#define DHCP_CLIENT_MIN_OPTIONS_SIZE            312
+
 struct DHCPClient {
         DHCPState state;
         int index;
         uint8_t *req_opts;
         int req_opts_size;
         struct in_addr *last_addr;
+        struct ether_addr mac_addr;
+        uint32_t xid;
 };
 
 static uint8_t default_req_opts[] = {
@@ -114,6 +120,163 @@ int dhcp_client_set_index(DHCPClient *client, int interface_index)
         return 0;
 }
 
+int dhcp_client_set_mac(DHCPClient *client, struct ether_addr *addr)
+{
+        if (!client)
+                return -EINVAL;
+
+        if (client->state != DHCP_STATE_INIT)
+                return -EBUSY;
+
+        memcpy(&client->mac_addr, addr, ETH_ALEN);
+
+        return 0;
+}
+
+static int client_packet_init(DHCPClient *client, uint8_t type,
+                              DHCPMessage *message, uint8_t **opt,
+                              int *optlen)
+{
+        int err;
+
+        *opt = (uint8_t *)(message + 1);
+
+        *optlen -= 4;
+        if (*optlen < 0)
+                return -ENOBUFS;
+
+        message->op = BOOTREQUEST;
+        message->htype = 1;
+        message->hlen = ETHER_ADDR_LEN;
+        message->xid = htonl(client->xid);
+
+        memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
+        (*opt)[0] = 0x63;
+        (*opt)[1] = 0x82;
+        (*opt)[2] = 0x53;
+        (*opt)[3] = 0x63;
+
+        *opt += 4;
+
+        err = __dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE,
+                                   1, &type);
+        if (err < 0)
+                return err;
+
+        err = __dhcp_option_append(opt, optlen,
+                                   DHCP_OPTION_CLIENT_IDENTIFIER,
+                                   ETH_ALEN, &client->mac_addr);
+        if (err < 0)
+                return err;
+
+         if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
+                err = __dhcp_option_append(opt, optlen,
+                                           DHCP_OPTION_PARAMETER_REQUEST_LIST,
+                                           client->req_opts_size,
+                                           client->req_opts);
+                if (err < 0)
+                        return err;
+        }
+
+        return 0;
+}
+
+static uint16_t client_checksum(void *buf, int len)
+{
+        uint32_t sum;
+        uint16_t *check;
+        int i;
+        uint8_t *odd;
+
+        sum = 0;
+        check = buf;
+
+        for (i = 0; i < len / 2 ; i++)
+                sum += check[i];
+
+        if (len & 0x01) {
+                odd = buf;
+                sum += odd[len];
+        }
+
+        return ~((sum & 0xffff) + (sum >> 16));
+}
+
+static int client_send_discover(DHCPClient *client)
+{
+        int err = 0;
+        DHCPPacket *discover;
+        int optlen, len;
+        uint8_t *opt;
+
+        optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
+        len = sizeof(DHCPPacket) + optlen;
+
+        discover = malloc0(len);
+
+        if (!discover)
+                return -ENOBUFS;
+
+        err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
+                                 &opt, &optlen);
+        if (err < 0)
+                goto error;
+
+        if (client->last_addr) {
+                err = __dhcp_option_append(&opt, &optlen,
+                                           DHCP_OPTION_REQUESTED_IP_ADDRESS,
+                                           4, &client->last_addr->s_addr);
+                if (err < 0)
+                        goto error;
+        }
+
+        err = __dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
+        if (err < 0)
+                goto error;
+
+        discover->ip.version = IPVERSION;
+        discover->ip.ihl = sizeof(discover->ip) >> 2;
+        discover->ip.tot_len = htons(len);
+
+        discover->ip.protocol = IPPROTO_UDP;
+        discover->ip.saddr = INADDR_ANY;
+        discover->ip.daddr = INADDR_BROADCAST;
+
+        discover->udp.source = htons(DHCP_PORT_CLIENT);
+        discover->udp.dest = htons(DHCP_PORT_SERVER);
+        discover->udp.len = htons(len - sizeof(discover->ip));
+
+        discover->ip.check = discover->udp.len;
+        discover->udp.check = client_checksum(&discover->ip.ttl,
+                                              len - 8);
+
+        discover->ip.ttl = IPDEFTTL;
+        discover->ip.check = 0;
+        discover->ip.check = client_checksum(&discover->ip,
+                                             sizeof(discover->ip));
+
+        err = __dhcp_network_send_raw_packet(client->index, discover, len);
+
+error:
+        free(discover);
+
+        return err;
+}
+
+int dhcp_client_start(DHCPClient *client)
+{
+        if (!client || client->index == -1)
+                return -EINVAL;
+
+        if (client->state != DHCP_STATE_INIT &&
+            client->state != DHCP_STATE_INIT_REBOOT)
+                return -EBUSY;
+
+        client->xid = random_u();
+
+        return client_send_discover(client);
+}
+
 DHCPClient *dhcp_client_new(void)
 {
         DHCPClient *client;
diff --git a/src/dhcp/client.h b/src/dhcp/client.h
index 447c03b..fb7682a 100644
--- a/src/dhcp/client.h
+++ b/src/dhcp/client.h
@@ -22,6 +22,7 @@
 ***/
 
 #include <netinet/in.h>
+#include <net/ethernet.h>
 
 struct DHCPClient;
 typedef struct DHCPClient DHCPClient;
@@ -30,5 +31,7 @@ int dhcp_client_set_request_option(DHCPClient *client, uint8_t option);
 int dhcp_client_set_request_address(DHCPClient *client,
                                     struct in_addr *last_address);
 int dhcp_client_set_index(DHCPClient *client, int interface_index);
+int dhcp_client_set_mac(DHCPClient *client, struct ether_addr *addr);
 
+int dhcp_client_start(DHCPClient *client);
 DHCPClient *dhcp_client_new(void);
diff --git a/src/dhcp/protocol.h b/src/dhcp/protocol.h
index 3fc4baf..a2c5216 100644
--- a/src/dhcp/protocol.h
+++ b/src/dhcp/protocol.h
@@ -52,6 +52,9 @@ struct DHCPPacket {
 
 typedef struct DHCPPacket DHCPPacket;
 
+#define DHCP_PORT_SERVER                        67
+#define DHCP_PORT_CLIENT                        68
+
 enum DHCPState {
         DHCP_STATE_INIT                         = 0,
         DHCP_STATE_SELECTING                    = 1,
@@ -90,4 +93,5 @@ typedef enum DHCPState DHCPState;
 #define DHCP_OPTION_OVERLOAD                    52
 #define DHCP_OPTION_MESSAGE_TYPE                53
 #define DHCP_OPTION_PARAMETER_REQUEST_LIST      55
+#define DHCP_OPTION_CLIENT_IDENTIFIER           61
 #define DHCP_OPTION_END                         255
-- 
1.7.10.4



More information about the systemd-devel mailing list