Imported Upstream version 1.26
[platform/upstream/connman.git] / src / dhcpv6.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012-2013  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 #include <net/if.h>
30
31 #include <connman/ipconfig.h>
32 #include <connman/storage.h>
33
34 #include <gdhcp/gdhcp.h>
35
36 #include <glib.h>
37
38 #include "connman.h"
39
40 /* Transmission params in msec, RFC 3315 chapter 5.5 */
41 #define INF_MAX_DELAY   (1 * 1000)
42 #define INF_TIMEOUT     (1 * 1000)
43 #define INF_MAX_RT      (120 * 1000)
44 #define SOL_MAX_DELAY   (1 * 1000)
45 #define SOL_TIMEOUT     (1 * 1000)
46 #define SOL_MAX_RT      (120 * 1000)
47 #define REQ_TIMEOUT     (1 * 1000)
48 #define REQ_MAX_RT      (30 * 1000)
49 #define REQ_MAX_RC      10
50 #define REN_TIMEOUT     (10 * 1000)
51 #define REN_MAX_RT      (600 * 1000)
52 #define REB_TIMEOUT     (10 * 1000)
53 #define REB_MAX_RT      (600 * 1000)
54 #define CNF_MAX_DELAY   (1 * 1000)
55 #define CNF_TIMEOUT     (1 * 1000)
56 #define CNF_MAX_RT      (4 * 1000)
57 #define CNF_MAX_RD      (10 * 1000)
58 #define DEC_TIMEOUT     (1 * 1000)
59 #define DEC_MAX_RC      5
60
61 enum request_type {
62         REQ_REQUEST = 1,
63         REQ_REBIND = 2,
64         REQ_RENEW = 3,
65 };
66
67 struct connman_dhcpv6 {
68         struct connman_network *network;
69         dhcpv6_cb callback;
70
71         char **nameservers;
72         char **timeservers;
73
74         GDHCPClient *dhcp_client;
75
76         guint timeout;          /* operation timeout in msec */
77         guint MRD;              /* max operation timeout in msec */
78         guint RT;               /* in msec */
79         bool use_ta;            /* set to TRUE if IPv6 privacy is enabled */
80         GSList *prefixes;       /* network prefixes from radvd or dhcpv6 pd */
81         int request_count;      /* how many times REQUEST have been sent */
82         bool stateless;         /* TRUE if stateless DHCPv6 is used */
83         bool started;           /* TRUE if we have DHCPv6 started */
84 };
85
86 static GHashTable *network_table;
87 static GHashTable *network_pd_table;
88
89 static int dhcpv6_request(struct connman_dhcpv6 *dhcp, bool add_addresses);
90 static int dhcpv6_pd_request(struct connman_dhcpv6 *dhcp);
91 static gboolean start_solicitation(gpointer user_data);
92 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp);
93 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp);
94
95 static void clear_timer(struct connman_dhcpv6 *dhcp)
96 {
97         if (dhcp->timeout > 0) {
98                 g_source_remove(dhcp->timeout);
99                 dhcp->timeout = 0;
100         }
101
102         if (dhcp->MRD > 0) {
103                 g_source_remove(dhcp->MRD);
104                 dhcp->MRD = 0;
105         }
106 }
107
108 static inline float get_random(void)
109 {
110         return (rand() % 200 - 100) / 1000.0;
111 }
112
113 /* Calculate a random delay, RFC 3315 chapter 14 */
114 /* RT and MRT are milliseconds */
115 static guint calc_delay(guint RT, guint MRT)
116 {
117         float delay = get_random();
118         float rt = RT * (2 + delay);
119
120         if (rt > MRT)
121                 rt = MRT * (1 + delay);
122
123         if (rt < 0)
124                 rt = MRT;
125
126         return (guint)rt;
127 }
128
129 static void free_prefix(gpointer data)
130 {
131         g_free(data);
132 }
133
134 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
135 {
136         g_strfreev(dhcp->nameservers);
137         g_strfreev(dhcp->timeservers);
138
139         dhcp->nameservers = NULL;
140         dhcp->timeservers = NULL;
141         dhcp->started = false;
142
143         g_slist_free_full(dhcp->prefixes, free_prefix);
144 }
145
146 static bool compare_string_arrays(char **array_a, char **array_b)
147 {
148         int i;
149
150         if (!array_a || !array_b)
151                 return false;
152
153         if (g_strv_length(array_a) != g_strv_length(array_b))
154                 return false;
155
156         for (i = 0; array_a[i] && array_b[i]; i++)
157                 if (g_strcmp0(array_a[i], array_b[i]) != 0)
158                         return false;
159
160         return true;
161 }
162
163 static void dhcpv6_debug(const char *str, void *data)
164 {
165         connman_info("%s: %s\n", (const char *) data, str);
166 }
167
168 static gchar *convert_to_hex(unsigned char *buf, int len)
169 {
170         gchar *ret = g_try_malloc(len * 2 + 1);
171         int i;
172
173         for (i = 0; ret && i < len; i++)
174                 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
175
176         return ret;
177 }
178
179 /*
180  * DUID should not change over time so save it to file.
181  * See RFC 3315 chapter 9 for details.
182  */
183 static int set_duid(struct connman_service *service,
184                         struct connman_network *network,
185                         GDHCPClient *dhcp_client, int index)
186 {
187         GKeyFile *keyfile;
188         const char *ident;
189         char *hex_duid;
190         unsigned char *duid;
191         int duid_len;
192
193         ident = __connman_service_get_ident(service);
194
195         keyfile = connman_storage_load_service(ident);
196         if (!keyfile)
197                 return -EINVAL;
198
199         hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
200                                         NULL);
201         if (hex_duid) {
202                 unsigned int i, j = 0, hex;
203                 size_t hex_duid_len = strlen(hex_duid);
204
205                 duid = g_try_malloc0(hex_duid_len / 2);
206                 if (!duid) {
207                         g_key_file_free(keyfile);
208                         g_free(hex_duid);
209                         return -ENOMEM;
210                 }
211
212                 for (i = 0; i < hex_duid_len; i += 2) {
213                         sscanf(hex_duid + i, "%02x", &hex);
214                         duid[j++] = hex;
215                 }
216
217                 duid_len = hex_duid_len / 2;
218         } else {
219                 int ret;
220                 int type = __connman_ipconfig_get_type_from_index(index);
221
222                 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
223                                         &duid, &duid_len);
224                 if (ret < 0) {
225                         g_key_file_free(keyfile);
226                         return ret;
227                 }
228
229                 hex_duid = convert_to_hex(duid, duid_len);
230                 if (!hex_duid) {
231                         g_key_file_free(keyfile);
232                         return -ENOMEM;
233                 }
234
235                 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
236                                 hex_duid);
237
238                 __connman_storage_save_service(keyfile, ident);
239         }
240         g_free(hex_duid);
241
242         g_key_file_free(keyfile);
243
244         g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
245
246         return 0;
247 }
248
249 static void clear_callbacks(GDHCPClient *dhcp_client)
250 {
251         g_dhcp_client_register_event(dhcp_client,
252                                 G_DHCP_CLIENT_EVENT_SOLICITATION,
253                                 NULL, NULL);
254
255         g_dhcp_client_register_event(dhcp_client,
256                                 G_DHCP_CLIENT_EVENT_ADVERTISE,
257                                 NULL, NULL);
258
259         g_dhcp_client_register_event(dhcp_client,
260                                 G_DHCP_CLIENT_EVENT_REQUEST,
261                                 NULL, NULL);
262
263         g_dhcp_client_register_event(dhcp_client,
264                                 G_DHCP_CLIENT_EVENT_CONFIRM,
265                                 NULL, NULL);
266
267         g_dhcp_client_register_event(dhcp_client,
268                                 G_DHCP_CLIENT_EVENT_RENEW,
269                                 NULL, NULL);
270
271         g_dhcp_client_register_event(dhcp_client,
272                                 G_DHCP_CLIENT_EVENT_REBIND,
273                                 NULL, NULL);
274
275         g_dhcp_client_register_event(dhcp_client,
276                                 G_DHCP_CLIENT_EVENT_RELEASE,
277                                 NULL, NULL);
278
279         g_dhcp_client_register_event(dhcp_client,
280                                 G_DHCP_CLIENT_EVENT_DECLINE,
281                                 NULL, NULL);
282
283         g_dhcp_client_register_event(dhcp_client,
284                                 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
285                                 NULL, NULL);
286 }
287
288 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
289 {
290         struct connman_dhcpv6 *dhcp = user_data;
291         struct connman_service *service;
292         int entries, i;
293         GList *option, *list;
294         char **nameservers, **timeservers;
295
296         DBG("dhcpv6 information-request %p", dhcp);
297
298         service = connman_service_lookup_from_network(dhcp->network);
299         if (!service) {
300                 connman_error("Can not lookup service");
301                 return;
302         }
303
304         g_dhcpv6_client_clear_retransmit(dhcp_client);
305
306         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
307         entries = g_list_length(option);
308
309         nameservers = g_try_new0(char *, entries + 1);
310         if (nameservers) {
311                 for (i = 0, list = option; list; list = list->next, i++)
312                         nameservers[i] = g_strdup(list->data);
313         }
314
315         if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
316                 if (dhcp->nameservers) {
317                         for (i = 0; dhcp->nameservers[i]; i++)
318                                 __connman_service_nameserver_remove(service,
319                                                         dhcp->nameservers[i],
320                                                         false);
321                         g_strfreev(dhcp->nameservers);
322                 }
323
324                 dhcp->nameservers = nameservers;
325
326                 for (i = 0; dhcp->nameservers &&
327                                         dhcp->nameservers[i]; i++)
328                         __connman_service_nameserver_append(service,
329                                                 dhcp->nameservers[i],
330                                                 false);
331         } else
332                 g_strfreev(nameservers);
333
334
335         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
336         entries = g_list_length(option);
337
338         timeservers = g_try_new0(char *, entries + 1);
339         if (timeservers) {
340                 for (i = 0, list = option; list; list = list->next, i++)
341                         timeservers[i] = g_strdup(list->data);
342         }
343
344         if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
345                 if (dhcp->timeservers) {
346                         for (i = 0; dhcp->timeservers[i]; i++)
347                                 __connman_service_timeserver_remove(service,
348                                                         dhcp->timeservers[i]);
349                         g_strfreev(dhcp->timeservers);
350                 }
351
352                 dhcp->timeservers = timeservers;
353
354                 for (i = 0; dhcp->timeservers &&
355                                         dhcp->timeservers[i]; i++)
356                         __connman_service_timeserver_append(service,
357                                                         dhcp->timeservers[i]);
358         } else
359                 g_strfreev(timeservers);
360
361
362         if (dhcp->callback) {
363                 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
364                 dhcp->callback(dhcp->network, status == 0 ?
365                                                 CONNMAN_DHCPV6_STATUS_SUCCEED :
366                                                 CONNMAN_DHCPV6_STATUS_FAIL,
367                                 NULL);
368         }
369 }
370
371 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
372 {
373         struct connman_service *service;
374         GDHCPClient *dhcp_client;
375         GDHCPClientError error;
376         int index, ret;
377
378         DBG("dhcp %p", dhcp);
379
380         index = connman_network_get_index(dhcp->network);
381
382         dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
383         if (error != G_DHCP_CLIENT_ERROR_NONE) {
384                 clear_timer(dhcp);
385                 return -EINVAL;
386         }
387
388         if (getenv("CONNMAN_DHCPV6_DEBUG"))
389                 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
390
391         service = connman_service_lookup_from_network(dhcp->network);
392         if (!service) {
393                 clear_timer(dhcp);
394                 g_dhcp_client_unref(dhcp_client);
395                 return -EINVAL;
396         }
397
398         ret = set_duid(service, dhcp->network, dhcp_client, index);
399         if (ret < 0) {
400                 clear_timer(dhcp);
401                 g_dhcp_client_unref(dhcp_client);
402                 return ret;
403         }
404
405         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
406         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
407         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
408         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
409
410         g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
411                                 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
412
413         g_dhcp_client_register_event(dhcp_client,
414                         G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
415
416         dhcp->dhcp_client = dhcp_client;
417
418         return g_dhcp_client_start(dhcp_client, NULL);
419 }
420
421 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
422 {
423         struct in6_addr addr_prefix, addr;
424         GSList *list;
425         int ret = 128, len;
426
427         for (list = prefixes; list; list = list->next) {
428                 char *prefix = list->data;
429                 const char *slash = g_strrstr(prefix, "/");
430                 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
431                                                 0xF0, 0xE0, 0xC0, 0x80 };
432                 int left, count, i, plen;
433
434                 if (!slash)
435                         continue;
436
437                 prefix = g_strndup(prefix, slash - prefix);
438                 len = strtol(slash + 1, NULL, 10);
439                 if (len < 3 || len > 128)
440                         break;
441
442                 plen = 128 - len;
443
444                 count = plen / 8;
445                 left = plen % 8;
446                 i = 16 - count;
447
448                 inet_pton(AF_INET6, prefix, &addr_prefix);
449                 inet_pton(AF_INET6, address, &addr);
450
451                 memset(&addr_prefix.s6_addr[i], 0, count);
452                 memset(&addr.s6_addr[i], 0, count);
453
454                 if (left) {
455                         addr_prefix.s6_addr[i - 1] &= bits[left];
456                         addr.s6_addr[i - 1] &= bits[left];
457                 }
458
459                 g_free(prefix);
460
461                 if (memcmp(&addr_prefix, &addr, 16) == 0) {
462                         ret = len;
463                         break;
464                 }
465         }
466
467         return ret;
468 }
469
470 static int set_other_addresses(GDHCPClient *dhcp_client,
471                                                 struct connman_dhcpv6 *dhcp)
472 {
473         struct connman_service *service;
474         int entries, i;
475         GList *option, *list;
476         char **nameservers, **timeservers;
477
478         service = connman_service_lookup_from_network(dhcp->network);
479         if (!service) {
480                 connman_error("Can not lookup service");
481                 return -EINVAL;
482         }
483
484         /*
485          * Check domains before nameservers so that the nameserver append
486          * function will update domain list in service.c
487          */
488         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DOMAIN_LIST);
489         entries = g_list_length(option);
490         if (entries > 0) {
491                 char **domains = g_try_new0(char *, entries + 1);
492                 if (domains) {
493                         for (i = 0, list = option; list;
494                                                 list = list->next, i++)
495                                 domains[i] = g_strdup(list->data);
496                         __connman_service_update_search_domains(service, domains);
497                         g_strfreev(domains);
498                 }
499         }
500
501         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
502         entries = g_list_length(option);
503
504         nameservers = g_try_new0(char *, entries + 1);
505         if (nameservers) {
506                 for (i = 0, list = option; list; list = list->next, i++)
507                         nameservers[i] = g_strdup(list->data);
508         }
509
510         if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
511                 if (dhcp->nameservers) {
512                         for (i = 0; dhcp->nameservers[i]; i++)
513                                 __connman_service_nameserver_remove(service,
514                                                         dhcp->nameservers[i],
515                                                         false);
516                         g_strfreev(dhcp->nameservers);
517                 }
518
519                 dhcp->nameservers = nameservers;
520
521                 for (i = 0; dhcp->nameservers &&
522                                         dhcp->nameservers[i]; i++)
523                         __connman_service_nameserver_append(service,
524                                                         dhcp->nameservers[i],
525                                                         false);
526         } else
527                 g_strfreev(nameservers);
528
529
530         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
531         entries = g_list_length(option);
532
533         timeservers = g_try_new0(char *, entries + 1);
534         if (timeservers) {
535                 for (i = 0, list = option; list; list = list->next, i++)
536                         timeservers[i] = g_strdup(list->data);
537         }
538
539         if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
540                 if (dhcp->timeservers) {
541                         for (i = 0; dhcp->timeservers[i]; i++)
542                                 __connman_service_timeserver_remove(service,
543                                                         dhcp->timeservers[i]);
544                         g_strfreev(dhcp->timeservers);
545                 }
546
547                 dhcp->timeservers = timeservers;
548
549                 for (i = 0; dhcp->timeservers &&
550                                         dhcp->timeservers[i]; i++)
551                         __connman_service_timeserver_append(service,
552                                                         dhcp->timeservers[i]);
553         } else
554                 g_strfreev(timeservers);
555
556         return 0;
557 }
558
559 static GSList *copy_prefixes(GSList *prefixes)
560 {
561         GSList *list, *copy = NULL;
562
563         for (list = prefixes; list; list = list->next)
564                 copy = g_slist_prepend(copy, g_strdup(list->data));
565
566         return copy;
567 }
568
569 /*
570  * Helper struct for doing DAD (duplicate address detection).
571  * It is refcounted and freed after all reply's to neighbor
572  * discovery request are received.
573  */
574 struct own_address {
575         int refcount;
576
577         int ifindex;
578         GDHCPClient *dhcp_client;
579         struct connman_ipconfig *ipconfig;
580         GSList *prefixes;
581         dhcpv6_cb callback;
582
583         GSList *dad_failed;
584         GSList *dad_succeed;
585 };
586
587 static void free_own_address(struct own_address *data)
588 {
589         g_dhcp_client_unref(data->dhcp_client);
590         __connman_ipconfig_unref(data->ipconfig);
591         g_slist_free_full(data->prefixes, free_prefix);
592         g_slist_free_full(data->dad_failed, g_free);
593         g_slist_free_full(data->dad_succeed, g_free);
594
595         g_free(data);
596 }
597
598 static struct own_address *ref_own_address(struct own_address *address)
599 {
600         DBG("%p ref %d", address, address->refcount + 1);
601
602         __sync_fetch_and_add(&address->refcount, 1);
603
604         return address;
605 }
606
607 static void unref_own_address(struct own_address *address)
608 {
609         if (!address)
610                 return;
611
612         DBG("%p ref %d", address, address->refcount - 1);
613
614         if (__sync_fetch_and_sub(&address->refcount, 1) != 1)
615                 return;
616
617         free_own_address(address);
618 }
619
620 static void set_address(int ifindex, struct connman_ipconfig *ipconfig,
621                         GSList *prefixes, char *address)
622 {
623         const char *c_address;
624
625         c_address = __connman_ipconfig_get_local(ipconfig);
626
627         if (address && ((c_address && g_strcmp0(address, c_address) != 0) ||
628                                                                 !c_address)) {
629                 int prefix_len;
630
631                 /* Is this prefix part of the subnet we are suppose to use? */
632                 prefix_len = check_ipv6_addr_prefix(prefixes, address);
633
634                 __connman_ipconfig_address_remove(ipconfig);
635                 __connman_ipconfig_set_local(ipconfig, address);
636                 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
637
638                 DBG("new address %s/%d", address, prefix_len);
639
640                 __connman_ipconfig_set_dhcp_address(ipconfig, address);
641                 __connman_service_save(
642                         __connman_service_lookup_from_index(ifindex));
643         }
644 }
645
646
647 /*
648  * Helper struct that is used when waiting a reply to DECLINE message.
649  */
650 struct decline_cb_data {
651         GDHCPClient *dhcp_client;
652         dhcpv6_cb callback;
653         int ifindex;
654         guint timeout;
655 };
656
657 static void decline_reply_callback(struct decline_cb_data *data)
658 {
659         struct connman_network *network;
660         struct connman_service *service;
661
662         service = __connman_service_lookup_from_index(data->ifindex);
663         network = __connman_service_get_network(service);
664
665         if (data->callback)
666                 data->callback(network, CONNMAN_DHCPV6_STATUS_RESTART, NULL);
667
668         g_dhcp_client_unref(data->dhcp_client);
669 }
670
671 static gboolean decline_timeout(gpointer user_data)
672 {
673         struct decline_cb_data *data = user_data;
674
675         DBG("ifindex %d", data->ifindex);
676
677         /*
678          * We ignore all DECLINE replies that are received after the timeout
679          */
680         g_dhcp_client_register_event(data->dhcp_client,
681                                         G_DHCP_CLIENT_EVENT_DECLINE,
682                                         NULL, NULL);
683
684         decline_reply_callback(data);
685
686         g_free(data);
687
688         return FALSE;
689 }
690
691 static void decline_cb(GDHCPClient *dhcp_client, gpointer user_data)
692 {
693         struct decline_cb_data *data = user_data;
694
695         DBG("ifindex %d", data->ifindex);
696
697         g_dhcpv6_client_clear_retransmit(dhcp_client);
698
699         if (data->timeout)
700                 g_source_remove(data->timeout);
701
702         decline_reply_callback(data);
703
704         g_free(data);
705 }
706
707 static int dhcpv6_decline(GDHCPClient *dhcp_client, int ifindex,
708                         dhcpv6_cb callback, GSList *failed)
709 {
710         struct decline_cb_data *data;
711         GList *option;
712         int code;
713
714         DBG("dhcp_client %p", dhcp_client);
715
716         g_dhcp_client_clear_requests(dhcp_client);
717
718         g_dhcpv6_client_clear_send(dhcp_client, G_DHCPV6_ORO);
719
720         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
721         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
722
723         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
724         if (!option) {
725                 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
726                 if (option)
727                         code = G_DHCPV6_IA_TA;
728                 else
729                         return -EINVAL;
730         } else
731                 code = G_DHCPV6_IA_NA;
732
733         g_dhcpv6_client_clear_send(dhcp_client, code);
734
735         g_dhcpv6_client_set_ias(dhcp_client, ifindex, code, NULL, NULL,
736                                 failed);
737
738         clear_callbacks(dhcp_client);
739
740         data = g_try_new(struct decline_cb_data, 1);
741         if (!data)
742                 return -ENOMEM;
743
744         data->ifindex = ifindex;
745         data->callback = callback;
746         data->dhcp_client = g_dhcp_client_ref(dhcp_client);
747         data->timeout = g_timeout_add(DEC_TIMEOUT, decline_timeout, data);
748
749         g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_DECLINE,
750                                 decline_cb, data);
751
752         return g_dhcp_client_start(dhcp_client, NULL);
753 }
754
755 static void dad_reply(struct nd_neighbor_advert *reply,
756                 unsigned int length, struct in6_addr *addr, void *user_data)
757 {
758         struct own_address *data = user_data;
759         GSList *list;
760         char address[INET6_ADDRSTRLEN];
761         enum __connman_dhcpv6_status status = CONNMAN_DHCPV6_STATUS_FAIL;
762
763         inet_ntop(AF_INET6, addr, address, INET6_ADDRSTRLEN);
764
765         DBG("user %p reply %p len %d address %s index %d data %p", user_data,
766                 reply, length, address, data->ifindex, data);
767
768         if (!reply) {
769                 if (length == 0)
770                         DBG("DAD succeed for %s", address);
771                 else
772                         DBG("DAD cannot be done for %s", address);
773
774                 data->dad_succeed = g_slist_prepend(data->dad_succeed,
775                                                 g_strdup(address));
776
777         } else {
778                 DBG("DAD failed for %s", address);
779
780                 data->dad_failed = g_slist_prepend(data->dad_failed,
781                                                 g_strdup(address));
782         }
783
784         /*
785          * If refcount == 1 then we got the final reply and can continue.
786          */
787         if (data->refcount > 1)
788                 return;
789
790         for (list = data->dad_succeed; list; list = list->next)
791                 set_address(data->ifindex, data->ipconfig, data->prefixes,
792                                                                 list->data);
793
794         if (data->dad_failed) {
795                 dhcpv6_decline(data->dhcp_client, data->ifindex,
796                         data->callback, data->dad_failed);
797         } else {
798                 if (data->dad_succeed)
799                         status = CONNMAN_DHCPV6_STATUS_SUCCEED;
800
801                 if (data->callback) {
802                         struct connman_network *network;
803                         struct connman_service *service;
804
805                         service = __connman_service_lookup_from_index(
806                                                                 data->ifindex);
807                         network = __connman_service_get_network(service);
808                         if (network)
809                                 data->callback(network, status, NULL);
810                 }
811         }
812
813         unref_own_address(data);
814 }
815
816 /*
817  * Is the kernel configured to do DAD? If 0, then do not do DAD.
818  * See also RFC 4862 chapter 5.4 about DupAddrDetectTransmits
819  */
820 static int dad_transmits(int ifindex)
821 {
822         char name[IF_NAMESIZE];
823         gchar *path;
824         int value = 1;
825         FILE *f;
826
827         if (!if_indextoname(ifindex, name))
828                 return value;
829
830         path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/dad_transmits",
831                                                                 name);
832
833         if (!path)
834                 return value;
835
836         f = fopen(path, "r");
837
838         g_free(path);
839
840         if (f) {
841                 if (fscanf(f, "%d", &value) < 0)
842                         value = 1;
843
844                 fclose(f);
845         }
846
847         return value;
848 }
849
850 static void do_dad(GDHCPClient *dhcp_client, struct connman_dhcpv6 *dhcp)
851 {
852         struct connman_service *service;
853         struct connman_ipconfig *ipconfig;
854         int ifindex;
855         GList *option, *list;
856         struct own_address *user_data;
857
858         option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
859         if (!option)
860                 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
861
862         /*
863          * Even if we didn't had any addresses, just try to set
864          * the other options.
865          */
866         set_other_addresses(dhcp_client, dhcp);
867
868         if (!option) {
869                 DBG("Skip DAD as no addresses found in reply");
870                 if (dhcp->callback)
871                         dhcp->callback(dhcp->network,
872                                         CONNMAN_DHCPV6_STATUS_SUCCEED, NULL);
873
874                 return;
875         }
876
877         ifindex = connman_network_get_index(dhcp->network);
878
879         DBG("index %d", ifindex);
880
881         service = connman_service_lookup_from_network(dhcp->network);
882         if (!service) {
883                 connman_error("Can not lookup service for index %d", ifindex);
884                 goto error;
885         }
886
887         ipconfig = __connman_service_get_ip6config(service);
888         if (!ipconfig) {
889                 connman_error("Could not lookup ip6config for index %d",
890                                                                 ifindex);
891                 goto error;
892         }
893
894         if (!dad_transmits(ifindex)) {
895                 DBG("Skip DAD because of kernel configuration");
896
897                 for (list = option; list; list = list->next)
898                         set_address(ifindex, ipconfig, dhcp->prefixes,
899                                                         option->data);
900
901                 if (dhcp->callback)
902                         dhcp->callback(dhcp->network,
903                                         CONNMAN_DHCPV6_STATUS_SUCCEED, NULL);
904
905                 return;
906         }
907
908         if (g_list_length(option) == 0) {
909                 DBG("No addresses when doing DAD");
910                 if (dhcp->callback)
911                         dhcp->callback(dhcp->network,
912                                         CONNMAN_DHCPV6_STATUS_SUCCEED, NULL);
913
914                 return;
915         }
916
917         user_data = g_try_new0(struct own_address, 1);
918         if (!user_data)
919                 goto error;
920
921         user_data->refcount = 0;
922         user_data->ifindex = ifindex;
923         user_data->dhcp_client = g_dhcp_client_ref(dhcp_client);
924         user_data->ipconfig = __connman_ipconfig_ref(ipconfig);
925         user_data->prefixes = copy_prefixes(dhcp->prefixes);
926         user_data->callback = dhcp->callback;
927
928         /*
929          * We send one neighbor discovery request / address
930          * and after all checks are done, then report the status
931          * via dhcp callback.
932          */
933
934         for (list = option; list; list = list->next) {
935                 char *address = option->data;
936                 struct in6_addr addr;
937                 int ret;
938
939                 ref_own_address(user_data);
940
941                 if (inet_pton(AF_INET6, address, &addr) < 0) {
942                         DBG("Invalid IPv6 address %s %d/%s", address,
943                                 -errno, strerror(errno));
944                         goto fail;
945                 }
946
947                 DBG("user %p address %s client %p ipconfig %p prefixes %p",
948                         user_data, address,
949                         user_data->dhcp_client, user_data->ipconfig,
950                         user_data->prefixes);
951
952                 ret = __connman_inet_ipv6_do_dad(ifindex, 1000,
953                                                 &addr,
954                                                 dad_reply,
955                                                 user_data);
956                 if (ret < 0) {
957                         DBG("Could not send neighbor solicitation for %s",
958                                                                 address);
959                         dad_reply(NULL, -1, &addr, user_data);
960                 } else {
961                         DBG("Sent neighbor solicitation %d bytes", ret);
962                 }
963         }
964
965         return;
966
967 fail:
968         unref_own_address(user_data);
969
970 error:
971         if (dhcp->callback)
972                 dhcp->callback(dhcp->network, CONNMAN_DHCPV6_STATUS_FAIL,
973                                                                 NULL);
974 }
975
976 static gboolean timeout_request_resend(gpointer user_data)
977 {
978         struct connman_dhcpv6 *dhcp = user_data;
979
980         if (dhcp->request_count >= REQ_MAX_RC) {
981                 DBG("max request retry attempts %d", dhcp->request_count);
982                 dhcp->request_count = 0;
983                 if (dhcp->callback)
984                         dhcp->callback(dhcp->network,
985                                         CONNMAN_DHCPV6_STATUS_FAIL, NULL);
986                 return FALSE;
987         }
988
989         dhcp->request_count++;
990
991         dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
992         DBG("request resend RT timeout %d msec", dhcp->RT);
993         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request_resend, dhcp);
994
995         g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
996
997         g_dhcp_client_start(dhcp->dhcp_client, NULL);
998
999         return FALSE;
1000 }
1001
1002 static gboolean request_resend(gpointer user_data)
1003 {
1004         struct connman_dhcpv6 *dhcp = user_data;
1005
1006         g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1007
1008         dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
1009         DBG("request resend RT timeout %d msec", dhcp->RT);
1010         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request_resend, dhcp);
1011
1012         dhcpv6_request(dhcp, true);
1013
1014         return FALSE;
1015 }
1016
1017 static void do_resend_request(struct connman_dhcpv6 *dhcp)
1018 {
1019         if (dhcp->request_count >= REQ_MAX_RC) {
1020                 DBG("max request retry attempts %d", dhcp->request_count);
1021                 dhcp->request_count = 0;
1022                 if (dhcp->callback)
1023                         dhcp->callback(dhcp->network,
1024                                         CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1025                 return;
1026         }
1027
1028         dhcp->request_count++;
1029
1030         dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
1031         DBG("resending request after %d msec", dhcp->RT);
1032         dhcp->timeout = g_timeout_add(dhcp->RT, request_resend, dhcp);
1033 }
1034
1035 static void re_cb(enum request_type req_type, GDHCPClient *dhcp_client,
1036                 gpointer user_data)
1037 {
1038         struct connman_dhcpv6 *dhcp = user_data;
1039         uint16_t status;
1040
1041         clear_timer(dhcp);
1042
1043         status = g_dhcpv6_client_get_status(dhcp_client);
1044
1045         DBG("dhcpv6 cb msg %p status %d", dhcp, status);
1046
1047         /*
1048          * RFC 3315, 18.1.8 handle the resend if error
1049          */
1050         if (status  == G_DHCPV6_ERROR_BINDING) {
1051                 dhcpv6_request(dhcp, false);
1052         } else if (status  == G_DHCPV6_ERROR_MCAST) {
1053                 switch (req_type) {
1054                 case REQ_REQUEST:
1055                         dhcpv6_request(dhcp, true);
1056                         break;
1057                 case REQ_REBIND:
1058                         dhcpv6_rebind(dhcp);
1059                         break;
1060                 case REQ_RENEW:
1061                         dhcpv6_renew(dhcp);
1062                         break;
1063                 }
1064         } else if (status  == G_DHCPV6_ERROR_LINK) {
1065                 if (req_type == REQ_REQUEST) {
1066                         g_dhcp_client_unref(dhcp->dhcp_client);
1067                         start_solicitation(dhcp);
1068                 } else {
1069                         if (dhcp->callback)
1070                                 dhcp->callback(dhcp->network,
1071                                         CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1072                 }
1073         } else if (status  == G_DHCPV6_ERROR_FAILURE) {
1074                 if (req_type == REQ_REQUEST) {
1075                         /* Rate limit the resend of request message */
1076                         do_resend_request(dhcp);
1077                 } else {
1078                         if (dhcp->callback)
1079                                 dhcp->callback(dhcp->network,
1080                                         CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1081                 }
1082         } else {
1083
1084                 /*
1085                  * If we did not got any addresses, then re-send
1086                  * a request.
1087                  */
1088                 GList *option;
1089
1090                 option = g_dhcp_client_get_option(dhcp->dhcp_client,
1091                                                 G_DHCPV6_IA_NA);
1092                 if (!option) {
1093                         option = g_dhcp_client_get_option(dhcp->dhcp_client,
1094                                                         G_DHCPV6_IA_TA);
1095                         if (!option) {
1096                                 switch (req_type) {
1097                                 case REQ_REQUEST:
1098                                         dhcpv6_request(dhcp, true);
1099                                         break;
1100                                 case REQ_REBIND:
1101                                         dhcpv6_rebind(dhcp);
1102                                         break;
1103                                 case REQ_RENEW:
1104                                         dhcpv6_renew(dhcp);
1105                                         break;
1106                                 }
1107                                 return;
1108                         }
1109                 }
1110
1111                 if (status == G_DHCPV6_ERROR_SUCCESS)
1112                         do_dad(dhcp_client, dhcp);
1113                 else if (dhcp->callback)
1114                         dhcp->callback(dhcp->network,
1115                                 CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1116         }
1117 }
1118
1119 static void rebind_cb(GDHCPClient *dhcp_client, gpointer user_data)
1120 {
1121         DBG("");
1122
1123         g_dhcpv6_client_reset_request(dhcp_client);
1124         g_dhcpv6_client_clear_retransmit(dhcp_client);
1125
1126         re_cb(REQ_REBIND, dhcp_client, user_data);
1127 }
1128
1129 static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp)
1130 {
1131         GDHCPClient *dhcp_client;
1132
1133         DBG("dhcp %p", dhcp);
1134
1135         dhcp_client = dhcp->dhcp_client;
1136
1137         g_dhcp_client_clear_requests(dhcp_client);
1138
1139         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1140         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1141         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
1142         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1143
1144         g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
1145                                 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
1146
1147         g_dhcpv6_client_set_ia(dhcp_client,
1148                         connman_network_get_index(dhcp->network),
1149                         dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1150                         NULL, NULL, TRUE, NULL);
1151
1152         clear_callbacks(dhcp_client);
1153
1154         g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
1155                                         rebind_cb, dhcp);
1156
1157         dhcp->dhcp_client = dhcp_client;
1158
1159         return g_dhcp_client_start(dhcp_client, NULL);
1160 }
1161
1162 static gboolean dhcpv6_restart(gpointer user_data)
1163 {
1164         struct connman_dhcpv6 *dhcp = user_data;
1165
1166         if (dhcp->callback)
1167                 dhcp->callback(dhcp->network, CONNMAN_DHCPV6_STATUS_FAIL,
1168                                                                 NULL);
1169
1170         return FALSE;
1171 }
1172
1173 /*
1174  * Check if we need to restart the solicitation procedure. This
1175  * is done if all the addresses have expired. RFC 3315, 18.1.4
1176  */
1177 static int check_restart(struct connman_dhcpv6 *dhcp)
1178 {
1179         time_t current, expired;
1180
1181         g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
1182                                 NULL, &expired);
1183         current = time(NULL);
1184
1185         if (current >= expired) {
1186                 DBG("expired by %d secs", (int)(current - expired));
1187
1188                 g_timeout_add(0, dhcpv6_restart, dhcp);
1189
1190                 return -ETIMEDOUT;
1191         }
1192
1193         return 0;
1194 }
1195
1196 static gboolean timeout_rebind(gpointer user_data)
1197 {
1198         struct connman_dhcpv6 *dhcp = user_data;
1199
1200         if (check_restart(dhcp) < 0)
1201                 return FALSE;
1202
1203         dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
1204
1205         DBG("rebind RT timeout %d msec", dhcp->RT);
1206
1207         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
1208
1209         g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1210
1211         g_dhcp_client_start(dhcp->dhcp_client, NULL);
1212
1213         return FALSE;
1214 }
1215
1216 static gboolean start_rebind(gpointer user_data)
1217 {
1218         struct connman_dhcpv6 *dhcp = user_data;
1219
1220         if (check_restart(dhcp) < 0)
1221                 return FALSE;
1222
1223         dhcp->RT = REB_TIMEOUT * (1 + get_random());
1224
1225         DBG("rebind initial RT timeout %d msec", dhcp->RT);
1226
1227         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
1228
1229         dhcpv6_rebind(dhcp);
1230
1231         return FALSE;
1232 }
1233
1234 static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
1235 {
1236         DBG("");
1237
1238         g_dhcpv6_client_reset_request(dhcp_client);
1239         g_dhcpv6_client_clear_retransmit(dhcp_client);
1240
1241         re_cb(REQ_REQUEST, dhcp_client, user_data);
1242 }
1243
1244 static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
1245                         bool add_addresses)
1246 {
1247         GDHCPClient *dhcp_client;
1248         uint32_t T1, T2;
1249
1250         DBG("dhcp %p add %d", dhcp, add_addresses);
1251
1252         dhcp_client = dhcp->dhcp_client;
1253
1254         g_dhcp_client_clear_requests(dhcp_client);
1255
1256         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1257         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
1258         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1259         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
1260         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1261
1262         g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
1263                                 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
1264
1265         g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
1266         g_dhcpv6_client_set_ia(dhcp_client,
1267                         connman_network_get_index(dhcp->network),
1268                         dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1269                         &T1, &T2, add_addresses, NULL);
1270
1271         clear_callbacks(dhcp_client);
1272
1273         g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
1274                                         request_cb, dhcp);
1275
1276         dhcp->dhcp_client = dhcp_client;
1277
1278         return g_dhcp_client_start(dhcp_client, NULL);
1279 }
1280
1281 static gboolean timeout_request(gpointer user_data)
1282 {
1283         struct connman_dhcpv6 *dhcp = user_data;
1284
1285         if (dhcp->request_count >= REQ_MAX_RC) {
1286                 DBG("max request retry attempts %d", dhcp->request_count);
1287                 dhcp->request_count = 0;
1288                 if (dhcp->callback)
1289                         dhcp->callback(dhcp->network,
1290                                         CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1291                 return FALSE;
1292         }
1293
1294         dhcp->request_count++;
1295
1296         dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
1297         DBG("request RT timeout %d msec", dhcp->RT);
1298         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
1299
1300         g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1301
1302         g_dhcp_client_start(dhcp->dhcp_client, NULL);
1303
1304         return FALSE;
1305 }
1306
1307 static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
1308 {
1309         DBG("");
1310
1311         g_dhcpv6_client_reset_request(dhcp_client);
1312         g_dhcpv6_client_clear_retransmit(dhcp_client);
1313
1314         re_cb(REQ_RENEW, dhcp_client, user_data);
1315 }
1316
1317 static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
1318 {
1319         GDHCPClient *dhcp_client;
1320         uint32_t T1, T2;
1321
1322         DBG("dhcp %p", dhcp);
1323
1324         dhcp_client = dhcp->dhcp_client;
1325
1326         g_dhcp_client_clear_requests(dhcp_client);
1327
1328         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1329         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
1330         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1331         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
1332         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1333
1334         g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
1335                                 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
1336
1337         g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
1338         g_dhcpv6_client_set_ia(dhcp_client,
1339                         connman_network_get_index(dhcp->network),
1340                         dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1341                         &T1, &T2, TRUE, NULL);
1342
1343         clear_callbacks(dhcp_client);
1344
1345         g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
1346                                         renew_cb, dhcp);
1347
1348         dhcp->dhcp_client = dhcp_client;
1349
1350         return g_dhcp_client_start(dhcp_client, NULL);
1351 }
1352
1353 static gboolean timeout_renew(gpointer user_data)
1354 {
1355         struct connman_dhcpv6 *dhcp = user_data;
1356         time_t last_rebind, current;
1357         uint32_t T2;
1358
1359         if (check_restart(dhcp) < 0)
1360                 return FALSE;
1361
1362         g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, &T2,
1363                                 &last_rebind, NULL);
1364         current = time(NULL);
1365         if ((unsigned)current >= (unsigned)last_rebind + T2) {
1366                 /*
1367                  * Do rebind instead if past T2
1368                  */
1369                 start_rebind(dhcp);
1370                 return FALSE;
1371         }
1372
1373         dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
1374
1375         DBG("renew RT timeout %d msec", dhcp->RT);
1376
1377         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
1378
1379         g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1380
1381         g_dhcp_client_start(dhcp->dhcp_client, NULL);
1382
1383         return FALSE;
1384 }
1385
1386 static gboolean start_renew(gpointer user_data)
1387 {
1388         struct connman_dhcpv6 *dhcp = user_data;
1389
1390         dhcp->RT = REN_TIMEOUT * (1 + get_random());
1391
1392         DBG("renew initial RT timeout %d msec", dhcp->RT);
1393
1394         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
1395
1396         dhcpv6_renew(dhcp);
1397
1398         return FALSE;
1399 }
1400
1401 int __connman_dhcpv6_start_renew(struct connman_network *network,
1402                                                         dhcpv6_cb callback)
1403 {
1404         struct connman_dhcpv6 *dhcp;
1405         uint32_t T1, T2, delta;
1406         time_t started, current, expired;
1407
1408         dhcp = g_hash_table_lookup(network_table, network);
1409         if (!dhcp)
1410                 return -ENOENT;
1411
1412         DBG("network %p dhcp %p", network, dhcp);
1413
1414         clear_timer(dhcp);
1415
1416         g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
1417                                 &started, &expired);
1418
1419         current = time(NULL);
1420
1421         DBG("T1 %u T2 %u expires %lu current %lu started %lu", T1, T2,
1422                 (unsigned long)expired, current, started);
1423
1424         if (T1 == 0xffffffff)
1425                 /* RFC 3315, 22.4 */
1426                 return 0;
1427
1428         if (T1 == 0) {
1429                 /* RFC 3315, 22.4
1430                  * Client can choose the timeout.
1431                  */
1432                 T1 = (expired - started) / 2;
1433                 T2 = (expired - started) / 10 * 8;
1434         }
1435
1436         dhcp->callback = callback;
1437
1438         /* RFC 3315, 18.1.4, start solicit if expired */
1439         if (check_restart(dhcp) < 0)
1440                 return 0;
1441
1442         if (T2 != 0xffffffff && T2 > 0) {
1443                 if ((unsigned)current >= (unsigned)started + T2) {
1444                         /* RFC 3315, chapter 18.1.3, start rebind */
1445                         DBG("start rebind immediately");
1446
1447                         dhcp->timeout = g_timeout_add_seconds(0, start_rebind,
1448                                                         dhcp);
1449
1450                 } else if ((unsigned)current < (unsigned)started + T1) {
1451                         delta = started + T1 - current;
1452                         DBG("renew after %d secs", delta);
1453
1454                         dhcp->timeout = g_timeout_add_seconds(delta,
1455                                         start_renew, dhcp);
1456                 } else {
1457                         delta = started + T2 - current;
1458                         DBG("rebind after %d secs", delta);
1459
1460                         dhcp->timeout = g_timeout_add_seconds(delta,
1461                                         start_rebind, dhcp);
1462                 }
1463         }
1464
1465         return 0;
1466 }
1467
1468 static void release_cb(GDHCPClient *dhcp_client, gpointer user_data)
1469 {
1470         DBG("");
1471 }
1472
1473 int __connman_dhcpv6_start_release(struct connman_network *network,
1474                                 dhcpv6_cb callback)
1475 {
1476         struct connman_dhcpv6 *dhcp;
1477         GDHCPClient *dhcp_client;
1478
1479         if (!network_table)
1480                 return 0;   /* we are already released */
1481
1482         dhcp = g_hash_table_lookup(network_table, network);
1483         if (!dhcp)
1484                 return -ENOENT;
1485
1486         DBG("network %p dhcp %p client %p stateless %d", network, dhcp,
1487                                         dhcp->dhcp_client, dhcp->stateless);
1488
1489         if (dhcp->stateless)
1490                 return -EINVAL;
1491
1492         clear_timer(dhcp);
1493
1494         dhcp_client = dhcp->dhcp_client;
1495         if (!dhcp_client) {
1496                 /*
1497                  * We had started the DHCPv6 handshaking i.e., we have called
1498                  * __connman_dhcpv6_start() but it has not yet sent
1499                  * a solicitation message to server. This means that we do not
1500                  * have DHCPv6 configured yet so we can just quit here.
1501                  */
1502                 DBG("DHCPv6 was not started");
1503                 return 0;
1504         }
1505
1506         g_dhcp_client_clear_requests(dhcp_client);
1507         g_dhcp_client_clear_values(dhcp_client);
1508
1509         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1510         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
1511
1512         g_dhcpv6_client_set_ia(dhcp_client,
1513                         connman_network_get_index(dhcp->network),
1514                         dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1515                         NULL, NULL, TRUE, NULL);
1516
1517         clear_callbacks(dhcp_client);
1518
1519         /*
1520          * Although we register a callback here we are really not interested in
1521          * the answer because it might take too long time and network code
1522          * might be in the middle of the disconnect.
1523          * So we just inform the server that we are done with the addresses
1524          * but ignore the reply from server. This is allowed by RFC 3315
1525          * chapter 18.1.6.
1526          */
1527         g_dhcp_client_register_event(dhcp_client,
1528                                 G_DHCP_CLIENT_EVENT_RELEASE,
1529                                 release_cb, NULL);
1530
1531         dhcp->dhcp_client = dhcp_client;
1532
1533         return g_dhcp_client_start(dhcp_client, NULL);
1534 }
1535
1536 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
1537 {
1538         DBG("dhcp %p", dhcp);
1539
1540         clear_timer(dhcp);
1541
1542         dhcpv6_free(dhcp);
1543
1544         if (!dhcp->dhcp_client)
1545                 return 0;
1546
1547         g_dhcp_client_stop(dhcp->dhcp_client);
1548         g_dhcp_client_unref(dhcp->dhcp_client);
1549
1550         dhcp->dhcp_client = NULL;
1551
1552         return 0;
1553 }
1554
1555 static void remove_network(gpointer user_data)
1556 {
1557         struct connman_dhcpv6 *dhcp = user_data;
1558
1559         DBG("dhcp %p", dhcp);
1560
1561         dhcpv6_release(dhcp);
1562
1563         g_free(dhcp);
1564 }
1565
1566 static gboolean timeout_info_req(gpointer user_data)
1567 {
1568         struct connman_dhcpv6 *dhcp = user_data;
1569
1570         dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
1571
1572         DBG("info RT timeout %d msec", dhcp->RT);
1573
1574         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
1575
1576         g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1577
1578         g_dhcp_client_start(dhcp->dhcp_client, NULL);
1579
1580         return FALSE;
1581 }
1582
1583 static gboolean start_info_req(gpointer user_data)
1584 {
1585         struct connman_dhcpv6 *dhcp = user_data;
1586
1587         /* Set the retransmission timeout, RFC 3315 chapter 14 */
1588         dhcp->RT = INF_TIMEOUT * (1 + get_random());
1589
1590         DBG("info initial RT timeout %d msec", dhcp->RT);
1591
1592         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
1593
1594         dhcpv6_info_request(dhcp);
1595
1596         return FALSE;
1597 }
1598
1599 int __connman_dhcpv6_start_info(struct connman_network *network,
1600                                 dhcpv6_cb callback)
1601 {
1602         struct connman_dhcpv6 *dhcp;
1603         int delay;
1604
1605         DBG("");
1606
1607         if (network_table) {
1608                 dhcp = g_hash_table_lookup(network_table, network);
1609                 if (dhcp && dhcp->started)
1610                         return -EBUSY;
1611         }
1612
1613         dhcp = g_try_new0(struct connman_dhcpv6, 1);
1614         if (!dhcp)
1615                 return -ENOMEM;
1616
1617         dhcp->network = network;
1618         dhcp->callback = callback;
1619         dhcp->stateless = true;
1620         dhcp->started = true;
1621
1622         connman_network_ref(network);
1623
1624         DBG("replace network %p dhcp %p", network, dhcp);
1625
1626         g_hash_table_replace(network_table, network, dhcp);
1627
1628         /* Initial timeout, RFC 3315, 18.1.5 */
1629         delay = rand() % 1000;
1630
1631         dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
1632
1633         return 0;
1634 }
1635
1636 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
1637 {
1638         struct connman_dhcpv6 *dhcp = user_data;
1639
1640         DBG("dhcpv6 advertise msg %p", dhcp);
1641
1642         clear_timer(dhcp);
1643
1644         g_dhcpv6_client_clear_retransmit(dhcp_client);
1645
1646         if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
1647                 if (dhcp->callback)
1648                         dhcp->callback(dhcp->network,
1649                                         CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1650                 return;
1651         }
1652
1653         dhcp->RT = REQ_TIMEOUT * (1 + get_random());
1654         DBG("request initial RT timeout %d msec", dhcp->RT);
1655         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
1656
1657         dhcp->request_count = 1;
1658
1659         dhcpv6_request(dhcp, true);
1660 }
1661
1662 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
1663 {
1664         /* We get here if server supports rapid commit */
1665         struct connman_dhcpv6 *dhcp = user_data;
1666
1667         DBG("dhcpv6 solicitation msg %p", dhcp);
1668
1669         clear_timer(dhcp);
1670
1671         do_dad(dhcp_client, dhcp);
1672
1673         g_dhcpv6_client_clear_retransmit(dhcp_client);
1674 }
1675
1676 static gboolean timeout_solicitation(gpointer user_data)
1677 {
1678         struct connman_dhcpv6 *dhcp = user_data;
1679
1680         dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
1681
1682         DBG("solicit RT timeout %d msec", dhcp->RT);
1683
1684         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1685
1686         g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
1687
1688         g_dhcp_client_start(dhcp->dhcp_client, NULL);
1689
1690         return FALSE;
1691 }
1692
1693 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
1694 {
1695         struct connman_service *service;
1696         struct connman_ipconfig *ipconfig_ipv6;
1697         GDHCPClient *dhcp_client;
1698         GDHCPClientError error;
1699         int index, ret;
1700
1701         DBG("dhcp %p", dhcp);
1702
1703         index = connman_network_get_index(dhcp->network);
1704
1705         dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
1706         if (error != G_DHCP_CLIENT_ERROR_NONE) {
1707                 clear_timer(dhcp);
1708                 return -EINVAL;
1709         }
1710
1711         if (getenv("CONNMAN_DHCPV6_DEBUG"))
1712                 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
1713
1714         service = connman_service_lookup_from_network(dhcp->network);
1715         if (!service) {
1716                 clear_timer(dhcp);
1717                 g_dhcp_client_unref(dhcp_client);
1718                 return -EINVAL;
1719         }
1720
1721         ret = set_duid(service, dhcp->network, dhcp_client, index);
1722         if (ret < 0) {
1723                 clear_timer(dhcp);
1724                 g_dhcp_client_unref(dhcp_client);
1725                 return ret;
1726         }
1727
1728         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
1729         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
1730         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
1731         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DOMAIN_LIST);
1732         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
1733
1734         g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
1735                                 G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
1736
1737         ipconfig_ipv6 = __connman_service_get_ip6config(service);
1738         dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
1739
1740         g_dhcpv6_client_set_ia(dhcp_client, index,
1741                         dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
1742                         NULL, NULL, FALSE, NULL);
1743
1744         clear_callbacks(dhcp_client);
1745
1746         g_dhcp_client_register_event(dhcp_client,
1747                                 G_DHCP_CLIENT_EVENT_SOLICITATION,
1748                                 solicitation_cb, dhcp);
1749
1750         g_dhcp_client_register_event(dhcp_client,
1751                                 G_DHCP_CLIENT_EVENT_ADVERTISE,
1752                                 advertise_cb, dhcp);
1753
1754         dhcp->dhcp_client = dhcp_client;
1755
1756         return g_dhcp_client_start(dhcp_client, NULL);
1757 }
1758
1759 static gboolean start_solicitation(gpointer user_data)
1760 {
1761         struct connman_dhcpv6 *dhcp = user_data;
1762
1763         /* Set the retransmission timeout, RFC 3315 chapter 14 */
1764         dhcp->RT = SOL_TIMEOUT * (1 + get_random());
1765
1766         DBG("solicit initial RT timeout %d msec", dhcp->RT);
1767
1768         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
1769
1770         dhcpv6_solicitation(dhcp);
1771
1772         return FALSE;
1773 }
1774
1775 int __connman_dhcpv6_start(struct connman_network *network,
1776                                 GSList *prefixes, dhcpv6_cb callback)
1777 {
1778         struct connman_service *service;
1779         struct connman_dhcpv6 *dhcp;
1780         int delay;
1781
1782         DBG("");
1783
1784         if (network_table) {
1785                 dhcp = g_hash_table_lookup(network_table, network);
1786                 if (dhcp && dhcp->started)
1787                         return -EBUSY;
1788         }
1789
1790         service = connman_service_lookup_from_network(network);
1791         if (!service)
1792                 return -EINVAL;
1793
1794         dhcp = g_try_new0(struct connman_dhcpv6, 1);
1795         if (!dhcp)
1796                 return -ENOMEM;
1797
1798         dhcp->network = network;
1799         dhcp->callback = callback;
1800         dhcp->prefixes = prefixes;
1801         dhcp->started = true;
1802
1803         connman_network_ref(network);
1804
1805         DBG("replace network %p dhcp %p", network, dhcp);
1806
1807         g_hash_table_replace(network_table, network, dhcp);
1808
1809         /* Initial timeout, RFC 3315, 17.1.2 */
1810         delay = rand() % 1000;
1811
1812         /*
1813          * Start from scratch.
1814          * RFC 3315, chapter 17.1.2 Solicitation message
1815          *
1816          * Note that we do not send CONFIRM message here as it does
1817          * not make much sense because we do not save expiration time
1818          * so we cannot really know how long the saved address is valid
1819          * anyway. The reply to CONFIRM message does not send
1820          * expiration times back to us. Because of this we need to
1821          * start using SOLICITATION anyway.
1822          */
1823         dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
1824
1825         return 0;
1826 }
1827
1828 void __connman_dhcpv6_stop(struct connman_network *network)
1829 {
1830         DBG("");
1831
1832         if (!network_table)
1833                 return;
1834
1835         if (g_hash_table_remove(network_table, network))
1836                 connman_network_unref(network);
1837 }
1838
1839 static int save_prefixes(struct connman_ipconfig *ipconfig,
1840                         GSList *prefixes)
1841 {
1842         GSList *list;
1843         int i = 0, count = g_slist_length(prefixes);
1844         char **array;
1845
1846         if (count == 0)
1847                 return 0;
1848
1849         array = g_try_new0(char *, count + 1);
1850         if (!array)
1851                 return -ENOMEM;
1852
1853         for (list = prefixes; list; list = list->next) {
1854                 char *elem, addr_str[INET6_ADDRSTRLEN];
1855                 GDHCPIAPrefix *prefix = list->data;
1856
1857                 elem = g_strdup_printf("%s/%d", inet_ntop(AF_INET6,
1858                                 &prefix->prefix, addr_str, INET6_ADDRSTRLEN),
1859                                 prefix->prefixlen);
1860                 if (!elem) {
1861                         g_strfreev(array);
1862                         return -ENOMEM;
1863                 }
1864
1865                 array[i++] = elem;
1866         }
1867
1868         __connman_ipconfig_set_dhcpv6_prefixes(ipconfig, array);
1869         return 0;
1870 }
1871
1872 static GSList *load_prefixes(struct connman_ipconfig *ipconfig)
1873 {
1874         int i;
1875         GSList *list = NULL;
1876         char **array =  __connman_ipconfig_get_dhcpv6_prefixes(ipconfig);
1877
1878         if (!array)
1879                 return NULL;
1880
1881         for (i = 0; array[i]; i++) {
1882                 GDHCPIAPrefix *prefix;
1883                 long int value;
1884                 char *ptr, **elems = g_strsplit(array[i], "/", 0);
1885
1886                 if (!elems)
1887                         return list;
1888
1889                 value = strtol(elems[1], &ptr, 10);
1890                 if (ptr != elems[1] && *ptr == '\0' && value <= 128) {
1891                         struct in6_addr addr;
1892
1893                         if (inet_pton(AF_INET6, elems[0], &addr) == 1) {
1894                                 prefix = g_try_new0(GDHCPIAPrefix, 1);
1895                                 if (!prefix) {
1896                                         g_strfreev(elems);
1897                                         return list;
1898                                 }
1899                                 memcpy(&prefix->prefix, &addr,
1900                                         sizeof(struct in6_addr));
1901                                 prefix->prefixlen = value;
1902
1903                                 list = g_slist_prepend(list, prefix);
1904                         }
1905                 }
1906
1907                 g_strfreev(elems);
1908         }
1909
1910         return list;
1911 }
1912
1913 static GDHCPIAPrefix *copy_prefix(gpointer data)
1914 {
1915         GDHCPIAPrefix *copy, *prefix = data;
1916
1917         copy = g_try_new(GDHCPIAPrefix, 1);
1918         if (!copy)
1919                 return NULL;
1920
1921         memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
1922
1923         return copy;
1924 }
1925
1926 static GSList *copy_and_convert_prefixes(GList *prefixes)
1927 {
1928         GSList *copy = NULL;
1929         GList *list;
1930
1931         for (list = prefixes; list; list = list->next)
1932                 copy = g_slist_prepend(copy, copy_prefix(list->data));
1933
1934         return copy;
1935 }
1936
1937 static int set_prefixes(GDHCPClient *dhcp_client, struct connman_dhcpv6 *dhcp)
1938 {
1939         if (dhcp->prefixes)
1940                 g_slist_free_full(dhcp->prefixes, free_prefix);
1941
1942         dhcp->prefixes =
1943                 copy_and_convert_prefixes(g_dhcp_client_get_option(dhcp_client,
1944                                                         G_DHCPV6_IA_PD));
1945
1946         DBG("Got %d prefix", g_slist_length(dhcp->prefixes));
1947
1948         if (dhcp->callback) {
1949                 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
1950                 if (status == G_DHCPV6_ERROR_NO_PREFIX)
1951                         dhcp->callback(dhcp->network,
1952                                         CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1953                 else {
1954                         struct connman_service *service;
1955                         struct connman_ipconfig *ipconfig;
1956                         int ifindex = connman_network_get_index(dhcp->network);
1957
1958                         service = __connman_service_lookup_from_index(ifindex);
1959                         if (service) {
1960                                 ipconfig = __connman_service_get_ip6config(
1961                                                                 service);
1962                                 save_prefixes(ipconfig, dhcp->prefixes);
1963                                 __connman_service_save(service);
1964                         }
1965
1966                         dhcp->callback(dhcp->network,
1967                                 CONNMAN_DHCPV6_STATUS_SUCCEED, dhcp->prefixes);
1968                 }
1969         } else {
1970                 g_slist_free_full(dhcp->prefixes, free_prefix);
1971                 dhcp->prefixes = NULL;
1972         }
1973
1974         return 0;
1975 }
1976
1977 static void re_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
1978 {
1979         struct connman_dhcpv6 *dhcp = user_data;
1980         uint16_t status;
1981         int ret;
1982
1983         status = g_dhcpv6_client_get_status(dhcp_client);
1984
1985         DBG("dhcpv6 cb msg %p status %d", dhcp, status);
1986
1987         if (status  == G_DHCPV6_ERROR_BINDING) {
1988                 /* RFC 3315, 18.1.8 */
1989                 dhcpv6_pd_request(dhcp);
1990         } else {
1991                 ret = set_prefixes(dhcp_client, dhcp);
1992                 if (ret < 0) {
1993                         if (dhcp->callback)
1994                                 dhcp->callback(dhcp->network,
1995                                         CONNMAN_DHCPV6_STATUS_FAIL, NULL);
1996                         return;
1997                 }
1998         }
1999 }
2000
2001 static void rebind_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2002 {
2003         DBG("");
2004
2005         g_dhcpv6_client_clear_retransmit(dhcp_client);
2006
2007         re_pd_cb(dhcp_client, user_data);
2008 }
2009
2010 static GDHCPClient *create_pd_client(struct connman_dhcpv6 *dhcp, int *err)
2011 {
2012         GDHCPClient *dhcp_client;
2013         GDHCPClientError error;
2014         struct connman_service *service;
2015         int index, ret;
2016         uint32_t iaid;
2017
2018         index = connman_network_get_index(dhcp->network);
2019
2020         dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
2021         if (error != G_DHCP_CLIENT_ERROR_NONE) {
2022                 *err = -EINVAL;
2023                 return NULL;
2024         }
2025
2026         if (getenv("CONNMAN_DHCPV6_DEBUG"))
2027                 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6:PD");
2028
2029         service = connman_service_lookup_from_network(dhcp->network);
2030         if (!service) {
2031                 g_dhcp_client_unref(dhcp_client);
2032                 *err = -EINVAL;
2033                 return NULL;
2034         }
2035
2036         ret = set_duid(service, dhcp->network, dhcp_client, index);
2037         if (ret < 0) {
2038                 g_dhcp_client_unref(dhcp_client);
2039                 *err = ret;
2040                 return NULL;
2041         }
2042
2043         g_dhcpv6_client_create_iaid(dhcp_client, index, (unsigned char *)&iaid);
2044         g_dhcpv6_client_set_iaid(dhcp_client, iaid);
2045
2046         return dhcp_client;
2047 }
2048
2049 static int dhcpv6_pd_rebind(struct connman_dhcpv6 *dhcp)
2050 {
2051         GDHCPClient *dhcp_client;
2052         uint32_t T1, T2;
2053
2054         DBG("dhcp %p", dhcp);
2055
2056         if (!dhcp->dhcp_client) {
2057                 /*
2058                  * We skipped the solicitation phase
2059                  */
2060                 int err;
2061
2062                 dhcp->dhcp_client = create_pd_client(dhcp, &err);
2063                 if (!dhcp->dhcp_client) {
2064                         clear_timer(dhcp);
2065                         return err;
2066                 }
2067         }
2068
2069         dhcp_client = dhcp->dhcp_client;
2070
2071         g_dhcp_client_clear_requests(dhcp_client);
2072
2073         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2074         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
2075         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
2076
2077         g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2078         g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2079
2080         clear_callbacks(dhcp_client);
2081
2082         g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
2083                                         rebind_pd_cb, dhcp);
2084
2085         return g_dhcp_client_start(dhcp_client, NULL);
2086 }
2087
2088 static void renew_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2089 {
2090         DBG("");
2091
2092         g_dhcpv6_client_clear_retransmit(dhcp_client);
2093
2094         re_pd_cb(dhcp_client, user_data);
2095 }
2096
2097 static int dhcpv6_pd_renew(struct connman_dhcpv6 *dhcp)
2098 {
2099         GDHCPClient *dhcp_client;
2100         uint32_t T1, T2;
2101
2102         DBG("dhcp %p", dhcp);
2103
2104         dhcp_client = dhcp->dhcp_client;
2105
2106         g_dhcp_client_clear_requests(dhcp_client);
2107
2108         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2109         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
2110         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
2111         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
2112
2113         g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2114         g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2115
2116         clear_callbacks(dhcp_client);
2117
2118         g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
2119                                         renew_pd_cb, dhcp);
2120
2121         return g_dhcp_client_start(dhcp_client, NULL);
2122 }
2123
2124 /*
2125  * Check if we need to restart the solicitation procedure. This
2126  * is done if all the prefixes have expired.
2127  */
2128 static int check_pd_restart(struct connman_dhcpv6 *dhcp)
2129 {
2130         time_t current, expired;
2131
2132         g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
2133                                 NULL, &expired);
2134         current = time(NULL);
2135
2136         if (current > expired) {
2137                 DBG("expired by %d secs", (int)(current - expired));
2138
2139                 g_timeout_add(0, dhcpv6_restart, dhcp);
2140
2141                 return -ETIMEDOUT;
2142         }
2143
2144         return 0;
2145 }
2146
2147 static gboolean timeout_pd_rebind(gpointer user_data)
2148 {
2149         struct connman_dhcpv6 *dhcp = user_data;
2150
2151         if (check_pd_restart(dhcp) < 0)
2152                 return FALSE;
2153
2154         dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
2155
2156         DBG("rebind RT timeout %d msec", dhcp->RT);
2157
2158         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_rebind, dhcp);
2159
2160         g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2161
2162         g_dhcp_client_start(dhcp->dhcp_client, NULL);
2163
2164         return FALSE;
2165 }
2166
2167 static gboolean start_pd_rebind(gpointer user_data)
2168 {
2169         struct connman_dhcpv6 *dhcp = user_data;
2170
2171         if (check_pd_restart(dhcp) < 0)
2172                 return FALSE;
2173
2174         dhcp->RT = REB_TIMEOUT * (1 + get_random());
2175
2176         DBG("rebind initial RT timeout %d msec", dhcp->RT);
2177
2178         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_rebind, dhcp);
2179
2180         dhcpv6_pd_rebind(dhcp);
2181
2182         return FALSE;
2183 }
2184
2185 static gboolean timeout_pd_rebind_confirm(gpointer user_data)
2186 {
2187         struct connman_dhcpv6 *dhcp = user_data;
2188
2189         dhcp->RT = calc_delay(dhcp->RT, CNF_MAX_RT);
2190
2191         DBG("rebind with confirm RT timeout %d msec", dhcp->RT);
2192
2193         dhcp->timeout = g_timeout_add(dhcp->RT,
2194                                         timeout_pd_rebind_confirm, dhcp);
2195
2196         g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2197
2198         g_dhcp_client_start(dhcp->dhcp_client, NULL);
2199
2200         return FALSE;
2201 }
2202
2203 static gboolean timeout_pd_max_confirm(gpointer user_data)
2204 {
2205         struct connman_dhcpv6 *dhcp = user_data;
2206
2207         dhcp->MRD = 0;
2208
2209         clear_timer(dhcp);
2210
2211         DBG("rebind with confirm max retransmit duration timeout");
2212
2213         g_dhcpv6_client_clear_retransmit(dhcp->dhcp_client);
2214
2215         if (dhcp->callback)
2216                 dhcp->callback(dhcp->network,
2217                         CONNMAN_DHCPV6_STATUS_FAIL, NULL);
2218
2219         return FALSE;
2220 }
2221
2222 static gboolean start_pd_rebind_with_confirm(gpointer user_data)
2223 {
2224         struct connman_dhcpv6 *dhcp = user_data;
2225
2226         dhcp->RT = CNF_TIMEOUT * (1 + get_random());
2227
2228         DBG("rebind with confirm initial RT timeout %d msec", dhcp->RT);
2229
2230         dhcp->timeout = g_timeout_add(dhcp->RT,
2231                                         timeout_pd_rebind_confirm, dhcp);
2232         dhcp->MRD = g_timeout_add(CNF_MAX_RD, timeout_pd_max_confirm, dhcp);
2233
2234         dhcpv6_pd_rebind(dhcp);
2235
2236         return FALSE;
2237 }
2238
2239 static gboolean timeout_pd_renew(gpointer user_data)
2240 {
2241         struct connman_dhcpv6 *dhcp = user_data;
2242
2243         if (check_pd_restart(dhcp) < 0)
2244                 return FALSE;
2245
2246         dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
2247
2248         DBG("renew RT timeout %d msec", dhcp->RT);
2249
2250         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
2251
2252         g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2253
2254         g_dhcp_client_start(dhcp->dhcp_client, NULL);
2255
2256         return FALSE;
2257 }
2258
2259 static gboolean start_pd_renew(gpointer user_data)
2260 {
2261         struct connman_dhcpv6 *dhcp = user_data;
2262
2263         dhcp->RT = REN_TIMEOUT * (1 + get_random());
2264
2265         DBG("renew initial RT timeout %d msec", dhcp->RT);
2266
2267         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_renew, dhcp);
2268
2269         dhcpv6_pd_renew(dhcp);
2270
2271         return FALSE;
2272 }
2273
2274 int __connman_dhcpv6_start_pd_renew(struct connman_network *network,
2275                                 dhcpv6_cb callback)
2276 {
2277         struct connman_dhcpv6 *dhcp;
2278         uint32_t T1, T2;
2279         time_t started, current, expired;
2280
2281         dhcp = g_hash_table_lookup(network_pd_table, network);
2282         if (!dhcp)
2283                 return -ENOENT;
2284
2285         DBG("network %p dhcp %p", network, dhcp);
2286
2287         clear_timer(dhcp);
2288
2289         g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
2290                                 &started, &expired);
2291
2292         current = time(NULL);
2293
2294         DBG("T1 %u T2 %u expires %lu current %lu started %lu", T1, T2,
2295                 expired, current, started);
2296
2297         if (T1 == 0xffffffff)
2298                 /* RFC 3633, ch 9 */
2299                 return 0;
2300
2301         if (T1 == 0)
2302                 /* RFC 3633, ch 9
2303                  * Client can choose the timeout.
2304                  */
2305                 T1 = 120;
2306
2307         dhcp->callback = callback;
2308
2309         /* RFC 3315, 18.1.4, start solicit if expired */
2310         if (check_pd_restart(dhcp) < 0)
2311                 return 0;
2312
2313         if (T2 != 0xffffffff && T2 > 0) {
2314                 if ((unsigned)current >= (unsigned)started + T2) {
2315                         /* RFC 3315, chapter 18.1.3, start rebind */
2316                         DBG("rebind after %d secs", T2);
2317
2318                         dhcp->timeout = g_timeout_add_seconds(T2,
2319                                                         start_pd_rebind,
2320                                                         dhcp);
2321
2322                 } else if ((unsigned)current < (unsigned)started + T1) {
2323                         DBG("renew after %d secs", T1);
2324
2325                         dhcp->timeout = g_timeout_add_seconds(T1,
2326                                                         start_pd_renew,
2327                                                         dhcp);
2328                 } else {
2329                         DBG("rebind after %d secs", T2 - T1);
2330
2331                         dhcp->timeout = g_timeout_add_seconds(T2 - T1,
2332                                                         start_pd_rebind,
2333                                                         dhcp);
2334                 }
2335         }
2336
2337         return 0;
2338 }
2339
2340 static void release_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2341 {
2342         DBG("");
2343 }
2344
2345 int __connman_dhcpv6_start_pd_release(struct connman_network *network,
2346                                 dhcpv6_cb callback)
2347 {
2348         struct connman_dhcpv6 *dhcp;
2349         GDHCPClient *dhcp_client;
2350         uint32_t T1, T2;
2351
2352         if (!network_table)
2353                 return 0;   /* we are already released */
2354
2355         dhcp = g_hash_table_lookup(network_pd_table, network);
2356         if (!dhcp)
2357                 return -ENOENT;
2358
2359         DBG("network %p dhcp %p client %p", network, dhcp, dhcp->dhcp_client);
2360
2361         clear_timer(dhcp);
2362
2363         dhcp_client = dhcp->dhcp_client;
2364         if (!dhcp_client) {
2365                 DBG("DHCPv6 PD was not started");
2366                 return 0;
2367         }
2368
2369         g_dhcp_client_clear_requests(dhcp_client);
2370         g_dhcp_client_clear_values(dhcp_client);
2371
2372         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2373         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
2374
2375         g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2376         g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2377
2378         clear_callbacks(dhcp_client);
2379
2380         g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RELEASE,
2381                                         release_pd_cb, dhcp);
2382
2383         dhcp->dhcp_client = dhcp_client;
2384
2385         return g_dhcp_client_start(dhcp_client, NULL);
2386 }
2387
2388 static gboolean timeout_pd_request(gpointer user_data)
2389 {
2390         struct connman_dhcpv6 *dhcp = user_data;
2391
2392         if (dhcp->request_count >= REQ_MAX_RC) {
2393                 DBG("max request retry attempts %d", dhcp->request_count);
2394                 dhcp->request_count = 0;
2395                 if (dhcp->callback)
2396                         dhcp->callback(dhcp->network,
2397                                         CONNMAN_DHCPV6_STATUS_FAIL, NULL);
2398                 return FALSE;
2399         }
2400
2401         dhcp->request_count++;
2402
2403         dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
2404         DBG("request RT timeout %d msec", dhcp->RT);
2405         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_request, dhcp);
2406
2407         g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
2408
2409         g_dhcp_client_start(dhcp->dhcp_client, NULL);
2410
2411         return FALSE;
2412 }
2413
2414 static void request_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2415 {
2416         struct connman_dhcpv6 *dhcp = user_data;
2417         uint16_t status;
2418
2419         DBG("");
2420
2421         g_dhcpv6_client_clear_retransmit(dhcp_client);
2422
2423         status = g_dhcpv6_client_get_status(dhcp_client);
2424
2425         DBG("dhcpv6 pd cb msg %p status %d", dhcp, status);
2426
2427         if (status  == G_DHCPV6_ERROR_BINDING) {
2428                 /* RFC 3315, 18.1.8 */
2429                 dhcpv6_pd_request(dhcp);
2430         } else {
2431                 set_prefixes(dhcp_client, dhcp);
2432         }
2433 }
2434
2435 static int dhcpv6_pd_request(struct connman_dhcpv6 *dhcp)
2436 {
2437         GDHCPClient *dhcp_client;
2438         uint32_t T1 = 0, T2 = 0;
2439
2440         DBG("dhcp %p", dhcp);
2441
2442         dhcp_client = dhcp->dhcp_client;
2443
2444         g_dhcp_client_clear_requests(dhcp_client);
2445
2446         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2447         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
2448         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
2449         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
2450
2451         g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
2452         g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
2453
2454         clear_callbacks(dhcp_client);
2455
2456         g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
2457                                         request_pd_cb, dhcp);
2458
2459         return g_dhcp_client_start(dhcp_client, NULL);
2460 }
2461
2462 static void advertise_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2463 {
2464         struct connman_dhcpv6 *dhcp = user_data;
2465
2466         DBG("dhcpv6 advertise pd msg %p", dhcp);
2467
2468         clear_timer(dhcp);
2469
2470         g_dhcpv6_client_clear_retransmit(dhcp_client);
2471
2472         if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
2473                 if (dhcp->callback)
2474                         dhcp->callback(dhcp->network,
2475                                         CONNMAN_DHCPV6_STATUS_FAIL, NULL);
2476                 return;
2477         }
2478
2479         dhcp->RT = REQ_TIMEOUT * (1 + get_random());
2480         DBG("request initial RT timeout %d msec", dhcp->RT);
2481         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_request, dhcp);
2482
2483         dhcp->request_count = 1;
2484
2485         dhcpv6_pd_request(dhcp);
2486 }
2487
2488 static void solicitation_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
2489 {
2490         /*
2491          * This callback is here so that g_dhcp_client_start()
2492          * will enter the proper L3 mode.
2493          */
2494         DBG("DHCPv6 %p solicitation msg received, ignoring it", user_data);
2495 }
2496
2497 static int dhcpv6_pd_solicitation(struct connman_dhcpv6 *dhcp)
2498 {
2499         GDHCPClient *dhcp_client;
2500         int ret;
2501
2502         DBG("dhcp %p", dhcp);
2503
2504         dhcp_client = create_pd_client(dhcp, &ret);
2505         if (!dhcp_client) {
2506                 clear_timer(dhcp);
2507                 return ret;
2508         }
2509
2510         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
2511
2512         g_dhcpv6_client_set_pd(dhcp_client, NULL, NULL, NULL);
2513
2514         clear_callbacks(dhcp_client);
2515
2516         g_dhcp_client_register_event(dhcp_client,
2517                                 G_DHCP_CLIENT_EVENT_ADVERTISE,
2518                                 advertise_pd_cb, dhcp);
2519
2520         g_dhcp_client_register_event(dhcp_client,
2521                                 G_DHCP_CLIENT_EVENT_SOLICITATION,
2522                                 solicitation_pd_cb, dhcp);
2523
2524         dhcp->dhcp_client = dhcp_client;
2525
2526         return g_dhcp_client_start(dhcp_client, NULL);
2527 }
2528
2529 static gboolean start_pd_solicitation(gpointer user_data)
2530 {
2531         struct connman_dhcpv6 *dhcp = user_data;
2532
2533         /* Set the retransmission timeout, RFC 3315 chapter 14 */
2534         dhcp->RT = SOL_TIMEOUT * (1 + get_random());
2535
2536         DBG("solicit initial RT timeout %d msec", dhcp->RT);
2537
2538         dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
2539
2540         dhcpv6_pd_solicitation(dhcp);
2541
2542         return FALSE;
2543 }
2544
2545 int __connman_dhcpv6_start_pd(int index, GSList *prefixes, dhcpv6_cb callback)
2546 {
2547         struct connman_service *service;
2548         struct connman_network *network;
2549         struct connman_dhcpv6 *dhcp;
2550
2551         if (index < 0)
2552                 return 0;
2553
2554         DBG("index %d", index);
2555
2556         service = __connman_service_lookup_from_index(index);
2557         if (!service)
2558                 return -EINVAL;
2559
2560         network = __connman_service_get_network(service);
2561         if (!network)
2562                 return -EINVAL;
2563
2564         if (network_pd_table) {
2565                 dhcp = g_hash_table_lookup(network_pd_table, network);
2566                 if (dhcp && dhcp->started)
2567                         return -EBUSY;
2568         }
2569
2570         dhcp = g_try_new0(struct connman_dhcpv6, 1);
2571         if (!dhcp)
2572                 return -ENOMEM;
2573
2574         dhcp->network = network;
2575         dhcp->callback = callback;
2576         dhcp->started = true;
2577
2578         if (!prefixes) {
2579                 /*
2580                  * Try to load the earlier prefixes if caller did not supply
2581                  * any that we could use.
2582                  */
2583                 struct connman_ipconfig *ipconfig;
2584                 ipconfig = __connman_service_get_ip6config(service);
2585
2586                 dhcp->prefixes = load_prefixes(ipconfig);
2587         } else
2588                 dhcp->prefixes = prefixes;
2589
2590         connman_network_ref(network);
2591
2592         DBG("replace network %p dhcp %p", network, dhcp);
2593
2594         g_hash_table_replace(network_pd_table, network, dhcp);
2595
2596         if (!dhcp->prefixes) {
2597                 /*
2598                  * Refresh start, try to get prefixes.
2599                  */
2600                 start_pd_solicitation(dhcp);
2601         } else {
2602                 /*
2603                  * We used to have prefixes, try to use them again.
2604                  * We need to use timeouts from confirm msg, RFC 3633, ch 12.1
2605                  */
2606                 start_pd_rebind_with_confirm(dhcp);
2607         }
2608
2609         return 0;
2610 }
2611
2612 void __connman_dhcpv6_stop_pd(int index)
2613 {
2614         struct connman_service *service;
2615         struct connman_network *network;
2616
2617         if (index < 0)
2618                 return;
2619
2620         DBG("index %d", index);
2621
2622         if (!network_pd_table)
2623                 return;
2624
2625         service = __connman_service_lookup_from_index(index);
2626         if (!service)
2627                 return;
2628
2629         network = __connman_service_get_network(service);
2630         if (!network)
2631                 return;
2632
2633         __connman_dhcpv6_start_pd_release(network, NULL);
2634
2635         if (g_hash_table_remove(network_pd_table, network))
2636                 connman_network_unref(network);
2637 }
2638
2639 int __connman_dhcpv6_init(void)
2640 {
2641         DBG("");
2642
2643         srand(time(NULL));
2644
2645         network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2646                                                         NULL, remove_network);
2647
2648         network_pd_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2649                                                         NULL, remove_network);
2650
2651         return 0;
2652 }
2653
2654 void __connman_dhcpv6_cleanup(void)
2655 {
2656         DBG("");
2657
2658         g_hash_table_destroy(network_table);
2659         network_table = NULL;
2660
2661         g_hash_table_destroy(network_pd_table);
2662         network_pd_table = NULL;
2663 }