1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2014 Intel Corporation. All rights reserved.
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/ioctl.h>
27 #include "udev-util.h"
29 #include "siphash24.h"
33 #include "network-internal.h"
34 #include "sd-dhcp6-client.h"
35 #include "dhcp6-protocol.h"
36 #include "dhcp6-internal.h"
37 #include "dhcp6-lease-internal.h"
39 #define SYSTEMD_PEN 43793
40 #define HASH_KEY SD_ID128_MAKE(80,11,8c,c2,fe,4a,03,ee,3e,d6,0c,6f,36,39,14,09)
42 struct sd_dhcp6_client {
45 enum DHCP6State state;
49 struct ether_addr mac_addr;
51 be32_t transaction_id;
52 struct sd_dhcp6_lease *lease;
55 size_t req_opts_allocated;
57 sd_event_source *receive_message;
58 usec_t retransmit_time;
59 uint8_t retransmit_count;
60 sd_event_source *timeout_resend;
61 sd_event_source *timeout_resend_expire;
62 sd_dhcp6_client_cb_t cb;
66 uint16_t type; /* DHCP6_DUID_EN */
72 static const uint16_t default_req_opts[] = {
73 DHCP6_OPTION_DNS_SERVERS,
74 DHCP6_OPTION_DOMAIN_LIST,
75 DHCP6_OPTION_NTP_SERVER,
78 const char * dhcp6_message_type_table[_DHCP6_MESSAGE_MAX] = {
79 [DHCP6_SOLICIT] = "SOLICIT",
80 [DHCP6_ADVERTISE] = "ADVERTISE",
81 [DHCP6_REQUEST] = "REQUEST",
82 [DHCP6_CONFIRM] = "CONFIRM",
83 [DHCP6_RENEW] = "RENEW",
84 [DHCP6_REBIND] = "REBIND",
85 [DHCP6_REPLY] = "REPLY",
86 [DHCP6_RELEASE] = "RELEASE",
87 [DHCP6_DECLINE] = "DECLINE",
88 [DHCP6_RECONFIGURE] = "RECONFIGURE",
89 [DHCP6_INFORMATION_REQUEST] = "INFORMATION-REQUEST",
90 [DHCP6_RELAY_FORW] = "RELAY-FORW",
91 [DHCP6_RELAY_REPL] = "RELAY-REPL",
94 DEFINE_STRING_TABLE_LOOKUP(dhcp6_message_type, int);
96 const char * dhcp6_message_status_table[_DHCP6_STATUS_MAX] = {
97 [DHCP6_STATUS_SUCCESS] = "Success",
98 [DHCP6_STATUS_UNSPEC_FAIL] = "Unspecified failure",
99 [DHCP6_STATUS_NO_ADDRS_AVAIL] = "No addresses available",
100 [DHCP6_STATUS_NO_BINDING] = "Binding unavailable",
101 [DHCP6_STATUS_NOT_ON_LINK] = "Not on link",
102 [DHCP6_STATUS_USE_MULTICAST] = "Use multicast",
105 DEFINE_STRING_TABLE_LOOKUP(dhcp6_message_status, int);
107 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp6_client*, sd_dhcp6_client_unref);
108 #define _cleanup_dhcp6_client_unref_ _cleanup_(sd_dhcp6_client_unrefp)
110 #define DHCP6_CLIENT_DONT_DESTROY(client) \
111 _cleanup_dhcp6_client_unref_ _unused_ sd_dhcp6_client *_dont_destroy_##client = sd_dhcp6_client_ref(client)
113 static int client_start(sd_dhcp6_client *client, enum DHCP6State state);
115 int sd_dhcp6_client_set_callback(sd_dhcp6_client *client,
116 sd_dhcp6_client_cb_t cb, void *userdata)
118 assert_return(client, -EINVAL);
121 client->userdata = userdata;
126 int sd_dhcp6_client_set_index(sd_dhcp6_client *client, int interface_index)
128 assert_return(client, -EINVAL);
129 assert_return(interface_index >= -1, -EINVAL);
131 client->index = interface_index;
136 int sd_dhcp6_client_set_mac(sd_dhcp6_client *client,
137 const struct ether_addr *mac_addr)
139 assert_return(client, -EINVAL);
142 memcpy(&client->mac_addr, mac_addr, sizeof(client->mac_addr));
144 memset(&client->mac_addr, 0x00, sizeof(client->mac_addr));
149 int sd_dhcp6_client_set_request_option(sd_dhcp6_client *client,
153 assert_return(client, -EINVAL);
154 assert_return(client->state == DHCP6_STATE_STOPPED, -EBUSY);
157 case DHCP6_OPTION_DNS_SERVERS:
158 case DHCP6_OPTION_DOMAIN_LIST:
159 case DHCP6_OPTION_SNTP_SERVERS:
160 case DHCP6_OPTION_NTP_SERVER:
167 for (t = 0; t < client->req_opts_len; t++)
168 if (client->req_opts[t] == htobe16(option))
171 if (!GREEDY_REALLOC(client->req_opts, client->req_opts_allocated,
172 client->req_opts_len + 1))
175 client->req_opts[client->req_opts_len++] = htobe16(option);
180 int sd_dhcp6_client_get_lease(sd_dhcp6_client *client, sd_dhcp6_lease **ret) {
181 assert_return(client, -EINVAL);
182 assert_return(ret, -EINVAL);
187 *ret = sd_dhcp6_lease_ref(client->lease);
192 static void client_notify(sd_dhcp6_client *client, int event) {
194 client->cb(client, event, client->userdata);
197 static int client_reset(sd_dhcp6_client *client) {
198 assert_return(client, -EINVAL);
200 client->receive_message =
201 sd_event_source_unref(client->receive_message);
203 client->fd = safe_close(client->fd);
205 client->transaction_id = 0;
207 client->ia_na.timeout_t1 =
208 sd_event_source_unref(client->ia_na.timeout_t1);
209 client->ia_na.timeout_t2 =
210 sd_event_source_unref(client->ia_na.timeout_t2);
212 client->retransmit_time = 0;
213 client->retransmit_count = 0;
214 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
215 client->timeout_resend_expire =
216 sd_event_source_unref(client->timeout_resend_expire);
218 client->state = DHCP6_STATE_STOPPED;
223 static void client_stop(sd_dhcp6_client *client, int error) {
224 DHCP6_CLIENT_DONT_DESTROY(client);
228 client_notify(client, error);
230 client_reset(client);
233 static int client_send_message(sd_dhcp6_client *client) {
234 _cleanup_free_ DHCP6Message *message = NULL;
235 struct in6_addr all_servers =
236 IN6ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS_INIT;
237 size_t len, optlen = 512;
241 len = sizeof(DHCP6Message) + optlen;
243 message = malloc0(len);
247 opt = (uint8_t *)(message + 1);
249 message->transaction_id = client->transaction_id;
251 switch(client->state) {
252 case DHCP6_STATE_SOLICITATION:
253 message->type = DHCP6_SOLICIT;
255 r = dhcp6_option_append(&opt, &optlen,
256 DHCP6_OPTION_RAPID_COMMIT, 0, NULL);
258 r = dhcp6_option_append_ia(&opt, &optlen, &client->ia_na);
264 case DHCP6_STATE_REQUEST:
265 case DHCP6_STATE_RENEW:
267 if (client->state == DHCP6_STATE_REQUEST)
268 message->type = DHCP6_REQUEST;
270 message->type = DHCP6_RENEW;
272 r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_SERVERID,
273 client->lease->serverid_len,
274 client->lease->serverid);
278 r = dhcp6_option_append_ia(&opt, &optlen, &client->lease->ia);
284 case DHCP6_STATE_REBIND:
285 message->type = DHCP6_REBIND;
287 r = dhcp6_option_append_ia(&opt, &optlen, &client->lease->ia);
293 case DHCP6_STATE_STOPPED:
295 case DHCP6_STATE_BOUND:
299 r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_ORO,
300 client->req_opts_len * sizeof(be16_t),
305 r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_CLIENTID,
306 sizeof(client->duid), &client->duid);
310 r = dhcp6_network_send_udp_socket(client->fd, &all_servers, message,
315 log_dhcp6_client(client, "Sent %s",
316 dhcp6_message_type_to_string(message->type));
321 static int client_timeout_t2(sd_event_source *s, uint64_t usec,
323 sd_dhcp6_client *client = userdata;
325 assert_return(s, -EINVAL);
326 assert_return(client, -EINVAL);
327 assert_return(client->lease, -EINVAL);
329 client->lease->ia.timeout_t2 =
330 sd_event_source_unref(client->lease->ia.timeout_t2);
332 log_dhcp6_client(client, "Timeout T2");
334 client_start(client, DHCP6_STATE_REBIND);
339 static int client_timeout_t1(sd_event_source *s, uint64_t usec,
341 sd_dhcp6_client *client = userdata;
343 assert_return(s, -EINVAL);
344 assert_return(client, -EINVAL);
345 assert_return(client->lease, -EINVAL);
347 client->lease->ia.timeout_t1 =
348 sd_event_source_unref(client->lease->ia.timeout_t1);
350 log_dhcp6_client(client, "Timeout T1");
352 client_start(client, DHCP6_STATE_RENEW);
357 static int client_timeout_resend_expire(sd_event_source *s, uint64_t usec,
359 sd_dhcp6_client *client = userdata;
360 DHCP6_CLIENT_DONT_DESTROY(client);
361 enum DHCP6State state;
365 assert(client->event);
367 state = client->state;
369 client_stop(client, DHCP6_EVENT_RESEND_EXPIRE);
371 /* RFC 3315, section 18.1.4., says that "...the client may choose to
372 use a Solicit message to locate a new DHCP server..." */
373 if (state == DHCP6_STATE_REBIND)
374 client_start(client, DHCP6_STATE_SOLICITATION);
379 static usec_t client_timeout_compute_random(usec_t val) {
380 return val - val / 10 +
381 (random_u32() % (2 * USEC_PER_SEC)) * val / 10 / USEC_PER_SEC;
384 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
387 sd_dhcp6_client *client = userdata;
388 usec_t time_now, init_retransmit_time, max_retransmit_time;
389 usec_t max_retransmit_duration;
390 uint8_t max_retransmit_count = 0;
391 char time_string[FORMAT_TIMESPAN_MAX];
396 assert(client->event);
398 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
400 switch (client->state) {
401 case DHCP6_STATE_SOLICITATION:
403 if (client->retransmit_count && client->lease) {
404 client_start(client, DHCP6_STATE_REQUEST);
408 init_retransmit_time = DHCP6_SOL_TIMEOUT;
409 max_retransmit_time = DHCP6_SOL_MAX_RT;
410 max_retransmit_count = 0;
411 max_retransmit_duration = 0;
415 case DHCP6_STATE_REQUEST:
416 init_retransmit_time = DHCP6_REQ_TIMEOUT;
417 max_retransmit_time = DHCP6_REQ_MAX_RT;
418 max_retransmit_count = DHCP6_REQ_MAX_RC;
419 max_retransmit_duration = 0;
423 case DHCP6_STATE_RENEW:
424 init_retransmit_time = DHCP6_REN_TIMEOUT;
425 max_retransmit_time = DHCP6_REN_MAX_RT;
426 max_retransmit_count = 0;
428 /* RFC 3315, section 18.1.3. says max retransmit duration will
429 be the remaining time until T2. Instead of setting MRD,
430 wait for T2 to trigger with the same end result */
431 max_retransmit_duration = 0;
435 case DHCP6_STATE_REBIND:
436 init_retransmit_time = DHCP6_REB_TIMEOUT;
437 max_retransmit_time = DHCP6_REB_MAX_RT;
438 max_retransmit_count = 0;
440 max_retransmit_duration = 0;
442 if (!client->timeout_resend_expire) {
443 r = dhcp6_lease_ia_rebind_expire(&client->lease->ia,
446 client_stop(client, r);
449 max_retransmit_duration = expire * USEC_PER_SEC;
454 case DHCP6_STATE_STOPPED:
456 case DHCP6_STATE_BOUND:
460 if (max_retransmit_count &&
461 client->retransmit_count >= max_retransmit_count) {
462 client_stop(client, DHCP6_EVENT_RETRANS_MAX);
466 r = client_send_message(client);
468 client->retransmit_count++;
471 r = sd_event_now(client->event, CLOCK_MONOTONIC, &time_now);
475 if (!client->retransmit_time) {
476 client->retransmit_time =
477 client_timeout_compute_random(init_retransmit_time);
479 if (client->state == DHCP6_STATE_SOLICITATION)
480 client->retransmit_time += init_retransmit_time / 10;
483 if (max_retransmit_time &&
484 client->retransmit_time > max_retransmit_time / 2)
485 client->retransmit_time = client_timeout_compute_random(max_retransmit_time);
487 client->retransmit_time += client_timeout_compute_random(client->retransmit_time);
490 log_dhcp6_client(client, "Next retransmission in %s",
491 format_timespan(time_string, FORMAT_TIMESPAN_MAX,
492 client->retransmit_time, 0));
494 r = sd_event_add_time(client->event, &client->timeout_resend,
496 time_now + client->retransmit_time,
497 10 * USEC_PER_MSEC, client_timeout_resend,
502 r = sd_event_source_set_priority(client->timeout_resend,
503 client->event_priority);
507 if (max_retransmit_duration && !client->timeout_resend_expire) {
509 log_dhcp6_client(client, "Max retransmission duration %"PRIu64" secs",
510 max_retransmit_duration / USEC_PER_SEC);
512 r = sd_event_add_time(client->event,
513 &client->timeout_resend_expire,
515 time_now + max_retransmit_duration,
517 client_timeout_resend_expire, client);
521 r = sd_event_source_set_priority(client->timeout_resend_expire,
522 client->event_priority);
529 client_stop(client, r);
534 static int client_ensure_iaid(sd_dhcp6_client *client) {
535 const char *name = NULL;
540 if (client->ia_na.id)
543 if (detect_container(NULL) <= 0) {
544 /* not in a container, udev will be around */
545 _cleanup_udev_unref_ struct udev *udev;
546 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
547 char ifindex_str[2 + DECIMAL_STR_MAX(int)];
553 sprintf(ifindex_str, "n%d", client->index);
554 device = udev_device_new_from_device_id(udev, ifindex_str);
558 if (udev_device_get_is_initialized(device) <= 0)
562 name = net_get_name(device);
566 siphash24((uint8_t*)&id, name, strlen(name), HASH_KEY.bytes);
568 /* fall back to mac address if no predictable name available */
569 siphash24((uint8_t*)&id, &client->mac_addr, ETH_ALEN,
572 /* fold into 32 bits */
573 client->ia_na.id = (id & 0xffffffff) ^ (id >> 32);
578 static int client_parse_message(sd_dhcp6_client *client,
579 DHCP6Message *message, size_t len,
580 sd_dhcp6_lease *lease) {
582 uint8_t *optval, *option = (uint8_t *)(message + 1), *id = NULL;
583 uint16_t optcode, status;
584 size_t optlen, id_len;
585 bool clientid = false;
588 while ((r = dhcp6_option_parse(&option, &len, &optcode, &optlen,
591 case DHCP6_OPTION_CLIENTID:
593 log_dhcp6_client(client, "%s contains multiple clientids",
594 dhcp6_message_type_to_string(message->type));
598 if (optlen != sizeof(client->duid) ||
599 memcmp(&client->duid, optval, optlen) != 0) {
600 log_dhcp6_client(client, "%s DUID does not match",
601 dhcp6_message_type_to_string(message->type));
609 case DHCP6_OPTION_SERVERID:
610 r = dhcp6_lease_get_serverid(lease, &id, &id_len);
612 log_dhcp6_client(client, "%s contains multiple serverids",
613 dhcp6_message_type_to_string(message->type));
617 r = dhcp6_lease_set_serverid(lease, optval, optlen);
623 case DHCP6_OPTION_PREFERENCE:
627 r = dhcp6_lease_set_preference(lease, *optval);
633 case DHCP6_OPTION_STATUS_CODE:
637 status = optval[0] << 8 | optval[1];
639 log_dhcp6_client(client, "%s Status %s",
640 dhcp6_message_type_to_string(message->type),
641 dhcp6_message_status_to_string(status));
647 case DHCP6_OPTION_IA_NA:
648 r = dhcp6_option_parse_ia(&optval, &optlen, optcode,
650 if (r < 0 && r != -ENOMSG)
653 r = dhcp6_lease_get_iaid(lease, &iaid_lease);
657 if (client->ia_na.id != iaid_lease) {
658 log_dhcp6_client(client, "%s has wrong IAID",
659 dhcp6_message_type_to_string(message->type));
665 case DHCP6_OPTION_RAPID_COMMIT:
666 r = dhcp6_lease_set_rapid_commit(lease);
674 if ((r < 0 && r != -ENOMSG) || !clientid) {
675 log_dhcp6_client(client, "%s has incomplete options",
676 dhcp6_message_type_to_string(message->type));
680 r = dhcp6_lease_get_serverid(lease, &id, &id_len);
682 log_dhcp6_client(client, "%s has no server id",
683 dhcp6_message_type_to_string(message->type));
688 static int client_receive_reply(sd_dhcp6_client *client, DHCP6Message *reply,
692 _cleanup_dhcp6_lease_free_ sd_dhcp6_lease *lease = NULL;
695 if (reply->type != DHCP6_REPLY)
698 r = dhcp6_lease_new(&lease);
702 r = client_parse_message(client, reply, len, lease);
706 if (client->state == DHCP6_STATE_SOLICITATION) {
707 r = dhcp6_lease_get_rapid_commit(lease, &rapid_commit);
715 dhcp6_lease_clear_timers(&client->lease->ia);
717 client->lease = sd_dhcp6_lease_unref(client->lease);
718 client->lease = lease;
721 return DHCP6_STATE_BOUND;
724 static int client_receive_advertise(sd_dhcp6_client *client,
725 DHCP6Message *advertise, size_t len) {
727 _cleanup_dhcp6_lease_free_ sd_dhcp6_lease *lease = NULL;
728 uint8_t pref_advertise = 0, pref_lease = 0;
730 if (advertise->type != DHCP6_ADVERTISE)
733 r = dhcp6_lease_new(&lease);
737 r = client_parse_message(client, advertise, len, lease);
741 r = dhcp6_lease_get_preference(lease, &pref_advertise);
745 r = dhcp6_lease_get_preference(client->lease, &pref_lease);
746 if (!client->lease || r < 0 || pref_advertise > pref_lease) {
747 sd_dhcp6_lease_unref(client->lease);
748 client->lease = lease;
753 if (pref_advertise == 255 || client->retransmit_count > 1)
754 r = DHCP6_STATE_REQUEST;
759 static int client_receive_message(sd_event_source *s, int fd, uint32_t revents,
761 sd_dhcp6_client *client = userdata;
762 DHCP6_CLIENT_DONT_DESTROY(client);
763 _cleanup_free_ DHCP6Message *message;
768 assert(client->event);
770 r = ioctl(fd, FIONREAD, &buflen);
771 if (r < 0 || buflen <= 0)
772 buflen = DHCP6_MIN_OPTIONS_SIZE;
774 message = malloc0(buflen);
778 len = read(fd, message, buflen);
779 if ((size_t)len < sizeof(DHCP6Message)) {
780 log_dhcp6_client(client, "could not receive message from UDP socket: %m");
784 switch(message->type) {
792 case DHCP6_INFORMATION_REQUEST:
793 case DHCP6_RELAY_FORW:
794 case DHCP6_RELAY_REPL:
797 case DHCP6_ADVERTISE:
799 case DHCP6_RECONFIGURE:
803 log_dhcp6_client(client, "unknown message type %d",
808 if (client->transaction_id != (message->transaction_id &
809 htobe32(0x00ffffff)))
812 switch (client->state) {
813 case DHCP6_STATE_SOLICITATION:
814 r = client_receive_advertise(client, message, len);
816 if (r == DHCP6_STATE_REQUEST) {
817 client_start(client, r);
822 /* fall through for Soliciation Rapid Commit option check */
823 case DHCP6_STATE_REQUEST:
824 case DHCP6_STATE_RENEW:
825 case DHCP6_STATE_REBIND:
827 r = client_receive_reply(client, message, len);
831 if (r == DHCP6_STATE_BOUND) {
833 r = client_start(client, DHCP6_STATE_BOUND);
835 client_stop(client, r);
839 client_notify(client, DHCP6_EVENT_IP_ACQUIRE);
844 case DHCP6_STATE_BOUND:
848 case DHCP6_STATE_STOPPED:
854 log_dhcp6_client(client, "Recv %s",
855 dhcp6_message_type_to_string(message->type));
861 static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
864 usec_t timeout, time_now;
865 char time_string[FORMAT_TIMESPAN_MAX];
867 assert_return(client, -EINVAL);
868 assert_return(client->event, -EINVAL);
869 assert_return(client->index > 0, -EINVAL);
870 assert_return(client->state != state, -EINVAL);
872 client->timeout_resend_expire =
873 sd_event_source_unref(client->timeout_resend_expire);
874 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
875 client->retransmit_time = 0;
876 client->retransmit_count = 0;
879 case DHCP6_STATE_STOPPED:
881 case DHCP6_STATE_SOLICITATION:
883 r = client_ensure_iaid(client);
887 r = dhcp6_network_bind_udp_socket(client->index, NULL);
893 r = sd_event_add_io(client->event, &client->receive_message,
894 client->fd, EPOLLIN, client_receive_message,
899 r = sd_event_source_set_priority(client->receive_message,
900 client->event_priority);
904 client->state = DHCP6_STATE_SOLICITATION;
908 case DHCP6_STATE_REQUEST:
909 case DHCP6_STATE_RENEW:
910 case DHCP6_STATE_REBIND:
912 client->state = state;
916 case DHCP6_STATE_BOUND:
918 r = sd_event_now(client->event, CLOCK_MONOTONIC, &time_now);
922 if (client->lease->ia.lifetime_t1 == 0xffffffff ||
923 client->lease->ia.lifetime_t2 == 0xffffffff) {
925 log_dhcp6_client(client, "infinite T1 0x%08x or T2 0x%08x",
926 be32toh(client->lease->ia.lifetime_t1),
927 be32toh(client->lease->ia.lifetime_t2));
932 timeout = client_timeout_compute_random(be32toh(client->lease->ia.lifetime_t1) * USEC_PER_SEC);
934 log_dhcp6_client(client, "T1 expires in %s",
935 format_timespan(time_string,
939 r = sd_event_add_time(client->event,
940 &client->lease->ia.timeout_t1,
941 CLOCK_MONOTONIC, time_now + timeout,
942 10 * USEC_PER_SEC, client_timeout_t1,
947 r = sd_event_source_set_priority(client->lease->ia.timeout_t1,
948 client->event_priority);
952 timeout = client_timeout_compute_random(be32toh(client->lease->ia.lifetime_t2) * USEC_PER_SEC);
954 log_dhcp6_client(client, "T2 expires in %s",
955 format_timespan(time_string,
959 r = sd_event_add_time(client->event,
960 &client->lease->ia.timeout_t2,
961 CLOCK_MONOTONIC, time_now + timeout,
962 10 * USEC_PER_SEC, client_timeout_t2,
967 r = sd_event_source_set_priority(client->lease->ia.timeout_t2,
968 client->event_priority);
972 client->state = state;
977 client->transaction_id = random_u32() & htobe32(0x00ffffff);
979 r = sd_event_add_time(client->event, &client->timeout_resend,
980 CLOCK_MONOTONIC, 0, 0, client_timeout_resend,
985 r = sd_event_source_set_priority(client->timeout_resend,
986 client->event_priority);
993 int sd_dhcp6_client_stop(sd_dhcp6_client *client)
995 client_stop(client, DHCP6_EVENT_STOP);
1000 int sd_dhcp6_client_start(sd_dhcp6_client *client)
1004 assert_return(client, -EINVAL);
1005 assert_return(client->event, -EINVAL);
1006 assert_return(client->index > 0, -EINVAL);
1008 r = client_reset(client);
1012 return client_start(client, DHCP6_STATE_SOLICITATION);
1015 int sd_dhcp6_client_attach_event(sd_dhcp6_client *client, sd_event *event,
1020 assert_return(client, -EINVAL);
1021 assert_return(!client->event, -EBUSY);
1024 client->event = sd_event_ref(event);
1026 r = sd_event_default(&client->event);
1031 client->event_priority = priority;
1036 int sd_dhcp6_client_detach_event(sd_dhcp6_client *client) {
1037 assert_return(client, -EINVAL);
1039 client->event = sd_event_unref(client->event);
1044 sd_event *sd_dhcp6_client_get_event(sd_dhcp6_client *client) {
1048 return client->event;
1051 sd_dhcp6_client *sd_dhcp6_client_ref(sd_dhcp6_client *client) {
1053 assert_se(REFCNT_INC(client->n_ref) >= 2);
1058 sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client) {
1059 if (client && REFCNT_DEC(client->n_ref) <= 0) {
1060 client_reset(client);
1062 sd_dhcp6_client_detach_event(client);
1064 free(client->req_opts);
1073 int sd_dhcp6_client_new(sd_dhcp6_client **ret)
1075 _cleanup_dhcp6_client_unref_ sd_dhcp6_client *client = NULL;
1076 sd_id128_t machine_id;
1080 assert_return(ret, -EINVAL);
1082 client = new0(sd_dhcp6_client, 1);
1086 client->n_ref = REFCNT_INIT;
1088 client->ia_na.type = DHCP6_OPTION_IA_NA;
1094 /* initialize DUID */
1095 client->duid.type = htobe16(DHCP6_DUID_EN);
1096 client->duid.pen = htobe32(SYSTEMD_PEN);
1098 r = sd_id128_get_machine(&machine_id);
1102 /* a bit of snake-oil perhaps, but no need to expose the machine-id
1104 siphash24(client->duid.id, &machine_id, sizeof(machine_id),
1107 client->req_opts_len = ELEMENTSOF(default_req_opts);
1109 client->req_opts = new0(be16_t, client->req_opts_len);
1110 if (!client->req_opts)
1113 for (t = 0; t < client->req_opts_len; t++)
1114 client->req_opts[t] = htobe16(default_req_opts[t]);