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;
51 struct ether_addr mac_addr;
58 sd_event_source *timeout_t1;
59 sd_event_source *timeout_t2;
60 sd_event_source *timeout_expire;
61 sd_dhcp_client_cb_t cb;
66 static const uint8_t default_req_opts[] = {
67 DHCP_OPTION_SUBNET_MASK,
69 DHCP_OPTION_HOST_NAME,
70 DHCP_OPTION_DOMAIN_NAME,
71 DHCP_OPTION_DOMAIN_NAME_SERVER,
72 DHCP_OPTION_NTP_SERVER,
75 static int client_receive_message_raw(sd_event_source *s, int fd,
76 uint32_t revents, void *userdata);
77 static int client_receive_message_udp(sd_event_source *s, int fd,
78 uint32_t revents, void *userdata);
80 int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
82 assert_return(client, -EINVAL);
85 client->userdata = userdata;
90 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) {
93 assert_return(client, -EINVAL);
94 assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
98 case DHCP_OPTION_OVERLOAD:
99 case DHCP_OPTION_MESSAGE_TYPE:
100 case DHCP_OPTION_PARAMETER_REQUEST_LIST:
101 case DHCP_OPTION_END:
108 for (i = 0; i < client->req_opts_size; i++)
109 if (client->req_opts[i] == option)
112 if (!GREEDY_REALLOC(client->req_opts, client->req_opts_allocated,
113 client->req_opts_size + 1))
116 client->req_opts[client->req_opts_size++] = option;
121 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
122 const struct in_addr *last_addr) {
123 assert_return(client, -EINVAL);
124 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
127 client->last_addr = last_addr->s_addr;
129 client->last_addr = INADDR_ANY;
134 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) {
135 assert_return(client, -EINVAL);
136 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
137 assert_return(interface_index >= -1, -EINVAL);
139 client->index = interface_index;
144 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
145 const struct ether_addr *addr) {
146 bool need_restart = false;
148 assert_return(client, -EINVAL);
149 assert_return(addr, -EINVAL);
151 if (memcmp(&client->client_id.mac_addr, addr, ETH_ALEN) == 0)
154 if (client->state != DHCP_STATE_INIT) {
155 log_dhcp_client(client, "Changing MAC address on running DHCP "
156 "client, restarting");
157 sd_dhcp_client_stop(client);
161 memcpy(&client->client_id.mac_addr, addr, ETH_ALEN);
162 client->client_id.type = 0x01;
165 sd_dhcp_client_start(client);
170 int sd_dhcp_client_get_lease(sd_dhcp_client *client, sd_dhcp_lease **ret) {
171 assert_return(client, -EINVAL);
172 assert_return(ret, -EINVAL);
174 if (client->state != DHCP_STATE_BOUND &&
175 client->state != DHCP_STATE_RENEWING &&
176 client->state != DHCP_STATE_REBINDING)
177 return -EADDRNOTAVAIL;
179 *ret = sd_dhcp_lease_ref(client->lease);
184 static int client_notify(sd_dhcp_client *client, int event) {
186 client->cb(client, event, client->userdata);
191 static int client_initialize(sd_dhcp_client *client) {
192 assert_return(client, -EINVAL);
194 client->receive_message =
195 sd_event_source_unref(client->receive_message);
197 client->fd = safe_close(client->fd);
199 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
201 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
202 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
203 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
207 client->state = DHCP_STATE_INIT;
211 client->lease = sd_dhcp_lease_unref(client->lease);
216 static int client_stop(sd_dhcp_client *client, int error) {
217 assert_return(client, -EINVAL);
219 client_notify(client, error);
221 client_initialize(client);
223 log_dhcp_client(client, "STOPPED");
228 static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
229 uint8_t type, uint8_t **opt, size_t *optlen) {
233 assert(client->secs);
238 r = dhcp_message_init(message, BOOTREQUEST, client->xid, type, opt,
243 /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
244 refuse to issue an DHCP lease if 'secs' is set to zero */
245 message->secs = htobe16(client->secs);
247 memcpy(&message->chaddr, &client->client_id.mac_addr, ETH_ALEN);
249 if (client->state == DHCP_STATE_RENEWING ||
250 client->state == DHCP_STATE_REBINDING)
251 message->ciaddr = client->lease->address;
253 /* Some DHCP servers will refuse to issue an DHCP lease if the Client
254 Identifier option is not set */
255 r = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
256 sizeof(client->client_id), &client->client_id);
260 if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
263 r = dhcp_option_append(opt, optlen,
264 DHCP_OPTION_PARAMETER_REQUEST_LIST,
265 client->req_opts_size,
270 /* Some DHCP servers will send bigger DHCP packets than the
271 defined default size unless the Maximum Messge Size option
272 is explicitely set */
273 max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
274 DHCP_MIN_OPTIONS_SIZE);
275 r = dhcp_option_append(opt, optlen,
276 DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
285 static int dhcp_client_send_raw(sd_dhcp_client *client, DHCPPacket *packet,
287 dhcp_packet_append_ip_headers(packet, INADDR_ANY, DHCP_PORT_CLIENT,
288 INADDR_BROADCAST, DHCP_PORT_SERVER, len);
290 return dhcp_network_send_raw_socket(client->fd, &client->link,
294 static int client_send_discover(sd_dhcp_client *client) {
295 _cleanup_free_ DHCPPacket *discover;
303 r = sd_event_get_now_monotonic(client->event, &time_now);
306 assert(time_now >= client->start_time);
308 /* seconds between sending first and last DISCOVER
309 * must always be strictly positive to deal with broken servers */
310 client->secs = ((time_now - client->start_time) / USEC_PER_SEC) ? : 1;
312 optlen = DHCP_MIN_OPTIONS_SIZE;
313 len = sizeof(DHCPPacket) + optlen;
315 discover = malloc0(len);
319 r = client_message_init(client, &discover->dhcp, DHCP_DISCOVER,
324 if (client->last_addr != INADDR_ANY) {
325 r = dhcp_option_append(&opt, &optlen,
326 DHCP_OPTION_REQUESTED_IP_ADDRESS,
327 4, &client->last_addr);
332 r = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
336 r = dhcp_client_send_raw(client, discover, len - optlen);
340 log_dhcp_client(client, "DISCOVER");
345 static int client_send_request(sd_dhcp_client *client) {
346 _cleanup_free_ DHCPPacket *request;
351 optlen = DHCP_MIN_OPTIONS_SIZE;
352 len = sizeof(DHCPPacket) + optlen;
354 request = malloc0(len);
358 r = client_message_init(client, &request->dhcp, DHCP_REQUEST, &opt,
363 switch (client->state) {
365 case DHCP_STATE_INIT_REBOOT:
366 r = dhcp_option_append(&opt, &optlen,
367 DHCP_OPTION_REQUESTED_IP_ADDRESS,
368 4, &client->last_addr);
373 case DHCP_STATE_REQUESTING:
374 r = dhcp_option_append(&opt, &optlen,
375 DHCP_OPTION_REQUESTED_IP_ADDRESS,
376 4, &client->lease->address);
380 r = dhcp_option_append(&opt, &optlen,
381 DHCP_OPTION_SERVER_IDENTIFIER,
382 4, &client->lease->server_address);
387 case DHCP_STATE_INIT:
388 case DHCP_STATE_SELECTING:
389 case DHCP_STATE_REBOOTING:
390 case DHCP_STATE_BOUND:
391 case DHCP_STATE_RENEWING:
392 case DHCP_STATE_REBINDING:
397 r = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
401 if (client->state == DHCP_STATE_RENEWING) {
402 r = dhcp_network_send_udp_socket(client->fd,
403 client->lease->server_address,
406 len - optlen - DHCP_IP_UDP_SIZE);
408 r = dhcp_client_send_raw(client, request, len - optlen);
413 log_dhcp_client(client, "REQUEST");
418 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
420 sd_dhcp_client *client = userdata;
421 usec_t next_timeout = 0;
428 assert(client->event);
430 r = sd_event_get_now_monotonic(client->event, &time_now);
434 switch (client->state) {
435 case DHCP_STATE_RENEWING:
437 time_left = (client->lease->t2 - client->lease->t1) / 2;
441 next_timeout = time_now + time_left * USEC_PER_SEC;
445 case DHCP_STATE_REBINDING:
447 time_left = (client->lease->lifetime - client->lease->t2) / 2;
451 next_timeout = time_now + time_left * USEC_PER_SEC;
454 case DHCP_STATE_REBOOTING:
455 /* start over as we did not receive a timely ack or nak */
456 client->state = DHCP_STATE_INIT;
458 client->xid = random_u32();
461 case DHCP_STATE_INIT:
462 case DHCP_STATE_INIT_REBOOT:
463 case DHCP_STATE_SELECTING:
464 case DHCP_STATE_REQUESTING:
465 case DHCP_STATE_BOUND:
467 if (client->attempt < 64)
468 client->attempt *= 2;
470 next_timeout = time_now + (client->attempt - 1) * USEC_PER_SEC;
475 next_timeout += (random_u32() & 0x1fffff);
477 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
479 r = sd_event_add_monotonic(client->event,
480 &client->timeout_resend,
483 client_timeout_resend, client);
487 r = sd_event_source_set_priority(client->timeout_resend,
488 client->event_priority);
492 switch (client->state) {
493 case DHCP_STATE_INIT:
494 r = client_send_discover(client);
496 client->state = DHCP_STATE_SELECTING;
499 if (client->attempt >= 64)
505 case DHCP_STATE_SELECTING:
506 r = client_send_discover(client);
507 if (r < 0 && client->attempt >= 64)
512 case DHCP_STATE_INIT_REBOOT:
513 case DHCP_STATE_REQUESTING:
514 case DHCP_STATE_RENEWING:
515 case DHCP_STATE_REBINDING:
516 r = client_send_request(client);
517 if (r < 0 && client->attempt >= 64)
520 if (client->state == DHCP_STATE_INIT_REBOOT)
521 client->state = DHCP_STATE_REBOOTING;
523 client->request_sent = time_now;
527 case DHCP_STATE_REBOOTING:
528 case DHCP_STATE_BOUND:
536 client_stop(client, r);
538 /* Errors were dealt with when stopping the client, don't spill
539 errors into the event loop handler */
543 static int client_initialize_events(sd_dhcp_client *client,
544 sd_event_io_handler_t io_callback) {
548 assert(client->event);
550 r = sd_event_add_io(client->event, &client->receive_message,
551 client->fd, EPOLLIN, io_callback,
556 r = sd_event_source_set_priority(client->receive_message,
557 client->event_priority);
561 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
563 r = sd_event_add_monotonic(client->event,
564 &client->timeout_resend, 0, 0,
565 client_timeout_resend, client);
569 r = sd_event_source_set_priority(client->timeout_resend,
570 client->event_priority);
574 client_stop(client, r);
580 static int client_start(sd_dhcp_client *client) {
583 assert_return(client, -EINVAL);
584 assert_return(client->event, -EINVAL);
585 assert_return(client->index > 0, -EINVAL);
586 assert_return(client->fd < 0, -EBUSY);
587 assert_return(client->xid == 0, -EINVAL);
588 assert_return(client->state == DHCP_STATE_INIT ||
589 client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
591 client->xid = random_u32();
593 r = dhcp_network_bind_raw_socket(client->index, &client->link);
596 client_stop(client, r);
601 if (client->state == DHCP_STATE_INIT) {
602 client->start_time = now(CLOCK_MONOTONIC);
606 log_dhcp_client(client, "STARTED");
608 return client_initialize_events(client, client_receive_message_raw);
611 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
613 sd_dhcp_client *client = userdata;
615 log_dhcp_client(client, "EXPIRED");
617 client_notify(client, DHCP_EVENT_EXPIRED);
619 /* start over as the lease was lost */
620 client_initialize(client);
621 client_start(client);
626 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) {
627 sd_dhcp_client *client = userdata;
630 client->receive_message = sd_event_source_unref(client->receive_message);
631 client->fd = safe_close(client->fd);
633 client->state = DHCP_STATE_REBINDING;
636 r = dhcp_network_bind_raw_socket(client->index, &client->link);
638 client_stop(client, r);
644 log_dhcp_client(client, "TIMEOUT T2");
646 return client_initialize_events(client, client_receive_message_raw);
649 static int client_timeout_t1(sd_event_source *s, uint64_t usec,
651 sd_dhcp_client *client = userdata;
654 client->state = DHCP_STATE_RENEWING;
657 r = dhcp_network_bind_udp_socket(client->index,
658 client->lease->address,
661 client_stop(client, r);
667 log_dhcp_client(client, "TIMEOUT T1");
669 return client_initialize_events(client, client_receive_message_udp);
672 static int client_handle_offer(sd_dhcp_client *client, DHCPMessage *offer,
674 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
677 r = dhcp_lease_new(&lease);
681 r = dhcp_option_parse(offer, len, dhcp_lease_parse_options, lease);
682 if (r != DHCP_OFFER) {
683 log_dhcp_client(client, "receieved message was not an OFFER, ignoring");
687 lease->next_server = offer->siaddr;
689 lease->address = offer->yiaddr;
691 if (lease->address == INADDR_ANY ||
692 lease->server_address == INADDR_ANY ||
693 lease->lifetime == 0) {
694 log_dhcp_client(client, "receieved lease lacks address, server "
695 "address or lease lifetime, ignoring");
699 if (lease->subnet_mask == INADDR_ANY) {
700 r = dhcp_lease_set_default_subnet_mask(lease);
702 log_dhcp_client(client, "receieved lease lacks subnet "
703 "mask, and a fallback one can not be "
704 "generated, ignoring");
709 client->lease = lease;
712 log_dhcp_client(client, "OFFER");
717 static int client_handle_ack(sd_dhcp_client *client, DHCPMessage *ack,
719 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
722 r = dhcp_lease_new(&lease);
726 r = dhcp_option_parse(ack, len, dhcp_lease_parse_options, lease);
728 log_dhcp_client(client, "NAK");
729 return DHCP_EVENT_NO_LEASE;
733 log_dhcp_client(client, "receieved message was not an ACK, ignoring");
737 lease->next_server = ack->siaddr;
739 lease->address = ack->yiaddr;
741 if (lease->address == INADDR_ANY ||
742 lease->server_address == INADDR_ANY ||
743 lease->lifetime == 0) {
744 log_dhcp_client(client, "receieved lease lacks address, server "
745 "address or lease lifetime, ignoring");
749 if (lease->subnet_mask == INADDR_ANY) {
750 r = dhcp_lease_set_default_subnet_mask(lease);
752 log_dhcp_client(client, "receieved lease lacks subnet "
753 "mask, and a fallback one can not be "
754 "generated, ignoring");
759 r = DHCP_EVENT_IP_ACQUIRE;
761 if (client->lease->address != lease->address ||
762 client->lease->subnet_mask != lease->subnet_mask ||
763 client->lease->router != lease->router) {
764 r = DHCP_EVENT_IP_CHANGE;
767 client->lease = sd_dhcp_lease_unref(client->lease);
770 client->lease = lease;
773 log_dhcp_client(client, "ACK");
778 static uint64_t client_compute_timeout(sd_dhcp_client *client,
779 uint32_t lifetime, double factor) {
781 assert(client->request_sent);
784 return client->request_sent + ((lifetime - 3) * USEC_PER_SEC * factor) +
785 + (random_u32() & 0x1fffff);
788 static int client_set_lease_timeouts(sd_dhcp_client *client) {
790 uint64_t lifetime_timeout;
793 char time_string[FORMAT_TIMESPAN_MAX];
797 assert(client->event);
798 assert(client->lease);
799 assert(client->lease->lifetime);
801 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
802 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
803 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
805 /* don't set timers for infinite leases */
806 if (client->lease->lifetime == 0xffffffff)
809 r = sd_event_get_now_monotonic(client->event, &time_now);
812 assert(client->request_sent <= time_now);
814 /* convert the various timeouts from relative (secs) to absolute (usecs) */
815 lifetime_timeout = client_compute_timeout(client, client->lease->lifetime, 1);
816 if (client->lease->t1 && client->lease->t2) {
817 /* both T1 and T2 are given */
818 if (client->lease->t1 < client->lease->t2 &&
819 client->lease->t2 < client->lease->lifetime) {
820 /* they are both valid */
821 t2_timeout = client_compute_timeout(client, client->lease->t2, 1);
822 t1_timeout = client_compute_timeout(client, client->lease->t1, 1);
825 t2_timeout = client_compute_timeout(client, client->lease->lifetime, 7.0 / 8.0);
826 client->lease->t2 = (client->lease->lifetime * 7) / 8;
827 t1_timeout = client_compute_timeout(client, client->lease->lifetime, 0.5);
828 client->lease->t1 = client->lease->lifetime / 2;
830 } else if (client->lease->t2 && client->lease->t2 < client->lease->lifetime) {
831 /* only T2 is given, and it is valid */
832 t2_timeout = client_compute_timeout(client, client->lease->t2, 1);
833 t1_timeout = client_compute_timeout(client, client->lease->lifetime, 0.5);
834 client->lease->t1 = client->lease->lifetime / 2;
835 if (t2_timeout <= t1_timeout) {
836 /* the computed T1 would be invalid, so discard T2 */
837 t2_timeout = client_compute_timeout(client, client->lease->lifetime, 7.0 / 8.0);
838 client->lease->t2 = (client->lease->lifetime * 7) / 8;
840 } else if (client->lease->t1 && client->lease->t1 < client->lease->lifetime) {
841 /* only T1 is given, and it is valid */
842 t1_timeout = client_compute_timeout(client, client->lease->t1, 1);
843 t2_timeout = client_compute_timeout(client, client->lease->lifetime, 7.0 / 8.0);
844 client->lease->t2 = (client->lease->lifetime * 7) / 8;
845 if (t2_timeout <= t1_timeout) {
846 /* the computed T2 would be invalid, so discard T1 */
847 t2_timeout = client_compute_timeout(client, client->lease->lifetime, 0.5);
848 client->lease->t2 = client->lease->lifetime / 2;
851 /* fall back to the default timeouts */
852 t1_timeout = client_compute_timeout(client, client->lease->lifetime, 0.5);
853 client->lease->t1 = client->lease->lifetime / 2;
854 t2_timeout = client_compute_timeout(client, client->lease->lifetime, 7.0 / 8.0);
855 client->lease->t2 = (client->lease->lifetime * 7) / 8;
858 /* arm lifetime timeout */
859 r = sd_event_add_monotonic(client->event,
860 &client->timeout_expire, lifetime_timeout,
862 client_timeout_expire, client);
866 r = sd_event_source_set_priority(client->timeout_expire,
867 client->event_priority);
871 log_dhcp_client(client, "lease expires in %s",
872 format_timespan(time_string, FORMAT_TIMESPAN_MAX,
873 lifetime_timeout - time_now, 0));
875 /* don't arm earlier timeouts if this has already expired */
876 if (lifetime_timeout <= time_now)
880 r = sd_event_add_monotonic(client->event,
884 client_timeout_t2, client);
888 r = sd_event_source_set_priority(client->timeout_t2,
889 client->event_priority);
893 log_dhcp_client(client, "T2 expires in %s",
894 format_timespan(time_string, FORMAT_TIMESPAN_MAX,
895 t2_timeout - time_now, 0));
897 /* don't arm earlier timeout if this has already expired */
898 if (t2_timeout <= time_now)
902 r = sd_event_add_monotonic(client->event,
906 client_timeout_t1, client);
910 r = sd_event_source_set_priority(client->timeout_t1,
911 client->event_priority);
915 log_dhcp_client(client, "T1 expires in %s",
916 format_timespan(time_string, FORMAT_TIMESPAN_MAX,
917 t1_timeout - time_now, 0));
922 static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
924 int r = 0, notify_event = 0;
927 assert(client->event);
930 if (len < DHCP_MESSAGE_SIZE) {
931 log_dhcp_client(client, "message too small (%d bytes): "
936 if (message->op != BOOTREPLY) {
937 log_dhcp_client(client, "not a BOOTREPLY message: ignoring");
941 if (be32toh(message->xid) != client->xid) {
942 log_dhcp_client(client, "received xid (%u) does not match "
943 "expected (%u): ignoring",
944 be32toh(message->xid), client->xid);
948 if (memcmp(&message->chaddr[0], &client->client_id.mac_addr,
950 log_dhcp_client(client, "received chaddr does not match "
951 "expected: ignoring");
955 switch (client->state) {
956 case DHCP_STATE_SELECTING:
958 r = client_handle_offer(client, message, len);
961 client->timeout_resend =
962 sd_event_source_unref(client->timeout_resend);
964 client->state = DHCP_STATE_REQUESTING;
967 r = sd_event_add_monotonic(client->event,
968 &client->timeout_resend, 0,
969 0, client_timeout_resend,
974 r = sd_event_source_set_priority(client->timeout_resend,
975 client->event_priority);
978 } else if (r == -ENOMSG)
979 /* invalid message, let's ignore it */
984 case DHCP_STATE_REBOOTING:
985 case DHCP_STATE_REQUESTING:
986 case DHCP_STATE_RENEWING:
987 case DHCP_STATE_REBINDING:
989 r = client_handle_ack(client, message, len);
990 if (r == DHCP_EVENT_NO_LEASE) {
992 client->timeout_resend =
993 sd_event_source_unref(client->timeout_resend);
995 if (client->state == DHCP_STATE_REBOOTING) {
996 r = client_initialize(client);
1000 r = client_start(client);
1006 } else if (r >= 0) {
1007 client->timeout_resend =
1008 sd_event_source_unref(client->timeout_resend);
1010 if (IN_SET(client->state, DHCP_STATE_REQUESTING,
1011 DHCP_STATE_REBOOTING))
1012 notify_event = DHCP_EVENT_IP_ACQUIRE;
1013 else if (r != DHCP_EVENT_IP_ACQUIRE)
1016 client->state = DHCP_STATE_BOUND;
1017 client->attempt = 1;
1019 client->last_addr = client->lease->address;
1021 r = client_set_lease_timeouts(client);
1026 client_notify(client, notify_event);
1028 client->receive_message =
1029 sd_event_source_unref(client->receive_message);
1030 client->fd = safe_close(client->fd);
1031 } else if (r == -ENOMSG)
1032 /* invalid message, let's ignore it */
1037 case DHCP_STATE_INIT:
1038 case DHCP_STATE_INIT_REBOOT:
1039 case DHCP_STATE_BOUND:
1045 if (r < 0 || r == DHCP_EVENT_NO_LEASE)
1046 return client_stop(client, r);
1051 static int client_receive_message_udp(sd_event_source *s, int fd,
1052 uint32_t revents, void *userdata) {
1053 sd_dhcp_client *client = userdata;
1054 _cleanup_free_ DHCPMessage *message = NULL;
1055 int buflen = 0, len, r;
1060 r = ioctl(fd, FIONREAD, &buflen);
1061 if (r < 0 || buflen <= 0)
1062 buflen = sizeof(DHCPMessage) + DHCP_MIN_OPTIONS_SIZE;
1064 message = malloc0(buflen);
1068 len = read(fd, message, buflen);
1072 return client_handle_message(client, message, len);
1075 static int client_receive_message_raw(sd_event_source *s, int fd,
1076 uint32_t revents, void *userdata) {
1077 sd_dhcp_client *client = userdata;
1078 _cleanup_free_ DHCPPacket *packet = NULL;
1079 uint8_t cmsgbuf[CMSG_LEN(sizeof(struct tpacket_auxdata))];
1080 struct iovec iov = {};
1081 struct msghdr msg = {
1084 .msg_control = cmsgbuf,
1085 .msg_controllen = sizeof(cmsgbuf),
1087 struct cmsghdr *cmsg;
1088 bool checksum = true;
1089 int buflen = 0, len, r;
1094 r = ioctl(fd, FIONREAD, &buflen);
1095 if (r < 0 || buflen <= 0)
1096 buflen = sizeof(DHCPPacket) + DHCP_MIN_OPTIONS_SIZE;
1098 packet = malloc0(buflen);
1102 iov.iov_base = packet;
1103 iov.iov_len = buflen;
1105 len = recvmsg(fd, &msg, 0);
1107 log_dhcp_client(client, "could not receive message from raw "
1108 "socket: %s", strerror(errno));
1112 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1113 if (cmsg->cmsg_level == SOL_PACKET && cmsg->cmsg_type == PACKET_AUXDATA) {
1114 struct tpacket_auxdata *aux = (void *)CMSG_DATA(cmsg);
1116 checksum = !(aux->tp_status & TP_STATUS_CSUMNOTREADY);
1121 r = dhcp_packet_verify_headers(packet, len, checksum);
1125 len -= DHCP_IP_UDP_SIZE;
1127 return client_handle_message(client, &packet->dhcp, len);
1130 int sd_dhcp_client_start(sd_dhcp_client *client) {
1133 assert_return(client, -EINVAL);
1135 r = client_initialize(client);
1139 if (client->last_addr)
1140 client->state = DHCP_STATE_INIT_REBOOT;
1142 return client_start(client);
1145 int sd_dhcp_client_stop(sd_dhcp_client *client) {
1146 return client_stop(client, DHCP_EVENT_STOP);
1149 int sd_dhcp_client_attach_event(sd_dhcp_client *client, sd_event *event,
1153 assert_return(client, -EINVAL);
1154 assert_return(!client->event, -EBUSY);
1157 client->event = sd_event_ref(event);
1159 r = sd_event_default(&client->event);
1164 client->event_priority = priority;
1169 int sd_dhcp_client_detach_event(sd_dhcp_client *client) {
1170 assert_return(client, -EINVAL);
1172 client->event = sd_event_unref(client->event);
1177 sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client) {
1181 return client->event;
1184 void sd_dhcp_client_free(sd_dhcp_client *client) {
1188 sd_dhcp_client_stop(client);
1189 sd_dhcp_client_detach_event(client);
1191 free(client->req_opts);
1195 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp_client*, sd_dhcp_client_free);
1196 #define _cleanup_dhcp_client_free_ _cleanup_(sd_dhcp_client_freep)
1198 int sd_dhcp_client_new(sd_dhcp_client **ret) {
1199 _cleanup_dhcp_client_free_ sd_dhcp_client *client = NULL;
1201 assert_return(ret, -EINVAL);
1203 client = new0(sd_dhcp_client, 1);
1207 client->state = DHCP_STATE_INIT;
1210 client->attempt = 1;
1212 client->req_opts_size = ELEMENTSOF(default_req_opts);
1214 client->req_opts = memdup(default_req_opts, client->req_opts_size);
1215 if (!client->req_opts)