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)
53 struct connman_dhcpv6 {
54 struct connman_network *network;
60 GDHCPClient *dhcp_client;
62 guint timeout; /* operation timeout in msec */
63 guint RT; /* in msec */
64 gboolean use_ta; /* set to TRUE if IPv6 privacy is enabled */
65 GSList *prefixes; /* network prefixes from radvd */
66 int request_count; /* how many times REQUEST have been sent */
69 static GHashTable *network_table;
71 static int dhcpv6_request(struct connman_dhcpv6 *dhcp, gboolean add_addresses);
73 static inline float get_random()
75 return (rand() % 200 - 100) / 1000.0;
78 /* Calculate a random delay, RFC 3315 chapter 14 */
79 /* RT and MRT are milliseconds */
80 static guint calc_delay(guint RT, guint MRT)
82 float delay = get_random();
83 float rt = RT * (2 + delay);
86 rt = MRT * (1 + delay);
94 static void free_prefix(gpointer data, gpointer user_data)
99 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
101 g_strfreev(dhcp->nameservers);
102 g_strfreev(dhcp->timeservers);
104 dhcp->nameservers = NULL;
105 dhcp->timeservers = NULL;
107 g_slist_foreach(dhcp->prefixes, free_prefix, NULL);
108 g_slist_free(dhcp->prefixes);
111 static gboolean compare_string_arrays(char **array_a, char **array_b)
115 if (array_a == NULL || array_b == NULL)
118 if (g_strv_length(array_a) != g_strv_length(array_b))
121 for (i = 0; array_a[i] != NULL && array_b[i] != NULL; i++)
122 if (g_strcmp0(array_a[i], array_b[i]) != 0)
128 static void dhcpv6_debug(const char *str, void *data)
130 connman_info("%s: %s\n", (const char *) data, str);
133 static gchar *convert_to_hex(unsigned char *buf, int len)
135 gchar *ret = g_try_malloc(len * 2 + 1);
138 for (i = 0; ret != NULL && i < len; i++)
139 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
145 * DUID should not change over time so save it to file.
146 * See RFC 3315 chapter 9 for details.
148 static int set_duid(struct connman_service *service,
149 struct connman_network *network,
150 GDHCPClient *dhcp_client, int index)
158 ident = __connman_service_get_ident(service);
160 keyfile = connman_storage_load_service(ident);
164 hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
166 if (hex_duid != NULL) {
167 unsigned int i, j = 0, hex;
168 size_t hex_duid_len = strlen(hex_duid);
170 duid = g_try_malloc0(hex_duid_len / 2);
172 g_key_file_free(keyfile);
177 for (i = 0; i < hex_duid_len; i += 2) {
178 sscanf(hex_duid + i, "%02x", &hex);
182 duid_len = hex_duid_len / 2;
185 int type = __connman_ipconfig_get_type_from_index(index);
187 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
190 g_key_file_free(keyfile);
194 hex_duid = convert_to_hex(duid, duid_len);
195 if (hex_duid == NULL) {
196 g_key_file_free(keyfile);
200 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
203 __connman_storage_save_service(keyfile, ident);
207 g_key_file_free(keyfile);
209 g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
214 static void clear_callbacks(GDHCPClient *dhcp_client)
216 g_dhcp_client_register_event(dhcp_client,
217 G_DHCP_CLIENT_EVENT_SOLICITATION,
220 g_dhcp_client_register_event(dhcp_client,
221 G_DHCP_CLIENT_EVENT_ADVERTISE,
224 g_dhcp_client_register_event(dhcp_client,
225 G_DHCP_CLIENT_EVENT_REQUEST,
228 g_dhcp_client_register_event(dhcp_client,
229 G_DHCP_CLIENT_EVENT_RENEW,
232 g_dhcp_client_register_event(dhcp_client,
233 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
237 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
239 struct connman_dhcpv6 *dhcp = user_data;
240 struct connman_service *service;
242 GList *option, *list;
243 char **nameservers, **timeservers;
245 DBG("dhcpv6 information-request %p", dhcp);
247 service = __connman_service_lookup_from_network(dhcp->network);
248 if (service == NULL) {
249 connman_error("Can not lookup service");
253 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
254 entries = g_list_length(option);
256 nameservers = g_try_new0(char *, entries + 1);
257 if (nameservers != NULL) {
258 for (i = 0, list = option; list; list = list->next, i++)
259 nameservers[i] = g_strdup(list->data);
262 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
263 if (dhcp->nameservers != NULL) {
264 for (i = 0; dhcp->nameservers[i] != NULL; i++)
265 __connman_service_nameserver_remove(service,
266 dhcp->nameservers[i],
268 g_strfreev(dhcp->nameservers);
271 dhcp->nameservers = nameservers;
273 for (i = 0; dhcp->nameservers[i] != NULL; i++)
274 __connman_service_nameserver_append(service,
275 dhcp->nameservers[i],
278 g_strfreev(nameservers);
281 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
282 entries = g_list_length(option);
284 timeservers = g_try_new0(char *, entries + 1);
285 if (timeservers != NULL) {
286 for (i = 0, list = option; list; list = list->next, i++)
287 timeservers[i] = g_strdup(list->data);
290 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
291 if (dhcp->timeservers != NULL) {
292 for (i = 0; dhcp->timeservers[i] != NULL; i++)
293 __connman_service_timeserver_remove(service,
294 dhcp->timeservers[i]);
295 g_strfreev(dhcp->timeservers);
298 dhcp->timeservers = timeservers;
300 for (i = 0; dhcp->timeservers[i] != NULL; i++)
301 __connman_service_timeserver_append(service,
302 dhcp->timeservers[i]);
304 g_strfreev(timeservers);
307 if (dhcp->callback != NULL) {
308 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
309 dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
313 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
315 struct connman_service *service;
316 GDHCPClient *dhcp_client;
317 GDHCPClientError error;
320 DBG("dhcp %p", dhcp);
322 index = connman_network_get_index(dhcp->network);
324 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
325 if (error != G_DHCP_CLIENT_ERROR_NONE)
328 if (getenv("CONNMAN_DHCPV6_DEBUG"))
329 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
331 service = __connman_service_lookup_from_network(dhcp->network);
335 ret = set_duid(service, dhcp->network, dhcp_client, index);
339 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
340 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
341 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
343 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
344 G_DHCPV6_SNTP_SERVERS);
346 g_dhcp_client_register_event(dhcp_client,
347 G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
349 dhcp->dhcp_client = dhcp_client;
351 return g_dhcp_client_start(dhcp_client, NULL);
354 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
356 struct in6_addr addr_prefix, addr;
360 for (list = prefixes; list; list = list->next) {
361 char *prefix = list->data;
362 const char *slash = g_strrstr(prefix, "/");
363 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
364 0xF0, 0xE0, 0xC0, 0x80 };
365 int left, count, i, plen;
370 prefix = g_strndup(prefix, slash - prefix);
371 len = strtol(slash + 1, NULL, 10);
378 inet_pton(AF_INET6, prefix, &addr_prefix);
379 inet_pton(AF_INET6, address, &addr);
381 memset(&addr_prefix.s6_addr[i], 0, count);
382 memset(&addr.s6_addr[i], 0, count);
385 addr_prefix.s6_addr[i - 1] &= bits[left];
386 addr.s6_addr[i - 1] &= bits[left];
391 if (memcmp(&addr_prefix, &addr, 16) == 0) {
400 static int set_addresses(GDHCPClient *dhcp_client,
401 struct connman_dhcpv6 *dhcp)
403 struct connman_service *service;
404 struct connman_ipconfig *ipconfig;
406 GList *option, *list;
407 char **nameservers, **timeservers;
408 const char *c_address;
409 char *address = NULL;
411 service = __connman_service_lookup_from_network(dhcp->network);
412 if (service == NULL) {
413 connman_error("Can not lookup service");
417 ipconfig = __connman_service_get_ip6config(service);
418 if (ipconfig == NULL) {
419 connman_error("Could not lookup ip6config");
423 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
424 entries = g_list_length(option);
426 nameservers = g_try_new0(char *, entries + 1);
427 if (nameservers != NULL) {
428 for (i = 0, list = option; list; list = list->next, i++)
429 nameservers[i] = g_strdup(list->data);
432 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
433 if (dhcp->nameservers != NULL) {
434 for (i = 0; dhcp->nameservers[i] != NULL; i++)
435 __connman_service_nameserver_remove(service,
436 dhcp->nameservers[i],
438 g_strfreev(dhcp->nameservers);
441 dhcp->nameservers = nameservers;
443 for (i = 0; dhcp->nameservers[i] != NULL; i++)
444 __connman_service_nameserver_append(service,
445 dhcp->nameservers[i],
448 g_strfreev(nameservers);
451 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
452 entries = g_list_length(option);
454 timeservers = g_try_new0(char *, entries + 1);
455 if (timeservers != NULL) {
456 for (i = 0, list = option; list; list = list->next, i++)
457 timeservers[i] = g_strdup(list->data);
460 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
461 if (dhcp->timeservers != NULL) {
462 for (i = 0; dhcp->timeservers[i] != NULL; i++)
463 __connman_service_timeserver_remove(service,
464 dhcp->timeservers[i]);
465 g_strfreev(dhcp->timeservers);
468 dhcp->timeservers = timeservers;
470 for (i = 0; dhcp->timeservers[i] != NULL; i++)
471 __connman_service_timeserver_append(service,
472 dhcp->timeservers[i]);
474 g_strfreev(timeservers);
477 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
479 address = g_strdup(option->data);
481 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
483 address = g_strdup(option->data);
486 c_address = __connman_ipconfig_get_local(ipconfig);
488 if (address != NULL &&
489 ((c_address != NULL &&
490 g_strcmp0(address, c_address) != 0) ||
491 (c_address == NULL))) {
494 /* Is this prefix part of the subnet we are suppose to use? */
495 prefix_len = check_ipv6_addr_prefix(dhcp->prefixes, address);
497 __connman_ipconfig_set_local(ipconfig, address);
498 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
500 DBG("new address %s/%d", address, prefix_len);
506 static void re_cb(GDHCPClient *dhcp_client, gpointer user_data)
508 struct connman_dhcpv6 *dhcp = user_data;
512 ret = set_addresses(dhcp_client, dhcp);
514 status = g_dhcpv6_client_get_status(dhcp_client);
516 DBG("dhcpv6 cb msg %p ret %d status %d", dhcp, ret, status);
519 if (dhcp->callback != NULL)
520 dhcp->callback(dhcp->network, FALSE);
524 if (status == G_DHCPV6_ERROR_BINDING) {
525 /* RFC 3315, 18.1.8 */
526 dhcpv6_request(dhcp, FALSE);
528 if (dhcp->callback != NULL)
529 dhcp->callback(dhcp->network,
530 status == 0 ? TRUE : FALSE);
534 static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
538 re_cb(dhcp_client, user_data);
541 static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
542 gboolean add_addresses)
544 GDHCPClient *dhcp_client;
547 DBG("dhcp %p add %d", dhcp, add_addresses);
549 dhcp_client = dhcp->dhcp_client;
551 g_dhcp_client_clear_requests(dhcp_client);
553 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
554 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
555 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
556 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
558 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
559 G_DHCPV6_SNTP_SERVERS);
561 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL);
562 g_dhcpv6_client_set_ia(dhcp_client,
563 connman_network_get_index(dhcp->network),
564 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
565 &T1, &T2, add_addresses);
567 clear_callbacks(dhcp_client);
569 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
572 dhcp->dhcp_client = dhcp_client;
574 return g_dhcp_client_start(dhcp_client, NULL);
577 static gboolean timeout_request(gpointer user_data)
579 struct connman_dhcpv6 *dhcp = user_data;
581 if (dhcp->request_count >= REQ_MAX_RC) {
582 DBG("max request retry attempts %d", dhcp->request_count);
583 dhcp->request_count = 0;
584 if (dhcp->callback != NULL)
585 dhcp->callback(dhcp->network, FALSE);
589 dhcp->request_count++;
591 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
592 DBG("request RT timeout %d msec", dhcp->RT);
593 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
595 g_dhcp_client_start(dhcp->dhcp_client, NULL);
600 static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
604 g_dhcpv6_client_reset_renew(dhcp_client);
606 re_cb(dhcp_client, user_data);
609 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
611 GDHCPClient *dhcp_client;
614 DBG("dhcp %p", dhcp);
616 dhcp_client = dhcp->dhcp_client;
618 g_dhcp_client_clear_requests(dhcp_client);
620 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
621 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
622 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
623 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
625 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
626 G_DHCPV6_SNTP_SERVERS);
628 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL);
629 g_dhcpv6_client_set_ia(dhcp_client,
630 connman_network_get_index(dhcp->network),
631 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
634 clear_callbacks(dhcp_client);
636 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
639 dhcp->dhcp_client = dhcp_client;
641 return g_dhcp_client_start(dhcp_client, NULL);
644 static gboolean timeout_renew(gpointer user_data)
646 struct connman_dhcpv6 *dhcp = user_data;
648 dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
650 DBG("renew RT timeout %d msec", dhcp->RT);
652 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
654 g_dhcp_client_start(dhcp->dhcp_client, NULL);
659 static gboolean start_renew(gpointer user_data)
661 struct connman_dhcpv6 *dhcp = user_data;
663 dhcp->RT = REN_TIMEOUT * (1 + get_random());
665 DBG("renew initial RT timeout %d msec", dhcp->RT);
667 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
674 int __connman_dhcpv6_start_renew(struct connman_network *network,
677 struct connman_dhcpv6 *dhcp;
679 time_t last_renew, current;
683 dhcp = g_hash_table_lookup(network_table, network);
687 if (dhcp->timeout > 0) {
688 g_source_remove(dhcp->timeout);
692 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
694 DBG("T1 %u T2 %u", T1, T2);
696 if (T1 == 0xffffffff)
702 * Client can choose the timeout.
708 dhcp->callback = callback;
710 DBG("renew after %d secs", T1);
712 dhcp->timeout = g_timeout_add_seconds(T1, start_renew, dhcp);
717 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
719 DBG("dhcp %p", dhcp);
721 if (dhcp->timeout > 0) {
722 g_source_remove(dhcp->timeout);
728 if (dhcp->dhcp_client == NULL)
731 g_dhcp_client_stop(dhcp->dhcp_client);
732 g_dhcp_client_unref(dhcp->dhcp_client);
734 dhcp->dhcp_client = NULL;
739 static void remove_network(gpointer user_data)
741 struct connman_dhcpv6 *dhcp = user_data;
743 DBG("dhcp %p", dhcp);
745 dhcpv6_release(dhcp);
750 static gboolean timeout_info_req(gpointer user_data)
752 struct connman_dhcpv6 *dhcp = user_data;
754 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
756 DBG("info RT timeout %d msec", dhcp->RT);
758 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
760 g_dhcp_client_start(dhcp->dhcp_client, NULL);
765 static gboolean start_info_req(gpointer user_data)
767 struct connman_dhcpv6 *dhcp = user_data;
769 /* Set the retransmission timeout, RFC 3315 chapter 14 */
770 dhcp->RT = INF_TIMEOUT * (1 + get_random());
772 DBG("info initial RT timeout %d msec", dhcp->RT);
774 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
776 dhcpv6_info_request(dhcp);
781 int __connman_dhcpv6_start_info(struct connman_network *network,
784 struct connman_dhcpv6 *dhcp;
789 dhcp = g_try_new0(struct connman_dhcpv6, 1);
793 dhcp->network = network;
794 dhcp->callback = callback;
796 connman_network_ref(network);
798 g_hash_table_replace(network_table, network, dhcp);
800 /* Initial timeout, RFC 3315, 18.1.5 */
801 delay = rand() % 1000;
803 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
808 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
810 struct connman_dhcpv6 *dhcp = user_data;
812 DBG("dhcpv6 advertise msg %p", dhcp);
814 if (dhcp->timeout > 0) {
815 g_source_remove(dhcp->timeout);
819 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
820 if (dhcp->callback != NULL)
821 dhcp->callback(dhcp->network, FALSE);
825 dhcp->RT = REQ_TIMEOUT * (1 + get_random());
826 DBG("request initial RT timeout %d msec", dhcp->RT);
827 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
829 dhcp->request_count = 1;
831 dhcpv6_request(dhcp, TRUE);
834 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
836 /* We get here if server supports rapid commit */
837 struct connman_dhcpv6 *dhcp = user_data;
839 DBG("dhcpv6 solicitation msg %p", dhcp);
841 if (dhcp->timeout > 0) {
842 g_source_remove(dhcp->timeout);
846 set_addresses(dhcp_client, dhcp);
849 static gboolean timeout_solicitation(gpointer user_data)
851 struct connman_dhcpv6 *dhcp = user_data;
853 dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
855 DBG("solicit RT timeout %d msec", dhcp->RT);
857 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
859 g_dhcp_client_start(dhcp->dhcp_client, NULL);
864 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
866 struct connman_service *service;
867 struct connman_ipconfig *ipconfig_ipv6;
868 GDHCPClient *dhcp_client;
869 GDHCPClientError error;
872 DBG("dhcp %p", dhcp);
874 index = connman_network_get_index(dhcp->network);
876 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
877 if (error != G_DHCP_CLIENT_ERROR_NONE)
880 if (getenv("CONNMAN_DHCPV6_DEBUG"))
881 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
883 service = __connman_service_lookup_from_network(dhcp->network);
887 ret = set_duid(service, dhcp->network, dhcp_client, index);
891 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
892 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
893 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
894 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
896 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
897 G_DHCPV6_SNTP_SERVERS);
899 ipconfig_ipv6 = __connman_service_get_ip6config(service);
900 dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
902 g_dhcpv6_client_set_ia(dhcp_client, index,
903 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
906 clear_callbacks(dhcp_client);
908 g_dhcp_client_register_event(dhcp_client,
909 G_DHCP_CLIENT_EVENT_SOLICITATION,
910 solicitation_cb, dhcp);
912 g_dhcp_client_register_event(dhcp_client,
913 G_DHCP_CLIENT_EVENT_ADVERTISE,
916 dhcp->dhcp_client = dhcp_client;
918 return g_dhcp_client_start(dhcp_client, NULL);
921 static gboolean start_solicitation(gpointer user_data)
923 struct connman_dhcpv6 *dhcp = user_data;
925 /* Set the retransmission timeout, RFC 3315 chapter 14 */
926 dhcp->RT = SOL_TIMEOUT * (1 + get_random());
928 DBG("solicit initial RT timeout %d msec", dhcp->RT);
930 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
932 dhcpv6_solicitation(dhcp);
937 int __connman_dhcpv6_start(struct connman_network *network,
938 GSList *prefixes, dhcp_cb callback)
940 struct connman_dhcpv6 *dhcp;
945 dhcp = g_try_new0(struct connman_dhcpv6, 1);
949 dhcp->network = network;
950 dhcp->callback = callback;
951 dhcp->prefixes = prefixes;
953 connman_network_ref(network);
955 g_hash_table_replace(network_table, network, dhcp);
957 /* Initial timeout, RFC 3315, 17.1.2 */
958 delay = rand() % 1000;
960 dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
965 void __connman_dhcpv6_stop(struct connman_network *network)
969 if (network_table == NULL)
972 if (g_hash_table_remove(network_table, network) == TRUE)
973 connman_network_unref(network);
976 int __connman_dhcpv6_init(void)
982 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
983 NULL, remove_network);
988 void __connman_dhcpv6_cleanup(void)
992 g_hash_table_destroy(network_table);
993 network_table = NULL;