5 * Copyright (C) 2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <connman/ipconfig.h>
31 #include <connman/storage.h>
33 #include <gdhcp/gdhcp.h>
39 /* Transmission params in msec, RFC 3315 chapter 5.5 */
40 #define INF_MAX_DELAY (1 * 1000)
41 #define INF_TIMEOUT (1 * 1000)
42 #define INF_MAX_RT (120 * 1000)
43 #define SOL_MAX_DELAY (1 * 1000)
44 #define SOL_TIMEOUT (1 * 1000)
45 #define SOL_MAX_RT (120 * 1000)
46 #define REQ_TIMEOUT (1 * 1000)
47 #define REQ_MAX_RT (30 * 1000)
49 #define REN_TIMEOUT (10 * 1000)
50 #define REN_MAX_RT (600 * 1000)
51 #define REB_TIMEOUT (10 * 1000)
52 #define REB_MAX_RT (600 * 1000)
55 struct connman_dhcpv6 {
56 struct connman_network *network;
62 GDHCPClient *dhcp_client;
64 guint timeout; /* operation timeout in msec */
65 guint RT; /* in msec */
66 gboolean use_ta; /* set to TRUE if IPv6 privacy is enabled */
67 GSList *prefixes; /* network prefixes from radvd */
68 int request_count; /* how many times REQUEST have been sent */
69 gboolean stateless; /* TRUE if stateless DHCPv6 is used */
72 static GHashTable *network_table;
74 static int dhcpv6_request(struct connman_dhcpv6 *dhcp, gboolean add_addresses);
76 static inline float get_random()
78 return (rand() % 200 - 100) / 1000.0;
81 /* Calculate a random delay, RFC 3315 chapter 14 */
82 /* RT and MRT are milliseconds */
83 static guint calc_delay(guint RT, guint MRT)
85 float delay = get_random();
86 float rt = RT * (2 + delay);
89 rt = MRT * (1 + delay);
97 static void free_prefix(gpointer data, gpointer user_data)
102 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
104 g_strfreev(dhcp->nameservers);
105 g_strfreev(dhcp->timeservers);
107 dhcp->nameservers = NULL;
108 dhcp->timeservers = NULL;
110 g_slist_foreach(dhcp->prefixes, free_prefix, NULL);
111 g_slist_free(dhcp->prefixes);
114 static gboolean compare_string_arrays(char **array_a, char **array_b)
118 if (array_a == NULL || array_b == NULL)
121 if (g_strv_length(array_a) != g_strv_length(array_b))
124 for (i = 0; array_a[i] != NULL && array_b[i] != NULL; i++)
125 if (g_strcmp0(array_a[i], array_b[i]) != 0)
131 static void dhcpv6_debug(const char *str, void *data)
133 connman_info("%s: %s\n", (const char *) data, str);
136 static gchar *convert_to_hex(unsigned char *buf, int len)
138 gchar *ret = g_try_malloc(len * 2 + 1);
141 for (i = 0; ret != NULL && i < len; i++)
142 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
148 * DUID should not change over time so save it to file.
149 * See RFC 3315 chapter 9 for details.
151 static int set_duid(struct connman_service *service,
152 struct connman_network *network,
153 GDHCPClient *dhcp_client, int index)
161 ident = __connman_service_get_ident(service);
163 keyfile = connman_storage_load_service(ident);
167 hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
169 if (hex_duid != NULL) {
170 unsigned int i, j = 0, hex;
171 size_t hex_duid_len = strlen(hex_duid);
173 duid = g_try_malloc0(hex_duid_len / 2);
175 g_key_file_free(keyfile);
180 for (i = 0; i < hex_duid_len; i += 2) {
181 sscanf(hex_duid + i, "%02x", &hex);
185 duid_len = hex_duid_len / 2;
188 int type = __connman_ipconfig_get_type_from_index(index);
190 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
193 g_key_file_free(keyfile);
197 hex_duid = convert_to_hex(duid, duid_len);
198 if (hex_duid == NULL) {
199 g_key_file_free(keyfile);
203 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
206 __connman_storage_save_service(keyfile, ident);
210 g_key_file_free(keyfile);
212 g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
217 static void clear_callbacks(GDHCPClient *dhcp_client)
219 g_dhcp_client_register_event(dhcp_client,
220 G_DHCP_CLIENT_EVENT_SOLICITATION,
223 g_dhcp_client_register_event(dhcp_client,
224 G_DHCP_CLIENT_EVENT_ADVERTISE,
227 g_dhcp_client_register_event(dhcp_client,
228 G_DHCP_CLIENT_EVENT_REQUEST,
231 g_dhcp_client_register_event(dhcp_client,
232 G_DHCP_CLIENT_EVENT_RENEW,
235 g_dhcp_client_register_event(dhcp_client,
236 G_DHCP_CLIENT_EVENT_REBIND,
239 g_dhcp_client_register_event(dhcp_client,
240 G_DHCP_CLIENT_EVENT_RELEASE,
243 g_dhcp_client_register_event(dhcp_client,
244 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
248 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
250 struct connman_dhcpv6 *dhcp = user_data;
251 struct connman_service *service;
253 GList *option, *list;
254 char **nameservers, **timeservers;
256 DBG("dhcpv6 information-request %p", dhcp);
258 service = __connman_service_lookup_from_network(dhcp->network);
259 if (service == NULL) {
260 connman_error("Can not lookup service");
264 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
265 entries = g_list_length(option);
267 nameservers = g_try_new0(char *, entries + 1);
268 if (nameservers != NULL) {
269 for (i = 0, list = option; list; list = list->next, i++)
270 nameservers[i] = g_strdup(list->data);
273 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
274 if (dhcp->nameservers != NULL) {
275 for (i = 0; dhcp->nameservers[i] != NULL; i++)
276 __connman_service_nameserver_remove(service,
277 dhcp->nameservers[i],
279 g_strfreev(dhcp->nameservers);
282 dhcp->nameservers = nameservers;
284 for (i = 0; dhcp->nameservers[i] != NULL; i++)
285 __connman_service_nameserver_append(service,
286 dhcp->nameservers[i],
289 g_strfreev(nameservers);
292 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
293 entries = g_list_length(option);
295 timeservers = g_try_new0(char *, entries + 1);
296 if (timeservers != NULL) {
297 for (i = 0, list = option; list; list = list->next, i++)
298 timeservers[i] = g_strdup(list->data);
301 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
302 if (dhcp->timeservers != NULL) {
303 for (i = 0; dhcp->timeservers[i] != NULL; i++)
304 __connman_service_timeserver_remove(service,
305 dhcp->timeservers[i]);
306 g_strfreev(dhcp->timeservers);
309 dhcp->timeservers = timeservers;
311 for (i = 0; dhcp->timeservers[i] != NULL; i++)
312 __connman_service_timeserver_append(service,
313 dhcp->timeservers[i]);
315 g_strfreev(timeservers);
318 if (dhcp->callback != NULL) {
319 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
320 dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
324 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
326 struct connman_service *service;
327 GDHCPClient *dhcp_client;
328 GDHCPClientError error;
331 DBG("dhcp %p", dhcp);
333 index = connman_network_get_index(dhcp->network);
335 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
336 if (error != G_DHCP_CLIENT_ERROR_NONE)
339 if (getenv("CONNMAN_DHCPV6_DEBUG"))
340 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
342 service = __connman_service_lookup_from_network(dhcp->network);
346 ret = set_duid(service, dhcp->network, dhcp_client, index);
350 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
351 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
352 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
354 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
355 G_DHCPV6_SNTP_SERVERS);
357 g_dhcp_client_register_event(dhcp_client,
358 G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
360 dhcp->dhcp_client = dhcp_client;
362 return g_dhcp_client_start(dhcp_client, NULL);
365 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
367 struct in6_addr addr_prefix, addr;
371 for (list = prefixes; list; list = list->next) {
372 char *prefix = list->data;
373 const char *slash = g_strrstr(prefix, "/");
374 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
375 0xF0, 0xE0, 0xC0, 0x80 };
376 int left, count, i, plen;
381 prefix = g_strndup(prefix, slash - prefix);
382 len = strtol(slash + 1, NULL, 10);
389 inet_pton(AF_INET6, prefix, &addr_prefix);
390 inet_pton(AF_INET6, address, &addr);
392 memset(&addr_prefix.s6_addr[i], 0, count);
393 memset(&addr.s6_addr[i], 0, count);
396 addr_prefix.s6_addr[i - 1] &= bits[left];
397 addr.s6_addr[i - 1] &= bits[left];
402 if (memcmp(&addr_prefix, &addr, 16) == 0) {
411 static int set_addresses(GDHCPClient *dhcp_client,
412 struct connman_dhcpv6 *dhcp)
414 struct connman_service *service;
415 struct connman_ipconfig *ipconfig;
417 GList *option, *list;
418 char **nameservers, **timeservers;
419 const char *c_address;
420 char *address = NULL;
422 service = __connman_service_lookup_from_network(dhcp->network);
423 if (service == NULL) {
424 connman_error("Can not lookup service");
428 ipconfig = __connman_service_get_ip6config(service);
429 if (ipconfig == NULL) {
430 connman_error("Could not lookup ip6config");
434 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
435 entries = g_list_length(option);
437 nameservers = g_try_new0(char *, entries + 1);
438 if (nameservers != NULL) {
439 for (i = 0, list = option; list; list = list->next, i++)
440 nameservers[i] = g_strdup(list->data);
443 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
444 if (dhcp->nameservers != NULL) {
445 for (i = 0; dhcp->nameservers[i] != NULL; i++)
446 __connman_service_nameserver_remove(service,
447 dhcp->nameservers[i],
449 g_strfreev(dhcp->nameservers);
452 dhcp->nameservers = nameservers;
454 for (i = 0; dhcp->nameservers[i] != NULL; i++)
455 __connman_service_nameserver_append(service,
456 dhcp->nameservers[i],
459 g_strfreev(nameservers);
462 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
463 entries = g_list_length(option);
465 timeservers = g_try_new0(char *, entries + 1);
466 if (timeservers != NULL) {
467 for (i = 0, list = option; list; list = list->next, i++)
468 timeservers[i] = g_strdup(list->data);
471 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
472 if (dhcp->timeservers != NULL) {
473 for (i = 0; dhcp->timeservers[i] != NULL; i++)
474 __connman_service_timeserver_remove(service,
475 dhcp->timeservers[i]);
476 g_strfreev(dhcp->timeservers);
479 dhcp->timeservers = timeservers;
481 for (i = 0; dhcp->timeservers[i] != NULL; i++)
482 __connman_service_timeserver_append(service,
483 dhcp->timeservers[i]);
485 g_strfreev(timeservers);
488 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
490 address = g_strdup(option->data);
492 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
494 address = g_strdup(option->data);
497 c_address = __connman_ipconfig_get_local(ipconfig);
499 if (address != NULL &&
500 ((c_address != NULL &&
501 g_strcmp0(address, c_address) != 0) ||
502 (c_address == NULL))) {
505 /* Is this prefix part of the subnet we are suppose to use? */
506 prefix_len = check_ipv6_addr_prefix(dhcp->prefixes, address);
508 __connman_ipconfig_set_local(ipconfig, address);
509 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
511 DBG("new address %s/%d", address, prefix_len);
517 static void re_cb(GDHCPClient *dhcp_client, gpointer user_data)
519 struct connman_dhcpv6 *dhcp = user_data;
523 ret = set_addresses(dhcp_client, dhcp);
525 status = g_dhcpv6_client_get_status(dhcp_client);
527 DBG("dhcpv6 cb msg %p ret %d status %d", dhcp, ret, status);
530 if (dhcp->callback != NULL)
531 dhcp->callback(dhcp->network, FALSE);
535 if (status == G_DHCPV6_ERROR_BINDING) {
536 /* RFC 3315, 18.1.8 */
537 dhcpv6_request(dhcp, FALSE);
539 if (dhcp->callback != NULL)
540 dhcp->callback(dhcp->network,
541 status == 0 ? TRUE : FALSE);
545 static void rebind_cb(GDHCPClient *dhcp_client, gpointer user_data)
549 g_dhcpv6_client_reset_rebind(dhcp_client);
550 g_dhcpv6_client_reset_renew(dhcp_client);
552 re_cb(dhcp_client, user_data);
555 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp)
557 GDHCPClient *dhcp_client;
559 DBG("dhcp %p", dhcp);
561 dhcp_client = dhcp->dhcp_client;
563 g_dhcp_client_clear_requests(dhcp_client);
565 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
566 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
567 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
569 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
570 G_DHCPV6_SNTP_SERVERS);
572 g_dhcpv6_client_set_ia(dhcp_client,
573 connman_network_get_index(dhcp->network),
574 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
577 clear_callbacks(dhcp_client);
579 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
582 dhcp->dhcp_client = dhcp_client;
584 return g_dhcp_client_start(dhcp_client, NULL);
587 static gboolean dhcpv6_restart(gpointer user_data)
589 struct connman_dhcpv6 *dhcp = user_data;
591 if (dhcp->callback != NULL)
592 dhcp->callback(dhcp->network, FALSE);
598 * Check if we need to restart the solicitation procedure. This
599 * is done if all the addresses have expired. RFC 3315, 18.1.4
601 static int check_restart(struct connman_dhcpv6 *dhcp)
603 time_t current, expired;
605 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
606 NULL, NULL, &expired);
609 if (current > expired) {
610 DBG("expired by %d secs", (int)(current - expired));
612 g_timeout_add(0, dhcpv6_restart, dhcp);
620 static gboolean timeout_rebind(gpointer user_data)
622 struct connman_dhcpv6 *dhcp = user_data;
624 if (check_restart(dhcp) < 0)
627 dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
629 DBG("rebind RT timeout %d msec", dhcp->RT);
631 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
633 g_dhcp_client_start(dhcp->dhcp_client, NULL);
638 static gboolean start_rebind(gpointer user_data)
640 struct connman_dhcpv6 *dhcp = user_data;
642 dhcp->RT = REB_TIMEOUT * (1 + get_random());
644 DBG("rebind initial RT timeout %d msec", dhcp->RT);
646 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
653 static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
657 re_cb(dhcp_client, user_data);
660 static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
661 gboolean add_addresses)
663 GDHCPClient *dhcp_client;
666 DBG("dhcp %p add %d", dhcp, add_addresses);
668 dhcp_client = dhcp->dhcp_client;
670 g_dhcp_client_clear_requests(dhcp_client);
672 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
673 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
674 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
675 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
677 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
678 G_DHCPV6_SNTP_SERVERS);
680 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
681 g_dhcpv6_client_set_ia(dhcp_client,
682 connman_network_get_index(dhcp->network),
683 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
684 &T1, &T2, add_addresses);
686 clear_callbacks(dhcp_client);
688 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
691 dhcp->dhcp_client = dhcp_client;
693 return g_dhcp_client_start(dhcp_client, NULL);
696 static gboolean timeout_request(gpointer user_data)
698 struct connman_dhcpv6 *dhcp = user_data;
700 if (dhcp->request_count >= REQ_MAX_RC) {
701 DBG("max request retry attempts %d", dhcp->request_count);
702 dhcp->request_count = 0;
703 if (dhcp->callback != NULL)
704 dhcp->callback(dhcp->network, FALSE);
708 dhcp->request_count++;
710 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
711 DBG("request RT timeout %d msec", dhcp->RT);
712 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
714 g_dhcp_client_start(dhcp->dhcp_client, NULL);
719 static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
723 g_dhcpv6_client_reset_renew(dhcp_client);
725 re_cb(dhcp_client, user_data);
728 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
730 GDHCPClient *dhcp_client;
733 DBG("dhcp %p", dhcp);
735 dhcp_client = dhcp->dhcp_client;
737 g_dhcp_client_clear_requests(dhcp_client);
739 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
740 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
741 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
742 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
744 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
745 G_DHCPV6_SNTP_SERVERS);
747 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
748 g_dhcpv6_client_set_ia(dhcp_client,
749 connman_network_get_index(dhcp->network),
750 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
753 clear_callbacks(dhcp_client);
755 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
758 dhcp->dhcp_client = dhcp_client;
760 return g_dhcp_client_start(dhcp_client, NULL);
763 static gboolean timeout_renew(gpointer user_data)
765 struct connman_dhcpv6 *dhcp = user_data;
767 if (check_restart(dhcp) < 0)
770 dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
772 DBG("renew RT timeout %d msec", dhcp->RT);
774 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
776 g_dhcp_client_start(dhcp->dhcp_client, NULL);
781 static gboolean start_renew(gpointer user_data)
783 struct connman_dhcpv6 *dhcp = user_data;
785 dhcp->RT = REN_TIMEOUT * (1 + get_random());
787 DBG("renew initial RT timeout %d msec", dhcp->RT);
789 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
796 int __connman_dhcpv6_start_renew(struct connman_network *network,
799 struct connman_dhcpv6 *dhcp;
801 time_t last_renew, last_rebind, current, expired;
803 dhcp = g_hash_table_lookup(network_table, network);
807 if (dhcp->timeout > 0) {
808 g_source_remove(dhcp->timeout);
812 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
813 &last_renew, &last_rebind, &expired);
817 DBG("T1 %u T2 %u expires %lu current %lu", T1, T2,
818 (unsigned long)expired, current);
820 if (T1 == 0xffffffff)
826 * Client can choose the timeout.
830 /* RFC 3315, 18.1.4, start solicit if expired */
831 if (current > expired) {
832 DBG("expired by %d secs", (int)(current - expired));
836 dhcp->callback = callback;
838 if (T2 != 0xffffffff && T2 > 0 &&
839 (unsigned)current > (unsigned)last_rebind + T2) {
842 /* RFC 3315, chapter 18.1.3, start rebind */
843 if ((unsigned)current > (unsigned)last_renew + T1)
846 timeout = last_renew - current + T1;
849 * If we just did a renew, do not restart the rebind
852 dhcp->timeout = g_timeout_add_seconds(timeout, start_rebind,
855 DBG("renew after %d secs", T1);
857 dhcp->timeout = g_timeout_add_seconds(T1, start_renew, dhcp);
862 int __connman_dhcpv6_start_release(struct connman_network *network,
865 struct connman_dhcpv6 *dhcp;
866 GDHCPClient *dhcp_client;
868 if (network_table == NULL)
869 return 0; /* we are already released */
871 dhcp = g_hash_table_lookup(network_table, network);
875 DBG("dhcp %p stateless %d", dhcp, dhcp->stateless);
877 if (dhcp->stateless == TRUE)
880 if (dhcp->timeout > 0) {
881 g_source_remove(dhcp->timeout);
885 dhcp_client = dhcp->dhcp_client;
887 g_dhcp_client_clear_requests(dhcp_client);
888 g_dhcp_client_clear_values(dhcp_client);
890 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
891 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
893 g_dhcpv6_client_set_ia(dhcp_client,
894 connman_network_get_index(dhcp->network),
895 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
898 clear_callbacks(dhcp_client);
901 * We do not register callback here because the answer might take too
902 * long time and network code might be in the middle of the disconnect.
903 * So we just inform the server that we are done with the addresses
904 * but ignore the reply from server. This is allowed by RFC 3315
908 dhcp->dhcp_client = dhcp_client;
910 return g_dhcp_client_start(dhcp_client, NULL);
913 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
915 DBG("dhcp %p", dhcp);
917 if (dhcp->timeout > 0) {
918 g_source_remove(dhcp->timeout);
924 if (dhcp->dhcp_client == NULL)
927 g_dhcp_client_stop(dhcp->dhcp_client);
928 g_dhcp_client_unref(dhcp->dhcp_client);
930 dhcp->dhcp_client = NULL;
935 static void remove_network(gpointer user_data)
937 struct connman_dhcpv6 *dhcp = user_data;
939 DBG("dhcp %p", dhcp);
941 dhcpv6_release(dhcp);
946 static gboolean timeout_info_req(gpointer user_data)
948 struct connman_dhcpv6 *dhcp = user_data;
950 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
952 DBG("info RT timeout %d msec", dhcp->RT);
954 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
956 g_dhcp_client_start(dhcp->dhcp_client, NULL);
961 static gboolean start_info_req(gpointer user_data)
963 struct connman_dhcpv6 *dhcp = user_data;
965 /* Set the retransmission timeout, RFC 3315 chapter 14 */
966 dhcp->RT = INF_TIMEOUT * (1 + get_random());
968 DBG("info initial RT timeout %d msec", dhcp->RT);
970 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
972 dhcpv6_info_request(dhcp);
977 int __connman_dhcpv6_start_info(struct connman_network *network,
980 struct connman_dhcpv6 *dhcp;
985 dhcp = g_try_new0(struct connman_dhcpv6, 1);
989 dhcp->network = network;
990 dhcp->callback = callback;
991 dhcp->stateless = TRUE;
993 connman_network_ref(network);
995 g_hash_table_replace(network_table, network, dhcp);
997 /* Initial timeout, RFC 3315, 18.1.5 */
998 delay = rand() % 1000;
1000 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
1005 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
1007 struct connman_dhcpv6 *dhcp = user_data;
1009 DBG("dhcpv6 advertise msg %p", dhcp);
1011 if (dhcp->timeout > 0) {
1012 g_source_remove(dhcp->timeout);
1016 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
1017 if (dhcp->callback != NULL)
1018 dhcp->callback(dhcp->network, FALSE);
1022 dhcp->RT = REQ_TIMEOUT * (1 + get_random());
1023 DBG("request initial RT timeout %d msec", dhcp->RT);
1024 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
1026 dhcp->request_count = 1;
1028 dhcpv6_request(dhcp, TRUE);
1031 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
1033 /* We get here if server supports rapid commit */
1034 struct connman_dhcpv6 *dhcp = user_data;
1036 DBG("dhcpv6 solicitation msg %p", dhcp);
1038 if (dhcp->timeout > 0) {
1039 g_source_remove(dhcp->timeout);
1043 set_addresses(dhcp_client, dhcp);
1046 static gboolean timeout_solicitation(gpointer user_data)
1048 struct connman_dhcpv6 *dhcp = user_data;
1050 dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
1052 DBG("solicit RT timeout %d msec", dhcp->RT);
1054 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1056 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1061 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
1063 struct connman_service *service;
1064 struct connman_ipconfig *ipconfig_ipv6;
1065 GDHCPClient *dhcp_client;
1066 GDHCPClientError error;
1069 DBG("dhcp %p", dhcp);
1071 index = connman_network_get_index(dhcp->network);
1073 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
1074 if (error != G_DHCP_CLIENT_ERROR_NONE)
1077 if (getenv("CONNMAN_DHCPV6_DEBUG"))
1078 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
1080 service = __connman_service_lookup_from_network(dhcp->network);
1081 if (service == NULL)
1084 ret = set_duid(service, dhcp->network, dhcp_client, index);
1088 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1089 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
1090 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1091 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1093 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
1094 G_DHCPV6_SNTP_SERVERS);
1096 ipconfig_ipv6 = __connman_service_get_ip6config(service);
1097 dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
1099 g_dhcpv6_client_set_ia(dhcp_client, index,
1100 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1103 clear_callbacks(dhcp_client);
1105 g_dhcp_client_register_event(dhcp_client,
1106 G_DHCP_CLIENT_EVENT_SOLICITATION,
1107 solicitation_cb, dhcp);
1109 g_dhcp_client_register_event(dhcp_client,
1110 G_DHCP_CLIENT_EVENT_ADVERTISE,
1111 advertise_cb, dhcp);
1113 dhcp->dhcp_client = dhcp_client;
1115 return g_dhcp_client_start(dhcp_client, NULL);
1118 static gboolean start_solicitation(gpointer user_data)
1120 struct connman_dhcpv6 *dhcp = user_data;
1122 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1123 dhcp->RT = SOL_TIMEOUT * (1 + get_random());
1125 DBG("solicit initial RT timeout %d msec", dhcp->RT);
1127 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1129 dhcpv6_solicitation(dhcp);
1134 int __connman_dhcpv6_start(struct connman_network *network,
1135 GSList *prefixes, dhcp_cb callback)
1137 struct connman_dhcpv6 *dhcp;
1142 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1146 dhcp->network = network;
1147 dhcp->callback = callback;
1148 dhcp->prefixes = prefixes;
1150 connman_network_ref(network);
1152 g_hash_table_replace(network_table, network, dhcp);
1154 /* Initial timeout, RFC 3315, 17.1.2 */
1155 delay = rand() % 1000;
1157 dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
1162 void __connman_dhcpv6_stop(struct connman_network *network)
1166 if (network_table == NULL)
1169 if (g_hash_table_remove(network_table, network) == TRUE)
1170 connman_network_unref(network);
1173 int __connman_dhcpv6_init(void)
1179 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1180 NULL, remove_network);
1185 void __connman_dhcpv6_cleanup(void)
1189 g_hash_table_destroy(network_table);
1190 network_table = NULL;