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);
260 r = dhcp6_option_append_ia(&opt, &optlen, &client->ia_na);
266 case DHCP6_STATE_REQUEST:
267 case DHCP6_STATE_RENEW:
269 if (client->state == DHCP6_STATE_REQUEST)
270 message->type = DHCP6_REQUEST;
272 message->type = DHCP6_RENEW;
274 r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_SERVERID,
275 client->lease->serverid_len,
276 client->lease->serverid);
280 r = dhcp6_option_append_ia(&opt, &optlen, &client->lease->ia);
286 case DHCP6_STATE_REBIND:
287 message->type = DHCP6_REBIND;
289 r = dhcp6_option_append_ia(&opt, &optlen, &client->lease->ia);
295 case DHCP6_STATE_STOPPED:
296 case DHCP6_STATE_BOUND:
300 r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_ORO,
301 client->req_opts_len * sizeof(be16_t),
306 r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_CLIENTID,
307 sizeof(client->duid), &client->duid);
311 r = dhcp6_network_send_udp_socket(client->fd, &all_servers, message,
316 log_dhcp6_client(client, "Sent %s",
317 dhcp6_message_type_to_string(message->type));
322 static int client_timeout_t2(sd_event_source *s, uint64_t usec,
324 sd_dhcp6_client *client = userdata;
326 assert_return(s, -EINVAL);
327 assert_return(client, -EINVAL);
328 assert_return(client->lease, -EINVAL);
330 client->lease->ia.timeout_t2 =
331 sd_event_source_unref(client->lease->ia.timeout_t2);
333 log_dhcp6_client(client, "Timeout T2");
335 client_start(client, DHCP6_STATE_REBIND);
340 static int client_timeout_t1(sd_event_source *s, uint64_t usec,
342 sd_dhcp6_client *client = userdata;
344 assert_return(s, -EINVAL);
345 assert_return(client, -EINVAL);
346 assert_return(client->lease, -EINVAL);
348 client->lease->ia.timeout_t1 =
349 sd_event_source_unref(client->lease->ia.timeout_t1);
351 log_dhcp6_client(client, "Timeout T1");
353 client_start(client, DHCP6_STATE_RENEW);
358 static int client_timeout_resend_expire(sd_event_source *s, uint64_t usec,
360 sd_dhcp6_client *client = userdata;
361 DHCP6_CLIENT_DONT_DESTROY(client);
362 enum DHCP6State state;
366 assert(client->event);
368 state = client->state;
370 client_stop(client, DHCP6_EVENT_RESEND_EXPIRE);
372 /* RFC 3315, section 18.1.4., says that "...the client may choose to
373 use a Solicit message to locate a new DHCP server..." */
374 if (state == DHCP6_STATE_REBIND)
375 client_start(client, DHCP6_STATE_SOLICITATION);
380 static usec_t client_timeout_compute_random(usec_t val) {
381 return val - val / 10 +
382 (random_u32() % (2 * USEC_PER_SEC)) * val / 10 / USEC_PER_SEC;
385 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
388 sd_dhcp6_client *client = userdata;
389 usec_t time_now, init_retransmit_time = 0, max_retransmit_time = 0;
390 usec_t max_retransmit_duration = 0;
391 uint8_t max_retransmit_count = 0;
392 char time_string[FORMAT_TIMESPAN_MAX];
397 assert(client->event);
399 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
401 switch (client->state) {
402 case DHCP6_STATE_SOLICITATION:
404 if (client->retransmit_count && client->lease) {
405 client_start(client, DHCP6_STATE_REQUEST);
409 init_retransmit_time = DHCP6_SOL_TIMEOUT;
410 max_retransmit_time = DHCP6_SOL_MAX_RT;
414 case DHCP6_STATE_REQUEST:
415 init_retransmit_time = DHCP6_REQ_TIMEOUT;
416 max_retransmit_time = DHCP6_REQ_MAX_RT;
417 max_retransmit_count = DHCP6_REQ_MAX_RC;
421 case DHCP6_STATE_RENEW:
422 init_retransmit_time = DHCP6_REN_TIMEOUT;
423 max_retransmit_time = DHCP6_REN_MAX_RT;
425 /* RFC 3315, section 18.1.3. says max retransmit duration will
426 be the remaining time until T2. Instead of setting MRD,
427 wait for T2 to trigger with the same end result */
431 case DHCP6_STATE_REBIND:
432 init_retransmit_time = DHCP6_REB_TIMEOUT;
433 max_retransmit_time = DHCP6_REB_MAX_RT;
435 if (!client->timeout_resend_expire) {
436 r = dhcp6_lease_ia_rebind_expire(&client->lease->ia,
439 client_stop(client, r);
442 max_retransmit_duration = expire * USEC_PER_SEC;
447 case DHCP6_STATE_STOPPED:
448 case DHCP6_STATE_BOUND:
452 if (max_retransmit_count &&
453 client->retransmit_count >= max_retransmit_count) {
454 client_stop(client, DHCP6_EVENT_RETRANS_MAX);
458 r = client_send_message(client);
460 client->retransmit_count++;
463 r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now);
467 if (!client->retransmit_time) {
468 client->retransmit_time =
469 client_timeout_compute_random(init_retransmit_time);
471 if (client->state == DHCP6_STATE_SOLICITATION)
472 client->retransmit_time += init_retransmit_time / 10;
475 if (max_retransmit_time &&
476 client->retransmit_time > max_retransmit_time / 2)
477 client->retransmit_time = client_timeout_compute_random(max_retransmit_time);
479 client->retransmit_time += client_timeout_compute_random(client->retransmit_time);
482 log_dhcp6_client(client, "Next retransmission in %s",
483 format_timespan(time_string, FORMAT_TIMESPAN_MAX,
484 client->retransmit_time, 0));
486 r = sd_event_add_time(client->event, &client->timeout_resend,
487 clock_boottime_or_monotonic(),
488 time_now + client->retransmit_time,
489 10 * USEC_PER_MSEC, client_timeout_resend,
494 r = sd_event_source_set_priority(client->timeout_resend,
495 client->event_priority);
499 if (max_retransmit_duration && !client->timeout_resend_expire) {
501 log_dhcp6_client(client, "Max retransmission duration %"PRIu64" secs",
502 max_retransmit_duration / USEC_PER_SEC);
504 r = sd_event_add_time(client->event,
505 &client->timeout_resend_expire,
506 clock_boottime_or_monotonic(),
507 time_now + max_retransmit_duration,
509 client_timeout_resend_expire, client);
513 r = sd_event_source_set_priority(client->timeout_resend_expire,
514 client->event_priority);
521 client_stop(client, r);
526 static int client_ensure_iaid(sd_dhcp6_client *client) {
527 /* name is a pointer to memory in the udev_device struct, so must
528 have the same scope */
529 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
530 const char *name = NULL;
535 if (client->ia_na.id)
538 if (detect_container(NULL) <= 0) {
539 /* not in a container, udev will be around */
540 _cleanup_udev_unref_ struct udev *udev;
541 char ifindex_str[2 + DECIMAL_STR_MAX(int)];
547 sprintf(ifindex_str, "n%d", client->index);
548 device = udev_device_new_from_device_id(udev, ifindex_str);
552 if (udev_device_get_is_initialized(device) <= 0)
556 name = net_get_name(device);
560 siphash24((uint8_t*)&id, name, strlen(name), HASH_KEY.bytes);
562 /* fall back to mac address if no predictable name available */
563 siphash24((uint8_t*)&id, &client->mac_addr, ETH_ALEN,
566 /* fold into 32 bits */
567 client->ia_na.id = (id & 0xffffffff) ^ (id >> 32);
572 static int client_parse_message(sd_dhcp6_client *client,
573 DHCP6Message *message, size_t len,
574 sd_dhcp6_lease *lease) {
576 uint8_t *optval, *option = (uint8_t *)(message + 1), *id = NULL;
577 uint16_t optcode, status;
578 size_t optlen, id_len;
579 bool clientid = false;
582 while ((r = dhcp6_option_parse(&option, &len, &optcode, &optlen,
585 case DHCP6_OPTION_CLIENTID:
587 log_dhcp6_client(client, "%s contains multiple clientids",
588 dhcp6_message_type_to_string(message->type));
592 if (optlen != sizeof(client->duid) ||
593 memcmp(&client->duid, optval, optlen) != 0) {
594 log_dhcp6_client(client, "%s DUID does not match",
595 dhcp6_message_type_to_string(message->type));
603 case DHCP6_OPTION_SERVERID:
604 r = dhcp6_lease_get_serverid(lease, &id, &id_len);
606 log_dhcp6_client(client, "%s contains multiple serverids",
607 dhcp6_message_type_to_string(message->type));
611 r = dhcp6_lease_set_serverid(lease, optval, optlen);
617 case DHCP6_OPTION_PREFERENCE:
621 r = dhcp6_lease_set_preference(lease, *optval);
627 case DHCP6_OPTION_STATUS_CODE:
631 status = optval[0] << 8 | optval[1];
633 log_dhcp6_client(client, "%s Status %s",
634 dhcp6_message_type_to_string(message->type),
635 dhcp6_message_status_to_string(status));
641 case DHCP6_OPTION_IA_NA:
642 r = dhcp6_option_parse_ia(&optval, &optlen, optcode,
644 if (r < 0 && r != -ENOMSG)
647 r = dhcp6_lease_get_iaid(lease, &iaid_lease);
651 if (client->ia_na.id != iaid_lease) {
652 log_dhcp6_client(client, "%s has wrong IAID",
653 dhcp6_message_type_to_string(message->type));
659 case DHCP6_OPTION_RAPID_COMMIT:
660 r = dhcp6_lease_set_rapid_commit(lease);
668 if ((r < 0 && r != -ENOMSG) || !clientid) {
669 log_dhcp6_client(client, "%s has incomplete options",
670 dhcp6_message_type_to_string(message->type));
674 r = dhcp6_lease_get_serverid(lease, &id, &id_len);
676 log_dhcp6_client(client, "%s has no server id",
677 dhcp6_message_type_to_string(message->type));
682 static int client_receive_reply(sd_dhcp6_client *client, DHCP6Message *reply,
686 _cleanup_dhcp6_lease_free_ sd_dhcp6_lease *lease = NULL;
689 if (reply->type != DHCP6_REPLY)
692 r = dhcp6_lease_new(&lease);
696 r = client_parse_message(client, reply, len, lease);
700 if (client->state == DHCP6_STATE_SOLICITATION) {
701 r = dhcp6_lease_get_rapid_commit(lease, &rapid_commit);
710 dhcp6_lease_clear_timers(&client->lease->ia);
712 client->lease = sd_dhcp6_lease_unref(client->lease);
713 client->lease = lease;
716 return DHCP6_STATE_BOUND;
719 static int client_receive_advertise(sd_dhcp6_client *client,
720 DHCP6Message *advertise, size_t len) {
722 _cleanup_dhcp6_lease_free_ sd_dhcp6_lease *lease = NULL;
723 uint8_t pref_advertise = 0, pref_lease = 0;
725 if (advertise->type != DHCP6_ADVERTISE)
728 r = dhcp6_lease_new(&lease);
732 r = client_parse_message(client, advertise, len, lease);
736 r = dhcp6_lease_get_preference(lease, &pref_advertise);
740 r = dhcp6_lease_get_preference(client->lease, &pref_lease);
741 if (!client->lease || r < 0 || pref_advertise > pref_lease) {
742 sd_dhcp6_lease_unref(client->lease);
743 client->lease = lease;
748 if (pref_advertise == 255 || client->retransmit_count > 1)
749 r = DHCP6_STATE_REQUEST;
754 static int client_receive_message(sd_event_source *s, int fd, uint32_t revents,
756 sd_dhcp6_client *client = userdata;
757 DHCP6_CLIENT_DONT_DESTROY(client);
758 _cleanup_free_ DHCP6Message *message;
763 assert(client->event);
765 r = ioctl(fd, FIONREAD, &buflen);
766 if (r < 0 || buflen <= 0)
767 buflen = DHCP6_MIN_OPTIONS_SIZE;
769 message = malloc0(buflen);
773 len = read(fd, message, buflen);
774 if ((size_t)len < sizeof(DHCP6Message)) {
775 log_dhcp6_client(client, "could not receive message from UDP socket: %m");
779 switch(message->type) {
787 case DHCP6_INFORMATION_REQUEST:
788 case DHCP6_RELAY_FORW:
789 case DHCP6_RELAY_REPL:
792 case DHCP6_ADVERTISE:
794 case DHCP6_RECONFIGURE:
798 log_dhcp6_client(client, "unknown message type %d",
803 if (client->transaction_id != (message->transaction_id &
804 htobe32(0x00ffffff)))
807 switch (client->state) {
808 case DHCP6_STATE_SOLICITATION:
809 r = client_receive_advertise(client, message, len);
811 if (r == DHCP6_STATE_REQUEST) {
812 client_start(client, r);
817 /* fall through for Soliciation Rapid Commit option check */
818 case DHCP6_STATE_REQUEST:
819 case DHCP6_STATE_RENEW:
820 case DHCP6_STATE_REBIND:
822 r = client_receive_reply(client, message, len);
826 if (r == DHCP6_STATE_BOUND) {
828 r = client_start(client, DHCP6_STATE_BOUND);
830 client_stop(client, r);
834 client_notify(client, DHCP6_EVENT_IP_ACQUIRE);
839 case DHCP6_STATE_BOUND:
843 case DHCP6_STATE_STOPPED:
848 log_dhcp6_client(client, "Recv %s",
849 dhcp6_message_type_to_string(message->type));
855 static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
858 usec_t timeout, time_now;
859 char time_string[FORMAT_TIMESPAN_MAX];
861 assert_return(client, -EINVAL);
862 assert_return(client->event, -EINVAL);
863 assert_return(client->index > 0, -EINVAL);
864 assert_return(client->state != state, -EINVAL);
866 client->timeout_resend_expire =
867 sd_event_source_unref(client->timeout_resend_expire);
868 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
869 client->retransmit_time = 0;
870 client->retransmit_count = 0;
873 case DHCP6_STATE_STOPPED:
874 case DHCP6_STATE_SOLICITATION:
876 r = client_ensure_iaid(client);
880 r = dhcp6_network_bind_udp_socket(client->index, NULL);
886 r = sd_event_add_io(client->event, &client->receive_message,
887 client->fd, EPOLLIN, client_receive_message,
892 r = sd_event_source_set_priority(client->receive_message,
893 client->event_priority);
897 client->state = DHCP6_STATE_SOLICITATION;
901 case DHCP6_STATE_REQUEST:
902 case DHCP6_STATE_RENEW:
903 case DHCP6_STATE_REBIND:
905 client->state = state;
909 case DHCP6_STATE_BOUND:
911 r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now);
915 if (client->lease->ia.lifetime_t1 == 0xffffffff ||
916 client->lease->ia.lifetime_t2 == 0xffffffff) {
918 log_dhcp6_client(client, "infinite T1 0x%08x or T2 0x%08x",
919 be32toh(client->lease->ia.lifetime_t1),
920 be32toh(client->lease->ia.lifetime_t2));
925 timeout = client_timeout_compute_random(be32toh(client->lease->ia.lifetime_t1) * USEC_PER_SEC);
927 log_dhcp6_client(client, "T1 expires in %s",
928 format_timespan(time_string,
932 r = sd_event_add_time(client->event,
933 &client->lease->ia.timeout_t1,
934 clock_boottime_or_monotonic(), time_now + timeout,
935 10 * USEC_PER_SEC, client_timeout_t1,
940 r = sd_event_source_set_priority(client->lease->ia.timeout_t1,
941 client->event_priority);
945 timeout = client_timeout_compute_random(be32toh(client->lease->ia.lifetime_t2) * USEC_PER_SEC);
947 log_dhcp6_client(client, "T2 expires in %s",
948 format_timespan(time_string,
952 r = sd_event_add_time(client->event,
953 &client->lease->ia.timeout_t2,
954 clock_boottime_or_monotonic(), time_now + timeout,
955 10 * USEC_PER_SEC, client_timeout_t2,
960 r = sd_event_source_set_priority(client->lease->ia.timeout_t2,
961 client->event_priority);
965 client->state = state;
970 client->transaction_id = random_u32() & htobe32(0x00ffffff);
972 r = sd_event_add_time(client->event, &client->timeout_resend,
973 clock_boottime_or_monotonic(), 0, 0, client_timeout_resend,
978 r = sd_event_source_set_priority(client->timeout_resend,
979 client->event_priority);
986 int sd_dhcp6_client_stop(sd_dhcp6_client *client)
988 client_stop(client, DHCP6_EVENT_STOP);
993 int sd_dhcp6_client_start(sd_dhcp6_client *client)
997 assert_return(client, -EINVAL);
998 assert_return(client->event, -EINVAL);
999 assert_return(client->index > 0, -EINVAL);
1001 r = client_reset(client);
1005 return client_start(client, DHCP6_STATE_SOLICITATION);
1008 int sd_dhcp6_client_attach_event(sd_dhcp6_client *client, sd_event *event,
1013 assert_return(client, -EINVAL);
1014 assert_return(!client->event, -EBUSY);
1017 client->event = sd_event_ref(event);
1019 r = sd_event_default(&client->event);
1024 client->event_priority = priority;
1029 int sd_dhcp6_client_detach_event(sd_dhcp6_client *client) {
1030 assert_return(client, -EINVAL);
1032 client->event = sd_event_unref(client->event);
1037 sd_event *sd_dhcp6_client_get_event(sd_dhcp6_client *client) {
1041 return client->event;
1044 sd_dhcp6_client *sd_dhcp6_client_ref(sd_dhcp6_client *client) {
1046 assert_se(REFCNT_INC(client->n_ref) >= 2);
1051 sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client) {
1052 if (client && REFCNT_DEC(client->n_ref) <= 0) {
1053 client_reset(client);
1055 sd_dhcp6_client_detach_event(client);
1057 free(client->req_opts);
1066 int sd_dhcp6_client_new(sd_dhcp6_client **ret)
1068 _cleanup_dhcp6_client_unref_ sd_dhcp6_client *client = NULL;
1069 sd_id128_t machine_id;
1073 assert_return(ret, -EINVAL);
1075 client = new0(sd_dhcp6_client, 1);
1079 client->n_ref = REFCNT_INIT;
1081 client->ia_na.type = DHCP6_OPTION_IA_NA;
1087 /* initialize DUID */
1088 client->duid.type = htobe16(DHCP6_DUID_EN);
1089 client->duid.pen = htobe32(SYSTEMD_PEN);
1091 r = sd_id128_get_machine(&machine_id);
1095 /* a bit of snake-oil perhaps, but no need to expose the machine-id
1097 siphash24(client->duid.id, &machine_id, sizeof(machine_id),
1100 client->req_opts_len = ELEMENTSOF(default_req_opts);
1102 client->req_opts = new0(be16_t, client->req_opts_len);
1103 if (!client->req_opts)
1106 for (t = 0; t < client->req_opts_len; t++)
1107 client->req_opts[t] = htobe16(default_req_opts[t]);