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.
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);
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)
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.
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);
}
#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"
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,
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)