]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/network/netdev/netdev.c
tree-wide: make sure net/if.h is included before any linux/ header
[thirdparty/systemd.git] / src / network / netdev / netdev.c
index 3d1a5e5b3107444ac8ea2a3332812b0652408749..229520dfb2b667bd0a8710a74ce1e90bed9c10e1 100644 (file)
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+/* Make sure the net/if.h header is included before any linux/ one */
 #include <net/if.h>
 #include <netinet/in.h>
 #include <linux/if_arp.h>
@@ -31,6 +32,7 @@
 #include "networkd-manager.h"
 #include "networkd-queue.h"
 #include "networkd-setlink.h"
+#include "networkd-sriov.h"
 #include "nlmon.h"
 #include "path-lookup.h"
 #include "siphash24.h"
@@ -128,7 +130,7 @@ static const char* const netdev_kind_table[_NETDEV_KIND_MAX] = {
         [NETDEV_KIND_VXCAN]     = "vxcan",
         [NETDEV_KIND_VXLAN]     = "vxlan",
         [NETDEV_KIND_WIREGUARD] = "wireguard",
-        [NETDEV_KIND_WLAN]      = "virtual-wlan",
+        [NETDEV_KIND_WLAN]      = "wlan",
         [NETDEV_KIND_XFRM]      = "xfrm",
 };
 
@@ -197,14 +199,6 @@ static void netdev_detach_from_manager(NetDev *netdev) {
 static NetDev *netdev_free(NetDev *netdev) {
         assert(netdev);
 
-        netdev_detach_from_manager(netdev);
-
-        free(netdev->filename);
-
-        free(netdev->description);
-        free(netdev->ifname);
-        condition_free_list(netdev->conditions);
-
         /* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that
          * because we parse .netdev files twice: once to determine the kind (with a short, minimal NetDev structure
          * allocation, with no room for per-kind fields), and once to read the kind's properties (with a full,
@@ -217,6 +211,13 @@ static NetDev *netdev_free(NetDev *netdev) {
             NETDEV_VTABLE(netdev)->done)
                 NETDEV_VTABLE(netdev)->done(netdev);
 
+        netdev_detach_from_manager(netdev);
+
+        condition_free_list(netdev->conditions);
+        free(netdev->filename);
+        free(netdev->description);
+        free(netdev->ifname);
+
         return mfree(netdev);
 }
 
@@ -236,6 +237,9 @@ void netdev_drop(NetDev *netdev) {
                 return;
         }
 
+        if (NETDEV_VTABLE(netdev) && NETDEV_VTABLE(netdev)->drop)
+                NETDEV_VTABLE(netdev)->drop(netdev);
+
         netdev->state = NETDEV_STATE_LINGER;
 
         log_netdev_debug(netdev, "netdev removed");
@@ -604,24 +608,39 @@ static int stacked_netdev_create(NetDev *netdev, Link *link, Request *req) {
         return 0;
 }
 
+static bool link_is_ready_to_create_stacked_netdev_one(Link *link, bool allow_unmanaged) {
+        assert(link);
+
+        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
+                return false;
+
+        if (!link->network)
+                return allow_unmanaged;
+
+        if (link->set_link_messages > 0)
+                return false;
+
+        /* If stacked netdevs are created before the underlying interface being activated, then
+         * the activation policy for the netdevs are ignored. See issue #22593. */
+        if (!link->activated)
+                return false;
+
+        return true;
+}
+
+static bool link_is_ready_to_create_stacked_netdev(Link *link) {
+        return check_ready_for_all_sr_iov_ports(link, /* allow_unmanaged = */ false,
+                                                link_is_ready_to_create_stacked_netdev_one);
+}
+
 static int netdev_is_ready_to_create(NetDev *netdev, Link *link) {
         assert(netdev);
 
         if (netdev->state != NETDEV_STATE_LOADING)
                 return false;
 
-        if (link) {
-                if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
-                        return false;
-
-                if (link->set_link_messages > 0)
-                        return false;
-
-                /* If stacked netdevs are created before the underlying interface being activated, then
-                 * the activation policy for the netdevs are ignored. See issue #22593. */
-                if (!link->activated)
-                        return false;
-        }
+        if (link && !link_is_ready_to_create_stacked_netdev(link))
+                return false;
 
         if (NETDEV_VTABLE(netdev)->is_ready_to_create)
                 return NETDEV_VTABLE(netdev)->is_ready_to_create(netdev, link);
@@ -683,7 +702,7 @@ int link_request_stacked_netdev(Link *link, NetDev *netdev) {
 
         link->stacked_netdevs_created = false;
         r = link_queue_request_full(link, REQUEST_TYPE_NETDEV_STACKED,
-                                    netdev_ref(netdev), (mfree_func_t) netdev_unref,
+                                    netdev, (mfree_func_t) netdev_unref,
                                     trivial_hash_func, trivial_compare_func,
                                     stacked_netdev_process_request,
                                     &link->create_stacked_netdev_messages,
@@ -691,9 +710,12 @@ int link_request_stacked_netdev(Link *link, NetDev *netdev) {
         if (r < 0)
                 return log_link_error_errno(link, r, "Failed to request stacked netdev '%s': %m",
                                             netdev->ifname);
+        if (r == 0)
+                return 0;
 
+        netdev_ref(netdev);
         log_link_debug(link, "Requested stacked netdev '%s'", netdev->ifname);
-        return 0;
+        return 1;
 }
 
 static int independent_netdev_process_request(Request *req, Link *link, void *userdata) {
@@ -717,6 +739,10 @@ static int netdev_request_to_create(NetDev *netdev) {
         int r;
 
         assert(netdev);
+        assert(netdev->manager);
+
+        if (netdev->manager->test_mode)
+                return 0;
 
         if (netdev_is_stacked(netdev))
                 return 0;
@@ -749,10 +775,8 @@ int netdev_load_one(Manager *manager, const char *filename) {
         assert(filename);
 
         r = null_or_empty_path(filename);
-        if (r == -ENOENT)
-                return 0;
         if (r < 0)
-                return r;
+                return log_warning_errno(r, "Failed to check if \"%s\" is empty: %m", filename);
         if (r > 0) {
                 log_debug("Skipping empty file: %s", filename);
                 return 0;
@@ -770,14 +794,15 @@ int netdev_load_one(Manager *manager, const char *filename) {
 
         dropin_dirname = strjoina(basename(filename), ".d");
         r = config_parse_many(
-                        STRV_MAKE_CONST(filename), NETWORK_DIRS, dropin_dirname,
+                        STRV_MAKE_CONST(filename), NETWORK_DIRS, dropin_dirname, /* root = */ NULL,
                         NETDEV_COMMON_SECTIONS NETDEV_OTHER_SECTIONS,
                         config_item_perf_lookup, network_netdev_gperf_lookup,
                         CONFIG_PARSE_WARN,
                         netdev_raw,
+                        NULL,
                         NULL);
         if (r < 0)
-                return r;
+                return r; /* config_parse_many() logs internally. */
 
         /* skip out early if configuration does not match the environment */
         if (!condition_test_list(netdev_raw->conditions, environ, NULL, NULL, NULL)) {
@@ -785,15 +810,11 @@ int netdev_load_one(Manager *manager, const char *filename) {
                 return 0;
         }
 
-        if (netdev_raw->kind == _NETDEV_KIND_INVALID) {
-                log_warning("NetDev has no Kind= configured in %s. Ignoring", filename);
-                return 0;
-        }
+        if (netdev_raw->kind == _NETDEV_KIND_INVALID)
+                return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "NetDev has no Kind= configured in \"%s\", ignoring.", filename);
 
-        if (!netdev_raw->ifname) {
-                log_warning("NetDev without Name= configured in %s. Ignoring", filename);
-                return 0;
-        }
+        if (!netdev_raw->ifname)
+                return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "NetDev without Name= configured in \"%s\", ignoring.", filename);
 
         netdev = malloc0(NETDEV_VTABLE(netdev_raw)->object_size);
         if (!netdev)
@@ -809,19 +830,19 @@ int netdev_load_one(Manager *manager, const char *filename) {
                 NETDEV_VTABLE(netdev)->init(netdev);
 
         r = config_parse_many(
-                        STRV_MAKE_CONST(filename), NETWORK_DIRS, dropin_dirname,
+                        STRV_MAKE_CONST(filename), NETWORK_DIRS, dropin_dirname, /* root = */ NULL,
                         NETDEV_VTABLE(netdev)->sections,
                         config_item_perf_lookup, network_netdev_gperf_lookup,
                         CONFIG_PARSE_WARN,
-                        netdev, NULL);
+                        netdev, NULL, NULL);
         if (r < 0)
-                return r;
+                return r; /* config_parse_many() logs internally. */
 
         /* verify configuration */
         if (NETDEV_VTABLE(netdev)->config_verify) {
                 r = NETDEV_VTABLE(netdev)->config_verify(netdev, filename);
                 if (r < 0)
-                        return 0;
+                        return r; /* config_verify() logs internally. */
         }
 
         netdev->filename = strdup(filename);
@@ -837,22 +858,21 @@ int netdev_load_one(Manager *manager, const char *filename) {
                 assert(n);
                 if (!streq(netdev->filename, n->filename))
                         log_netdev_warning_errno(netdev, r,
-                                                 "Device was already configured by file %s, ignoring %s.",
+                                                 "Device was already configured by \"%s\", ignoring %s.",
                                                  n->filename, netdev->filename);
 
                 /* Clear ifname before netdev_free() is called. Otherwise, the NetDev object 'n' is
                  * removed from the hashmap 'manager->netdevs'. */
                 netdev->ifname = mfree(netdev->ifname);
-                return 0;
+                return -EEXIST;
         }
-        if (r < 0)
-                return r;
+        assert(r > 0);
 
-        log_netdev_debug(netdev, "loaded %s", netdev_kind_to_string(netdev->kind));
+        log_netdev_debug(netdev, "loaded \"%s\"", netdev_kind_to_string(netdev->kind));
 
         r = netdev_request_to_create(netdev);
         if (r < 0)
-                return r;
+                return r; /* netdev_request_to_create() logs internally. */
 
         TAKE_PTR(netdev);
         return 0;
@@ -871,11 +891,8 @@ int netdev_load(Manager *manager, bool reload) {
         if (r < 0)
                 return log_error_errno(r, "Failed to enumerate netdev files: %m");
 
-        STRV_FOREACH(f, files) {
-                r = netdev_load_one(manager, *f);
-                if (r < 0)
-                        log_error_errno(r, "Failed to load %s, ignoring: %m", *f);
-        }
+        STRV_FOREACH(f, files)
+                (void) netdev_load_one(manager, *f);
 
         return 0;
 }
@@ -892,11 +909,10 @@ int config_parse_netdev_kind(
                 void *data,
                 void *userdata) {
 
-        NetDevKind k, *kind = data;
+        NetDevKind k, *kind = ASSERT_PTR(data);
 
         assert(filename);
         assert(rvalue);
-        assert(data);
 
         k = netdev_kind_from_string(rvalue);
         if (k < 0) {
@@ -928,10 +944,9 @@ int config_parse_netdev_hw_addr(
                 void *data,
                 void *userdata) {
 
-        struct hw_addr_data *hw_addr = data;
+        struct hw_addr_data *hw_addr = ASSERT_PTR(data);
 
         assert(rvalue);
-        assert(data);
 
         if (streq(rvalue, "none")) {
                 *hw_addr = HW_ADDR_NONE;