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>
26 #include <sys/ioctl.h>
31 #include "dhcp-protocol.h"
32 #include "dhcp-internal.h"
33 #include "dhcp-lease-internal.h"
34 #include "sd-dhcp-client.h"
36 struct sd_dhcp_client {
40 sd_event_source *timeout_resend;
43 union sockaddr_union link;
44 sd_event_source *receive_message;
46 size_t req_opts_allocated;
49 struct ether_addr mac_addr;
55 sd_event_source *timeout_t1;
56 sd_event_source *timeout_t2;
57 sd_event_source *timeout_expire;
58 sd_dhcp_client_cb_t cb;
63 static const uint8_t default_req_opts[] = {
64 DHCP_OPTION_SUBNET_MASK,
66 DHCP_OPTION_HOST_NAME,
67 DHCP_OPTION_DOMAIN_NAME,
68 DHCP_OPTION_DOMAIN_NAME_SERVER,
69 DHCP_OPTION_NTP_SERVER,
72 static int client_receive_message_raw(sd_event_source *s, int fd,
73 uint32_t revents, void *userdata);
74 static int client_receive_message_udp(sd_event_source *s, int fd,
75 uint32_t revents, void *userdata);
77 int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
79 assert_return(client, -EINVAL);
82 client->userdata = userdata;
87 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) {
90 assert_return(client, -EINVAL);
91 assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
95 case DHCP_OPTION_OVERLOAD:
96 case DHCP_OPTION_MESSAGE_TYPE:
97 case DHCP_OPTION_PARAMETER_REQUEST_LIST:
105 for (i = 0; i < client->req_opts_size; i++)
106 if (client->req_opts[i] == option)
109 if (!GREEDY_REALLOC(client->req_opts, client->req_opts_allocated,
110 client->req_opts_size + 1))
113 client->req_opts[client->req_opts_size++] = option;
118 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
119 const struct in_addr *last_addr) {
120 assert_return(client, -EINVAL);
121 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
124 client->last_addr = last_addr->s_addr;
126 client->last_addr = INADDR_ANY;
131 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) {
132 assert_return(client, -EINVAL);
133 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
134 assert_return(interface_index >= -1, -EINVAL);
136 client->index = interface_index;
141 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
142 const struct ether_addr *addr) {
143 assert_return(client, -EINVAL);
144 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
146 log_dhcp_client(client, "set MAC address to "
147 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
148 addr->ether_addr_octet[0],
149 addr->ether_addr_octet[1],
150 addr->ether_addr_octet[2],
151 addr->ether_addr_octet[3],
152 addr->ether_addr_octet[4],
153 addr->ether_addr_octet[5]);
155 memcpy(&client->mac_addr, addr, ETH_ALEN);
160 int sd_dhcp_client_get_lease(sd_dhcp_client *client, sd_dhcp_lease **ret) {
161 assert_return(client, -EINVAL);
162 assert_return(ret, -EINVAL);
164 if (client->state != DHCP_STATE_BOUND &&
165 client->state != DHCP_STATE_RENEWING &&
166 client->state != DHCP_STATE_REBINDING)
167 return -EADDRNOTAVAIL;
169 *ret = sd_dhcp_lease_ref(client->lease);
174 static int client_notify(sd_dhcp_client *client, int event) {
176 client->cb(client, event, client->userdata);
181 static int client_stop(sd_dhcp_client *client, int error) {
182 assert_return(client, -EINVAL);
184 client->receive_message =
185 sd_event_source_unref(client->receive_message);
191 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
193 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
194 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
195 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
199 client_notify(client, error);
201 client->start_time = 0;
203 client->state = DHCP_STATE_INIT;
206 client->lease = sd_dhcp_lease_unref(client->lease);
208 log_dhcp_client(client, "STOPPED");
213 static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
214 uint8_t type, uint16_t secs, uint8_t **opt,
220 r = dhcp_message_init(message, BOOTREQUEST, client->xid, type, opt,
225 /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
226 refuse to issue an DHCP lease if 'secs' is set to zero */
227 message->secs = htobe16(secs);
229 memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
231 if (client->state == DHCP_STATE_RENEWING ||
232 client->state == DHCP_STATE_REBINDING)
233 message->ciaddr = client->lease->address;
235 /* Some DHCP servers will refuse to issue an DHCP lease if the Client
236 Identifier option is not set */
237 r = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
238 ETH_ALEN, &client->mac_addr);
242 if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
245 r = dhcp_option_append(opt, optlen,
246 DHCP_OPTION_PARAMETER_REQUEST_LIST,
247 client->req_opts_size,
252 /* Some DHCP servers will send bigger DHCP packets than the
253 defined default size unless the Maximum Messge Size option
254 is explicitely set */
255 max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
256 DHCP_MIN_OPTIONS_SIZE);
257 r = dhcp_option_append(opt, optlen,
258 DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
267 static int client_send_discover(sd_dhcp_client *client, uint16_t secs) {
269 _cleanup_free_ DHCPPacket *discover;
273 optlen = DHCP_MIN_OPTIONS_SIZE;
274 len = sizeof(DHCPPacket) + optlen;
276 discover = malloc0(len);
281 err = client_message_init(client, &discover->dhcp, DHCP_DISCOVER,
282 secs, &opt, &optlen);
286 if (client->last_addr != INADDR_ANY) {
287 err = dhcp_option_append(&opt, &optlen,
288 DHCP_OPTION_REQUESTED_IP_ADDRESS,
289 4, &client->last_addr);
294 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
298 dhcp_packet_append_ip_headers(discover, len);
300 err = dhcp_network_send_raw_socket(client->fd, &client->link,
303 log_dhcp_client(client, "DISCOVER");
308 static int client_send_request(sd_dhcp_client *client, uint16_t secs) {
309 _cleanup_free_ DHCPPacket *request;
314 optlen = DHCP_MIN_OPTIONS_SIZE;
315 len = sizeof(DHCPPacket) + optlen;
317 request = malloc0(len);
321 err = client_message_init(client, &request->dhcp, DHCP_REQUEST, secs,
326 if (client->state == DHCP_STATE_REQUESTING) {
327 err = dhcp_option_append(&opt, &optlen,
328 DHCP_OPTION_REQUESTED_IP_ADDRESS,
329 4, &client->lease->address);
333 err = dhcp_option_append(&opt, &optlen,
334 DHCP_OPTION_SERVER_IDENTIFIER,
335 4, &client->lease->server_address);
340 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
344 if (client->state == DHCP_STATE_RENEWING) {
345 err = dhcp_network_send_udp_socket(client->fd,
346 client->lease->server_address,
349 len - DHCP_IP_UDP_SIZE);
351 dhcp_packet_append_ip_headers(request, len);
353 err = dhcp_network_send_raw_socket(client->fd, &client->link,
357 log_dhcp_client(client, "REQUEST");
362 static uint16_t client_update_secs(sd_dhcp_client *client, usec_t time_now)
364 client->secs = ((time_now - client->start_time) / USEC_PER_SEC) ? : 1;
369 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
371 sd_dhcp_client *client = userdata;
372 usec_t next_timeout = 0;
378 assert(client->event);
380 switch (client->state) {
381 case DHCP_STATE_RENEWING:
383 time_left = (client->lease->t2 - client->lease->t1) / 2;
387 next_timeout = usec + time_left * USEC_PER_SEC;
391 case DHCP_STATE_REBINDING:
393 time_left = (client->lease->lifetime - client->lease->t2) / 2;
397 next_timeout = usec + time_left * USEC_PER_SEC;
400 case DHCP_STATE_INIT:
401 case DHCP_STATE_INIT_REBOOT:
402 case DHCP_STATE_REBOOTING:
403 case DHCP_STATE_SELECTING:
404 case DHCP_STATE_REQUESTING:
405 case DHCP_STATE_BOUND:
407 if (client->attempt < 64)
408 client->attempt *= 2;
410 next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC;
415 next_timeout += (random_u32() & 0x1fffff);
417 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
419 r = sd_event_add_monotonic(client->event,
420 &client->timeout_resend,
423 client_timeout_resend, client);
427 r = sd_event_source_set_priority(client->timeout_resend,
428 client->event_priority);
432 switch (client->state) {
433 case DHCP_STATE_INIT:
435 client_update_secs(client, usec);
437 r = client_send_discover(client, client->secs);
439 client->state = DHCP_STATE_SELECTING;
442 if (client->attempt >= 64)
448 case DHCP_STATE_SELECTING:
449 client_update_secs(client, usec);
451 r = client_send_discover(client, client->secs);
452 if (r < 0 && client->attempt >= 64)
457 case DHCP_STATE_REQUESTING:
458 case DHCP_STATE_RENEWING:
459 case DHCP_STATE_REBINDING:
460 r = client_send_request(client, client->secs);
461 if (r < 0 && client->attempt >= 64)
464 client->request_sent = usec;
468 case DHCP_STATE_INIT_REBOOT:
469 case DHCP_STATE_REBOOTING:
470 case DHCP_STATE_BOUND:
478 client_stop(client, r);
480 /* Errors were dealt with when stopping the client, don't spill
481 errors into the event loop handler */
485 static int client_initialize_events(sd_dhcp_client *client,
486 sd_event_io_handler_t io_callback,
491 assert(client->event);
493 r = sd_event_add_io(client->event, &client->receive_message,
494 client->fd, EPOLLIN, io_callback,
499 r = sd_event_source_set_priority(client->receive_message,
500 client->event_priority);
504 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
506 r = sd_event_add_monotonic(client->event,
507 &client->timeout_resend,
509 client_timeout_resend, client);
513 r = sd_event_source_set_priority(client->timeout_resend,
514 client->event_priority);
518 client_stop(client, r);
524 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
526 sd_dhcp_client *client = userdata;
528 log_dhcp_client(client, "EXPIRED");
530 client_stop(client, DHCP_EVENT_EXPIRED);
535 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) {
536 sd_dhcp_client *client = userdata;
539 if (client->fd >= 0) {
540 client->receive_message =
541 sd_event_source_unref(client->receive_message);
546 client->state = DHCP_STATE_REBINDING;
549 r = dhcp_network_bind_raw_socket(client->index, &client->link);
551 client_stop(client, r);
557 log_dhcp_client(client, "TIMEOUT T2");
559 return client_initialize_events(client, client_receive_message_raw,
563 static int client_timeout_t1(sd_event_source *s, uint64_t usec,
565 sd_dhcp_client *client = userdata;
568 client->state = DHCP_STATE_RENEWING;
571 r = dhcp_network_bind_udp_socket(client->index,
572 client->lease->address,
575 client_stop(client, r);
581 log_dhcp_client(client, "TIMEOUT T1");
583 return client_initialize_events(client, client_receive_message_udp, usec);
586 static int client_handle_offer(sd_dhcp_client *client, DHCPMessage *offer,
588 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
591 r = dhcp_lease_new(&lease);
595 r = dhcp_option_parse(offer, len, dhcp_lease_parse_options, lease);
599 lease->next_server = offer->siaddr;
601 lease->address = offer->yiaddr;
603 if (lease->address == INADDR_ANY ||
604 lease->server_address == INADDR_ANY ||
605 lease->subnet_mask == INADDR_ANY ||
606 lease->lifetime == 0)
609 client->lease = lease;
612 log_dhcp_client(client, "OFFER");
617 static int client_handle_ack(sd_dhcp_client *client, DHCPMessage *ack,
619 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
622 r = dhcp_lease_new(&lease);
626 r = dhcp_option_parse(ack, len, dhcp_lease_parse_options, lease);
628 log_dhcp_client(client, "NAK");
629 return DHCP_EVENT_NO_LEASE;
635 lease->next_server = ack->siaddr;
637 lease->address = ack->yiaddr;
639 if (lease->address == INADDR_ANY ||
640 lease->server_address == INADDR_ANY ||
641 lease->subnet_mask == INADDR_ANY || lease->lifetime == 0)
644 r = DHCP_EVENT_IP_ACQUIRE;
646 if (client->lease->address != lease->address ||
647 client->lease->subnet_mask != lease->subnet_mask ||
648 client->lease->router != lease->router) {
649 r = DHCP_EVENT_IP_CHANGE;
652 client->lease = sd_dhcp_lease_unref(client->lease);
655 client->lease = lease;
658 log_dhcp_client(client, "ACK");
663 static uint64_t client_compute_timeout(uint64_t request_sent,
665 return request_sent + (lifetime - 3) * USEC_PER_SEC +
666 + (random_u32() & 0x1fffff);
669 static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) {
670 uint64_t next_timeout;
674 assert(client->event);
676 if (client->lease->lifetime < 10)
679 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
680 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
681 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
683 if (!client->lease->t1)
684 client->lease->t1 = client->lease->lifetime / 2;
686 next_timeout = client_compute_timeout(client->request_sent,
688 if (next_timeout < usec)
691 r = sd_event_add_monotonic(client->event,
695 client_timeout_t1, client);
699 r = sd_event_source_set_priority(client->timeout_t1,
700 client->event_priority);
704 if (!client->lease->t2)
705 client->lease->t2 = client->lease->lifetime * 7 / 8;
707 if (client->lease->t2 < client->lease->t1)
710 if (client->lease->lifetime < client->lease->t2)
713 next_timeout = client_compute_timeout(client->request_sent,
715 if (next_timeout < usec)
718 r = sd_event_add_monotonic(client->event,
722 client_timeout_t2, client);
726 r = sd_event_source_set_priority(client->timeout_t2,
727 client->event_priority);
731 next_timeout = client_compute_timeout(client->request_sent,
732 client->lease->lifetime);
733 if (next_timeout < usec)
736 r = sd_event_add_monotonic(client->event,
737 &client->timeout_expire, next_timeout,
739 client_timeout_expire, client);
743 r = sd_event_source_set_priority(client->timeout_expire,
744 client->event_priority);
751 static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
752 int len, usec_t time_now) {
753 int r = 0, notify_event = 0;
756 assert(client->event);
759 if (len < DHCP_MESSAGE_SIZE) {
760 log_dhcp_client(client, "message too small (%d bytes): "
765 if (message->op != BOOTREPLY) {
766 log_dhcp_client(client, "not a BOOTREPLY message: ignoring");
770 if (be32toh(message->xid) != client->xid) {
771 log_dhcp_client(client, "received xid (%u) does not match "
772 "expected (%u): ignoring",
773 be32toh(message->xid), client->xid);
777 if (memcmp(&message->chaddr[0], &client->mac_addr.ether_addr_octet,
779 log_dhcp_client(client, "received chaddr does not match "
780 "expected: ignoring");
784 switch (client->state) {
785 case DHCP_STATE_SELECTING:
787 r = client_handle_offer(client, message, len);
790 client->timeout_resend =
791 sd_event_source_unref(client->timeout_resend);
793 client->state = DHCP_STATE_REQUESTING;
796 r = sd_event_add_monotonic(client->event,
797 &client->timeout_resend,
799 client_timeout_resend,
804 r = sd_event_source_set_priority(client->timeout_resend,
805 client->event_priority);
812 case DHCP_STATE_REQUESTING:
813 case DHCP_STATE_RENEWING:
814 case DHCP_STATE_REBINDING:
816 r = client_handle_ack(client, message, len);
818 if (r == DHCP_EVENT_NO_LEASE)
822 client->timeout_resend =
823 sd_event_source_unref(client->timeout_resend);
825 if (client->state == DHCP_STATE_REQUESTING)
826 notify_event = DHCP_EVENT_IP_ACQUIRE;
827 else if (r != DHCP_EVENT_IP_ACQUIRE)
830 client->state = DHCP_STATE_BOUND;
833 client->last_addr = client->lease->address;
835 r = client_set_lease_timeouts(client, time_now);
840 client_notify(client, notify_event);
842 client->receive_message =
843 sd_event_source_unref(client->receive_message);
852 case DHCP_STATE_INIT:
853 case DHCP_STATE_INIT_REBOOT:
854 case DHCP_STATE_REBOOTING:
855 case DHCP_STATE_BOUND:
861 if (r < 0 || r == DHCP_EVENT_NO_LEASE)
862 return client_stop(client, r);
867 static int client_receive_message_udp(sd_event_source *s, int fd,
868 uint32_t revents, void *userdata) {
869 sd_dhcp_client *client = userdata;
870 _cleanup_free_ DHCPMessage *message = NULL;
871 int buflen = 0, len, r;
876 assert(client->event);
878 r = ioctl(fd, FIONREAD, &buflen);
879 if (r < 0 || buflen <= 0)
880 buflen = sizeof(DHCPMessage) + DHCP_MIN_OPTIONS_SIZE;
882 message = malloc0(buflen);
886 len = read(fd, message, buflen);
890 r = sd_event_get_now_monotonic(client->event, &time_now);
892 return client_stop(client, r);
894 return client_handle_message(client, message, len,
898 static int client_receive_message_raw(sd_event_source *s, int fd,
899 uint32_t revents, void *userdata) {
900 sd_dhcp_client *client = userdata;
901 _cleanup_free_ DHCPPacket *packet = NULL;
903 uint8_t cmsgbuf[CMSG_LEN(sizeof(struct tpacket_auxdata))];
904 struct iovec iov = {};
905 struct msghdr msg = {
908 .msg_control = cmsgbuf,
909 .msg_controllen = sizeof(cmsgbuf),
911 struct cmsghdr *cmsg;
912 bool checksum = true;
913 int buflen = 0, len, r;
917 assert(client->event);
919 r = ioctl(fd, FIONREAD, &buflen);
920 if (r < 0 || buflen <= 0)
921 buflen = sizeof(DHCPPacket) + DHCP_MIN_OPTIONS_SIZE;
923 packet = malloc0(buflen);
927 iov.iov_base = packet;
928 iov.iov_len = buflen;
930 len = recvmsg(fd, &msg, 0);
932 log_dhcp_client(client, "could not receive message from raw "
933 "socket: %s", strerror(errno));
937 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
938 if (cmsg->cmsg_level == SOL_PACKET && cmsg->cmsg_type == PACKET_AUXDATA) {
939 struct tpacket_auxdata *aux = (void *)CMSG_DATA(cmsg);
941 checksum = !(aux->tp_status & TP_STATUS_CSUMNOTREADY);
946 r = dhcp_packet_verify_headers(packet, len, checksum);
950 len -= DHCP_IP_UDP_SIZE;
952 r = sd_event_get_now_monotonic(client->event, &time_now);
954 return client_stop(client, r);
956 return client_handle_message(client, &packet->dhcp, len, time_now);
959 int sd_dhcp_client_start(sd_dhcp_client *client) {
962 assert_return(client, -EINVAL);
963 assert_return(client->event, -EINVAL);
964 assert_return(client->index > 0, -EINVAL);
965 assert_return(client->state == DHCP_STATE_INIT ||
966 client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
968 client->xid = random_u32();
970 r = dhcp_network_bind_raw_socket(client->index, &client->link);
973 client_stop(client, r);
978 client->start_time = now(CLOCK_MONOTONIC);
981 log_dhcp_client(client, "STARTED");
983 return client_initialize_events(client, client_receive_message_raw,
987 int sd_dhcp_client_stop(sd_dhcp_client *client) {
988 return client_stop(client, DHCP_EVENT_STOP);
991 int sd_dhcp_client_attach_event(sd_dhcp_client *client, sd_event *event,
995 assert_return(client, -EINVAL);
996 assert_return(!client->event, -EBUSY);
999 client->event = sd_event_ref(event);
1001 r = sd_event_default(&client->event);
1006 client->event_priority = priority;
1011 int sd_dhcp_client_detach_event(sd_dhcp_client *client) {
1012 assert_return(client, -EINVAL);
1014 client->event = sd_event_unref(client->event);
1019 sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client) {
1023 return client->event;
1026 void sd_dhcp_client_free(sd_dhcp_client *client) {
1030 sd_dhcp_client_stop(client);
1031 sd_dhcp_client_detach_event(client);
1033 free(client->req_opts);
1037 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp_client*, sd_dhcp_client_free);
1038 #define _cleanup_dhcp_client_free_ _cleanup_(sd_dhcp_client_freep)
1040 int sd_dhcp_client_new(sd_dhcp_client **ret) {
1041 _cleanup_dhcp_client_free_ sd_dhcp_client *client = NULL;
1043 assert_return(ret, -EINVAL);
1045 client = new0(sd_dhcp_client, 1);
1049 client->state = DHCP_STATE_INIT;
1052 client->attempt = 1;
1054 client->req_opts_size = ELEMENTSOF(default_req_opts);
1056 client->req_opts = memdup(default_req_opts, client->req_opts_size);
1057 if (!client->req_opts)