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