5 * Copyright (C) 2007-2013 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
38 * Just to avoid build failure due to missing STATEDIR
44 #define STATEDIR "/etc"
47 #define RESOLV_CONF_STATEDIR STATEDIR"/resolv.conf"
48 #define RESOLV_CONF_ETC "/etc/resolv.conf"
50 #define RESOLVER_FLAG_PUBLIC (1 << 0)
53 * Threshold for RDNSS lifetime. Will be used to trigger RS
54 * before RDNSS entries actually expire
56 #define RESOLVER_LIFETIME_REFRESH_THRESHOLD 0.8
64 unsigned int lifetime;
68 static GSList *entry_list = NULL;
69 static bool dnsproxy_enabled = false;
71 struct resolvfile_entry {
77 static GList *resolvfile_list = NULL;
79 static void resolvfile_remove_entries(GList *entries)
83 for (list = entries; list; list = list->next) {
84 struct resolvfile_entry *entry = list->data;
86 resolvfile_list = g_list_remove(resolvfile_list, entry);
88 g_free(entry->server);
89 g_free(entry->domain);
96 static bool already_exported(GList *export_list, const char *str)
100 for (list = export_list; list; list = g_list_next(list)) {
101 const char *str0 = list->data;
102 if (g_strcmp0(str0, str) == 0)
109 static int resolvfile_export(void)
111 GList *list, *export_list;
117 content = g_string_new("# Generated by Connection Manager\n");
120 * Domains and nameservers are added in reverse so that the most
121 * recently appended entry is the primary one. No more than
122 * MAXDNSRCH/MAXNS entries are used.
126 for (count = 0, list = g_list_first(resolvfile_list);
127 list && (count < MAXDNSRCH);
128 list = g_list_next(list)) {
129 struct resolvfile_entry *entry = list->data;
134 if (already_exported(export_list, entry->domain))
138 g_string_append_printf(content, "search ");
140 g_string_append_printf(content, "%s ", entry->domain);
142 export_list = g_list_append(export_list, entry->domain);
146 g_list_free(export_list);
150 g_string_append_printf(content, "\n");
153 for (count = 0, list = g_list_first(resolvfile_list);
154 list && (count < MAXNS);
155 list = g_list_next(list)) {
156 struct resolvfile_entry *entry = list->data;
161 if (already_exported(export_list, entry->server))
164 g_string_append_printf(content, "nameserver %s\n", entry->server);
166 export_list = g_list_append(export_list, entry->server);
170 g_list_free(export_list);
172 old_umask = umask(022);
174 fd = open(RESOLV_CONF_STATEDIR, O_RDWR | O_CREAT | O_CLOEXEC,
175 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
177 connman_warn_once("Cannot create "RESOLV_CONF_STATEDIR" "
178 "falling back to "RESOLV_CONF_ETC);
180 fd = open(RESOLV_CONF_ETC, O_RDWR | O_CREAT | O_CLOEXEC,
181 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
189 if (ftruncate(fd, 0) < 0) {
196 if (write(fd, content->str, content->len) < 0)
203 g_string_free(content, TRUE);
209 int __connman_resolvfile_append(int index, const char *domain,
212 struct resolvfile_entry *entry;
214 DBG("index %d domain %s server %s", index, domain, server);
219 entry = g_try_new0(struct resolvfile_entry, 1);
223 entry->index = index;
224 entry->domain = g_strdup(domain);
225 entry->server = g_strdup(server);
227 resolvfile_list = g_list_append(resolvfile_list, entry);
229 return resolvfile_export();
232 int __connman_resolvfile_remove(int index, const char *domain,
235 GList *list, *matches = NULL;
237 DBG("index %d domain %s server %s", index, domain, server);
239 for (list = resolvfile_list; list; list = g_list_next(list)) {
240 struct resolvfile_entry *entry = list->data;
242 if (index >= 0 && entry->index != index)
245 if (domain && g_strcmp0(entry->domain, domain) != 0)
248 if (g_strcmp0(entry->server, server) != 0)
251 matches = g_list_append(matches, entry);
254 resolvfile_remove_entries(matches);
256 return resolvfile_export();
259 void __connman_resolver_append_fallback_nameservers(void)
263 for (list = entry_list; list; list = list->next) {
264 struct entry_data *entry = list->data;
266 if (entry->index >= 0 && entry->server)
270 for (list = entry_list; list; list = list->next) {
271 struct entry_data *entry = list->data;
273 if (entry->index != -1 || !entry->server)
276 DBG("index %d server %s", entry->index, entry->server);
278 if (dnsproxy_enabled) {
279 __connman_dnsproxy_append(entry->index, entry->domain,
282 __connman_resolvfile_append(entry->index,
283 entry->domain, entry->server);
288 static void remove_fallback_nameservers(void)
292 for (list = entry_list; list; list = list->next) {
293 struct entry_data *entry = list->data;
295 if (entry->index >= 0 || !entry->server)
298 DBG("index %d server %s", entry->index, entry->server);
300 if (dnsproxy_enabled) {
301 __connman_dnsproxy_remove(entry->index, entry->domain,
304 __connman_resolvfile_remove(entry->index,
305 entry->domain, entry->server);
310 static void remove_entries(GSList *entries)
314 for (list = entries; list; list = list->next) {
315 struct entry_data *entry = list->data;
317 entry_list = g_slist_remove(entry_list, entry);
319 if (dnsproxy_enabled) {
320 __connman_dnsproxy_remove(entry->index, entry->domain,
323 __connman_resolvfile_remove(entry->index, entry->domain,
328 g_source_remove(entry->timeout);
329 g_free(entry->server);
330 g_free(entry->domain);
334 g_slist_free(entries);
336 __connman_resolver_append_fallback_nameservers();
339 static gboolean resolver_expire_cb(gpointer user_data)
341 struct entry_data *entry = user_data;
344 DBG("index %d domain %s server %s",
345 entry->index, entry->domain, entry->server);
347 list = g_slist_prepend(NULL, entry);
349 if (entry->index >= 0) {
350 struct connman_service *service;
351 service = __connman_service_lookup_from_index(entry->index);
353 #if defined TIZEN_EXT
354 __connman_service_nameserver_remove(service,
356 CONNMAN_IPCONFIG_TYPE_ALL);
358 __connman_service_nameserver_remove(service,
359 entry->server, true);
363 remove_entries(list);
368 static gboolean resolver_refresh_cb(gpointer user_data)
370 struct entry_data *entry = user_data;
371 unsigned int interval;
372 struct connman_service *service = NULL;
374 /* Round up what we have left from lifetime */
375 interval = entry->lifetime *
376 (1 - RESOLVER_LIFETIME_REFRESH_THRESHOLD) + 1.0;
378 DBG("RDNSS start index %d domain %s "
379 "server %s remaining lifetime %d",
380 entry->index, entry->domain,
381 entry->server, interval);
383 entry->timeout = g_timeout_add_seconds(interval,
384 resolver_expire_cb, entry);
386 if (entry->index >= 0) {
387 service = __connman_service_lookup_from_index(entry->index);
390 * Send Router Solicitation to refresh RDNSS entries
391 * before their lifetime expires
393 __connman_network_refresh_rs_ipv6(
394 __connman_service_get_network(service),
401 static int append_resolver(int index, const char *domain,
402 const char *server, unsigned int lifetime,
405 struct entry_data *entry;
406 unsigned int interval;
408 DBG("index %d domain %s server %s lifetime %d flags %d",
409 index, domain, server, lifetime, flags);
411 if (!server && !domain)
415 if (g_strcmp0(server, "0.0.0.0") == 0)
419 entry = g_try_new0(struct entry_data, 1);
423 entry->index = index;
424 entry->domain = g_strdup(domain);
425 entry->server = g_strdup(server);
426 entry->flags = flags;
427 entry->lifetime = lifetime;
430 entry->family = connman_inet_check_ipaddress(server);
433 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
435 DBG("RDNSS start index %d domain %s "
436 "server %s lifetime threshold %d",
437 index, domain, server, interval);
439 entry->timeout = g_timeout_add_seconds(interval,
440 resolver_refresh_cb, entry);
443 if (entry->index >= 0 && entry->server)
444 remove_fallback_nameservers();
446 entry_list = g_slist_append(entry_list, entry);
448 if (dnsproxy_enabled)
449 __connman_dnsproxy_append(entry->index, domain, server);
451 __connman_resolvfile_append(entry->index, domain, server);
454 * We update the service only for those nameservers
455 * that are automagically added via netlink (lifetime > 0)
457 if (server && entry->index >= 0 && lifetime) {
458 struct connman_service *service;
459 service = __connman_service_lookup_from_index(entry->index);
461 #if defined TIZEN_EXT
462 __connman_service_nameserver_append(service,
464 CONNMAN_IPCONFIG_TYPE_ALL);
466 __connman_service_nameserver_append(service,
475 * connman_resolver_append:
476 * @index: network interface index
477 * @domain: domain limitation
478 * @server: server address
480 * Append resolver server address to current list
482 int connman_resolver_append(int index, const char *domain,
487 DBG("index %d domain %s server %s", index, domain, server);
489 if (!server && !domain)
492 for (list = entry_list; list; list = list->next) {
493 struct entry_data *entry = list->data;
495 if (entry->timeout > 0)
498 if (entry->index == index &&
499 g_strcmp0(entry->domain, domain) == 0 &&
500 g_strcmp0(entry->server, server) == 0) {
501 if (dnsproxy_enabled)
502 __connman_dnsproxy_append(entry->index, domain,
509 return append_resolver(index, domain, server, 0, 0);
513 * connman_resolver_append_lifetime:
514 * @index: network interface index
515 * @domain: domain limitation
516 * @server: server address
517 * @timeout: server lifetime in seconds
519 * Append resolver server address to current list
521 int connman_resolver_append_lifetime(int index, const char *domain,
522 const char *server, unsigned int lifetime)
525 unsigned int interval;
527 DBG("index %d domain %s server %s lifetime %d",
528 index, domain, server, lifetime);
530 if (!server && !domain)
533 for (list = entry_list; list; list = list->next) {
534 struct entry_data *entry = list->data;
536 if (entry->timeout == 0 ||
537 entry->index != index ||
538 g_strcmp0(entry->domain, domain) != 0 ||
539 g_strcmp0(entry->server, server) != 0)
542 g_source_remove(entry->timeout);
545 resolver_expire_cb(entry);
549 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
551 DBG("RDNSS start index %d domain %s "
552 "server %s lifetime threshold %d",
553 index, domain, server, interval);
555 entry->timeout = g_timeout_add_seconds(interval,
556 resolver_refresh_cb, entry);
560 return append_resolver(index, domain, server, lifetime, 0);
564 * connman_resolver_remove:
565 * @index: network interface index
566 * @domain: domain limitation
567 * @server: server address
569 * Remover resolver server address from current list
571 int connman_resolver_remove(int index, const char *domain, const char *server)
573 GSList *list, *matches = NULL;
575 DBG("index %d domain %s server %s", index, domain, server);
577 for (list = entry_list; list; list = list->next) {
578 struct entry_data *entry = list->data;
580 if (entry->index != index)
583 if (g_strcmp0(entry->domain, domain) != 0)
586 if (g_strcmp0(entry->server, server) != 0)
589 matches = g_slist_prepend(matches, entry);
596 remove_entries(matches);
602 * connman_resolver_remove_all:
603 * @index: network interface index
605 * Remove all resolver server address for the specified interface index
607 int connman_resolver_remove_all(int index)
609 GSList *list, *matches = NULL;
611 DBG("index %d", index);
616 for (list = entry_list; list; list = list->next) {
617 struct entry_data *entry = list->data;
619 if (entry->index != index)
622 matches = g_slist_prepend(matches, entry);
628 remove_entries(matches);
633 int __connman_resolver_redo_servers(int index)
637 if (!dnsproxy_enabled)
640 DBG("index %d", index);
645 for (list = entry_list; list; list = list->next) {
646 struct entry_data *entry = list->data;
648 if (entry->timeout == 0 || entry->index != index)
652 * This function must only check IPv6 server addresses so
653 * do not remove IPv4 name servers unnecessarily.
655 if (entry->family != AF_INET6)
659 * We remove the server, and then re-create so that it will
660 * use proper source addresses when sending DNS queries.
662 __connman_dnsproxy_remove(entry->index, entry->domain,
665 __connman_dnsproxy_append(entry->index, entry->domain,
670 * We want to re-add all search domains back to search
671 * domain lists as they just got removed for RDNSS IPv6-servers
673 * Removal of search domains is not necessary
674 * as there can be only one instance of each search domain
675 * in the each dns-servers search domain list.
678 for (list = entry_list; list; list = list->next) {
679 struct entry_data *entry = list->data;
681 if (entry->index != index)
687 __connman_dnsproxy_append(entry->index, entry->domain,
694 static void free_entry(gpointer data)
696 struct entry_data *entry = data;
697 g_free(entry->domain);
698 g_free(entry->server);
702 static void free_resolvfile(gpointer data)
704 struct resolvfile_entry *entry = data;
705 g_free(entry->domain);
706 g_free(entry->server);
710 int __connman_resolver_set_mdns(int index, bool enabled)
712 if (!dnsproxy_enabled)
715 return __connman_dnsproxy_set_mdns(index, enabled);
718 int __connman_resolver_init(gboolean dnsproxy)
723 DBG("dnsproxy %d", dnsproxy);
725 /* get autoip nameservers */
726 ns = __connman_inet_get_pnp_nameservers(NULL);
727 for (i = 0; ns && ns[i]; i += 1) {
728 DBG("pnp server %s", ns[i]);
729 append_resolver(i, NULL, ns[i], 86400, 0);
736 if (__connman_dnsproxy_init() < 0) {
737 /* Fall back to resolv.conf */
741 dnsproxy_enabled = true;
743 ns = connman_setting_get_string_list("FallbackNameservers");
744 for (i = 0; ns && ns[i]; i += 1) {
745 DBG("server %s", ns[i]);
746 append_resolver(-1, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
752 void __connman_resolver_cleanup(void)
756 if (dnsproxy_enabled)
757 __connman_dnsproxy_cleanup();
762 for (list = resolvfile_list; list; list = g_list_next(list))
763 free_resolvfile(list->data);
764 g_list_free(resolvfile_list);
765 resolvfile_list = NULL;
767 for (slist = entry_list; slist; slist = g_slist_next(slist))
768 free_entry(slist->data);
769 g_slist_free(entry_list);