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);
188 close_nointr_nofail(client->fd);
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 dhcp_client_send_raw(sd_dhcp_client *client, DHCPPacket *packet,
269 dhcp_packet_append_ip_headers(packet, INADDR_ANY, DHCP_PORT_CLIENT,
270 INADDR_BROADCAST, DHCP_PORT_SERVER, len);
272 return dhcp_network_send_raw_socket(client->fd, &client->link,
276 static int client_send_discover(sd_dhcp_client *client, uint16_t secs) {
278 _cleanup_free_ DHCPPacket *discover;
282 optlen = DHCP_MIN_OPTIONS_SIZE;
283 len = sizeof(DHCPPacket) + optlen;
285 discover = malloc0(len);
290 err = client_message_init(client, &discover->dhcp, DHCP_DISCOVER,
291 secs, &opt, &optlen);
295 if (client->last_addr != INADDR_ANY) {
296 err = dhcp_option_append(&opt, &optlen,
297 DHCP_OPTION_REQUESTED_IP_ADDRESS,
298 4, &client->last_addr);
303 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
307 err = dhcp_client_send_raw(client, discover, len);
311 log_dhcp_client(client, "DISCOVER");
316 static int client_send_request(sd_dhcp_client *client, uint16_t secs) {
317 _cleanup_free_ DHCPPacket *request;
322 optlen = DHCP_MIN_OPTIONS_SIZE;
323 len = sizeof(DHCPPacket) + optlen;
325 request = malloc0(len);
329 err = client_message_init(client, &request->dhcp, DHCP_REQUEST, secs,
334 if (client->state == DHCP_STATE_REQUESTING) {
335 err = dhcp_option_append(&opt, &optlen,
336 DHCP_OPTION_REQUESTED_IP_ADDRESS,
337 4, &client->lease->address);
341 err = dhcp_option_append(&opt, &optlen,
342 DHCP_OPTION_SERVER_IDENTIFIER,
343 4, &client->lease->server_address);
348 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
352 if (client->state == DHCP_STATE_RENEWING) {
353 err = dhcp_network_send_udp_socket(client->fd,
354 client->lease->server_address,
357 len - DHCP_IP_UDP_SIZE);
359 err = dhcp_client_send_raw(client, request, len);
364 log_dhcp_client(client, "REQUEST");
369 static uint16_t client_update_secs(sd_dhcp_client *client, usec_t time_now)
371 client->secs = ((time_now - client->start_time) / USEC_PER_SEC) ? : 1;
376 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
378 sd_dhcp_client *client = userdata;
379 usec_t next_timeout = 0;
385 assert(client->event);
387 switch (client->state) {
388 case DHCP_STATE_RENEWING:
390 time_left = (client->lease->t2 - client->lease->t1) / 2;
394 next_timeout = usec + time_left * USEC_PER_SEC;
398 case DHCP_STATE_REBINDING:
400 time_left = (client->lease->lifetime - client->lease->t2) / 2;
404 next_timeout = usec + time_left * USEC_PER_SEC;
407 case DHCP_STATE_INIT:
408 case DHCP_STATE_INIT_REBOOT:
409 case DHCP_STATE_REBOOTING:
410 case DHCP_STATE_SELECTING:
411 case DHCP_STATE_REQUESTING:
412 case DHCP_STATE_BOUND:
414 if (client->attempt < 64)
415 client->attempt *= 2;
417 next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC;
422 next_timeout += (random_u32() & 0x1fffff);
424 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
426 r = sd_event_add_monotonic(client->event,
427 &client->timeout_resend,
430 client_timeout_resend, client);
434 r = sd_event_source_set_priority(client->timeout_resend,
435 client->event_priority);
439 switch (client->state) {
440 case DHCP_STATE_INIT:
442 client_update_secs(client, usec);
444 r = client_send_discover(client, client->secs);
446 client->state = DHCP_STATE_SELECTING;
449 if (client->attempt >= 64)
455 case DHCP_STATE_SELECTING:
456 client_update_secs(client, usec);
458 r = client_send_discover(client, client->secs);
459 if (r < 0 && client->attempt >= 64)
464 case DHCP_STATE_REQUESTING:
465 case DHCP_STATE_RENEWING:
466 case DHCP_STATE_REBINDING:
467 r = client_send_request(client, client->secs);
468 if (r < 0 && client->attempt >= 64)
471 client->request_sent = usec;
475 case DHCP_STATE_INIT_REBOOT:
476 case DHCP_STATE_REBOOTING:
477 case DHCP_STATE_BOUND:
485 client_stop(client, r);
487 /* Errors were dealt with when stopping the client, don't spill
488 errors into the event loop handler */
492 static int client_initialize_events(sd_dhcp_client *client,
493 sd_event_io_handler_t io_callback,
498 assert(client->event);
500 r = sd_event_add_io(client->event, &client->receive_message,
501 client->fd, EPOLLIN, io_callback,
506 r = sd_event_source_set_priority(client->receive_message,
507 client->event_priority);
511 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
513 r = sd_event_add_monotonic(client->event,
514 &client->timeout_resend,
516 client_timeout_resend, client);
520 r = sd_event_source_set_priority(client->timeout_resend,
521 client->event_priority);
525 client_stop(client, r);
531 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
533 sd_dhcp_client *client = userdata;
535 log_dhcp_client(client, "EXPIRED");
537 client_stop(client, DHCP_EVENT_EXPIRED);
542 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) {
543 sd_dhcp_client *client = userdata;
546 if (client->fd >= 0) {
547 client->receive_message =
548 sd_event_source_unref(client->receive_message);
549 close_nointr_nofail(client->fd);
553 client->state = DHCP_STATE_REBINDING;
556 r = dhcp_network_bind_raw_socket(client->index, &client->link);
558 client_stop(client, r);
564 log_dhcp_client(client, "TIMEOUT T2");
566 return client_initialize_events(client, client_receive_message_raw,
570 static int client_timeout_t1(sd_event_source *s, uint64_t usec,
572 sd_dhcp_client *client = userdata;
575 client->state = DHCP_STATE_RENEWING;
578 r = dhcp_network_bind_udp_socket(client->index,
579 client->lease->address,
582 client_stop(client, r);
588 log_dhcp_client(client, "TIMEOUT T1");
590 return client_initialize_events(client, client_receive_message_udp, usec);
593 static int client_handle_offer(sd_dhcp_client *client, DHCPMessage *offer,
595 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
598 r = dhcp_lease_new(&lease);
602 r = dhcp_option_parse(offer, len, dhcp_lease_parse_options, lease);
606 lease->next_server = offer->siaddr;
608 lease->address = offer->yiaddr;
610 if (lease->address == INADDR_ANY ||
611 lease->server_address == INADDR_ANY ||
612 lease->subnet_mask == INADDR_ANY ||
613 lease->lifetime == 0)
616 client->lease = lease;
619 log_dhcp_client(client, "OFFER");
624 static int client_handle_ack(sd_dhcp_client *client, DHCPMessage *ack,
626 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
629 r = dhcp_lease_new(&lease);
633 r = dhcp_option_parse(ack, len, dhcp_lease_parse_options, lease);
635 log_dhcp_client(client, "NAK");
636 return DHCP_EVENT_NO_LEASE;
642 lease->next_server = ack->siaddr;
644 lease->address = ack->yiaddr;
646 if (lease->address == INADDR_ANY ||
647 lease->server_address == INADDR_ANY ||
648 lease->subnet_mask == INADDR_ANY || lease->lifetime == 0)
651 r = DHCP_EVENT_IP_ACQUIRE;
653 if (client->lease->address != lease->address ||
654 client->lease->subnet_mask != lease->subnet_mask ||
655 client->lease->router != lease->router) {
656 r = DHCP_EVENT_IP_CHANGE;
659 client->lease = sd_dhcp_lease_unref(client->lease);
662 client->lease = lease;
665 log_dhcp_client(client, "ACK");
670 static uint64_t client_compute_timeout(uint64_t request_sent,
672 return request_sent + (lifetime - 3) * USEC_PER_SEC +
673 + (random_u32() & 0x1fffff);
676 static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) {
677 uint64_t next_timeout;
681 assert(client->event);
683 if (client->lease->lifetime < 10)
686 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
687 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
688 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
690 if (!client->lease->t1)
691 client->lease->t1 = client->lease->lifetime / 2;
693 next_timeout = client_compute_timeout(client->request_sent,
695 if (next_timeout < usec)
698 r = sd_event_add_monotonic(client->event,
702 client_timeout_t1, client);
706 r = sd_event_source_set_priority(client->timeout_t1,
707 client->event_priority);
711 if (!client->lease->t2)
712 client->lease->t2 = client->lease->lifetime * 7 / 8;
714 if (client->lease->t2 < client->lease->t1)
717 if (client->lease->lifetime < client->lease->t2)
720 next_timeout = client_compute_timeout(client->request_sent,
722 if (next_timeout < usec)
725 r = sd_event_add_monotonic(client->event,
729 client_timeout_t2, client);
733 r = sd_event_source_set_priority(client->timeout_t2,
734 client->event_priority);
738 next_timeout = client_compute_timeout(client->request_sent,
739 client->lease->lifetime);
740 if (next_timeout < usec)
743 r = sd_event_add_monotonic(client->event,
744 &client->timeout_expire, next_timeout,
746 client_timeout_expire, client);
750 r = sd_event_source_set_priority(client->timeout_expire,
751 client->event_priority);
758 static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
759 int len, usec_t time_now) {
760 int r = 0, notify_event = 0;
763 assert(client->event);
766 if (len < DHCP_MESSAGE_SIZE) {
767 log_dhcp_client(client, "message too small (%d bytes): "
772 if (message->op != BOOTREPLY) {
773 log_dhcp_client(client, "not a BOOTREPLY message: ignoring");
777 if (be32toh(message->xid) != client->xid) {
778 log_dhcp_client(client, "received xid (%u) does not match "
779 "expected (%u): ignoring",
780 be32toh(message->xid), client->xid);
784 if (memcmp(&message->chaddr[0], &client->mac_addr.ether_addr_octet,
786 log_dhcp_client(client, "received chaddr does not match "
787 "expected: ignoring");
791 switch (client->state) {
792 case DHCP_STATE_SELECTING:
794 r = client_handle_offer(client, message, len);
797 client->timeout_resend =
798 sd_event_source_unref(client->timeout_resend);
800 client->state = DHCP_STATE_REQUESTING;
803 r = sd_event_add_monotonic(client->event,
804 &client->timeout_resend,
806 client_timeout_resend,
811 r = sd_event_source_set_priority(client->timeout_resend,
812 client->event_priority);
819 case DHCP_STATE_REQUESTING:
820 case DHCP_STATE_RENEWING:
821 case DHCP_STATE_REBINDING:
823 r = client_handle_ack(client, message, len);
825 if (r == DHCP_EVENT_NO_LEASE)
829 client->timeout_resend =
830 sd_event_source_unref(client->timeout_resend);
832 if (client->state == DHCP_STATE_REQUESTING)
833 notify_event = DHCP_EVENT_IP_ACQUIRE;
834 else if (r != DHCP_EVENT_IP_ACQUIRE)
837 client->state = DHCP_STATE_BOUND;
840 client->last_addr = client->lease->address;
842 r = client_set_lease_timeouts(client, time_now);
847 client_notify(client, notify_event);
849 client->receive_message =
850 sd_event_source_unref(client->receive_message);
851 close_nointr_nofail(client->fd);
859 case DHCP_STATE_INIT:
860 case DHCP_STATE_INIT_REBOOT:
861 case DHCP_STATE_REBOOTING:
862 case DHCP_STATE_BOUND:
868 if (r < 0 || r == DHCP_EVENT_NO_LEASE)
869 return client_stop(client, r);
874 static int client_receive_message_udp(sd_event_source *s, int fd,
875 uint32_t revents, void *userdata) {
876 sd_dhcp_client *client = userdata;
877 _cleanup_free_ DHCPMessage *message = NULL;
878 int buflen = 0, len, r;
883 assert(client->event);
885 r = ioctl(fd, FIONREAD, &buflen);
886 if (r < 0 || buflen <= 0)
887 buflen = sizeof(DHCPMessage) + DHCP_MIN_OPTIONS_SIZE;
889 message = malloc0(buflen);
893 len = read(fd, message, buflen);
897 r = sd_event_get_now_monotonic(client->event, &time_now);
899 return client_stop(client, r);
901 return client_handle_message(client, message, len,
905 static int client_receive_message_raw(sd_event_source *s, int fd,
906 uint32_t revents, void *userdata) {
907 sd_dhcp_client *client = userdata;
908 _cleanup_free_ DHCPPacket *packet = NULL;
910 uint8_t cmsgbuf[CMSG_LEN(sizeof(struct tpacket_auxdata))];
911 struct iovec iov = {};
912 struct msghdr msg = {
915 .msg_control = cmsgbuf,
916 .msg_controllen = sizeof(cmsgbuf),
918 struct cmsghdr *cmsg;
919 bool checksum = true;
920 int buflen = 0, len, r;
924 assert(client->event);
926 r = ioctl(fd, FIONREAD, &buflen);
927 if (r < 0 || buflen <= 0)
928 buflen = sizeof(DHCPPacket) + DHCP_MIN_OPTIONS_SIZE;
930 packet = malloc0(buflen);
934 iov.iov_base = packet;
935 iov.iov_len = buflen;
937 len = recvmsg(fd, &msg, 0);
939 log_dhcp_client(client, "could not receive message from raw "
940 "socket: %s", strerror(errno));
944 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
945 if (cmsg->cmsg_level == SOL_PACKET && cmsg->cmsg_type == PACKET_AUXDATA) {
946 struct tpacket_auxdata *aux = (void *)CMSG_DATA(cmsg);
948 checksum = !(aux->tp_status & TP_STATUS_CSUMNOTREADY);
953 r = dhcp_packet_verify_headers(packet, len, checksum);
957 len -= DHCP_IP_UDP_SIZE;
959 r = sd_event_get_now_monotonic(client->event, &time_now);
961 return client_stop(client, r);
963 return client_handle_message(client, &packet->dhcp, len, time_now);
966 int sd_dhcp_client_start(sd_dhcp_client *client) {
969 assert_return(client, -EINVAL);
970 assert_return(client->event, -EINVAL);
971 assert_return(client->index > 0, -EINVAL);
972 assert_return(client->state == DHCP_STATE_INIT ||
973 client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
975 client->xid = random_u32();
977 r = dhcp_network_bind_raw_socket(client->index, &client->link);
980 client_stop(client, r);
985 client->start_time = now(CLOCK_MONOTONIC);
988 log_dhcp_client(client, "STARTED");
990 return client_initialize_events(client, client_receive_message_raw,
994 int sd_dhcp_client_stop(sd_dhcp_client *client) {
995 return client_stop(client, DHCP_EVENT_STOP);
998 int sd_dhcp_client_attach_event(sd_dhcp_client *client, sd_event *event,
1002 assert_return(client, -EINVAL);
1003 assert_return(!client->event, -EBUSY);
1006 client->event = sd_event_ref(event);
1008 r = sd_event_default(&client->event);
1013 client->event_priority = priority;
1018 int sd_dhcp_client_detach_event(sd_dhcp_client *client) {
1019 assert_return(client, -EINVAL);
1021 client->event = sd_event_unref(client->event);
1026 sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client) {
1030 return client->event;
1033 void sd_dhcp_client_free(sd_dhcp_client *client) {
1037 sd_dhcp_client_stop(client);
1038 sd_dhcp_client_detach_event(client);
1040 free(client->req_opts);
1044 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp_client*, sd_dhcp_client_free);
1045 #define _cleanup_dhcp_client_free_ _cleanup_(sd_dhcp_client_freep)
1047 int sd_dhcp_client_new(sd_dhcp_client **ret) {
1048 _cleanup_dhcp_client_free_ sd_dhcp_client *client = NULL;
1050 assert_return(ret, -EINVAL);
1052 client = new0(sd_dhcp_client, 1);
1056 client->state = DHCP_STATE_INIT;
1059 client->attempt = 1;
1061 client->req_opts_size = ELEMENTSOF(default_req_opts);
1063 client->req_opts = memdup(default_req_opts, client->req_opts_size);
1064 if (!client->req_opts)