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