chiark / gitweb /
networkd: add minimal IP forwarding and masquerading support to .network files
[elogind.git] / src / network / networkd-network.c
index fc62395217fd5032a8ae8f331514a4ae8b08c9e6..d6504cc1787cfebe17307eae97d98b07cb656361 100644 (file)
 #include <ctype.h>
 #include <net/if.h>
 
-#include "networkd.h"
-#include "networkd-netdev.h"
-#include "networkd-link.h"
-#include "network-internal.h"
 #include "path-util.h"
 #include "conf-files.h"
 #include "conf-parser.h"
 #include "util.h"
+#include "networkd.h"
+#include "networkd-netdev.h"
+#include "networkd-link.h"
+#include "network-internal.h"
 
 static int network_load_one(Manager *manager, const char *filename) {
         _cleanup_network_free_ Network *network = NULL;
@@ -62,19 +62,24 @@ static int network_load_one(Manager *manager, const char *filename) {
 
         LIST_HEAD_INIT(network->static_addresses);
         LIST_HEAD_INIT(network->static_routes);
+        LIST_HEAD_INIT(network->static_fdb_entries);
 
-        network->stacked_netdevs = hashmap_new(string_hash_func, string_compare_func);
+        network->stacked_netdevs = hashmap_new(&string_hash_ops);
         if (!network->stacked_netdevs)
                 return log_oom();
 
-        network->addresses_by_section = hashmap_new(NULL, NULL);
+        network->addresses_by_section = hashmap_new(NULL);
         if (!network->addresses_by_section)
                 return log_oom();
 
-        network->routes_by_section = hashmap_new(NULL, NULL);
+        network->routes_by_section = hashmap_new(NULL);
         if (!network->routes_by_section)
                 return log_oom();
 
+        network->fdb_entries_by_section = hashmap_new(NULL);
+        if (!network->fdb_entries_by_section)
+                return log_oom();
+
         network->filename = strdup(filename);
         if (!network->filename)
                 return log_oom();
@@ -85,16 +90,29 @@ static int network_load_one(Manager *manager, const char *filename) {
         network->dhcp_hostname = true;
         network->dhcp_routes = true;
         network->dhcp_sendhost = true;
+        network->dhcp_route_metric = DHCP_ROUTE_METRIC;
 
         network->llmnr = LLMNR_SUPPORT_YES;
 
         r = config_parse(NULL, filename, file,
-                         "Match\0Network\0Address\0Route\0DHCP\0DHCPv4\0",
+                         "Match\0"
+                         "Link\0"
+                         "Network\0"
+                         "Address\0"
+                         "Route\0"
+                         "DHCP\0"
+                         "DHCPv4\0"
+                         "Bridge\0"
+                         "BridgeFDB\0",
                          config_item_perf_lookup, network_network_gperf_lookup,
                          false, false, true, network);
         if (r < 0)
                 return r;
 
+        /* IPMasquerade=yes implies IPForward=yes */
+        if (network->ip_masquerade)
+                network->ip_forward = true;
+
         LIST_PREPEND(networks, manager->networks, network);
 
         LIST_FOREACH(routes, route, network->static_routes) {
@@ -130,10 +148,8 @@ int network_load(Manager *manager) {
                 network_free(network);
 
         r = conf_files_list_strv(&files, ".network", NULL, network_dirs);
-        if (r < 0) {
-                log_error("Failed to enumerate network files: %s", strerror(-r));
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to enumerate network files: %m");
 
         STRV_FOREACH_BACKWARDS(f, files) {
                 r = network_load_one(manager, *f);
@@ -148,6 +164,7 @@ void network_free(Network *network) {
         NetDev *netdev;
         Route *route;
         Address *address;
+        FdbEntry *fdb_entry;
         Iterator i;
 
         if (!network)
@@ -164,6 +181,8 @@ void network_free(Network *network) {
         free(network->description);
         free(network->dhcp_vendor_class_identifier);
 
+        free(network->mac);
+
         strv_free(network->ntp);
         strv_free(network->dns);
         strv_free(network->domains);
@@ -184,8 +203,12 @@ void network_free(Network *network) {
         while ((address = network->static_addresses))
                 address_free(address);
 
+        while ((fdb_entry = network->static_fdb_entries))
+                fdb_entry_free(fdb_entry);
+
         hashmap_free(network->addresses_by_section);
         hashmap_free(network->routes_by_section);
+        hashmap_free(network->fdb_entries_by_section);
 
         if (network->manager && network->manager->networks)
                 LIST_REMOVE(networks, network->manager->networks, network);
@@ -218,8 +241,22 @@ int network_get(Manager *manager, struct udev_device *device,
                                      udev_device_get_property_value(device, "ID_NET_DRIVER"),
                                      udev_device_get_devtype(device),
                                      ifname)) {
-                        log_debug("%-*s: found matching network '%s'", IFNAMSIZ, ifname,
-                                  network->filename);
+                        if (network->match_name) {
+                                const char *attr;
+                                uint8_t name_assign_type = NET_NAME_UNKNOWN;
+
+                                attr = udev_device_get_sysattr_value(device, "name_assign_type");
+                                if (attr)
+                                        (void)safe_atou8(attr, &name_assign_type);
+
+                                if (name_assign_type == NET_NAME_ENUM)
+                                        log_warning("%-*s: found matching network '%s', based on potentially unpredictable ifname",
+                                                    IFNAMSIZ, ifname, network->filename);
+                                else
+                                        log_debug("%-*s: found matching network '%s'", IFNAMSIZ, ifname, network->filename);
+                        } else
+                                log_debug("%-*s: found matching network '%s'", IFNAMSIZ, ifname, network->filename);
+
                         *ret = network;
                         return 0;
                 }