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>
25 #include <sys/param.h>
30 #include "dhcp-protocol.h"
31 #include "dhcp-lease.h"
32 #include "dhcp-internal.h"
33 #include "sd-dhcp-client.h"
35 #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
37 struct sd_dhcp_client {
41 sd_event_source *timeout_resend;
44 union sockaddr_union link;
45 sd_event_source *receive_message;
47 size_t req_opts_allocated;
50 struct ether_addr mac_addr;
56 sd_event_source *timeout_t1;
57 sd_event_source *timeout_t2;
58 sd_event_source *timeout_expire;
59 sd_dhcp_client_cb_t cb;
64 static const uint8_t default_req_opts[] = {
65 DHCP_OPTION_SUBNET_MASK,
67 DHCP_OPTION_HOST_NAME,
68 DHCP_OPTION_DOMAIN_NAME,
69 DHCP_OPTION_DOMAIN_NAME_SERVER,
70 DHCP_OPTION_NTP_SERVER,
73 static int client_receive_message(sd_event_source *s, int fd,
74 uint32_t revents, void *userdata);
76 int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
78 assert_return(client, -EINVAL);
81 client->userdata = userdata;
86 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) {
89 assert_return(client, -EINVAL);
90 assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
94 case DHCP_OPTION_OVERLOAD:
95 case DHCP_OPTION_MESSAGE_TYPE:
96 case DHCP_OPTION_PARAMETER_REQUEST_LIST:
104 for (i = 0; i < client->req_opts_size; i++)
105 if (client->req_opts[i] == option)
108 if (!GREEDY_REALLOC(client->req_opts, client->req_opts_allocated,
109 client->req_opts_size + 1))
112 client->req_opts[client->req_opts_size++] = option;
117 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
118 const struct in_addr *last_addr) {
119 assert_return(client, -EINVAL);
120 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
123 client->last_addr = last_addr->s_addr;
125 client->last_addr = INADDR_ANY;
130 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) {
131 assert_return(client, -EINVAL);
132 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
133 assert_return(interface_index >= -1, -EINVAL);
135 client->index = interface_index;
140 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
141 const struct ether_addr *addr) {
142 assert_return(client, -EINVAL);
143 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
145 memcpy(&client->mac_addr, addr, ETH_ALEN);
150 int sd_dhcp_client_get_lease(sd_dhcp_client *client, sd_dhcp_lease **ret) {
151 assert_return(client, -EINVAL);
152 assert_return(ret, -EINVAL);
154 if (client->state != DHCP_STATE_BOUND &&
155 client->state != DHCP_STATE_RENEWING &&
156 client->state != DHCP_STATE_REBINDING)
157 return -EADDRNOTAVAIL;
159 *ret = sd_dhcp_lease_ref(client->lease);
164 static int client_notify(sd_dhcp_client *client, int event) {
166 client->cb(client, event, client->userdata);
171 static int client_stop(sd_dhcp_client *client, int error) {
172 assert_return(client, -EINVAL);
174 client->receive_message =
175 sd_event_source_unref(client->receive_message);
181 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
183 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
184 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
185 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
189 client_notify(client, error);
191 client->start_time = 0;
193 client->state = DHCP_STATE_INIT;
196 client->lease = sd_dhcp_lease_unref(client->lease);
201 static int client_packet_init(sd_dhcp_client *client, uint8_t type,
202 DHCPMessage *message, uint16_t secs,
203 uint8_t **opt, size_t *optlen) {
207 *opt = (uint8_t *)(message + 1);
213 message->op = BOOTREQUEST;
215 message->hlen = ETHER_ADDR_LEN;
216 message->xid = htobe32(client->xid);
218 /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
219 refuse to issue an DHCP lease if 'secs' is set to zero */
220 message->secs = htobe16(secs);
222 if (client->state == DHCP_STATE_RENEWING ||
223 client->state == DHCP_STATE_REBINDING)
224 message->ciaddr = client->lease->address;
226 memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
234 err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
239 /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
240 Identifier option is not set */
241 err = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
242 ETH_ALEN, &client->mac_addr);
246 if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
247 err = dhcp_option_append(opt, optlen,
248 DHCP_OPTION_PARAMETER_REQUEST_LIST,
249 client->req_opts_size,
254 /* Some DHCP servers will send bigger DHCP packets than the
255 defined default size unless the Maximum Messge Size option
256 is explicitely set */
257 max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
258 DHCP_CLIENT_MIN_OPTIONS_SIZE);
259 err = dhcp_option_append(opt, optlen,
260 DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
269 static uint16_t client_checksum(void *buf, int len) {
278 for (i = 0; i < len / 2 ; i++)
287 sum = (sum & 0xffff) + (sum >> 16);
292 static void client_append_ip_headers(DHCPPacket *packet, uint16_t len) {
293 packet->ip.version = IPVERSION;
294 packet->ip.ihl = DHCP_IP_SIZE / 4;
295 packet->ip.tot_len = htobe16(len);
297 packet->ip.protocol = IPPROTO_UDP;
298 packet->ip.saddr = INADDR_ANY;
299 packet->ip.daddr = INADDR_BROADCAST;
301 packet->udp.source = htobe16(DHCP_PORT_CLIENT);
302 packet->udp.dest = htobe16(DHCP_PORT_SERVER);
303 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
305 packet->ip.check = packet->udp.len;
306 packet->udp.check = client_checksum(&packet->ip.ttl, len - 8);
308 packet->ip.ttl = IPDEFTTL;
309 packet->ip.check = 0;
310 packet->ip.check = client_checksum(&packet->ip, DHCP_IP_SIZE);
313 static int client_send_discover(sd_dhcp_client *client, uint16_t secs) {
315 _cleanup_free_ DHCPPacket *discover;
319 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
320 len = sizeof(DHCPPacket) + optlen;
322 discover = malloc0(len);
327 err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
328 secs, &opt, &optlen);
332 if (client->last_addr != INADDR_ANY) {
333 err = dhcp_option_append(&opt, &optlen,
334 DHCP_OPTION_REQUESTED_IP_ADDRESS,
335 4, &client->last_addr);
340 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
344 client_append_ip_headers(discover, len);
346 err = dhcp_network_send_raw_socket(client->fd, &client->link,
352 static int client_send_request(sd_dhcp_client *client, uint16_t secs) {
353 _cleanup_free_ DHCPPacket *request;
358 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
359 len = DHCP_MESSAGE_SIZE + optlen;
361 request = malloc0(len);
365 err = client_packet_init(client, DHCP_REQUEST, &request->dhcp, secs,
370 if (client->state == DHCP_STATE_REQUESTING) {
371 err = dhcp_option_append(&opt, &optlen,
372 DHCP_OPTION_REQUESTED_IP_ADDRESS,
373 4, &client->lease->address);
377 err = dhcp_option_append(&opt, &optlen,
378 DHCP_OPTION_SERVER_IDENTIFIER,
379 4, &client->lease->server_address);
384 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
388 if (client->state == DHCP_STATE_RENEWING) {
389 err = dhcp_network_send_udp_socket(client->fd,
390 client->lease->server_address,
392 len - DHCP_IP_UDP_SIZE);
394 client_append_ip_headers(request, len);
396 err = dhcp_network_send_raw_socket(client->fd, &client->link,
403 static uint16_t client_update_secs(sd_dhcp_client *client, usec_t time_now)
405 client->secs = (time_now - client->start_time) / USEC_PER_SEC;
410 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
412 sd_dhcp_client *client = userdata;
413 usec_t next_timeout = 0;
419 assert(client->event);
421 switch (client->state) {
422 case DHCP_STATE_RENEWING:
424 time_left = (client->lease->t2 - client->lease->t1)/2;
428 next_timeout = usec + time_left * USEC_PER_SEC;
432 case DHCP_STATE_REBINDING:
434 time_left = (client->lease->lifetime - client->lease->t2)/2;
438 next_timeout = usec + time_left * USEC_PER_SEC;
441 case DHCP_STATE_INIT:
442 case DHCP_STATE_INIT_REBOOT:
443 case DHCP_STATE_REBOOTING:
444 case DHCP_STATE_SELECTING:
445 case DHCP_STATE_REQUESTING:
446 case DHCP_STATE_BOUND:
448 if (client->attempt < 64)
449 client->attempt *= 2;
451 next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC;
456 next_timeout += (random_u32() & 0x1fffff);
458 r = sd_event_add_monotonic(client->event, next_timeout,
460 client_timeout_resend, client,
461 &client->timeout_resend);
465 r = sd_event_source_set_priority(client->timeout_resend, client->event_priority);
469 switch (client->state) {
470 case DHCP_STATE_INIT:
472 client_update_secs(client, usec);
474 r = client_send_discover(client, client->secs);
476 client->state = DHCP_STATE_SELECTING;
479 if (client->attempt >= 64)
485 case DHCP_STATE_SELECTING:
486 client_update_secs(client, usec);
488 r = client_send_discover(client, client->secs);
489 if (r < 0 && client->attempt >= 64)
494 case DHCP_STATE_REQUESTING:
495 case DHCP_STATE_RENEWING:
496 case DHCP_STATE_REBINDING:
497 r = client_send_request(client, client->secs);
498 if (r < 0 && client->attempt >= 64)
501 client->request_sent = usec;
505 case DHCP_STATE_INIT_REBOOT:
506 case DHCP_STATE_REBOOTING:
507 case DHCP_STATE_BOUND:
515 client_stop(client, r);
517 /* Errors were dealt with when stopping the client, don't spill
518 errors into the event loop handler */
522 static int client_initialize_events(sd_dhcp_client *client, usec_t usec) {
526 assert(client->event);
528 r = sd_event_add_io(client->event, client->fd, EPOLLIN,
529 client_receive_message, client,
530 &client->receive_message);
534 r = sd_event_source_set_priority(client->receive_message, client->event_priority);
538 r = sd_event_add_monotonic(client->event, usec, 0,
539 client_timeout_resend, client,
540 &client->timeout_resend);
544 r = sd_event_source_set_priority(client->timeout_resend, client->event_priority);
548 client_stop(client, r);
554 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
556 sd_dhcp_client *client = userdata;
558 client_stop(client, DHCP_EVENT_EXPIRED);
563 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) {
564 sd_dhcp_client *client = userdata;
567 if (client->fd >= 0) {
568 client->receive_message =
569 sd_event_source_unref(client->receive_message);
574 client->state = DHCP_STATE_REBINDING;
577 r = dhcp_network_bind_raw_socket(client->index, &client->link);
579 client_stop(client, r);
585 return client_initialize_events(client, usec);
588 static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata) {
589 sd_dhcp_client *client = userdata;
592 client->state = DHCP_STATE_RENEWING;
595 r = dhcp_network_bind_udp_socket(client->index,
596 client->lease->address);
598 client_stop(client, r);
604 return client_initialize_events(client, usec);
607 static int client_verify_headers(sd_dhcp_client *client, DHCPPacket *message,
611 if (len < (DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE))
614 hdrlen = message->ip.ihl * 4;
615 if (hdrlen < 20 || hdrlen > len || client_checksum(&message->ip,
619 if (hdrlen + be16toh(message->udp.len) > len)
622 if (message->udp.check) {
623 message->ip.check = message->udp.len;
626 if (client_checksum(&message->ip.ttl,
627 be16toh(message->udp.len) + 12))
631 if (be16toh(message->udp.source) != DHCP_PORT_SERVER ||
632 be16toh(message->udp.dest) != DHCP_PORT_CLIENT)
635 if (message->dhcp.op != BOOTREPLY)
638 if (be32toh(message->dhcp.xid) != client->xid)
641 if (memcmp(&message->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet,
648 static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
650 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
653 r = client_verify_headers(client, offer, len);
657 r = dhcp_lease_new(&lease);
661 len = len - DHCP_IP_UDP_SIZE;
662 r = dhcp_option_parse(&offer->dhcp, len, dhcp_lease_parse_options,
667 lease->address = offer->dhcp.yiaddr;
669 if (lease->address == INADDR_ANY ||
670 lease->server_address == INADDR_ANY ||
671 lease->subnet_mask == INADDR_ANY ||
672 lease->lifetime == 0)
675 client->lease = lease;
681 static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf,
685 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
688 if (client->state == DHCP_STATE_RENEWING) {
689 dhcp = (DHCPMessage *)buf;
691 ack = (DHCPPacket *)buf;
693 r = client_verify_headers(client, ack, len);
698 len -= DHCP_IP_UDP_SIZE;
701 r = dhcp_lease_new(&lease);
705 r = dhcp_option_parse(dhcp, len, dhcp_lease_parse_options, lease);
707 return DHCP_EVENT_NO_LEASE;
712 lease->address = dhcp->yiaddr;
714 if (lease->address == INADDR_ANY ||
715 lease->server_address == INADDR_ANY ||
716 lease->subnet_mask == INADDR_ANY || lease->lifetime == 0)
719 r = DHCP_EVENT_IP_ACQUIRE;
721 if (client->lease->address != lease->address ||
722 client->lease->subnet_mask != lease->subnet_mask ||
723 client->lease->router != lease->router) {
724 r = DHCP_EVENT_IP_CHANGE;
727 client->lease = sd_dhcp_lease_unref(client->lease);
730 client->lease = lease;
736 static uint64_t client_compute_timeout(uint64_t request_sent,
738 return request_sent + (lifetime - 3) * USEC_PER_SEC +
739 + (random_u32() & 0x1fffff);
742 static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) {
743 uint64_t next_timeout;
747 assert(client->event);
749 if (client->lease->lifetime < 10)
752 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
753 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
754 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
756 if (!client->lease->t1)
757 client->lease->t1 = client->lease->lifetime / 2;
759 next_timeout = client_compute_timeout(client->request_sent,
761 if (next_timeout < usec)
764 r = sd_event_add_monotonic(client->event, next_timeout,
766 client_timeout_t1, client,
767 &client->timeout_t1);
771 r = sd_event_source_set_priority(client->timeout_t1, client->event_priority);
775 if (!client->lease->t2)
776 client->lease->t2 = client->lease->lifetime * 7 / 8;
778 if (client->lease->t2 < client->lease->t1)
781 if (client->lease->lifetime < client->lease->t2)
784 next_timeout = client_compute_timeout(client->request_sent,
786 if (next_timeout < usec)
789 r = sd_event_add_monotonic(client->event, next_timeout,
791 client_timeout_t2, client,
792 &client->timeout_t2);
796 r = sd_event_source_set_priority(client->timeout_t2, client->event_priority);
800 next_timeout = client_compute_timeout(client->request_sent,
801 client->lease->lifetime);
802 if (next_timeout < usec)
805 r = sd_event_add_monotonic(client->event, next_timeout,
807 client_timeout_expire, client,
808 &client->timeout_expire);
812 r = sd_event_source_set_priority(client->timeout_expire, client->event_priority);
819 static int client_receive_message(sd_event_source *s, int fd,
820 uint32_t revents, void *userdata) {
821 sd_dhcp_client *client = userdata;
822 uint8_t buf[sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE];
823 int buflen = sizeof(buf);
824 int len, r = 0, notify_event = 0;
830 assert(client->event);
832 len = read(fd, &buf, buflen);
836 r = sd_event_get_now_monotonic(client->event, &time_now);
840 switch (client->state) {
841 case DHCP_STATE_SELECTING:
843 message = (DHCPPacket *)&buf;
845 if (client_receive_offer(client, message, len) >= 0) {
847 client->timeout_resend =
848 sd_event_source_unref(client->timeout_resend);
850 client->state = DHCP_STATE_REQUESTING;
853 r = sd_event_add_monotonic(client->event, time_now, 0,
854 client_timeout_resend,
856 &client->timeout_resend);
860 r = sd_event_source_set_priority(client->timeout_resend, client->event_priority);
867 case DHCP_STATE_REQUESTING:
868 case DHCP_STATE_RENEWING:
869 case DHCP_STATE_REBINDING:
871 r = client_receive_ack(client, buf, len);
873 if (r == DHCP_EVENT_NO_LEASE)
877 client->timeout_resend =
878 sd_event_source_unref(client->timeout_resend);
880 if (client->state == DHCP_STATE_REQUESTING)
881 notify_event = DHCP_EVENT_IP_ACQUIRE;
882 else if (r != DHCP_EVENT_IP_ACQUIRE)
885 client->state = DHCP_STATE_BOUND;
888 client->last_addr = client->lease->address;
890 r = client_set_lease_timeouts(client, time_now);
895 client_notify(client, notify_event);
897 client->receive_message =
898 sd_event_source_unref(client->receive_message);
907 case DHCP_STATE_INIT:
908 case DHCP_STATE_INIT_REBOOT:
909 case DHCP_STATE_REBOOTING:
910 case DHCP_STATE_BOUND:
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) {
925 assert_return(client, -EINVAL);
926 assert_return(client->event, -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_u32();
933 r = dhcp_network_bind_raw_socket(client->index, &client->link);
936 client_stop(client, r);
941 client->start_time = now(CLOCK_MONOTONIC);
944 return client_initialize_events(client, client->start_time);
947 int sd_dhcp_client_stop(sd_dhcp_client *client) {
948 return client_stop(client, DHCP_EVENT_STOP);
951 int sd_dhcp_client_attach_event(sd_dhcp_client *client, sd_event *event, int priority) {
954 assert_return(client, -EINVAL);
955 assert_return(!client->event, -EBUSY);
958 client->event = sd_event_ref(event);
960 r = sd_event_default(&client->event);
965 client->event_priority = priority;
970 int sd_dhcp_client_detach_event(sd_dhcp_client *client) {
971 assert_return(client, -EINVAL);
973 client->event = sd_event_unref(client->event);
978 sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client) {
982 return client->event;
985 void sd_dhcp_client_free(sd_dhcp_client *client) {
989 sd_dhcp_client_stop(client);
990 sd_dhcp_client_detach_event(client);
992 free(client->req_opts);
996 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp_client*, sd_dhcp_client_free);
997 #define _cleanup_dhcp_client_free_ _cleanup_(sd_dhcp_client_freep)
999 int sd_dhcp_client_new(sd_dhcp_client **ret) {
1000 _cleanup_dhcp_client_free_ sd_dhcp_client *client = NULL;
1002 assert_return(ret, -EINVAL);
1004 client = new0(sd_dhcp_client, 1);
1008 client->state = DHCP_STATE_INIT;
1011 client->attempt = 1;
1013 client->req_opts_size = ELEMENTSOF(default_req_opts);
1015 client->req_opts = memdup(default_req_opts, client->req_opts_size);
1016 if (!client->req_opts)