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-internal.h"
32 #include "sd-dhcp-client.h"
34 #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
41 be32_t server_address;
46 typedef struct DHCPLease DHCPLease;
48 struct sd_dhcp_client {
51 sd_event_source *timeout_resend;
54 union sockaddr_union link;
55 sd_event_source *receive_message;
59 struct ether_addr mac_addr;
64 sd_event_source *timeout_t1;
65 sd_event_source *timeout_t2;
66 sd_event_source *timeout_expire;
67 sd_dhcp_client_cb_t cb;
72 static const uint8_t default_req_opts[] = {
73 DHCP_OPTION_SUBNET_MASK,
75 DHCP_OPTION_HOST_NAME,
76 DHCP_OPTION_DOMAIN_NAME,
77 DHCP_OPTION_DOMAIN_NAME_SERVER,
78 DHCP_OPTION_NTP_SERVER,
81 static int client_receive_message(sd_event_source *s, int fd,
82 uint32_t revents, void *userdata);
84 int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
87 assert_return(client, -EINVAL);
90 client->userdata = userdata;
95 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option)
99 assert_return(client, -EINVAL);
100 assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
103 case DHCP_OPTION_PAD:
104 case DHCP_OPTION_OVERLOAD:
105 case DHCP_OPTION_MESSAGE_TYPE:
106 case DHCP_OPTION_PARAMETER_REQUEST_LIST:
107 case DHCP_OPTION_END:
114 for (i = 0; i < client->req_opts_size; i++)
115 if (client->req_opts[i] == option)
118 if (!GREEDY_REALLOC(client->req_opts, client->req_opts_size,
119 client->req_opts_size + 1))
122 client->req_opts[client->req_opts_size - 1] = option;
127 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
128 const struct in_addr *last_addr)
130 assert_return(client, -EINVAL);
131 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
134 client->last_addr = last_addr->s_addr;
136 client->last_addr = INADDR_ANY;
141 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index)
143 assert_return(client, -EINVAL);
144 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
145 assert_return(interface_index >= -1, -EINVAL);
147 client->index = interface_index;
152 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
153 const struct ether_addr *addr)
155 assert_return(client, -EINVAL);
156 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
158 memcpy(&client->mac_addr, addr, ETH_ALEN);
163 int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr)
165 assert_return(client, -EINVAL);
166 assert_return(addr, -EINVAL);
168 switch (client->state) {
169 case DHCP_STATE_INIT:
170 case DHCP_STATE_SELECTING:
171 case DHCP_STATE_INIT_REBOOT:
172 case DHCP_STATE_REBOOTING:
173 case DHCP_STATE_REQUESTING:
174 return -EADDRNOTAVAIL;
176 case DHCP_STATE_BOUND:
177 case DHCP_STATE_RENEWING:
178 case DHCP_STATE_REBINDING:
179 addr->s_addr = client->lease->address;
187 int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr)
189 assert_return(client, -EINVAL);
190 assert_return(addr, -EINVAL);
192 switch (client->state) {
193 case DHCP_STATE_INIT:
194 case DHCP_STATE_SELECTING:
195 case DHCP_STATE_INIT_REBOOT:
196 case DHCP_STATE_REBOOTING:
197 case DHCP_STATE_REQUESTING:
198 return -EADDRNOTAVAIL;
200 case DHCP_STATE_BOUND:
201 case DHCP_STATE_RENEWING:
202 case DHCP_STATE_REBINDING:
203 addr->s_addr = client->lease->subnet_mask;
211 int sd_dhcp_client_prefixlen(const struct in_addr *addr)
216 assert_return(addr, -EADDRNOTAVAIL);
218 mask = be32toh(addr->s_addr);
227 int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr)
229 assert_return(client, -EINVAL);
230 assert_return(addr, -EINVAL);
232 switch (client->state) {
233 case DHCP_STATE_INIT:
234 case DHCP_STATE_SELECTING:
235 case DHCP_STATE_INIT_REBOOT:
236 case DHCP_STATE_REBOOTING:
237 case DHCP_STATE_REQUESTING:
238 return -EADDRNOTAVAIL;
240 case DHCP_STATE_BOUND:
241 case DHCP_STATE_RENEWING:
242 case DHCP_STATE_REBINDING:
243 addr->s_addr = client->lease->router;
251 static int client_notify(sd_dhcp_client *client, int event)
254 client->cb(client, event, client->userdata);
259 static int client_stop(sd_dhcp_client *client, int error)
261 assert_return(client, -EINVAL);
262 assert_return(client->state != DHCP_STATE_INIT &&
263 client->state != DHCP_STATE_INIT_REBOOT, -EALREADY);
265 client->receive_message =
266 sd_event_source_unref(client->receive_message);
272 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
274 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
275 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
276 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
280 client_notify(client, error);
282 switch (client->state) {
284 case DHCP_STATE_INIT:
285 case DHCP_STATE_SELECTING:
286 case DHCP_STATE_REQUESTING:
287 case DHCP_STATE_BOUND:
289 client->start_time = 0;
290 client->state = DHCP_STATE_INIT;
293 case DHCP_STATE_INIT_REBOOT:
294 case DHCP_STATE_REBOOTING:
295 case DHCP_STATE_RENEWING:
296 case DHCP_STATE_REBINDING:
303 client->lease = NULL;
309 static int client_packet_init(sd_dhcp_client *client, uint8_t type,
310 DHCPMessage *message, uint16_t secs,
311 uint8_t **opt, size_t *optlen)
316 *opt = (uint8_t *)(message + 1);
322 message->op = BOOTREQUEST;
324 message->hlen = ETHER_ADDR_LEN;
325 message->xid = htobe32(client->xid);
327 /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
328 refuse to issue an DHCP lease if 'secs' is set to zero */
329 message->secs = htobe16(secs);
331 if (client->state == DHCP_STATE_RENEWING ||
332 client->state == DHCP_STATE_REBINDING)
333 message->ciaddr = client->lease->address;
335 memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
343 err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
348 /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
349 Identifier option is not set */
350 err = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
351 ETH_ALEN, &client->mac_addr);
355 if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
356 err = dhcp_option_append(opt, optlen,
357 DHCP_OPTION_PARAMETER_REQUEST_LIST,
358 client->req_opts_size,
363 /* Some DHCP servers will send bigger DHCP packets than the
364 defined default size unless the Maximum Messge Size option
365 is explicitely set */
366 max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
367 DHCP_CLIENT_MIN_OPTIONS_SIZE);
368 err = dhcp_option_append(opt, optlen,
369 DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
378 static uint16_t client_checksum(void *buf, int len)
388 for (i = 0; i < len / 2 ; i++)
397 sum = (sum & 0xffff) + (sum >> 16);
402 static void client_append_ip_headers(DHCPPacket *packet, uint16_t len)
404 packet->ip.version = IPVERSION;
405 packet->ip.ihl = DHCP_IP_SIZE / 4;
406 packet->ip.tot_len = htobe16(len);
408 packet->ip.protocol = IPPROTO_UDP;
409 packet->ip.saddr = INADDR_ANY;
410 packet->ip.daddr = INADDR_BROADCAST;
412 packet->udp.source = htobe16(DHCP_PORT_CLIENT);
413 packet->udp.dest = htobe16(DHCP_PORT_SERVER);
414 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
416 packet->ip.check = packet->udp.len;
417 packet->udp.check = client_checksum(&packet->ip.ttl, len - 8);
419 packet->ip.ttl = IPDEFTTL;
420 packet->ip.check = 0;
421 packet->ip.check = client_checksum(&packet->ip, DHCP_IP_SIZE);
424 static int client_send_discover(sd_dhcp_client *client, uint16_t secs)
427 _cleanup_free_ DHCPPacket *discover;
431 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
432 len = sizeof(DHCPPacket) + optlen;
434 discover = malloc0(len);
439 err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
440 secs, &opt, &optlen);
444 if (client->last_addr != INADDR_ANY) {
445 err = dhcp_option_append(&opt, &optlen,
446 DHCP_OPTION_REQUESTED_IP_ADDRESS,
447 4, &client->last_addr);
452 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
456 client_append_ip_headers(discover, len);
458 err = dhcp_network_send_raw_socket(client->fd, &client->link,
464 static int client_send_request(sd_dhcp_client *client, uint16_t secs)
466 _cleanup_free_ DHCPPacket *request;
471 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
472 len = DHCP_MESSAGE_SIZE + optlen;
474 request = malloc0(len);
478 err = client_packet_init(client, DHCP_REQUEST, &request->dhcp, secs,
483 if (client->state == DHCP_STATE_REQUESTING) {
484 err = dhcp_option_append(&opt, &optlen,
485 DHCP_OPTION_REQUESTED_IP_ADDRESS,
486 4, &client->lease->address);
490 err = dhcp_option_append(&opt, &optlen,
491 DHCP_OPTION_SERVER_IDENTIFIER,
492 4, &client->lease->server_address);
497 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
501 if (client->state == DHCP_STATE_RENEWING) {
502 err = dhcp_network_send_udp_socket(client->fd,
503 client->lease->server_address,
505 len - DHCP_IP_UDP_SIZE);
507 client_append_ip_headers(request, len);
509 err = dhcp_network_send_raw_socket(client->fd, &client->link,
516 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
519 sd_dhcp_client *client = userdata;
520 usec_t next_timeout = 0;
525 switch (client->state) {
526 case DHCP_STATE_RENEWING:
528 time_left = (client->lease->t2 - client->lease->t1)/2;
532 next_timeout = usec + time_left * USEC_PER_SEC;
536 case DHCP_STATE_REBINDING:
538 time_left = (client->lease->lifetime - client->lease->t2)/2;
542 next_timeout = usec + time_left * USEC_PER_SEC;
545 case DHCP_STATE_INIT:
546 case DHCP_STATE_INIT_REBOOT:
547 case DHCP_STATE_REBOOTING:
548 case DHCP_STATE_SELECTING:
549 case DHCP_STATE_REQUESTING:
550 case DHCP_STATE_BOUND:
552 if (client->attempt < 64)
553 client->attempt *= 2;
555 next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC;
560 next_timeout += (random_u32() & 0x1fffff);
562 err = sd_event_add_monotonic(client->event, next_timeout,
564 client_timeout_resend, client,
565 &client->timeout_resend);
569 secs = (usec - client->start_time) / USEC_PER_SEC;
571 switch (client->state) {
572 case DHCP_STATE_INIT:
573 err = client_send_discover(client, secs);
575 client->state = DHCP_STATE_SELECTING;
578 if (client->attempt >= 64)
584 case DHCP_STATE_SELECTING:
585 err = client_send_discover(client, secs);
586 if (err < 0 && client->attempt >= 64)
591 case DHCP_STATE_REQUESTING:
592 case DHCP_STATE_RENEWING:
593 case DHCP_STATE_REBINDING:
594 err = client_send_request(client, secs);
595 if (err < 0 && client->attempt >= 64)
598 client->request_sent = usec;
602 case DHCP_STATE_INIT_REBOOT:
603 case DHCP_STATE_REBOOTING:
604 case DHCP_STATE_BOUND:
612 client_stop(client, err);
614 /* Errors were dealt with when stopping the client, don't spill
615 errors into the event loop handler */
619 static int client_initialize_events(sd_dhcp_client *client, usec_t usec)
623 r = sd_event_add_io(client->event, client->fd, EPOLLIN,
624 client_receive_message, client,
625 &client->receive_message);
629 r = sd_event_add_monotonic(client->event, usec, 0,
630 client_timeout_resend, client,
631 &client->timeout_resend);
635 client_stop(client, r);
641 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
644 sd_dhcp_client *client = userdata;
646 client_stop(client, DHCP_EVENT_EXPIRED);
651 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata)
653 sd_dhcp_client *client = userdata;
656 if (client->fd >= 0) {
657 client->receive_message =
658 sd_event_source_unref(client->receive_message);
663 client->state = DHCP_STATE_REBINDING;
666 r = dhcp_network_bind_raw_socket(client->index, &client->link);
668 client_stop(client, r);
674 return client_initialize_events(client, usec);
677 static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata)
679 sd_dhcp_client *client = userdata;
682 client->state = DHCP_STATE_RENEWING;
685 r = dhcp_network_bind_udp_socket(client->index,
686 client->lease->address);
688 client_stop(client, r);
694 return client_initialize_events(client, usec);
697 static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
700 DHCPLease *lease = user_data;
705 case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
707 memcpy(&val, option, 4);
708 lease->lifetime = be32toh(val);
713 case DHCP_OPTION_SERVER_IDENTIFIER:
715 memcpy(&lease->server_address, option, 4);
719 case DHCP_OPTION_SUBNET_MASK:
721 memcpy(&lease->subnet_mask, option, 4);
725 case DHCP_OPTION_ROUTER:
727 memcpy(&lease->router, option, 4);
731 case DHCP_OPTION_RENEWAL_T1_TIME:
733 memcpy(&val, option, 4);
734 lease->t1 = be32toh(val);
739 case DHCP_OPTION_REBINDING_T2_TIME:
741 memcpy(&val, option, 4);
742 lease->t2 = be32toh(val);
751 static int client_verify_headers(sd_dhcp_client *client, DHCPPacket *message,
756 if (len < (DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE))
759 hdrlen = message->ip.ihl * 4;
760 if (hdrlen < 20 || hdrlen > len || client_checksum(&message->ip,
764 message->ip.check = message->udp.len;
767 if (hdrlen + be16toh(message->udp.len) > len ||
768 client_checksum(&message->ip.ttl, be16toh(message->udp.len) + 12))
771 if (be16toh(message->udp.source) != DHCP_PORT_SERVER ||
772 be16toh(message->udp.dest) != DHCP_PORT_CLIENT)
775 if (message->dhcp.op != BOOTREPLY)
778 if (be32toh(message->dhcp.xid) != client->xid)
781 if (memcmp(&message->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet,
788 static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
794 err = client_verify_headers(client, offer, len);
798 lease = new0(DHCPLease, 1);
802 len = len - DHCP_IP_UDP_SIZE;
803 if (dhcp_option_parse(&offer->dhcp, len, client_parse_offer,
804 lease) != DHCP_OFFER)
807 lease->address = offer->dhcp.yiaddr;
809 if (lease->address == INADDR_ANY ||
810 lease->server_address == INADDR_ANY ||
811 lease->subnet_mask == INADDR_ANY ||
812 lease->lifetime == 0)
815 client->lease = lease;
825 static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf,
833 if (client->state == DHCP_STATE_RENEWING) {
834 dhcp = (DHCPMessage *)buf;
836 ack = (DHCPPacket *)buf;
838 r = client_verify_headers(client, ack, len);
843 len -= DHCP_IP_UDP_SIZE;
846 lease = new0(DHCPLease, 1);
850 r = dhcp_option_parse(dhcp, len, client_parse_offer, lease);
853 r = DHCP_EVENT_NO_LEASE;
862 lease->address = dhcp->yiaddr;
864 if (lease->address == INADDR_ANY ||
865 lease->server_address == INADDR_ANY ||
866 lease->subnet_mask == INADDR_ANY || lease->lifetime == 0) {
871 r = DHCP_EVENT_IP_ACQUIRE;
873 if (client->lease->address != lease->address ||
874 client->lease->subnet_mask != lease->subnet_mask ||
875 client->lease->router != lease->router) {
876 r = DHCP_EVENT_IP_CHANGE;
882 client->lease = lease;
892 static uint64_t client_compute_timeout(uint64_t request_sent,
895 return request_sent + (lifetime - 3) * USEC_PER_SEC +
896 + (random_u32() & 0x1fffff);
899 static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec)
902 uint64_t next_timeout;
904 if (client->lease->lifetime < 10)
907 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
908 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
909 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
911 if (!client->lease->t1)
912 client->lease->t1 = client->lease->lifetime / 2;
914 next_timeout = client_compute_timeout(client->request_sent,
916 if (next_timeout < usec)
919 err = sd_event_add_monotonic(client->event, next_timeout,
921 client_timeout_t1, client,
922 &client->timeout_t1);
926 if (!client->lease->t2)
927 client->lease->t2 = client->lease->lifetime * 7 / 8;
929 if (client->lease->t2 < client->lease->t1)
932 if (client->lease->lifetime < client->lease->t2)
935 next_timeout = client_compute_timeout(client->request_sent,
937 if (next_timeout < usec)
940 err = sd_event_add_monotonic(client->event, next_timeout,
942 client_timeout_t2, client,
943 &client->timeout_t2);
947 next_timeout = client_compute_timeout(client->request_sent,
948 client->lease->lifetime);
949 if (next_timeout < usec)
952 err = sd_event_add_monotonic(client->event, next_timeout,
954 client_timeout_expire, client,
955 &client->timeout_expire);
962 static int client_receive_message(sd_event_source *s, int fd,
963 uint32_t revents, void *userdata)
965 sd_dhcp_client *client = userdata;
966 uint8_t buf[sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE];
967 int buflen = sizeof(buf);
968 int len, r = 0, notify_event = 0;
972 len = read(fd, &buf, buflen);
976 r = sd_event_get_now_monotonic(client->event, &time_now);
980 switch (client->state) {
981 case DHCP_STATE_SELECTING:
983 message = (DHCPPacket *)&buf;
985 if (client_receive_offer(client, message, len) >= 0) {
987 client->timeout_resend =
988 sd_event_source_unref(client->timeout_resend);
990 client->state = DHCP_STATE_REQUESTING;
993 r = sd_event_add_monotonic(client->event, time_now, 0,
994 client_timeout_resend,
996 &client->timeout_resend);
1003 case DHCP_STATE_REQUESTING:
1004 case DHCP_STATE_RENEWING:
1005 case DHCP_STATE_REBINDING:
1007 r = client_receive_ack(client, buf, len);
1009 if (r == DHCP_EVENT_NO_LEASE)
1013 client->timeout_resend =
1014 sd_event_source_unref(client->timeout_resend);
1016 if (client->state == DHCP_STATE_REQUESTING)
1017 notify_event = DHCP_EVENT_IP_ACQUIRE;
1018 else if (r != DHCP_EVENT_IP_ACQUIRE)
1021 client->state = DHCP_STATE_BOUND;
1022 client->attempt = 1;
1024 client->last_addr = client->lease->address;
1026 r = client_set_lease_timeouts(client, time_now);
1031 client_notify(client, notify_event);
1033 client->receive_message =
1034 sd_event_source_unref(client->receive_message);
1043 case DHCP_STATE_INIT:
1044 case DHCP_STATE_INIT_REBOOT:
1045 case DHCP_STATE_REBOOTING:
1046 case DHCP_STATE_BOUND:
1052 if (r < 0 || r == DHCP_EVENT_NO_LEASE)
1053 return client_stop(client, r);
1058 int sd_dhcp_client_start(sd_dhcp_client *client)
1062 assert_return(client, -EINVAL);
1063 assert_return(client->index > 0, -EINVAL);
1064 assert_return(client->state == DHCP_STATE_INIT ||
1065 client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
1067 client->xid = random_u32();
1069 r = dhcp_network_bind_raw_socket(client->index, &client->link);
1072 client_stop(client, r);
1077 client->start_time = now(CLOCK_MONOTONIC);
1079 return client_initialize_events(client, client->start_time);
1082 int sd_dhcp_client_stop(sd_dhcp_client *client)
1084 return client_stop(client, DHCP_EVENT_STOP);
1087 sd_dhcp_client *sd_dhcp_client_free(sd_dhcp_client *client)
1089 assert_return(client, NULL);
1091 sd_dhcp_client_stop(client);
1093 sd_event_unref(client->event);
1094 free(client->req_opts);
1100 sd_dhcp_client *sd_dhcp_client_new(sd_event *event)
1102 sd_dhcp_client *client;
1104 assert_return(event, NULL);
1106 client = new0(sd_dhcp_client, 1);
1110 client->event = sd_event_ref(event);
1111 client->state = DHCP_STATE_INIT;
1114 client->attempt = 1;
1116 client->req_opts_size = ELEMENTSOF(default_req_opts);
1118 client->req_opts = memdup(default_req_opts, client->req_opts_size);
1119 if (!client->req_opts) {