2 This file is part of systemd.
4 Copyright (C) 2013 Intel Corporation. All rights reserved.
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <net/ethernet.h>
29 #include "dhcp-protocol.h"
30 #include "dhcp-internal.h"
31 #include "sd-dhcp-client.h"
33 #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
40 uint32_t server_address;
45 typedef struct DHCPLease DHCPLease;
47 struct sd_dhcp_client {
50 sd_event_source *timeout_resend;
53 union sockaddr_union link;
54 sd_event_source *receive_message;
58 struct ether_addr mac_addr;
63 sd_event_source *timeout_t1;
64 sd_event_source *timeout_t2;
65 sd_event_source *timeout_expire;
66 sd_dhcp_client_cb_t cb;
71 static const uint8_t default_req_opts[] = {
72 DHCP_OPTION_SUBNET_MASK,
74 DHCP_OPTION_HOST_NAME,
75 DHCP_OPTION_DOMAIN_NAME,
76 DHCP_OPTION_DOMAIN_NAME_SERVER,
77 DHCP_OPTION_NTP_SERVER,
80 int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
83 assert_return(client, -EINVAL);
86 client->userdata = userdata;
91 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option)
95 assert_return(client, -EINVAL);
96 assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
100 case DHCP_OPTION_OVERLOAD:
101 case DHCP_OPTION_MESSAGE_TYPE:
102 case DHCP_OPTION_PARAMETER_REQUEST_LIST:
103 case DHCP_OPTION_END:
110 for (i = 0; i < client->req_opts_size; i++)
111 if (client->req_opts[i] == option)
114 if (!GREEDY_REALLOC(client->req_opts, client->req_opts_size,
115 client->req_opts_size + 1))
118 client->req_opts[client->req_opts_size - 1] = option;
123 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
124 const struct in_addr *last_addr)
126 assert_return(client, -EINVAL);
127 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
130 client->last_addr = last_addr->s_addr;
132 client->last_addr = INADDR_ANY;
137 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index)
139 assert_return(client, -EINVAL);
140 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
141 assert_return(interface_index >= -1, -EINVAL);
143 client->index = interface_index;
148 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
149 const struct ether_addr *addr)
151 assert_return(client, -EINVAL);
152 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
154 memcpy(&client->mac_addr, addr, ETH_ALEN);
159 int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr)
161 assert_return(client, -EINVAL);
162 assert_return(addr, -EINVAL);
164 switch (client->state) {
165 case DHCP_STATE_INIT:
166 case DHCP_STATE_SELECTING:
167 case DHCP_STATE_INIT_REBOOT:
168 case DHCP_STATE_REBOOTING:
169 case DHCP_STATE_REQUESTING:
170 return -EADDRNOTAVAIL;
172 case DHCP_STATE_BOUND:
173 case DHCP_STATE_RENEWING:
174 case DHCP_STATE_REBINDING:
175 addr->s_addr = client->lease->address;
183 int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr)
185 assert_return(client, -EINVAL);
186 assert_return(addr, -EINVAL);
188 switch (client->state) {
189 case DHCP_STATE_INIT:
190 case DHCP_STATE_SELECTING:
191 case DHCP_STATE_INIT_REBOOT:
192 case DHCP_STATE_REBOOTING:
193 case DHCP_STATE_REQUESTING:
194 return -EADDRNOTAVAIL;
196 case DHCP_STATE_BOUND:
197 case DHCP_STATE_RENEWING:
198 case DHCP_STATE_REBINDING:
199 addr->s_addr = client->lease->subnet_mask;
207 int sd_dhcp_client_prefixlen(const struct in_addr *addr)
212 assert_return(addr, -EADDRNOTAVAIL);
214 mask = be32toh(addr->s_addr);
223 int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr)
225 assert_return(client, -EINVAL);
226 assert_return(addr, -EINVAL);
228 switch (client->state) {
229 case DHCP_STATE_INIT:
230 case DHCP_STATE_SELECTING:
231 case DHCP_STATE_INIT_REBOOT:
232 case DHCP_STATE_REBOOTING:
233 case DHCP_STATE_REQUESTING:
234 return -EADDRNOTAVAIL;
236 case DHCP_STATE_BOUND:
237 case DHCP_STATE_RENEWING:
238 case DHCP_STATE_REBINDING:
239 addr->s_addr = client->lease->router;
247 static int client_notify(sd_dhcp_client *client, int event)
250 client->cb(client, event, client->userdata);
255 static int client_stop(sd_dhcp_client *client, int error)
257 assert_return(client, -EINVAL);
258 assert_return(client->state != DHCP_STATE_INIT &&
259 client->state != DHCP_STATE_INIT_REBOOT, -EALREADY);
261 client->receive_message =
262 sd_event_source_unref(client->receive_message);
268 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
270 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
271 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
272 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
276 client_notify(client, error);
278 switch (client->state) {
280 case DHCP_STATE_INIT:
281 case DHCP_STATE_SELECTING:
282 case DHCP_STATE_REQUESTING:
283 case DHCP_STATE_BOUND:
285 client->start_time = 0;
286 client->state = DHCP_STATE_INIT;
289 case DHCP_STATE_INIT_REBOOT:
290 case DHCP_STATE_REBOOTING:
291 case DHCP_STATE_RENEWING:
292 case DHCP_STATE_REBINDING:
299 client->lease = NULL;
305 static int client_packet_init(sd_dhcp_client *client, uint8_t type,
306 DHCPMessage *message, uint16_t secs,
307 uint8_t **opt, size_t *optlen)
312 *opt = (uint8_t *)(message + 1);
318 message->op = BOOTREQUEST;
320 message->hlen = ETHER_ADDR_LEN;
321 message->xid = htobe32(client->xid);
323 /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
324 refuse to issue an DHCP lease if 'secs' is set to zero */
325 message->secs = htobe16(secs);
327 memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
335 err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
340 /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
341 Identifier option is not set */
342 err = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
343 ETH_ALEN, &client->mac_addr);
347 if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
348 err = dhcp_option_append(opt, optlen,
349 DHCP_OPTION_PARAMETER_REQUEST_LIST,
350 client->req_opts_size,
355 /* Some DHCP servers will send bigger DHCP packets than the
356 defined default size unless the Maximum Messge Size option
357 is explicitely set */
358 max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
359 DHCP_CLIENT_MIN_OPTIONS_SIZE);
360 err = dhcp_option_append(opt, optlen,
361 DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
370 static uint16_t client_checksum(void *buf, int len)
380 for (i = 0; i < len / 2 ; i++)
388 return ~((sum & 0xffff) + (sum >> 16));
391 static void client_append_ip_headers(DHCPPacket *packet, uint16_t len)
393 packet->ip.version = IPVERSION;
394 packet->ip.ihl = DHCP_IP_SIZE / 4;
395 packet->ip.tot_len = htobe16(len);
397 packet->ip.protocol = IPPROTO_UDP;
398 packet->ip.saddr = INADDR_ANY;
399 packet->ip.daddr = INADDR_BROADCAST;
401 packet->udp.source = htobe16(DHCP_PORT_CLIENT);
402 packet->udp.dest = htobe16(DHCP_PORT_SERVER);
403 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
405 packet->ip.check = packet->udp.len;
406 packet->udp.check = client_checksum(&packet->ip.ttl, len - 8);
408 packet->ip.ttl = IPDEFTTL;
409 packet->ip.check = 0;
410 packet->ip.check = client_checksum(&packet->ip, DHCP_IP_SIZE);
413 static int client_send_discover(sd_dhcp_client *client, uint16_t secs)
416 _cleanup_free_ DHCPPacket *discover;
420 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
421 len = sizeof(DHCPPacket) + optlen;
423 discover = malloc0(len);
428 err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
429 secs, &opt, &optlen);
433 if (client->last_addr != INADDR_ANY) {
434 err = dhcp_option_append(&opt, &optlen,
435 DHCP_OPTION_REQUESTED_IP_ADDRESS,
436 4, &client->last_addr);
441 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
445 client_append_ip_headers(discover, len);
447 err = dhcp_network_send_raw_socket(client->fd, &client->link,
453 static int client_send_request(sd_dhcp_client *client, uint16_t secs)
455 _cleanup_free_ DHCPPacket *request;
460 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
461 len = DHCP_MESSAGE_SIZE + optlen;
463 request = malloc0(len);
467 err = client_packet_init(client, DHCP_REQUEST, &request->dhcp, secs,
472 if (client->state == DHCP_STATE_REQUESTING) {
473 err = dhcp_option_append(&opt, &optlen,
474 DHCP_OPTION_REQUESTED_IP_ADDRESS,
475 4, &client->lease->address);
479 err = dhcp_option_append(&opt, &optlen,
480 DHCP_OPTION_SERVER_IDENTIFIER,
481 4, &client->lease->server_address);
486 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
490 client_append_ip_headers(request, len);
492 err = dhcp_network_send_raw_socket(client->fd, &client->link,
498 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
501 sd_dhcp_client *client = userdata;
506 secs = (usec - client->start_time) / USEC_PER_SEC;
508 if (client->attempt < 64)
509 client->attempt *= 2;
511 next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC +
512 (random_u() & 0x1fffff);
514 err = sd_event_add_monotonic(client->event, next_timeout,
516 client_timeout_resend, client,
517 &client->timeout_resend);
521 switch (client->state) {
522 case DHCP_STATE_INIT:
523 err = client_send_discover(client, secs);
525 client->state = DHCP_STATE_SELECTING;
528 if (client->attempt >= 64)
534 case DHCP_STATE_SELECTING:
535 err = client_send_discover(client, secs);
536 if (err < 0 && client->attempt >= 64)
541 case DHCP_STATE_REQUESTING:
542 err = client_send_request(client, secs);
543 if (err < 0 && client->attempt >= 64)
546 client->request_sent = usec;
550 case DHCP_STATE_INIT_REBOOT:
551 case DHCP_STATE_REBOOTING:
552 case DHCP_STATE_BOUND:
553 case DHCP_STATE_RENEWING:
554 case DHCP_STATE_REBINDING:
562 client_stop(client, err);
564 /* Errors were dealt with when stopping the client, don't spill
565 errors into the event loop handler */
569 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
572 sd_dhcp_client *client = userdata;
574 client_stop(client, DHCP_EVENT_EXPIRED);
579 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata)
584 static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata)
589 static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
592 DHCPLease *lease = user_data;
597 case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
599 memcpy(&val, option, 4);
600 lease->lifetime = be32toh(val);
605 case DHCP_OPTION_SERVER_IDENTIFIER:
607 memcpy(&lease->server_address, option, 4);
611 case DHCP_OPTION_SUBNET_MASK:
613 memcpy(&lease->subnet_mask, option, 4);
617 case DHCP_OPTION_ROUTER:
619 memcpy(&lease->router, option, 4);
623 case DHCP_OPTION_RENEWAL_T1_TIME:
625 memcpy(&val, option, 4);
626 lease->t1 = be32toh(val);
631 case DHCP_OPTION_REBINDING_T2_TIME:
633 memcpy(&val, option, 4);
634 lease->t2 = be32toh(val);
643 static int client_verify_headers(sd_dhcp_client *client, DHCPPacket *message,
648 if (len < (DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE))
651 hdrlen = message->ip.ihl * 4;
652 if (hdrlen < 20 || hdrlen > len || client_checksum(&message->ip,
656 message->ip.check = message->udp.len;
659 if (hdrlen + be16toh(message->udp.len) > len ||
660 client_checksum(&message->ip.ttl, be16toh(message->udp.len) + 12))
663 if (be16toh(message->udp.source) != DHCP_PORT_SERVER ||
664 be16toh(message->udp.dest) != DHCP_PORT_CLIENT)
667 if (message->dhcp.op != BOOTREPLY)
670 if (be32toh(message->dhcp.xid) != client->xid)
673 if (memcmp(&message->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet,
680 static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
686 err = client_verify_headers(client, offer, len);
690 lease = new0(DHCPLease, 1);
694 len = len - DHCP_IP_UDP_SIZE;
695 if (dhcp_option_parse(&offer->dhcp, len, client_parse_offer,
696 lease) != DHCP_OFFER)
699 lease->address = offer->dhcp.yiaddr;
701 if (lease->address == INADDR_ANY ||
702 lease->server_address == INADDR_ANY ||
703 lease->subnet_mask == INADDR_ANY ||
704 lease->lifetime == 0)
707 client->lease = lease;
717 static int client_receive_ack(sd_dhcp_client *client, DHCPPacket *offer,
723 r = client_verify_headers(client, offer, len);
727 lease = new0(DHCPLease, 1);
731 len = len - DHCP_IP_UDP_SIZE;
732 r = dhcp_option_parse(&offer->dhcp, len, client_parse_offer, lease);
737 lease->address = offer->dhcp.yiaddr;
739 if (lease->address == INADDR_ANY ||
740 lease->server_address == INADDR_ANY ||
741 lease->subnet_mask == INADDR_ANY || lease->lifetime == 0) {
746 r = DHCP_EVENT_IP_ACQUIRE;
748 if (client->lease->address != lease->address ||
749 client->lease->subnet_mask != lease->subnet_mask ||
750 client->lease->router != lease->router) {
751 r = DHCP_EVENT_IP_CHANGE;
757 client->lease = lease;
767 static uint64_t client_compute_timeout(uint64_t request_sent,
770 return request_sent + (lifetime - 3) * USEC_PER_SEC +
771 + (random_u() & 0x1fffff);
774 static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec)
777 uint64_t next_timeout;
779 if (client->lease->lifetime < 10)
782 if (!client->lease->t1)
783 client->lease->t1 = client->lease->lifetime / 2;
785 next_timeout = client_compute_timeout(client->request_sent,
787 if (next_timeout < usec)
790 err = sd_event_add_monotonic(client->event, next_timeout,
792 client_timeout_t1, client,
793 &client->timeout_t1);
797 if (!client->lease->t2)
798 client->lease->t2 = client->lease->lifetime * 7 / 8;
800 if (client->lease->t2 < client->lease->t1)
803 if (client->lease->lifetime < client->lease->t2)
806 next_timeout = client_compute_timeout(client->request_sent,
808 if (next_timeout < usec)
811 err = sd_event_add_monotonic(client->event, next_timeout,
813 client_timeout_t2, client,
814 &client->timeout_t2);
818 next_timeout = client_compute_timeout(client->request_sent,
819 client->lease->lifetime);
820 if (next_timeout < usec)
823 err = sd_event_add_monotonic(client->event, next_timeout,
825 client_timeout_expire, client,
826 &client->timeout_expire);
833 static int client_receive_raw_message(sd_event_source *s, int fd,
834 uint32_t revents, void *userdata)
836 sd_dhcp_client *client = userdata;
837 uint8_t buf[sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE];
838 int buflen = sizeof(buf);
843 len = read(fd, &buf, buflen);
847 r = sd_event_get_now_monotonic(client->event, &time_now);
851 message = (DHCPPacket *)&buf;
853 switch (client->state) {
854 case DHCP_STATE_SELECTING:
856 if (client_receive_offer(client, message, len) >= 0) {
858 client->timeout_resend =
859 sd_event_source_unref(client->timeout_resend);
861 client->state = DHCP_STATE_REQUESTING;
864 r = sd_event_add_monotonic(client->event, time_now, 0,
865 client_timeout_resend,
867 &client->timeout_resend);
874 case DHCP_STATE_REQUESTING:
876 r = client_receive_ack(client, message, len);
877 if (r == DHCP_EVENT_NO_LEASE)
881 client->timeout_resend =
882 sd_event_source_unref(client->timeout_resend);
884 client->state = DHCP_STATE_BOUND;
887 client->last_addr = client->lease->address;
889 r = client_set_lease_timeouts(client, time_now);
893 client_notify(client, DHCP_EVENT_IP_ACQUIRE);
897 client->receive_message =
898 sd_event_source_unref(client->receive_message);
905 case DHCP_STATE_INIT:
906 case DHCP_STATE_INIT_REBOOT:
907 case DHCP_STATE_REBOOTING:
908 case DHCP_STATE_BOUND:
909 case DHCP_STATE_RENEWING:
910 case DHCP_STATE_REBINDING:
916 if (r < 0 || r == DHCP_EVENT_NO_LEASE)
917 return client_stop(client, r);
922 int sd_dhcp_client_start(sd_dhcp_client *client)
926 assert_return(client, -EINVAL);
927 assert_return(client->index >= 0, -EINVAL);
928 assert_return(client->state == DHCP_STATE_INIT ||
929 client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
931 client->xid = random_u();
933 client->fd = dhcp_network_bind_raw_socket(client->index,
936 if (client->fd < 0) {
941 err = sd_event_add_io(client->event, client->fd, EPOLLIN,
942 client_receive_raw_message, client,
943 &client->receive_message);
947 client->start_time = now(CLOCK_MONOTONIC);
948 err = sd_event_add_monotonic(client->event, client->start_time, 0,
949 client_timeout_resend, client,
950 &client->timeout_resend);
957 client_stop(client, err);
962 int sd_dhcp_client_stop(sd_dhcp_client *client)
964 return client_stop(client, DHCP_EVENT_STOP);
967 sd_dhcp_client *sd_dhcp_client_free(sd_dhcp_client *client)
969 assert_return(client, NULL);
971 sd_dhcp_client_stop(client);
973 sd_event_unref(client->event);
974 free(client->req_opts);
980 sd_dhcp_client *sd_dhcp_client_new(sd_event *event)
982 sd_dhcp_client *client;
984 assert_return(event, NULL);
986 client = new0(sd_dhcp_client, 1);
990 client->event = sd_event_ref(event);
991 client->state = DHCP_STATE_INIT;
996 client->req_opts_size = ELEMENTSOF(default_req_opts);
998 client->req_opts = memdup(default_req_opts, client->req_opts_size);
999 if (!client->req_opts) {