dhcpv6: Handle address expiration by restarting the stack.
[framework/connectivity/connman.git] / src / dhcpv6.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2011  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <connman/ipconfig.h>
31 #include <connman/storage.h>
32
33 #include <gdhcp/gdhcp.h>
34
35 #include <glib.h>
36
37 #include "connman.h"
38
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)
48 #define REQ_MAX_RC      10
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)
53
54
55 struct connman_dhcpv6 {
56         struct connman_network *network;
57         dhcp_cb callback;
58
59         char **nameservers;
60         char **timeservers;
61
62         GDHCPClient *dhcp_client;
63
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 };
70
71 static GHashTable *network_table;
72
73 static int dhcpv6_request(struct connman_dhcpv6 *dhcp, gboolean add_addresses);
74
75 static inline float get_random()
76 {
77         return (rand() % 200 - 100) / 1000.0;
78 }
79
80 /* Calculate a random delay, RFC 3315 chapter 14 */
81 /* RT and MRT are milliseconds */
82 static guint calc_delay(guint RT, guint MRT)
83 {
84         float delay = get_random();
85         float rt = RT * (2 + delay);
86
87         if (rt > MRT)
88                 rt = MRT * (1 + delay);
89
90         if (rt < 0)
91                 rt = MRT;
92
93         return (guint)rt;
94 }
95
96 static void free_prefix(gpointer data, gpointer user_data)
97 {
98         g_free(data);
99 }
100
101 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
102 {
103         g_strfreev(dhcp->nameservers);
104         g_strfreev(dhcp->timeservers);
105
106         dhcp->nameservers = NULL;
107         dhcp->timeservers = NULL;
108
109         g_slist_foreach(dhcp->prefixes, free_prefix, NULL);
110         g_slist_free(dhcp->prefixes);
111 }
112
113 static gboolean compare_string_arrays(char **array_a, char **array_b)
114 {
115         int i;
116
117         if (array_a == NULL || array_b == NULL)
118                 return FALSE;
119
120         if (g_strv_length(array_a) != g_strv_length(array_b))
121                 return FALSE;
122
123         for (i = 0; array_a[i] != NULL && array_b[i] != NULL; i++)
124                 if (g_strcmp0(array_a[i], array_b[i]) != 0)
125                         return FALSE;
126
127         return TRUE;
128 }
129
130 static void dhcpv6_debug(const char *str, void *data)
131 {
132         connman_info("%s: %s\n", (const char *) data, str);
133 }
134
135 static gchar *convert_to_hex(unsigned char *buf, int len)
136 {
137         gchar *ret = g_try_malloc(len * 2 + 1);
138         int i;
139
140         for (i = 0; ret != NULL && i < len; i++)
141                 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
142
143         return ret;
144 }
145
146 /*
147  * DUID should not change over time so save it to file.
148  * See RFC 3315 chapter 9 for details.
149  */
150 static int set_duid(struct connman_service *service,
151                         struct connman_network *network,
152                         GDHCPClient *dhcp_client, int index)
153 {
154         GKeyFile *keyfile;
155         const char *ident;
156         char *hex_duid;
157         unsigned char *duid;
158         int duid_len;
159
160         ident = __connman_service_get_ident(service);
161
162         keyfile = connman_storage_load_service(ident);
163         if (keyfile == NULL)
164                 return -EINVAL;
165
166         hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
167                                         NULL);
168         if (hex_duid != NULL) {
169                 unsigned int i, j = 0, hex;
170                 size_t hex_duid_len = strlen(hex_duid);
171
172                 duid = g_try_malloc0(hex_duid_len / 2);
173                 if (duid == NULL) {
174                         g_key_file_free(keyfile);
175                         g_free(hex_duid);
176                         return -ENOMEM;
177                 }
178
179                 for (i = 0; i < hex_duid_len; i += 2) {
180                         sscanf(hex_duid + i, "%02x", &hex);
181                         duid[j++] = hex;
182                 }
183
184                 duid_len = hex_duid_len / 2;
185         } else {
186                 int ret;
187                 int type = __connman_ipconfig_get_type_from_index(index);
188
189                 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
190                                         &duid, &duid_len);
191                 if (ret < 0) {
192                         g_key_file_free(keyfile);
193                         return ret;
194                 }
195
196                 hex_duid = convert_to_hex(duid, duid_len);
197                 if (hex_duid == NULL) {
198                         g_key_file_free(keyfile);
199                         return -ENOMEM;
200                 }
201
202                 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
203                                 hex_duid);
204
205                 __connman_storage_save_service(keyfile, ident);
206         }
207         g_free(hex_duid);
208
209         g_key_file_free(keyfile);
210
211         g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
212
213         return 0;
214 }
215
216 static void clear_callbacks(GDHCPClient *dhcp_client)
217 {
218         g_dhcp_client_register_event(dhcp_client,
219                                 G_DHCP_CLIENT_EVENT_SOLICITATION,
220                                 NULL, NULL);
221
222         g_dhcp_client_register_event(dhcp_client,
223                                 G_DHCP_CLIENT_EVENT_ADVERTISE,
224                                 NULL, NULL);
225
226         g_dhcp_client_register_event(dhcp_client,
227                                 G_DHCP_CLIENT_EVENT_REQUEST,
228                                 NULL, NULL);
229
230         g_dhcp_client_register_event(dhcp_client,
231                                 G_DHCP_CLIENT_EVENT_RENEW,
232                                 NULL, NULL);
233
234         g_dhcp_client_register_event(dhcp_client,
235                                 G_DHCP_CLIENT_EVENT_REBIND,
236                                 NULL, NULL);
237
238         g_dhcp_client_register_event(dhcp_client,
239                                 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
240                                 NULL, NULL);
241 }
242
243 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
244 {
245         struct connman_dhcpv6 *dhcp = user_data;
246         struct connman_service *service;
247         int entries, i;
248         GList *option, *list;
249         char **nameservers, **timeservers;
250
251         DBG("dhcpv6 information-request %p", dhcp);
252
253         service = __connman_service_lookup_from_network(dhcp->network);
254         if (service == NULL) {
255                 connman_error("Can not lookup service");
256                 return;
257         }
258
259         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
260         entries = g_list_length(option);
261
262         nameservers = g_try_new0(char *, entries + 1);
263         if (nameservers != NULL) {
264                 for (i = 0, list = option; list; list = list->next, i++)
265                         nameservers[i] = g_strdup(list->data);
266         }
267
268         if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
269                 if (dhcp->nameservers != NULL) {
270                         for (i = 0; dhcp->nameservers[i] != NULL; i++)
271                                 __connman_service_nameserver_remove(service,
272                                                         dhcp->nameservers[i],
273                                                         FALSE);
274                         g_strfreev(dhcp->nameservers);
275                 }
276
277                 dhcp->nameservers = nameservers;
278
279                 for (i = 0; dhcp->nameservers[i] != NULL; i++)
280                         __connman_service_nameserver_append(service,
281                                                 dhcp->nameservers[i],
282                                                 FALSE);
283         } else
284                 g_strfreev(nameservers);
285
286
287         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
288         entries = g_list_length(option);
289
290         timeservers = g_try_new0(char *, entries + 1);
291         if (timeservers != NULL) {
292                 for (i = 0, list = option; list; list = list->next, i++)
293                         timeservers[i] = g_strdup(list->data);
294         }
295
296         if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
297                 if (dhcp->timeservers != NULL) {
298                         for (i = 0; dhcp->timeservers[i] != NULL; i++)
299                                 __connman_service_timeserver_remove(service,
300                                                         dhcp->timeservers[i]);
301                         g_strfreev(dhcp->timeservers);
302                 }
303
304                 dhcp->timeservers = timeservers;
305
306                 for (i = 0; dhcp->timeservers[i] != NULL; i++)
307                         __connman_service_timeserver_append(service,
308                                                         dhcp->timeservers[i]);
309         } else
310                 g_strfreev(timeservers);
311
312
313         if (dhcp->callback != NULL) {
314                 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
315                 dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
316         }
317 }
318
319 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
320 {
321         struct connman_service *service;
322         GDHCPClient *dhcp_client;
323         GDHCPClientError error;
324         int index, ret;
325
326         DBG("dhcp %p", dhcp);
327
328         index = connman_network_get_index(dhcp->network);
329
330         dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
331         if (error != G_DHCP_CLIENT_ERROR_NONE)
332                 return -EINVAL;
333
334         if (getenv("CONNMAN_DHCPV6_DEBUG"))
335                 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
336
337         service = __connman_service_lookup_from_network(dhcp->network);
338         if (service == NULL)
339                 return -EINVAL;
340
341         ret = set_duid(service, dhcp->network, dhcp_client, index);
342         if (ret < 0)
343                 return ret;
344
345         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
346         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
347         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
348
349         g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
350                                 G_DHCPV6_SNTP_SERVERS);
351
352         g_dhcp_client_register_event(dhcp_client,
353                         G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
354
355         dhcp->dhcp_client = dhcp_client;
356
357         return g_dhcp_client_start(dhcp_client, NULL);
358 }
359
360 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
361 {
362         struct in6_addr addr_prefix, addr;
363         GSList *list;
364         int ret = 128, len;
365
366         for (list = prefixes; list; list = list->next) {
367                 char *prefix = list->data;
368                 const char *slash = g_strrstr(prefix, "/");
369                 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
370                                                 0xF0, 0xE0, 0xC0, 0x80 };
371                 int left, count, i, plen;
372
373                 if (slash == NULL)
374                         continue;
375
376                 prefix = g_strndup(prefix, slash - prefix);
377                 len = strtol(slash + 1, NULL, 10);
378                 plen = 128 - len;
379
380                 count = plen / 8;
381                 left = plen % 8;
382                 i = 16 - count;
383
384                 inet_pton(AF_INET6, prefix, &addr_prefix);
385                 inet_pton(AF_INET6, address, &addr);
386
387                 memset(&addr_prefix.s6_addr[i], 0, count);
388                 memset(&addr.s6_addr[i], 0, count);
389
390                 if (left) {
391                         addr_prefix.s6_addr[i - 1] &= bits[left];
392                         addr.s6_addr[i - 1] &= bits[left];
393                 }
394
395                 g_free(prefix);
396
397                 if (memcmp(&addr_prefix, &addr, 16) == 0) {
398                         ret = len;
399                         break;
400                 }
401         }
402
403         return ret;
404 }
405
406 static int set_addresses(GDHCPClient *dhcp_client,
407                                                 struct connman_dhcpv6 *dhcp)
408 {
409         struct connman_service *service;
410         struct connman_ipconfig *ipconfig;
411         int entries, i;
412         GList *option, *list;
413         char **nameservers, **timeservers;
414         const char *c_address;
415         char *address = NULL;
416
417         service = __connman_service_lookup_from_network(dhcp->network);
418         if (service == NULL) {
419                 connman_error("Can not lookup service");
420                 return -EINVAL;
421         }
422
423         ipconfig = __connman_service_get_ip6config(service);
424         if (ipconfig == NULL) {
425                 connman_error("Could not lookup ip6config");
426                 return -EINVAL;
427         }
428
429         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
430         entries = g_list_length(option);
431
432         nameservers = g_try_new0(char *, entries + 1);
433         if (nameservers != NULL) {
434                 for (i = 0, list = option; list; list = list->next, i++)
435                         nameservers[i] = g_strdup(list->data);
436         }
437
438         if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
439                 if (dhcp->nameservers != NULL) {
440                         for (i = 0; dhcp->nameservers[i] != NULL; i++)
441                                 __connman_service_nameserver_remove(service,
442                                                         dhcp->nameservers[i],
443                                                         FALSE);
444                         g_strfreev(dhcp->nameservers);
445                 }
446
447                 dhcp->nameservers = nameservers;
448
449                 for (i = 0; dhcp->nameservers[i] != NULL; i++)
450                         __connman_service_nameserver_append(service,
451                                                         dhcp->nameservers[i],
452                                                         FALSE);
453         } else
454                 g_strfreev(nameservers);
455
456
457         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
458         entries = g_list_length(option);
459
460         timeservers = g_try_new0(char *, entries + 1);
461         if (timeservers != NULL) {
462                 for (i = 0, list = option; list; list = list->next, i++)
463                         timeservers[i] = g_strdup(list->data);
464         }
465
466         if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
467                 if (dhcp->timeservers != NULL) {
468                         for (i = 0; dhcp->timeservers[i] != NULL; i++)
469                                 __connman_service_timeserver_remove(service,
470                                                         dhcp->timeservers[i]);
471                         g_strfreev(dhcp->timeservers);
472                 }
473
474                 dhcp->timeservers = timeservers;
475
476                 for (i = 0; dhcp->timeservers[i] != NULL; i++)
477                         __connman_service_timeserver_append(service,
478                                                         dhcp->timeservers[i]);
479         } else
480                 g_strfreev(timeservers);
481
482
483         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
484         if (option != NULL)
485                 address = g_strdup(option->data);
486         else {
487                 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
488                 if (option != NULL)
489                         address = g_strdup(option->data);
490         }
491
492         c_address = __connman_ipconfig_get_local(ipconfig);
493
494         if (address != NULL &&
495                         ((c_address != NULL &&
496                                 g_strcmp0(address, c_address) != 0) ||
497                         (c_address == NULL))) {
498                 int prefix_len;
499
500                 /* Is this prefix part of the subnet we are suppose to use? */
501                 prefix_len = check_ipv6_addr_prefix(dhcp->prefixes, address);
502
503                 __connman_ipconfig_set_local(ipconfig, address);
504                 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
505
506                 DBG("new address %s/%d", address, prefix_len);
507         }
508
509         return 0;
510 }
511
512 static void re_cb(GDHCPClient *dhcp_client, gpointer user_data)
513 {
514         struct connman_dhcpv6 *dhcp = user_data;
515         uint16_t status;
516         int ret;
517
518         ret = set_addresses(dhcp_client, dhcp);
519
520         status = g_dhcpv6_client_get_status(dhcp_client);
521
522         DBG("dhcpv6 cb msg %p ret %d status %d", dhcp, ret, status);
523
524         if (ret < 0) {
525                 if (dhcp->callback != NULL)
526                         dhcp->callback(dhcp->network, FALSE);
527                 return;
528         }
529
530         if (status  == G_DHCPV6_ERROR_BINDING) {
531                 /* RFC 3315, 18.1.8 */
532                 dhcpv6_request(dhcp, FALSE);
533         } else {
534                 if (dhcp->callback != NULL)
535                         dhcp->callback(dhcp->network,
536                                                 status == 0 ? TRUE : FALSE);
537         }
538 }
539
540 static void rebind_cb(GDHCPClient *dhcp_client, gpointer user_data)
541 {
542         DBG("");
543
544         g_dhcpv6_client_reset_rebind(dhcp_client);
545         g_dhcpv6_client_reset_renew(dhcp_client);
546
547         re_cb(dhcp_client, user_data);
548 }
549
550 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp)
551 {
552         GDHCPClient *dhcp_client;
553
554         DBG("dhcp %p", dhcp);
555
556         dhcp_client = dhcp->dhcp_client;
557
558         g_dhcp_client_clear_requests(dhcp_client);
559
560         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
561         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
562         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
563
564         g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
565                                 G_DHCPV6_SNTP_SERVERS);
566
567         g_dhcpv6_client_set_ia(dhcp_client,
568                         connman_network_get_index(dhcp->network),
569                         dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
570                         NULL, NULL, FALSE);
571
572         clear_callbacks(dhcp_client);
573
574         g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
575                                         rebind_cb, dhcp);
576
577         dhcp->dhcp_client = dhcp_client;
578
579         return g_dhcp_client_start(dhcp_client, NULL);
580 }
581
582 static gboolean dhcpv6_restart(gpointer user_data)
583 {
584         struct connman_dhcpv6 *dhcp = user_data;
585
586         if (dhcp->callback != NULL)
587                 dhcp->callback(dhcp->network, FALSE);
588
589         return FALSE;
590 }
591
592 /*
593  * Check if we need to restart the solicitation procedure. This
594  * is done if all the addresses have expired. RFC 3315, 18.1.4
595  */
596 static int check_restart(struct connman_dhcpv6 *dhcp)
597 {
598         time_t current, expired;
599
600         g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
601                                 NULL, NULL, &expired);
602         current = time(0);
603
604         if (current > expired) {
605                 DBG("expired by %d secs", (int)(current - expired));
606
607                 g_timeout_add(0, dhcpv6_restart, dhcp);
608
609                 return -ETIMEDOUT;
610         }
611
612         return 0;
613 }
614
615 static gboolean timeout_rebind(gpointer user_data)
616 {
617         struct connman_dhcpv6 *dhcp = user_data;
618
619         if (check_restart(dhcp) < 0)
620                 return FALSE;
621
622         dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
623
624         DBG("rebind RT timeout %d msec", dhcp->RT);
625
626         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
627
628         g_dhcp_client_start(dhcp->dhcp_client, NULL);
629
630         return FALSE;
631 }
632
633 static gboolean start_rebind(gpointer user_data)
634 {
635         struct connman_dhcpv6 *dhcp = user_data;
636
637         dhcp->RT = REB_TIMEOUT * (1 + get_random());
638
639         DBG("rebind initial RT timeout %d msec", dhcp->RT);
640
641         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
642
643         dhcpv6_rebind(dhcp);
644
645         return FALSE;
646 }
647
648 static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
649 {
650         DBG("");
651
652         re_cb(dhcp_client, user_data);
653 }
654
655 static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
656                         gboolean add_addresses)
657 {
658         GDHCPClient *dhcp_client;
659         uint32_t T1, T2;
660
661         DBG("dhcp %p add %d", dhcp, add_addresses);
662
663         dhcp_client = dhcp->dhcp_client;
664
665         g_dhcp_client_clear_requests(dhcp_client);
666
667         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
668         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
669         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
670         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
671
672         g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
673                                 G_DHCPV6_SNTP_SERVERS);
674
675         g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
676         g_dhcpv6_client_set_ia(dhcp_client,
677                         connman_network_get_index(dhcp->network),
678                         dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
679                         &T1, &T2, add_addresses);
680
681         clear_callbacks(dhcp_client);
682
683         g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
684                                         request_cb, dhcp);
685
686         dhcp->dhcp_client = dhcp_client;
687
688         return g_dhcp_client_start(dhcp_client, NULL);
689 }
690
691 static gboolean timeout_request(gpointer user_data)
692 {
693         struct connman_dhcpv6 *dhcp = user_data;
694
695         if (dhcp->request_count >= REQ_MAX_RC) {
696                 DBG("max request retry attempts %d", dhcp->request_count);
697                 dhcp->request_count = 0;
698                 if (dhcp->callback != NULL)
699                         dhcp->callback(dhcp->network, FALSE);
700                 return FALSE;
701         }
702
703         dhcp->request_count++;
704
705         dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
706         DBG("request RT timeout %d msec", dhcp->RT);
707         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
708
709         g_dhcp_client_start(dhcp->dhcp_client, NULL);
710
711         return FALSE;
712 }
713
714 static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
715 {
716         DBG("");
717
718         g_dhcpv6_client_reset_renew(dhcp_client);
719
720         re_cb(dhcp_client, user_data);
721 }
722
723 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
724 {
725         GDHCPClient *dhcp_client;
726         uint32_t T1, T2;
727
728         DBG("dhcp %p", dhcp);
729
730         dhcp_client = dhcp->dhcp_client;
731
732         g_dhcp_client_clear_requests(dhcp_client);
733
734         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
735         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
736         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
737         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
738
739         g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
740                                 G_DHCPV6_SNTP_SERVERS);
741
742         g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
743         g_dhcpv6_client_set_ia(dhcp_client,
744                         connman_network_get_index(dhcp->network),
745                         dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
746                         &T1, &T2, TRUE);
747
748         clear_callbacks(dhcp_client);
749
750         g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
751                                         renew_cb, dhcp);
752
753         dhcp->dhcp_client = dhcp_client;
754
755         return g_dhcp_client_start(dhcp_client, NULL);
756 }
757
758 static gboolean timeout_renew(gpointer user_data)
759 {
760         struct connman_dhcpv6 *dhcp = user_data;
761
762         if (check_restart(dhcp) < 0)
763                 return FALSE;
764
765         dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
766
767         DBG("renew RT timeout %d msec", dhcp->RT);
768
769         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
770
771         g_dhcp_client_start(dhcp->dhcp_client, NULL);
772
773         return FALSE;
774 }
775
776 static gboolean start_renew(gpointer user_data)
777 {
778         struct connman_dhcpv6 *dhcp = user_data;
779
780         dhcp->RT = REN_TIMEOUT * (1 + get_random());
781
782         DBG("renew initial RT timeout %d msec", dhcp->RT);
783
784         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
785
786         dhcpv6_renew(dhcp);
787
788         return FALSE;
789 }
790
791 int __connman_dhcpv6_start_renew(struct connman_network *network,
792                                                         dhcp_cb callback)
793 {
794         struct connman_dhcpv6 *dhcp;
795         uint32_t T1, T2;
796         time_t last_renew, last_rebind, current, expired;
797
798         dhcp = g_hash_table_lookup(network_table, network);
799         if (dhcp == NULL)
800                 return -ENOENT;
801
802         if (dhcp->timeout > 0) {
803                 g_source_remove(dhcp->timeout);
804                 dhcp->timeout = 0;
805         }
806
807         g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
808                                 &last_renew, &last_rebind, &expired);
809
810         current = time(0);
811
812         DBG("T1 %u T2 %u expires %lu current %lu", T1, T2,
813                 (unsigned long)expired, current);
814
815         if (T1 == 0xffffffff)
816                 /* RFC 3315, 22.4 */
817                 return 0;
818
819         if (T1 == 0)
820                 /* RFC 3315, 22.4
821                  * Client can choose the timeout.
822                  */
823                 T1 = 1800;
824
825         /* RFC 3315, 18.1.4, start solicit if expired */
826         if (current > expired) {
827                 DBG("expired by %d secs", (int)(current - expired));
828                 return -ETIMEDOUT;
829         }
830
831         dhcp->callback = callback;
832
833         if (T2 != 0xffffffff && T2 > 0 &&
834                         (unsigned)current > (unsigned)last_rebind + T2) {
835                 int timeout;
836
837                 /* RFC 3315, chapter 18.1.3, start rebind */
838                 if ((unsigned)current > (unsigned)last_renew + T1)
839                         timeout = 0;
840                 else
841                         timeout = last_renew - current + T1;
842
843                 /*
844                  * If we just did a renew, do not restart the rebind
845                  * immediately.
846                  */
847                 dhcp->timeout = g_timeout_add_seconds(timeout, start_rebind,
848                                                 dhcp);
849         } else {
850                 DBG("renew after %d secs", T1);
851
852                 dhcp->timeout = g_timeout_add_seconds(T1, start_renew, dhcp);
853         }
854         return 0;
855 }
856
857 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
858 {
859         DBG("dhcp %p", dhcp);
860
861         if (dhcp->timeout > 0) {
862                 g_source_remove(dhcp->timeout);
863                 dhcp->timeout = 0;
864         }
865
866         dhcpv6_free(dhcp);
867
868         if (dhcp->dhcp_client == NULL)
869                 return 0;
870
871         g_dhcp_client_stop(dhcp->dhcp_client);
872         g_dhcp_client_unref(dhcp->dhcp_client);
873
874         dhcp->dhcp_client = NULL;
875
876         return 0;
877 }
878
879 static void remove_network(gpointer user_data)
880 {
881         struct connman_dhcpv6 *dhcp = user_data;
882
883         DBG("dhcp %p", dhcp);
884
885         dhcpv6_release(dhcp);
886
887         g_free(dhcp);
888 }
889
890 static gboolean timeout_info_req(gpointer user_data)
891 {
892         struct connman_dhcpv6 *dhcp = user_data;
893
894         dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
895
896         DBG("info RT timeout %d msec", dhcp->RT);
897
898         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
899
900         g_dhcp_client_start(dhcp->dhcp_client, NULL);
901
902         return FALSE;
903 }
904
905 static gboolean start_info_req(gpointer user_data)
906 {
907         struct connman_dhcpv6 *dhcp = user_data;
908
909         /* Set the retransmission timeout, RFC 3315 chapter 14 */
910         dhcp->RT = INF_TIMEOUT * (1 + get_random());
911
912         DBG("info initial RT timeout %d msec", dhcp->RT);
913
914         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
915
916         dhcpv6_info_request(dhcp);
917
918         return FALSE;
919 }
920
921 int __connman_dhcpv6_start_info(struct connman_network *network,
922                                 dhcp_cb callback)
923 {
924         struct connman_dhcpv6 *dhcp;
925         int delay;
926
927         DBG("");
928
929         dhcp = g_try_new0(struct connman_dhcpv6, 1);
930         if (dhcp == NULL)
931                 return -ENOMEM;
932
933         dhcp->network = network;
934         dhcp->callback = callback;
935
936         connman_network_ref(network);
937
938         g_hash_table_replace(network_table, network, dhcp);
939
940         /* Initial timeout, RFC 3315, 18.1.5 */
941         delay = rand() % 1000;
942
943         dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
944
945         return 0;
946 }
947
948 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
949 {
950         struct connman_dhcpv6 *dhcp = user_data;
951
952         DBG("dhcpv6 advertise msg %p", dhcp);
953
954         if (dhcp->timeout > 0) {
955                 g_source_remove(dhcp->timeout);
956                 dhcp->timeout = 0;
957         }
958
959         if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
960                 if (dhcp->callback != NULL)
961                         dhcp->callback(dhcp->network, FALSE);
962                 return;
963         }
964
965         dhcp->RT = REQ_TIMEOUT * (1 + get_random());
966         DBG("request initial RT timeout %d msec", dhcp->RT);
967         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
968
969         dhcp->request_count = 1;
970
971         dhcpv6_request(dhcp, TRUE);
972 }
973
974 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
975 {
976         /* We get here if server supports rapid commit */
977         struct connman_dhcpv6 *dhcp = user_data;
978
979         DBG("dhcpv6 solicitation msg %p", dhcp);
980
981         if (dhcp->timeout > 0) {
982                 g_source_remove(dhcp->timeout);
983                 dhcp->timeout = 0;
984         }
985
986         set_addresses(dhcp_client, dhcp);
987 }
988
989 static gboolean timeout_solicitation(gpointer user_data)
990 {
991         struct connman_dhcpv6 *dhcp = user_data;
992
993         dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
994
995         DBG("solicit RT timeout %d msec", dhcp->RT);
996
997         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
998
999         g_dhcp_client_start(dhcp->dhcp_client, NULL);
1000
1001         return FALSE;
1002 }
1003
1004 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
1005 {
1006         struct connman_service *service;
1007         struct connman_ipconfig *ipconfig_ipv6;
1008         GDHCPClient *dhcp_client;
1009         GDHCPClientError error;
1010         int index, ret;
1011
1012         DBG("dhcp %p", dhcp);
1013
1014         index = connman_network_get_index(dhcp->network);
1015
1016         dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
1017         if (error != G_DHCP_CLIENT_ERROR_NONE)
1018                 return -EINVAL;
1019
1020         if (getenv("CONNMAN_DHCPV6_DEBUG"))
1021                 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
1022
1023         service = __connman_service_lookup_from_network(dhcp->network);
1024         if (service == NULL)
1025                 return -EINVAL;
1026
1027         ret = set_duid(service, dhcp->network, dhcp_client, index);
1028         if (ret < 0)
1029                 return ret;
1030
1031         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1032         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
1033         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1034         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1035
1036         g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
1037                                 G_DHCPV6_SNTP_SERVERS);
1038
1039         ipconfig_ipv6 = __connman_service_get_ip6config(service);
1040         dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
1041
1042         g_dhcpv6_client_set_ia(dhcp_client, index,
1043                         dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1044                         NULL, NULL, FALSE);
1045
1046         clear_callbacks(dhcp_client);
1047
1048         g_dhcp_client_register_event(dhcp_client,
1049                                 G_DHCP_CLIENT_EVENT_SOLICITATION,
1050                                 solicitation_cb, dhcp);
1051
1052         g_dhcp_client_register_event(dhcp_client,
1053                                 G_DHCP_CLIENT_EVENT_ADVERTISE,
1054                                 advertise_cb, dhcp);
1055
1056         dhcp->dhcp_client = dhcp_client;
1057
1058         return g_dhcp_client_start(dhcp_client, NULL);
1059 }
1060
1061 static gboolean start_solicitation(gpointer user_data)
1062 {
1063         struct connman_dhcpv6 *dhcp = user_data;
1064
1065         /* Set the retransmission timeout, RFC 3315 chapter 14 */
1066         dhcp->RT = SOL_TIMEOUT * (1 + get_random());
1067
1068         DBG("solicit initial RT timeout %d msec", dhcp->RT);
1069
1070         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1071
1072         dhcpv6_solicitation(dhcp);
1073
1074         return FALSE;
1075 }
1076
1077 int __connman_dhcpv6_start(struct connman_network *network,
1078                                 GSList *prefixes, dhcp_cb callback)
1079 {
1080         struct connman_dhcpv6 *dhcp;
1081         int delay;
1082
1083         DBG("");
1084
1085         dhcp = g_try_new0(struct connman_dhcpv6, 1);
1086         if (dhcp == NULL)
1087                 return -ENOMEM;
1088
1089         dhcp->network = network;
1090         dhcp->callback = callback;
1091         dhcp->prefixes = prefixes;
1092
1093         connman_network_ref(network);
1094
1095         g_hash_table_replace(network_table, network, dhcp);
1096
1097         /* Initial timeout, RFC 3315, 17.1.2 */
1098         delay = rand() % 1000;
1099
1100         dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
1101
1102         return 0;
1103 }
1104
1105 void __connman_dhcpv6_stop(struct connman_network *network)
1106 {
1107         DBG("");
1108
1109         if (network_table == NULL)
1110                 return;
1111
1112         if (g_hash_table_remove(network_table, network) == TRUE)
1113                 connman_network_unref(network);
1114 }
1115
1116 int __connman_dhcpv6_init(void)
1117 {
1118         DBG("");
1119
1120         srand(time(0));
1121
1122         network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1123                                                         NULL, remove_network);
1124
1125         return 0;
1126 }
1127
1128 void __connman_dhcpv6_cleanup(void)
1129 {
1130         DBG("");
1131
1132         g_hash_table_destroy(network_table);
1133         network_table = NULL;
1134 }