chiark / gitweb /
networkd: DHCPv4 - allow opting out of using DNS servers
[elogind.git] / src / network / networkd-network.c
index 80c952a55b74990f5fd372d6b892dd6bdcccc4ab..498dea65fe8d76f229d1c5544e4e3d4e64011e84 100644 (file)
 #include "path-util.h"
 #include "conf-files.h"
 #include "conf-parser.h"
+#include "util.h"
 
 static int network_load_one(Manager *manager, const char *filename) {
         _cleanup_network_free_ Network *network = NULL;
         _cleanup_fclose_ FILE *file = NULL;
+        Route *route;
+        Address *address;
         int r;
 
         assert(manager);
@@ -47,8 +50,8 @@ static int network_load_one(Manager *manager, const char *filename) {
 
         network->manager = manager;
 
-        LIST_HEAD_INIT(network->addresses);
-        LIST_HEAD_INIT(network->routes);
+        LIST_HEAD_INIT(network->static_addresses);
+        LIST_HEAD_INIT(network->static_routes);
 
         network->addresses_by_section = hashmap_new(uint64_hash_func, uint64_compare_func);
         if (!network->addresses_by_section)
@@ -62,15 +65,39 @@ static int network_load_one(Manager *manager, const char *filename) {
         if (!network->filename)
                 return log_oom();
 
-        r = config_parse(NULL, filename, file, "Match\0Network\0Address\0Route\0", config_item_perf_lookup,
+        network->dhcp_dns = true;
+
+        r = config_parse(NULL, filename, file, "Match\0Network\0Address\0Route\0DHCPv4\0", config_item_perf_lookup,
                         (void*) network_gperf_lookup, false, false, network);
         if (r < 0) {
                 log_warning("Could not parse config file %s: %s", filename, strerror(-r));
                 return r;
-        } else
-                log_debug("Parsed configuration file %s", filename);
+        }
 
         LIST_PREPEND(networks, manager->networks, network);
+
+        LIST_FOREACH(static_routes, route, network->static_routes) {
+                if (!route->family) {
+                        log_warning("Route section without Gateway field configured in %s. "
+                                    "Ignoring", filename);
+                        return 0;
+                }
+
+                if (route->dst_family && route->family != route->dst_family) {
+                        log_warning("Route section with conflicting Gateway and Destination address "
+                                    "family configured in %s. Ignoring", filename);
+                        return 0;
+                }
+        }
+
+        LIST_FOREACH(static_addresses, address, network->static_addresses) {
+                if (!address->family) {
+                        log_warning("Address section without Address field configured in %s. "
+                                    "Ignoring", filename);
+                        return 0;
+                }
+        }
+
         network = NULL;
 
         return 0;
@@ -78,7 +105,8 @@ static int network_load_one(Manager *manager, const char *filename) {
 
 int network_load(Manager *manager) {
         Network *network;
-        char **files, **f;
+        _cleanup_strv_free_ char **files = NULL;
+        char **f;
         int r;
 
         assert(manager);
@@ -86,9 +114,9 @@ int network_load(Manager *manager) {
         while ((network = manager->networks))
                 network_free(network);
 
-        r = conf_files_list_strv(&files, ".network", NULL, (const char **)manager->network_dirs);
+        r = conf_files_list_strv(&files, ".network", NULL, network_dirs);
         if (r < 0) {
-                log_error("failed to enumerate network files: %s", strerror(-r));
+                log_error("Failed to enumerate network files: %s", strerror(-r));
                 return r;
         }
 
@@ -98,8 +126,6 @@ int network_load(Manager *manager) {
                         return r;
         }
 
-        strv_free(files);
-
         return 0;
 }
 
@@ -120,16 +146,19 @@ void network_free(Network *network) {
 
         free(network->description);
 
-        while ((route = network->routes))
+        address_free(network->dns);
+
+        while ((route = network->static_routes))
                 route_free(route);
 
-        while ((address = network->addresses))
+        while ((address = network->static_addresses))
                 address_free(address);
 
         hashmap_free(network->addresses_by_section);
         hashmap_free(network->routes_by_section);
 
-        LIST_REMOVE(networks, network->manager->networks, network);
+        if (network->manager && network->manager->networks)
+                LIST_REMOVE(networks, network->manager->networks, network);
 
         free(network);
 }
@@ -141,9 +170,6 @@ int network_get(Manager *manager, struct udev_device *device, Network **ret) {
         assert(device);
         assert(ret);
 
-        if (manager_should_reload(manager))
-                manager_load_config(manager);
-
         LIST_FOREACH(networks, network, manager->networks) {
                 if (net_match_config(network->match_mac, network->match_path,
                                         network->match_driver, network->match_type,
@@ -153,9 +179,9 @@ int network_get(Manager *manager, struct udev_device *device, Network **ret) {
                                         udev_device_get_driver(device),
                                         udev_device_get_devtype(device),
                                         udev_device_get_sysname(device))) {
-                        log_debug("Network file %s applies to link %s",
-                                        network->filename,
-                                        udev_device_get_sysname(device));
+                        log_debug("%s: found matching network '%s'",
+                                        udev_device_get_sysname(device),
+                                        network->filename);
                         *ret = network;
                         return 0;
                 }
@@ -169,15 +195,18 @@ int network_get(Manager *manager, struct udev_device *device, Network **ret) {
 int network_apply(Manager *manager, Network *network, Link *link) {
         int r;
 
-        log_info("Network '%s' being applied to link '%s'",
-                        network->description, link->ifname);
-
         link->network = network;
 
         r = link_configure(link);
         if (r < 0)
                 return r;
 
+        if (network->dns) {
+                r = manager_update_resolv_conf(manager);
+                if (r < 0)
+                        return r;
+        }
+
         return 0;
 }