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