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 */
71 static GHashTable *network_table;
73 static int dhcpv6_request(struct connman_dhcpv6 *dhcp, gboolean add_addresses);
75 static inline float get_random()
77 return (rand() % 200 - 100) / 1000.0;
80 /* Calculate a random delay, RFC 3315 chapter 14 */
81 /* RT and MRT are milliseconds */
82 static guint calc_delay(guint RT, guint MRT)
84 float delay = get_random();
85 float rt = RT * (2 + delay);
88 rt = MRT * (1 + delay);
96 static void free_prefix(gpointer data, gpointer user_data)
101 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
103 g_strfreev(dhcp->nameservers);
104 g_strfreev(dhcp->timeservers);
106 dhcp->nameservers = NULL;
107 dhcp->timeservers = NULL;
109 g_slist_foreach(dhcp->prefixes, free_prefix, NULL);
110 g_slist_free(dhcp->prefixes);
113 static gboolean compare_string_arrays(char **array_a, char **array_b)
117 if (array_a == NULL || array_b == NULL)
120 if (g_strv_length(array_a) != g_strv_length(array_b))
123 for (i = 0; array_a[i] != NULL && array_b[i] != NULL; i++)
124 if (g_strcmp0(array_a[i], array_b[i]) != 0)
130 static void dhcpv6_debug(const char *str, void *data)
132 connman_info("%s: %s\n", (const char *) data, str);
135 static gchar *convert_to_hex(unsigned char *buf, int len)
137 gchar *ret = g_try_malloc(len * 2 + 1);
140 for (i = 0; ret != NULL && i < len; i++)
141 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
147 * DUID should not change over time so save it to file.
148 * See RFC 3315 chapter 9 for details.
150 static int set_duid(struct connman_service *service,
151 struct connman_network *network,
152 GDHCPClient *dhcp_client, int index)
160 ident = __connman_service_get_ident(service);
162 keyfile = connman_storage_load_service(ident);
166 hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
168 if (hex_duid != NULL) {
169 unsigned int i, j = 0, hex;
170 size_t hex_duid_len = strlen(hex_duid);
172 duid = g_try_malloc0(hex_duid_len / 2);
174 g_key_file_free(keyfile);
179 for (i = 0; i < hex_duid_len; i += 2) {
180 sscanf(hex_duid + i, "%02x", &hex);
184 duid_len = hex_duid_len / 2;
187 int type = __connman_ipconfig_get_type_from_index(index);
189 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
192 g_key_file_free(keyfile);
196 hex_duid = convert_to_hex(duid, duid_len);
197 if (hex_duid == NULL) {
198 g_key_file_free(keyfile);
202 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
205 __connman_storage_save_service(keyfile, ident);
209 g_key_file_free(keyfile);
211 g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
216 static void clear_callbacks(GDHCPClient *dhcp_client)
218 g_dhcp_client_register_event(dhcp_client,
219 G_DHCP_CLIENT_EVENT_SOLICITATION,
222 g_dhcp_client_register_event(dhcp_client,
223 G_DHCP_CLIENT_EVENT_ADVERTISE,
226 g_dhcp_client_register_event(dhcp_client,
227 G_DHCP_CLIENT_EVENT_REQUEST,
230 g_dhcp_client_register_event(dhcp_client,
231 G_DHCP_CLIENT_EVENT_RENEW,
234 g_dhcp_client_register_event(dhcp_client,
235 G_DHCP_CLIENT_EVENT_REBIND,
238 g_dhcp_client_register_event(dhcp_client,
239 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
243 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
245 struct connman_dhcpv6 *dhcp = user_data;
246 struct connman_service *service;
248 GList *option, *list;
249 char **nameservers, **timeservers;
251 DBG("dhcpv6 information-request %p", dhcp);
253 service = __connman_service_lookup_from_network(dhcp->network);
254 if (service == NULL) {
255 connman_error("Can not lookup service");
259 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
260 entries = g_list_length(option);
262 nameservers = g_try_new0(char *, entries + 1);
263 if (nameservers != NULL) {
264 for (i = 0, list = option; list; list = list->next, i++)
265 nameservers[i] = g_strdup(list->data);
268 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
269 if (dhcp->nameservers != NULL) {
270 for (i = 0; dhcp->nameservers[i] != NULL; i++)
271 __connman_service_nameserver_remove(service,
272 dhcp->nameservers[i],
274 g_strfreev(dhcp->nameservers);
277 dhcp->nameservers = nameservers;
279 for (i = 0; dhcp->nameservers[i] != NULL; i++)
280 __connman_service_nameserver_append(service,
281 dhcp->nameservers[i],
284 g_strfreev(nameservers);
287 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
288 entries = g_list_length(option);
290 timeservers = g_try_new0(char *, entries + 1);
291 if (timeservers != NULL) {
292 for (i = 0, list = option; list; list = list->next, i++)
293 timeservers[i] = g_strdup(list->data);
296 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
297 if (dhcp->timeservers != NULL) {
298 for (i = 0; dhcp->timeservers[i] != NULL; i++)
299 __connman_service_timeserver_remove(service,
300 dhcp->timeservers[i]);
301 g_strfreev(dhcp->timeservers);
304 dhcp->timeservers = timeservers;
306 for (i = 0; dhcp->timeservers[i] != NULL; i++)
307 __connman_service_timeserver_append(service,
308 dhcp->timeservers[i]);
310 g_strfreev(timeservers);
313 if (dhcp->callback != NULL) {
314 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
315 dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
319 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
321 struct connman_service *service;
322 GDHCPClient *dhcp_client;
323 GDHCPClientError error;
326 DBG("dhcp %p", dhcp);
328 index = connman_network_get_index(dhcp->network);
330 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
331 if (error != G_DHCP_CLIENT_ERROR_NONE)
334 if (getenv("CONNMAN_DHCPV6_DEBUG"))
335 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
337 service = __connman_service_lookup_from_network(dhcp->network);
341 ret = set_duid(service, dhcp->network, dhcp_client, index);
345 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
346 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
347 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
349 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
350 G_DHCPV6_SNTP_SERVERS);
352 g_dhcp_client_register_event(dhcp_client,
353 G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
355 dhcp->dhcp_client = dhcp_client;
357 return g_dhcp_client_start(dhcp_client, NULL);
360 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
362 struct in6_addr addr_prefix, addr;
366 for (list = prefixes; list; list = list->next) {
367 char *prefix = list->data;
368 const char *slash = g_strrstr(prefix, "/");
369 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
370 0xF0, 0xE0, 0xC0, 0x80 };
371 int left, count, i, plen;
376 prefix = g_strndup(prefix, slash - prefix);
377 len = strtol(slash + 1, NULL, 10);
384 inet_pton(AF_INET6, prefix, &addr_prefix);
385 inet_pton(AF_INET6, address, &addr);
387 memset(&addr_prefix.s6_addr[i], 0, count);
388 memset(&addr.s6_addr[i], 0, count);
391 addr_prefix.s6_addr[i - 1] &= bits[left];
392 addr.s6_addr[i - 1] &= bits[left];
397 if (memcmp(&addr_prefix, &addr, 16) == 0) {
406 static int set_addresses(GDHCPClient *dhcp_client,
407 struct connman_dhcpv6 *dhcp)
409 struct connman_service *service;
410 struct connman_ipconfig *ipconfig;
412 GList *option, *list;
413 char **nameservers, **timeservers;
414 const char *c_address;
415 char *address = NULL;
417 service = __connman_service_lookup_from_network(dhcp->network);
418 if (service == NULL) {
419 connman_error("Can not lookup service");
423 ipconfig = __connman_service_get_ip6config(service);
424 if (ipconfig == NULL) {
425 connman_error("Could not lookup ip6config");
429 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
430 entries = g_list_length(option);
432 nameservers = g_try_new0(char *, entries + 1);
433 if (nameservers != NULL) {
434 for (i = 0, list = option; list; list = list->next, i++)
435 nameservers[i] = g_strdup(list->data);
438 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
439 if (dhcp->nameservers != NULL) {
440 for (i = 0; dhcp->nameservers[i] != NULL; i++)
441 __connman_service_nameserver_remove(service,
442 dhcp->nameservers[i],
444 g_strfreev(dhcp->nameservers);
447 dhcp->nameservers = nameservers;
449 for (i = 0; dhcp->nameservers[i] != NULL; i++)
450 __connman_service_nameserver_append(service,
451 dhcp->nameservers[i],
454 g_strfreev(nameservers);
457 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
458 entries = g_list_length(option);
460 timeservers = g_try_new0(char *, entries + 1);
461 if (timeservers != NULL) {
462 for (i = 0, list = option; list; list = list->next, i++)
463 timeservers[i] = g_strdup(list->data);
466 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
467 if (dhcp->timeservers != NULL) {
468 for (i = 0; dhcp->timeservers[i] != NULL; i++)
469 __connman_service_timeserver_remove(service,
470 dhcp->timeservers[i]);
471 g_strfreev(dhcp->timeservers);
474 dhcp->timeservers = timeservers;
476 for (i = 0; dhcp->timeservers[i] != NULL; i++)
477 __connman_service_timeserver_append(service,
478 dhcp->timeservers[i]);
480 g_strfreev(timeservers);
483 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
485 address = g_strdup(option->data);
487 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
489 address = g_strdup(option->data);
492 c_address = __connman_ipconfig_get_local(ipconfig);
494 if (address != NULL &&
495 ((c_address != NULL &&
496 g_strcmp0(address, c_address) != 0) ||
497 (c_address == NULL))) {
500 /* Is this prefix part of the subnet we are suppose to use? */
501 prefix_len = check_ipv6_addr_prefix(dhcp->prefixes, address);
503 __connman_ipconfig_set_local(ipconfig, address);
504 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
506 DBG("new address %s/%d", address, prefix_len);
512 static void re_cb(GDHCPClient *dhcp_client, gpointer user_data)
514 struct connman_dhcpv6 *dhcp = user_data;
518 ret = set_addresses(dhcp_client, dhcp);
520 status = g_dhcpv6_client_get_status(dhcp_client);
522 DBG("dhcpv6 cb msg %p ret %d status %d", dhcp, ret, status);
525 if (dhcp->callback != NULL)
526 dhcp->callback(dhcp->network, FALSE);
530 if (status == G_DHCPV6_ERROR_BINDING) {
531 /* RFC 3315, 18.1.8 */
532 dhcpv6_request(dhcp, FALSE);
534 if (dhcp->callback != NULL)
535 dhcp->callback(dhcp->network,
536 status == 0 ? TRUE : FALSE);
540 static void rebind_cb(GDHCPClient *dhcp_client, gpointer user_data)
544 g_dhcpv6_client_reset_rebind(dhcp_client);
545 g_dhcpv6_client_reset_renew(dhcp_client);
547 re_cb(dhcp_client, user_data);
550 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp)
552 GDHCPClient *dhcp_client;
554 DBG("dhcp %p", dhcp);
556 dhcp_client = dhcp->dhcp_client;
558 g_dhcp_client_clear_requests(dhcp_client);
560 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
561 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
562 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
564 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
565 G_DHCPV6_SNTP_SERVERS);
567 g_dhcpv6_client_set_ia(dhcp_client,
568 connman_network_get_index(dhcp->network),
569 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
572 clear_callbacks(dhcp_client);
574 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
577 dhcp->dhcp_client = dhcp_client;
579 return g_dhcp_client_start(dhcp_client, NULL);
582 static gboolean timeout_rebind(gpointer user_data)
584 struct connman_dhcpv6 *dhcp = user_data;
586 dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
588 DBG("rebind RT timeout %d msec", dhcp->RT);
590 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
592 g_dhcp_client_start(dhcp->dhcp_client, NULL);
597 static gboolean start_rebind(gpointer user_data)
599 struct connman_dhcpv6 *dhcp = user_data;
601 dhcp->RT = REB_TIMEOUT * (1 + get_random());
603 DBG("rebind initial RT timeout %d msec", dhcp->RT);
605 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
612 static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
616 re_cb(dhcp_client, user_data);
619 static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
620 gboolean add_addresses)
622 GDHCPClient *dhcp_client;
625 DBG("dhcp %p add %d", dhcp, add_addresses);
627 dhcp_client = dhcp->dhcp_client;
629 g_dhcp_client_clear_requests(dhcp_client);
631 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
632 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
633 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
634 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
636 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
637 G_DHCPV6_SNTP_SERVERS);
639 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
640 g_dhcpv6_client_set_ia(dhcp_client,
641 connman_network_get_index(dhcp->network),
642 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
643 &T1, &T2, add_addresses);
645 clear_callbacks(dhcp_client);
647 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
650 dhcp->dhcp_client = dhcp_client;
652 return g_dhcp_client_start(dhcp_client, NULL);
655 static gboolean timeout_request(gpointer user_data)
657 struct connman_dhcpv6 *dhcp = user_data;
659 if (dhcp->request_count >= REQ_MAX_RC) {
660 DBG("max request retry attempts %d", dhcp->request_count);
661 dhcp->request_count = 0;
662 if (dhcp->callback != NULL)
663 dhcp->callback(dhcp->network, FALSE);
667 dhcp->request_count++;
669 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
670 DBG("request RT timeout %d msec", dhcp->RT);
671 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
673 g_dhcp_client_start(dhcp->dhcp_client, NULL);
678 static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
682 g_dhcpv6_client_reset_renew(dhcp_client);
684 re_cb(dhcp_client, user_data);
687 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
689 GDHCPClient *dhcp_client;
692 DBG("dhcp %p", dhcp);
694 dhcp_client = dhcp->dhcp_client;
696 g_dhcp_client_clear_requests(dhcp_client);
698 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
699 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
700 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
701 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
703 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
704 G_DHCPV6_SNTP_SERVERS);
706 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
707 g_dhcpv6_client_set_ia(dhcp_client,
708 connman_network_get_index(dhcp->network),
709 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
712 clear_callbacks(dhcp_client);
714 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
717 dhcp->dhcp_client = dhcp_client;
719 return g_dhcp_client_start(dhcp_client, NULL);
722 static gboolean timeout_renew(gpointer user_data)
724 struct connman_dhcpv6 *dhcp = user_data;
726 dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
728 DBG("renew RT timeout %d msec", dhcp->RT);
730 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
732 g_dhcp_client_start(dhcp->dhcp_client, NULL);
737 static gboolean start_renew(gpointer user_data)
739 struct connman_dhcpv6 *dhcp = user_data;
741 dhcp->RT = REN_TIMEOUT * (1 + get_random());
743 DBG("renew initial RT timeout %d msec", dhcp->RT);
745 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
752 int __connman_dhcpv6_start_renew(struct connman_network *network,
755 struct connman_dhcpv6 *dhcp;
757 time_t last_renew, last_rebind, current;
761 dhcp = g_hash_table_lookup(network_table, network);
765 if (dhcp->timeout > 0) {
766 g_source_remove(dhcp->timeout);
770 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
771 &last_renew, &last_rebind);
772 DBG("T1 %u T2 %u", T1, T2);
774 if (T1 == 0xffffffff)
780 * Client can choose the timeout.
786 dhcp->callback = callback;
788 if (T2 != 0xffffffff && T2 > 0 &&
789 (unsigned)current > (unsigned)last_rebind + T2) {
790 /* RFC 3315, chapter 18.1.3, start rebind */
793 if ((unsigned)current > (unsigned)last_renew + T1)
796 timeout = last_renew - current + T1;
799 * If we just did a renew, do not restart the rebind
802 dhcp->timeout = g_timeout_add_seconds(timeout, start_rebind,
805 DBG("renew after %d secs", T1);
807 dhcp->timeout = g_timeout_add_seconds(T1, start_renew, dhcp);
812 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
814 DBG("dhcp %p", dhcp);
816 if (dhcp->timeout > 0) {
817 g_source_remove(dhcp->timeout);
823 if (dhcp->dhcp_client == NULL)
826 g_dhcp_client_stop(dhcp->dhcp_client);
827 g_dhcp_client_unref(dhcp->dhcp_client);
829 dhcp->dhcp_client = NULL;
834 static void remove_network(gpointer user_data)
836 struct connman_dhcpv6 *dhcp = user_data;
838 DBG("dhcp %p", dhcp);
840 dhcpv6_release(dhcp);
845 static gboolean timeout_info_req(gpointer user_data)
847 struct connman_dhcpv6 *dhcp = user_data;
849 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
851 DBG("info RT timeout %d msec", dhcp->RT);
853 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
855 g_dhcp_client_start(dhcp->dhcp_client, NULL);
860 static gboolean start_info_req(gpointer user_data)
862 struct connman_dhcpv6 *dhcp = user_data;
864 /* Set the retransmission timeout, RFC 3315 chapter 14 */
865 dhcp->RT = INF_TIMEOUT * (1 + get_random());
867 DBG("info initial RT timeout %d msec", dhcp->RT);
869 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
871 dhcpv6_info_request(dhcp);
876 int __connman_dhcpv6_start_info(struct connman_network *network,
879 struct connman_dhcpv6 *dhcp;
884 dhcp = g_try_new0(struct connman_dhcpv6, 1);
888 dhcp->network = network;
889 dhcp->callback = callback;
891 connman_network_ref(network);
893 g_hash_table_replace(network_table, network, dhcp);
895 /* Initial timeout, RFC 3315, 18.1.5 */
896 delay = rand() % 1000;
898 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
903 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
905 struct connman_dhcpv6 *dhcp = user_data;
907 DBG("dhcpv6 advertise msg %p", dhcp);
909 if (dhcp->timeout > 0) {
910 g_source_remove(dhcp->timeout);
914 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
915 if (dhcp->callback != NULL)
916 dhcp->callback(dhcp->network, FALSE);
920 dhcp->RT = REQ_TIMEOUT * (1 + get_random());
921 DBG("request initial RT timeout %d msec", dhcp->RT);
922 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
924 dhcp->request_count = 1;
926 dhcpv6_request(dhcp, TRUE);
929 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
931 /* We get here if server supports rapid commit */
932 struct connman_dhcpv6 *dhcp = user_data;
934 DBG("dhcpv6 solicitation msg %p", dhcp);
936 if (dhcp->timeout > 0) {
937 g_source_remove(dhcp->timeout);
941 set_addresses(dhcp_client, dhcp);
944 static gboolean timeout_solicitation(gpointer user_data)
946 struct connman_dhcpv6 *dhcp = user_data;
948 dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
950 DBG("solicit RT timeout %d msec", dhcp->RT);
952 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
954 g_dhcp_client_start(dhcp->dhcp_client, NULL);
959 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
961 struct connman_service *service;
962 struct connman_ipconfig *ipconfig_ipv6;
963 GDHCPClient *dhcp_client;
964 GDHCPClientError error;
967 DBG("dhcp %p", dhcp);
969 index = connman_network_get_index(dhcp->network);
971 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
972 if (error != G_DHCP_CLIENT_ERROR_NONE)
975 if (getenv("CONNMAN_DHCPV6_DEBUG"))
976 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
978 service = __connman_service_lookup_from_network(dhcp->network);
982 ret = set_duid(service, dhcp->network, dhcp_client, index);
986 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
987 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
988 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
989 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
991 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
992 G_DHCPV6_SNTP_SERVERS);
994 ipconfig_ipv6 = __connman_service_get_ip6config(service);
995 dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
997 g_dhcpv6_client_set_ia(dhcp_client, index,
998 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1001 clear_callbacks(dhcp_client);
1003 g_dhcp_client_register_event(dhcp_client,
1004 G_DHCP_CLIENT_EVENT_SOLICITATION,
1005 solicitation_cb, dhcp);
1007 g_dhcp_client_register_event(dhcp_client,
1008 G_DHCP_CLIENT_EVENT_ADVERTISE,
1009 advertise_cb, dhcp);
1011 dhcp->dhcp_client = dhcp_client;
1013 return g_dhcp_client_start(dhcp_client, NULL);
1016 static gboolean start_solicitation(gpointer user_data)
1018 struct connman_dhcpv6 *dhcp = user_data;
1020 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1021 dhcp->RT = SOL_TIMEOUT * (1 + get_random());
1023 DBG("solicit initial RT timeout %d msec", dhcp->RT);
1025 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1027 dhcpv6_solicitation(dhcp);
1032 int __connman_dhcpv6_start(struct connman_network *network,
1033 GSList *prefixes, dhcp_cb callback)
1035 struct connman_dhcpv6 *dhcp;
1040 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1044 dhcp->network = network;
1045 dhcp->callback = callback;
1046 dhcp->prefixes = prefixes;
1048 connman_network_ref(network);
1050 g_hash_table_replace(network_table, network, dhcp);
1052 /* Initial timeout, RFC 3315, 17.1.2 */
1053 delay = rand() % 1000;
1055 dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
1060 void __connman_dhcpv6_stop(struct connman_network *network)
1064 if (network_table == NULL)
1067 if (g_hash_table_remove(network_table, network) == TRUE)
1068 connman_network_unref(network);
1071 int __connman_dhcpv6_init(void)
1077 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1078 NULL, remove_network);
1083 void __connman_dhcpv6_cleanup(void)
1087 g_hash_table_destroy(network_table);
1088 network_table = NULL;