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 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 void clear_timer(struct connman_dhcpv6 *dhcp)
79 if (dhcp->timeout > 0) {
80 g_source_remove(dhcp->timeout);
85 static inline float get_random()
87 return (rand() % 200 - 100) / 1000.0;
90 /* Calculate a random delay, RFC 3315 chapter 14 */
91 /* RT and MRT are milliseconds */
92 static guint calc_delay(guint RT, guint MRT)
94 float delay = get_random();
95 float rt = RT * (2 + delay);
98 rt = MRT * (1 + delay);
106 static void free_prefix(gpointer data, gpointer user_data)
111 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
113 g_strfreev(dhcp->nameservers);
114 g_strfreev(dhcp->timeservers);
116 dhcp->nameservers = NULL;
117 dhcp->timeservers = NULL;
118 dhcp->started = FALSE;
120 g_slist_foreach(dhcp->prefixes, free_prefix, NULL);
121 g_slist_free(dhcp->prefixes);
124 static gboolean compare_string_arrays(char **array_a, char **array_b)
128 if (array_a == NULL || array_b == NULL)
131 if (g_strv_length(array_a) != g_strv_length(array_b))
134 for (i = 0; array_a[i] != NULL && array_b[i] != NULL; i++)
135 if (g_strcmp0(array_a[i], array_b[i]) != 0)
141 static void dhcpv6_debug(const char *str, void *data)
143 connman_info("%s: %s\n", (const char *) data, str);
146 static gchar *convert_to_hex(unsigned char *buf, int len)
148 gchar *ret = g_try_malloc(len * 2 + 1);
151 for (i = 0; ret != NULL && i < len; i++)
152 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
158 * DUID should not change over time so save it to file.
159 * See RFC 3315 chapter 9 for details.
161 static int set_duid(struct connman_service *service,
162 struct connman_network *network,
163 GDHCPClient *dhcp_client, int index)
171 ident = __connman_service_get_ident(service);
173 keyfile = connman_storage_load_service(ident);
177 hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
179 if (hex_duid != NULL) {
180 unsigned int i, j = 0, hex;
181 size_t hex_duid_len = strlen(hex_duid);
183 duid = g_try_malloc0(hex_duid_len / 2);
185 g_key_file_free(keyfile);
190 for (i = 0; i < hex_duid_len; i += 2) {
191 sscanf(hex_duid + i, "%02x", &hex);
195 duid_len = hex_duid_len / 2;
198 int type = __connman_ipconfig_get_type_from_index(index);
200 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
203 g_key_file_free(keyfile);
207 hex_duid = convert_to_hex(duid, duid_len);
208 if (hex_duid == NULL) {
209 g_key_file_free(keyfile);
213 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
216 __connman_storage_save_service(keyfile, ident);
220 g_key_file_free(keyfile);
222 g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
227 static void clear_callbacks(GDHCPClient *dhcp_client)
229 g_dhcp_client_register_event(dhcp_client,
230 G_DHCP_CLIENT_EVENT_SOLICITATION,
233 g_dhcp_client_register_event(dhcp_client,
234 G_DHCP_CLIENT_EVENT_ADVERTISE,
237 g_dhcp_client_register_event(dhcp_client,
238 G_DHCP_CLIENT_EVENT_REQUEST,
241 g_dhcp_client_register_event(dhcp_client,
242 G_DHCP_CLIENT_EVENT_RENEW,
245 g_dhcp_client_register_event(dhcp_client,
246 G_DHCP_CLIENT_EVENT_REBIND,
249 g_dhcp_client_register_event(dhcp_client,
250 G_DHCP_CLIENT_EVENT_RELEASE,
253 g_dhcp_client_register_event(dhcp_client,
254 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
258 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
260 struct connman_dhcpv6 *dhcp = user_data;
261 struct connman_service *service;
263 GList *option, *list;
264 char **nameservers, **timeservers;
266 DBG("dhcpv6 information-request %p", dhcp);
268 service = connman_service_lookup_from_network(dhcp->network);
269 if (service == NULL) {
270 connman_error("Can not lookup service");
274 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
275 entries = g_list_length(option);
277 nameservers = g_try_new0(char *, entries + 1);
278 if (nameservers != NULL) {
279 for (i = 0, list = option; list; list = list->next, i++)
280 nameservers[i] = g_strdup(list->data);
283 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
284 if (dhcp->nameservers != NULL) {
285 for (i = 0; dhcp->nameservers[i] != NULL; i++)
286 __connman_service_nameserver_remove(service,
287 dhcp->nameservers[i],
289 g_strfreev(dhcp->nameservers);
292 dhcp->nameservers = nameservers;
294 for (i = 0; dhcp->nameservers != NULL &&
295 dhcp->nameservers[i] != NULL; i++)
296 __connman_service_nameserver_append(service,
297 dhcp->nameservers[i],
300 g_strfreev(nameservers);
303 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
304 entries = g_list_length(option);
306 timeservers = g_try_new0(char *, entries + 1);
307 if (timeservers != NULL) {
308 for (i = 0, list = option; list; list = list->next, i++)
309 timeservers[i] = g_strdup(list->data);
312 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
313 if (dhcp->timeservers != NULL) {
314 for (i = 0; dhcp->timeservers[i] != NULL; i++)
315 __connman_service_timeserver_remove(service,
316 dhcp->timeservers[i]);
317 g_strfreev(dhcp->timeservers);
320 dhcp->timeservers = timeservers;
322 for (i = 0; dhcp->timeservers != NULL &&
323 dhcp->timeservers[i] != NULL; i++)
324 __connman_service_timeserver_append(service,
325 dhcp->timeservers[i]);
327 g_strfreev(timeservers);
330 if (dhcp->callback != NULL) {
331 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
332 dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
336 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
338 struct connman_service *service;
339 GDHCPClient *dhcp_client;
340 GDHCPClientError error;
343 DBG("dhcp %p", dhcp);
345 index = connman_network_get_index(dhcp->network);
347 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
348 if (error != G_DHCP_CLIENT_ERROR_NONE) {
353 if (getenv("CONNMAN_DHCPV6_DEBUG"))
354 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
356 service = connman_service_lookup_from_network(dhcp->network);
357 if (service == NULL) {
359 g_dhcp_client_unref(dhcp_client);
363 ret = set_duid(service, dhcp->network, dhcp_client, index);
366 g_dhcp_client_unref(dhcp_client);
370 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
371 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
372 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
374 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
375 G_DHCPV6_SNTP_SERVERS);
377 g_dhcp_client_register_event(dhcp_client,
378 G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
380 dhcp->dhcp_client = dhcp_client;
382 return g_dhcp_client_start(dhcp_client, NULL);
385 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
387 struct in6_addr addr_prefix, addr;
391 for (list = prefixes; list; list = list->next) {
392 char *prefix = list->data;
393 const char *slash = g_strrstr(prefix, "/");
394 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
395 0xF0, 0xE0, 0xC0, 0x80 };
396 int left, count, i, plen;
401 prefix = g_strndup(prefix, slash - prefix);
402 len = strtol(slash + 1, NULL, 10);
409 inet_pton(AF_INET6, prefix, &addr_prefix);
410 inet_pton(AF_INET6, address, &addr);
412 memset(&addr_prefix.s6_addr[i], 0, count);
413 memset(&addr.s6_addr[i], 0, count);
416 addr_prefix.s6_addr[i - 1] &= bits[left];
417 addr.s6_addr[i - 1] &= bits[left];
422 if (memcmp(&addr_prefix, &addr, 16) == 0) {
431 static int set_addresses(GDHCPClient *dhcp_client,
432 struct connman_dhcpv6 *dhcp)
434 struct connman_service *service;
435 struct connman_ipconfig *ipconfig;
437 GList *option, *list;
438 char **nameservers, **timeservers;
439 const char *c_address;
440 char *address = NULL;
442 service = connman_service_lookup_from_network(dhcp->network);
443 if (service == NULL) {
444 connman_error("Can not lookup service");
448 ipconfig = __connman_service_get_ip6config(service);
449 if (ipconfig == NULL) {
450 connman_error("Could not lookup ip6config");
454 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
455 entries = g_list_length(option);
457 nameservers = g_try_new0(char *, entries + 1);
458 if (nameservers != NULL) {
459 for (i = 0, list = option; list; list = list->next, i++)
460 nameservers[i] = g_strdup(list->data);
463 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
464 if (dhcp->nameservers != NULL) {
465 for (i = 0; dhcp->nameservers[i] != NULL; i++)
466 __connman_service_nameserver_remove(service,
467 dhcp->nameservers[i],
469 g_strfreev(dhcp->nameservers);
472 dhcp->nameservers = nameservers;
474 for (i = 0; dhcp->nameservers != NULL &&
475 dhcp->nameservers[i] != NULL; i++)
476 __connman_service_nameserver_append(service,
477 dhcp->nameservers[i],
480 g_strfreev(nameservers);
483 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
484 entries = g_list_length(option);
486 timeservers = g_try_new0(char *, entries + 1);
487 if (timeservers != NULL) {
488 for (i = 0, list = option; list; list = list->next, i++)
489 timeservers[i] = g_strdup(list->data);
492 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
493 if (dhcp->timeservers != NULL) {
494 for (i = 0; dhcp->timeservers[i] != NULL; i++)
495 __connman_service_timeserver_remove(service,
496 dhcp->timeservers[i]);
497 g_strfreev(dhcp->timeservers);
500 dhcp->timeservers = timeservers;
502 for (i = 0; dhcp->timeservers != NULL &&
503 dhcp->timeservers[i] != NULL; i++)
504 __connman_service_timeserver_append(service,
505 dhcp->timeservers[i]);
507 g_strfreev(timeservers);
510 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
512 address = g_strdup(option->data);
514 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
516 address = g_strdup(option->data);
519 c_address = __connman_ipconfig_get_local(ipconfig);
521 if (address != NULL &&
522 ((c_address != NULL &&
523 g_strcmp0(address, c_address) != 0) ||
524 (c_address == NULL))) {
527 /* Is this prefix part of the subnet we are suppose to use? */
528 prefix_len = check_ipv6_addr_prefix(dhcp->prefixes, address);
530 __connman_ipconfig_set_local(ipconfig, address);
531 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
533 DBG("new address %s/%d", address, prefix_len);
539 static void re_cb(GDHCPClient *dhcp_client, gpointer user_data)
541 struct connman_dhcpv6 *dhcp = user_data;
545 ret = set_addresses(dhcp_client, dhcp);
547 status = g_dhcpv6_client_get_status(dhcp_client);
549 DBG("dhcpv6 cb msg %p ret %d status %d", dhcp, ret, status);
552 if (dhcp->callback != NULL)
553 dhcp->callback(dhcp->network, FALSE);
557 if (status == G_DHCPV6_ERROR_BINDING) {
558 /* RFC 3315, 18.1.8 */
559 dhcpv6_request(dhcp, FALSE);
561 if (dhcp->callback != NULL)
562 dhcp->callback(dhcp->network,
563 status == 0 ? TRUE : FALSE);
567 static void rebind_cb(GDHCPClient *dhcp_client, gpointer user_data)
571 g_dhcpv6_client_reset_rebind(dhcp_client);
572 g_dhcpv6_client_reset_renew(dhcp_client);
574 re_cb(dhcp_client, user_data);
577 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp)
579 GDHCPClient *dhcp_client;
581 DBG("dhcp %p", dhcp);
583 dhcp_client = dhcp->dhcp_client;
585 g_dhcp_client_clear_requests(dhcp_client);
587 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
588 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
589 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
591 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
592 G_DHCPV6_SNTP_SERVERS);
594 g_dhcpv6_client_set_ia(dhcp_client,
595 connman_network_get_index(dhcp->network),
596 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
599 clear_callbacks(dhcp_client);
601 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
604 dhcp->dhcp_client = dhcp_client;
606 return g_dhcp_client_start(dhcp_client, NULL);
609 static gboolean dhcpv6_restart(gpointer user_data)
611 struct connman_dhcpv6 *dhcp = user_data;
613 if (dhcp->callback != NULL)
614 dhcp->callback(dhcp->network, FALSE);
620 * Check if we need to restart the solicitation procedure. This
621 * is done if all the addresses have expired. RFC 3315, 18.1.4
623 static int check_restart(struct connman_dhcpv6 *dhcp)
625 time_t current, expired;
627 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
628 NULL, NULL, &expired);
629 current = time(NULL);
631 if (current > expired) {
632 DBG("expired by %d secs", (int)(current - expired));
634 g_timeout_add(0, dhcpv6_restart, dhcp);
642 static gboolean timeout_rebind(gpointer user_data)
644 struct connman_dhcpv6 *dhcp = user_data;
646 if (check_restart(dhcp) < 0)
649 dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
651 DBG("rebind RT timeout %d msec", dhcp->RT);
653 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
655 g_dhcp_client_start(dhcp->dhcp_client, NULL);
660 static gboolean start_rebind(gpointer user_data)
662 struct connman_dhcpv6 *dhcp = user_data;
664 dhcp->RT = REB_TIMEOUT * (1 + get_random());
666 DBG("rebind initial RT timeout %d msec", dhcp->RT);
668 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
675 static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
679 re_cb(dhcp_client, user_data);
682 static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
683 gboolean add_addresses)
685 GDHCPClient *dhcp_client;
688 DBG("dhcp %p add %d", dhcp, add_addresses);
690 dhcp_client = dhcp->dhcp_client;
692 g_dhcp_client_clear_requests(dhcp_client);
694 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
695 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
696 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
697 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
699 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
700 G_DHCPV6_SNTP_SERVERS);
702 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
703 g_dhcpv6_client_set_ia(dhcp_client,
704 connman_network_get_index(dhcp->network),
705 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
706 &T1, &T2, add_addresses);
708 clear_callbacks(dhcp_client);
710 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
713 dhcp->dhcp_client = dhcp_client;
715 return g_dhcp_client_start(dhcp_client, NULL);
718 static gboolean timeout_request(gpointer user_data)
720 struct connman_dhcpv6 *dhcp = user_data;
722 if (dhcp->request_count >= REQ_MAX_RC) {
723 DBG("max request retry attempts %d", dhcp->request_count);
724 dhcp->request_count = 0;
725 if (dhcp->callback != NULL)
726 dhcp->callback(dhcp->network, FALSE);
730 dhcp->request_count++;
732 dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
733 DBG("request RT timeout %d msec", dhcp->RT);
734 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
736 g_dhcp_client_start(dhcp->dhcp_client, NULL);
741 static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
745 g_dhcpv6_client_reset_renew(dhcp_client);
747 re_cb(dhcp_client, user_data);
750 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
752 GDHCPClient *dhcp_client;
755 DBG("dhcp %p", dhcp);
757 dhcp_client = dhcp->dhcp_client;
759 g_dhcp_client_clear_requests(dhcp_client);
761 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
762 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
763 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
764 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
766 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
767 G_DHCPV6_SNTP_SERVERS);
769 g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
770 g_dhcpv6_client_set_ia(dhcp_client,
771 connman_network_get_index(dhcp->network),
772 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
775 clear_callbacks(dhcp_client);
777 g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
780 dhcp->dhcp_client = dhcp_client;
782 return g_dhcp_client_start(dhcp_client, NULL);
785 static gboolean timeout_renew(gpointer user_data)
787 struct connman_dhcpv6 *dhcp = user_data;
789 if (check_restart(dhcp) < 0)
792 dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
794 DBG("renew RT timeout %d msec", dhcp->RT);
796 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
798 g_dhcp_client_start(dhcp->dhcp_client, NULL);
803 static gboolean start_renew(gpointer user_data)
805 struct connman_dhcpv6 *dhcp = user_data;
807 dhcp->RT = REN_TIMEOUT * (1 + get_random());
809 DBG("renew initial RT timeout %d msec", dhcp->RT);
811 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
818 int __connman_dhcpv6_start_renew(struct connman_network *network,
821 struct connman_dhcpv6 *dhcp;
823 time_t last_renew, last_rebind, current, expired;
825 dhcp = g_hash_table_lookup(network_table, network);
829 DBG("network %p dhcp %p", network, dhcp);
833 g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
834 &last_renew, &last_rebind, &expired);
836 current = time(NULL);
838 DBG("T1 %u T2 %u expires %lu current %lu", T1, T2,
839 (unsigned long)expired, current);
841 if (T1 == 0xffffffff)
847 * Client can choose the timeout.
851 /* RFC 3315, 18.1.4, start solicit if expired */
852 if (current > expired) {
853 DBG("expired by %d secs", (int)(current - expired));
857 dhcp->callback = callback;
859 if (T2 != 0xffffffff && T2 > 0 &&
860 (unsigned)current > (unsigned)last_rebind + T2) {
863 /* RFC 3315, chapter 18.1.3, start rebind */
864 if ((unsigned)current > (unsigned)last_renew + T1)
867 timeout = last_renew - current + T1;
870 * If we just did a renew, do not restart the rebind
873 dhcp->timeout = g_timeout_add_seconds(timeout, start_rebind,
876 DBG("renew after %d secs", T1);
878 dhcp->timeout = g_timeout_add_seconds(T1, start_renew, dhcp);
883 int __connman_dhcpv6_start_release(struct connman_network *network,
886 struct connman_dhcpv6 *dhcp;
887 GDHCPClient *dhcp_client;
889 if (network_table == NULL)
890 return 0; /* we are already released */
892 dhcp = g_hash_table_lookup(network_table, network);
896 DBG("network %p dhcp %p client %p stateless %d", network, dhcp,
897 dhcp->dhcp_client, dhcp->stateless);
899 if (dhcp->stateless == TRUE)
904 dhcp_client = dhcp->dhcp_client;
905 if (dhcp_client == NULL) {
907 * We had started the DHCPv6 handshaking i.e., we have called
908 * __connman_dhcpv6_start() but it has not yet sent
909 * a solicitation message to server. This means that we do not
910 * have DHCPv6 configured yet so we can just quit here.
912 DBG("DHCPv6 was not started");
916 g_dhcp_client_clear_requests(dhcp_client);
917 g_dhcp_client_clear_values(dhcp_client);
919 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
920 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
922 g_dhcpv6_client_set_ia(dhcp_client,
923 connman_network_get_index(dhcp->network),
924 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
927 clear_callbacks(dhcp_client);
930 * We do not register callback here because the answer might take too
931 * long time and network code might be in the middle of the disconnect.
932 * So we just inform the server that we are done with the addresses
933 * but ignore the reply from server. This is allowed by RFC 3315
937 dhcp->dhcp_client = dhcp_client;
939 return g_dhcp_client_start(dhcp_client, NULL);
942 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
944 DBG("dhcp %p", dhcp);
950 if (dhcp->dhcp_client == NULL)
953 g_dhcp_client_stop(dhcp->dhcp_client);
954 g_dhcp_client_unref(dhcp->dhcp_client);
956 dhcp->dhcp_client = NULL;
961 static void remove_network(gpointer user_data)
963 struct connman_dhcpv6 *dhcp = user_data;
965 DBG("dhcp %p", dhcp);
967 dhcpv6_release(dhcp);
972 static gboolean timeout_info_req(gpointer user_data)
974 struct connman_dhcpv6 *dhcp = user_data;
976 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
978 DBG("info RT timeout %d msec", dhcp->RT);
980 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
982 g_dhcp_client_start(dhcp->dhcp_client, NULL);
987 static gboolean start_info_req(gpointer user_data)
989 struct connman_dhcpv6 *dhcp = user_data;
991 /* Set the retransmission timeout, RFC 3315 chapter 14 */
992 dhcp->RT = INF_TIMEOUT * (1 + get_random());
994 DBG("info initial RT timeout %d msec", dhcp->RT);
996 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
998 dhcpv6_info_request(dhcp);
1003 int __connman_dhcpv6_start_info(struct connman_network *network,
1006 struct connman_dhcpv6 *dhcp;
1011 if (network_table != NULL) {
1012 dhcp = g_hash_table_lookup(network_table, network);
1013 if (dhcp != NULL && dhcp->started == TRUE)
1017 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1021 dhcp->network = network;
1022 dhcp->callback = callback;
1023 dhcp->stateless = TRUE;
1024 dhcp->started = TRUE;
1026 connman_network_ref(network);
1028 DBG("replace network %p dhcp %p", network, dhcp);
1030 g_hash_table_replace(network_table, network, dhcp);
1032 /* Initial timeout, RFC 3315, 18.1.5 */
1033 delay = rand() % 1000;
1035 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
1040 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
1042 struct connman_dhcpv6 *dhcp = user_data;
1044 DBG("dhcpv6 advertise msg %p", dhcp);
1048 if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
1049 if (dhcp->callback != NULL)
1050 dhcp->callback(dhcp->network, FALSE);
1054 dhcp->RT = REQ_TIMEOUT * (1 + get_random());
1055 DBG("request initial RT timeout %d msec", dhcp->RT);
1056 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
1058 dhcp->request_count = 1;
1060 dhcpv6_request(dhcp, TRUE);
1063 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
1065 /* We get here if server supports rapid commit */
1066 struct connman_dhcpv6 *dhcp = user_data;
1068 DBG("dhcpv6 solicitation msg %p", dhcp);
1072 set_addresses(dhcp_client, dhcp);
1075 static gboolean timeout_solicitation(gpointer user_data)
1077 struct connman_dhcpv6 *dhcp = user_data;
1079 dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
1081 DBG("solicit RT timeout %d msec", dhcp->RT);
1083 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1085 g_dhcp_client_start(dhcp->dhcp_client, NULL);
1090 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
1092 struct connman_service *service;
1093 struct connman_ipconfig *ipconfig_ipv6;
1094 GDHCPClient *dhcp_client;
1095 GDHCPClientError error;
1098 DBG("dhcp %p", dhcp);
1100 index = connman_network_get_index(dhcp->network);
1102 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
1103 if (error != G_DHCP_CLIENT_ERROR_NONE) {
1108 if (getenv("CONNMAN_DHCPV6_DEBUG"))
1109 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
1111 service = connman_service_lookup_from_network(dhcp->network);
1112 if (service == NULL) {
1114 g_dhcp_client_unref(dhcp_client);
1118 ret = set_duid(service, dhcp->network, dhcp_client, index);
1121 g_dhcp_client_unref(dhcp_client);
1125 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1126 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
1127 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1128 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1130 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
1131 G_DHCPV6_SNTP_SERVERS);
1133 ipconfig_ipv6 = __connman_service_get_ip6config(service);
1134 dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
1136 g_dhcpv6_client_set_ia(dhcp_client, index,
1137 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1140 clear_callbacks(dhcp_client);
1142 g_dhcp_client_register_event(dhcp_client,
1143 G_DHCP_CLIENT_EVENT_SOLICITATION,
1144 solicitation_cb, dhcp);
1146 g_dhcp_client_register_event(dhcp_client,
1147 G_DHCP_CLIENT_EVENT_ADVERTISE,
1148 advertise_cb, dhcp);
1150 dhcp->dhcp_client = dhcp_client;
1152 return g_dhcp_client_start(dhcp_client, NULL);
1155 static gboolean start_solicitation(gpointer user_data)
1157 struct connman_dhcpv6 *dhcp = user_data;
1159 /* Set the retransmission timeout, RFC 3315 chapter 14 */
1160 dhcp->RT = SOL_TIMEOUT * (1 + get_random());
1162 DBG("solicit initial RT timeout %d msec", dhcp->RT);
1164 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1166 dhcpv6_solicitation(dhcp);
1171 int __connman_dhcpv6_start(struct connman_network *network,
1172 GSList *prefixes, dhcp_cb callback)
1174 struct connman_dhcpv6 *dhcp;
1179 if (network_table != NULL) {
1180 dhcp = g_hash_table_lookup(network_table, network);
1181 if (dhcp != NULL && dhcp->started == TRUE)
1185 dhcp = g_try_new0(struct connman_dhcpv6, 1);
1189 dhcp->network = network;
1190 dhcp->callback = callback;
1191 dhcp->prefixes = prefixes;
1192 dhcp->started = TRUE;
1194 connman_network_ref(network);
1196 DBG("replace network %p dhcp %p", network, dhcp);
1198 g_hash_table_replace(network_table, network, dhcp);
1200 /* Initial timeout, RFC 3315, 17.1.2 */
1201 delay = rand() % 1000;
1203 dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
1208 void __connman_dhcpv6_stop(struct connman_network *network)
1212 if (network_table == NULL)
1215 if (g_hash_table_remove(network_table, network) == TRUE)
1216 connman_network_unref(network);
1219 int __connman_dhcpv6_init(void)
1225 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1226 NULL, remove_network);
1231 void __connman_dhcpv6_cleanup(void)
1235 g_hash_table_destroy(network_table);
1236 network_table = NULL;