]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: introduce link_should_mark_config() 36414/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 17 Feb 2025 17:11:17 +0000 (02:11 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 18 Feb 2025 14:13:51 +0000 (23:13 +0900)
This split-out common logic from link_drop_routes() and friends.

This is mostly a refactoring, and not change behavior in most cases.
But slightly change behavior for how foreign nexthops and routing
policy rules are managed.

E.g. when KeepConfiguration=static, previously all foreign nexthops and
routing policy rules were kept, but now only foreign nexthops and rules
with RTPROT_STATIC are kept and others are dropped.

Similary, when KeepConfiguration=dynamic, previously all foreign nexthops
and rules were removed, but now foreign configs with a dynamic protocol
e.g. RTPROT_DHCP are kept, and still configs with RTPROT_STATIC are
dropped.

Currently, we do not set/get/manage protocol for neighbor entries.
Hence, the logic of managing foreign neighbor entries is unchanged.

src/network/networkd-neighbor.c
src/network/networkd-nexthop.c
src/network/networkd-route.c
src/network/networkd-routing-policy-rule.c
src/network/networkd-util.c
src/network/networkd-util.h

index c953f89daa6c87cbf19a1e566f1d5b11822e1319..d66c04caa5d68a0c6b68729200cdc723a4c90a4a 100644 (file)
@@ -489,9 +489,7 @@ int link_drop_unmanaged_neighbors(Link *link) {
                 if (!neighbor_exists(neighbor))
                         continue;
 
-                /* Ignore foreign neighbors when KeepConfiguration=yes or static. */
-                if (neighbor->source == NETWORK_CONFIG_SOURCE_FOREIGN &&
-                    FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
+                if (!link_should_mark_config(link, /* only_static = */ false, neighbor->source, RTPROT_STATIC))
                         continue;
 
                 neighbor_mark(neighbor);
index c962e882d86df50c55349c2deef6364745c8cb31..d61ff6f4f28ff52f89964e210c323231dbb55ce1 100644 (file)
@@ -924,17 +924,8 @@ int link_drop_nexthops(Link *link, bool only_static) {
                 if (!nexthop_exists(nexthop))
                         continue;
 
-                if (nexthop->source == NETWORK_CONFIG_SOURCE_FOREIGN) {
-                        if (only_static)
-                                continue;
-
-                        /* Do not mark foreign nexthop when KeepConfiguration= is enabled. */
-                        if (link->network &&
-                            FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
-                                continue;
-
-                } else if (nexthop->source != NETWORK_CONFIG_SOURCE_STATIC)
-                        continue; /* Ignore dynamically configurad nexthops. */
+                if (!link_should_mark_config(link, only_static, nexthop->source, nexthop->protocol))
+                        continue;
 
                 /* Ignore nexthops bound to other links. */
                 if (nexthop->ifindex > 0 && nexthop->ifindex != link->ifindex)
index 9a05bf7c5cdabd2f1a564cb12de0af57f2ebbf9f..9498fa4ccc8f2c82ae01312a2af4a73a73a4a886 100644 (file)
@@ -1476,27 +1476,8 @@ int link_drop_routes(Link *link, bool only_static) {
                 if (!route_exists(route))
                         continue;
 
-                if (only_static) {
-                        if (route->source != NETWORK_CONFIG_SOURCE_STATIC)
-                                continue;
-                } else {
-                        /* Ignore dynamically assigned routes. */
-                        if (!IN_SET(route->source, NETWORK_CONFIG_SOURCE_FOREIGN, NETWORK_CONFIG_SOURCE_STATIC))
-                                continue;
-
-                        if (route->source == NETWORK_CONFIG_SOURCE_FOREIGN && link->network) {
-                                if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_YES))
-                                        continue;
-
-                                if (route->protocol == RTPROT_STATIC &&
-                                    FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
-                                        continue;
-
-                                if (IN_SET(route->protocol, RTPROT_DHCP, RTPROT_RA, RTPROT_REDIRECT) &&
-                                    FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC))
-                                        continue;
-                        }
-                }
+                if (!link_should_mark_config(link, only_static, route->source, route->protocol))
+                        continue;
 
                 /* When we also mark foreign routes, do not mark routes assigned to other interfaces.
                  * Otherwise, routes assigned to unmanaged interfaces will be dropped.
index 2cac730a4170a0ab9454137a07e7cbcfc41620e6..7eb7548412d5fc394ad48693db03c60b19628f4d 100644 (file)
@@ -822,22 +822,13 @@ int link_drop_routing_policy_rules(Link *link, bool only_static) {
                 if (rule->protocol == RTPROT_KERNEL)
                         continue;
 
-                if (only_static) {
-                        /* When 'only_static' is true, mark only static rules. */
-                        if (rule->source != NETWORK_CONFIG_SOURCE_STATIC)
-                                continue;
-                } else {
-                        /* Do not mark foreign rules when KeepConfiguration= is enabled. */
-                        if (rule->source == NETWORK_CONFIG_SOURCE_FOREIGN &&
-                            link->network &&
-                            FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
-                                continue;
-                }
-
                 /* Ignore rules not assigned yet or already removing. */
                 if (!routing_policy_rule_exists(rule))
                         continue;
 
+                if (!link_should_mark_config(link, only_static, rule->source, rule->protocol))
+                        continue;
+
                 routing_policy_rule_mark(rule);
         }
 
index 5db89c604722124fee93f18b7a8a0f54c44251af..80cce4dcdc9964f3a1e8f583b5ba2a0cb3473eb7 100644 (file)
@@ -6,6 +6,7 @@
 #include "escape.h"
 #include "logarithm.h"
 #include "networkd-link.h"
+#include "networkd-network.h"
 #include "networkd-util.h"
 #include "parse-util.h"
 #include "string-table.h"
@@ -113,6 +114,42 @@ DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_deprecated_address_family, AddressFa
 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(ip_masquerade_address_family, AddressFamily);
 DEFINE_STRING_TABLE_LOOKUP(dhcp_lease_server_type, sd_dhcp_lease_server_type_t);
 
+bool link_should_mark_config(Link *link, bool only_static, NetworkConfigSource source, uint8_t protocol) {
+        /* Always mark static configs. */
+        if (source == NETWORK_CONFIG_SOURCE_STATIC)
+                return true;
+
+        /* When 'only_static' is true, do not mark other configs. */
+        if (only_static)
+                return false;
+
+        /* Always ignore dynamically assigned configs. */
+        if (source != NETWORK_CONFIG_SOURCE_FOREIGN)
+                return false;
+
+        /* When only_static is false, the logic is conditionalized with KeepConfiguration=. Hence, the
+         * interface needs to have a matching .network file. */
+        assert(link);
+        assert(link->network);
+
+        /* When KeepConfiguration=yes, keep all foreign configs. */
+        if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_YES))
+                return false;
+
+        /* When static, keep all static configs. */
+        if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC) &&
+            protocol == RTPROT_STATIC)
+                return false;
+
+        /* When dynamic, keep all dynamic configs. */
+        if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC) &&
+            IN_SET(protocol, RTPROT_DHCP, RTPROT_RA, RTPROT_REDIRECT))
+                return false;
+
+        /* Otherwise, mark the config. */
+        return true;
+}
+
 int config_parse_ip_masquerade(
                 const char *unit,
                 const char *filename,
index b424092ef643a92aa30f8343908081a8cbc745d9..2a3b5683fad28f0cf38895c6ea7510cb8e6fce5c 100644 (file)
@@ -151,6 +151,8 @@ AddressFamily dhcp_deprecated_address_family_from_string(const char *s) _pure_;
 const char* dhcp_lease_server_type_to_string(sd_dhcp_lease_server_type_t t) _const_;
 sd_dhcp_lease_server_type_t dhcp_lease_server_type_from_string(const char *s) _pure_;
 
+bool link_should_mark_config(Link *link, bool only_static, NetworkConfigSource source, uint8_t protocol);
+
 int log_link_message_full_errno(Link *link, sd_netlink_message *m, int level, int err, const char *msg);
 #define log_link_message_error_errno(link, m, err, msg)   log_link_message_full_errno(link, m, LOG_ERR, err, msg)
 #define log_link_message_warning_errno(link, m, err, msg) log_link_message_full_errno(link, m, LOG_WARNING, err, msg)