This adds support to generate a basic resolv.conf in /run/systemd/network.
This file will not take any effect unless a symlink is created from
/etc/resolv.conf.
Nameservers received over DHCP takes precedence over statically configured ones.
Note: /etc/resolv.conf is severely limited, so in the future we will likely
rather provide a much more powerfull nss plugin (or something to that effect),
but this should allow current users to function without any loss of
functionality.
libsystemd-id128-internal.la \
libsystemd-rtnl.la \
libsystemd-dhcp.la \
+ libsystemd-label.la \
libsystemd-shared.la
nodist_systemunit_DATA += \
libsystemd-daemon-internal.la \
libsystemd-rtnl.la \
libsystemd-dhcp.la \
+ libsystemd-label.la \
libsystemd-shared.la
tests += \
return 0;
}
+int config_parse_dns(const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+ Address **dns = data;
+ _cleanup_address_free_ Address *n = NULL;
+ int r;
+
+ assert(filename);
+ assert(section);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ r = address_new_dynamic(&n);
+ if (r < 0)
+ return r;
+
+ r = net_parse_inaddr(rvalue, &n->family, &n->in_addr);
+ if (r < 0) {
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+ "DNS address is invalid, ignoring assignment: %s", rvalue);
+ return 0;
+ }
+
+ *dns = n;
+ n = NULL;
+
+ return 0;
+}
+
int config_parse_address(const char *unit,
const char *filename,
unsigned line,
Network.DHCP, config_parse_bool, 0, offsetof(Network, dhcp)
Network.Address, config_parse_address, 0, 0
Network.Gateway, config_parse_gateway, 0, 0
+Network.DNS, config_parse_dns, 0, offsetof(Network, dns)
Address.Address, config_parse_address, 0, 0
Address.Label, config_parse_label, 0, 0
Route.Gateway, config_parse_gateway, 0, 0
if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_IP_ACQUIRE) {
_cleanup_address_free_ Address *addr = NULL;
_cleanup_route_free_ Route *rt = NULL;
+ struct in_addr **nameservers;
log_struct_link(LOG_INFO, link,
"MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
addr = NULL;
rt = NULL;
+ r = sd_dhcp_client_get_dns(client, &nameservers);
+ if (r >= 0) {
+ r = manager_update_resolv_conf(link->manager);
+ if (r < 0)
+ log_error("Failed to update resolv.conf");
+ }
+
link_enter_set_addresses(link);
}
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include <resolv.h>
+
#include "path-util.h"
#include "networkd.h"
#include "libudev-private.h"
#include "udev-util.h"
+#include "mkdir.h"
const char* const network_dirs[] = {
"/etc/systemd/network",
return 0;
}
+
+static void append_dns(FILE *f, struct in_addr *dns, unsigned char family, unsigned *count) {
+ char buf[INET6_ADDRSTRLEN];
+ const char *address;
+
+ address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN);
+ if (!address) {
+ log_warning("Invalid DNS address. Ignoring.");
+ return;
+ }
+
+ if (*count == MAXNS)
+ fputs("# Too many dynamic name servers configured, the "
+ "following entries will be ignored\n", f);
+
+ fprintf(f, "nameserver %s\n", address);
+
+ (*count) ++;
+}
+
+int manager_update_resolv_conf(Manager *m) {
+ _cleanup_free_ char *temp_path = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ Link *link;
+ Iterator i;
+ unsigned count = 0;
+ int r;
+
+ assert(m);
+
+ r = mkdir_safe_label("/run/systemd/network", 0755, 0, 0);
+ if (r < 0)
+ return r;
+
+ r = fopen_temporary("/run/systemd/network/resolv.conf", &f, &temp_path);
+ if (r < 0)
+ return r;
+
+ fchmod(fileno(f), 0644);
+
+ fputs("# This file is managed by systemd-networkd(8). Do not edit.\n", f);
+
+ HASHMAP_FOREACH(link, m->links, i) {
+ if (link->dhcp) {
+ struct in_addr **nameservers;
+
+ r = sd_dhcp_client_get_dns(link->dhcp, &nameservers);
+ if (r >= 0) {
+ unsigned j;
+
+ for (j = 0; nameservers[j]; j++)
+ append_dns(f, nameservers[j], AF_INET, &count);
+ }
+ }
+ }
+
+ HASHMAP_FOREACH(link, m->links, i)
+ if (link->network && link->network->dns)
+ append_dns(f, &link->network->dns->in_addr.in,
+ link->network->dns->family, &count);
+
+ fflush(f);
+
+ if (ferror(f) || rename(temp_path, "/run/systemd/network/resolv.conf") < 0) {
+ r = -errno;
+ unlink("/run/systemd/network/resolv.conf");
+ unlink(temp_path);
+ return r;
+ }
+
+ return 0;
+}
free(network->description);
+ address_free(network->dns);
+
while ((route = network->static_routes))
route_free(route);
if (r < 0)
return r;
+ if (network->dns) {
+ r = manager_update_resolv_conf(manager);
+ if (r < 0)
+ return r;
+ }
+
return 0;
}
if (r < 0)
goto out;
+
+ /* write out empty resolv.conf to avoid a
+ * dangling symlink */
+ r = manager_update_resolv_conf(m);
+ if (r < 0)
+ goto out;
+
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
LIST_HEAD(Address, static_addresses);
LIST_HEAD(Route, static_routes);
+ Address *dns;
Hashmap *addresses_by_section;
Hashmap *routes_by_section;
Route *dhcp_route;
Address *dhcp_address;
+ Address *dns;
LinkState state;
int manager_rtnl_listen(Manager *m);
+int manager_update_resolv_conf(Manager *m);
+
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
#define _cleanup_manager_free_ _cleanup_(manager_freep)
DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
#define _cleanup_address_free_ _cleanup_(address_freep)
+int config_parse_dns(const char *unit, const char *filename, unsigned line,
+ const char *section, unsigned section_line, const char *lvalue,
+ int ltype, const char *rvalue, void *data, void *userdata);
+
int config_parse_address(const char *unit, const char *filename, unsigned line,
const char *section, unsigned section_line, const char *lvalue,
int ltype, const char *rvalue, void *data, void *userdata);