+ 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_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
+
+ g_dhcp_client_start(dhcp->dhcp_client, NULL);
+
+ return FALSE;
+}
+
+static gboolean start_rebind(gpointer user_data)
+{
+ struct connman_dhcpv6 *dhcp = user_data;
+
+ if (check_restart(dhcp) < 0)
+ return FALSE;
+
+ dhcp->RT = initial_rt(REB_TIMEOUT);
+
+ 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("");
+
+ g_dhcpv6_client_reset_request(dhcp_client);
+ g_dhcpv6_client_clear_retransmit(dhcp_client);
+
+ re_cb(REQ_REQUEST, dhcp_client, user_data);
+}
+
+static int dhcpv6_request(struct connman_dhcpv6 *dhcp,
+ bool 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_DOMAIN_LIST);
+ g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
+
+ g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
+ G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
+
+ g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
+ g_dhcpv6_client_set_ia(dhcp_client,
+ connman_network_get_index(dhcp->network),
+ dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
+ &T1, &T2, add_addresses, NULL);
+
+ 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)
+ dhcp->callback(dhcp->network,
+ CONNMAN_DHCPV6_STATUS_FAIL, NULL);
+ 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_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
+
+ 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_request(dhcp_client);
+ g_dhcpv6_client_clear_retransmit(dhcp_client);
+
+ re_cb(REQ_RENEW, 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_DOMAIN_LIST);
+ g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
+
+ g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
+ G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
+
+ g_dhcpv6_client_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
+ g_dhcpv6_client_set_ia(dhcp_client,
+ connman_network_get_index(dhcp->network),
+ dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
+ &T1, &T2, TRUE, NULL);
+
+ 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;
+ time_t last_rebind, current;
+ uint32_t T2;
+
+ if (check_restart(dhcp) < 0)
+ return FALSE;
+
+ g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, &T2,
+ &last_rebind, NULL);
+ current = time(NULL);
+ if ((unsigned)current >= (unsigned)last_rebind + T2) {
+ /*
+ * Do rebind instead if past T2
+ */
+ start_rebind(dhcp);
+ 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_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
+
+ 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 = initial_rt(REN_TIMEOUT);
+
+ 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,
+ dhcpv6_cb callback)
+{
+ struct connman_dhcpv6 *dhcp;
+ uint32_t T1, T2, delta;
+ time_t started, current, expired;
+
+ dhcp = g_hash_table_lookup(network_table, network);
+ if (!dhcp)
+ return -ENOENT;
+
+ DBG("network %p dhcp %p", network, dhcp);
+
+ clear_timer(dhcp);
+
+ g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, &T1, &T2,
+ &started, &expired);
+
+ current = time(NULL);
+
+ DBG("T1 %u T2 %u expires %lu current %lu started %lu", T1, T2,
+ (unsigned long)expired, current, started);
+
+ if (T1 == 0xffffffff)
+ /* RFC 3315, 22.4 */
+ return 0;
+
+ if (T1 == 0) {
+ /* RFC 3315, 22.4
+ * Client can choose the timeout.
+ */
+ T1 = (expired - started) / 2;
+ T2 = (expired - started) / 10 * 8;
+ }
+
+ dhcp->callback = callback;
+
+ /* RFC 3315, 18.1.4, start solicit if expired */
+ if (check_restart(dhcp) < 0)
+ return 0;
+
+ if (T2 != 0xffffffff && T2 > 0) {
+ if ((unsigned)current >= (unsigned)started + T2) {
+ /* RFC 3315, chapter 18.1.3, start rebind */
+ DBG("start rebind immediately");
+
+ dhcp->timeout = g_idle_add(start_rebind, dhcp);
+
+ } else if ((unsigned)current < (unsigned)started + T1) {
+ delta = started + T1 - current;
+ DBG("renew after %d secs", delta);
+
+ dhcp->timeout = g_timeout_add_seconds(delta,
+ start_renew, dhcp);
+ } else {
+ delta = started + T2 - current;
+ DBG("rebind after %d secs", delta);
+
+ dhcp->timeout = g_timeout_add_seconds(delta,
+ start_rebind, dhcp);
+ }
+ }
+
+ return 0;
+}
+
+static void release_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+ DBG("");
+}
+
+int __connman_dhcpv6_start_release(struct connman_network *network,
+ dhcpv6_cb callback)
+{
+ struct connman_dhcpv6 *dhcp;
+ GDHCPClient *dhcp_client;
+
+ if (!network_table)
+ return 0; /* we are already released */
+
+ dhcp = g_hash_table_lookup(network_table, network);
+ if (!dhcp)
+ return -ENOENT;
+
+ DBG("network %p dhcp %p client %p stateless %d", network, dhcp,
+ dhcp->dhcp_client, dhcp->stateless);
+
+ if (dhcp->stateless)
+ return -EINVAL;
+
+ clear_timer(dhcp);
+
+ dhcp_client = dhcp->dhcp_client;
+ if (!dhcp_client) {
+ /*
+ * 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 ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
+ NULL, NULL, TRUE, NULL);
+
+ clear_callbacks(dhcp_client);
+
+ /*
+ * Although we register a callback here we are really not interested in
+ * the answer because it 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.
+ */
+ g_dhcp_client_register_event(dhcp_client,
+ G_DHCP_CLIENT_EVENT_RELEASE,
+ release_cb, NULL);
+
+ 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)
+ return 0;
+
+ g_dhcp_client_stop(dhcp->dhcp_client);
+ g_dhcp_client_unref(dhcp->dhcp_client);
+
+ dhcp->dhcp_client = NULL;
+
+ return 0;
+}
+
+static void remove_network(gpointer user_data)
+{
+ struct connman_dhcpv6 *dhcp = user_data;
+
+ DBG("dhcp %p", dhcp);
+
+ dhcpv6_release(dhcp);
+
+ g_free(dhcp);
+}
+
+static gboolean timeout_info_req(gpointer user_data)
+{
+ struct connman_dhcpv6 *dhcp = user_data;
+
+ dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
+
+ DBG("info RT timeout %d msec", dhcp->RT);
+
+ dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
+
+ g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
+
+ g_dhcp_client_start(dhcp->dhcp_client, NULL);
+
+ return FALSE;
+}
+
+static gboolean start_info_req(gpointer user_data)
+{
+ struct connman_dhcpv6 *dhcp = user_data;
+
+ /* Set the retransmission timeout, RFC 3315 chapter 14 */
+ dhcp->RT = initial_rt(INF_TIMEOUT);
+
+ DBG("info initial RT timeout %d msec", dhcp->RT);
+
+ dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
+
+ dhcpv6_info_request(dhcp);
+
+ return FALSE;
+}
+
+int __connman_dhcpv6_start_info(struct connman_network *network,
+ dhcpv6_cb callback)
+{
+ struct connman_dhcpv6 *dhcp;
+ int delay;
+ uint64_t rand;
+
+ DBG("");
+
+ if (network_table) {
+ dhcp = g_hash_table_lookup(network_table, network);
+ if (dhcp && dhcp->started)
+ return -EBUSY;
+ }
+
+ dhcp = g_try_new0(struct connman_dhcpv6, 1);
+ if (!dhcp)
+ 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 */
+ __connman_util_get_random(&rand);
+ delay = rand % 1000;
+
+ dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
+
+ 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);
+
+ g_dhcpv6_client_clear_retransmit(dhcp_client);
+
+ if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
+ if (dhcp->callback)
+ dhcp->callback(dhcp->network,
+ CONNMAN_DHCPV6_STATUS_FAIL, NULL);
+ return;
+ }
+
+ dhcp->RT = initial_rt(REQ_TIMEOUT);
+ 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);
+
+ g_dhcpv6_client_clear_retransmit(dhcp_client);
+
+ if (g_dhcpv6_client_get_status(dhcp_client) != 0) {
+ if (dhcp->callback)
+ dhcp->callback(dhcp->network,
+ CONNMAN_DHCPV6_STATUS_FAIL, NULL);
+ return;
+ }
+
+ do_dad(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_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
+
+ g_dhcp_client_start(dhcp->dhcp_client, NULL);
+
+ return FALSE;
+}
+
+static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
+{
+ struct connman_service *service;
+#if !defined TIZEN_EXT
+ struct connman_ipconfig *ipconfig_ipv6;
+#endif
+ 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 !defined TIZEN_EXT
+ if (getenv("CONNMAN_DHCPV6_DEBUG"))
+#endif
+ g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
+
+ service = connman_service_lookup_from_network(dhcp->network);
+ if (!service) {
+ 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_DOMAIN_LIST);
+ g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
+
+ g_dhcpv6_client_set_oro(dhcp_client, 3, G_DHCPV6_DNS_SERVERS,
+ G_DHCPV6_DOMAIN_LIST, G_DHCPV6_SNTP_SERVERS);
+
+#if defined TIZEN_EXT
+ /**
+ When privacy extension is enabled then connman requests
+ OPTION_IA_TA (4) from DHCPv6 server. This option is used to request
+ temporary IPv6 address from DHCPv6 server but we found that DHCPv6
+ server never provided temporary IPv6 address and connman resend dhcpv6
+ requests. So always set OPTION_IA_NA in dhcpv6 request to get IPv6
+ address from DHCPv6 server.
+ */
+ dhcp->use_ta = FALSE;
+#else
+ ipconfig_ipv6 = __connman_service_get_ip6config(service);
+ dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
+#endif
+
+ g_dhcpv6_client_set_ia(dhcp_client, index,
+ dhcp->use_ta ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
+ NULL, NULL, FALSE, NULL);
+
+ 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 = initial_rt(SOL_TIMEOUT);
+
+ 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, dhcpv6_cb callback)
+{
+ struct connman_service *service;
+ struct connman_dhcpv6 *dhcp;
+ int delay;
+ uint64_t rand;
+
+ DBG("");
+
+ if (network_table) {
+ dhcp = g_hash_table_lookup(network_table, network);
+ if (dhcp && dhcp->started)
+ return -EBUSY;
+ }
+
+ service = connman_service_lookup_from_network(network);
+ if (!service)
+ return -EINVAL;
+
+ dhcp = g_try_new0(struct connman_dhcpv6, 1);
+ if (!dhcp)
+ 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 */
+ __connman_util_get_random(&rand);
+ delay = rand % 1000;
+
+ /*
+ * Start from scratch.
+ * RFC 3315, chapter 17.1.2 Solicitation message
+ *
+ * Note that we do not send CONFIRM message here as it does
+ * not make much sense because we do not save expiration time
+ * so we cannot really know how long the saved address is valid
+ * anyway. The reply to CONFIRM message does not send
+ * expiration times back to us. Because of this we need to
+ * start using SOLICITATION anyway.
+ */
+ dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
+
+ return 0;
+}
+
+void __connman_dhcpv6_stop(struct connman_network *network)
+{
+ DBG("");
+
+ if (!network_table)
+ return;
+
+ if (g_hash_table_remove(network_table, network))
+ connman_network_unref(network);
+}
+
+static int save_prefixes(struct connman_ipconfig *ipconfig,
+ GSList *prefixes)
+{
+ GSList *list;
+ int i = 0, count = g_slist_length(prefixes);
+ char **array;
+
+ if (count == 0)
+ return 0;
+
+ array = g_try_new0(char *, count + 1);
+ if (!array)
+ return -ENOMEM;
+
+ for (list = prefixes; list; list = list->next) {
+ char *elem, addr_str[INET6_ADDRSTRLEN];
+ GDHCPIAPrefix *prefix = list->data;
+
+ elem = g_strdup_printf("%s/%d", inet_ntop(AF_INET6,
+ &prefix->prefix, addr_str, INET6_ADDRSTRLEN),
+ prefix->prefixlen);
+ if (!elem) {
+ g_strfreev(array);
+ return -ENOMEM;
+ }
+
+ array[i++] = elem;
+ }
+
+ __connman_ipconfig_set_dhcpv6_prefixes(ipconfig, array);
+ return 0;
+}
+
+static GSList *load_prefixes(struct connman_ipconfig *ipconfig)
+{
+ int i;
+ GSList *list = NULL;
+ char **array = __connman_ipconfig_get_dhcpv6_prefixes(ipconfig);
+
+ if (!array)
+ return NULL;
+
+ for (i = 0; array[i]; i++) {
+ GDHCPIAPrefix *prefix;
+ long int value;
+ char *ptr, **elems = g_strsplit(array[i], "/", 0);
+
+ if (!elems)
+ return list;
+
+ value = strtol(elems[1], &ptr, 10);
+ if (ptr != elems[1] && *ptr == '\0' && value <= 128) {
+ struct in6_addr addr;
+
+ if (inet_pton(AF_INET6, elems[0], &addr) == 1) {
+ prefix = g_try_new0(GDHCPIAPrefix, 1);
+ if (!prefix) {
+ g_strfreev(elems);
+ return list;
+ }
+ memcpy(&prefix->prefix, &addr,
+ sizeof(struct in6_addr));
+ prefix->prefixlen = value;
+
+ list = g_slist_prepend(list, prefix);
+ }
+ }
+
+ g_strfreev(elems);
+ }
+
+ return list;
+}
+
+static GDHCPIAPrefix *copy_prefix(gpointer data)
+{
+ GDHCPIAPrefix *copy, *prefix = data;
+
+ copy = g_try_new(GDHCPIAPrefix, 1);
+ if (!copy)
+ return NULL;
+
+ memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
+
+ return copy;
+}
+
+static GSList *copy_and_convert_prefixes(GList *prefixes)
+{
+ GSList *copy = NULL;
+ GList *list;
+
+ for (list = prefixes; list; list = list->next)
+ copy = g_slist_prepend(copy, copy_prefix(list->data));
+
+ return copy;
+}
+
+static int set_prefixes(GDHCPClient *dhcp_client, struct connman_dhcpv6 *dhcp)
+{
+ if (dhcp->prefixes)
+ g_slist_free_full(dhcp->prefixes, free_prefix);
+
+ dhcp->prefixes =
+ copy_and_convert_prefixes(g_dhcp_client_get_option(dhcp_client,
+ G_DHCPV6_IA_PD));
+
+ DBG("Got %d prefix", g_slist_length(dhcp->prefixes));
+
+ if (dhcp->callback) {
+ uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
+ if (status == G_DHCPV6_ERROR_NO_PREFIX)
+ dhcp->callback(dhcp->network,
+ CONNMAN_DHCPV6_STATUS_FAIL, NULL);
+ else {
+ struct connman_service *service;
+ struct connman_ipconfig *ipconfig;
+ int ifindex = connman_network_get_index(dhcp->network);
+
+ service = __connman_service_lookup_from_index(ifindex);
+ if (service) {
+ ipconfig = __connman_service_get_ip6config(
+ service);
+ save_prefixes(ipconfig, dhcp->prefixes);
+ __connman_service_save(service);
+ }
+
+ dhcp->callback(dhcp->network,
+ CONNMAN_DHCPV6_STATUS_SUCCEED, dhcp->prefixes);
+ }
+ } else {
+ g_slist_free_full(dhcp->prefixes, free_prefix);
+ dhcp->prefixes = NULL;
+ }
+
+ return 0;
+}
+
+static void re_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+ struct connman_dhcpv6 *dhcp = user_data;
+ uint16_t status;
+ int ret;
+
+ status = g_dhcpv6_client_get_status(dhcp_client);
+
+ DBG("dhcpv6 cb msg %p status %d", dhcp, status);
+
+ if (status == G_DHCPV6_ERROR_BINDING) {
+ /* RFC 3315, 18.1.8 */
+ dhcpv6_pd_request(dhcp);
+ } else {
+ ret = set_prefixes(dhcp_client, dhcp);
+ if (ret < 0) {
+ if (dhcp->callback)
+ dhcp->callback(dhcp->network,
+ CONNMAN_DHCPV6_STATUS_FAIL, NULL);
+ return;
+ }
+ }
+}
+
+static void rebind_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+ DBG("");
+
+ g_dhcpv6_client_clear_retransmit(dhcp_client);
+
+ re_pd_cb(dhcp_client, user_data);
+}
+
+static GDHCPClient *create_pd_client(struct connman_dhcpv6 *dhcp, int *err)
+{
+ GDHCPClient *dhcp_client;
+ GDHCPClientError error;
+ struct connman_service *service;
+ int index, ret;
+ uint32_t iaid;
+
+ 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) {
+ *err = -EINVAL;
+ return NULL;
+ }
+
+#if !defined TIZEN_EXT
+ if (getenv("CONNMAN_DHCPV6_DEBUG"))
+#endif
+ g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6:PD");
+
+ service = connman_service_lookup_from_network(dhcp->network);
+ if (!service) {
+ g_dhcp_client_unref(dhcp_client);
+ *err = -EINVAL;
+ return NULL;
+ }
+
+ ret = set_duid(service, dhcp->network, dhcp_client, index);
+ if (ret < 0) {
+ g_dhcp_client_unref(dhcp_client);
+ *err = ret;
+ return NULL;
+ }
+
+ g_dhcpv6_client_create_iaid(dhcp_client, index, (unsigned char *)&iaid);
+ g_dhcpv6_client_set_iaid(dhcp_client, iaid);
+
+ return dhcp_client;
+}
+
+static int dhcpv6_pd_rebind(struct connman_dhcpv6 *dhcp)
+{
+ GDHCPClient *dhcp_client;
+ uint32_t T1, T2;
+
+ DBG("dhcp %p", dhcp);
+
+ if (!dhcp->dhcp_client) {
+ /*
+ * We skipped the solicitation phase
+ */
+ int err;
+
+ dhcp->dhcp_client = create_pd_client(dhcp, &err);
+ if (!dhcp->dhcp_client) {
+ clear_timer(dhcp);
+ return err;
+ }
+ }
+
+ 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_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
+ g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
+
+ clear_callbacks(dhcp_client);
+
+ g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_REBIND,
+ rebind_pd_cb, dhcp);
+
+ return g_dhcp_client_start(dhcp_client, NULL);
+}
+
+static void renew_pd_cb(GDHCPClient *dhcp_client, gpointer user_data)
+{
+ DBG("");
+
+ g_dhcpv6_client_clear_retransmit(dhcp_client);
+
+ re_pd_cb(dhcp_client, user_data);
+}
+
+static int dhcpv6_pd_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_get_timeouts(dhcp_client, &T1, &T2, NULL, NULL);
+ g_dhcpv6_client_set_pd(dhcp_client, &T1, &T2, dhcp->prefixes);
+
+ clear_callbacks(dhcp_client);
+
+ g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_RENEW,
+ renew_pd_cb, dhcp);
+
+ return g_dhcp_client_start(dhcp_client, NULL);
+}
+
+/*
+ * Check if we need to restart the solicitation procedure. This
+ * is done if all the prefixes have expired.
+ */
+static int check_pd_restart(struct connman_dhcpv6 *dhcp)
+{
+ time_t current, expired;
+
+ g_dhcpv6_client_get_timeouts(dhcp->dhcp_client, NULL, NULL,
+ NULL, &expired);
+ current = time(NULL);
+
+ if (current > expired) {
+ DBG("expired by %d secs", (int)(current - expired));
+
+ g_idle_add(dhcpv6_restart, dhcp);
+
+ return -ETIMEDOUT;
+ }
+
+ return 0;
+}
+
+static gboolean timeout_pd_rebind(gpointer user_data)
+{
+ struct connman_dhcpv6 *dhcp = user_data;
+
+ if (check_pd_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_pd_rebind, dhcp);
+
+ g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
+
+ g_dhcp_client_start(dhcp->dhcp_client, NULL);
+
+ return FALSE;
+}
+
+static gboolean start_pd_rebind(gpointer user_data)
+{
+ struct connman_dhcpv6 *dhcp = user_data;
+
+ if (check_pd_restart(dhcp) < 0)
+ return FALSE;
+
+ dhcp->RT = initial_rt(REB_TIMEOUT);
+
+ DBG("rebind initial RT timeout %d msec", dhcp->RT);
+
+ dhcp->timeout = g_timeout_add(dhcp->RT, timeout_pd_rebind, dhcp);
+
+ dhcpv6_pd_rebind(dhcp);
+
+ return FALSE;
+}
+
+static gboolean timeout_pd_rebind_confirm(gpointer user_data)
+{
+ struct connman_dhcpv6 *dhcp = user_data;
+
+ dhcp->RT = calc_delay(dhcp->RT, CNF_MAX_RT);
+
+ DBG("rebind with confirm RT timeout %d msec", dhcp->RT);
+
+ dhcp->timeout = g_timeout_add(dhcp->RT,
+ timeout_pd_rebind_confirm, dhcp);
+
+ g_dhcpv6_client_set_retransmit(dhcp->dhcp_client);
+
+ g_dhcp_client_start(dhcp->dhcp_client, NULL);
+
+ return FALSE;
+}
+
+static gboolean timeout_pd_max_confirm(gpointer user_data)
+{
+ struct connman_dhcpv6 *dhcp = user_data;
+
+ dhcp->MRD = 0;
+
+ clear_timer(dhcp);
+
+ DBG("rebind with confirm max retransmit duration timeout");
+
+ g_dhcpv6_client_clear_retransmit(dhcp->dhcp_client);
+
+ if (dhcp->callback)
+ dhcp->callback(dhcp->network,
+ CONNMAN_DHCPV6_STATUS_FAIL, NULL);
+
+ return FALSE;
+}
+
+static gboolean start_pd_rebind_with_confirm(gpointer user_data)
+{
+ struct connman_dhcpv6 *dhcp = user_data;
+
+ dhcp->RT = initial_rt(CNF_TIMEOUT);
+
+ DBG("rebind with confirm initial RT timeout %d msec", dhcp->RT);
+
+ dhcp->timeout = g_timeout_add(dhcp->RT,
+ timeout_pd_rebind_confirm, dhcp);
+ dhcp->MRD = g_timeout_add(CNF_MAX_RD, timeout_pd_max_confirm, dhcp);
+
+ dhcpv6_pd_rebind(dhcp);
+
+ return FALSE;
+}
+
+static gboolean timeout_pd_renew(gpointer user_data)
+{
+ struct connman_dhcpv6 *dhcp = user_data;
+
+ if (check_pd_restart(dhcp) < 0)
+ return FALSE;
+