5 * Copyright (C) 2011 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
30 #include <connman/ipconfig.h>
31 #include <connman/storage.h>
33 #include <gdhcp/gdhcp.h>
39 /* Transmission params in msec, RFC 3315 chapter 5.5 */
40 #define INF_MAX_DELAY (1 * 1000)
41 #define INF_TIMEOUT (1 * 1000)
42 #define INF_MAX_RT (120 * 1000)
43 #define SOL_MAX_DELAY (1 * 1000)
44 #define SOL_TIMEOUT (1 * 1000)
45 #define SOL_MAX_RT (120 * 1000)
46 #define REQ_TIMEOUT (1 * 1000)
47 #define REQ_MAX_RT (30 * 1000)
49 #define REN_TIMEOUT (10 * 1000)
50 #define REN_MAX_RT (600 * 1000)
51 #define REB_TIMEOUT (10 * 1000)
52 #define REB_MAX_RT (600 * 1000)
55 struct connman_dhcpv6 {
56 struct connman_network *network;
62 GDHCPClient *dhcp_client;
64 guint timeout; /* operation timeout in msec */
65 guint RT; /* in msec */
66 gboolean use_ta; /* set to TRUE if IPv6 privacy is enabled */
67 GSList *prefixes; /* network prefixes from radvd */
68 int request_count; /* how many times REQUEST have been sent */
69 gboolean stateless; /* TRUE if stateless DHCPv6 is used */
72 static GHashTable *network_table;
74 static int dhcpv6_request(struct connman_dhcpv6 *dhcp, gboolean add_addresses);
76 static inline float get_random()
78 return (rand() % 200 - 100) / 1000.0;
81 /* Calculate a random delay, RFC 3315 chapter 14 */
82 /* RT and MRT are milliseconds */
83 static guint calc_delay(guint RT, guint MRT)
85 float delay = get_random();
86 float rt = RT * (2 + delay);
89 rt = MRT * (1 + delay);
97 static void free_prefix(gpointer data, gpointer user_data)
102 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
104 g_strfreev(dhcp->nameservers);
105 g_strfreev(dhcp->timeservers);
107 dhcp->nameservers = NULL;
108 dhcp->timeservers = NULL;
110 g_slist_foreach(dhcp->prefixes, free_prefix, NULL);
111 g_slist_free(dhcp->prefixes);
114 static gboolean compare_string_arrays(char **array_a, char **array_b)
118 if (array_a == NULL || array_b == NULL)
121 if (g_strv_length(array_a) != g_strv_length(array_b))
124 for (i = 0; array_a[i] != NULL && array_b[i] != NULL; i++)
125 if (g_strcmp0(array_a[i], array_b[i]) != 0)
131 static void dhcpv6_debug(const char *str, void *data)
133 connman_info("%s: %s\n", (const char *) data, str);
136 static gchar *convert_to_hex(unsigned char *buf, int len)
138 gchar *ret = g_try_malloc(len * 2 + 1);
141 for (i = 0; ret != NULL && i < len; i++)
142 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
148 * DUID should not change over time so save it to file.
149 * See RFC 3315 chapter 9 for details.
151 static int set_duid(struct connman_service *service,
152 struct connman_network *network,
153 GDHCPClient *dhcp_client, int index)
161 ident = __connman_service_get_ident(service);
163 keyfile = connman_storage_load_service(ident);
167 hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
169 if (hex_duid != NULL) {
170 unsigned int i, j = 0, hex;
171 size_t hex_duid_len = strlen(hex_duid);
173 duid = g_try_malloc0(hex_duid_len / 2);
175 g_key_file_free(keyfile);
180 for (i = 0; i < hex_duid_len; i += 2) {
181 sscanf(hex_duid + i, "%02x", &hex);
185 duid_len = hex_duid_len / 2;
188 int type = __connman_ipconfig_get_type_from_index(index);
190 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
193 g_key_file_free(keyfile);
197 hex_duid = convert_to_hex(duid, duid_len);
198 if (hex_duid == NULL) {
199 g_key_file_free(keyfile);
203 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
206 __connman_storage_save_service(keyfile, ident);
210 g_key_file_free(keyfile);
212 g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
217 static void clear_callbacks(GDHCPClient *dhcp_client)
219 g_dhcp_client_register_event(dhcp_client,
220 G_DHCP_CLIENT_EVENT_SOLICITATION,
223 g_dhcp_client_register_event(dhcp_client,
224 G_DHCP_CLIENT_EVENT_ADVERTISE,
227 g_dhcp_client_register_event(dhcp_client,
228 G_DHCP_CLIENT_EVENT_REQUEST,
231 g_dhcp_client_register_event(dhcp_client,
232 G_DHCP_CLIENT_EVENT_RENEW,
235 g_dhcp_client_register_event(dhcp_client,
236 G_DHCP_CLIENT_EVENT_REBIND,
239 g_dhcp_client_register_event(dhcp_client,
240 G_DHCP_CLIENT_EVENT_RELEASE,
243 g_dhcp_client_register_event(dhcp_client,
244 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
248 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
250 struct connman_dhcpv6 *dhcp = user_data;
251 struct connman_service *service;
253 GList *option, *list;
254 char **nameservers, **timeservers;
256 DBG("dhcpv6 information-request %p", dhcp);
258 service = __connman_service_lookup_from_network(dhcp->network);
259 if (service == NULL) {
260 connman_error("Can not lookup service");
264 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
265 entries = g_list_length(option);
267 nameservers = g_try_new0(char *, entries + 1);
268 if (nameservers != NULL) {
269 for (i = 0, list = option; list; list = list->next, i++)
270 nameservers[i] = g_strdup(list->data);
273 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
274 if (dhcp->nameservers != NULL) {
275 for (i = 0; dhcp->nameservers[i] != NULL; i++)
276 __connman_service_nameserver_remove(service,
277 dhcp->nameservers[i],
279 g_strfreev(dhcp->nameservers);
282 dhcp->nameservers = nameservers;
284 for (i = 0; dhcp->nameservers[i] != NULL; i++)
285 __connman_service_nameserver_append(service,
286 dhcp->nameservers[i],
289 g_strfreev(nameservers);
292 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
293 entries = g_list_length(option);
295 timeservers = g_try_new0(char *, entries + 1);
296 if (timeservers != NULL) {
297 for (i = 0, list = option; list; list = list->next, i++)
298 timeservers[i] = g_strdup(list->data);
301 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
302 if (dhcp->timeservers != NULL) {
303 for (i = 0; dhcp->timeservers[i] != NULL; i++)
304 __connman_service_timeserver_remove(service,
305 dhcp->timeservers[i]);
306 g_strfreev(dhcp->timeservers);
309 dhcp->timeservers = timeservers;
311 for (i = 0; dhcp->timeservers[i] != NULL; i++)
312 __connman_service_timeserver_append(service,
313 dhcp->timeservers[i]);
315 g_strfreev(timeservers);
318 if (dhcp->callback != NULL) {
319 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
320 dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
324 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
326 struct connman_service *service;
327 GDHCPClient *dhcp_client;
328 GDHCPClientError error;
331 DBG("dhcp %p", dhcp);
333 index = connman_network_get_index(dhcp->network);
335 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
336 if (error != G_DHCP_CLIENT_ERROR_NONE)
339 if (getenv("CONNMAN_DHCPV6_DEBUG"))
340 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
342 service = __connman_service_lookup_from_network(dhcp->network);
346 ret = set_duid(service, dhcp->network, dhcp_client, index);
350 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
351 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
352 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
354 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
355 G_DHCPV6_SNTP_SERVERS);
357 g_dhcp_client_register_event(dhcp_client,
358 G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
360 dhcp->dhcp_client = dhcp_client;
362 return g_dhcp_client_start(dhcp_client, NULL);
365 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
367 struct in6_addr addr_prefix, addr;
371 for (list = prefixes; list; list = list->next) {
372 char *prefix = list->data;
373 const char *slash = g_strrstr(prefix, "/");
374 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
375 0xF0, 0xE0, 0xC0, 0x80 };
376 int left, count, i, plen;
381 prefix = g_strndup(prefix, slash - prefix);
382 len = strtol(slash + 1, NULL, 10);
389 inet_pton(AF_INET6, prefix, &addr_prefix);
390 inet_pton(AF_INET6, address, &addr);
392 memset(&addr_prefix.s6_addr[i], 0, count);
393 memset(&addr.s6_addr[i], 0, count);
396 addr_prefix.s6_addr[i - 1] &= bits[left];
397 addr.s6_addr[i - 1] &= bits[left];
402 if (memcmp(&addr_prefix, &addr, 16) == 0) {
411 static int set_addresses(GDHCPClient *dhcp_client,
412 struct connman_dhcpv6 *dhcp)
414 struct connman_service *service;
415 struct connman_ipconfig *ipconfig;
417 GList *option, *list;
418 char **nameservers, **timeservers;
419 const char *c_address;
420 char *address = NULL;
422 service = __connman_service_lookup_from_network(dhcp->network);
423 if (service == NULL) {
424 connman_error("Can not lookup service");
428 ipconfig = __connman_service_get_ip6config(service);
429 if (ipconfig == NULL) {
430 connman_error("Could not lookup ip6config");
434 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
435 entries = g_list_length(option);
437 nameservers = g_try_new0(char *, entries + 1);
438 if (nameservers != NULL) {
439 for (i = 0, list = option; list; list = list->next, i++)
440 nameservers[i] = g_strdup(list->data);
443 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
444 if (dhcp->nameservers != NULL) {
445 for (i = 0; dhcp->nameservers[i] != NULL; i++)
446 __connman_service_nameserver_remove(service,
447 dhcp->nameservers[i],
449 g_strfreev(dhcp->nameservers);
452 dhcp->nameservers = nameservers;
454 for (i = 0; dhcp->nameservers[i] != NULL; i++)
455 __connman_service_nameserver_append(service,
456 dhcp->nameservers[i],
459 g_strfreev(nameservers);
462 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
463 entries = g_list_length(option);
465 timeservers = g_try_new0(char *, entries + 1);
466 if (timeservers != NULL) {
467 for (i = 0, list = option; list; list = list->next, i++)
468 timeservers[i] = g_strdup(list->data);
471 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
472 if (dhcp->timeservers != NULL) {
473 for (i = 0; dhcp->timeservers[i] != NULL; i++)
474 __connman_service_timeserver_remove(service,
475 dhcp->timeservers[i]);
476 g_strfreev(dhcp->timeservers);
479 dhcp->timeservers = timeservers;
481 for (i = 0; dhcp->timeservers[i] != NULL; i++)
482 __connman_service_timeserver_append(service,
483 dhcp->timeservers[i]);
485 g_strfreev(timeservers);
488 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
490 address = g_strdup(option->data);
492 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
494 address = g_strdup(option->data);
497 c_address = __connman_ipconfig_get_local(ipconfig);
499 if (address != NULL &&
500 ((c_address != NULL &&
501 g_strcmp0(address, c_address) != 0) ||
502 (c_address == NULL))) {
505 /* Is this prefix part of the subnet we are suppose to use? */
506 prefix_len = check_ipv6_addr_prefix(dhcp->prefixes, address);
508 __connman_ipconfig_set_local(ipconfig, address);
509 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
511 DBG("new address %s/%d", address, prefix_len);
517 static void re_cb(GDHCPClient *dhcp_client, gpointer user_data)
519 struct connman_dhcpv6 *dhcp = user_data;
523 ret = set_addresses(dhcp_client, dhcp);
525 status = g_dhcpv6_client_get_status(dhcp_client);
527 DBG("dhcpv6 cb msg %p ret %d status %d", dhcp, ret, status);
530 if (dhcp->callback != NULL)
531 dhcp->callback(dhcp->network, FALSE);
535 if (status == G_DHCPV6_ERROR_BINDING) {
536 /* RFC 3315, 18.1.8 */
537 dhcpv6_request(dhcp, FALSE);
539 if (dhcp->callback != NULL)
540 dhcp->callback(dhcp->network,
541 status == 0 ? TRUE : FALSE);
545 static void rebind_cb(GDHCPClient *dhcp_client, gpointer user_data)
549 g_dhcpv6_client_reset_rebind(dhcp_client);
550 g_dhcpv6_client_reset_renew(dhcp_client);
552 re_cb(dhcp_client, user_data);
555 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp)
557 GDHCPClient *dhcp_client;
559 DBG("dhcp %p", dhcp);
561 dhcp_client = dhcp->dhcp_client;
563 g_dhcp_client_clear_requests(dhcp_client);
565 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
566 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
567 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
569 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
570 G_DHCPV6_SNTP_SERVERS);
572 g_dhcpv6_client_set_ia(dhcp_client,
573 connman_network_get_index(dhcp->network),
574 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
577 clear_callbacks(dhcp_client);
579 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
582 dhcp->dhcp_client = dhcp_client;
584 return g_dhcp_client_start(dhcp_client, NULL);
587 static gboolean dhcpv6_restart(gpointer user_data)
589 struct connman_dhcpv6 *dhcp = user_data;
591 if (dhcp->callback != NULL)
592 dhcp->callback(dhcp->network, FALSE);
598 * Check if we need to restart the solicitation procedure. This
599 * is done if all the addresses have expired. RFC 3315, 18.1.4
601 static int check_restart(struct connman_dhcpv6 *dhcp)
603 time_t current, expired;
605 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
606 NULL, NULL, &expired);
609 if (current > expired) {
610 DBG("expired by %d secs", (int)(current - expired));
612 g_timeout_add(0, dhcpv6_restart, dhcp);
620 static gboolean timeout_rebind(gpointer user_data)
622 struct connman_dhcpv6 *dhcp = user_data;
624 if (check_restart(dhcp) < 0)
627 dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
629 DBG("rebind RT timeout %d msec", dhcp->RT);
631 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
633 g_dhcp_client_start(dhcp->dhcp_client, NULL);
638 static gboolean start_rebind(gpointer user_data)
640 struct connman_dhcpv6 *dhcp = user_data;
642 dhcp->RT = REB_TIMEOUT * (1 + get_random());
644 DBG("rebind initial RT timeout %d msec", dhcp->RT);
646 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
653 static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
657 re_cb(dhcp_client, user_data);
660 static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
661 gboolean add_addresses)
663 GDHCPClient *dhcp_client;
666 DBG("dhcp %p add %d", dhcp, add_addresses);
668 dhcp_client = dhcp->dhcp_client;
670 g_dhcp_client_clear_requests(dhcp_client);
672 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
673 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
674 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
675 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
677 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
678 G_DHCPV6_SNTP_SERVERS);
680 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
681 g_dhcpv6_client_set_ia(dhcp_client,
682 connman_network_get_index(dhcp->network),
683 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
684 &T1, &T2, add_addresses);
686 clear_callbacks(dhcp_client);
688 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
691 dhcp->dhcp_client = dhcp_client;
693 return g_dhcp_client_start(dhcp_client, NULL);
696 static gboolean timeout_request(gpointer user_data)
698 struct connman_dhcpv6 *dhcp = user_data;
700 if (dhcp->request_count >= REQ_MAX_RC) {
701 DBG("max request retry attempts %d", dhcp->request_count);
702 dhcp->request_count = 0;
703 if (dhcp->callback != NULL)
704 dhcp->callback(dhcp->network, FALSE);
708 dhcp->request_count++;
710 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
711 DBG("request RT timeout %d msec", dhcp->RT);
712 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
714 g_dhcp_client_start(dhcp->dhcp_client, NULL);
719 static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
723 g_dhcpv6_client_reset_renew(dhcp_client);
725 re_cb(dhcp_client, user_data);
728 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
730 GDHCPClient *dhcp_client;
733 DBG("dhcp %p", dhcp);
735 dhcp_client = dhcp->dhcp_client;
737 g_dhcp_client_clear_requests(dhcp_client);
739 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
740 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
741 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
742 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
744 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
745 G_DHCPV6_SNTP_SERVERS);
747 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
748 g_dhcpv6_client_set_ia(dhcp_client,
749 connman_network_get_index(dhcp->network),
750 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
753 clear_callbacks(dhcp_client);
755 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
758 dhcp->dhcp_client = dhcp_client;
760 return g_dhcp_client_start(dhcp_client, NULL);
763 static gboolean timeout_renew(gpointer user_data)
765 struct connman_dhcpv6 *dhcp = user_data;
767 if (check_restart(dhcp) < 0)
770 dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
772 DBG("renew RT timeout %d msec", dhcp->RT);
774 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
776 g_dhcp_client_start(dhcp->dhcp_client, NULL);
781 static gboolean start_renew(gpointer user_data)
783 struct connman_dhcpv6 *dhcp = user_data;
785 dhcp->RT = REN_TIMEOUT * (1 + get_random());
787 DBG("renew initial RT timeout %d msec", dhcp->RT);
789 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
796 int __connman_dhcpv6_start_renew(struct connman_network *network,
799 struct connman_dhcpv6 *dhcp;
801 time_t last_renew, last_rebind, current, expired;
803 dhcp = g_hash_table_lookup(network_table, network);
807 if (dhcp->timeout > 0) {
808 g_source_remove(dhcp->timeout);
812 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
813 &last_renew, &last_rebind, &expired);
817 DBG("T1 %u T2 %u expires %lu current %lu", T1, T2,
818 (unsigned long)expired, current);
820 if (T1 == 0xffffffff)
826 * Client can choose the timeout.
830 /* RFC 3315, 18.1.4, start solicit if expired */
831 if (current > expired) {
832 DBG("expired by %d secs", (int)(current - expired));
836 dhcp->callback = callback;
838 if (T2 != 0xffffffff && T2 > 0 &&
839 (unsigned)current > (unsigned)last_rebind + T2) {
842 /* RFC 3315, chapter 18.1.3, start rebind */
843 if ((unsigned)current > (unsigned)last_renew + T1)
846 timeout = last_renew - current + T1;
849 * If we just did a renew, do not restart the rebind
852 dhcp->timeout = g_timeout_add_seconds(timeout, start_rebind,
855 DBG("renew after %d secs", T1);
857 dhcp->timeout = g_timeout_add_seconds(T1, start_renew, dhcp);
862 static void release_cb(GDHCPClient *dhcp_client, gpointer user_data)
864 struct connman_dhcpv6 *dhcp = user_data;
866 DBG("dhcpv6 release msg %p", dhcp);
868 if (dhcp->callback != NULL) {
869 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
870 dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
874 int __connman_dhcpv6_start_release(struct connman_network *network,
877 struct connman_dhcpv6 *dhcp;
878 GDHCPClient *dhcp_client;
880 if (network_table == NULL)
881 return 0; /* we are already released */
883 dhcp = g_hash_table_lookup(network_table, network);
887 DBG("dhcp %p stateless %d", dhcp, dhcp->stateless);
889 if (dhcp->stateless == TRUE)
892 if (dhcp->timeout > 0) {
893 g_source_remove(dhcp->timeout);
897 dhcp_client = dhcp->dhcp_client;
899 g_dhcp_client_clear_requests(dhcp_client);
900 g_dhcp_client_clear_values(dhcp_client);
902 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
903 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
905 g_dhcpv6_client_set_ia(dhcp_client,
906 connman_network_get_index(dhcp->network),
907 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
910 clear_callbacks(dhcp_client);
912 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RELEASE,
915 dhcp->dhcp_client = dhcp_client;
917 return g_dhcp_client_start(dhcp_client, NULL);
920 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
922 DBG("dhcp %p", dhcp);
924 if (dhcp->timeout > 0) {
925 g_source_remove(dhcp->timeout);
931 if (dhcp->dhcp_client == NULL)
934 g_dhcp_client_stop(dhcp->dhcp_client);
935 g_dhcp_client_unref(dhcp->dhcp_client);
937 dhcp->dhcp_client = NULL;
942 static void remove_network(gpointer user_data)
944 struct connman_dhcpv6 *dhcp = user_data;
946 DBG("dhcp %p", dhcp);
948 dhcpv6_release(dhcp);
953 static gboolean timeout_info_req(gpointer user_data)
955 struct connman_dhcpv6 *dhcp = user_data;
957 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
959 DBG("info RT timeout %d msec", dhcp->RT);
961 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
963 g_dhcp_client_start(dhcp->dhcp_client, NULL);
968 static gboolean start_info_req(gpointer user_data)
970 struct connman_dhcpv6 *dhcp = user_data;
972 /* Set the retransmission timeout, RFC 3315 chapter 14 */
973 dhcp->RT = INF_TIMEOUT * (1 + get_random());
975 DBG("info initial RT timeout %d msec", dhcp->RT);
977 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
979 dhcpv6_info_request(dhcp);
984 int __connman_dhcpv6_start_info(struct connman_network *network,
987 struct connman_dhcpv6 *dhcp;
992 dhcp = g_try_new0(struct connman_dhcpv6, 1);
996 dhcp->network = network;
997 dhcp->callback = callback;
998 dhcp->stateless = TRUE;
1000 connman_network_ref(network);
1002 g_hash_table_replace(network_table, network, dhcp);
1004 /* Initial timeout, RFC 3315, 18.1.5 */
1005 delay = rand() % 1000;
1007 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
1012 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
1014 struct connman_dhcpv6 *dhcp = user_data;
1016 DBG("dhcpv6 advertise msg %p", dhcp);
1018 if (dhcp->timeout > 0) {
1019 g_source_remove(dhcp->timeout);
1023 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
1024 if (dhcp->callback != NULL)
1025 dhcp->callback(dhcp->network, FALSE);
1029 dhcp->RT = REQ_TIMEOUT * (1 + get_random());
1030 DBG("request initial RT timeout %d msec", dhcp->RT);
1031 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
1033 dhcp->request_count = 1;
1035 dhcpv6_request(dhcp, TRUE);
1038 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
1040 /* We get here if server supports rapid commit */
1041 struct connman_dhcpv6 *dhcp = user_data;
1043 DBG("dhcpv6 solicitation msg %p", dhcp);
1045 if (dhcp->timeout > 0) {
1046 g_source_remove(dhcp->timeout);
1050 set_addresses(dhcp_client, dhcp);
1053 static gboolean timeout_solicitation(gpointer user_data)
1055 struct connman_dhcpv6 *dhcp = user_data;
1057 dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
1059 DBG("solicit RT timeout %d msec", dhcp->RT);
1061 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1063 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1068 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
1070 struct connman_service *service;
1071 struct connman_ipconfig *ipconfig_ipv6;
1072 GDHCPClient *dhcp_client;
1073 GDHCPClientError error;
1076 DBG("dhcp %p", dhcp);
1078 index = connman_network_get_index(dhcp->network);
1080 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
1081 if (error != G_DHCP_CLIENT_ERROR_NONE)
1084 if (getenv("CONNMAN_DHCPV6_DEBUG"))
1085 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
1087 service = __connman_service_lookup_from_network(dhcp->network);
1088 if (service == NULL)
1091 ret = set_duid(service, dhcp->network, dhcp_client, index);
1095 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1096 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
1097 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1098 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1100 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
1101 G_DHCPV6_SNTP_SERVERS);
1103 ipconfig_ipv6 = __connman_service_get_ip6config(service);
1104 dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
1106 g_dhcpv6_client_set_ia(dhcp_client, index,
1107 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1110 clear_callbacks(dhcp_client);
1112 g_dhcp_client_register_event(dhcp_client,
1113 G_DHCP_CLIENT_EVENT_SOLICITATION,
1114 solicitation_cb, dhcp);
1116 g_dhcp_client_register_event(dhcp_client,
1117 G_DHCP_CLIENT_EVENT_ADVERTISE,
1118 advertise_cb, dhcp);
1120 dhcp->dhcp_client = dhcp_client;
1122 return g_dhcp_client_start(dhcp_client, NULL);
1125 static gboolean start_solicitation(gpointer user_data)
1127 struct connman_dhcpv6 *dhcp = user_data;
1129 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1130 dhcp->RT = SOL_TIMEOUT * (1 + get_random());
1132 DBG("solicit initial RT timeout %d msec", dhcp->RT);
1134 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1136 dhcpv6_solicitation(dhcp);
1141 int __connman_dhcpv6_start(struct connman_network *network,
1142 GSList *prefixes, dhcp_cb callback)
1144 struct connman_dhcpv6 *dhcp;
1149 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1153 dhcp->network = network;
1154 dhcp->callback = callback;
1155 dhcp->prefixes = prefixes;
1157 connman_network_ref(network);
1159 g_hash_table_replace(network_table, network, dhcp);
1161 /* Initial timeout, RFC 3315, 17.1.2 */
1162 delay = rand() % 1000;
1164 dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
1169 void __connman_dhcpv6_stop(struct connman_network *network)
1173 if (network_table == NULL)
1176 if (g_hash_table_remove(network_table, network) == TRUE)
1177 connman_network_unref(network);
1180 int __connman_dhcpv6_init(void)
1186 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1187 NULL, remove_network);
1192 void __connman_dhcpv6_cleanup(void)
1196 g_hash_table_destroy(network_table);
1197 network_table = NULL;