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 guint compute_random(guint val)
112 __connman_util_get_random(&rand);
114 return val - val / 10 +
115 ((guint) rand % (2 * 1000)) * val / 10 / 1000;
118 /* Calculate a random delay, RFC 3315 chapter 14 */
119 /* RT and MRT are milliseconds */
120 static guint calc_delay(guint RT, guint MRT)
122 if (MRT && (RT > MRT / 2))
123 RT = compute_random(MRT);
125 RT += compute_random(RT);
130 static guint initial_rt(guint timeout)
132 return compute_random(timeout);
135 static void free_prefix(gpointer data)
140 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
142 g_strfreev(dhcp->nameservers);
143 g_strfreev(dhcp->timeservers);
145 dhcp->nameservers = NULL;
146 dhcp->timeservers = NULL;
147 dhcp->started = false;
149 g_slist_free_full(dhcp->prefixes, free_prefix);
152 static bool compare_string_arrays(char **array_a, char **array_b)
156 if (!array_a || !array_b)
159 if (g_strv_length(array_a) != g_strv_length(array_b))
162 for (i = 0; array_a[i] && array_b[i]; i++)
163 if (g_strcmp0(array_a[i], array_b[i]) != 0)
169 static void dhcpv6_debug(const char *str, void *data)
171 connman_info("%s: %s\n", (const char *) data, str);
174 static gchar *convert_to_hex(unsigned char *buf, int len)
176 gchar *ret = g_try_malloc(len * 2 + 1);
179 for (i = 0; ret && i < len; i++)
180 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
186 * DUID should not change over time so save it to file.
187 * See RFC 3315 chapter 9 for details.
189 static int set_duid(struct connman_service *service,
190 struct connman_network *network,
191 GDHCPClient *dhcp_client, int index)
199 ident = connman_service_get_identifier(service);
201 keyfile = connman_storage_load_service(ident);
205 hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
208 unsigned int i, j = 0, hex;
209 size_t hex_duid_len = strlen(hex_duid);
211 duid = g_try_malloc0(hex_duid_len / 2);
213 g_key_file_free(keyfile);
218 for (i = 0; i < hex_duid_len; i += 2) {
219 sscanf(hex_duid + i, "%02x", &hex);
223 duid_len = hex_duid_len / 2;
226 int type = __connman_ipconfig_get_type_from_index(index);
228 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
231 g_key_file_free(keyfile);
235 hex_duid = convert_to_hex(duid, duid_len);
238 g_key_file_free(keyfile);
242 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
245 __connman_storage_save_service(keyfile, ident);
249 g_key_file_free(keyfile);
251 g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
256 static void clear_callbacks(GDHCPClient *dhcp_client)
258 g_dhcp_client_register_event(dhcp_client,
259 G_DHCP_CLIENT_EVENT_SOLICITATION,
262 g_dhcp_client_register_event(dhcp_client,
263 G_DHCP_CLIENT_EVENT_ADVERTISE,
266 g_dhcp_client_register_event(dhcp_client,
267 G_DHCP_CLIENT_EVENT_REQUEST,
270 g_dhcp_client_register_event(dhcp_client,
271 G_DHCP_CLIENT_EVENT_CONFIRM,
274 g_dhcp_client_register_event(dhcp_client,
275 G_DHCP_CLIENT_EVENT_RENEW,
278 g_dhcp_client_register_event(dhcp_client,
279 G_DHCP_CLIENT_EVENT_REBIND,
282 g_dhcp_client_register_event(dhcp_client,
283 G_DHCP_CLIENT_EVENT_RELEASE,
286 g_dhcp_client_register_event(dhcp_client,
287 G_DHCP_CLIENT_EVENT_DECLINE,
290 g_dhcp_client_register_event(dhcp_client,
291 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
295 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
297 struct connman_dhcpv6 *dhcp = user_data;
298 struct connman_service *service;
300 GList *option, *list;
301 char **nameservers, **timeservers;
303 DBG("dhcpv6 information-request %p", dhcp);
305 service = connman_service_lookup_from_network(dhcp->network);
307 connman_error("Can not lookup service");
311 g_dhcpv6_client_clear_retransmit(dhcp_client);
313 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
314 entries = g_list_length(option);
316 nameservers = g_try_new0(char *, entries + 1);
318 for (i = 0, list = option; list; list = list->next, i++)
319 nameservers[i] = g_strdup(list->data);
322 if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
323 if (dhcp->nameservers) {
324 for (i = 0; dhcp->nameservers[i]; i++)
325 __connman_service_nameserver_remove(service,
326 dhcp->nameservers[i],
328 g_strfreev(dhcp->nameservers);
331 dhcp->nameservers = nameservers;
333 for (i = 0; dhcp->nameservers &&
334 dhcp->nameservers[i]; i++)
335 __connman_service_nameserver_append(service,
336 dhcp->nameservers[i],
339 g_strfreev(nameservers);
342 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
343 entries = g_list_length(option);
345 timeservers = g_try_new0(char *, entries + 1);
347 for (i = 0, list = option; list; list = list->next, i++)
348 timeservers[i] = g_strdup(list->data);
351 if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
352 if (dhcp->timeservers) {
353 for (i = 0; dhcp->timeservers[i]; i++)
354 __connman_service_timeserver_remove(service,
355 dhcp->timeservers[i]);
356 g_strfreev(dhcp->timeservers);
359 dhcp->timeservers = timeservers;
361 for (i = 0; dhcp->timeservers &&
362 dhcp->timeservers[i]; i++)
363 __connman_service_timeserver_append(service,
364 dhcp->timeservers[i]);
366 g_strfreev(timeservers);
369 if (dhcp->callback) {
370 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
371 dhcp->callback(dhcp->network, status == 0 ?
372 CONNMAN_DHCPV6_STATUS_SUCCEED :
373 CONNMAN_DHCPV6_STATUS_FAIL,
378 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
380 struct connman_service *service;
381 GDHCPClient *dhcp_client;
382 GDHCPClientError error;
385 DBG("dhcp %p", dhcp);
387 index = connman_network_get_index(dhcp->network);
389 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
390 if (error != G_DHCP_CLIENT_ERROR_NONE) {
395 if (getenv("CONNMAN_DHCPV6_DEBUG"))
396 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
398 service = connman_service_lookup_from_network(dhcp->network);
401 g_dhcp_client_unref(dhcp_client);
405 ret = set_duid(service, dhcp->network, dhcp_client, index);
408 g_dhcp_client_unref(dhcp_client);
412 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
413 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
414 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
415 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
417 g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
418 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
420 g_dhcp_client_register_event(dhcp_client,
421 G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
423 dhcp->dhcp_client = dhcp_client;
425 return g_dhcp_client_start(dhcp_client, NULL);
428 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
430 struct in6_addr addr_prefix, addr;
434 for (list = prefixes; list; list = list->next) {
435 char *prefix = list->data;
436 const char *slash = g_strrstr(prefix, "/");
437 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
438 0xF0, 0xE0, 0xC0, 0x80 };
439 int left, count, i, plen;
444 len = strtol(slash + 1, NULL, 10);
445 if (len < 3 || len > 128)
454 prefix = g_strndup(prefix, slash - prefix);
455 inet_pton(AF_INET6, prefix, &addr_prefix);
456 inet_pton(AF_INET6, address, &addr);
458 memset(&addr_prefix.s6_addr[i], 0, count);
459 memset(&addr.s6_addr[i], 0, count);
462 addr_prefix.s6_addr[i - 1] &= bits[left];
463 addr.s6_addr[i - 1] &= bits[left];
468 if (memcmp(&addr_prefix, &addr, 16) == 0) {
477 static int set_other_addresses(GDHCPClient *dhcp_client,
478 struct connman_dhcpv6 *dhcp)
480 struct connman_service *service;
482 GList *option, *list;
483 char **nameservers, **timeservers;
485 service = connman_service_lookup_from_network(dhcp->network);
487 connman_error("Can not lookup service");
492 * Check domains before nameservers so that the nameserver append
493 * function will update domain list in service.c
495 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DOMAIN_LIST);
496 entries = g_list_length(option);
498 char **domains = g_try_new0(char *, entries + 1);
500 for (i = 0, list = option; list;
501 list = list->next, i++)
502 domains[i] = g_strdup(list->data);
503 __connman_service_set_search_domains(service, domains);
508 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
509 entries = g_list_length(option);
511 nameservers = g_try_new0(char *, entries + 1);
513 for (i = 0, list = option; list; list = list->next, i++)
514 nameservers[i] = g_strdup(list->data);
517 if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
518 if (dhcp->nameservers) {
519 for (i = 0; dhcp->nameservers[i]; i++)
520 __connman_service_nameserver_remove(service,
521 dhcp->nameservers[i],
523 g_strfreev(dhcp->nameservers);
526 dhcp->nameservers = nameservers;
528 for (i = 0; dhcp->nameservers &&
529 dhcp->nameservers[i]; i++)
530 __connman_service_nameserver_append(service,
531 dhcp->nameservers[i],
534 g_strfreev(nameservers);
537 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
538 entries = g_list_length(option);
540 timeservers = g_try_new0(char *, entries + 1);
542 for (i = 0, list = option; list; list = list->next, i++)
543 timeservers[i] = g_strdup(list->data);
546 if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
547 if (dhcp->timeservers) {
548 for (i = 0; dhcp->timeservers[i]; i++)
549 __connman_service_timeserver_remove(service,
550 dhcp->timeservers[i]);
551 g_strfreev(dhcp->timeservers);
554 dhcp->timeservers = timeservers;
556 for (i = 0; dhcp->timeservers &&
557 dhcp->timeservers[i]; i++)
558 __connman_service_timeserver_append(service,
559 dhcp->timeservers[i]);
561 g_strfreev(timeservers);
566 static GSList *copy_prefixes(GSList *prefixes)
568 GSList *list, *copy = NULL;
570 for (list = prefixes; list; list = list->next)
571 copy = g_slist_prepend(copy, g_strdup(list->data));
577 * Helper struct for doing DAD (duplicate address detection).
578 * It is refcounted and freed after all reply's to neighbor
579 * discovery request are received.
585 GDHCPClient *dhcp_client;
586 struct connman_ipconfig *ipconfig;
594 static void free_own_address(struct own_address *data)
596 g_dhcp_client_unref(data->dhcp_client);
597 __connman_ipconfig_unref(data->ipconfig);
598 g_slist_free_full(data->prefixes, free_prefix);
599 g_slist_free_full(data->dad_failed, g_free);
600 g_slist_free_full(data->dad_succeed, g_free);
605 static struct own_address *ref_own_address(struct own_address *address)
607 DBG("%p ref %d", address, address->refcount + 1);
609 __sync_fetch_and_add(&address->refcount, 1);
614 static void unref_own_address(struct own_address *address)
619 DBG("%p ref %d", address, address->refcount - 1);
621 if (__sync_fetch_and_sub(&address->refcount, 1) != 1)
624 free_own_address(address);
627 static void set_address(int ifindex, struct connman_ipconfig *ipconfig,
628 GSList *prefixes, char *address)
630 const char *c_address;
632 c_address = __connman_ipconfig_get_local(ipconfig);
634 if (address && ((c_address && g_strcmp0(address, c_address) != 0) ||
638 /* Is this prefix part of the subnet we are suppose to use? */
639 prefix_len = check_ipv6_addr_prefix(prefixes, address);
641 __connman_ipconfig_address_remove(ipconfig);
642 __connman_ipconfig_set_local(ipconfig, address);
643 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
645 DBG("new address %s/%d", address, prefix_len);
647 __connman_ipconfig_set_dhcp_address(ipconfig, address);
648 __connman_service_save(
649 __connman_service_lookup_from_index(ifindex));
655 * Helper struct that is used when waiting a reply to DECLINE message.
657 struct decline_cb_data {
658 GDHCPClient *dhcp_client;
664 static void decline_reply_callback(struct decline_cb_data *data)
666 struct connman_network *network;
667 struct connman_service *service;
669 service = __connman_service_lookup_from_index(data->ifindex);
670 network = __connman_service_get_network(service);
673 data->callback(network, CONNMAN_DHCPV6_STATUS_RESTART, NULL);
675 g_dhcp_client_unref(data->dhcp_client);
678 static gboolean decline_timeout(gpointer user_data)
680 struct decline_cb_data *data = user_data;
682 DBG("ifindex %d", data->ifindex);
685 * We ignore all DECLINE replies that are received after the timeout
687 g_dhcp_client_register_event(data->dhcp_client,
688 G_DHCP_CLIENT_EVENT_DECLINE,
691 decline_reply_callback(data);
698 static void decline_cb(GDHCPClient *dhcp_client, gpointer user_data)
700 struct decline_cb_data *data = user_data;
702 DBG("ifindex %d", data->ifindex);
704 g_dhcpv6_client_clear_retransmit(dhcp_client);
707 g_source_remove(data->timeout);
709 decline_reply_callback(data);
714 static int dhcpv6_decline(GDHCPClient *dhcp_client, int ifindex,
715 dhcpv6_cb callback, GSList *failed)
717 struct decline_cb_data *data;
721 DBG("dhcp_client %p", dhcp_client);
723 g_dhcp_client_clear_requests(dhcp_client);
725 g_dhcpv6_client_clear_send(dhcp_client, G_DHCPV6_ORO);
727 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
728 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
730 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
732 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
734 code = G_DHCPV6_IA_TA;
738 code = G_DHCPV6_IA_NA;
740 g_dhcpv6_client_clear_send(dhcp_client, code);
742 g_dhcpv6_client_set_ias(dhcp_client, ifindex, code, NULL, NULL,
745 clear_callbacks(dhcp_client);
747 data = g_try_new(struct decline_cb_data, 1);
751 data->ifindex = ifindex;
752 data->callback = callback;
753 data->dhcp_client = g_dhcp_client_ref(dhcp_client);
754 data->timeout = g_timeout_add(DEC_TIMEOUT, decline_timeout, data);
756 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_DECLINE,
759 return g_dhcp_client_start(dhcp_client, NULL);
762 static void dad_reply(struct nd_neighbor_advert *reply,
763 unsigned int length, struct in6_addr *addr, void *user_data)
765 struct own_address *data = user_data;
767 char address[INET6_ADDRSTRLEN];
768 enum __connman_dhcpv6_status status = CONNMAN_DHCPV6_STATUS_FAIL;
770 inet_ntop(AF_INET6, addr, address, INET6_ADDRSTRLEN);
772 DBG("user %p reply %p len %d address %s index %d data %p", user_data,
773 reply, length, address, data->ifindex, data);
777 DBG("DAD succeed for %s", address);
779 DBG("DAD cannot be done for %s", address);
781 data->dad_succeed = g_slist_prepend(data->dad_succeed,
785 DBG("DAD failed for %s", address);
787 data->dad_failed = g_slist_prepend(data->dad_failed,
792 * If refcount == 1 then we got the final reply and can continue.
794 if (data->refcount > 1)
797 for (list = data->dad_succeed; list; list = list->next)
798 set_address(data->ifindex, data->ipconfig, data->prefixes,
801 if (data->dad_failed) {
802 dhcpv6_decline(data->dhcp_client, data->ifindex,
803 data->callback, data->dad_failed);
805 if (data->dad_succeed)
806 status = CONNMAN_DHCPV6_STATUS_SUCCEED;
808 if (data->callback) {
809 struct connman_network *network;
810 struct connman_service *service;
812 service = __connman_service_lookup_from_index(
814 network = __connman_service_get_network(service);
816 data->callback(network, status, NULL);
820 unref_own_address(data);
824 * Is the kernel configured to do DAD? If 0, then do not do DAD.
825 * See also RFC 4862 chapter 5.4 about DupAddrDetectTransmits
827 static int dad_transmits(int ifindex)
829 char name[IF_NAMESIZE];
834 if (!if_indextoname(ifindex, name))
837 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/dad_transmits",
843 f = fopen(path, "r");
848 if (fscanf(f, "%d", &value) < 0)
857 static void do_dad(GDHCPClient *dhcp_client, struct connman_dhcpv6 *dhcp)
859 struct connman_service *service;
860 struct connman_ipconfig *ipconfig;
862 GList *option, *list;
863 struct own_address *user_data;
865 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
867 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
870 * Even if we didn't had any addresses, just try to set
873 set_other_addresses(dhcp_client, dhcp);
876 DBG("Skip DAD as no addresses found in reply");
878 dhcp->callback(dhcp->network,
879 CONNMAN_DHCPV6_STATUS_SUCCEED, NULL);
884 ifindex = connman_network_get_index(dhcp->network);
886 DBG("index %d", ifindex);
888 service = connman_service_lookup_from_network(dhcp->network);
890 connman_error("Can not lookup service for index %d", ifindex);
894 ipconfig = __connman_service_get_ip6config(service);
896 connman_error("Could not lookup ip6config for index %d",
901 if (!dad_transmits(ifindex)) {
902 DBG("Skip DAD because of kernel configuration");
904 for (list = option; list; list = list->next)
905 set_address(ifindex, ipconfig, dhcp->prefixes,
909 dhcp->callback(dhcp->network,
910 CONNMAN_DHCPV6_STATUS_SUCCEED, NULL);
915 if (g_list_length(option) == 0) {
916 DBG("No addresses when doing DAD");
918 dhcp->callback(dhcp->network,
919 CONNMAN_DHCPV6_STATUS_SUCCEED, NULL);
924 user_data = g_try_new0(struct own_address, 1);
928 user_data->refcount = 0;
929 user_data->ifindex = ifindex;
930 user_data->dhcp_client = g_dhcp_client_ref(dhcp_client);
931 user_data->ipconfig = __connman_ipconfig_ref(ipconfig);
932 user_data->prefixes = copy_prefixes(dhcp->prefixes);
933 user_data->callback = dhcp->callback;
936 * We send one neighbor discovery request / address
937 * and after all checks are done, then report the status
941 for (list = option; list; list = list->next) {
942 char *address = option->data;
943 struct in6_addr addr;
946 ref_own_address(user_data);
948 if (inet_pton(AF_INET6, address, &addr) != 1) {
949 DBG("Invalid IPv6 address %s %d/%s", address,
950 -errno, strerror(errno));
954 DBG("user %p address %s client %p ipconfig %p prefixes %p",
956 user_data->dhcp_client, user_data->ipconfig,
957 user_data->prefixes);
959 ret = __connman_inet_ipv6_do_dad(ifindex, 1000,
964 DBG("Could not send neighbor solicitation for %s",
966 dad_reply(NULL, -1, &addr, user_data);
968 DBG("Sent neighbor solicitation %d bytes", ret);
975 unref_own_address(user_data);
979 dhcp->callback(dhcp->network, CONNMAN_DHCPV6_STATUS_FAIL,
983 static gboolean timeout_request_resend(gpointer user_data)
985 struct connman_dhcpv6 *dhcp = user_data;
987 if (dhcp->request_count >= REQ_MAX_RC) {
988 DBG("max request retry attempts %d", dhcp->request_count);
989 dhcp->request_count = 0;
991 dhcp->callback(dhcp->network,
992 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
996 dhcp->request_count++;
998 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
999 DBG("request resend RT timeout %d msec", dhcp->RT);
1000 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request_resend, dhcp);
1002 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1004 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1009 static gboolean request_resend(gpointer user_data)
1011 struct connman_dhcpv6 *dhcp = user_data;
1013 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1015 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
1016 DBG("request resend RT timeout %d msec", dhcp->RT);
1017 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request_resend, dhcp);
1019 dhcpv6_request(dhcp, true);
1024 static void do_resend_request(struct connman_dhcpv6 *dhcp)
1026 if (dhcp->request_count >= REQ_MAX_RC) {
1027 DBG("max request retry attempts %d", dhcp->request_count);
1028 dhcp->request_count = 0;
1030 dhcp->callback(dhcp->network,
1031 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1035 dhcp->request_count++;
1037 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
1038 DBG("resending request after %d msec", dhcp->RT);
1039 dhcp->timeout = g_timeout_add(dhcp->RT, request_resend, dhcp);
1042 static void re_cb(enum request_type req_type, GDHCPClient *dhcp_client,
1045 struct connman_dhcpv6 *dhcp = user_data;
1050 status = g_dhcpv6_client_get_status(dhcp_client);
1052 DBG("dhcpv6 cb msg %p status %d", dhcp, status);
1055 * RFC 3315, 18.1.8 handle the resend if error
1057 if (status == G_DHCPV6_ERROR_BINDING) {
1058 dhcpv6_request(dhcp, false);
1059 } else if (status == G_DHCPV6_ERROR_MCAST) {
1062 dhcpv6_request(dhcp, true);
1065 dhcpv6_rebind(dhcp);
1071 } else if (status == G_DHCPV6_ERROR_LINK) {
1072 if (req_type == REQ_REQUEST) {
1073 g_dhcp_client_unref(dhcp->dhcp_client);
1074 start_solicitation(dhcp);
1077 dhcp->callback(dhcp->network,
1078 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1080 } else if (status == G_DHCPV6_ERROR_FAILURE) {
1081 if (req_type == REQ_REQUEST) {
1082 /* Rate limit the resend of request message */
1083 do_resend_request(dhcp);
1086 dhcp->callback(dhcp->network,
1087 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1092 * If we did not got any addresses, then re-send
1097 option = g_dhcp_client_get_option(dhcp->dhcp_client,
1100 option = g_dhcp_client_get_option(dhcp->dhcp_client,
1105 do_resend_request(dhcp);
1108 dhcpv6_rebind(dhcp);
1118 if (status == G_DHCPV6_ERROR_SUCCESS)
1119 do_dad(dhcp_client, dhcp);
1120 else if (dhcp->callback)
1121 dhcp->callback(dhcp->network,
1122 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1126 static void rebind_cb(GDHCPClient *dhcp_client, gpointer user_data)
1130 g_dhcpv6_client_reset_request(dhcp_client);
1131 g_dhcpv6_client_clear_retransmit(dhcp_client);
1133 re_cb(REQ_REBIND, dhcp_client, user_data);
1136 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp)
1138 GDHCPClient *dhcp_client;
1140 DBG("dhcp %p", dhcp);
1142 dhcp_client = dhcp->dhcp_client;
1144 g_dhcp_client_clear_requests(dhcp_client);
1146 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1147 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1148 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
1149 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1151 g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
1152 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
1154 g_dhcpv6_client_set_ia(dhcp_client,
1155 connman_network_get_index(dhcp->network),
1156 dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1157 NULL, NULL, TRUE, NULL);
1159 clear_callbacks(dhcp_client);
1161 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
1164 dhcp->dhcp_client = dhcp_client;
1166 return g_dhcp_client_start(dhcp_client, NULL);
1169 static gboolean dhcpv6_restart(gpointer user_data)
1171 struct connman_dhcpv6 *dhcp = user_data;
1174 dhcp->callback(dhcp->network, CONNMAN_DHCPV6_STATUS_FAIL,
1181 * Check if we need to restart the solicitation procedure. This
1182 * is done if all the addresses have expired. RFC 3315, 18.1.4
1184 static int check_restart(struct connman_dhcpv6 *dhcp)
1186 time_t current, expired;
1188 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
1191 /* infinite lifetime for an DHCPv6 address */
1192 if (expired == 0xffffffff)
1195 current = time(NULL);
1197 if (current >= expired) {
1198 DBG("expired by %d secs", (int)(current - expired));
1200 g_idle_add(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_idle_add(start_rebind, dhcp);
1461 } else if ((unsigned)current < (unsigned)started + T1) {
1462 delta = started + T1 - current;
1463 DBG("renew after %d secs", delta);
1465 dhcp->timeout = g_timeout_add_seconds(delta,
1468 delta = started + T2 - current;
1469 DBG("rebind after %d secs", delta);
1471 dhcp->timeout = g_timeout_add_seconds(delta,
1472 start_rebind, dhcp);
1479 static void release_cb(GDHCPClient *dhcp_client, gpointer user_data)
1484 int __connman_dhcpv6_start_release(struct connman_network *network,
1487 struct connman_dhcpv6 *dhcp;
1488 GDHCPClient *dhcp_client;
1491 return 0; /* we are already released */
1493 dhcp = g_hash_table_lookup(network_table, network);
1497 DBG("network %p dhcp %p client %p stateless %d", network, dhcp,
1498 dhcp->dhcp_client, dhcp->stateless);
1500 if (dhcp->stateless)
1505 dhcp_client = dhcp->dhcp_client;
1508 * We had started the DHCPv6 handshaking i.e., we have called
1509 * __connman_dhcpv6_start() but it has not yet sent
1510 * a solicitation message to server. This means that we do not
1511 * have DHCPv6 configured yet so we can just quit here.
1513 DBG("DHCPv6 was not started");
1517 g_dhcp_client_clear_requests(dhcp_client);
1518 g_dhcp_client_clear_values(dhcp_client);
1520 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1521 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
1523 g_dhcpv6_client_set_ia(dhcp_client,
1524 connman_network_get_index(dhcp->network),
1525 dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1526 NULL, NULL, TRUE, NULL);
1528 clear_callbacks(dhcp_client);
1531 * Although we register a callback here we are really not interested in
1532 * the answer because it might take too long time and network code
1533 * might be in the middle of the disconnect.
1534 * So we just inform the server that we are done with the addresses
1535 * but ignore the reply from server. This is allowed by RFC 3315
1538 g_dhcp_client_register_event(dhcp_client,
1539 G_DHCP_CLIENT_EVENT_RELEASE,
1542 dhcp->dhcp_client = dhcp_client;
1544 return g_dhcp_client_start(dhcp_client, NULL);
1547 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
1549 DBG("dhcp %p", dhcp);
1555 if (!dhcp->dhcp_client)
1558 g_dhcp_client_stop(dhcp->dhcp_client);
1559 g_dhcp_client_unref(dhcp->dhcp_client);
1561 dhcp->dhcp_client = NULL;
1566 static void remove_network(gpointer user_data)
1568 struct connman_dhcpv6 *dhcp = user_data;
1570 DBG("dhcp %p", dhcp);
1572 dhcpv6_release(dhcp);
1577 static gboolean timeout_info_req(gpointer user_data)
1579 struct connman_dhcpv6 *dhcp = user_data;
1581 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
1583 DBG("info RT timeout %d msec", dhcp->RT);
1585 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
1587 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1589 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1594 static gboolean start_info_req(gpointer user_data)
1596 struct connman_dhcpv6 *dhcp = user_data;
1598 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1599 dhcp->RT = initial_rt(INF_TIMEOUT);
1601 DBG("info initial RT timeout %d msec", dhcp->RT);
1603 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
1605 dhcpv6_info_request(dhcp);
1610 int __connman_dhcpv6_start_info(struct connman_network *network,
1613 struct connman_dhcpv6 *dhcp;
1619 if (network_table) {
1620 dhcp = g_hash_table_lookup(network_table, network);
1621 if (dhcp && dhcp->started)
1625 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1629 dhcp->network = network;
1630 dhcp->callback = callback;
1631 dhcp->stateless = true;
1632 dhcp->started = true;
1634 connman_network_ref(network);
1636 DBG("replace network %p dhcp %p", network, dhcp);
1638 g_hash_table_replace(network_table, network, dhcp);
1640 /* Initial timeout, RFC 3315, 18.1.5 */
1641 __connman_util_get_random(&rand);
1642 delay = rand % 1000;
1644 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
1649 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
1651 struct connman_dhcpv6 *dhcp = user_data;
1653 DBG("dhcpv6 advertise msg %p", dhcp);
1657 g_dhcpv6_client_clear_retransmit(dhcp_client);
1659 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
1661 dhcp->callback(dhcp->network,
1662 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1666 dhcp->RT = initial_rt(REQ_TIMEOUT);
1667 DBG("request initial RT timeout %d msec", dhcp->RT);
1668 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
1670 dhcp->request_count = 1;
1672 dhcpv6_request(dhcp, true);
1675 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
1677 /* We get here if server supports rapid commit */
1678 struct connman_dhcpv6 *dhcp = user_data;
1680 DBG("dhcpv6 solicitation msg %p", dhcp);
1684 g_dhcpv6_client_clear_retransmit(dhcp_client);
1686 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
1688 dhcp->callback(dhcp->network,
1689 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1693 do_dad(dhcp_client, dhcp);
1696 static gboolean timeout_solicitation(gpointer user_data)
1698 struct connman_dhcpv6 *dhcp = user_data;
1700 dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
1702 DBG("solicit RT timeout %d msec", dhcp->RT);
1704 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1706 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1708 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1713 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
1715 struct connman_service *service;
1716 struct connman_ipconfig *ipconfig_ipv6;
1717 GDHCPClient *dhcp_client;
1718 GDHCPClientError error;
1721 DBG("dhcp %p", dhcp);
1723 index = connman_network_get_index(dhcp->network);
1725 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
1726 if (error != G_DHCP_CLIENT_ERROR_NONE) {
1731 if (getenv("CONNMAN_DHCPV6_DEBUG"))
1732 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
1734 service = connman_service_lookup_from_network(dhcp->network);
1737 g_dhcp_client_unref(dhcp_client);
1741 ret = set_duid(service, dhcp->network, dhcp_client, index);
1744 g_dhcp_client_unref(dhcp_client);
1748 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1749 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
1750 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1751 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
1752 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1754 g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
1755 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
1757 ipconfig_ipv6 = __connman_service_get_ip6config(service);
1758 dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
1760 g_dhcpv6_client_set_ia(dhcp_client, index,
1761 dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1762 NULL, NULL, FALSE, NULL);
1764 clear_callbacks(dhcp_client);
1766 g_dhcp_client_register_event(dhcp_client,
1767 G_DHCP_CLIENT_EVENT_SOLICITATION,
1768 solicitation_cb, dhcp);
1770 g_dhcp_client_register_event(dhcp_client,
1771 G_DHCP_CLIENT_EVENT_ADVERTISE,
1772 advertise_cb, dhcp);
1774 dhcp->dhcp_client = dhcp_client;
1776 return g_dhcp_client_start(dhcp_client, NULL);
1779 static gboolean start_solicitation(gpointer user_data)
1781 struct connman_dhcpv6 *dhcp = user_data;
1783 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1784 dhcp->RT = initial_rt(SOL_TIMEOUT);
1786 DBG("solicit initial RT timeout %d msec", dhcp->RT);
1788 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1790 dhcpv6_solicitation(dhcp);
1795 int __connman_dhcpv6_start(struct connman_network *network,
1796 GSList *prefixes, dhcpv6_cb callback)
1798 struct connman_service *service;
1799 struct connman_dhcpv6 *dhcp;
1805 if (network_table) {
1806 dhcp = g_hash_table_lookup(network_table, network);
1807 if (dhcp && dhcp->started)
1811 service = connman_service_lookup_from_network(network);
1815 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1819 dhcp->network = network;
1820 dhcp->callback = callback;
1821 dhcp->prefixes = prefixes;
1822 dhcp->started = true;
1824 connman_network_ref(network);
1826 DBG("replace network %p dhcp %p", network, dhcp);
1828 g_hash_table_replace(network_table, network, dhcp);
1830 /* Initial timeout, RFC 3315, 17.1.2 */
1831 __connman_util_get_random(&rand);
1832 delay = rand % 1000;
1835 * Start from scratch.
1836 * RFC 3315, chapter 17.1.2 Solicitation message
1838 * Note that we do not send CONFIRM message here as it does
1839 * not make much sense because we do not save expiration time
1840 * so we cannot really know how long the saved address is valid
1841 * anyway. The reply to CONFIRM message does not send
1842 * expiration times back to us. Because of this we need to
1843 * start using SOLICITATION anyway.
1845 dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
1850 void __connman_dhcpv6_stop(struct connman_network *network)
1857 if (g_hash_table_remove(network_table, network))
1858 connman_network_unref(network);
1861 static int save_prefixes(struct connman_ipconfig *ipconfig,
1865 int i = 0, count = g_slist_length(prefixes);
1871 array = g_try_new0(char *, count + 1);
1875 for (list = prefixes; list; list = list->next) {
1876 char *elem, addr_str[INET6_ADDRSTRLEN];
1877 GDHCPIAPrefix *prefix = list->data;
1879 elem = g_strdup_printf("%s/%d", inet_ntop(AF_INET6,
1880 &prefix->prefix, addr_str, INET6_ADDRSTRLEN),
1890 __connman_ipconfig_set_dhcpv6_prefixes(ipconfig, array);
1894 static GSList *load_prefixes(struct connman_ipconfig *ipconfig)
1897 GSList *list = NULL;
1898 char **array = __connman_ipconfig_get_dhcpv6_prefixes(ipconfig);
1903 for (i = 0; array[i]; i++) {
1904 GDHCPIAPrefix *prefix;
1906 char *ptr, **elems = g_strsplit(array[i], "/", 0);
1911 value = strtol(elems[1], &ptr, 10);
1912 if (ptr != elems[1] && *ptr == '\0' && value <= 128) {
1913 struct in6_addr addr;
1915 if (inet_pton(AF_INET6, elems[0], &addr) == 1) {
1916 prefix = g_try_new0(GDHCPIAPrefix, 1);
1921 memcpy(&prefix->prefix, &addr,
1922 sizeof(struct in6_addr));
1923 prefix->prefixlen = value;
1925 list = g_slist_prepend(list, prefix);
1935 static GDHCPIAPrefix *copy_prefix(gpointer data)
1937 GDHCPIAPrefix *copy, *prefix = data;
1939 copy = g_try_new(GDHCPIAPrefix, 1);
1943 memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
1948 static GSList *copy_and_convert_prefixes(GList *prefixes)
1950 GSList *copy = NULL;
1953 for (list = prefixes; list; list = list->next)
1954 copy = g_slist_prepend(copy, copy_prefix(list->data));
1959 static int set_prefixes(GDHCPClient *dhcp_client, struct connman_dhcpv6 *dhcp)
1962 g_slist_free_full(dhcp->prefixes, free_prefix);
1965 copy_and_convert_prefixes(g_dhcp_client_get_option(dhcp_client,
1968 DBG("Got %d prefix", g_slist_length(dhcp->prefixes));
1970 if (dhcp->callback) {
1971 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
1972 if (status == G_DHCPV6_ERROR_NO_PREFIX)
1973 dhcp->callback(dhcp->network,
1974 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1976 struct connman_service *service;
1977 struct connman_ipconfig *ipconfig;
1978 int ifindex = connman_network_get_index(dhcp->network);
1980 service = __connman_service_lookup_from_index(ifindex);
1982 ipconfig = __connman_service_get_ip6config(
1984 save_prefixes(ipconfig, dhcp->prefixes);
1985 __connman_service_save(service);
1988 dhcp->callback(dhcp->network,
1989 CONNMAN_DHCPV6_STATUS_SUCCEED, dhcp->prefixes);
1992 g_slist_free_full(dhcp->prefixes, free_prefix);
1993 dhcp->prefixes = NULL;
1999 static void re_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2001 struct connman_dhcpv6 *dhcp = user_data;
2005 status = g_dhcpv6_client_get_status(dhcp_client);
2007 DBG("dhcpv6 cb msg %p status %d", dhcp, status);
2009 if (status == G_DHCPV6_ERROR_BINDING) {
2010 /* RFC 3315, 18.1.8 */
2011 dhcpv6_pd_request(dhcp);
2013 ret = set_prefixes(dhcp_client, dhcp);
2016 dhcp->callback(dhcp->network,
2017 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
2023 static void rebind_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2027 g_dhcpv6_client_clear_retransmit(dhcp_client);
2029 re_pd_cb(dhcp_client, user_data);
2032 static GDHCPClient *create_pd_client(struct connman_dhcpv6 *dhcp, int *err)
2034 GDHCPClient *dhcp_client;
2035 GDHCPClientError error;
2036 struct connman_service *service;
2040 index = connman_network_get_index(dhcp->network);
2042 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
2043 if (error != G_DHCP_CLIENT_ERROR_NONE) {
2048 if (getenv("CONNMAN_DHCPV6_DEBUG"))
2049 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6:PD");
2051 service = connman_service_lookup_from_network(dhcp->network);
2053 g_dhcp_client_unref(dhcp_client);
2058 ret = set_duid(service, dhcp->network, dhcp_client, index);
2060 g_dhcp_client_unref(dhcp_client);
2065 g_dhcpv6_client_create_iaid(dhcp_client, index, (unsigned char *)&iaid);
2066 g_dhcpv6_client_set_iaid(dhcp_client, iaid);
2071 static int dhcpv6_pd_rebind(struct connman_dhcpv6 *dhcp)
2073 GDHCPClient *dhcp_client;
2076 DBG("dhcp %p", dhcp);
2078 if (!dhcp->dhcp_client) {
2080 * We skipped the solicitation phase
2084 dhcp->dhcp_client = create_pd_client(dhcp, &err);
2085 if (!dhcp->dhcp_client) {
2091 dhcp_client = dhcp->dhcp_client;
2093 g_dhcp_client_clear_requests(dhcp_client);
2095 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2096 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
2097 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
2099 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2100 g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2102 clear_callbacks(dhcp_client);
2104 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
2105 rebind_pd_cb, dhcp);
2107 return g_dhcp_client_start(dhcp_client, NULL);
2110 static void renew_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2114 g_dhcpv6_client_clear_retransmit(dhcp_client);
2116 re_pd_cb(dhcp_client, user_data);
2119 static int dhcpv6_pd_renew(struct connman_dhcpv6 *dhcp)
2121 GDHCPClient *dhcp_client;
2124 DBG("dhcp %p", dhcp);
2126 dhcp_client = dhcp->dhcp_client;
2128 g_dhcp_client_clear_requests(dhcp_client);
2130 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2131 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
2132 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
2133 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
2135 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2136 g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2138 clear_callbacks(dhcp_client);
2140 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
2143 return g_dhcp_client_start(dhcp_client, NULL);
2147 * Check if we need to restart the solicitation procedure. This
2148 * is done if all the prefixes have expired.
2150 static int check_pd_restart(struct connman_dhcpv6 *dhcp)
2152 time_t current, expired;
2154 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
2156 current = time(NULL);
2158 if (current > expired) {
2159 DBG("expired by %d secs", (int)(current - expired));
2161 g_idle_add(dhcpv6_restart, dhcp);
2169 static gboolean timeout_pd_rebind(gpointer user_data)
2171 struct connman_dhcpv6 *dhcp = user_data;
2173 if (check_pd_restart(dhcp) < 0)
2176 dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
2178 DBG("rebind RT timeout %d msec", dhcp->RT);
2180 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_rebind, dhcp);
2182 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2184 g_dhcp_client_start(dhcp->dhcp_client, NULL);
2189 static gboolean start_pd_rebind(gpointer user_data)
2191 struct connman_dhcpv6 *dhcp = user_data;
2193 if (check_pd_restart(dhcp) < 0)
2196 dhcp->RT = initial_rt(REB_TIMEOUT);
2198 DBG("rebind initial RT timeout %d msec", dhcp->RT);
2200 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_rebind, dhcp);
2202 dhcpv6_pd_rebind(dhcp);
2207 static gboolean timeout_pd_rebind_confirm(gpointer user_data)
2209 struct connman_dhcpv6 *dhcp = user_data;
2211 dhcp->RT = calc_delay(dhcp->RT, CNF_MAX_RT);
2213 DBG("rebind with confirm RT timeout %d msec", dhcp->RT);
2215 dhcp->timeout = g_timeout_add(dhcp->RT,
2216 timeout_pd_rebind_confirm, dhcp);
2218 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2220 g_dhcp_client_start(dhcp->dhcp_client, NULL);
2225 static gboolean timeout_pd_max_confirm(gpointer user_data)
2227 struct connman_dhcpv6 *dhcp = user_data;
2233 DBG("rebind with confirm max retransmit duration timeout");
2235 g_dhcpv6_client_clear_retransmit(dhcp->dhcp_client);
2238 dhcp->callback(dhcp->network,
2239 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
2244 static gboolean start_pd_rebind_with_confirm(gpointer user_data)
2246 struct connman_dhcpv6 *dhcp = user_data;
2248 dhcp->RT = initial_rt(CNF_TIMEOUT);
2250 DBG("rebind with confirm initial RT timeout %d msec", dhcp->RT);
2252 dhcp->timeout = g_timeout_add(dhcp->RT,
2253 timeout_pd_rebind_confirm, dhcp);
2254 dhcp->MRD = g_timeout_add(CNF_MAX_RD, timeout_pd_max_confirm, dhcp);
2256 dhcpv6_pd_rebind(dhcp);
2261 static gboolean timeout_pd_renew(gpointer user_data)
2263 struct connman_dhcpv6 *dhcp = user_data;
2265 if (check_pd_restart(dhcp) < 0)
2268 dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
2270 DBG("renew RT timeout %d msec", dhcp->RT);
2272 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
2274 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2276 g_dhcp_client_start(dhcp->dhcp_client, NULL);
2281 static gboolean start_pd_renew(gpointer user_data)
2283 struct connman_dhcpv6 *dhcp = user_data;
2285 dhcp->RT = initial_rt(REN_TIMEOUT);
2287 DBG("renew initial RT timeout %d msec", dhcp->RT);
2289 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_renew, dhcp);
2291 dhcpv6_pd_renew(dhcp);
2296 int __connman_dhcpv6_start_pd_renew(struct connman_network *network,
2299 struct connman_dhcpv6 *dhcp;
2301 time_t started, current, expired;
2303 dhcp = g_hash_table_lookup(network_pd_table, network);
2307 DBG("network %p dhcp %p", network, dhcp);
2311 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
2312 &started, &expired);
2314 current = time(NULL);
2316 DBG("T1 %u T2 %u expires %lu current %lu started %lu", T1, T2,
2317 expired, current, started);
2319 if (T1 == 0xffffffff)
2320 /* RFC 3633, ch 9 */
2325 * Client can choose the timeout.
2329 dhcp->callback = callback;
2331 /* RFC 3315, 18.1.4, start solicit if expired */
2332 if (check_pd_restart(dhcp) < 0)
2335 if (T2 != 0xffffffff && T2 > 0) {
2336 if ((unsigned)current >= (unsigned)started + T2) {
2337 /* RFC 3315, chapter 18.1.3, start rebind */
2338 DBG("rebind after %d secs", T2);
2340 dhcp->timeout = g_timeout_add_seconds(T2,
2344 } else if ((unsigned)current < (unsigned)started + T1) {
2345 DBG("renew after %d secs", T1);
2347 dhcp->timeout = g_timeout_add_seconds(T1,
2351 DBG("rebind after %d secs", T2 - T1);
2353 dhcp->timeout = g_timeout_add_seconds(T2 - T1,
2362 static void release_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2367 int __connman_dhcpv6_start_pd_release(struct connman_network *network,
2370 struct connman_dhcpv6 *dhcp;
2371 GDHCPClient *dhcp_client;
2375 return 0; /* we are already released */
2377 dhcp = g_hash_table_lookup(network_pd_table, network);
2381 DBG("network %p dhcp %p client %p", network, dhcp, dhcp->dhcp_client);
2385 dhcp_client = dhcp->dhcp_client;
2387 DBG("DHCPv6 PD was not started");
2391 g_dhcp_client_clear_requests(dhcp_client);
2392 g_dhcp_client_clear_values(dhcp_client);
2394 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2395 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
2397 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2398 g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2400 clear_callbacks(dhcp_client);
2402 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RELEASE,
2403 release_pd_cb, dhcp);
2405 dhcp->dhcp_client = dhcp_client;
2407 return g_dhcp_client_start(dhcp_client, NULL);
2410 static gboolean timeout_pd_request(gpointer user_data)
2412 struct connman_dhcpv6 *dhcp = user_data;
2414 if (dhcp->request_count >= REQ_MAX_RC) {
2415 DBG("max request retry attempts %d", dhcp->request_count);
2416 dhcp->request_count = 0;
2418 dhcp->callback(dhcp->network,
2419 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
2423 dhcp->request_count++;
2425 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
2426 DBG("request RT timeout %d msec", dhcp->RT);
2427 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_request, dhcp);
2429 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2431 g_dhcp_client_start(dhcp->dhcp_client, NULL);
2436 static void request_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2438 struct connman_dhcpv6 *dhcp = user_data;
2443 g_dhcpv6_client_clear_retransmit(dhcp_client);
2445 status = g_dhcpv6_client_get_status(dhcp_client);
2447 DBG("dhcpv6 pd cb msg %p status %d", dhcp, status);
2449 if (status == G_DHCPV6_ERROR_BINDING) {
2450 /* RFC 3315, 18.1.8 */
2451 dhcpv6_pd_request(dhcp);
2453 set_prefixes(dhcp_client, dhcp);
2457 static int dhcpv6_pd_request(struct connman_dhcpv6 *dhcp)
2459 GDHCPClient *dhcp_client;
2460 uint32_t T1 = 0, T2 = 0;
2462 DBG("dhcp %p", dhcp);
2464 dhcp_client = dhcp->dhcp_client;
2466 g_dhcp_client_clear_requests(dhcp_client);
2468 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2469 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
2470 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
2471 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
2473 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2474 g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2476 clear_callbacks(dhcp_client);
2478 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
2479 request_pd_cb, dhcp);
2481 return g_dhcp_client_start(dhcp_client, NULL);
2484 static void advertise_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2486 struct connman_dhcpv6 *dhcp = user_data;
2488 DBG("dhcpv6 advertise pd msg %p", dhcp);
2492 g_dhcpv6_client_clear_retransmit(dhcp_client);
2494 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
2496 dhcp->callback(dhcp->network,
2497 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
2501 dhcp->RT = initial_rt(REQ_TIMEOUT);
2502 DBG("request initial RT timeout %d msec", dhcp->RT);
2503 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_request, dhcp);
2505 dhcp->request_count = 1;
2507 dhcpv6_pd_request(dhcp);
2510 static void solicitation_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2513 * This callback is here so that g_dhcp_client_start()
2514 * will enter the proper L3 mode.
2516 DBG("DHCPv6 %p solicitation msg received, ignoring it", user_data);
2519 static int dhcpv6_pd_solicitation(struct connman_dhcpv6 *dhcp)
2521 GDHCPClient *dhcp_client;
2524 DBG("dhcp %p", dhcp);
2526 dhcp_client = create_pd_client(dhcp, &ret);
2532 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2534 g_dhcpv6_client_set_pd(dhcp_client, NULL, NULL, NULL);
2536 clear_callbacks(dhcp_client);
2538 g_dhcp_client_register_event(dhcp_client,
2539 G_DHCP_CLIENT_EVENT_ADVERTISE,
2540 advertise_pd_cb, dhcp);
2542 g_dhcp_client_register_event(dhcp_client,
2543 G_DHCP_CLIENT_EVENT_SOLICITATION,
2544 solicitation_pd_cb, dhcp);
2546 dhcp->dhcp_client = dhcp_client;
2548 return g_dhcp_client_start(dhcp_client, NULL);
2551 static gboolean start_pd_solicitation(gpointer user_data)
2553 struct connman_dhcpv6 *dhcp = user_data;
2555 /* Set the retransmission timeout, RFC 3315 chapter 14 */
2556 dhcp->RT = initial_rt(SOL_TIMEOUT);
2558 DBG("solicit initial RT timeout %d msec", dhcp->RT);
2560 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
2562 dhcpv6_pd_solicitation(dhcp);
2567 int __connman_dhcpv6_start_pd(int index, GSList *prefixes, dhcpv6_cb callback)
2569 struct connman_service *service;
2570 struct connman_network *network;
2571 struct connman_dhcpv6 *dhcp;
2576 DBG("index %d", index);
2578 service = __connman_service_lookup_from_index(index);
2582 network = __connman_service_get_network(service);
2586 if (network_pd_table) {
2587 dhcp = g_hash_table_lookup(network_pd_table, network);
2588 if (dhcp && dhcp->started)
2592 dhcp = g_try_new0(struct connman_dhcpv6, 1);
2596 dhcp->network = network;
2597 dhcp->callback = callback;
2598 dhcp->started = true;
2602 * Try to load the earlier prefixes if caller did not supply
2603 * any that we could use.
2605 struct connman_ipconfig *ipconfig;
2606 ipconfig = __connman_service_get_ip6config(service);
2608 dhcp->prefixes = load_prefixes(ipconfig);
2610 dhcp->prefixes = prefixes;
2612 connman_network_ref(network);
2614 DBG("replace network %p dhcp %p", network, dhcp);
2616 g_hash_table_replace(network_pd_table, network, dhcp);
2618 if (!dhcp->prefixes) {
2620 * Refresh start, try to get prefixes.
2622 start_pd_solicitation(dhcp);
2625 * We used to have prefixes, try to use them again.
2626 * We need to use timeouts from confirm msg, RFC 3633, ch 12.1
2628 start_pd_rebind_with_confirm(dhcp);
2634 void __connman_dhcpv6_stop_pd(int index)
2636 struct connman_service *service;
2637 struct connman_network *network;
2642 DBG("index %d", index);
2644 if (!network_pd_table)
2647 service = __connman_service_lookup_from_index(index);
2651 network = __connman_service_get_network(service);
2655 __connman_dhcpv6_start_pd_release(network, NULL);
2657 if (g_hash_table_remove(network_pd_table, network))
2658 connman_network_unref(network);
2661 int __connman_dhcpv6_init(void)
2665 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2666 NULL, remove_network);
2668 network_pd_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2669 NULL, remove_network);
2674 void __connman_dhcpv6_cleanup(void)
2678 g_hash_table_destroy(network_table);
2679 network_table = NULL;
2681 g_hash_table_destroy(network_pd_table);
2682 network_pd_table = NULL;