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