dnsproxy: Only one copy of the relevant buffers will be made to a TCP request
[framework/connectivity/connman.git] / src / dhcpv6.c
index bedcc7f..574b19e 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2011  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2012  Intel Corporation. All rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
 #define INF_MAX_DELAY  (1 * 1000)
 #define INF_TIMEOUT    (1 * 1000)
 #define INF_MAX_RT     (120 * 1000)
+#define SOL_MAX_DELAY   (1 * 1000)
+#define SOL_TIMEOUT     (1 * 1000)
+#define SOL_MAX_RT      (120 * 1000)
+#define REQ_TIMEOUT    (1 * 1000)
+#define REQ_MAX_RT     (30 * 1000)
+#define REQ_MAX_RC     10
+#define REN_TIMEOUT     (10 * 1000)
+#define REN_MAX_RT      (600 * 1000)
+#define REB_TIMEOUT     (10 * 1000)
+#define REB_MAX_RT      (600 * 1000)
 
 
 struct connman_dhcpv6 {
@@ -51,12 +61,27 @@ struct connman_dhcpv6 {
 
        GDHCPClient *dhcp_client;
 
-       guint timeout;
-       guint RT;       /* in msec */
+       guint timeout;          /* operation timeout in msec */
+       guint RT;               /* in msec */
+       gboolean use_ta;        /* set to TRUE if IPv6 privacy is enabled */
+       GSList *prefixes;       /* network prefixes from radvd */
+       int request_count;      /* how many times REQUEST have been sent */
+       gboolean stateless;     /* TRUE if stateless DHCPv6 is used */
+       gboolean started;       /* TRUE if we have DHCPv6 started */
 };
 
 static GHashTable *network_table;
 
+static int dhcpv6_request(struct connman_dhcpv6 *dhcp, gboolean add_addresses);
+
+static void clear_timer(struct connman_dhcpv6 *dhcp)
+{
+       if (dhcp->timeout > 0) {
+               g_source_remove(dhcp->timeout);
+               dhcp->timeout = 0;
+       }
+}
+
 static inline float get_random()
 {
        return (rand() % 200 - 100) / 1000.0;
@@ -78,6 +103,11 @@ static guint calc_delay(guint RT, guint MRT)
        return (guint)rt;
 }
 
+static void free_prefix(gpointer data, gpointer user_data)
+{
+       g_free(data);
+}
+
 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
 {
        g_strfreev(dhcp->nameservers);
@@ -85,6 +115,10 @@ static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
 
        dhcp->nameservers = NULL;
        dhcp->timeservers = NULL;
+       dhcp->started = FALSE;
+
+       g_slist_foreach(dhcp->prefixes, free_prefix, NULL);
+       g_slist_free(dhcp->prefixes);
 }
 
 static gboolean compare_string_arrays(char **array_a, char **array_b)
@@ -190,6 +224,37 @@ static int set_duid(struct connman_service *service,
        return 0;
 }
 
+static void clear_callbacks(GDHCPClient *dhcp_client)
+{
+       g_dhcp_client_register_event(dhcp_client,
+                               G_DHCP_CLIENT_EVENT_SOLICITATION,
+                               NULL, NULL);
+
+       g_dhcp_client_register_event(dhcp_client,
+                               G_DHCP_CLIENT_EVENT_ADVERTISE,
+                               NULL, NULL);
+
+       g_dhcp_client_register_event(dhcp_client,
+                               G_DHCP_CLIENT_EVENT_REQUEST,
+                               NULL, NULL);
+
+       g_dhcp_client_register_event(dhcp_client,
+                               G_DHCP_CLIENT_EVENT_RENEW,
+                               NULL, NULL);
+
+       g_dhcp_client_register_event(dhcp_client,
+                               G_DHCP_CLIENT_EVENT_REBIND,
+                               NULL, NULL);
+
+       g_dhcp_client_register_event(dhcp_client,
+                               G_DHCP_CLIENT_EVENT_RELEASE,
+                               NULL, NULL);
+
+       g_dhcp_client_register_event(dhcp_client,
+                               G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
+                               NULL, NULL);
+}
+
 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
 {
        struct connman_dhcpv6 *dhcp = user_data;
@@ -226,7 +291,8 @@ static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
 
                dhcp->nameservers = nameservers;
 
-               for (i = 0; dhcp->nameservers[i] != NULL; i++)
+               for (i = 0; dhcp->nameservers != NULL &&
+                                       dhcp->nameservers[i] != NULL; i++)
                        __connman_service_nameserver_append(service,
                                                dhcp->nameservers[i],
                                                FALSE);
@@ -253,7 +319,8 @@ static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
 
                dhcp->timeservers = timeservers;
 
-               for (i = 0; dhcp->timeservers[i] != NULL; i++)
+               for (i = 0; dhcp->timeservers != NULL &&
+                                       dhcp->timeservers[i] != NULL; i++)
                        __connman_service_timeserver_append(service,
                                                        dhcp->timeservers[i]);
        } else
@@ -278,19 +345,27 @@ static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
        index = connman_network_get_index(dhcp->network);
 
        dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
-       if (error != G_DHCP_CLIENT_ERROR_NONE)
+       if (error != G_DHCP_CLIENT_ERROR_NONE) {
+               clear_timer(dhcp);
                return -EINVAL;
+       }
 
        if (getenv("CONNMAN_DHCPV6_DEBUG"))
                g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
 
        service = __connman_service_lookup_from_network(dhcp->network);
-       if (service == NULL)
+       if (service == NULL) {
+               clear_timer(dhcp);
+               g_dhcp_client_unref(dhcp_client);
                return -EINVAL;
+       }
 
        ret = set_duid(service, dhcp->network, dhcp_client, index);
-       if (ret < 0)
+       if (ret < 0) {
+               clear_timer(dhcp);
+               g_dhcp_client_unref(dhcp_client);
                return ret;
+       }
 
        g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
        g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
@@ -307,15 +382,569 @@ static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
        return g_dhcp_client_start(dhcp_client, NULL);
 }
 
-static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
+static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
+{
+       struct in6_addr addr_prefix, addr;
+       GSList *list;
+       int ret = 128, len;
+
+       for (list = prefixes; list; list = list->next) {
+               char *prefix = list->data;
+               const char *slash = g_strrstr(prefix, "/");
+               const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
+                                               0xF0, 0xE0, 0xC0, 0x80 };
+               int left, count, i, plen;
+
+               if (slash == NULL)
+                       continue;
+
+               prefix = g_strndup(prefix, slash - prefix);
+               len = strtol(slash + 1, NULL, 10);
+               plen = 128 - len;
+
+               count = plen / 8;
+               left = plen % 8;
+               i = 16 - count;
+
+               inet_pton(AF_INET6, prefix, &addr_prefix);
+               inet_pton(AF_INET6, address, &addr);
+
+               memset(&addr_prefix.s6_addr[i], 0, count);
+               memset(&addr.s6_addr[i], 0, count);
+
+               if (left) {
+                       addr_prefix.s6_addr[i - 1] &= bits[left];
+                       addr.s6_addr[i - 1] &= bits[left];
+               }
+
+               g_free(prefix);
+
+               if (memcmp(&addr_prefix, &addr, 16) == 0) {
+                       ret = len;
+                       break;
+               }
+       }
+
+       return ret;
+}
+
+static int set_addresses(GDHCPClient *dhcp_client,
+                                               struct connman_dhcpv6 *dhcp)
+{
+       struct connman_service *service;
+       struct connman_ipconfig *ipconfig;
+       int entries, i;
+       GList *option, *list;
+       char **nameservers, **timeservers;
+       const char *c_address;
+       char *address = NULL;
+
+       service = __connman_service_lookup_from_network(dhcp->network);
+       if (service == NULL) {
+               connman_error("Can not lookup service");
+               return -EINVAL;
+       }
+
+       ipconfig = __connman_service_get_ip6config(service);
+       if (ipconfig == NULL) {
+               connman_error("Could not lookup ip6config");
+               return -EINVAL;
+       }
+
+       option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
+       entries = g_list_length(option);
+
+       nameservers = g_try_new0(char *, entries + 1);
+       if (nameservers != NULL) {
+               for (i = 0, list = option; list; list = list->next, i++)
+                       nameservers[i] = g_strdup(list->data);
+       }
+
+       if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
+               if (dhcp->nameservers != NULL) {
+                       for (i = 0; dhcp->nameservers[i] != NULL; i++)
+                               __connman_service_nameserver_remove(service,
+                                                       dhcp->nameservers[i],
+                                                       FALSE);
+                       g_strfreev(dhcp->nameservers);
+               }
+
+               dhcp->nameservers = nameservers;
+
+               for (i = 0; dhcp->nameservers != NULL &&
+                                       dhcp->nameservers[i] != NULL; i++)
+                       __connman_service_nameserver_append(service,
+                                                       dhcp->nameservers[i],
+                                                       FALSE);
+       } else
+               g_strfreev(nameservers);
+
+
+       option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
+       entries = g_list_length(option);
+
+       timeservers = g_try_new0(char *, entries + 1);
+       if (timeservers != NULL) {
+               for (i = 0, list = option; list; list = list->next, i++)
+                       timeservers[i] = g_strdup(list->data);
+       }
+
+       if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
+               if (dhcp->timeservers != NULL) {
+                       for (i = 0; dhcp->timeservers[i] != NULL; i++)
+                               __connman_service_timeserver_remove(service,
+                                                       dhcp->timeservers[i]);
+                       g_strfreev(dhcp->timeservers);
+               }
+
+               dhcp->timeservers = timeservers;
+
+               for (i = 0; dhcp->timeservers != NULL &&
+                                       dhcp->timeservers[i] != NULL; i++)
+                       __connman_service_timeserver_append(service,
+                                                       dhcp->timeservers[i]);
+       } else
+               g_strfreev(timeservers);
+
+
+       option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
+       if (option != NULL)
+               address = g_strdup(option->data);
+       else {
+               option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
+               if (option != NULL)
+                       address = g_strdup(option->data);
+       }
+
+       c_address = __connman_ipconfig_get_local(ipconfig);
+
+       if (address != NULL &&
+                       ((c_address != NULL &&
+                               g_strcmp0(address, c_address) != 0) ||
+                       (c_address == NULL))) {
+               int prefix_len;
+
+               /* Is this prefix part of the subnet we are suppose to use? */
+               prefix_len = check_ipv6_addr_prefix(dhcp->prefixes, address);
+
+               __connman_ipconfig_set_local(ipconfig, address);
+               __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
+
+               DBG("new address %s/%d", address, prefix_len);
+       }
+
+       return 0;
+}
+
+static void re_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+       struct connman_dhcpv6 *dhcp = user_data;
+       uint16_t status;
+       int ret;
+
+       ret = set_addresses(dhcp_client, dhcp);
+
+       status = g_dhcpv6_client_get_status(dhcp_client);
+
+       DBG("dhcpv6 cb msg %p ret %d status %d", dhcp, ret, status);
+
+       if (ret < 0) {
+               if (dhcp->callback != NULL)
+                       dhcp->callback(dhcp->network, FALSE);
+               return;
+       }
+
+       if (status  == G_DHCPV6_ERROR_BINDING) {
+               /* RFC 3315, 18.1.8 */
+               dhcpv6_request(dhcp, FALSE);
+       } else {
+               if (dhcp->callback != NULL)
+                       dhcp->callback(dhcp->network,
+                                               status == 0 ? TRUE : FALSE);
+       }
+}
+
+static void rebind_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+       DBG("");
+
+       g_dhcpv6_client_reset_rebind(dhcp_client);
+       g_dhcpv6_client_reset_renew(dhcp_client);
+
+       re_cb(dhcp_client, user_data);
+}
+
+static int dhcpv6_rebind(struct connman_dhcpv6 *dhcp)
 {
+       GDHCPClient *dhcp_client;
+
        DBG("dhcp %p", dhcp);
 
-       if (dhcp->timeout > 0) {
-               g_source_remove(dhcp->timeout);
-               dhcp->timeout = 0;
+       dhcp_client = dhcp->dhcp_client;
+
+       g_dhcp_client_clear_requests(dhcp_client);
+
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
+
+       g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
+                               G_DHCPV6_SNTP_SERVERS);
+
+       g_dhcpv6_client_set_ia(dhcp_client,
+                       connman_network_get_index(dhcp->network),
+                       dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
+                       NULL, NULL, FALSE);
+
+       clear_callbacks(dhcp_client);
+
+       g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
+                                       rebind_cb, dhcp);
+
+       dhcp->dhcp_client = dhcp_client;
+
+       return g_dhcp_client_start(dhcp_client, NULL);
+}
+
+static gboolean dhcpv6_restart(gpointer user_data)
+{
+       struct connman_dhcpv6 *dhcp = user_data;
+
+       if (dhcp->callback != NULL)
+               dhcp->callback(dhcp->network, FALSE);
+
+       return FALSE;
+}
+
+/*
+ * Check if we need to restart the solicitation procedure. This
+ * is done if all the addresses have expired. RFC 3315, 18.1.4
+ */
+static int check_restart(struct connman_dhcpv6 *dhcp)
+{
+       time_t current, expired;
+
+       g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
+                               NULL, NULL, &expired);
+       current = time(NULL);
+
+       if (current > expired) {
+               DBG("expired by %d secs", (int)(current - expired));
+
+               g_timeout_add(0, dhcpv6_restart, dhcp);
+
+               return -ETIMEDOUT;
+       }
+
+       return 0;
+}
+
+static gboolean timeout_rebind(gpointer user_data)
+{
+       struct connman_dhcpv6 *dhcp = user_data;
+
+       if (check_restart(dhcp) < 0)
+               return FALSE;
+
+       dhcp->RT = calc_delay(dhcp->RT, REB_MAX_RT);
+
+       DBG("rebind RT timeout %d msec", dhcp->RT);
+
+       dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
+
+       g_dhcp_client_start(dhcp->dhcp_client, NULL);
+
+       return FALSE;
+}
+
+static gboolean start_rebind(gpointer user_data)
+{
+       struct connman_dhcpv6 *dhcp = user_data;
+
+       dhcp->RT = REB_TIMEOUT * (1 + get_random());
+
+       DBG("rebind initial RT timeout %d msec", dhcp->RT);
+
+       dhcp->timeout = g_timeout_add(dhcp->RT, timeout_rebind, dhcp);
+
+       dhcpv6_rebind(dhcp);
+
+       return FALSE;
+}
+
+static void request_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+       DBG("");
+
+       re_cb(dhcp_client, user_data);
+}
+
+static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
+                       gboolean add_addresses)
+{
+       GDHCPClient *dhcp_client;
+       uint32_t T1, T2;
+
+       DBG("dhcp %p add %d", dhcp, add_addresses);
+
+       dhcp_client = dhcp->dhcp_client;
+
+       g_dhcp_client_clear_requests(dhcp_client);
+
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
+
+       g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
+                               G_DHCPV6_SNTP_SERVERS);
+
+       g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
+       g_dhcpv6_client_set_ia(dhcp_client,
+                       connman_network_get_index(dhcp->network),
+                       dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
+                       &T1, &T2, add_addresses);
+
+       clear_callbacks(dhcp_client);
+
+       g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REQUEST,
+                                       request_cb, dhcp);
+
+       dhcp->dhcp_client = dhcp_client;
+
+       return g_dhcp_client_start(dhcp_client, NULL);
+}
+
+static gboolean timeout_request(gpointer user_data)
+{
+       struct connman_dhcpv6 *dhcp = user_data;
+
+       if (dhcp->request_count >= REQ_MAX_RC) {
+               DBG("max request retry attempts %d", dhcp->request_count);
+               dhcp->request_count = 0;
+               if (dhcp->callback != NULL)
+                       dhcp->callback(dhcp->network, FALSE);
+               return FALSE;
        }
 
+       dhcp->request_count++;
+
+       dhcp->RT = calc_delay(dhcp->RT, REQ_MAX_RT);
+       DBG("request RT timeout %d msec", dhcp->RT);
+       dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
+
+       g_dhcp_client_start(dhcp->dhcp_client, NULL);
+
+       return FALSE;
+}
+
+static void renew_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+       DBG("");
+
+       g_dhcpv6_client_reset_renew(dhcp_client);
+
+       re_cb(dhcp_client, user_data);
+}
+
+static int dhcpv6_renew(struct connman_dhcpv6 *dhcp)
+{
+       GDHCPClient *dhcp_client;
+       uint32_t T1, T2;
+
+       DBG("dhcp %p", dhcp);
+
+       dhcp_client = dhcp->dhcp_client;
+
+       g_dhcp_client_clear_requests(dhcp_client);
+
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
+
+       g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
+                               G_DHCPV6_SNTP_SERVERS);
+
+       g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL, NULL);
+       g_dhcpv6_client_set_ia(dhcp_client,
+                       connman_network_get_index(dhcp->network),
+                       dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
+                       &T1, &T2, TRUE);
+
+       clear_callbacks(dhcp_client);
+
+       g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
+                                       renew_cb, dhcp);
+
+       dhcp->dhcp_client = dhcp_client;
+
+       return g_dhcp_client_start(dhcp_client, NULL);
+}
+
+static gboolean timeout_renew(gpointer user_data)
+{
+       struct connman_dhcpv6 *dhcp = user_data;
+
+       if (check_restart(dhcp) < 0)
+               return FALSE;
+
+       dhcp->RT = calc_delay(dhcp->RT, REN_MAX_RT);
+
+       DBG("renew RT timeout %d msec", dhcp->RT);
+
+       dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
+
+       g_dhcp_client_start(dhcp->dhcp_client, NULL);
+
+       return FALSE;
+}
+
+static gboolean start_renew(gpointer user_data)
+{
+       struct connman_dhcpv6 *dhcp = user_data;
+
+       dhcp->RT = REN_TIMEOUT * (1 + get_random());
+
+       DBG("renew initial RT timeout %d msec", dhcp->RT);
+
+       dhcp->timeout = g_timeout_add(dhcp->RT, timeout_renew, dhcp);
+
+       dhcpv6_renew(dhcp);
+
+       return FALSE;
+}
+
+int __connman_dhcpv6_start_renew(struct connman_network *network,
+                                                       dhcp_cb callback)
+{
+       struct connman_dhcpv6 *dhcp;
+       uint32_t T1, T2;
+       time_t last_renew, last_rebind, current, expired;
+
+       dhcp = g_hash_table_lookup(network_table, network);
+       if (dhcp == NULL)
+               return -ENOENT;
+
+       DBG("network %p dhcp %p", network, dhcp);
+
+       clear_timer(dhcp);
+
+       g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
+                               &last_renew, &last_rebind, &expired);
+
+       current = time(NULL);
+
+       DBG("T1 %u T2 %u expires %lu current %lu", T1, T2,
+               (unsigned long)expired, current);
+
+       if (T1 == 0xffffffff)
+               /* RFC 3315, 22.4 */
+               return 0;
+
+       if (T1 == 0)
+               /* RFC 3315, 22.4
+                * Client can choose the timeout.
+                */
+               T1 = 1800;
+
+       /* RFC 3315, 18.1.4, start solicit if expired */
+       if (current > expired) {
+               DBG("expired by %d secs", (int)(current - expired));
+               return -ETIMEDOUT;
+       }
+
+       dhcp->callback = callback;
+
+       if (T2 != 0xffffffff && T2 > 0 &&
+                       (unsigned)current > (unsigned)last_rebind + T2) {
+               int timeout;
+
+               /* RFC 3315, chapter 18.1.3, start rebind */
+               if ((unsigned)current > (unsigned)last_renew + T1)
+                       timeout = 0;
+               else
+                       timeout = last_renew - current + T1;
+
+               /*
+                * If we just did a renew, do not restart the rebind
+                * immediately.
+                */
+               dhcp->timeout = g_timeout_add_seconds(timeout, start_rebind,
+                                               dhcp);
+       } else {
+               DBG("renew after %d secs", T1);
+
+               dhcp->timeout = g_timeout_add_seconds(T1, start_renew, dhcp);
+       }
+       return 0;
+}
+
+int __connman_dhcpv6_start_release(struct connman_network *network,
+                               dhcp_cb callback)
+{
+       struct connman_dhcpv6 *dhcp;
+       GDHCPClient *dhcp_client;
+
+       if (network_table == NULL)
+               return 0;   /* we are already released */
+
+       dhcp = g_hash_table_lookup(network_table, network);
+       if (dhcp == NULL)
+               return -ENOENT;
+
+       DBG("network %p dhcp %p client %p stateless %d", network, dhcp,
+                                       dhcp->dhcp_client, dhcp->stateless);
+
+       if (dhcp->stateless == TRUE)
+               return -EINVAL;
+
+       clear_timer(dhcp);
+
+       dhcp_client = dhcp->dhcp_client;
+       if (dhcp_client == NULL) {
+               /*
+                * We had started the DHCPv6 handshaking i.e., we have called
+                * __connman_dhcpv6_start() but it has not yet sent
+                * a solicitation message to server. This means that we do not
+                * have DHCPv6 configured yet so we can just quit here.
+                */
+               DBG("DHCPv6 was not started");
+               return 0;
+       }
+
+       g_dhcp_client_clear_requests(dhcp_client);
+       g_dhcp_client_clear_values(dhcp_client);
+
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SERVERID);
+
+       g_dhcpv6_client_set_ia(dhcp_client,
+                       connman_network_get_index(dhcp->network),
+                       dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
+                       NULL, NULL, TRUE);
+
+       clear_callbacks(dhcp_client);
+
+       /*
+        * We do not register callback here because the answer might take too
+        * long time and network code might be in the middle of the disconnect.
+        * So we just inform the server that we are done with the addresses
+        * but ignore the reply from server. This is allowed by RFC 3315
+        * chapter 18.1.6.
+        */
+
+       dhcp->dhcp_client = dhcp_client;
+
+       return g_dhcp_client_start(dhcp_client, NULL);
+}
+
+static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
+{
+       DBG("dhcp %p", dhcp);
+
+       clear_timer(dhcp);
+
        dhcpv6_free(dhcp);
 
        if (dhcp->dhcp_client == NULL)
@@ -379,15 +1008,25 @@ int __connman_dhcpv6_start_info(struct connman_network *network,
 
        DBG("");
 
+       if (network_table != NULL) {
+               dhcp = g_hash_table_lookup(network_table, network);
+               if (dhcp != NULL && dhcp->started == TRUE)
+                       return -EBUSY;
+       }
+
        dhcp = g_try_new0(struct connman_dhcpv6, 1);
        if (dhcp == NULL)
                return -ENOMEM;
 
        dhcp->network = network;
        dhcp->callback = callback;
+       dhcp->stateless = TRUE;
+       dhcp->started = TRUE;
 
        connman_network_ref(network);
 
+       DBG("replace network %p dhcp %p", network, dhcp);
+
        g_hash_table_replace(network_table, network, dhcp);
 
        /* Initial timeout, RFC 3315, 18.1.5 */
@@ -398,6 +1037,174 @@ int __connman_dhcpv6_start_info(struct connman_network *network,
        return 0;
 }
 
+static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+       struct connman_dhcpv6 *dhcp = user_data;
+
+       DBG("dhcpv6 advertise msg %p", dhcp);
+
+       clear_timer(dhcp);
+
+       if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
+               if (dhcp->callback != NULL)
+                       dhcp->callback(dhcp->network, FALSE);
+               return;
+       }
+
+       dhcp->RT = REQ_TIMEOUT * (1 + get_random());
+       DBG("request initial RT timeout %d msec", dhcp->RT);
+       dhcp->timeout = g_timeout_add(dhcp->RT, timeout_request, dhcp);
+
+       dhcp->request_count = 1;
+
+       dhcpv6_request(dhcp, TRUE);
+}
+
+static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+       /* We get here if server supports rapid commit */
+       struct connman_dhcpv6 *dhcp = user_data;
+
+       DBG("dhcpv6 solicitation msg %p", dhcp);
+
+       clear_timer(dhcp);
+
+       set_addresses(dhcp_client, dhcp);
+}
+
+static gboolean timeout_solicitation(gpointer user_data)
+{
+       struct connman_dhcpv6 *dhcp = user_data;
+
+       dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
+
+       DBG("solicit RT timeout %d msec", dhcp->RT);
+
+       dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
+
+       g_dhcp_client_start(dhcp->dhcp_client, NULL);
+
+       return FALSE;
+}
+
+static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
+{
+       struct connman_service *service;
+       struct connman_ipconfig *ipconfig_ipv6;
+       GDHCPClient *dhcp_client;
+       GDHCPClientError error;
+       int index, ret;
+
+       DBG("dhcp %p", dhcp);
+
+       index = connman_network_get_index(dhcp->network);
+
+       dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
+       if (error != G_DHCP_CLIENT_ERROR_NONE) {
+               clear_timer(dhcp);
+               return -EINVAL;
+       }
+
+       if (getenv("CONNMAN_DHCPV6_DEBUG"))
+               g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
+
+       service = __connman_service_lookup_from_network(dhcp->network);
+       if (service == NULL) {
+               clear_timer(dhcp);
+               g_dhcp_client_unref(dhcp_client);
+               return -EINVAL;
+       }
+
+       ret = set_duid(service, dhcp->network, dhcp_client, index);
+       if (ret < 0) {
+               clear_timer(dhcp);
+               g_dhcp_client_unref(dhcp_client);
+               return ret;
+       }
+
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
+       g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
+
+       g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
+                               G_DHCPV6_SNTP_SERVERS);
+
+       ipconfig_ipv6 = __connman_service_get_ip6config(service);
+       dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
+
+       g_dhcpv6_client_set_ia(dhcp_client, index,
+                       dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
+                       NULL, NULL, FALSE);
+
+       clear_callbacks(dhcp_client);
+
+       g_dhcp_client_register_event(dhcp_client,
+                               G_DHCP_CLIENT_EVENT_SOLICITATION,
+                               solicitation_cb, dhcp);
+
+       g_dhcp_client_register_event(dhcp_client,
+                               G_DHCP_CLIENT_EVENT_ADVERTISE,
+                               advertise_cb, dhcp);
+
+       dhcp->dhcp_client = dhcp_client;
+
+       return g_dhcp_client_start(dhcp_client, NULL);
+}
+
+static gboolean start_solicitation(gpointer user_data)
+{
+       struct connman_dhcpv6 *dhcp = user_data;
+
+       /* Set the retransmission timeout, RFC 3315 chapter 14 */
+       dhcp->RT = SOL_TIMEOUT * (1 + get_random());
+
+       DBG("solicit initial RT timeout %d msec", dhcp->RT);
+
+       dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
+
+       dhcpv6_solicitation(dhcp);
+
+       return FALSE;
+}
+
+int __connman_dhcpv6_start(struct connman_network *network,
+                               GSList *prefixes, dhcp_cb callback)
+{
+       struct connman_dhcpv6 *dhcp;
+       int delay;
+
+       DBG("");
+
+       if (network_table != NULL) {
+               dhcp = g_hash_table_lookup(network_table, network);
+               if (dhcp != NULL && dhcp->started == TRUE)
+                       return -EBUSY;
+       }
+
+       dhcp = g_try_new0(struct connman_dhcpv6, 1);
+       if (dhcp == NULL)
+               return -ENOMEM;
+
+       dhcp->network = network;
+       dhcp->callback = callback;
+       dhcp->prefixes = prefixes;
+       dhcp->started = TRUE;
+
+       connman_network_ref(network);
+
+       DBG("replace network %p dhcp %p", network, dhcp);
+
+       g_hash_table_replace(network_table, network, dhcp);
+
+       /* Initial timeout, RFC 3315, 17.1.2 */
+       delay = rand() % 1000;
+
+       dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
+
+       return 0;
+}
+
 void __connman_dhcpv6_stop(struct connman_network *network)
 {
        DBG("");
@@ -413,7 +1220,7 @@ int __connman_dhcpv6_init(void)
 {
        DBG("");
 
-       srand(time(0));
+       srand(time(NULL));
 
        network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
                                                        NULL, remove_network);