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 */
70 gboolean started; /* TRUE if we have DHCPv6 started */
73 static GHashTable *network_table;
75 static int dhcpv6_request(struct connman_dhcpv6 *dhcp, gboolean add_addresses);
77 static inline float get_random()
79 return (rand() % 200 - 100) / 1000.0;
82 /* Calculate a random delay, RFC 3315 chapter 14 */
83 /* RT and MRT are milliseconds */
84 static guint calc_delay(guint RT, guint MRT)
86 float delay = get_random();
87 float rt = RT * (2 + delay);
90 rt = MRT * (1 + delay);
98 static void free_prefix(gpointer data, gpointer user_data)
103 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
105 g_strfreev(dhcp->nameservers);
106 g_strfreev(dhcp->timeservers);
108 dhcp->nameservers = NULL;
109 dhcp->timeservers = NULL;
110 dhcp->started = FALSE;
112 g_slist_foreach(dhcp->prefixes, free_prefix, NULL);
113 g_slist_free(dhcp->prefixes);
116 static gboolean compare_string_arrays(char **array_a, char **array_b)
120 if (array_a == NULL || array_b == NULL)
123 if (g_strv_length(array_a) != g_strv_length(array_b))
126 for (i = 0; array_a[i] != NULL && array_b[i] != NULL; i++)
127 if (g_strcmp0(array_a[i], array_b[i]) != 0)
133 static void dhcpv6_debug(const char *str, void *data)
135 connman_info("%s: %s\n", (const char *) data, str);
138 static gchar *convert_to_hex(unsigned char *buf, int len)
140 gchar *ret = g_try_malloc(len * 2 + 1);
143 for (i = 0; ret != NULL && i < len; i++)
144 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
150 * DUID should not change over time so save it to file.
151 * See RFC 3315 chapter 9 for details.
153 static int set_duid(struct connman_service *service,
154 struct connman_network *network,
155 GDHCPClient *dhcp_client, int index)
163 ident = __connman_service_get_ident(service);
165 keyfile = connman_storage_load_service(ident);
169 hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
171 if (hex_duid != NULL) {
172 unsigned int i, j = 0, hex;
173 size_t hex_duid_len = strlen(hex_duid);
175 duid = g_try_malloc0(hex_duid_len / 2);
177 g_key_file_free(keyfile);
182 for (i = 0; i < hex_duid_len; i += 2) {
183 sscanf(hex_duid + i, "%02x", &hex);
187 duid_len = hex_duid_len / 2;
190 int type = __connman_ipconfig_get_type_from_index(index);
192 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
195 g_key_file_free(keyfile);
199 hex_duid = convert_to_hex(duid, duid_len);
200 if (hex_duid == NULL) {
201 g_key_file_free(keyfile);
205 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
208 __connman_storage_save_service(keyfile, ident);
212 g_key_file_free(keyfile);
214 g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
219 static void clear_callbacks(GDHCPClient *dhcp_client)
221 g_dhcp_client_register_event(dhcp_client,
222 G_DHCP_CLIENT_EVENT_SOLICITATION,
225 g_dhcp_client_register_event(dhcp_client,
226 G_DHCP_CLIENT_EVENT_ADVERTISE,
229 g_dhcp_client_register_event(dhcp_client,
230 G_DHCP_CLIENT_EVENT_REQUEST,
233 g_dhcp_client_register_event(dhcp_client,
234 G_DHCP_CLIENT_EVENT_RENEW,
237 g_dhcp_client_register_event(dhcp_client,
238 G_DHCP_CLIENT_EVENT_REBIND,
241 g_dhcp_client_register_event(dhcp_client,
242 G_DHCP_CLIENT_EVENT_RELEASE,
245 g_dhcp_client_register_event(dhcp_client,
246 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
250 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
252 struct connman_dhcpv6 *dhcp = user_data;
253 struct connman_service *service;
255 GList *option, *list;
256 char **nameservers, **timeservers;
258 DBG("dhcpv6 information-request %p", dhcp);
260 service = __connman_service_lookup_from_network(dhcp->network);
261 if (service == NULL) {
262 connman_error("Can not lookup service");
266 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
267 entries = g_list_length(option);
269 nameservers = g_try_new0(char *, entries + 1);
270 if (nameservers != NULL) {
271 for (i = 0, list = option; list; list = list->next, i++)
272 nameservers[i] = g_strdup(list->data);
275 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
276 if (dhcp->nameservers != NULL) {
277 for (i = 0; dhcp->nameservers[i] != NULL; i++)
278 __connman_service_nameserver_remove(service,
279 dhcp->nameservers[i],
281 g_strfreev(dhcp->nameservers);
284 dhcp->nameservers = nameservers;
286 for (i = 0; dhcp->nameservers[i] != NULL; i++)
287 __connman_service_nameserver_append(service,
288 dhcp->nameservers[i],
291 g_strfreev(nameservers);
294 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
295 entries = g_list_length(option);
297 timeservers = g_try_new0(char *, entries + 1);
298 if (timeservers != NULL) {
299 for (i = 0, list = option; list; list = list->next, i++)
300 timeservers[i] = g_strdup(list->data);
303 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
304 if (dhcp->timeservers != NULL) {
305 for (i = 0; dhcp->timeservers[i] != NULL; i++)
306 __connman_service_timeserver_remove(service,
307 dhcp->timeservers[i]);
308 g_strfreev(dhcp->timeservers);
311 dhcp->timeservers = timeservers;
313 for (i = 0; dhcp->timeservers[i] != NULL; i++)
314 __connman_service_timeserver_append(service,
315 dhcp->timeservers[i]);
317 g_strfreev(timeservers);
320 if (dhcp->callback != NULL) {
321 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
322 dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
326 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
328 struct connman_service *service;
329 GDHCPClient *dhcp_client;
330 GDHCPClientError error;
333 DBG("dhcp %p", dhcp);
335 index = connman_network_get_index(dhcp->network);
337 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
338 if (error != G_DHCP_CLIENT_ERROR_NONE)
341 if (getenv("CONNMAN_DHCPV6_DEBUG"))
342 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
344 service = __connman_service_lookup_from_network(dhcp->network);
348 ret = set_duid(service, dhcp->network, dhcp_client, index);
352 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
353 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
354 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
356 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
357 G_DHCPV6_SNTP_SERVERS);
359 g_dhcp_client_register_event(dhcp_client,
360 G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
362 dhcp->dhcp_client = dhcp_client;
364 return g_dhcp_client_start(dhcp_client, NULL);
367 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
369 struct in6_addr addr_prefix, addr;
373 for (list = prefixes; list; list = list->next) {
374 char *prefix = list->data;
375 const char *slash = g_strrstr(prefix, "/");
376 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
377 0xF0, 0xE0, 0xC0, 0x80 };
378 int left, count, i, plen;
383 prefix = g_strndup(prefix, slash - prefix);
384 len = strtol(slash + 1, NULL, 10);
391 inet_pton(AF_INET6, prefix, &addr_prefix);
392 inet_pton(AF_INET6, address, &addr);
394 memset(&addr_prefix.s6_addr[i], 0, count);
395 memset(&addr.s6_addr[i], 0, count);
398 addr_prefix.s6_addr[i - 1] &= bits[left];
399 addr.s6_addr[i - 1] &= bits[left];
404 if (memcmp(&addr_prefix, &addr, 16) == 0) {
413 static int set_addresses(GDHCPClient *dhcp_client,
414 struct connman_dhcpv6 *dhcp)
416 struct connman_service *service;
417 struct connman_ipconfig *ipconfig;
419 GList *option, *list;
420 char **nameservers, **timeservers;
421 const char *c_address;
422 char *address = NULL;
424 service = __connman_service_lookup_from_network(dhcp->network);
425 if (service == NULL) {
426 connman_error("Can not lookup service");
430 ipconfig = __connman_service_get_ip6config(service);
431 if (ipconfig == NULL) {
432 connman_error("Could not lookup ip6config");
436 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
437 entries = g_list_length(option);
439 nameservers = g_try_new0(char *, entries + 1);
440 if (nameservers != NULL) {
441 for (i = 0, list = option; list; list = list->next, i++)
442 nameservers[i] = g_strdup(list->data);
445 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
446 if (dhcp->nameservers != NULL) {
447 for (i = 0; dhcp->nameservers[i] != NULL; i++)
448 __connman_service_nameserver_remove(service,
449 dhcp->nameservers[i],
451 g_strfreev(dhcp->nameservers);
454 dhcp->nameservers = nameservers;
456 for (i = 0; dhcp->nameservers[i] != NULL; i++)
457 __connman_service_nameserver_append(service,
458 dhcp->nameservers[i],
461 g_strfreev(nameservers);
464 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
465 entries = g_list_length(option);
467 timeservers = g_try_new0(char *, entries + 1);
468 if (timeservers != NULL) {
469 for (i = 0, list = option; list; list = list->next, i++)
470 timeservers[i] = g_strdup(list->data);
473 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
474 if (dhcp->timeservers != NULL) {
475 for (i = 0; dhcp->timeservers[i] != NULL; i++)
476 __connman_service_timeserver_remove(service,
477 dhcp->timeservers[i]);
478 g_strfreev(dhcp->timeservers);
481 dhcp->timeservers = timeservers;
483 for (i = 0; dhcp->timeservers[i] != NULL; i++)
484 __connman_service_timeserver_append(service,
485 dhcp->timeservers[i]);
487 g_strfreev(timeservers);
490 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
492 address = g_strdup(option->data);
494 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
496 address = g_strdup(option->data);
499 c_address = __connman_ipconfig_get_local(ipconfig);
501 if (address != NULL &&
502 ((c_address != NULL &&
503 g_strcmp0(address, c_address) != 0) ||
504 (c_address == NULL))) {
507 /* Is this prefix part of the subnet we are suppose to use? */
508 prefix_len = check_ipv6_addr_prefix(dhcp->prefixes, address);
510 __connman_ipconfig_set_local(ipconfig, address);
511 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
513 DBG("new address %s/%d", address, prefix_len);
519 static void re_cb(GDHCPClient *dhcp_client, gpointer user_data)
521 struct connman_dhcpv6 *dhcp = user_data;
525 ret = set_addresses(dhcp_client, dhcp);
527 status = g_dhcpv6_client_get_status(dhcp_client);
529 DBG("dhcpv6 cb msg %p ret %d status %d", dhcp, ret, status);
532 if (dhcp->callback != NULL)
533 dhcp->callback(dhcp->network, FALSE);
537 if (status == G_DHCPV6_ERROR_BINDING) {
538 /* RFC 3315, 18.1.8 */
539 dhcpv6_request(dhcp, FALSE);
541 if (dhcp->callback != NULL)
542 dhcp->callback(dhcp->network,
543 status == 0 ? TRUE : FALSE);
547 static void rebind_cb(GDHCPClient *dhcp_client, gpointer user_data)
551 g_dhcpv6_client_reset_rebind(dhcp_client);
552 g_dhcpv6_client_reset_renew(dhcp_client);
554 re_cb(dhcp_client, user_data);
557 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp)
559 GDHCPClient *dhcp_client;
561 DBG("dhcp %p", dhcp);
563 dhcp_client = dhcp->dhcp_client;
565 g_dhcp_client_clear_requests(dhcp_client);
567 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
568 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
569 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
571 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
572 G_DHCPV6_SNTP_SERVERS);
574 g_dhcpv6_client_set_ia(dhcp_client,
575 connman_network_get_index(dhcp->network),
576 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
579 clear_callbacks(dhcp_client);
581 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
584 dhcp->dhcp_client = dhcp_client;
586 return g_dhcp_client_start(dhcp_client, NULL);
589 static gboolean dhcpv6_restart(gpointer user_data)
591 struct connman_dhcpv6 *dhcp = user_data;
593 if (dhcp->callback != NULL)
594 dhcp->callback(dhcp->network, FALSE);
600 * Check if we need to restart the solicitation procedure. This
601 * is done if all the addresses have expired. RFC 3315, 18.1.4
603 static int check_restart(struct connman_dhcpv6 *dhcp)
605 time_t current, expired;
607 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
608 NULL, NULL, &expired);
609 current = time(NULL);
611 if (current > expired) {
612 DBG("expired by %d secs", (int)(current - expired));
614 g_timeout_add(0, dhcpv6_restart, dhcp);
622 static gboolean timeout_rebind(gpointer user_data)
624 struct connman_dhcpv6 *dhcp = user_data;
626 if (check_restart(dhcp) < 0)
629 dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
631 DBG("rebind RT timeout %d msec", dhcp->RT);
633 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
635 g_dhcp_client_start(dhcp->dhcp_client, NULL);
640 static gboolean start_rebind(gpointer user_data)
642 struct connman_dhcpv6 *dhcp = user_data;
644 dhcp->RT = REB_TIMEOUT * (1 + get_random());
646 DBG("rebind initial RT timeout %d msec", dhcp->RT);
648 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
655 static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
659 re_cb(dhcp_client, user_data);
662 static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
663 gboolean add_addresses)
665 GDHCPClient *dhcp_client;
668 DBG("dhcp %p add %d", dhcp, add_addresses);
670 dhcp_client = dhcp->dhcp_client;
672 g_dhcp_client_clear_requests(dhcp_client);
674 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
675 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
676 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
677 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
679 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
680 G_DHCPV6_SNTP_SERVERS);
682 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
683 g_dhcpv6_client_set_ia(dhcp_client,
684 connman_network_get_index(dhcp->network),
685 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
686 &T1, &T2, add_addresses);
688 clear_callbacks(dhcp_client);
690 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
693 dhcp->dhcp_client = dhcp_client;
695 return g_dhcp_client_start(dhcp_client, NULL);
698 static gboolean timeout_request(gpointer user_data)
700 struct connman_dhcpv6 *dhcp = user_data;
702 if (dhcp->request_count >= REQ_MAX_RC) {
703 DBG("max request retry attempts %d", dhcp->request_count);
704 dhcp->request_count = 0;
705 if (dhcp->callback != NULL)
706 dhcp->callback(dhcp->network, FALSE);
710 dhcp->request_count++;
712 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
713 DBG("request RT timeout %d msec", dhcp->RT);
714 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
716 g_dhcp_client_start(dhcp->dhcp_client, NULL);
721 static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
725 g_dhcpv6_client_reset_renew(dhcp_client);
727 re_cb(dhcp_client, user_data);
730 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
732 GDHCPClient *dhcp_client;
735 DBG("dhcp %p", dhcp);
737 dhcp_client = dhcp->dhcp_client;
739 g_dhcp_client_clear_requests(dhcp_client);
741 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
742 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
743 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
744 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
746 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
747 G_DHCPV6_SNTP_SERVERS);
749 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
750 g_dhcpv6_client_set_ia(dhcp_client,
751 connman_network_get_index(dhcp->network),
752 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
755 clear_callbacks(dhcp_client);
757 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
760 dhcp->dhcp_client = dhcp_client;
762 return g_dhcp_client_start(dhcp_client, NULL);
765 static gboolean timeout_renew(gpointer user_data)
767 struct connman_dhcpv6 *dhcp = user_data;
769 if (check_restart(dhcp) < 0)
772 dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
774 DBG("renew RT timeout %d msec", dhcp->RT);
776 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
778 g_dhcp_client_start(dhcp->dhcp_client, NULL);
783 static gboolean start_renew(gpointer user_data)
785 struct connman_dhcpv6 *dhcp = user_data;
787 dhcp->RT = REN_TIMEOUT * (1 + get_random());
789 DBG("renew initial RT timeout %d msec", dhcp->RT);
791 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
798 int __connman_dhcpv6_start_renew(struct connman_network *network,
801 struct connman_dhcpv6 *dhcp;
803 time_t last_renew, last_rebind, current, expired;
805 dhcp = g_hash_table_lookup(network_table, network);
809 DBG("network %p dhcp %p", network, dhcp);
811 if (dhcp->timeout > 0) {
812 g_source_remove(dhcp->timeout);
816 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
817 &last_renew, &last_rebind, &expired);
819 current = time(NULL);
821 DBG("T1 %u T2 %u expires %lu current %lu", T1, T2,
822 (unsigned long)expired, current);
824 if (T1 == 0xffffffff)
830 * Client can choose the timeout.
834 /* RFC 3315, 18.1.4, start solicit if expired */
835 if (current > expired) {
836 DBG("expired by %d secs", (int)(current - expired));
840 dhcp->callback = callback;
842 if (T2 != 0xffffffff && T2 > 0 &&
843 (unsigned)current > (unsigned)last_rebind + T2) {
846 /* RFC 3315, chapter 18.1.3, start rebind */
847 if ((unsigned)current > (unsigned)last_renew + T1)
850 timeout = last_renew - current + T1;
853 * If we just did a renew, do not restart the rebind
856 dhcp->timeout = g_timeout_add_seconds(timeout, start_rebind,
859 DBG("renew after %d secs", T1);
861 dhcp->timeout = g_timeout_add_seconds(T1, start_renew, dhcp);
866 int __connman_dhcpv6_start_release(struct connman_network *network,
869 struct connman_dhcpv6 *dhcp;
870 GDHCPClient *dhcp_client;
872 if (network_table == NULL)
873 return 0; /* we are already released */
875 dhcp = g_hash_table_lookup(network_table, network);
879 DBG("network %p dhcp %p client %p stateless %d", network, dhcp,
880 dhcp->dhcp_client, dhcp->stateless);
882 if (dhcp->stateless == TRUE)
885 if (dhcp->timeout > 0) {
886 g_source_remove(dhcp->timeout);
890 dhcp_client = dhcp->dhcp_client;
891 if (dhcp_client == NULL) {
893 * We had started the DHCPv6 handshaking i.e., we have called
894 * __connman_dhcpv6_start() but it has not yet sent
895 * a solicitation message to server. This means that we do not
896 * have DHCPv6 configured yet so we can just quit here.
898 DBG("DHCPv6 was not started");
902 g_dhcp_client_clear_requests(dhcp_client);
903 g_dhcp_client_clear_values(dhcp_client);
905 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
906 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
908 g_dhcpv6_client_set_ia(dhcp_client,
909 connman_network_get_index(dhcp->network),
910 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
913 clear_callbacks(dhcp_client);
916 * We do not register callback here because the answer might take too
917 * long time and network code might be in the middle of the disconnect.
918 * So we just inform the server that we are done with the addresses
919 * but ignore the reply from server. This is allowed by RFC 3315
923 dhcp->dhcp_client = dhcp_client;
925 return g_dhcp_client_start(dhcp_client, NULL);
928 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
930 DBG("dhcp %p", dhcp);
932 if (dhcp->timeout > 0) {
933 g_source_remove(dhcp->timeout);
939 if (dhcp->dhcp_client == NULL)
942 g_dhcp_client_stop(dhcp->dhcp_client);
943 g_dhcp_client_unref(dhcp->dhcp_client);
945 dhcp->dhcp_client = NULL;
950 static void remove_network(gpointer user_data)
952 struct connman_dhcpv6 *dhcp = user_data;
954 DBG("dhcp %p", dhcp);
956 dhcpv6_release(dhcp);
961 static gboolean timeout_info_req(gpointer user_data)
963 struct connman_dhcpv6 *dhcp = user_data;
965 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
967 DBG("info RT timeout %d msec", dhcp->RT);
969 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
971 g_dhcp_client_start(dhcp->dhcp_client, NULL);
976 static gboolean start_info_req(gpointer user_data)
978 struct connman_dhcpv6 *dhcp = user_data;
980 /* Set the retransmission timeout, RFC 3315 chapter 14 */
981 dhcp->RT = INF_TIMEOUT * (1 + get_random());
983 DBG("info initial RT timeout %d msec", dhcp->RT);
985 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
987 dhcpv6_info_request(dhcp);
992 int __connman_dhcpv6_start_info(struct connman_network *network,
995 struct connman_dhcpv6 *dhcp;
1000 if (network_table != NULL) {
1001 dhcp = g_hash_table_lookup(network_table, network);
1002 if (dhcp != NULL && dhcp->started == TRUE)
1006 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1010 dhcp->network = network;
1011 dhcp->callback = callback;
1012 dhcp->stateless = TRUE;
1013 dhcp->started = TRUE;
1015 connman_network_ref(network);
1017 DBG("replace network %p dhcp %p", network, dhcp);
1019 g_hash_table_replace(network_table, network, dhcp);
1021 /* Initial timeout, RFC 3315, 18.1.5 */
1022 delay = rand() % 1000;
1024 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
1029 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
1031 struct connman_dhcpv6 *dhcp = user_data;
1033 DBG("dhcpv6 advertise msg %p", dhcp);
1035 if (dhcp->timeout > 0) {
1036 g_source_remove(dhcp->timeout);
1040 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
1041 if (dhcp->callback != NULL)
1042 dhcp->callback(dhcp->network, FALSE);
1046 dhcp->RT = REQ_TIMEOUT * (1 + get_random());
1047 DBG("request initial RT timeout %d msec", dhcp->RT);
1048 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
1050 dhcp->request_count = 1;
1052 dhcpv6_request(dhcp, TRUE);
1055 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
1057 /* We get here if server supports rapid commit */
1058 struct connman_dhcpv6 *dhcp = user_data;
1060 DBG("dhcpv6 solicitation msg %p", dhcp);
1062 if (dhcp->timeout > 0) {
1063 g_source_remove(dhcp->timeout);
1067 set_addresses(dhcp_client, dhcp);
1070 static gboolean timeout_solicitation(gpointer user_data)
1072 struct connman_dhcpv6 *dhcp = user_data;
1074 dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
1076 DBG("solicit RT timeout %d msec", dhcp->RT);
1078 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1080 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1085 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
1087 struct connman_service *service;
1088 struct connman_ipconfig *ipconfig_ipv6;
1089 GDHCPClient *dhcp_client;
1090 GDHCPClientError error;
1093 DBG("dhcp %p", dhcp);
1095 index = connman_network_get_index(dhcp->network);
1097 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
1098 if (error != G_DHCP_CLIENT_ERROR_NONE)
1101 if (getenv("CONNMAN_DHCPV6_DEBUG"))
1102 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
1104 service = __connman_service_lookup_from_network(dhcp->network);
1105 if (service == NULL)
1108 ret = set_duid(service, dhcp->network, dhcp_client, index);
1112 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1113 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
1114 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1115 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1117 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
1118 G_DHCPV6_SNTP_SERVERS);
1120 ipconfig_ipv6 = __connman_service_get_ip6config(service);
1121 dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
1123 g_dhcpv6_client_set_ia(dhcp_client, index,
1124 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1127 clear_callbacks(dhcp_client);
1129 g_dhcp_client_register_event(dhcp_client,
1130 G_DHCP_CLIENT_EVENT_SOLICITATION,
1131 solicitation_cb, dhcp);
1133 g_dhcp_client_register_event(dhcp_client,
1134 G_DHCP_CLIENT_EVENT_ADVERTISE,
1135 advertise_cb, dhcp);
1137 dhcp->dhcp_client = dhcp_client;
1139 return g_dhcp_client_start(dhcp_client, NULL);
1142 static gboolean start_solicitation(gpointer user_data)
1144 struct connman_dhcpv6 *dhcp = user_data;
1146 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1147 dhcp->RT = SOL_TIMEOUT * (1 + get_random());
1149 DBG("solicit initial RT timeout %d msec", dhcp->RT);
1151 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1153 dhcpv6_solicitation(dhcp);
1158 int __connman_dhcpv6_start(struct connman_network *network,
1159 GSList *prefixes, dhcp_cb callback)
1161 struct connman_dhcpv6 *dhcp;
1166 if (network_table != NULL) {
1167 dhcp = g_hash_table_lookup(network_table, network);
1168 if (dhcp != NULL && dhcp->started == TRUE)
1172 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1176 dhcp->network = network;
1177 dhcp->callback = callback;
1178 dhcp->prefixes = prefixes;
1179 dhcp->started = TRUE;
1181 connman_network_ref(network);
1183 DBG("replace network %p dhcp %p", network, dhcp);
1185 g_hash_table_replace(network_table, network, dhcp);
1187 /* Initial timeout, RFC 3315, 17.1.2 */
1188 delay = rand() % 1000;
1190 dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
1195 void __connman_dhcpv6_stop(struct connman_network *network)
1199 if (network_table == NULL)
1202 if (g_hash_table_remove(network_table, network) == TRUE)
1203 connman_network_unref(network);
1206 int __connman_dhcpv6_init(void)
1212 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1213 NULL, remove_network);
1218 void __connman_dhcpv6_cleanup(void)
1222 g_hash_table_destroy(network_table);
1223 network_table = NULL;