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 dhcpv6_restart(gpointer user_data)
584 struct connman_dhcpv6 *dhcp = user_data;
586 if (dhcp->callback != NULL)
587 dhcp->callback(dhcp->network, FALSE);
593 * Check if we need to restart the solicitation procedure. This
594 * is done if all the addresses have expired. RFC 3315, 18.1.4
596 static int check_restart(struct connman_dhcpv6 *dhcp)
598 time_t current, expired;
600 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
601 NULL, NULL, &expired);
604 if (current > expired) {
605 DBG("expired by %d secs", (int)(current - expired));
607 g_timeout_add(0, dhcpv6_restart, dhcp);
615 static gboolean timeout_rebind(gpointer user_data)
617 struct connman_dhcpv6 *dhcp = user_data;
619 if (check_restart(dhcp) < 0)
622 dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
624 DBG("rebind RT timeout %d msec", dhcp->RT);
626 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
628 g_dhcp_client_start(dhcp->dhcp_client, NULL);
633 static gboolean start_rebind(gpointer user_data)
635 struct connman_dhcpv6 *dhcp = user_data;
637 dhcp->RT = REB_TIMEOUT * (1 + get_random());
639 DBG("rebind initial RT timeout %d msec", dhcp->RT);
641 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
648 static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
652 re_cb(dhcp_client, user_data);
655 static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
656 gboolean add_addresses)
658 GDHCPClient *dhcp_client;
661 DBG("dhcp %p add %d", dhcp, add_addresses);
663 dhcp_client = dhcp->dhcp_client;
665 g_dhcp_client_clear_requests(dhcp_client);
667 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
668 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
669 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
670 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
672 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
673 G_DHCPV6_SNTP_SERVERS);
675 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
676 g_dhcpv6_client_set_ia(dhcp_client,
677 connman_network_get_index(dhcp->network),
678 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
679 &T1, &T2, add_addresses);
681 clear_callbacks(dhcp_client);
683 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
686 dhcp->dhcp_client = dhcp_client;
688 return g_dhcp_client_start(dhcp_client, NULL);
691 static gboolean timeout_request(gpointer user_data)
693 struct connman_dhcpv6 *dhcp = user_data;
695 if (dhcp->request_count >= REQ_MAX_RC) {
696 DBG("max request retry attempts %d", dhcp->request_count);
697 dhcp->request_count = 0;
698 if (dhcp->callback != NULL)
699 dhcp->callback(dhcp->network, FALSE);
703 dhcp->request_count++;
705 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
706 DBG("request RT timeout %d msec", dhcp->RT);
707 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
709 g_dhcp_client_start(dhcp->dhcp_client, NULL);
714 static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
718 g_dhcpv6_client_reset_renew(dhcp_client);
720 re_cb(dhcp_client, user_data);
723 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
725 GDHCPClient *dhcp_client;
728 DBG("dhcp %p", dhcp);
730 dhcp_client = dhcp->dhcp_client;
732 g_dhcp_client_clear_requests(dhcp_client);
734 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
735 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
736 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
737 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
739 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
740 G_DHCPV6_SNTP_SERVERS);
742 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
743 g_dhcpv6_client_set_ia(dhcp_client,
744 connman_network_get_index(dhcp->network),
745 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
748 clear_callbacks(dhcp_client);
750 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
753 dhcp->dhcp_client = dhcp_client;
755 return g_dhcp_client_start(dhcp_client, NULL);
758 static gboolean timeout_renew(gpointer user_data)
760 struct connman_dhcpv6 *dhcp = user_data;
762 if (check_restart(dhcp) < 0)
765 dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
767 DBG("renew RT timeout %d msec", dhcp->RT);
769 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
771 g_dhcp_client_start(dhcp->dhcp_client, NULL);
776 static gboolean start_renew(gpointer user_data)
778 struct connman_dhcpv6 *dhcp = user_data;
780 dhcp->RT = REN_TIMEOUT * (1 + get_random());
782 DBG("renew initial RT timeout %d msec", dhcp->RT);
784 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
791 int __connman_dhcpv6_start_renew(struct connman_network *network,
794 struct connman_dhcpv6 *dhcp;
796 time_t last_renew, last_rebind, current, expired;
798 dhcp = g_hash_table_lookup(network_table, network);
802 if (dhcp->timeout > 0) {
803 g_source_remove(dhcp->timeout);
807 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
808 &last_renew, &last_rebind, &expired);
812 DBG("T1 %u T2 %u expires %lu current %lu", T1, T2,
813 (unsigned long)expired, current);
815 if (T1 == 0xffffffff)
821 * Client can choose the timeout.
825 /* RFC 3315, 18.1.4, start solicit if expired */
826 if (current > expired) {
827 DBG("expired by %d secs", (int)(current - expired));
831 dhcp->callback = callback;
833 if (T2 != 0xffffffff && T2 > 0 &&
834 (unsigned)current > (unsigned)last_rebind + T2) {
837 /* RFC 3315, chapter 18.1.3, start rebind */
838 if ((unsigned)current > (unsigned)last_renew + T1)
841 timeout = last_renew - current + T1;
844 * If we just did a renew, do not restart the rebind
847 dhcp->timeout = g_timeout_add_seconds(timeout, start_rebind,
850 DBG("renew after %d secs", T1);
852 dhcp->timeout = g_timeout_add_seconds(T1, start_renew, dhcp);
857 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
859 DBG("dhcp %p", dhcp);
861 if (dhcp->timeout > 0) {
862 g_source_remove(dhcp->timeout);
868 if (dhcp->dhcp_client == NULL)
871 g_dhcp_client_stop(dhcp->dhcp_client);
872 g_dhcp_client_unref(dhcp->dhcp_client);
874 dhcp->dhcp_client = NULL;
879 static void remove_network(gpointer user_data)
881 struct connman_dhcpv6 *dhcp = user_data;
883 DBG("dhcp %p", dhcp);
885 dhcpv6_release(dhcp);
890 static gboolean timeout_info_req(gpointer user_data)
892 struct connman_dhcpv6 *dhcp = user_data;
894 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
896 DBG("info RT timeout %d msec", dhcp->RT);
898 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
900 g_dhcp_client_start(dhcp->dhcp_client, NULL);
905 static gboolean start_info_req(gpointer user_data)
907 struct connman_dhcpv6 *dhcp = user_data;
909 /* Set the retransmission timeout, RFC 3315 chapter 14 */
910 dhcp->RT = INF_TIMEOUT * (1 + get_random());
912 DBG("info initial RT timeout %d msec", dhcp->RT);
914 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
916 dhcpv6_info_request(dhcp);
921 int __connman_dhcpv6_start_info(struct connman_network *network,
924 struct connman_dhcpv6 *dhcp;
929 dhcp = g_try_new0(struct connman_dhcpv6, 1);
933 dhcp->network = network;
934 dhcp->callback = callback;
936 connman_network_ref(network);
938 g_hash_table_replace(network_table, network, dhcp);
940 /* Initial timeout, RFC 3315, 18.1.5 */
941 delay = rand() % 1000;
943 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
948 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
950 struct connman_dhcpv6 *dhcp = user_data;
952 DBG("dhcpv6 advertise msg %p", dhcp);
954 if (dhcp->timeout > 0) {
955 g_source_remove(dhcp->timeout);
959 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
960 if (dhcp->callback != NULL)
961 dhcp->callback(dhcp->network, FALSE);
965 dhcp->RT = REQ_TIMEOUT * (1 + get_random());
966 DBG("request initial RT timeout %d msec", dhcp->RT);
967 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
969 dhcp->request_count = 1;
971 dhcpv6_request(dhcp, TRUE);
974 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
976 /* We get here if server supports rapid commit */
977 struct connman_dhcpv6 *dhcp = user_data;
979 DBG("dhcpv6 solicitation msg %p", dhcp);
981 if (dhcp->timeout > 0) {
982 g_source_remove(dhcp->timeout);
986 set_addresses(dhcp_client, dhcp);
989 static gboolean timeout_solicitation(gpointer user_data)
991 struct connman_dhcpv6 *dhcp = user_data;
993 dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
995 DBG("solicit RT timeout %d msec", dhcp->RT);
997 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
999 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1004 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
1006 struct connman_service *service;
1007 struct connman_ipconfig *ipconfig_ipv6;
1008 GDHCPClient *dhcp_client;
1009 GDHCPClientError error;
1012 DBG("dhcp %p", dhcp);
1014 index = connman_network_get_index(dhcp->network);
1016 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
1017 if (error != G_DHCP_CLIENT_ERROR_NONE)
1020 if (getenv("CONNMAN_DHCPV6_DEBUG"))
1021 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
1023 service = __connman_service_lookup_from_network(dhcp->network);
1024 if (service == NULL)
1027 ret = set_duid(service, dhcp->network, dhcp_client, index);
1031 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1032 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
1033 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1034 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1036 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
1037 G_DHCPV6_SNTP_SERVERS);
1039 ipconfig_ipv6 = __connman_service_get_ip6config(service);
1040 dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
1042 g_dhcpv6_client_set_ia(dhcp_client, index,
1043 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1046 clear_callbacks(dhcp_client);
1048 g_dhcp_client_register_event(dhcp_client,
1049 G_DHCP_CLIENT_EVENT_SOLICITATION,
1050 solicitation_cb, dhcp);
1052 g_dhcp_client_register_event(dhcp_client,
1053 G_DHCP_CLIENT_EVENT_ADVERTISE,
1054 advertise_cb, dhcp);
1056 dhcp->dhcp_client = dhcp_client;
1058 return g_dhcp_client_start(dhcp_client, NULL);
1061 static gboolean start_solicitation(gpointer user_data)
1063 struct connman_dhcpv6 *dhcp = user_data;
1065 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1066 dhcp->RT = SOL_TIMEOUT * (1 + get_random());
1068 DBG("solicit initial RT timeout %d msec", dhcp->RT);
1070 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1072 dhcpv6_solicitation(dhcp);
1077 int __connman_dhcpv6_start(struct connman_network *network,
1078 GSList *prefixes, dhcp_cb callback)
1080 struct connman_dhcpv6 *dhcp;
1085 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1089 dhcp->network = network;
1090 dhcp->callback = callback;
1091 dhcp->prefixes = prefixes;
1093 connman_network_ref(network);
1095 g_hash_table_replace(network_table, network, dhcp);
1097 /* Initial timeout, RFC 3315, 17.1.2 */
1098 delay = rand() % 1000;
1100 dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
1105 void __connman_dhcpv6_stop(struct connman_network *network)
1109 if (network_table == NULL)
1112 if (g_hash_table_remove(network_table, network) == TRUE)
1113 connman_network_unref(network);
1116 int __connman_dhcpv6_init(void)
1122 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1123 NULL, remove_network);
1128 void __connman_dhcpv6_cleanup(void)
1132 g_hash_table_destroy(network_table);
1133 network_table = NULL;