X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flibsystemd-dhcp%2Fdhcp-client.c;h=dbec869816532fc93ebef5e1f199bf0284ae7610;hp=18a6e3c50d725cb93ed06df60cd708267284e9c1;hb=9f9a964f195df68175cd3d5c9c90a476fa474073;hpb=2ed0375c2db786012842f02b84014a70c4806017 diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c index 18a6e3c50..dbec86981 100644 --- a/src/libsystemd-dhcp/dhcp-client.c +++ b/src/libsystemd-dhcp/dhcp-client.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "util.h" #include "list.h" @@ -40,6 +41,7 @@ struct DHCPLease { be32_t server_address; be32_t subnet_mask; be32_t router; + be32_t dns; }; typedef struct DHCPLease DHCPLease; @@ -53,6 +55,7 @@ struct sd_dhcp_client { union sockaddr_union link; sd_event_source *receive_message; uint8_t *req_opts; + size_t req_opts_allocated; size_t req_opts_size; be32_t last_addr; struct ether_addr mac_addr; @@ -77,6 +80,9 @@ static const uint8_t default_req_opts[] = { DHCP_OPTION_NTP_SERVER, }; +static int client_receive_message(sd_event_source *s, int fd, + uint32_t revents, void *userdata); + int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb, void *userdata) { @@ -111,11 +117,11 @@ int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) if (client->req_opts[i] == option) return -EEXIST; - if (!GREEDY_REALLOC(client->req_opts, client->req_opts_size, + if (!GREEDY_REALLOC(client->req_opts, client->req_opts_allocated, client->req_opts_size + 1)) return -ENOMEM; - client->req_opts[client->req_opts_size - 1] = option; + client->req_opts[client->req_opts_size++] = option; return 0; } @@ -204,6 +210,33 @@ int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr) return 0; } +int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr *addr) +{ + assert_return(client, -EINVAL); + assert_return(addr, -EINVAL); + + switch (client->state) { + case DHCP_STATE_INIT: + case DHCP_STATE_SELECTING: + case DHCP_STATE_INIT_REBOOT: + case DHCP_STATE_REBOOTING: + case DHCP_STATE_REQUESTING: + return -EADDRNOTAVAIL; + + case DHCP_STATE_BOUND: + case DHCP_STATE_RENEWING: + case DHCP_STATE_REBINDING: + if (client->lease->dns) + addr->s_addr = client->lease->dns; + else + return -ENOENT; + + break; + } + + return 0; +} + int sd_dhcp_client_prefixlen(const struct in_addr *addr) { int len = 0; @@ -255,8 +288,6 @@ static int client_notify(sd_dhcp_client *client, int event) static int client_stop(sd_dhcp_client *client, int error) { assert_return(client, -EINVAL); - assert_return(client->state != DHCP_STATE_INIT && - client->state != DHCP_STATE_INIT_REBOOT, -EALREADY); client->receive_message = sd_event_source_unref(client->receive_message); @@ -324,6 +355,10 @@ static int client_packet_init(sd_dhcp_client *client, uint8_t type, refuse to issue an DHCP lease if 'secs' is set to zero */ message->secs = htobe16(secs); + if (client->state == DHCP_STATE_RENEWING || + client->state == DHCP_STATE_REBINDING) + message->ciaddr = client->lease->address; + memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN); (*opt)[0] = 0x63; (*opt)[1] = 0x82; @@ -382,10 +417,13 @@ static uint16_t client_checksum(void *buf, int len) if (len & 0x01) { odd = buf; - sum += odd[len]; + sum += odd[len - 1]; } - return ~((sum & 0xffff) + (sum >> 16)); + while (sum >> 16) + sum = (sum & 0xffff) + (sum >> 16); + + return ~sum; } static void client_append_ip_headers(DHCPPacket *packet, uint16_t len) @@ -487,10 +525,17 @@ static int client_send_request(sd_dhcp_client *client, uint16_t secs) if (err < 0) return err; - client_append_ip_headers(request, len); + if (client->state == DHCP_STATE_RENEWING) { + err = dhcp_network_send_udp_socket(client->fd, + client->lease->server_address, + &request->dhcp, + len - DHCP_IP_UDP_SIZE); + } else { + client_append_ip_headers(request, len); - err = dhcp_network_send_raw_socket(client->fd, &client->link, - request, len); + err = dhcp_network_send_raw_socket(client->fd, &client->link, + request, len); + } return err; } @@ -499,17 +544,47 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, void *userdata) { sd_dhcp_client *client = userdata; - usec_t next_timeout; + usec_t next_timeout = 0; + uint32_t time_left; uint16_t secs; int err = 0; - secs = (usec - client->start_time) / USEC_PER_SEC; + switch (client->state) { + case DHCP_STATE_RENEWING: + + time_left = (client->lease->t2 - client->lease->t1)/2; + if (time_left < 60) + time_left = 60; + + next_timeout = usec + time_left * USEC_PER_SEC; + + break; + + case DHCP_STATE_REBINDING: + + time_left = (client->lease->lifetime - client->lease->t2)/2; + if (time_left < 60) + time_left = 60; + + next_timeout = usec + time_left * USEC_PER_SEC; + break; + + case DHCP_STATE_INIT: + case DHCP_STATE_INIT_REBOOT: + case DHCP_STATE_REBOOTING: + case DHCP_STATE_SELECTING: + case DHCP_STATE_REQUESTING: + case DHCP_STATE_BOUND: + + if (client->attempt < 64) + client->attempt *= 2; - if (client->attempt < 64) - client->attempt *= 2; + next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC; - next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC + - (random_u() & 0x1fffff); + break; + } + + next_timeout += (random_u32() & 0x1fffff); err = sd_event_add_monotonic(client->event, next_timeout, 10 * USEC_PER_MSEC, @@ -518,6 +593,8 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, if (err < 0) goto error; + secs = (usec - client->start_time) / USEC_PER_SEC; + switch (client->state) { case DHCP_STATE_INIT: err = client_send_discover(client, secs); @@ -539,6 +616,8 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, break; case DHCP_STATE_REQUESTING: + case DHCP_STATE_RENEWING: + case DHCP_STATE_REBINDING: err = client_send_request(client, secs); if (err < 0 && client->attempt >= 64) goto error; @@ -550,8 +629,6 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, case DHCP_STATE_INIT_REBOOT: case DHCP_STATE_REBOOTING: case DHCP_STATE_BOUND: - case DHCP_STATE_RENEWING: - case DHCP_STATE_REBINDING: break; } @@ -566,6 +643,28 @@ error: return 0; } +static int client_initialize_events(sd_dhcp_client *client, usec_t usec) +{ + int r; + + r = sd_event_add_io(client->event, client->fd, EPOLLIN, + client_receive_message, client, + &client->receive_message); + if (r < 0) + goto error; + + r = sd_event_add_monotonic(client->event, usec, 0, + client_timeout_resend, client, + &client->timeout_resend); + +error: + if (r < 0) + client_stop(client, r); + + return 0; + +} + static int client_timeout_expire(sd_event_source *s, uint64_t usec, void *userdata) { @@ -578,12 +677,48 @@ static int client_timeout_expire(sd_event_source *s, uint64_t usec, static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) { - return 0; + sd_dhcp_client *client = userdata; + int r; + + if (client->fd >= 0) { + client->receive_message = + sd_event_source_unref(client->receive_message); + close(client->fd); + client->fd = -1; + } + + client->state = DHCP_STATE_REBINDING; + client->attempt = 1; + + r = dhcp_network_bind_raw_socket(client->index, &client->link); + if (r < 0) { + client_stop(client, r); + return 0; + } + + client->fd = r; + + return client_initialize_events(client, usec); } static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata) { - return 0; + sd_dhcp_client *client = userdata; + int r; + + client->state = DHCP_STATE_RENEWING; + client->attempt = 1; + + r = dhcp_network_bind_udp_socket(client->index, + client->lease->address); + if (r < 0) { + client_stop(client, r); + return 0; + } + + client->fd = r; + + return client_initialize_events(client, usec); } static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option, @@ -620,6 +755,12 @@ static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option, break; + case DHCP_OPTION_DOMAIN_NAME_SERVER: + if (len >= 4) + memcpy(&lease->dns, option, 4); + + break; + case DHCP_OPTION_RENEWAL_T1_TIME: if (len == 4) { memcpy(&val, option, 4); @@ -714,27 +855,44 @@ error: return -ENOMSG; } -static int client_receive_ack(sd_dhcp_client *client, DHCPPacket *offer, +static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf, size_t len) { int r; + DHCPPacket *ack; + DHCPMessage *dhcp; DHCPLease *lease; - r = client_verify_headers(client, offer, len); - if (r < 0) - return r; + if (client->state == DHCP_STATE_RENEWING) { + dhcp = (DHCPMessage *)buf; + } else { + ack = (DHCPPacket *)buf; + + r = client_verify_headers(client, ack, len); + if (r < 0) + return r; + + dhcp = &ack->dhcp; + len -= DHCP_IP_UDP_SIZE; + } lease = new0(DHCPLease, 1); if (!lease) return -ENOMEM; - len = len - DHCP_IP_UDP_SIZE; - r = dhcp_option_parse(&offer->dhcp, len, client_parse_offer, lease); + r = dhcp_option_parse(dhcp, len, client_parse_offer, lease); - if (r != DHCP_ACK) + if (r == DHCP_NAK) { + r = DHCP_EVENT_NO_LEASE; goto error; + } - lease->address = offer->dhcp.yiaddr; + if (r != DHCP_ACK) { + r = -ENOMSG; + goto error; + } + + lease->address = dhcp->yiaddr; if (lease->address == INADDR_ANY || lease->server_address == INADDR_ANY || @@ -768,7 +926,7 @@ static uint64_t client_compute_timeout(uint64_t request_sent, uint32_t lifetime) { return request_sent + (lifetime - 3) * USEC_PER_SEC + - + (random_u() & 0x1fffff); + + (random_u32() & 0x1fffff); } static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) @@ -779,6 +937,10 @@ static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) if (client->lease->lifetime < 10) return -EINVAL; + client->timeout_t1 = sd_event_source_unref(client->timeout_t1); + client->timeout_t2 = sd_event_source_unref(client->timeout_t2); + client->timeout_expire = sd_event_source_unref(client->timeout_expire); + if (!client->lease->t1) client->lease->t1 = client->lease->lifetime / 2; @@ -830,13 +992,13 @@ static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) return 0; } -static int client_receive_raw_message(sd_event_source *s, int fd, - uint32_t revents, void *userdata) +static int client_receive_message(sd_event_source *s, int fd, + uint32_t revents, void *userdata) { sd_dhcp_client *client = userdata; uint8_t buf[sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE]; int buflen = sizeof(buf); - int len, r = 0; + int len, r = 0, notify_event = 0; DHCPPacket *message; usec_t time_now; @@ -848,11 +1010,11 @@ static int client_receive_raw_message(sd_event_source *s, int fd, if (r < 0) goto error; - message = (DHCPPacket *)&buf; - switch (client->state) { case DHCP_STATE_SELECTING: + message = (DHCPPacket *)&buf; + if (client_receive_offer(client, message, len) >= 0) { client->timeout_resend = @@ -872,8 +1034,11 @@ static int client_receive_raw_message(sd_event_source *s, int fd, break; case DHCP_STATE_REQUESTING: + case DHCP_STATE_RENEWING: + case DHCP_STATE_REBINDING: + + r = client_receive_ack(client, buf, len); - r = client_receive_ack(client, message, len); if (r == DHCP_EVENT_NO_LEASE) goto error; @@ -881,16 +1046,22 @@ static int client_receive_raw_message(sd_event_source *s, int fd, client->timeout_resend = sd_event_source_unref(client->timeout_resend); + if (client->state == DHCP_STATE_REQUESTING) + notify_event = DHCP_EVENT_IP_ACQUIRE; + else if (r != DHCP_EVENT_IP_ACQUIRE) + notify_event = r; + client->state = DHCP_STATE_BOUND; client->attempt = 1; client->last_addr = client->lease->address; r = client_set_lease_timeouts(client, time_now); - if (r < 0 ) + if (r < 0) goto error; - client_notify(client, DHCP_EVENT_IP_ACQUIRE); + if (notify_event) + client_notify(client, notify_event); client->receive_message = sd_event_source_unref(client->receive_message); @@ -906,8 +1077,6 @@ static int client_receive_raw_message(sd_event_source *s, int fd, case DHCP_STATE_INIT_REBOOT: case DHCP_STATE_REBOOTING: case DHCP_STATE_BOUND: - case DHCP_STATE_RENEWING: - case DHCP_STATE_REBINDING: break; } @@ -921,42 +1090,26 @@ error: int sd_dhcp_client_start(sd_dhcp_client *client) { - int err; + int r; assert_return(client, -EINVAL); - assert_return(client->index >= 0, -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(); + client->xid = random_u32(); - client->fd = dhcp_network_bind_raw_socket(client->index, - &client->link); + r = dhcp_network_bind_raw_socket(client->index, &client->link); - if (client->fd < 0) { - err = client->fd; - goto error; + if (r < 0) { + client_stop(client, r); + return r; } - err = sd_event_add_io(client->event, client->fd, EPOLLIN, - client_receive_raw_message, client, - &client->receive_message); - if (err < 0) - goto error; - + client->fd = r; client->start_time = now(CLOCK_MONOTONIC); - err = sd_event_add_monotonic(client->event, client->start_time, 0, - client_timeout_resend, client, - &client->timeout_resend); - if (err < 0) - goto error; - - return 0; -error: - client_stop(client, err); - - return err; + return client_initialize_events(client, client->start_time); } int sd_dhcp_client_stop(sd_dhcp_client *client)