5 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <connman/ipconfig.h>
32 #include <connman/storage.h>
34 #include <gdhcp/gdhcp.h>
40 /* Transmission params in msec, RFC 3315 chapter 5.5 */
41 #define INF_MAX_DELAY (1 * 1000)
42 #define INF_TIMEOUT (1 * 1000)
43 #define INF_MAX_RT (120 * 1000)
44 #define SOL_MAX_DELAY (1 * 1000)
45 #define SOL_TIMEOUT (1 * 1000)
46 #define SOL_MAX_RT (120 * 1000)
47 #define REQ_TIMEOUT (1 * 1000)
48 #define REQ_MAX_RT (30 * 1000)
50 #define REN_TIMEOUT (10 * 1000)
51 #define REN_MAX_RT (600 * 1000)
52 #define REB_TIMEOUT (10 * 1000)
53 #define REB_MAX_RT (600 * 1000)
54 #define CNF_MAX_DELAY (1 * 1000)
55 #define CNF_TIMEOUT (1 * 1000)
56 #define CNF_MAX_RT (4 * 1000)
57 #define CNF_MAX_RD (10 * 1000)
58 #define DEC_TIMEOUT (1 * 1000)
67 struct connman_dhcpv6 {
68 struct connman_network *network;
74 GDHCPClient *dhcp_client;
76 guint timeout; /* operation timeout in msec */
77 guint MRD; /* max operation timeout in msec */
78 guint RT; /* in msec */
79 bool use_ta; /* set to TRUE if IPv6 privacy is enabled */
80 GSList *prefixes; /* network prefixes from radvd or dhcpv6 pd */
81 int request_count; /* how many times REQUEST have been sent */
82 bool stateless; /* TRUE if stateless DHCPv6 is used */
83 bool started; /* TRUE if we have DHCPv6 started */
86 static GHashTable *network_table;
87 static GHashTable *network_pd_table;
89 static int dhcpv6_request(struct connman_dhcpv6 *dhcp, bool add_addresses);
90 static int dhcpv6_pd_request(struct connman_dhcpv6 *dhcp);
91 static gboolean start_solicitation(gpointer user_data);
92 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp);
93 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp);
95 static void clear_timer(struct connman_dhcpv6 *dhcp)
97 if (dhcp->timeout > 0) {
98 g_source_remove(dhcp->timeout);
103 g_source_remove(dhcp->MRD);
108 static inline guint get_random(void)
112 __connman_util_get_random(&val);
114 /* Make sure the value is always positive so strip MSB */
115 return ((uint32_t)val) >> 1;
118 static guint compute_random(guint val)
120 return val - val / 10 +
121 (get_random() % (2 * 1000)) * val / 10 / 1000;
124 /* Calculate a random delay, RFC 3315 chapter 14 */
125 /* RT and MRT are milliseconds */
126 static guint calc_delay(guint RT, guint MRT)
128 if (MRT && (RT > MRT / 2))
129 RT = compute_random(MRT);
131 RT += compute_random(RT);
136 static guint initial_rt(guint timeout)
138 return compute_random(timeout);
141 static void free_prefix(gpointer data)
146 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
148 g_strfreev(dhcp->nameservers);
149 g_strfreev(dhcp->timeservers);
151 dhcp->nameservers = NULL;
152 dhcp->timeservers = NULL;
153 dhcp->started = false;
155 g_slist_free_full(dhcp->prefixes, free_prefix);
158 static bool compare_string_arrays(char **array_a, char **array_b)
162 if (!array_a || !array_b)
165 if (g_strv_length(array_a) != g_strv_length(array_b))
168 for (i = 0; array_a[i] && array_b[i]; i++)
169 if (g_strcmp0(array_a[i], array_b[i]) != 0)
175 static void dhcpv6_debug(const char *str, void *data)
177 connman_info("%s: %s\n", (const char *) data, str);
180 static gchar *convert_to_hex(unsigned char *buf, int len)
182 gchar *ret = g_try_malloc(len * 2 + 1);
185 for (i = 0; ret && i < len; i++)
186 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
192 * DUID should not change over time so save it to file.
193 * See RFC 3315 chapter 9 for details.
195 static int set_duid(struct connman_service *service,
196 struct connman_network *network,
197 GDHCPClient *dhcp_client, int index)
205 ident = __connman_service_get_ident(service);
207 keyfile = connman_storage_load_service(ident);
211 hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
214 unsigned int i, j = 0, hex;
215 size_t hex_duid_len = strlen(hex_duid);
217 duid = g_try_malloc0(hex_duid_len / 2);
219 g_key_file_free(keyfile);
224 for (i = 0; i < hex_duid_len; i += 2) {
225 sscanf(hex_duid + i, "%02x", &hex);
229 duid_len = hex_duid_len / 2;
232 int type = __connman_ipconfig_get_type_from_index(index);
234 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
237 g_key_file_free(keyfile);
241 hex_duid = convert_to_hex(duid, duid_len);
243 g_key_file_free(keyfile);
247 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
250 __connman_storage_save_service(keyfile, ident);
254 g_key_file_free(keyfile);
256 g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
261 static void clear_callbacks(GDHCPClient *dhcp_client)
263 g_dhcp_client_register_event(dhcp_client,
264 G_DHCP_CLIENT_EVENT_SOLICITATION,
267 g_dhcp_client_register_event(dhcp_client,
268 G_DHCP_CLIENT_EVENT_ADVERTISE,
271 g_dhcp_client_register_event(dhcp_client,
272 G_DHCP_CLIENT_EVENT_REQUEST,
275 g_dhcp_client_register_event(dhcp_client,
276 G_DHCP_CLIENT_EVENT_CONFIRM,
279 g_dhcp_client_register_event(dhcp_client,
280 G_DHCP_CLIENT_EVENT_RENEW,
283 g_dhcp_client_register_event(dhcp_client,
284 G_DHCP_CLIENT_EVENT_REBIND,
287 g_dhcp_client_register_event(dhcp_client,
288 G_DHCP_CLIENT_EVENT_RELEASE,
291 g_dhcp_client_register_event(dhcp_client,
292 G_DHCP_CLIENT_EVENT_DECLINE,
295 g_dhcp_client_register_event(dhcp_client,
296 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
300 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
302 struct connman_dhcpv6 *dhcp = user_data;
303 struct connman_service *service;
305 GList *option, *list;
306 char **nameservers, **timeservers;
308 DBG("dhcpv6 information-request %p", dhcp);
310 service = connman_service_lookup_from_network(dhcp->network);
312 connman_error("Can not lookup service");
316 g_dhcpv6_client_clear_retransmit(dhcp_client);
318 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
319 entries = g_list_length(option);
321 nameservers = g_try_new0(char *, entries + 1);
323 for (i = 0, list = option; list; list = list->next, i++)
324 nameservers[i] = g_strdup(list->data);
327 if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
328 if (dhcp->nameservers) {
329 for (i = 0; dhcp->nameservers[i]; i++)
330 __connman_service_nameserver_remove(service,
331 dhcp->nameservers[i],
333 g_strfreev(dhcp->nameservers);
336 dhcp->nameservers = nameservers;
338 for (i = 0; dhcp->nameservers &&
339 dhcp->nameservers[i]; i++)
340 __connman_service_nameserver_append(service,
341 dhcp->nameservers[i],
344 g_strfreev(nameservers);
347 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
348 entries = g_list_length(option);
350 timeservers = g_try_new0(char *, entries + 1);
352 for (i = 0, list = option; list; list = list->next, i++)
353 timeservers[i] = g_strdup(list->data);
356 if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
357 if (dhcp->timeservers) {
358 for (i = 0; dhcp->timeservers[i]; i++)
359 __connman_service_timeserver_remove(service,
360 dhcp->timeservers[i]);
361 g_strfreev(dhcp->timeservers);
364 dhcp->timeservers = timeservers;
366 for (i = 0; dhcp->timeservers &&
367 dhcp->timeservers[i]; i++)
368 __connman_service_timeserver_append(service,
369 dhcp->timeservers[i]);
371 g_strfreev(timeservers);
374 if (dhcp->callback) {
375 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
376 dhcp->callback(dhcp->network, status == 0 ?
377 CONNMAN_DHCPV6_STATUS_SUCCEED :
378 CONNMAN_DHCPV6_STATUS_FAIL,
383 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
385 struct connman_service *service;
386 GDHCPClient *dhcp_client;
387 GDHCPClientError error;
390 DBG("dhcp %p", dhcp);
392 index = connman_network_get_index(dhcp->network);
394 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
395 if (error != G_DHCP_CLIENT_ERROR_NONE) {
400 if (getenv("CONNMAN_DHCPV6_DEBUG"))
401 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
403 service = connman_service_lookup_from_network(dhcp->network);
406 g_dhcp_client_unref(dhcp_client);
410 ret = set_duid(service, dhcp->network, dhcp_client, index);
413 g_dhcp_client_unref(dhcp_client);
417 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
418 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
419 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
420 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
422 g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
423 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
425 g_dhcp_client_register_event(dhcp_client,
426 G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
428 dhcp->dhcp_client = dhcp_client;
430 return g_dhcp_client_start(dhcp_client, NULL);
433 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
435 struct in6_addr addr_prefix, addr;
439 for (list = prefixes; list; list = list->next) {
440 char *prefix = list->data;
441 const char *slash = g_strrstr(prefix, "/");
442 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
443 0xF0, 0xE0, 0xC0, 0x80 };
444 int left, count, i, plen;
449 prefix = g_strndup(prefix, slash - prefix);
450 len = strtol(slash + 1, NULL, 10);
451 if (len < 3 || len > 128)
460 inet_pton(AF_INET6, prefix, &addr_prefix);
461 inet_pton(AF_INET6, address, &addr);
463 memset(&addr_prefix.s6_addr[i], 0, count);
464 memset(&addr.s6_addr[i], 0, count);
467 addr_prefix.s6_addr[i - 1] &= bits[left];
468 addr.s6_addr[i - 1] &= bits[left];
473 if (memcmp(&addr_prefix, &addr, 16) == 0) {
482 static int set_other_addresses(GDHCPClient *dhcp_client,
483 struct connman_dhcpv6 *dhcp)
485 struct connman_service *service;
487 GList *option, *list;
488 char **nameservers, **timeservers;
490 service = connman_service_lookup_from_network(dhcp->network);
492 connman_error("Can not lookup service");
497 * Check domains before nameservers so that the nameserver append
498 * function will update domain list in service.c
500 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DOMAIN_LIST);
501 entries = g_list_length(option);
503 char **domains = g_try_new0(char *, entries + 1);
505 for (i = 0, list = option; list;
506 list = list->next, i++)
507 domains[i] = g_strdup(list->data);
508 __connman_service_update_search_domains(service, domains);
513 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
514 entries = g_list_length(option);
516 nameservers = g_try_new0(char *, entries + 1);
518 for (i = 0, list = option; list; list = list->next, i++)
519 nameservers[i] = g_strdup(list->data);
522 if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
523 if (dhcp->nameservers) {
524 for (i = 0; dhcp->nameservers[i]; i++)
525 __connman_service_nameserver_remove(service,
526 dhcp->nameservers[i],
528 g_strfreev(dhcp->nameservers);
531 dhcp->nameservers = nameservers;
533 for (i = 0; dhcp->nameservers &&
534 dhcp->nameservers[i]; i++)
535 __connman_service_nameserver_append(service,
536 dhcp->nameservers[i],
539 g_strfreev(nameservers);
542 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
543 entries = g_list_length(option);
545 timeservers = g_try_new0(char *, entries + 1);
547 for (i = 0, list = option; list; list = list->next, i++)
548 timeservers[i] = g_strdup(list->data);
551 if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
552 if (dhcp->timeservers) {
553 for (i = 0; dhcp->timeservers[i]; i++)
554 __connman_service_timeserver_remove(service,
555 dhcp->timeservers[i]);
556 g_strfreev(dhcp->timeservers);
559 dhcp->timeservers = timeservers;
561 for (i = 0; dhcp->timeservers &&
562 dhcp->timeservers[i]; i++)
563 __connman_service_timeserver_append(service,
564 dhcp->timeservers[i]);
566 g_strfreev(timeservers);
571 static GSList *copy_prefixes(GSList *prefixes)
573 GSList *list, *copy = NULL;
575 for (list = prefixes; list; list = list->next)
576 copy = g_slist_prepend(copy, g_strdup(list->data));
582 * Helper struct for doing DAD (duplicate address detection).
583 * It is refcounted and freed after all reply's to neighbor
584 * discovery request are received.
590 GDHCPClient *dhcp_client;
591 struct connman_ipconfig *ipconfig;
599 static void free_own_address(struct own_address *data)
601 g_dhcp_client_unref(data->dhcp_client);
602 __connman_ipconfig_unref(data->ipconfig);
603 g_slist_free_full(data->prefixes, free_prefix);
604 g_slist_free_full(data->dad_failed, g_free);
605 g_slist_free_full(data->dad_succeed, g_free);
610 static struct own_address *ref_own_address(struct own_address *address)
612 DBG("%p ref %d", address, address->refcount + 1);
614 __sync_fetch_and_add(&address->refcount, 1);
619 static void unref_own_address(struct own_address *address)
624 DBG("%p ref %d", address, address->refcount - 1);
626 if (__sync_fetch_and_sub(&address->refcount, 1) != 1)
629 free_own_address(address);
632 static void set_address(int ifindex, struct connman_ipconfig *ipconfig,
633 GSList *prefixes, char *address)
635 const char *c_address;
637 c_address = __connman_ipconfig_get_local(ipconfig);
639 if (address && ((c_address && g_strcmp0(address, c_address) != 0) ||
643 /* Is this prefix part of the subnet we are suppose to use? */
644 prefix_len = check_ipv6_addr_prefix(prefixes, address);
646 __connman_ipconfig_address_remove(ipconfig);
647 __connman_ipconfig_set_local(ipconfig, address);
648 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
650 DBG("new address %s/%d", address, prefix_len);
652 __connman_ipconfig_set_dhcp_address(ipconfig, address);
653 __connman_service_save(
654 __connman_service_lookup_from_index(ifindex));
660 * Helper struct that is used when waiting a reply to DECLINE message.
662 struct decline_cb_data {
663 GDHCPClient *dhcp_client;
669 static void decline_reply_callback(struct decline_cb_data *data)
671 struct connman_network *network;
672 struct connman_service *service;
674 service = __connman_service_lookup_from_index(data->ifindex);
675 network = __connman_service_get_network(service);
678 data->callback(network, CONNMAN_DHCPV6_STATUS_RESTART, NULL);
680 g_dhcp_client_unref(data->dhcp_client);
683 static gboolean decline_timeout(gpointer user_data)
685 struct decline_cb_data *data = user_data;
687 DBG("ifindex %d", data->ifindex);
690 * We ignore all DECLINE replies that are received after the timeout
692 g_dhcp_client_register_event(data->dhcp_client,
693 G_DHCP_CLIENT_EVENT_DECLINE,
696 decline_reply_callback(data);
703 static void decline_cb(GDHCPClient *dhcp_client, gpointer user_data)
705 struct decline_cb_data *data = user_data;
707 DBG("ifindex %d", data->ifindex);
709 g_dhcpv6_client_clear_retransmit(dhcp_client);
712 g_source_remove(data->timeout);
714 decline_reply_callback(data);
719 static int dhcpv6_decline(GDHCPClient *dhcp_client, int ifindex,
720 dhcpv6_cb callback, GSList *failed)
722 struct decline_cb_data *data;
726 DBG("dhcp_client %p", dhcp_client);
728 g_dhcp_client_clear_requests(dhcp_client);
730 g_dhcpv6_client_clear_send(dhcp_client, G_DHCPV6_ORO);
732 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
733 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
735 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
737 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
739 code = G_DHCPV6_IA_TA;
743 code = G_DHCPV6_IA_NA;
745 g_dhcpv6_client_clear_send(dhcp_client, code);
747 g_dhcpv6_client_set_ias(dhcp_client, ifindex, code, NULL, NULL,
750 clear_callbacks(dhcp_client);
752 data = g_try_new(struct decline_cb_data, 1);
756 data->ifindex = ifindex;
757 data->callback = callback;
758 data->dhcp_client = g_dhcp_client_ref(dhcp_client);
759 data->timeout = g_timeout_add(DEC_TIMEOUT, decline_timeout, data);
761 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_DECLINE,
764 return g_dhcp_client_start(dhcp_client, NULL);
767 static void dad_reply(struct nd_neighbor_advert *reply,
768 unsigned int length, struct in6_addr *addr, void *user_data)
770 struct own_address *data = user_data;
772 char address[INET6_ADDRSTRLEN];
773 enum __connman_dhcpv6_status status = CONNMAN_DHCPV6_STATUS_FAIL;
775 inet_ntop(AF_INET6, addr, address, INET6_ADDRSTRLEN);
777 DBG("user %p reply %p len %d address %s index %d data %p", user_data,
778 reply, length, address, data->ifindex, data);
782 DBG("DAD succeed for %s", address);
784 DBG("DAD cannot be done for %s", address);
786 data->dad_succeed = g_slist_prepend(data->dad_succeed,
790 DBG("DAD failed for %s", address);
792 data->dad_failed = g_slist_prepend(data->dad_failed,
797 * If refcount == 1 then we got the final reply and can continue.
799 if (data->refcount > 1)
802 for (list = data->dad_succeed; list; list = list->next)
803 set_address(data->ifindex, data->ipconfig, data->prefixes,
806 if (data->dad_failed) {
807 dhcpv6_decline(data->dhcp_client, data->ifindex,
808 data->callback, data->dad_failed);
810 if (data->dad_succeed)
811 status = CONNMAN_DHCPV6_STATUS_SUCCEED;
813 if (data->callback) {
814 struct connman_network *network;
815 struct connman_service *service;
817 service = __connman_service_lookup_from_index(
819 network = __connman_service_get_network(service);
821 data->callback(network, status, NULL);
825 unref_own_address(data);
829 * Is the kernel configured to do DAD? If 0, then do not do DAD.
830 * See also RFC 4862 chapter 5.4 about DupAddrDetectTransmits
832 static int dad_transmits(int ifindex)
834 char name[IF_NAMESIZE];
839 if (!if_indextoname(ifindex, name))
842 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/dad_transmits",
848 f = fopen(path, "r");
853 if (fscanf(f, "%d", &value) < 0)
862 static void do_dad(GDHCPClient *dhcp_client, struct connman_dhcpv6 *dhcp)
864 struct connman_service *service;
865 struct connman_ipconfig *ipconfig;
867 GList *option, *list;
868 struct own_address *user_data;
870 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
872 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
875 * Even if we didn't had any addresses, just try to set
878 set_other_addresses(dhcp_client, dhcp);
881 DBG("Skip DAD as no addresses found in reply");
883 dhcp->callback(dhcp->network,
884 CONNMAN_DHCPV6_STATUS_SUCCEED, NULL);
889 ifindex = connman_network_get_index(dhcp->network);
891 DBG("index %d", ifindex);
893 service = connman_service_lookup_from_network(dhcp->network);
895 connman_error("Can not lookup service for index %d", ifindex);
899 ipconfig = __connman_service_get_ip6config(service);
901 connman_error("Could not lookup ip6config for index %d",
906 if (!dad_transmits(ifindex)) {
907 DBG("Skip DAD because of kernel configuration");
909 for (list = option; list; list = list->next)
910 set_address(ifindex, ipconfig, dhcp->prefixes,
914 dhcp->callback(dhcp->network,
915 CONNMAN_DHCPV6_STATUS_SUCCEED, NULL);
920 if (g_list_length(option) == 0) {
921 DBG("No addresses when doing DAD");
923 dhcp->callback(dhcp->network,
924 CONNMAN_DHCPV6_STATUS_SUCCEED, NULL);
929 user_data = g_try_new0(struct own_address, 1);
933 user_data->refcount = 0;
934 user_data->ifindex = ifindex;
935 user_data->dhcp_client = g_dhcp_client_ref(dhcp_client);
936 user_data->ipconfig = __connman_ipconfig_ref(ipconfig);
937 user_data->prefixes = copy_prefixes(dhcp->prefixes);
938 user_data->callback = dhcp->callback;
941 * We send one neighbor discovery request / address
942 * and after all checks are done, then report the status
946 for (list = option; list; list = list->next) {
947 char *address = option->data;
948 struct in6_addr addr;
951 ref_own_address(user_data);
953 if (inet_pton(AF_INET6, address, &addr) < 0) {
954 DBG("Invalid IPv6 address %s %d/%s", address,
955 -errno, strerror(errno));
959 DBG("user %p address %s client %p ipconfig %p prefixes %p",
961 user_data->dhcp_client, user_data->ipconfig,
962 user_data->prefixes);
964 ret = __connman_inet_ipv6_do_dad(ifindex, 1000,
969 DBG("Could not send neighbor solicitation for %s",
971 dad_reply(NULL, -1, &addr, user_data);
973 DBG("Sent neighbor solicitation %d bytes", ret);
980 unref_own_address(user_data);
984 dhcp->callback(dhcp->network, CONNMAN_DHCPV6_STATUS_FAIL,
988 static gboolean timeout_request_resend(gpointer user_data)
990 struct connman_dhcpv6 *dhcp = user_data;
992 if (dhcp->request_count >= REQ_MAX_RC) {
993 DBG("max request retry attempts %d", dhcp->request_count);
994 dhcp->request_count = 0;
996 dhcp->callback(dhcp->network,
997 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1001 dhcp->request_count++;
1003 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
1004 DBG("request resend RT timeout %d msec", dhcp->RT);
1005 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request_resend, dhcp);
1007 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1009 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1014 static gboolean request_resend(gpointer user_data)
1016 struct connman_dhcpv6 *dhcp = user_data;
1018 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1020 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
1021 DBG("request resend RT timeout %d msec", dhcp->RT);
1022 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request_resend, dhcp);
1024 dhcpv6_request(dhcp, true);
1029 static void do_resend_request(struct connman_dhcpv6 *dhcp)
1031 if (dhcp->request_count >= REQ_MAX_RC) {
1032 DBG("max request retry attempts %d", dhcp->request_count);
1033 dhcp->request_count = 0;
1035 dhcp->callback(dhcp->network,
1036 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1040 dhcp->request_count++;
1042 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
1043 DBG("resending request after %d msec", dhcp->RT);
1044 dhcp->timeout = g_timeout_add(dhcp->RT, request_resend, dhcp);
1047 static void re_cb(enum request_type req_type, GDHCPClient *dhcp_client,
1050 struct connman_dhcpv6 *dhcp = user_data;
1055 status = g_dhcpv6_client_get_status(dhcp_client);
1057 DBG("dhcpv6 cb msg %p status %d", dhcp, status);
1060 * RFC 3315, 18.1.8 handle the resend if error
1062 if (status == G_DHCPV6_ERROR_BINDING) {
1063 dhcpv6_request(dhcp, false);
1064 } else if (status == G_DHCPV6_ERROR_MCAST) {
1067 dhcpv6_request(dhcp, true);
1070 dhcpv6_rebind(dhcp);
1076 } else if (status == G_DHCPV6_ERROR_LINK) {
1077 if (req_type == REQ_REQUEST) {
1078 g_dhcp_client_unref(dhcp->dhcp_client);
1079 start_solicitation(dhcp);
1082 dhcp->callback(dhcp->network,
1083 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1085 } else if (status == G_DHCPV6_ERROR_FAILURE) {
1086 if (req_type == REQ_REQUEST) {
1087 /* Rate limit the resend of request message */
1088 do_resend_request(dhcp);
1091 dhcp->callback(dhcp->network,
1092 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1097 * If we did not got any addresses, then re-send
1102 option = g_dhcp_client_get_option(dhcp->dhcp_client,
1105 option = g_dhcp_client_get_option(dhcp->dhcp_client,
1110 do_resend_request(dhcp);
1113 dhcpv6_rebind(dhcp);
1123 if (status == G_DHCPV6_ERROR_SUCCESS)
1124 do_dad(dhcp_client, dhcp);
1125 else if (dhcp->callback)
1126 dhcp->callback(dhcp->network,
1127 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1131 static void rebind_cb(GDHCPClient *dhcp_client, gpointer user_data)
1135 g_dhcpv6_client_reset_request(dhcp_client);
1136 g_dhcpv6_client_clear_retransmit(dhcp_client);
1138 re_cb(REQ_REBIND, dhcp_client, user_data);
1141 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp)
1143 GDHCPClient *dhcp_client;
1145 DBG("dhcp %p", dhcp);
1147 dhcp_client = dhcp->dhcp_client;
1149 g_dhcp_client_clear_requests(dhcp_client);
1151 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1152 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1153 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
1154 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1156 g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
1157 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
1159 g_dhcpv6_client_set_ia(dhcp_client,
1160 connman_network_get_index(dhcp->network),
1161 dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1162 NULL, NULL, TRUE, NULL);
1164 clear_callbacks(dhcp_client);
1166 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
1169 dhcp->dhcp_client = dhcp_client;
1171 return g_dhcp_client_start(dhcp_client, NULL);
1174 static gboolean dhcpv6_restart(gpointer user_data)
1176 struct connman_dhcpv6 *dhcp = user_data;
1179 dhcp->callback(dhcp->network, CONNMAN_DHCPV6_STATUS_FAIL,
1186 * Check if we need to restart the solicitation procedure. This
1187 * is done if all the addresses have expired. RFC 3315, 18.1.4
1189 static int check_restart(struct connman_dhcpv6 *dhcp)
1191 time_t current, expired;
1193 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
1195 current = time(NULL);
1197 if (current >= expired) {
1198 DBG("expired by %d secs", (int)(current - expired));
1200 g_timeout_add(0, dhcpv6_restart, dhcp);
1208 static gboolean timeout_rebind(gpointer user_data)
1210 struct connman_dhcpv6 *dhcp = user_data;
1212 if (check_restart(dhcp) < 0)
1215 dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
1217 DBG("rebind RT timeout %d msec", dhcp->RT);
1219 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
1221 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1223 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1228 static gboolean start_rebind(gpointer user_data)
1230 struct connman_dhcpv6 *dhcp = user_data;
1232 if (check_restart(dhcp) < 0)
1235 dhcp->RT = initial_rt(REB_TIMEOUT);
1237 DBG("rebind initial RT timeout %d msec", dhcp->RT);
1239 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
1241 dhcpv6_rebind(dhcp);
1246 static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
1250 g_dhcpv6_client_reset_request(dhcp_client);
1251 g_dhcpv6_client_clear_retransmit(dhcp_client);
1253 re_cb(REQ_REQUEST, dhcp_client, user_data);
1256 static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
1259 GDHCPClient *dhcp_client;
1262 DBG("dhcp %p add %d", dhcp, add_addresses);
1264 dhcp_client = dhcp->dhcp_client;
1266 g_dhcp_client_clear_requests(dhcp_client);
1268 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1269 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
1270 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1271 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
1272 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1274 g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
1275 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
1277 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
1278 g_dhcpv6_client_set_ia(dhcp_client,
1279 connman_network_get_index(dhcp->network),
1280 dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1281 &T1, &T2, add_addresses, NULL);
1283 clear_callbacks(dhcp_client);
1285 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
1288 dhcp->dhcp_client = dhcp_client;
1290 return g_dhcp_client_start(dhcp_client, NULL);
1293 static gboolean timeout_request(gpointer user_data)
1295 struct connman_dhcpv6 *dhcp = user_data;
1297 if (dhcp->request_count >= REQ_MAX_RC) {
1298 DBG("max request retry attempts %d", dhcp->request_count);
1299 dhcp->request_count = 0;
1301 dhcp->callback(dhcp->network,
1302 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1306 dhcp->request_count++;
1308 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
1309 DBG("request RT timeout %d msec", dhcp->RT);
1310 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
1312 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1314 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1319 static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
1323 g_dhcpv6_client_reset_request(dhcp_client);
1324 g_dhcpv6_client_clear_retransmit(dhcp_client);
1326 re_cb(REQ_RENEW, dhcp_client, user_data);
1329 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
1331 GDHCPClient *dhcp_client;
1334 DBG("dhcp %p", dhcp);
1336 dhcp_client = dhcp->dhcp_client;
1338 g_dhcp_client_clear_requests(dhcp_client);
1340 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1341 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
1342 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1343 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
1344 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1346 g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
1347 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
1349 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
1350 g_dhcpv6_client_set_ia(dhcp_client,
1351 connman_network_get_index(dhcp->network),
1352 dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1353 &T1, &T2, TRUE, NULL);
1355 clear_callbacks(dhcp_client);
1357 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
1360 dhcp->dhcp_client = dhcp_client;
1362 return g_dhcp_client_start(dhcp_client, NULL);
1365 static gboolean timeout_renew(gpointer user_data)
1367 struct connman_dhcpv6 *dhcp = user_data;
1368 time_t last_rebind, current;
1371 if (check_restart(dhcp) < 0)
1374 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, &T2,
1375 &last_rebind, NULL);
1376 current = time(NULL);
1377 if ((unsigned)current >= (unsigned)last_rebind + T2) {
1379 * Do rebind instead if past T2
1385 dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
1387 DBG("renew RT timeout %d msec", dhcp->RT);
1389 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
1391 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1393 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1398 static gboolean start_renew(gpointer user_data)
1400 struct connman_dhcpv6 *dhcp = user_data;
1402 dhcp->RT = initial_rt(REN_TIMEOUT);
1404 DBG("renew initial RT timeout %d msec", dhcp->RT);
1406 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
1413 int __connman_dhcpv6_start_renew(struct connman_network *network,
1416 struct connman_dhcpv6 *dhcp;
1417 uint32_t T1, T2, delta;
1418 time_t started, current, expired;
1420 dhcp = g_hash_table_lookup(network_table, network);
1424 DBG("network %p dhcp %p", network, dhcp);
1428 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
1429 &started, &expired);
1431 current = time(NULL);
1433 DBG("T1 %u T2 %u expires %lu current %lu started %lu", T1, T2,
1434 (unsigned long)expired, current, started);
1436 if (T1 == 0xffffffff)
1437 /* RFC 3315, 22.4 */
1442 * Client can choose the timeout.
1444 T1 = (expired - started) / 2;
1445 T2 = (expired - started) / 10 * 8;
1448 dhcp->callback = callback;
1450 /* RFC 3315, 18.1.4, start solicit if expired */
1451 if (check_restart(dhcp) < 0)
1454 if (T2 != 0xffffffff && T2 > 0) {
1455 if ((unsigned)current >= (unsigned)started + T2) {
1456 /* RFC 3315, chapter 18.1.3, start rebind */
1457 DBG("start rebind immediately");
1459 dhcp->timeout = g_timeout_add_seconds(0, start_rebind,
1462 } else if ((unsigned)current < (unsigned)started + T1) {
1463 delta = started + T1 - current;
1464 DBG("renew after %d secs", delta);
1466 dhcp->timeout = g_timeout_add_seconds(delta,
1469 delta = started + T2 - current;
1470 DBG("rebind after %d secs", delta);
1472 dhcp->timeout = g_timeout_add_seconds(delta,
1473 start_rebind, dhcp);
1480 static void release_cb(GDHCPClient *dhcp_client, gpointer user_data)
1485 int __connman_dhcpv6_start_release(struct connman_network *network,
1488 struct connman_dhcpv6 *dhcp;
1489 GDHCPClient *dhcp_client;
1492 return 0; /* we are already released */
1494 dhcp = g_hash_table_lookup(network_table, network);
1498 DBG("network %p dhcp %p client %p stateless %d", network, dhcp,
1499 dhcp->dhcp_client, dhcp->stateless);
1501 if (dhcp->stateless)
1506 dhcp_client = dhcp->dhcp_client;
1509 * We had started the DHCPv6 handshaking i.e., we have called
1510 * __connman_dhcpv6_start() but it has not yet sent
1511 * a solicitation message to server. This means that we do not
1512 * have DHCPv6 configured yet so we can just quit here.
1514 DBG("DHCPv6 was not started");
1518 g_dhcp_client_clear_requests(dhcp_client);
1519 g_dhcp_client_clear_values(dhcp_client);
1521 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1522 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
1524 g_dhcpv6_client_set_ia(dhcp_client,
1525 connman_network_get_index(dhcp->network),
1526 dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1527 NULL, NULL, TRUE, NULL);
1529 clear_callbacks(dhcp_client);
1532 * Although we register a callback here we are really not interested in
1533 * the answer because it might take too long time and network code
1534 * might be in the middle of the disconnect.
1535 * So we just inform the server that we are done with the addresses
1536 * but ignore the reply from server. This is allowed by RFC 3315
1539 g_dhcp_client_register_event(dhcp_client,
1540 G_DHCP_CLIENT_EVENT_RELEASE,
1543 dhcp->dhcp_client = dhcp_client;
1545 return g_dhcp_client_start(dhcp_client, NULL);
1548 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
1550 DBG("dhcp %p", dhcp);
1556 if (!dhcp->dhcp_client)
1559 g_dhcp_client_stop(dhcp->dhcp_client);
1560 g_dhcp_client_unref(dhcp->dhcp_client);
1562 dhcp->dhcp_client = NULL;
1567 static void remove_network(gpointer user_data)
1569 struct connman_dhcpv6 *dhcp = user_data;
1571 DBG("dhcp %p", dhcp);
1573 dhcpv6_release(dhcp);
1578 static gboolean timeout_info_req(gpointer user_data)
1580 struct connman_dhcpv6 *dhcp = user_data;
1582 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
1584 DBG("info RT timeout %d msec", dhcp->RT);
1586 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
1588 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1590 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1595 static gboolean start_info_req(gpointer user_data)
1597 struct connman_dhcpv6 *dhcp = user_data;
1599 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1600 dhcp->RT = initial_rt(INF_TIMEOUT);
1602 DBG("info initial RT timeout %d msec", dhcp->RT);
1604 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
1606 dhcpv6_info_request(dhcp);
1611 int __connman_dhcpv6_start_info(struct connman_network *network,
1614 struct connman_dhcpv6 *dhcp;
1620 if (network_table) {
1621 dhcp = g_hash_table_lookup(network_table, network);
1622 if (dhcp && dhcp->started)
1626 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1630 dhcp->network = network;
1631 dhcp->callback = callback;
1632 dhcp->stateless = true;
1633 dhcp->started = true;
1635 connman_network_ref(network);
1637 DBG("replace network %p dhcp %p", network, dhcp);
1639 g_hash_table_replace(network_table, network, dhcp);
1641 /* Initial timeout, RFC 3315, 18.1.5 */
1642 __connman_util_get_random(&rand);
1643 delay = rand % 1000;
1645 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
1650 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
1652 struct connman_dhcpv6 *dhcp = user_data;
1654 DBG("dhcpv6 advertise msg %p", dhcp);
1658 g_dhcpv6_client_clear_retransmit(dhcp_client);
1660 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
1662 dhcp->callback(dhcp->network,
1663 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1667 dhcp->RT = initial_rt(REQ_TIMEOUT);
1668 DBG("request initial RT timeout %d msec", dhcp->RT);
1669 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
1671 dhcp->request_count = 1;
1673 dhcpv6_request(dhcp, true);
1676 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
1678 /* We get here if server supports rapid commit */
1679 struct connman_dhcpv6 *dhcp = user_data;
1681 DBG("dhcpv6 solicitation msg %p", dhcp);
1685 g_dhcpv6_client_clear_retransmit(dhcp_client);
1687 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
1689 dhcp->callback(dhcp->network,
1690 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1694 do_dad(dhcp_client, dhcp);
1697 static gboolean timeout_solicitation(gpointer user_data)
1699 struct connman_dhcpv6 *dhcp = user_data;
1701 dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
1703 DBG("solicit RT timeout %d msec", dhcp->RT);
1705 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1707 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1709 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1714 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
1716 struct connman_service *service;
1717 struct connman_ipconfig *ipconfig_ipv6;
1718 GDHCPClient *dhcp_client;
1719 GDHCPClientError error;
1722 DBG("dhcp %p", dhcp);
1724 index = connman_network_get_index(dhcp->network);
1726 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
1727 if (error != G_DHCP_CLIENT_ERROR_NONE) {
1732 if (getenv("CONNMAN_DHCPV6_DEBUG"))
1733 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
1735 service = connman_service_lookup_from_network(dhcp->network);
1738 g_dhcp_client_unref(dhcp_client);
1742 ret = set_duid(service, dhcp->network, dhcp_client, index);
1745 g_dhcp_client_unref(dhcp_client);
1749 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1750 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
1751 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1752 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
1753 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1755 g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
1756 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
1758 ipconfig_ipv6 = __connman_service_get_ip6config(service);
1759 dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
1761 g_dhcpv6_client_set_ia(dhcp_client, index,
1762 dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1763 NULL, NULL, FALSE, NULL);
1765 clear_callbacks(dhcp_client);
1767 g_dhcp_client_register_event(dhcp_client,
1768 G_DHCP_CLIENT_EVENT_SOLICITATION,
1769 solicitation_cb, dhcp);
1771 g_dhcp_client_register_event(dhcp_client,
1772 G_DHCP_CLIENT_EVENT_ADVERTISE,
1773 advertise_cb, dhcp);
1775 dhcp->dhcp_client = dhcp_client;
1777 return g_dhcp_client_start(dhcp_client, NULL);
1780 static gboolean start_solicitation(gpointer user_data)
1782 struct connman_dhcpv6 *dhcp = user_data;
1784 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1785 dhcp->RT = initial_rt(SOL_TIMEOUT);
1787 DBG("solicit initial RT timeout %d msec", dhcp->RT);
1789 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1791 dhcpv6_solicitation(dhcp);
1796 int __connman_dhcpv6_start(struct connman_network *network,
1797 GSList *prefixes, dhcpv6_cb callback)
1799 struct connman_service *service;
1800 struct connman_dhcpv6 *dhcp;
1806 if (network_table) {
1807 dhcp = g_hash_table_lookup(network_table, network);
1808 if (dhcp && dhcp->started)
1812 service = connman_service_lookup_from_network(network);
1816 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1820 dhcp->network = network;
1821 dhcp->callback = callback;
1822 dhcp->prefixes = prefixes;
1823 dhcp->started = true;
1825 connman_network_ref(network);
1827 DBG("replace network %p dhcp %p", network, dhcp);
1829 g_hash_table_replace(network_table, network, dhcp);
1831 /* Initial timeout, RFC 3315, 17.1.2 */
1832 __connman_util_get_random(&rand);
1833 delay = rand % 1000;
1836 * Start from scratch.
1837 * RFC 3315, chapter 17.1.2 Solicitation message
1839 * Note that we do not send CONFIRM message here as it does
1840 * not make much sense because we do not save expiration time
1841 * so we cannot really know how long the saved address is valid
1842 * anyway. The reply to CONFIRM message does not send
1843 * expiration times back to us. Because of this we need to
1844 * start using SOLICITATION anyway.
1846 dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
1851 void __connman_dhcpv6_stop(struct connman_network *network)
1858 if (g_hash_table_remove(network_table, network))
1859 connman_network_unref(network);
1862 static int save_prefixes(struct connman_ipconfig *ipconfig,
1866 int i = 0, count = g_slist_length(prefixes);
1872 array = g_try_new0(char *, count + 1);
1876 for (list = prefixes; list; list = list->next) {
1877 char *elem, addr_str[INET6_ADDRSTRLEN];
1878 GDHCPIAPrefix *prefix = list->data;
1880 elem = g_strdup_printf("%s/%d", inet_ntop(AF_INET6,
1881 &prefix->prefix, addr_str, INET6_ADDRSTRLEN),
1891 __connman_ipconfig_set_dhcpv6_prefixes(ipconfig, array);
1895 static GSList *load_prefixes(struct connman_ipconfig *ipconfig)
1898 GSList *list = NULL;
1899 char **array = __connman_ipconfig_get_dhcpv6_prefixes(ipconfig);
1904 for (i = 0; array[i]; i++) {
1905 GDHCPIAPrefix *prefix;
1907 char *ptr, **elems = g_strsplit(array[i], "/", 0);
1912 value = strtol(elems[1], &ptr, 10);
1913 if (ptr != elems[1] && *ptr == '\0' && value <= 128) {
1914 struct in6_addr addr;
1916 if (inet_pton(AF_INET6, elems[0], &addr) == 1) {
1917 prefix = g_try_new0(GDHCPIAPrefix, 1);
1922 memcpy(&prefix->prefix, &addr,
1923 sizeof(struct in6_addr));
1924 prefix->prefixlen = value;
1926 list = g_slist_prepend(list, prefix);
1936 static GDHCPIAPrefix *copy_prefix(gpointer data)
1938 GDHCPIAPrefix *copy, *prefix = data;
1940 copy = g_try_new(GDHCPIAPrefix, 1);
1944 memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
1949 static GSList *copy_and_convert_prefixes(GList *prefixes)
1951 GSList *copy = NULL;
1954 for (list = prefixes; list; list = list->next)
1955 copy = g_slist_prepend(copy, copy_prefix(list->data));
1960 static int set_prefixes(GDHCPClient *dhcp_client, struct connman_dhcpv6 *dhcp)
1963 g_slist_free_full(dhcp->prefixes, free_prefix);
1966 copy_and_convert_prefixes(g_dhcp_client_get_option(dhcp_client,
1969 DBG("Got %d prefix", g_slist_length(dhcp->prefixes));
1971 if (dhcp->callback) {
1972 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
1973 if (status == G_DHCPV6_ERROR_NO_PREFIX)
1974 dhcp->callback(dhcp->network,
1975 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1977 struct connman_service *service;
1978 struct connman_ipconfig *ipconfig;
1979 int ifindex = connman_network_get_index(dhcp->network);
1981 service = __connman_service_lookup_from_index(ifindex);
1983 ipconfig = __connman_service_get_ip6config(
1985 save_prefixes(ipconfig, dhcp->prefixes);
1986 __connman_service_save(service);
1989 dhcp->callback(dhcp->network,
1990 CONNMAN_DHCPV6_STATUS_SUCCEED, dhcp->prefixes);
1993 g_slist_free_full(dhcp->prefixes, free_prefix);
1994 dhcp->prefixes = NULL;
2000 static void re_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2002 struct connman_dhcpv6 *dhcp = user_data;
2006 status = g_dhcpv6_client_get_status(dhcp_client);
2008 DBG("dhcpv6 cb msg %p status %d", dhcp, status);
2010 if (status == G_DHCPV6_ERROR_BINDING) {
2011 /* RFC 3315, 18.1.8 */
2012 dhcpv6_pd_request(dhcp);
2014 ret = set_prefixes(dhcp_client, dhcp);
2017 dhcp->callback(dhcp->network,
2018 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
2024 static void rebind_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2028 g_dhcpv6_client_clear_retransmit(dhcp_client);
2030 re_pd_cb(dhcp_client, user_data);
2033 static GDHCPClient *create_pd_client(struct connman_dhcpv6 *dhcp, int *err)
2035 GDHCPClient *dhcp_client;
2036 GDHCPClientError error;
2037 struct connman_service *service;
2041 index = connman_network_get_index(dhcp->network);
2043 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
2044 if (error != G_DHCP_CLIENT_ERROR_NONE) {
2049 if (getenv("CONNMAN_DHCPV6_DEBUG"))
2050 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6:PD");
2052 service = connman_service_lookup_from_network(dhcp->network);
2054 g_dhcp_client_unref(dhcp_client);
2059 ret = set_duid(service, dhcp->network, dhcp_client, index);
2061 g_dhcp_client_unref(dhcp_client);
2066 g_dhcpv6_client_create_iaid(dhcp_client, index, (unsigned char *)&iaid);
2067 g_dhcpv6_client_set_iaid(dhcp_client, iaid);
2072 static int dhcpv6_pd_rebind(struct connman_dhcpv6 *dhcp)
2074 GDHCPClient *dhcp_client;
2077 DBG("dhcp %p", dhcp);
2079 if (!dhcp->dhcp_client) {
2081 * We skipped the solicitation phase
2085 dhcp->dhcp_client = create_pd_client(dhcp, &err);
2086 if (!dhcp->dhcp_client) {
2092 dhcp_client = dhcp->dhcp_client;
2094 g_dhcp_client_clear_requests(dhcp_client);
2096 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2097 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
2098 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
2100 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2101 g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2103 clear_callbacks(dhcp_client);
2105 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
2106 rebind_pd_cb, dhcp);
2108 return g_dhcp_client_start(dhcp_client, NULL);
2111 static void renew_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2115 g_dhcpv6_client_clear_retransmit(dhcp_client);
2117 re_pd_cb(dhcp_client, user_data);
2120 static int dhcpv6_pd_renew(struct connman_dhcpv6 *dhcp)
2122 GDHCPClient *dhcp_client;
2125 DBG("dhcp %p", dhcp);
2127 dhcp_client = dhcp->dhcp_client;
2129 g_dhcp_client_clear_requests(dhcp_client);
2131 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2132 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
2133 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
2134 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
2136 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2137 g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2139 clear_callbacks(dhcp_client);
2141 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
2144 return g_dhcp_client_start(dhcp_client, NULL);
2148 * Check if we need to restart the solicitation procedure. This
2149 * is done if all the prefixes have expired.
2151 static int check_pd_restart(struct connman_dhcpv6 *dhcp)
2153 time_t current, expired;
2155 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
2157 current = time(NULL);
2159 if (current > expired) {
2160 DBG("expired by %d secs", (int)(current - expired));
2162 g_timeout_add(0, dhcpv6_restart, dhcp);
2170 static gboolean timeout_pd_rebind(gpointer user_data)
2172 struct connman_dhcpv6 *dhcp = user_data;
2174 if (check_pd_restart(dhcp) < 0)
2177 dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
2179 DBG("rebind RT timeout %d msec", dhcp->RT);
2181 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_rebind, dhcp);
2183 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2185 g_dhcp_client_start(dhcp->dhcp_client, NULL);
2190 static gboolean start_pd_rebind(gpointer user_data)
2192 struct connman_dhcpv6 *dhcp = user_data;
2194 if (check_pd_restart(dhcp) < 0)
2197 dhcp->RT = initial_rt(REB_TIMEOUT);
2199 DBG("rebind initial RT timeout %d msec", dhcp->RT);
2201 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_rebind, dhcp);
2203 dhcpv6_pd_rebind(dhcp);
2208 static gboolean timeout_pd_rebind_confirm(gpointer user_data)
2210 struct connman_dhcpv6 *dhcp = user_data;
2212 dhcp->RT = calc_delay(dhcp->RT, CNF_MAX_RT);
2214 DBG("rebind with confirm RT timeout %d msec", dhcp->RT);
2216 dhcp->timeout = g_timeout_add(dhcp->RT,
2217 timeout_pd_rebind_confirm, dhcp);
2219 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2221 g_dhcp_client_start(dhcp->dhcp_client, NULL);
2226 static gboolean timeout_pd_max_confirm(gpointer user_data)
2228 struct connman_dhcpv6 *dhcp = user_data;
2234 DBG("rebind with confirm max retransmit duration timeout");
2236 g_dhcpv6_client_clear_retransmit(dhcp->dhcp_client);
2239 dhcp->callback(dhcp->network,
2240 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
2245 static gboolean start_pd_rebind_with_confirm(gpointer user_data)
2247 struct connman_dhcpv6 *dhcp = user_data;
2249 dhcp->RT = initial_rt(CNF_TIMEOUT);
2251 DBG("rebind with confirm initial RT timeout %d msec", dhcp->RT);
2253 dhcp->timeout = g_timeout_add(dhcp->RT,
2254 timeout_pd_rebind_confirm, dhcp);
2255 dhcp->MRD = g_timeout_add(CNF_MAX_RD, timeout_pd_max_confirm, dhcp);
2257 dhcpv6_pd_rebind(dhcp);
2262 static gboolean timeout_pd_renew(gpointer user_data)
2264 struct connman_dhcpv6 *dhcp = user_data;
2266 if (check_pd_restart(dhcp) < 0)
2269 dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
2271 DBG("renew RT timeout %d msec", dhcp->RT);
2273 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
2275 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2277 g_dhcp_client_start(dhcp->dhcp_client, NULL);
2282 static gboolean start_pd_renew(gpointer user_data)
2284 struct connman_dhcpv6 *dhcp = user_data;
2286 dhcp->RT = initial_rt(REN_TIMEOUT);
2288 DBG("renew initial RT timeout %d msec", dhcp->RT);
2290 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_renew, dhcp);
2292 dhcpv6_pd_renew(dhcp);
2297 int __connman_dhcpv6_start_pd_renew(struct connman_network *network,
2300 struct connman_dhcpv6 *dhcp;
2302 time_t started, current, expired;
2304 dhcp = g_hash_table_lookup(network_pd_table, network);
2308 DBG("network %p dhcp %p", network, dhcp);
2312 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
2313 &started, &expired);
2315 current = time(NULL);
2317 DBG("T1 %u T2 %u expires %lu current %lu started %lu", T1, T2,
2318 expired, current, started);
2320 if (T1 == 0xffffffff)
2321 /* RFC 3633, ch 9 */
2326 * Client can choose the timeout.
2330 dhcp->callback = callback;
2332 /* RFC 3315, 18.1.4, start solicit if expired */
2333 if (check_pd_restart(dhcp) < 0)
2336 if (T2 != 0xffffffff && T2 > 0) {
2337 if ((unsigned)current >= (unsigned)started + T2) {
2338 /* RFC 3315, chapter 18.1.3, start rebind */
2339 DBG("rebind after %d secs", T2);
2341 dhcp->timeout = g_timeout_add_seconds(T2,
2345 } else if ((unsigned)current < (unsigned)started + T1) {
2346 DBG("renew after %d secs", T1);
2348 dhcp->timeout = g_timeout_add_seconds(T1,
2352 DBG("rebind after %d secs", T2 - T1);
2354 dhcp->timeout = g_timeout_add_seconds(T2 - T1,
2363 static void release_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2368 int __connman_dhcpv6_start_pd_release(struct connman_network *network,
2371 struct connman_dhcpv6 *dhcp;
2372 GDHCPClient *dhcp_client;
2376 return 0; /* we are already released */
2378 dhcp = g_hash_table_lookup(network_pd_table, network);
2382 DBG("network %p dhcp %p client %p", network, dhcp, dhcp->dhcp_client);
2386 dhcp_client = dhcp->dhcp_client;
2388 DBG("DHCPv6 PD was not started");
2392 g_dhcp_client_clear_requests(dhcp_client);
2393 g_dhcp_client_clear_values(dhcp_client);
2395 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2396 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
2398 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2399 g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2401 clear_callbacks(dhcp_client);
2403 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RELEASE,
2404 release_pd_cb, dhcp);
2406 dhcp->dhcp_client = dhcp_client;
2408 return g_dhcp_client_start(dhcp_client, NULL);
2411 static gboolean timeout_pd_request(gpointer user_data)
2413 struct connman_dhcpv6 *dhcp = user_data;
2415 if (dhcp->request_count >= REQ_MAX_RC) {
2416 DBG("max request retry attempts %d", dhcp->request_count);
2417 dhcp->request_count = 0;
2419 dhcp->callback(dhcp->network,
2420 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
2424 dhcp->request_count++;
2426 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
2427 DBG("request RT timeout %d msec", dhcp->RT);
2428 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_request, dhcp);
2430 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2432 g_dhcp_client_start(dhcp->dhcp_client, NULL);
2437 static void request_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2439 struct connman_dhcpv6 *dhcp = user_data;
2444 g_dhcpv6_client_clear_retransmit(dhcp_client);
2446 status = g_dhcpv6_client_get_status(dhcp_client);
2448 DBG("dhcpv6 pd cb msg %p status %d", dhcp, status);
2450 if (status == G_DHCPV6_ERROR_BINDING) {
2451 /* RFC 3315, 18.1.8 */
2452 dhcpv6_pd_request(dhcp);
2454 set_prefixes(dhcp_client, dhcp);
2458 static int dhcpv6_pd_request(struct connman_dhcpv6 *dhcp)
2460 GDHCPClient *dhcp_client;
2461 uint32_t T1 = 0, T2 = 0;
2463 DBG("dhcp %p", dhcp);
2465 dhcp_client = dhcp->dhcp_client;
2467 g_dhcp_client_clear_requests(dhcp_client);
2469 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2470 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
2471 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
2472 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
2474 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2475 g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2477 clear_callbacks(dhcp_client);
2479 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
2480 request_pd_cb, dhcp);
2482 return g_dhcp_client_start(dhcp_client, NULL);
2485 static void advertise_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2487 struct connman_dhcpv6 *dhcp = user_data;
2489 DBG("dhcpv6 advertise pd msg %p", dhcp);
2493 g_dhcpv6_client_clear_retransmit(dhcp_client);
2495 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
2497 dhcp->callback(dhcp->network,
2498 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
2502 dhcp->RT = initial_rt(REQ_TIMEOUT);
2503 DBG("request initial RT timeout %d msec", dhcp->RT);
2504 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_request, dhcp);
2506 dhcp->request_count = 1;
2508 dhcpv6_pd_request(dhcp);
2511 static void solicitation_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2514 * This callback is here so that g_dhcp_client_start()
2515 * will enter the proper L3 mode.
2517 DBG("DHCPv6 %p solicitation msg received, ignoring it", user_data);
2520 static int dhcpv6_pd_solicitation(struct connman_dhcpv6 *dhcp)
2522 GDHCPClient *dhcp_client;
2525 DBG("dhcp %p", dhcp);
2527 dhcp_client = create_pd_client(dhcp, &ret);
2533 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2535 g_dhcpv6_client_set_pd(dhcp_client, NULL, NULL, NULL);
2537 clear_callbacks(dhcp_client);
2539 g_dhcp_client_register_event(dhcp_client,
2540 G_DHCP_CLIENT_EVENT_ADVERTISE,
2541 advertise_pd_cb, dhcp);
2543 g_dhcp_client_register_event(dhcp_client,
2544 G_DHCP_CLIENT_EVENT_SOLICITATION,
2545 solicitation_pd_cb, dhcp);
2547 dhcp->dhcp_client = dhcp_client;
2549 return g_dhcp_client_start(dhcp_client, NULL);
2552 static gboolean start_pd_solicitation(gpointer user_data)
2554 struct connman_dhcpv6 *dhcp = user_data;
2556 /* Set the retransmission timeout, RFC 3315 chapter 14 */
2557 dhcp->RT = initial_rt(SOL_TIMEOUT);
2559 DBG("solicit initial RT timeout %d msec", dhcp->RT);
2561 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
2563 dhcpv6_pd_solicitation(dhcp);
2568 int __connman_dhcpv6_start_pd(int index, GSList *prefixes, dhcpv6_cb callback)
2570 struct connman_service *service;
2571 struct connman_network *network;
2572 struct connman_dhcpv6 *dhcp;
2577 DBG("index %d", index);
2579 service = __connman_service_lookup_from_index(index);
2583 network = __connman_service_get_network(service);
2587 if (network_pd_table) {
2588 dhcp = g_hash_table_lookup(network_pd_table, network);
2589 if (dhcp && dhcp->started)
2593 dhcp = g_try_new0(struct connman_dhcpv6, 1);
2597 dhcp->network = network;
2598 dhcp->callback = callback;
2599 dhcp->started = true;
2603 * Try to load the earlier prefixes if caller did not supply
2604 * any that we could use.
2606 struct connman_ipconfig *ipconfig;
2607 ipconfig = __connman_service_get_ip6config(service);
2609 dhcp->prefixes = load_prefixes(ipconfig);
2611 dhcp->prefixes = prefixes;
2613 connman_network_ref(network);
2615 DBG("replace network %p dhcp %p", network, dhcp);
2617 g_hash_table_replace(network_pd_table, network, dhcp);
2619 if (!dhcp->prefixes) {
2621 * Refresh start, try to get prefixes.
2623 start_pd_solicitation(dhcp);
2626 * We used to have prefixes, try to use them again.
2627 * We need to use timeouts from confirm msg, RFC 3633, ch 12.1
2629 start_pd_rebind_with_confirm(dhcp);
2635 void __connman_dhcpv6_stop_pd(int index)
2637 struct connman_service *service;
2638 struct connman_network *network;
2643 DBG("index %d", index);
2645 if (!network_pd_table)
2648 service = __connman_service_lookup_from_index(index);
2652 network = __connman_service_get_network(service);
2656 __connman_dhcpv6_start_pd_release(network, NULL);
2658 if (g_hash_table_remove(network_pd_table, network))
2659 connman_network_unref(network);
2662 int __connman_dhcpv6_init(void)
2666 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2667 NULL, remove_network);
2669 network_pd_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2670 NULL, remove_network);
2675 void __connman_dhcpv6_cleanup(void)
2679 g_hash_table_destroy(network_table);
2680 network_table = NULL;
2682 g_hash_table_destroy(network_pd_table);
2683 network_pd_table = NULL;