diff options
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/networkd-address.c | 37 | ||||
-rw-r--r-- | src/network/networkd-gperf.gperf | 1 | ||||
-rw-r--r-- | src/network/networkd-link.c | 8 | ||||
-rw-r--r-- | src/network/networkd-manager.c | 75 | ||||
-rw-r--r-- | src/network/networkd-network.c | 8 | ||||
-rw-r--r-- | src/network/networkd.c | 7 | ||||
-rw-r--r-- | src/network/networkd.h | 8 |
7 files changed, 144 insertions, 0 deletions
diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c index 8a71630051..7d06cf8877 100644 --- a/src/network/networkd-address.c +++ b/src/network/networkd-address.c @@ -190,6 +190,43 @@ int address_configure(Address *address, Link *link, 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, diff --git a/src/network/networkd-gperf.gperf b/src/network/networkd-gperf.gperf index f1594d56c9..92954a8365 100644 --- a/src/network/networkd-gperf.gperf +++ b/src/network/networkd-gperf.gperf @@ -25,6 +25,7 @@ Network.Bridge, config_parse_bridge, 0, offsetof(Networ 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 diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 25d92c8aa4..5dd8e91007 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -378,6 +378,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { 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", @@ -420,6 +421,13 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { 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); } diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index c9ce1d6a13..f02eed1a59 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -19,10 +19,13 @@ 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", @@ -276,3 +279,75 @@ int manager_rtnl_listen(Manager *m) { 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; +} diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index f2546440cf..f987310011 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -144,6 +144,8 @@ void network_free(Network *network) { free(network->description); + address_free(network->dns); + while ((route = network->static_routes)) route_free(route); @@ -197,6 +199,12 @@ int network_apply(Manager *manager, Network *network, Link *link) { if (r < 0) return r; + if (network->dns) { + r = manager_update_resolv_conf(manager); + if (r < 0) + return r; + } + return 0; } diff --git a/src/network/networkd.c b/src/network/networkd.c index 360afba986..055200cb00 100644 --- a/src/network/networkd.c +++ b/src/network/networkd.c @@ -60,6 +60,13 @@ int main(int argc, char *argv[]) { 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..."); diff --git a/src/network/networkd.h b/src/network/networkd.h index 55181f2bbc..607feba0e7 100644 --- a/src/network/networkd.h +++ b/src/network/networkd.h @@ -88,6 +88,7 @@ struct Network { LIST_HEAD(Address, static_addresses); LIST_HEAD(Route, static_routes); + Address *dns; Hashmap *addresses_by_section; Hashmap *routes_by_section; @@ -157,6 +158,7 @@ struct Link { Route *dhcp_route; Address *dhcp_address; + Address *dns; LinkState state; @@ -195,6 +197,8 @@ int manager_udev_listen(Manager *m); 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) @@ -258,6 +262,10 @@ int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callbac 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); |