5 * Copyright (C) 2011 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <connman/ipconfig.h>
31 #include <connman/storage.h>
33 #include <gdhcp/gdhcp.h>
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)
45 struct connman_dhcpv6 {
46 struct connman_network *network;
52 GDHCPClient *dhcp_client;
55 guint RT; /* in msec */
58 static GHashTable *network_table;
60 static inline float get_random()
62 return (rand() % 200 - 100) / 1000.0;
65 /* Calculate a random delay, RFC 3315 chapter 14 */
66 /* RT and MRT are milliseconds */
67 static guint calc_delay(guint RT, guint MRT)
69 float delay = get_random();
70 float rt = RT * (2 + delay);
73 rt = MRT * (1 + delay);
81 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
83 g_strfreev(dhcp->nameservers);
84 g_strfreev(dhcp->timeservers);
86 dhcp->nameservers = NULL;
87 dhcp->timeservers = NULL;
90 static gboolean compare_string_arrays(char **array_a, char **array_b)
94 if (array_a == NULL || array_b == NULL)
97 if (g_strv_length(array_a) != g_strv_length(array_b))
100 for (i = 0; array_a[i] != NULL && array_b[i] != NULL; i++)
101 if (g_strcmp0(array_a[i], array_b[i]) != 0)
107 static void dhcpv6_debug(const char *str, void *data)
109 connman_info("%s: %s\n", (const char *) data, str);
112 static gchar *convert_to_hex(unsigned char *buf, int len)
114 gchar *ret = g_try_malloc(len * 2 + 1);
117 for (i = 0; ret != NULL && i < len; i++)
118 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
124 * DUID should not change over time so save it to file.
125 * See RFC 3315 chapter 9 for details.
127 static int set_duid(struct connman_service *service,
128 struct connman_network *network,
129 GDHCPClient *dhcp_client, int index)
137 ident = __connman_service_get_ident(service);
139 keyfile = connman_storage_load_service(ident);
143 hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
145 if (hex_duid != NULL) {
146 unsigned int i, j = 0, hex;
147 size_t hex_duid_len = strlen(hex_duid);
149 duid = g_try_malloc0(hex_duid_len / 2);
151 g_key_file_free(keyfile);
156 for (i = 0; i < hex_duid_len; i += 2) {
157 sscanf(hex_duid + i, "%02x", &hex);
161 duid_len = hex_duid_len / 2;
164 int type = __connman_ipconfig_get_type_from_index(index);
166 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
169 g_key_file_free(keyfile);
173 hex_duid = convert_to_hex(duid, duid_len);
174 if (hex_duid == NULL) {
175 g_key_file_free(keyfile);
179 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
182 __connman_storage_save_service(keyfile, ident);
186 g_key_file_free(keyfile);
188 g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
193 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
195 struct connman_dhcpv6 *dhcp = user_data;
196 struct connman_service *service;
198 GList *option, *list;
199 char **nameservers, **timeservers;
201 DBG("dhcpv6 information-request %p", dhcp);
203 service = __connman_service_lookup_from_network(dhcp->network);
204 if (service == NULL) {
205 connman_error("Can not lookup service");
209 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
210 entries = g_list_length(option);
212 nameservers = g_try_new0(char *, entries + 1);
213 if (nameservers != NULL) {
214 for (i = 0, list = option; list; list = list->next, i++)
215 nameservers[i] = g_strdup(list->data);
218 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
219 if (dhcp->nameservers != NULL) {
220 for (i = 0; dhcp->nameservers[i] != NULL; i++)
221 __connman_service_nameserver_remove(service,
222 dhcp->nameservers[i],
224 g_strfreev(dhcp->nameservers);
227 dhcp->nameservers = nameservers;
229 for (i = 0; dhcp->nameservers[i] != NULL; i++)
230 __connman_service_nameserver_append(service,
231 dhcp->nameservers[i],
234 g_strfreev(nameservers);
237 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
238 entries = g_list_length(option);
240 timeservers = g_try_new0(char *, entries + 1);
241 if (timeservers != NULL) {
242 for (i = 0, list = option; list; list = list->next, i++)
243 timeservers[i] = g_strdup(list->data);
246 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
247 if (dhcp->timeservers != NULL) {
248 for (i = 0; dhcp->timeservers[i] != NULL; i++)
249 __connman_service_timeserver_remove(service,
250 dhcp->timeservers[i]);
251 g_strfreev(dhcp->timeservers);
254 dhcp->timeservers = timeservers;
256 for (i = 0; dhcp->timeservers[i] != NULL; i++)
257 __connman_service_timeserver_append(service,
258 dhcp->timeservers[i]);
260 g_strfreev(timeservers);
263 if (dhcp->callback != NULL) {
264 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
265 dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
269 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
271 struct connman_service *service;
272 GDHCPClient *dhcp_client;
273 GDHCPClientError error;
276 DBG("dhcp %p", dhcp);
278 index = connman_network_get_index(dhcp->network);
280 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
281 if (error != G_DHCP_CLIENT_ERROR_NONE)
284 if (getenv("CONNMAN_DHCPV6_DEBUG"))
285 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
287 service = __connman_service_lookup_from_network(dhcp->network);
291 ret = set_duid(service, dhcp->network, dhcp_client, index);
295 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
296 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
297 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
299 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
300 G_DHCPV6_SNTP_SERVERS);
302 g_dhcp_client_register_event(dhcp_client,
303 G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
305 dhcp->dhcp_client = dhcp_client;
307 return g_dhcp_client_start(dhcp_client, NULL);
310 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
312 DBG("dhcp %p", dhcp);
314 if (dhcp->timeout > 0) {
315 g_source_remove(dhcp->timeout);
321 if (dhcp->dhcp_client == NULL)
324 g_dhcp_client_stop(dhcp->dhcp_client);
325 g_dhcp_client_unref(dhcp->dhcp_client);
327 dhcp->dhcp_client = NULL;
332 static void remove_network(gpointer user_data)
334 struct connman_dhcpv6 *dhcp = user_data;
336 DBG("dhcp %p", dhcp);
338 dhcpv6_release(dhcp);
343 static gboolean timeout_info_req(gpointer user_data)
345 struct connman_dhcpv6 *dhcp = user_data;
347 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
349 DBG("info RT timeout %d msec", dhcp->RT);
351 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
353 g_dhcp_client_start(dhcp->dhcp_client, NULL);
358 static gboolean start_info_req(gpointer user_data)
360 struct connman_dhcpv6 *dhcp = user_data;
362 /* Set the retransmission timeout, RFC 3315 chapter 14 */
363 dhcp->RT = INF_TIMEOUT * (1 + get_random());
365 DBG("info initial RT timeout %d msec", dhcp->RT);
367 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
369 dhcpv6_info_request(dhcp);
374 int __connman_dhcpv6_start_info(struct connman_network *network,
377 struct connman_dhcpv6 *dhcp;
382 dhcp = g_try_new0(struct connman_dhcpv6, 1);
386 dhcp->network = network;
387 dhcp->callback = callback;
389 connman_network_ref(network);
391 g_hash_table_replace(network_table, network, dhcp);
393 /* Initial timeout, RFC 3315, 18.1.5 */
394 delay = rand() % 1000;
396 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
401 void __connman_dhcpv6_stop(struct connman_network *network)
405 if (network_table == NULL)
408 if (g_hash_table_remove(network_table, network) == TRUE)
409 connman_network_unref(network);
412 int __connman_dhcpv6_init(void)
418 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
419 NULL, remove_network);
424 void __connman_dhcpv6_cleanup(void)
428 g_hash_table_destroy(network_table);
429 network_table = NULL;