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=a15866932c0a2bbfcd581ce36cd66ff106a5b66b;hp=d3ba7ae8ede44a53386cd0a0900e95acaf553d2d;hb=3e3d8f7857a7a59d62cc5bd1e4e792dc4d0f40b3;hpb=8c00042c939938818365753023ff2d50f984dec6 diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c index d3ba7ae8e..a15866932 100644 --- a/src/libsystemd-dhcp/dhcp-client.c +++ b/src/libsystemd-dhcp/dhcp-client.c @@ -56,6 +56,7 @@ struct sd_dhcp_client { struct ether_addr mac_addr; uint32_t xid; usec_t start_time; + unsigned int attempt; DHCPLease *lease; }; @@ -136,6 +137,11 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client, return 0; } +static int client_notify(sd_dhcp_client *client, int event) +{ + return 0; +} + static int client_stop(sd_dhcp_client *client, int error) { assert_return(client, -EINVAL); @@ -151,10 +157,13 @@ static int client_stop(sd_dhcp_client *client, int error) client->timeout_resend = sd_event_source_unref(client->timeout_resend); + client->attempt = 1; + switch (client->state) { case DHCP_STATE_INIT: case DHCP_STATE_SELECTING: + case DHCP_STATE_REQUESTING: client->start_time = 0; client->state = DHCP_STATE_INIT; @@ -162,7 +171,6 @@ static int client_stop(sd_dhcp_client *client, int error) 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: @@ -183,6 +191,7 @@ static int client_packet_init(sd_dhcp_client *client, uint8_t type, uint8_t **opt, size_t *optlen) { int err; + be16_t max_size; *opt = (uint8_t *)(message + 1); @@ -226,6 +235,17 @@ static int client_packet_init(sd_dhcp_client *client, uint8_t type, client->req_opts); if (err < 0) return err; + + /* Some DHCP servers will send bigger DHCP packets than the + defined default size unless the Maximum Messge Size option + is explicitely set */ + max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE + + DHCP_CLIENT_MIN_OPTIONS_SIZE); + err = dhcp_option_append(opt, optlen, + DHCP_OPTION_MAXIMUM_MESSAGE_SIZE, + 2, &max_size); + if (err < 0) + return err; } return 0; @@ -252,6 +272,28 @@ static uint16_t client_checksum(void *buf, int len) return ~((sum & 0xffff) + (sum >> 16)); } +static void client_append_ip_headers(DHCPPacket *packet, uint16_t len) +{ + packet->ip.version = IPVERSION; + packet->ip.ihl = DHCP_IP_SIZE / 4; + packet->ip.tot_len = htobe16(len); + + packet->ip.protocol = IPPROTO_UDP; + packet->ip.saddr = INADDR_ANY; + packet->ip.daddr = INADDR_BROADCAST; + + packet->udp.source = htobe16(DHCP_PORT_CLIENT); + packet->udp.dest = htobe16(DHCP_PORT_SERVER); + packet->udp.len = htobe16(len - DHCP_IP_SIZE); + + packet->ip.check = packet->udp.len; + packet->udp.check = client_checksum(&packet->ip.ttl, len - 8); + + packet->ip.ttl = IPDEFTTL; + packet->ip.check = 0; + packet->ip.check = client_checksum(&packet->ip, DHCP_IP_SIZE); +} + static int client_send_discover(sd_dhcp_client *client, uint16_t secs) { int err = 0; @@ -284,29 +326,55 @@ static int client_send_discover(sd_dhcp_client *client, uint16_t secs) if (err < 0) return err; - discover->ip.version = IPVERSION; - discover->ip.ihl = sizeof(discover->ip) >> 2; - discover->ip.tot_len = htobe16(len); + client_append_ip_headers(discover, len); - discover->ip.protocol = IPPROTO_UDP; - discover->ip.saddr = INADDR_ANY; - discover->ip.daddr = INADDR_BROADCAST; + err = dhcp_network_send_raw_socket(client->fd, &client->link, + discover, len); + + return err; +} + +static int client_send_request(sd_dhcp_client *client, uint16_t secs) +{ + _cleanup_free_ DHCPPacket *request; + size_t optlen, len; + int err; + uint8_t *opt; - discover->udp.source = htobe16(DHCP_PORT_CLIENT); - discover->udp.dest = htobe16(DHCP_PORT_SERVER); - discover->udp.len = htobe16(len - sizeof(discover->ip)); + optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE; + len = DHCP_MESSAGE_SIZE + optlen; - discover->ip.check = discover->udp.len; - discover->udp.check = client_checksum(&discover->ip.ttl, - len - 8); + request = malloc0(len); + if (!request) + return -ENOMEM; - discover->ip.ttl = IPDEFTTL; - discover->ip.check = 0; - discover->ip.check = client_checksum(&discover->ip, - sizeof(discover->ip)); + err = client_packet_init(client, DHCP_REQUEST, &request->dhcp, secs, + &opt, &optlen); + if (err < 0) + return err; + + if (client->state == DHCP_STATE_REQUESTING) { + err = dhcp_option_append(&opt, &optlen, + DHCP_OPTION_REQUESTED_IP_ADDRESS, + 4, &client->lease->address); + if (err < 0) + return err; + + err = dhcp_option_append(&opt, &optlen, + DHCP_OPTION_SERVER_IDENTIFIER, + 4, &client->lease->server_address); + if (err < 0) + return err; + } + + err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL); + if (err < 0) + return err; + + client_append_ip_headers(request, len); err = dhcp_network_send_raw_socket(client->fd, &client->link, - discover, len); + request, len); return err; } @@ -319,32 +387,50 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, uint16_t secs; int err = 0; - switch (client->state) { - case DHCP_STATE_INIT: - case DHCP_STATE_SELECTING: + secs = (usec - client->start_time) / USEC_PER_SEC; - if (!client->start_time) - client->start_time = usec; + if (client->attempt < 64) + client->attempt *= 2; - secs = (usec - client->start_time) / USEC_PER_SEC; + next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC + + (random_u() & 0x1fffff); - next_timeout = usec + 2 * USEC_PER_SEC + (random() & 0x1fffff); + err = sd_event_add_monotonic(client->event, next_timeout, + 10 * USEC_PER_MSEC, + client_timeout_resend, client, + &client->timeout_resend); + if (err < 0) + goto error; - err = sd_event_add_monotonic(client->event, next_timeout, - 10 * USEC_PER_MSEC, - client_timeout_resend, client, - &client->timeout_resend); - if (err < 0) + switch (client->state) { + case DHCP_STATE_INIT: + err = client_send_discover(client, secs); + if (err >= 0) { + client->state = DHCP_STATE_SELECTING; + client->attempt = 1; + } else { + if (client->attempt >= 64) + goto error; + } + + break; + + case DHCP_STATE_SELECTING: + err = client_send_discover(client, secs); + if (err < 0 && client->attempt >= 64) goto error; - if (client_send_discover(client, secs) >= 0) - client->state = DHCP_STATE_SELECTING; + break; + + case DHCP_STATE_REQUESTING: + err = client_send_request(client, secs); + if (err < 0 && client->attempt >= 64) + goto error; break; 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: @@ -400,41 +486,53 @@ static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option, return 0; } -static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer, - size_t len) +static int client_verify_headers(sd_dhcp_client *client, DHCPPacket *message, + size_t len) { size_t 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 = message->ip.ihl * 4; + if (hdrlen < 20 || hdrlen > len || client_checksum(&message->ip, hdrlen)) return -EINVAL; - offer->ip.check = offer->udp.len; - offer->ip.ttl = 0; + message->ip.check = message->udp.len; + message->ip.ttl = 0; - if (hdrlen + be16toh(offer->udp.len) > len || - client_checksum(&offer->ip.ttl, be16toh(offer->udp.len) + 12)) + if (hdrlen + be16toh(message->udp.len) > len || + client_checksum(&message->ip.ttl, be16toh(message->udp.len) + 12)) return -EINVAL; - if (be16toh(offer->udp.source) != DHCP_PORT_SERVER || - be16toh(offer->udp.dest) != DHCP_PORT_CLIENT) + if (be16toh(message->udp.source) != DHCP_PORT_SERVER || + be16toh(message->udp.dest) != DHCP_PORT_CLIENT) return -EINVAL; - if (offer->dhcp.op != BOOTREPLY) + if (message->dhcp.op != BOOTREPLY) return -EINVAL; - if (be32toh(offer->dhcp.xid) != client->xid) + if (be32toh(message->dhcp.xid) != client->xid) return -EINVAL; - if (memcmp(&offer->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet, + if (memcmp(&message->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet, ETHER_ADDR_LEN)) return -EINVAL; + return 0; +} + +static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer, + size_t len) +{ + int err; + DHCPLease *lease; + + err = client_verify_headers(client, offer, len); + if (err < 0) + return err; + lease = new0(DHCPLease, 1); if (!lease) return -ENOMEM; @@ -462,17 +560,72 @@ error: return -ENOMSG; } +static int client_receive_ack(sd_dhcp_client *client, DHCPPacket *offer, + size_t len) +{ + int r; + DHCPLease *lease; + + r = client_verify_headers(client, offer, len); + if (r < 0) + return r; + + lease = new0(DHCPLease, 1); + if (!lease) + return -ENOBUFS; + + len = len - DHCP_IP_UDP_SIZE; + r = dhcp_option_parse(&offer->dhcp, len, client_parse_offer, lease); + + if (r != DHCP_ACK) + 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) { + r = -ENOMSG; + goto error; + } + + r = DHCP_EVENT_IP_ACQUIRE; + if (client->lease) { + if (client->lease->address != lease->address || + client->lease->subnet_mask != lease->subnet_mask || + client->lease->router != lease->router) { + r = DHCP_EVENT_IP_CHANGE; + } + + free(client->lease); + } + + client->lease = lease; + + return r; + +error: + free(lease); + + return r; +} + static int client_receive_raw_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; + int len, r = 0; DHCPPacket *message; + usec_t time_now; len = read(fd, &buf, buflen); if (len < 0) + return 0; + + r = sd_event_get_now_monotonic(client->event, &time_now); + if (r < 0) goto error; message = (DHCPPacket *)&buf; @@ -482,23 +635,49 @@ static int client_receive_raw_message(sd_event_source *s, int fd, if (client_receive_offer(client, message, len) >= 0) { - client->receive_message = - sd_event_source_unref(client->receive_message); - close(client->fd); - client->fd = -1; - client->timeout_resend = sd_event_source_unref(client->timeout_resend); client->state = DHCP_STATE_REQUESTING; + client->attempt = 1; + + r = sd_event_add_monotonic(client->event, time_now, 0, + client_timeout_resend, + client, + &client->timeout_resend); + if (r < 0) + goto error; } break; + case DHCP_STATE_REQUESTING: + + r = client_receive_ack(client, message, len); + if (r == DHCP_EVENT_NO_LEASE) + goto error; + + if (r >= 0) { + client->timeout_resend = + sd_event_source_unref(client->timeout_resend); + + client->state = DHCP_STATE_BOUND; + client->attempt = 1; + + client->last_addr = client->lease->address; + + client_notify(client, DHCP_EVENT_IP_ACQUIRE); + + close(client->fd); + client->fd = -1; + client->receive_message = + sd_event_source_unref(client->receive_message); + } + 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: @@ -507,6 +686,9 @@ static int client_receive_raw_message(sd_event_source *s, int fd, } error: + if (r < 0) + return client_stop(client, r); + return 0; } @@ -535,7 +717,8 @@ int sd_dhcp_client_start(sd_dhcp_client *client) if (err < 0) goto error; - err = sd_event_add_monotonic(client->event, now(CLOCK_MONOTONIC), 0, + 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) @@ -568,6 +751,7 @@ sd_dhcp_client *sd_dhcp_client_new(sd_event *event) client->state = DHCP_STATE_INIT; client->index = -1; client->fd = -1; + client->attempt = 1; client->req_opts_size = ELEMENTSOF(default_req_opts);