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