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." --- v2: - previous 12/28 - use ENOMEM for oom errors, _cleanup_free_ - add comment to code src/dhcp/client.c | 156 ++++++++++++++++++++++++++++++++++++++++++ src/dhcp/protocol.h | 6 ++ src/systemd/sd-dhcp-client.h | 3 + 3 files changed, 165 insertions(+) diff --git a/src/dhcp/client.c b/src/dhcp/client.c index 6e34ba9..d812614 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 "sd-dhcp-client.h" +#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312 + struct DHCPClient { DHCPState state; int index; uint8_t *req_opts; size_t req_opts_size; uint32_t last_addr; + struct ether_addr mac_addr; + uint32_t xid; }; static const uint8_t default_req_opts[] = { @@ -102,6 +108,156 @@ int sd_dhcp_client_set_index(DHCPClient *client, const int interface_index) return 0; } +int sd_dhcp_client_set_mac(DHCPClient *client, const struct ether_addr *addr) +{ + assert_return(client, -EINVAL); + assert_return(client->state == DHCP_STATE_INIT, -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 = htobe32(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; + + /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient + Identifier option is not set */ + 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; + _cleanup_free_ DHCPPacket *discover; + int optlen, len; + uint8_t *opt; + + optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE; + len = sizeof(DHCPPacket) + optlen; + + discover = malloc0(len); + + if (!discover) + return -ENOMEM; + + err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp, + &opt, &optlen); + if (err < 0) + return err; + + if (client->last_addr != INADDR_ANY) { + err = dhcp_option_append(&opt, &optlen, + DHCP_OPTION_REQUESTED_IP_ADDRESS, + 4, &client->last_addr); + if (err < 0) + return err; + } + + err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL); + if (err < 0) + return err; + + discover->ip.version = IPVERSION; + discover->ip.ihl = sizeof(discover->ip) >> 2; + discover->ip.tot_len = htobe16(len); + + discover->ip.protocol = IPPROTO_UDP; + discover->ip.saddr = INADDR_ANY; + discover->ip.daddr = INADDR_BROADCAST; + + discover->udp.source = htobe16(DHCP_PORT_CLIENT); + discover->udp.dest = htobe16(DHCP_PORT_SERVER); + discover->udp.len = htobe16(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); + + return 0; +} + +int sd_dhcp_client_start(DHCPClient *client) +{ + assert_return(client, -EINVAL); + assert_return(client->index >= 0, -EINVAL); + assert_return(client->state == DHCP_STATE_INIT || + client->state == DHCP_STATE_INIT_REBOOT, -EBUSY); + + client->xid = random_u(); + + return client_send_discover(client); +} + DHCPClient *sd_dhcp_client_new(void) { DHCPClient *client; diff --git a/src/dhcp/protocol.h b/src/dhcp/protocol.h index d375467..fa077fb 100644 --- a/src/dhcp/protocol.h +++ b/src/dhcp/protocol.h @@ -55,6 +55,11 @@ struct DHCPPacket { typedef struct DHCPPacket DHCPPacket; +enum { + DHCP_PORT_SERVER = 67, + DHCP_PORT_CLIENT = 68, +}; + enum DHCPState { DHCP_STATE_INIT = 0, DHCP_STATE_SELECTING = 1, @@ -100,5 +105,6 @@ enum { DHCP_OPTION_OVERLOAD = 52, DHCP_OPTION_MESSAGE_TYPE = 53, DHCP_OPTION_PARAMETER_REQUEST_LIST = 55, + DHCP_OPTION_CLIENT_IDENTIFIER = 61, DHCP_OPTION_END = 255, }; diff --git a/src/systemd/sd-dhcp-client.h b/src/systemd/sd-dhcp-client.h index 65caf59..5a45f07 100644 --- a/src/systemd/sd-dhcp-client.h +++ b/src/systemd/sd-dhcp-client.h @@ -22,6 +22,7 @@ ***/ #include <netinet/in.h> +#include <net/ethernet.h> typedef struct DHCPClient DHCPClient; @@ -29,5 +30,7 @@ int sd_dhcp_client_set_request_option(DHCPClient *client, const uint8_t option); int sd_dhcp_client_set_request_address(DHCPClient *client, const struct in_addr *last_address); int sd_dhcp_client_set_index(DHCPClient *client, const int interface_index); +int sd_dhcp_client_set_mac(DHCPClient *client, const struct ether_addr *addr); +int sd_dhcp_client_start(DHCPClient *client); DHCPClient *sd_dhcp_client_new(void); -- 1.7.10.4 _______________________________________________ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel