From 18c9e9acd5d39a162ebe75efe31463d1497dd39f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 22 Aug 2024 15:01:12 +0900 Subject: [PATCH 1/4] network/routing-policy-rule: sort conf-parser prototypes --- src/network/networkd-routing-policy-rule.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/network/networkd-routing-policy-rule.h b/src/network/networkd-routing-policy-rule.h index c7a0512b44..6882afc3e7 100644 --- a/src/network/networkd-routing-policy-rule.h +++ b/src/network/networkd-routing-policy-rule.h @@ -77,19 +77,19 @@ void link_foreignize_routing_policy_rules(Link *link); DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(RoutingPolicyRule, routing_policy_rule); -CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_tos); -CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_table); -CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_fwmark_mask); -CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_prefix); -CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_priority); -CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_goto); CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_device); +CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_family); +CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_fwmark_mask); +CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_goto); +CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_invert); +CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_ip_protocol); CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_l3mdev); CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_port_range); -CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_ip_protocol); -CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_invert); -CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_family); -CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_uid_range); -CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_suppress_prefixlen); +CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_prefix); +CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_priority); CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_suppress_ifgroup); +CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_suppress_prefixlen); +CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_table); +CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_tos); CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_type); +CONFIG_PARSER_PROTOTYPE(config_parse_routing_policy_rule_uid_range); From 21719d67c1adf3a7f51d812caa235802dd9260b4 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 22 Aug 2024 16:17:32 +0900 Subject: [PATCH 2/4] network/routing-policy-rule: add more assertions --- src/network/networkd-routing-policy-rule.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/network/networkd-routing-policy-rule.c b/src/network/networkd-routing-policy-rule.c index ea3cb45868..75019fabef 100644 --- a/src/network/networkd-routing-policy-rule.c +++ b/src/network/networkd-routing-policy-rule.c @@ -993,6 +993,9 @@ static int link_request_routing_policy_rule(Link *link, const RoutingPolicyRule static int link_request_static_routing_policy_rule(Link *link, const RoutingPolicyRule *rule) { int r; + assert(link); + assert(rule); + if (IN_SET(rule->family, AF_INET, AF_INET6)) return link_request_routing_policy_rule(link, rule, rule->family); From 361f70f707fb6d0fc0fee49c37c6f753b25b29f6 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 23 Aug 2024 03:44:39 +0900 Subject: [PATCH 3/4] network/routing-policy-rule: read FRA_PROTOCOL afer reading other properties No functional change, just refactoring. --- src/network/networkd-routing-policy-rule.c | 24 ++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/network/networkd-routing-policy-rule.c b/src/network/networkd-routing-policy-rule.c index 75019fabef..d752c427dd 100644 --- a/src/network/networkd-routing-policy-rule.c +++ b/src/network/networkd-routing-policy-rule.c @@ -1060,7 +1060,6 @@ static bool routing_policy_rule_is_created_by_kernel(const RoutingPolicyRule *ru int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) { _cleanup_(routing_policy_rule_unrefp) RoutingPolicyRule *tmp = NULL; - bool adjust_protocol = false, is_new = false; RoutingPolicyRule *rule = NULL; Request *req = NULL; uint16_t type; @@ -1182,17 +1181,6 @@ int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, Man return 0; } - r = sd_netlink_message_read_u8(message, FRA_PROTOCOL, &tmp->protocol); - if (r == -ENODATA) - /* If FRA_PROTOCOL is supported by kernel, then the attribute is always appended. - * When the received message does not have FRA_PROTOCOL, then we need to adjust the - * protocol of the rule later. */ - adjust_protocol = true; - else if (r < 0) { - log_warning_errno(r, "rtnl: could not get FRA_PROTOCOL attribute, ignoring: %m"); - return 0; - } - uint8_t l3mdev = 0; r = sd_netlink_message_read_u8(message, FRA_L3MDEV, &l3mdev); if (r < 0 && r != -ENODATA) { @@ -1251,10 +1239,19 @@ int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, Man return 0; } - if (adjust_protocol) + /* If FRA_PROTOCOL is supported by kernel, then the attribute is always appended. If the received + * message does not have FRA_PROTOCOL, then we need to adjust the protocol of the rule. That requires + * all properties compared in the routing_policy_rule_compare_func(), hence it must be done after + * reading them. */ + r = sd_netlink_message_read_u8(message, FRA_PROTOCOL, &tmp->protocol); + if (r == -ENODATA) /* As .network files does not have setting to specify protocol, we can assume the * protocol of the received rule is RTPROT_KERNEL or RTPROT_STATIC. */ tmp->protocol = routing_policy_rule_is_created_by_kernel(tmp) ? RTPROT_KERNEL : RTPROT_STATIC; + else if (r < 0) { + log_warning_errno(r, "rtnl: could not get FRA_PROTOCOL attribute, ignoring: %m"); + return 0; + } (void) routing_policy_rule_get(m, tmp, tmp->family, &rule); (void) routing_policy_rule_get_request(m, tmp, tmp->family, &req); @@ -1273,6 +1270,7 @@ int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, Man return 0; } + bool is_new = false; if (!rule) { if (!req && !m->manage_foreign_rules) { routing_policy_rule_enter_configured(tmp); From 5dc74c6667b0ddcb86b798d6dffc4f991acffbad Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 23 Aug 2024 04:30:02 +0900 Subject: [PATCH 4/4] test-network: check one more rule we configure --- test/test-network/systemd-networkd-tests.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index 180db8bde9..04ababe827 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -3225,6 +3225,10 @@ class NetworkdNetworkTests(unittest.TestCase, Utilities): print(output) self.assertIn('101: from all iif test1 lookup 9', output) + output = check_output('ip -6 rule list iif test1 priority 101') + print(output) + self.assertIn('101: from all iif test1 lookup 9', output) + output = check_output('ip rule list iif test1 priority 102') print(output) self.assertIn('102: from 0.0.0.0/8 iif test1 lookup 10', output)