5 * Copyright (C) 2012 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 MRD; /* max operation timeout in msec */
66 guint RT; /* in msec */
67 gboolean use_ta; /* set to TRUE if IPv6 privacy is enabled */
68 GSList *prefixes; /* network prefixes from radvd */
69 int request_count; /* how many times REQUEST have been sent */
70 gboolean stateless; /* TRUE if stateless DHCPv6 is used */
71 gboolean started; /* TRUE if we have DHCPv6 started */
74 static GHashTable *network_table;
76 static int dhcpv6_request(struct connman_dhcpv6 *dhcp, gboolean add_addresses);
78 static void clear_timer(struct connman_dhcpv6 *dhcp)
80 if (dhcp->timeout > 0) {
81 g_source_remove(dhcp->timeout);
86 g_source_remove(dhcp->MRD);
91 static inline float get_random()
93 return (rand() % 200 - 100) / 1000.0;
96 /* Calculate a random delay, RFC 3315 chapter 14 */
97 /* RT and MRT are milliseconds */
98 static guint calc_delay(guint RT, guint MRT)
100 float delay = get_random();
101 float rt = RT * (2 + delay);
104 rt = MRT * (1 + delay);
112 static void free_prefix(gpointer data, gpointer user_data)
117 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
119 g_strfreev(dhcp->nameservers);
120 g_strfreev(dhcp->timeservers);
122 dhcp->nameservers = NULL;
123 dhcp->timeservers = NULL;
124 dhcp->started = FALSE;
126 g_slist_foreach(dhcp->prefixes, free_prefix, NULL);
127 g_slist_free(dhcp->prefixes);
130 static gboolean compare_string_arrays(char **array_a, char **array_b)
134 if (array_a == NULL || array_b == NULL)
137 if (g_strv_length(array_a) != g_strv_length(array_b))
140 for (i = 0; array_a[i] != NULL && array_b[i] != NULL; i++)
141 if (g_strcmp0(array_a[i], array_b[i]) != 0)
147 static void dhcpv6_debug(const char *str, void *data)
149 connman_info("%s: %s\n", (const char *) data, str);
152 static gchar *convert_to_hex(unsigned char *buf, int len)
154 gchar *ret = g_try_malloc(len * 2 + 1);
157 for (i = 0; ret != NULL && i < len; i++)
158 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
164 * DUID should not change over time so save it to file.
165 * See RFC 3315 chapter 9 for details.
167 static int set_duid(struct connman_service *service,
168 struct connman_network *network,
169 GDHCPClient *dhcp_client, int index)
177 ident = __connman_service_get_ident(service);
179 keyfile = connman_storage_load_service(ident);
183 hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
185 if (hex_duid != NULL) {
186 unsigned int i, j = 0, hex;
187 size_t hex_duid_len = strlen(hex_duid);
189 duid = g_try_malloc0(hex_duid_len / 2);
191 g_key_file_free(keyfile);
196 for (i = 0; i < hex_duid_len; i += 2) {
197 sscanf(hex_duid + i, "%02x", &hex);
201 duid_len = hex_duid_len / 2;
204 int type = __connman_ipconfig_get_type_from_index(index);
206 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
209 g_key_file_free(keyfile);
213 hex_duid = convert_to_hex(duid, duid_len);
214 if (hex_duid == NULL) {
215 g_key_file_free(keyfile);
219 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
222 __connman_storage_save_service(keyfile, ident);
226 g_key_file_free(keyfile);
228 g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
233 static void clear_callbacks(GDHCPClient *dhcp_client)
235 g_dhcp_client_register_event(dhcp_client,
236 G_DHCP_CLIENT_EVENT_SOLICITATION,
239 g_dhcp_client_register_event(dhcp_client,
240 G_DHCP_CLIENT_EVENT_ADVERTISE,
243 g_dhcp_client_register_event(dhcp_client,
244 G_DHCP_CLIENT_EVENT_REQUEST,
247 g_dhcp_client_register_event(dhcp_client,
248 G_DHCP_CLIENT_EVENT_RENEW,
251 g_dhcp_client_register_event(dhcp_client,
252 G_DHCP_CLIENT_EVENT_REBIND,
255 g_dhcp_client_register_event(dhcp_client,
256 G_DHCP_CLIENT_EVENT_RELEASE,
259 g_dhcp_client_register_event(dhcp_client,
260 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
264 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
266 struct connman_dhcpv6 *dhcp = user_data;
267 struct connman_service *service;
269 GList *option, *list;
270 char **nameservers, **timeservers;
272 DBG("dhcpv6 information-request %p", dhcp);
274 service = connman_service_lookup_from_network(dhcp->network);
275 if (service == NULL) {
276 connman_error("Can not lookup service");
280 g_dhcpv6_client_clear_retransmit(dhcp_client);
282 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
283 entries = g_list_length(option);
285 nameservers = g_try_new0(char *, entries + 1);
286 if (nameservers != NULL) {
287 for (i = 0, list = option; list; list = list->next, i++)
288 nameservers[i] = g_strdup(list->data);
291 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
292 if (dhcp->nameservers != NULL) {
293 for (i = 0; dhcp->nameservers[i] != NULL; i++)
294 __connman_service_nameserver_remove(service,
295 dhcp->nameservers[i],
297 g_strfreev(dhcp->nameservers);
300 dhcp->nameservers = nameservers;
302 for (i = 0; dhcp->nameservers != NULL &&
303 dhcp->nameservers[i] != NULL; i++)
304 __connman_service_nameserver_append(service,
305 dhcp->nameservers[i],
308 g_strfreev(nameservers);
311 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
312 entries = g_list_length(option);
314 timeservers = g_try_new0(char *, entries + 1);
315 if (timeservers != NULL) {
316 for (i = 0, list = option; list; list = list->next, i++)
317 timeservers[i] = g_strdup(list->data);
320 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
321 if (dhcp->timeservers != NULL) {
322 for (i = 0; dhcp->timeservers[i] != NULL; i++)
323 __connman_service_timeserver_remove(service,
324 dhcp->timeservers[i]);
325 g_strfreev(dhcp->timeservers);
328 dhcp->timeservers = timeservers;
330 for (i = 0; dhcp->timeservers != NULL &&
331 dhcp->timeservers[i] != NULL; i++)
332 __connman_service_timeserver_append(service,
333 dhcp->timeservers[i]);
335 g_strfreev(timeservers);
338 if (dhcp->callback != NULL) {
339 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
340 dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
344 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
346 struct connman_service *service;
347 GDHCPClient *dhcp_client;
348 GDHCPClientError error;
351 DBG("dhcp %p", dhcp);
353 index = connman_network_get_index(dhcp->network);
355 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
356 if (error != G_DHCP_CLIENT_ERROR_NONE) {
361 if (getenv("CONNMAN_DHCPV6_DEBUG"))
362 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
364 service = connman_service_lookup_from_network(dhcp->network);
365 if (service == NULL) {
367 g_dhcp_client_unref(dhcp_client);
371 ret = set_duid(service, dhcp->network, dhcp_client, index);
374 g_dhcp_client_unref(dhcp_client);
378 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
379 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
380 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
382 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
383 G_DHCPV6_SNTP_SERVERS);
385 g_dhcp_client_register_event(dhcp_client,
386 G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
388 dhcp->dhcp_client = dhcp_client;
390 return g_dhcp_client_start(dhcp_client, NULL);
393 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
395 struct in6_addr addr_prefix, addr;
399 for (list = prefixes; list; list = list->next) {
400 char *prefix = list->data;
401 const char *slash = g_strrstr(prefix, "/");
402 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
403 0xF0, 0xE0, 0xC0, 0x80 };
404 int left, count, i, plen;
409 prefix = g_strndup(prefix, slash - prefix);
410 len = strtol(slash + 1, NULL, 10);
417 inet_pton(AF_INET6, prefix, &addr_prefix);
418 inet_pton(AF_INET6, address, &addr);
420 memset(&addr_prefix.s6_addr[i], 0, count);
421 memset(&addr.s6_addr[i], 0, count);
424 addr_prefix.s6_addr[i - 1] &= bits[left];
425 addr.s6_addr[i - 1] &= bits[left];
430 if (memcmp(&addr_prefix, &addr, 16) == 0) {
439 static int set_addresses(GDHCPClient *dhcp_client,
440 struct connman_dhcpv6 *dhcp)
442 struct connman_service *service;
443 struct connman_ipconfig *ipconfig;
445 GList *option, *list;
446 char **nameservers, **timeservers;
447 const char *c_address;
448 char *address = NULL;
450 service = connman_service_lookup_from_network(dhcp->network);
451 if (service == NULL) {
452 connman_error("Can not lookup service");
456 ipconfig = __connman_service_get_ip6config(service);
457 if (ipconfig == NULL) {
458 connman_error("Could not lookup ip6config");
462 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
463 entries = g_list_length(option);
465 nameservers = g_try_new0(char *, entries + 1);
466 if (nameservers != NULL) {
467 for (i = 0, list = option; list; list = list->next, i++)
468 nameservers[i] = g_strdup(list->data);
471 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
472 if (dhcp->nameservers != NULL) {
473 for (i = 0; dhcp->nameservers[i] != NULL; i++)
474 __connman_service_nameserver_remove(service,
475 dhcp->nameservers[i],
477 g_strfreev(dhcp->nameservers);
480 dhcp->nameservers = nameservers;
482 for (i = 0; dhcp->nameservers != NULL &&
483 dhcp->nameservers[i] != NULL; i++)
484 __connman_service_nameserver_append(service,
485 dhcp->nameservers[i],
488 g_strfreev(nameservers);
491 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
492 entries = g_list_length(option);
494 timeservers = g_try_new0(char *, entries + 1);
495 if (timeservers != NULL) {
496 for (i = 0, list = option; list; list = list->next, i++)
497 timeservers[i] = g_strdup(list->data);
500 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
501 if (dhcp->timeservers != NULL) {
502 for (i = 0; dhcp->timeservers[i] != NULL; i++)
503 __connman_service_timeserver_remove(service,
504 dhcp->timeservers[i]);
505 g_strfreev(dhcp->timeservers);
508 dhcp->timeservers = timeservers;
510 for (i = 0; dhcp->timeservers != NULL &&
511 dhcp->timeservers[i] != NULL; i++)
512 __connman_service_timeserver_append(service,
513 dhcp->timeservers[i]);
515 g_strfreev(timeservers);
518 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
520 address = g_strdup(option->data);
522 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
524 address = g_strdup(option->data);
527 c_address = __connman_ipconfig_get_local(ipconfig);
529 if (address != NULL &&
530 ((c_address != NULL &&
531 g_strcmp0(address, c_address) != 0) ||
532 (c_address == NULL))) {
535 /* Is this prefix part of the subnet we are suppose to use? */
536 prefix_len = check_ipv6_addr_prefix(dhcp->prefixes, address);
538 __connman_ipconfig_set_local(ipconfig, address);
539 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
541 DBG("new address %s/%d", address, prefix_len);
549 static void re_cb(GDHCPClient *dhcp_client, gpointer user_data)
551 struct connman_dhcpv6 *dhcp = user_data;
555 ret = set_addresses(dhcp_client, dhcp);
557 status = g_dhcpv6_client_get_status(dhcp_client);
559 DBG("dhcpv6 cb msg %p ret %d status %d", dhcp, ret, status);
562 if (dhcp->callback != NULL)
563 dhcp->callback(dhcp->network, FALSE);
567 if (status == G_DHCPV6_ERROR_BINDING) {
568 /* RFC 3315, 18.1.8 */
569 dhcpv6_request(dhcp, FALSE);
571 if (dhcp->callback != NULL)
572 dhcp->callback(dhcp->network,
573 status == 0 ? TRUE : FALSE);
577 static void rebind_cb(GDHCPClient *dhcp_client, gpointer user_data)
581 g_dhcpv6_client_reset_rebind(dhcp_client);
582 g_dhcpv6_client_reset_renew(dhcp_client);
583 g_dhcpv6_client_clear_retransmit(dhcp_client);
585 re_cb(dhcp_client, user_data);
588 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp)
590 GDHCPClient *dhcp_client;
592 DBG("dhcp %p", dhcp);
594 dhcp_client = dhcp->dhcp_client;
596 g_dhcp_client_clear_requests(dhcp_client);
598 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
599 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
600 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
602 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
603 G_DHCPV6_SNTP_SERVERS);
605 g_dhcpv6_client_set_ia(dhcp_client,
606 connman_network_get_index(dhcp->network),
607 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
610 clear_callbacks(dhcp_client);
612 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
615 dhcp->dhcp_client = dhcp_client;
617 return g_dhcp_client_start(dhcp_client, NULL);
620 static gboolean dhcpv6_restart(gpointer user_data)
622 struct connman_dhcpv6 *dhcp = user_data;
624 if (dhcp->callback != NULL)
625 dhcp->callback(dhcp->network, FALSE);
631 * Check if we need to restart the solicitation procedure. This
632 * is done if all the addresses have expired. RFC 3315, 18.1.4
634 static int check_restart(struct connman_dhcpv6 *dhcp)
636 time_t current, expired;
638 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
639 NULL, NULL, &expired);
640 current = time(NULL);
642 if (current > expired) {
643 DBG("expired by %d secs", (int)(current - expired));
645 g_timeout_add(0, dhcpv6_restart, dhcp);
653 static gboolean timeout_rebind(gpointer user_data)
655 struct connman_dhcpv6 *dhcp = user_data;
657 if (check_restart(dhcp) < 0)
660 dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
662 DBG("rebind RT timeout %d msec", dhcp->RT);
664 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
666 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
668 g_dhcp_client_start(dhcp->dhcp_client, NULL);
673 static gboolean start_rebind(gpointer user_data)
675 struct connman_dhcpv6 *dhcp = user_data;
677 dhcp->RT = REB_TIMEOUT * (1 + get_random());
679 DBG("rebind initial RT timeout %d msec", dhcp->RT);
681 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
688 static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
692 g_dhcpv6_client_clear_retransmit(dhcp_client);
694 re_cb(dhcp_client, user_data);
697 static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
698 gboolean add_addresses)
700 GDHCPClient *dhcp_client;
703 DBG("dhcp %p add %d", dhcp, add_addresses);
705 dhcp_client = dhcp->dhcp_client;
707 g_dhcp_client_clear_requests(dhcp_client);
709 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
710 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
711 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
712 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
714 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
715 G_DHCPV6_SNTP_SERVERS);
717 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
718 g_dhcpv6_client_set_ia(dhcp_client,
719 connman_network_get_index(dhcp->network),
720 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
721 &T1, &T2, add_addresses);
723 clear_callbacks(dhcp_client);
725 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
728 dhcp->dhcp_client = dhcp_client;
730 return g_dhcp_client_start(dhcp_client, NULL);
733 static gboolean timeout_request(gpointer user_data)
735 struct connman_dhcpv6 *dhcp = user_data;
737 if (dhcp->request_count >= REQ_MAX_RC) {
738 DBG("max request retry attempts %d", dhcp->request_count);
739 dhcp->request_count = 0;
740 if (dhcp->callback != NULL)
741 dhcp->callback(dhcp->network, FALSE);
745 dhcp->request_count++;
747 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
748 DBG("request RT timeout %d msec", dhcp->RT);
749 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
751 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
753 g_dhcp_client_start(dhcp->dhcp_client, NULL);
758 static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
762 g_dhcpv6_client_reset_renew(dhcp_client);
763 g_dhcpv6_client_clear_retransmit(dhcp_client);
765 re_cb(dhcp_client, user_data);
768 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
770 GDHCPClient *dhcp_client;
773 DBG("dhcp %p", dhcp);
775 dhcp_client = dhcp->dhcp_client;
777 g_dhcp_client_clear_requests(dhcp_client);
779 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
780 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
781 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
782 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
784 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
785 G_DHCPV6_SNTP_SERVERS);
787 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
788 g_dhcpv6_client_set_ia(dhcp_client,
789 connman_network_get_index(dhcp->network),
790 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
793 clear_callbacks(dhcp_client);
795 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
798 dhcp->dhcp_client = dhcp_client;
800 return g_dhcp_client_start(dhcp_client, NULL);
803 static gboolean timeout_renew(gpointer user_data)
805 struct connman_dhcpv6 *dhcp = user_data;
807 if (check_restart(dhcp) < 0)
810 dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
812 DBG("renew RT timeout %d msec", dhcp->RT);
814 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
816 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
818 g_dhcp_client_start(dhcp->dhcp_client, NULL);
823 static gboolean start_renew(gpointer user_data)
825 struct connman_dhcpv6 *dhcp = user_data;
827 dhcp->RT = REN_TIMEOUT * (1 + get_random());
829 DBG("renew initial RT timeout %d msec", dhcp->RT);
831 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
838 int __connman_dhcpv6_start_renew(struct connman_network *network,
841 struct connman_dhcpv6 *dhcp;
843 time_t last_renew, last_rebind, current, expired;
845 dhcp = g_hash_table_lookup(network_table, network);
849 DBG("network %p dhcp %p", network, dhcp);
853 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
854 &last_renew, &last_rebind, &expired);
856 current = time(NULL);
858 DBG("T1 %u T2 %u expires %lu current %lu", T1, T2,
859 (unsigned long)expired, current);
861 if (T1 == 0xffffffff)
867 * Client can choose the timeout.
871 /* RFC 3315, 18.1.4, start solicit if expired */
872 if (current > expired) {
873 DBG("expired by %d secs", (int)(current - expired));
877 dhcp->callback = callback;
879 if (T2 != 0xffffffff && T2 > 0 &&
880 (unsigned)current > (unsigned)last_rebind + T2) {
883 /* RFC 3315, chapter 18.1.3, start rebind */
884 if ((unsigned)current > (unsigned)last_renew + T1)
887 timeout = last_renew - current + T1;
890 * If we just did a renew, do not restart the rebind
893 dhcp->timeout = g_timeout_add_seconds(timeout, start_rebind,
896 DBG("renew after %d secs", T1);
898 dhcp->timeout = g_timeout_add_seconds(T1, start_renew, dhcp);
903 int __connman_dhcpv6_start_release(struct connman_network *network,
906 struct connman_dhcpv6 *dhcp;
907 GDHCPClient *dhcp_client;
909 if (network_table == NULL)
910 return 0; /* we are already released */
912 dhcp = g_hash_table_lookup(network_table, network);
916 DBG("network %p dhcp %p client %p stateless %d", network, dhcp,
917 dhcp->dhcp_client, dhcp->stateless);
919 if (dhcp->stateless == TRUE)
924 dhcp_client = dhcp->dhcp_client;
925 if (dhcp_client == NULL) {
927 * We had started the DHCPv6 handshaking i.e., we have called
928 * __connman_dhcpv6_start() but it has not yet sent
929 * a solicitation message to server. This means that we do not
930 * have DHCPv6 configured yet so we can just quit here.
932 DBG("DHCPv6 was not started");
936 g_dhcp_client_clear_requests(dhcp_client);
937 g_dhcp_client_clear_values(dhcp_client);
939 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
940 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
942 g_dhcpv6_client_set_ia(dhcp_client,
943 connman_network_get_index(dhcp->network),
944 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
947 clear_callbacks(dhcp_client);
950 * We do not register callback here because the answer might take too
951 * long time and network code might be in the middle of the disconnect.
952 * So we just inform the server that we are done with the addresses
953 * but ignore the reply from server. This is allowed by RFC 3315
957 dhcp->dhcp_client = dhcp_client;
959 return g_dhcp_client_start(dhcp_client, NULL);
962 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
964 DBG("dhcp %p", dhcp);
970 if (dhcp->dhcp_client == NULL)
973 g_dhcp_client_stop(dhcp->dhcp_client);
974 g_dhcp_client_unref(dhcp->dhcp_client);
976 dhcp->dhcp_client = NULL;
981 static void remove_network(gpointer user_data)
983 struct connman_dhcpv6 *dhcp = user_data;
985 DBG("dhcp %p", dhcp);
987 dhcpv6_release(dhcp);
992 static gboolean timeout_info_req(gpointer user_data)
994 struct connman_dhcpv6 *dhcp = user_data;
996 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
998 DBG("info RT timeout %d msec", dhcp->RT);
1000 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
1002 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1004 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1009 static gboolean start_info_req(gpointer user_data)
1011 struct connman_dhcpv6 *dhcp = user_data;
1013 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1014 dhcp->RT = INF_TIMEOUT * (1 + get_random());
1016 DBG("info initial RT timeout %d msec", dhcp->RT);
1018 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
1020 dhcpv6_info_request(dhcp);
1025 int __connman_dhcpv6_start_info(struct connman_network *network,
1028 struct connman_dhcpv6 *dhcp;
1033 if (network_table != NULL) {
1034 dhcp = g_hash_table_lookup(network_table, network);
1035 if (dhcp != NULL && dhcp->started == TRUE)
1039 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1043 dhcp->network = network;
1044 dhcp->callback = callback;
1045 dhcp->stateless = TRUE;
1046 dhcp->started = TRUE;
1048 connman_network_ref(network);
1050 DBG("replace network %p dhcp %p", network, dhcp);
1052 g_hash_table_replace(network_table, network, dhcp);
1054 /* Initial timeout, RFC 3315, 18.1.5 */
1055 delay = rand() % 1000;
1057 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
1062 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
1064 struct connman_dhcpv6 *dhcp = user_data;
1066 DBG("dhcpv6 advertise msg %p", dhcp);
1070 g_dhcpv6_client_clear_retransmit(dhcp_client);
1072 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
1073 if (dhcp->callback != NULL)
1074 dhcp->callback(dhcp->network, FALSE);
1078 dhcp->RT = REQ_TIMEOUT * (1 + get_random());
1079 DBG("request initial RT timeout %d msec", dhcp->RT);
1080 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
1082 dhcp->request_count = 1;
1084 dhcpv6_request(dhcp, TRUE);
1087 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
1089 /* We get here if server supports rapid commit */
1090 struct connman_dhcpv6 *dhcp = user_data;
1092 DBG("dhcpv6 solicitation msg %p", dhcp);
1096 set_addresses(dhcp_client, dhcp);
1098 g_dhcpv6_client_clear_retransmit(dhcp_client);
1101 static gboolean timeout_solicitation(gpointer user_data)
1103 struct connman_dhcpv6 *dhcp = user_data;
1105 dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
1107 DBG("solicit RT timeout %d msec", dhcp->RT);
1109 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1111 g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1113 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1118 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
1120 struct connman_service *service;
1121 struct connman_ipconfig *ipconfig_ipv6;
1122 GDHCPClient *dhcp_client;
1123 GDHCPClientError error;
1126 DBG("dhcp %p", dhcp);
1128 index = connman_network_get_index(dhcp->network);
1130 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
1131 if (error != G_DHCP_CLIENT_ERROR_NONE) {
1136 if (getenv("CONNMAN_DHCPV6_DEBUG"))
1137 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
1139 service = connman_service_lookup_from_network(dhcp->network);
1140 if (service == NULL) {
1142 g_dhcp_client_unref(dhcp_client);
1146 ret = set_duid(service, dhcp->network, dhcp_client, index);
1149 g_dhcp_client_unref(dhcp_client);
1153 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1154 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
1155 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1156 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1158 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
1159 G_DHCPV6_SNTP_SERVERS);
1161 ipconfig_ipv6 = __connman_service_get_ip6config(service);
1162 dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
1164 g_dhcpv6_client_set_ia(dhcp_client, index,
1165 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1168 clear_callbacks(dhcp_client);
1170 g_dhcp_client_register_event(dhcp_client,
1171 G_DHCP_CLIENT_EVENT_SOLICITATION,
1172 solicitation_cb, dhcp);
1174 g_dhcp_client_register_event(dhcp_client,
1175 G_DHCP_CLIENT_EVENT_ADVERTISE,
1176 advertise_cb, dhcp);
1178 dhcp->dhcp_client = dhcp_client;
1180 return g_dhcp_client_start(dhcp_client, NULL);
1183 static gboolean start_solicitation(gpointer user_data)
1185 struct connman_dhcpv6 *dhcp = user_data;
1187 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1188 dhcp->RT = SOL_TIMEOUT * (1 + get_random());
1190 DBG("solicit initial RT timeout %d msec", dhcp->RT);
1192 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1194 dhcpv6_solicitation(dhcp);
1199 int __connman_dhcpv6_start(struct connman_network *network,
1200 GSList *prefixes, dhcp_cb callback)
1202 struct connman_dhcpv6 *dhcp;
1207 if (network_table != NULL) {
1208 dhcp = g_hash_table_lookup(network_table, network);
1209 if (dhcp != NULL && dhcp->started == TRUE)
1213 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1217 dhcp->network = network;
1218 dhcp->callback = callback;
1219 dhcp->prefixes = prefixes;
1220 dhcp->started = TRUE;
1222 connman_network_ref(network);
1224 DBG("replace network %p dhcp %p", network, dhcp);
1226 g_hash_table_replace(network_table, network, dhcp);
1228 /* Initial timeout, RFC 3315, 17.1.2 */
1229 delay = rand() % 1000;
1231 dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
1236 void __connman_dhcpv6_stop(struct connman_network *network)
1240 if (network_table == NULL)
1243 if (g_hash_table_remove(network_table, network) == TRUE)
1244 connman_network_unref(network);
1247 int __connman_dhcpv6_init(void)
1253 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1254 NULL, remove_network);
1259 void __connman_dhcpv6_cleanup(void)
1263 g_hash_table_destroy(network_table);
1264 network_table = NULL;