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