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)
43 #define SOL_MAX_DELAY (1 * 1000)
44 #define SOL_TIMEOUT (1 * 1000)
45 #define SOL_MAX_RT (120 * 1000)
48 struct connman_dhcpv6 {
49 struct connman_network *network;
55 GDHCPClient *dhcp_client;
57 guint timeout; /* operation timeout in msec */
58 guint RT; /* in msec */
59 gboolean use_ta; /* set to TRUE if IPv6 privacy is enabled */
60 GSList *prefixes; /* network prefixes from radvd */
63 static GHashTable *network_table;
65 static inline float get_random()
67 return (rand() % 200 - 100) / 1000.0;
70 /* Calculate a random delay, RFC 3315 chapter 14 */
71 /* RT and MRT are milliseconds */
72 static guint calc_delay(guint RT, guint MRT)
74 float delay = get_random();
75 float rt = RT * (2 + delay);
78 rt = MRT * (1 + delay);
86 static void free_prefix(gpointer data, gpointer user_data)
91 static void dhcpv6_free(struct connman_dhcpv6 *dhcp)
93 g_strfreev(dhcp->nameservers);
94 g_strfreev(dhcp->timeservers);
96 dhcp->nameservers = NULL;
97 dhcp->timeservers = NULL;
99 g_slist_foreach(dhcp->prefixes, free_prefix, NULL);
100 g_slist_free(dhcp->prefixes);
103 static gboolean compare_string_arrays(char **array_a, char **array_b)
107 if (array_a == NULL || array_b == NULL)
110 if (g_strv_length(array_a) != g_strv_length(array_b))
113 for (i = 0; array_a[i] != NULL && array_b[i] != NULL; i++)
114 if (g_strcmp0(array_a[i], array_b[i]) != 0)
120 static void dhcpv6_debug(const char *str, void *data)
122 connman_info("%s: %s\n", (const char *) data, str);
125 static gchar *convert_to_hex(unsigned char *buf, int len)
127 gchar *ret = g_try_malloc(len * 2 + 1);
130 for (i = 0; ret != NULL && i < len; i++)
131 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
137 * DUID should not change over time so save it to file.
138 * See RFC 3315 chapter 9 for details.
140 static int set_duid(struct connman_service *service,
141 struct connman_network *network,
142 GDHCPClient *dhcp_client, int index)
150 ident = __connman_service_get_ident(service);
152 keyfile = connman_storage_load_service(ident);
156 hex_duid = g_key_file_get_string(keyfile, ident, "IPv6.DHCP.DUID",
158 if (hex_duid != NULL) {
159 unsigned int i, j = 0, hex;
160 size_t hex_duid_len = strlen(hex_duid);
162 duid = g_try_malloc0(hex_duid_len / 2);
164 g_key_file_free(keyfile);
169 for (i = 0; i < hex_duid_len; i += 2) {
170 sscanf(hex_duid + i, "%02x", &hex);
174 duid_len = hex_duid_len / 2;
177 int type = __connman_ipconfig_get_type_from_index(index);
179 ret = g_dhcpv6_create_duid(G_DHCPV6_DUID_LLT, index, type,
182 g_key_file_free(keyfile);
186 hex_duid = convert_to_hex(duid, duid_len);
187 if (hex_duid == NULL) {
188 g_key_file_free(keyfile);
192 g_key_file_set_string(keyfile, ident, "IPv6.DHCP.DUID",
195 __connman_storage_save_service(keyfile, ident);
199 g_key_file_free(keyfile);
201 g_dhcpv6_client_set_duid(dhcp_client, duid, duid_len);
206 static void clear_callbacks(GDHCPClient *dhcp_client)
208 g_dhcp_client_register_event(dhcp_client,
209 G_DHCP_CLIENT_EVENT_SOLICITATION,
212 g_dhcp_client_register_event(dhcp_client,
213 G_DHCP_CLIENT_EVENT_ADVERTISE,
216 g_dhcp_client_register_event(dhcp_client,
217 G_DHCP_CLIENT_EVENT_INFORMATION_REQ,
221 static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
223 struct connman_dhcpv6 *dhcp = user_data;
224 struct connman_service *service;
226 GList *option, *list;
227 char **nameservers, **timeservers;
229 DBG("dhcpv6 information-request %p", dhcp);
231 service = __connman_service_lookup_from_network(dhcp->network);
232 if (service == NULL) {
233 connman_error("Can not lookup service");
237 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
238 entries = g_list_length(option);
240 nameservers = g_try_new0(char *, entries + 1);
241 if (nameservers != NULL) {
242 for (i = 0, list = option; list; list = list->next, i++)
243 nameservers[i] = g_strdup(list->data);
246 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
247 if (dhcp->nameservers != NULL) {
248 for (i = 0; dhcp->nameservers[i] != NULL; i++)
249 __connman_service_nameserver_remove(service,
250 dhcp->nameservers[i],
252 g_strfreev(dhcp->nameservers);
255 dhcp->nameservers = nameservers;
257 for (i = 0; dhcp->nameservers[i] != NULL; i++)
258 __connman_service_nameserver_append(service,
259 dhcp->nameservers[i],
262 g_strfreev(nameservers);
265 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
266 entries = g_list_length(option);
268 timeservers = g_try_new0(char *, entries + 1);
269 if (timeservers != NULL) {
270 for (i = 0, list = option; list; list = list->next, i++)
271 timeservers[i] = g_strdup(list->data);
274 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
275 if (dhcp->timeservers != NULL) {
276 for (i = 0; dhcp->timeservers[i] != NULL; i++)
277 __connman_service_timeserver_remove(service,
278 dhcp->timeservers[i]);
279 g_strfreev(dhcp->timeservers);
282 dhcp->timeservers = timeservers;
284 for (i = 0; dhcp->timeservers[i] != NULL; i++)
285 __connman_service_timeserver_append(service,
286 dhcp->timeservers[i]);
288 g_strfreev(timeservers);
291 if (dhcp->callback != NULL) {
292 uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
293 dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
297 static int dhcpv6_info_request(struct connman_dhcpv6 *dhcp)
299 struct connman_service *service;
300 GDHCPClient *dhcp_client;
301 GDHCPClientError error;
304 DBG("dhcp %p", dhcp);
306 index = connman_network_get_index(dhcp->network);
308 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
309 if (error != G_DHCP_CLIENT_ERROR_NONE)
312 if (getenv("CONNMAN_DHCPV6_DEBUG"))
313 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
315 service = __connman_service_lookup_from_network(dhcp->network);
319 ret = set_duid(service, dhcp->network, dhcp_client, index);
323 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
324 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
325 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
327 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
328 G_DHCPV6_SNTP_SERVERS);
330 g_dhcp_client_register_event(dhcp_client,
331 G_DHCP_CLIENT_EVENT_INFORMATION_REQ, info_req_cb, dhcp);
333 dhcp->dhcp_client = dhcp_client;
335 return g_dhcp_client_start(dhcp_client, NULL);
338 static int check_ipv6_addr_prefix(GSList *prefixes, char *address)
340 struct in6_addr addr_prefix, addr;
344 for (list = prefixes; list; list = list->next) {
345 char *prefix = list->data;
346 const char *slash = g_strrstr(prefix, "/");
347 const unsigned char bits[] = { 0x00, 0xFE, 0xFC, 0xF8,
348 0xF0, 0xE0, 0xC0, 0x80 };
349 int left, count, i, plen;
354 prefix = g_strndup(prefix, slash - prefix);
355 len = strtol(slash + 1, NULL, 10);
362 inet_pton(AF_INET6, prefix, &addr_prefix);
363 inet_pton(AF_INET6, address, &addr);
365 memset(&addr_prefix.s6_addr[i], 0, count);
366 memset(&addr.s6_addr[i], 0, count);
369 addr_prefix.s6_addr[i - 1] &= bits[left];
370 addr.s6_addr[i - 1] &= bits[left];
375 if (memcmp(&addr_prefix, &addr, 16) == 0) {
384 static int set_addresses(GDHCPClient *dhcp_client,
385 struct connman_dhcpv6 *dhcp)
387 struct connman_service *service;
388 struct connman_ipconfig *ipconfig;
390 GList *option, *list;
391 char **nameservers, **timeservers;
392 const char *c_address;
393 char *address = NULL;
395 service = __connman_service_lookup_from_network(dhcp->network);
396 if (service == NULL) {
397 connman_error("Can not lookup service");
401 ipconfig = __connman_service_get_ip6config(service);
402 if (ipconfig == NULL) {
403 connman_error("Could not lookup ip6config");
407 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
408 entries = g_list_length(option);
410 nameservers = g_try_new0(char *, entries + 1);
411 if (nameservers != NULL) {
412 for (i = 0, list = option; list; list = list->next, i++)
413 nameservers[i] = g_strdup(list->data);
416 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
417 if (dhcp->nameservers != NULL) {
418 for (i = 0; dhcp->nameservers[i] != NULL; i++)
419 __connman_service_nameserver_remove(service,
420 dhcp->nameservers[i],
422 g_strfreev(dhcp->nameservers);
425 dhcp->nameservers = nameservers;
427 for (i = 0; dhcp->nameservers[i] != NULL; i++)
428 __connman_service_nameserver_append(service,
429 dhcp->nameservers[i],
432 g_strfreev(nameservers);
435 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
436 entries = g_list_length(option);
438 timeservers = g_try_new0(char *, entries + 1);
439 if (timeservers != NULL) {
440 for (i = 0, list = option; list; list = list->next, i++)
441 timeservers[i] = g_strdup(list->data);
444 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
445 if (dhcp->timeservers != NULL) {
446 for (i = 0; dhcp->timeservers[i] != NULL; i++)
447 __connman_service_timeserver_remove(service,
448 dhcp->timeservers[i]);
449 g_strfreev(dhcp->timeservers);
452 dhcp->timeservers = timeservers;
454 for (i = 0; dhcp->timeservers[i] != NULL; i++)
455 __connman_service_timeserver_append(service,
456 dhcp->timeservers[i]);
458 g_strfreev(timeservers);
461 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
463 address = g_strdup(option->data);
465 option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
467 address = g_strdup(option->data);
470 c_address = __connman_ipconfig_get_local(ipconfig);
472 if (address != NULL &&
473 ((c_address != NULL &&
474 g_strcmp0(address, c_address) != 0) ||
475 (c_address == NULL))) {
478 /* Is this prefix part of the subnet we are suppose to use? */
479 prefix_len = check_ipv6_addr_prefix(dhcp->prefixes, address);
481 __connman_ipconfig_set_local(ipconfig, address);
482 __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);
484 DBG("new address %s/%d", address, prefix_len);
490 static int dhcpv6_release(struct connman_dhcpv6 *dhcp)
492 DBG("dhcp %p", dhcp);
494 if (dhcp->timeout > 0) {
495 g_source_remove(dhcp->timeout);
501 if (dhcp->dhcp_client == NULL)
504 g_dhcp_client_stop(dhcp->dhcp_client);
505 g_dhcp_client_unref(dhcp->dhcp_client);
507 dhcp->dhcp_client = NULL;
512 static void remove_network(gpointer user_data)
514 struct connman_dhcpv6 *dhcp = user_data;
516 DBG("dhcp %p", dhcp);
518 dhcpv6_release(dhcp);
523 static gboolean timeout_info_req(gpointer user_data)
525 struct connman_dhcpv6 *dhcp = user_data;
527 dhcp->RT = calc_delay(dhcp->RT, INF_MAX_RT);
529 DBG("info RT timeout %d msec", dhcp->RT);
531 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
533 g_dhcp_client_start(dhcp->dhcp_client, NULL);
538 static gboolean start_info_req(gpointer user_data)
540 struct connman_dhcpv6 *dhcp = user_data;
542 /* Set the retransmission timeout, RFC 3315 chapter 14 */
543 dhcp->RT = INF_TIMEOUT * (1 + get_random());
545 DBG("info initial RT timeout %d msec", dhcp->RT);
547 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_info_req, dhcp);
549 dhcpv6_info_request(dhcp);
554 int __connman_dhcpv6_start_info(struct connman_network *network,
557 struct connman_dhcpv6 *dhcp;
562 dhcp = g_try_new0(struct connman_dhcpv6, 1);
566 dhcp->network = network;
567 dhcp->callback = callback;
569 connman_network_ref(network);
571 g_hash_table_replace(network_table, network, dhcp);
573 /* Initial timeout, RFC 3315, 18.1.5 */
574 delay = rand() % 1000;
576 dhcp->timeout = g_timeout_add(delay, start_info_req, dhcp);
581 static void advertise_cb(GDHCPClient *dhcp_client, gpointer user_data)
583 struct connman_dhcpv6 *dhcp = user_data;
585 DBG("dhcpv6 advertise msg %p", dhcp);
588 static void solicitation_cb(GDHCPClient *dhcp_client, gpointer user_data)
590 /* We get here if server supports rapid commit */
591 struct connman_dhcpv6 *dhcp = user_data;
593 DBG("dhcpv6 solicitation msg %p", dhcp);
595 if (dhcp->timeout > 0) {
596 g_source_remove(dhcp->timeout);
600 set_addresses(dhcp_client, dhcp);
603 static gboolean timeout_solicitation(gpointer user_data)
605 struct connman_dhcpv6 *dhcp = user_data;
607 dhcp->RT = calc_delay(dhcp->RT, SOL_MAX_RT);
609 DBG("solicit RT timeout %d msec", dhcp->RT);
611 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
613 g_dhcp_client_start(dhcp->dhcp_client, NULL);
618 static int dhcpv6_solicitation(struct connman_dhcpv6 *dhcp)
620 struct connman_service *service;
621 struct connman_ipconfig *ipconfig_ipv6;
622 GDHCPClient *dhcp_client;
623 GDHCPClientError error;
626 DBG("dhcp %p", dhcp);
628 index = connman_network_get_index(dhcp->network);
630 dhcp_client = g_dhcp_client_new(G_DHCP_IPV6, index, &error);
631 if (error != G_DHCP_CLIENT_ERROR_NONE)
634 if (getenv("CONNMAN_DHCPV6_DEBUG"))
635 g_dhcp_client_set_debug(dhcp_client, dhcpv6_debug, "DHCPv6");
637 service = __connman_service_lookup_from_network(dhcp->network);
641 ret = set_duid(service, dhcp->network, dhcp_client, index);
645 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_CLIENTID);
646 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_RAPID_COMMIT);
647 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_DNS_SERVERS);
648 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_SNTP_SERVERS);
650 g_dhcpv6_client_set_oro(dhcp_client, 2, G_DHCPV6_DNS_SERVERS,
651 G_DHCPV6_SNTP_SERVERS);
653 ipconfig_ipv6 = __connman_service_get_ip6config(service);
654 dhcp->use_ta = __connman_ipconfig_ipv6_privacy_enabled(ipconfig_ipv6);
656 g_dhcpv6_client_set_ia(dhcp_client, index,
657 dhcp->use_ta == TRUE ? G_DHCPV6_IA_TA : G_DHCPV6_IA_NA,
660 clear_callbacks(dhcp_client);
662 g_dhcp_client_register_event(dhcp_client,
663 G_DHCP_CLIENT_EVENT_SOLICITATION,
664 solicitation_cb, dhcp);
666 g_dhcp_client_register_event(dhcp_client,
667 G_DHCP_CLIENT_EVENT_ADVERTISE,
670 dhcp->dhcp_client = dhcp_client;
672 return g_dhcp_client_start(dhcp_client, NULL);
675 static gboolean start_solicitation(gpointer user_data)
677 struct connman_dhcpv6 *dhcp = user_data;
679 /* Set the retransmission timeout, RFC 3315 chapter 14 */
680 dhcp->RT = SOL_TIMEOUT * (1 + get_random());
682 DBG("solicit initial RT timeout %d msec", dhcp->RT);
684 dhcp->timeout = g_timeout_add(dhcp->RT, timeout_solicitation, dhcp);
686 dhcpv6_solicitation(dhcp);
691 int __connman_dhcpv6_start(struct connman_network *network,
692 GSList *prefixes, dhcp_cb callback)
694 struct connman_dhcpv6 *dhcp;
699 dhcp = g_try_new0(struct connman_dhcpv6, 1);
703 dhcp->network = network;
704 dhcp->callback = callback;
705 dhcp->prefixes = prefixes;
707 connman_network_ref(network);
709 g_hash_table_replace(network_table, network, dhcp);
711 /* Initial timeout, RFC 3315, 17.1.2 */
712 delay = rand() % 1000;
714 dhcp->timeout = g_timeout_add(delay, start_solicitation, dhcp);
719 void __connman_dhcpv6_stop(struct connman_network *network)
723 if (network_table == NULL)
726 if (g_hash_table_remove(network_table, network) == TRUE)
727 connman_network_unref(network);
730 int __connman_dhcpv6_init(void)
736 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
737 NULL, remove_network);
742 void __connman_dhcpv6_cleanup(void)
746 g_hash_table_destroy(network_table);
747 network_table = NULL;