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 struct sd_dhcp_client {
39 sd_event_source *timeout_resend;
42 union sockaddr_union link;
43 sd_event_source *receive_message;
45 size_t req_opts_allocated;
48 struct ether_addr mac_addr;
54 sd_event_source *timeout_t1;
55 sd_event_source *timeout_t2;
56 sd_event_source *timeout_expire;
57 sd_dhcp_client_cb_t cb;
62 static const uint8_t default_req_opts[] = {
63 DHCP_OPTION_SUBNET_MASK,
65 DHCP_OPTION_HOST_NAME,
66 DHCP_OPTION_DOMAIN_NAME,
67 DHCP_OPTION_DOMAIN_NAME_SERVER,
68 DHCP_OPTION_NTP_SERVER,
71 static int client_receive_message_raw(sd_event_source *s, int fd,
72 uint32_t revents, void *userdata);
73 static int client_receive_message_udp(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_message_init(sd_dhcp_client *client, DHCPMessage *message,
202 uint8_t type, uint16_t secs, uint8_t **opt,
206 r = dhcp_message_init(message, BOOTREQUEST, client->xid, type,
211 memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
213 if (client->state == DHCP_STATE_RENEWING ||
214 client->state == DHCP_STATE_REBINDING)
215 message->ciaddr = client->lease->address;
217 /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
218 Identifier option is not set */
219 r = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
220 ETH_ALEN, &client->mac_addr);
224 if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
227 r = dhcp_option_append(opt, optlen,
228 DHCP_OPTION_PARAMETER_REQUEST_LIST,
229 client->req_opts_size,
234 /* Some DHCP servers will send bigger DHCP packets than the
235 defined default size unless the Maximum Messge Size option
236 is explicitely set */
237 max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
238 DHCP_MIN_OPTIONS_SIZE);
239 r = dhcp_option_append(opt, optlen,
240 DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
249 static int client_send_discover(sd_dhcp_client *client, uint16_t secs) {
251 _cleanup_free_ DHCPPacket *discover;
255 optlen = DHCP_MIN_OPTIONS_SIZE;
256 len = sizeof(DHCPPacket) + optlen;
258 discover = malloc0(len);
263 err = client_message_init(client, &discover->dhcp, DHCP_DISCOVER,
264 secs, &opt, &optlen);
268 if (client->last_addr != INADDR_ANY) {
269 err = dhcp_option_append(&opt, &optlen,
270 DHCP_OPTION_REQUESTED_IP_ADDRESS,
271 4, &client->last_addr);
276 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
280 dhcp_packet_append_ip_headers(discover, BOOTREQUEST, len);
282 err = dhcp_network_send_raw_socket(client->fd, &client->link,
288 static int client_send_request(sd_dhcp_client *client, uint16_t secs) {
289 _cleanup_free_ DHCPPacket *request;
294 optlen = DHCP_MIN_OPTIONS_SIZE;
295 len = DHCP_MESSAGE_SIZE + optlen;
297 request = malloc0(len);
301 err = client_message_init(client, &request->dhcp, DHCP_REQUEST, secs,
306 if (client->state == DHCP_STATE_REQUESTING) {
307 err = dhcp_option_append(&opt, &optlen,
308 DHCP_OPTION_REQUESTED_IP_ADDRESS,
309 4, &client->lease->address);
313 err = dhcp_option_append(&opt, &optlen,
314 DHCP_OPTION_SERVER_IDENTIFIER,
315 4, &client->lease->server_address);
320 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
324 if (client->state == DHCP_STATE_RENEWING) {
325 err = dhcp_network_send_udp_socket(client->fd,
326 client->lease->server_address,
329 len - DHCP_IP_UDP_SIZE);
331 dhcp_packet_append_ip_headers(request, BOOTREQUEST, len);
333 err = dhcp_network_send_raw_socket(client->fd, &client->link,
340 static uint16_t client_update_secs(sd_dhcp_client *client, usec_t time_now)
342 client->secs = ((time_now - client->start_time) / USEC_PER_SEC) ? : 1;
347 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
349 sd_dhcp_client *client = userdata;
350 usec_t next_timeout = 0;
356 assert(client->event);
358 switch (client->state) {
359 case DHCP_STATE_RENEWING:
361 time_left = (client->lease->t2 - client->lease->t1) / 2;
365 next_timeout = usec + time_left * USEC_PER_SEC;
369 case DHCP_STATE_REBINDING:
371 time_left = (client->lease->lifetime - client->lease->t2) / 2;
375 next_timeout = usec + time_left * USEC_PER_SEC;
378 case DHCP_STATE_INIT:
379 case DHCP_STATE_INIT_REBOOT:
380 case DHCP_STATE_REBOOTING:
381 case DHCP_STATE_SELECTING:
382 case DHCP_STATE_REQUESTING:
383 case DHCP_STATE_BOUND:
385 if (client->attempt < 64)
386 client->attempt *= 2;
388 next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC;
393 next_timeout += (random_u32() & 0x1fffff);
395 r = sd_event_add_monotonic(client->event, next_timeout,
397 client_timeout_resend, client,
398 &client->timeout_resend);
402 r = sd_event_source_set_priority(client->timeout_resend,
403 client->event_priority);
407 switch (client->state) {
408 case DHCP_STATE_INIT:
410 client_update_secs(client, usec);
412 r = client_send_discover(client, client->secs);
414 client->state = DHCP_STATE_SELECTING;
417 if (client->attempt >= 64)
423 case DHCP_STATE_SELECTING:
424 client_update_secs(client, usec);
426 r = client_send_discover(client, client->secs);
427 if (r < 0 && client->attempt >= 64)
432 case DHCP_STATE_REQUESTING:
433 case DHCP_STATE_RENEWING:
434 case DHCP_STATE_REBINDING:
435 r = client_send_request(client, client->secs);
436 if (r < 0 && client->attempt >= 64)
439 client->request_sent = usec;
443 case DHCP_STATE_INIT_REBOOT:
444 case DHCP_STATE_REBOOTING:
445 case DHCP_STATE_BOUND:
453 client_stop(client, r);
455 /* Errors were dealt with when stopping the client, don't spill
456 errors into the event loop handler */
460 static int client_initialize_events(sd_dhcp_client *client,
461 sd_event_io_handler_t io_callback,
466 assert(client->event);
468 r = sd_event_add_io(client->event, client->fd, EPOLLIN, io_callback,
469 client, &client->receive_message);
473 r = sd_event_source_set_priority(client->receive_message,
474 client->event_priority);
478 r = sd_event_add_monotonic(client->event, usec, 0,
479 client_timeout_resend, client,
480 &client->timeout_resend);
484 r = sd_event_source_set_priority(client->timeout_resend,
485 client->event_priority);
489 client_stop(client, r);
495 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
497 sd_dhcp_client *client = userdata;
499 client_stop(client, DHCP_EVENT_EXPIRED);
504 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) {
505 sd_dhcp_client *client = userdata;
508 if (client->fd >= 0) {
509 client->receive_message =
510 sd_event_source_unref(client->receive_message);
515 client->state = DHCP_STATE_REBINDING;
518 r = dhcp_network_bind_raw_socket(client->index, &client->link);
520 client_stop(client, r);
526 return client_initialize_events(client, client_receive_message_raw,
530 static int client_timeout_t1(sd_event_source *s, uint64_t usec,
532 sd_dhcp_client *client = userdata;
535 client->state = DHCP_STATE_RENEWING;
538 r = dhcp_network_bind_udp_socket(client->index,
539 client->lease->address,
542 client_stop(client, r);
548 return client_initialize_events(client, client_receive_message_udp, usec);
551 static int client_handle_offer(sd_dhcp_client *client, DHCPMessage *offer,
553 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
556 r = dhcp_lease_new(&lease);
560 r = dhcp_option_parse(offer, len, dhcp_lease_parse_options, lease);
564 lease->address = offer->yiaddr;
566 if (lease->address == INADDR_ANY ||
567 lease->server_address == INADDR_ANY ||
568 lease->subnet_mask == INADDR_ANY ||
569 lease->lifetime == 0)
572 client->lease = lease;
578 static int client_handle_ack(sd_dhcp_client *client, DHCPMessage *ack,
580 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
583 r = dhcp_lease_new(&lease);
587 r = dhcp_option_parse(ack, len, dhcp_lease_parse_options, lease);
589 return DHCP_EVENT_NO_LEASE;
594 lease->address = ack->yiaddr;
596 if (lease->address == INADDR_ANY ||
597 lease->server_address == INADDR_ANY ||
598 lease->subnet_mask == INADDR_ANY || lease->lifetime == 0)
601 r = DHCP_EVENT_IP_ACQUIRE;
603 if (client->lease->address != lease->address ||
604 client->lease->subnet_mask != lease->subnet_mask ||
605 client->lease->router != lease->router) {
606 r = DHCP_EVENT_IP_CHANGE;
609 client->lease = sd_dhcp_lease_unref(client->lease);
612 client->lease = lease;
618 static uint64_t client_compute_timeout(uint64_t request_sent,
620 return request_sent + (lifetime - 3) * USEC_PER_SEC +
621 + (random_u32() & 0x1fffff);
624 static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) {
625 uint64_t next_timeout;
629 assert(client->event);
631 if (client->lease->lifetime < 10)
634 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
635 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
636 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
638 if (!client->lease->t1)
639 client->lease->t1 = client->lease->lifetime / 2;
641 next_timeout = client_compute_timeout(client->request_sent,
643 if (next_timeout < usec)
646 r = sd_event_add_monotonic(client->event, next_timeout,
648 client_timeout_t1, client,
649 &client->timeout_t1);
653 r = sd_event_source_set_priority(client->timeout_t1,
654 client->event_priority);
658 if (!client->lease->t2)
659 client->lease->t2 = client->lease->lifetime * 7 / 8;
661 if (client->lease->t2 < client->lease->t1)
664 if (client->lease->lifetime < client->lease->t2)
667 next_timeout = client_compute_timeout(client->request_sent,
669 if (next_timeout < usec)
672 r = sd_event_add_monotonic(client->event, next_timeout,
674 client_timeout_t2, client,
675 &client->timeout_t2);
679 r = sd_event_source_set_priority(client->timeout_t2,
680 client->event_priority);
684 next_timeout = client_compute_timeout(client->request_sent,
685 client->lease->lifetime);
686 if (next_timeout < usec)
689 r = sd_event_add_monotonic(client->event, next_timeout,
691 client_timeout_expire, client,
692 &client->timeout_expire);
696 r = sd_event_source_set_priority(client->timeout_expire,
697 client->event_priority);
704 static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
705 int len, usec_t time_now) {
706 int r = 0, notify_event = 0;
709 assert(client->event);
712 if (be32toh(message->xid) != client->xid)
715 if (memcmp(&message->chaddr[0], &client->mac_addr.ether_addr_octet,
719 switch (client->state) {
720 case DHCP_STATE_SELECTING:
722 r = client_handle_offer(client, message, len);
725 client->timeout_resend =
726 sd_event_source_unref(client->timeout_resend);
728 client->state = DHCP_STATE_REQUESTING;
731 r = sd_event_add_monotonic(client->event, time_now, 0,
732 client_timeout_resend,
734 &client->timeout_resend);
738 r = sd_event_source_set_priority(client->timeout_resend,
739 client->event_priority);
746 case DHCP_STATE_REQUESTING:
747 case DHCP_STATE_RENEWING:
748 case DHCP_STATE_REBINDING:
750 r = client_handle_ack(client, message, len);
752 if (r == DHCP_EVENT_NO_LEASE)
756 client->timeout_resend =
757 sd_event_source_unref(client->timeout_resend);
759 if (client->state == DHCP_STATE_REQUESTING)
760 notify_event = DHCP_EVENT_IP_ACQUIRE;
761 else if (r != DHCP_EVENT_IP_ACQUIRE)
764 client->state = DHCP_STATE_BOUND;
767 client->last_addr = client->lease->address;
769 r = client_set_lease_timeouts(client, time_now);
774 client_notify(client, notify_event);
776 client->receive_message =
777 sd_event_source_unref(client->receive_message);
786 case DHCP_STATE_INIT:
787 case DHCP_STATE_INIT_REBOOT:
788 case DHCP_STATE_REBOOTING:
789 case DHCP_STATE_BOUND:
795 if (r < 0 || r == DHCP_EVENT_NO_LEASE)
796 return client_stop(client, r);
801 static int client_receive_message_raw(sd_event_source *s, int fd,
802 uint32_t revents, void *userdata) {
803 sd_dhcp_client *client = userdata;
804 uint8_t buf[sizeof(DHCPMessage) + DHCP_MIN_OPTIONS_SIZE];
805 int buflen = sizeof(buf);
811 assert(client->event);
813 len = read(fd, &buf, buflen);
817 r = sd_event_get_now_monotonic(client->event, &time_now);
819 return client_stop(client, r);
821 return client_handle_message(client, (DHCPMessage *) buf, len,
825 static int client_receive_message_udp(sd_event_source *s, int fd,
826 uint32_t revents, void *userdata) {
827 sd_dhcp_client *client = userdata;
828 uint8_t buf[sizeof(DHCPPacket) + DHCP_MIN_OPTIONS_SIZE];
829 int buflen = sizeof(buf);
836 assert(client->event);
838 len = read(fd, &buf, buflen);
842 packet = (DHCPPacket *) buf;
844 r = dhcp_packet_verify_headers(packet, BOOTREPLY, len);
848 len -= DHCP_IP_UDP_SIZE;
850 r = sd_event_get_now_monotonic(client->event, &time_now);
852 return client_stop(client, r);
854 return client_handle_message(client, &packet->dhcp, len, time_now);
857 int sd_dhcp_client_start(sd_dhcp_client *client) {
860 assert_return(client, -EINVAL);
861 assert_return(client->event, -EINVAL);
862 assert_return(client->index > 0, -EINVAL);
863 assert_return(client->state == DHCP_STATE_INIT ||
864 client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
866 client->xid = random_u32();
868 r = dhcp_network_bind_raw_socket(client->index, &client->link);
871 client_stop(client, r);
876 client->start_time = now(CLOCK_MONOTONIC);
879 return client_initialize_events(client, client_receive_message_udp,
883 int sd_dhcp_client_stop(sd_dhcp_client *client) {
884 return client_stop(client, DHCP_EVENT_STOP);
887 int sd_dhcp_client_attach_event(sd_dhcp_client *client, sd_event *event,
891 assert_return(client, -EINVAL);
892 assert_return(!client->event, -EBUSY);
895 client->event = sd_event_ref(event);
897 r = sd_event_default(&client->event);
902 client->event_priority = priority;
907 int sd_dhcp_client_detach_event(sd_dhcp_client *client) {
908 assert_return(client, -EINVAL);
910 client->event = sd_event_unref(client->event);
915 sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client) {
919 return client->event;
922 void sd_dhcp_client_free(sd_dhcp_client *client) {
926 sd_dhcp_client_stop(client);
927 sd_dhcp_client_detach_event(client);
929 free(client->req_opts);
933 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp_client*, sd_dhcp_client_free);
934 #define _cleanup_dhcp_client_free_ _cleanup_(sd_dhcp_client_freep)
936 int sd_dhcp_client_new(sd_dhcp_client **ret) {
937 _cleanup_dhcp_client_free_ sd_dhcp_client *client = NULL;
939 assert_return(ret, -EINVAL);
941 client = new0(sd_dhcp_client, 1);
945 client->state = DHCP_STATE_INIT;
950 client->req_opts_size = ELEMENTSOF(default_req_opts);
952 client->req_opts = memdup(default_req_opts, client->req_opts_size);
953 if (!client->req_opts)