]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/network/networkd-routing-policy-rule.c
Merge pull request #10920 from yuwata/hashmap-destructor
[thirdparty/systemd.git] / src / network / networkd-routing-policy-rule.c
index b809d825a3af92b3a4ed71dcf40535843099cb34..96013e7026ec1c2c710d896237157880a4289fb7 100644 (file)
@@ -6,11 +6,11 @@
 #include "alloc-util.h"
 #include "conf-parser.h"
 #include "fileio.h"
+#include "ip-protocol-list.h"
 #include "networkd-routing-policy-rule.h"
 #include "netlink-util.h"
 #include "networkd-manager.h"
 #include "parse-util.h"
-#include "socket-protocol-list.h"
 #include "socket-util.h"
 #include "string-util.h"
 #include "strv.h"
@@ -335,8 +335,7 @@ int routing_policy_rule_add_foreign(Manager *m,
         return routing_policy_rule_add_internal(m, &m->rules_foreign, family, from, from_prefixlen, to, to_prefixlen, tos, fwmark, table, iif, oif, protocol, sport, dport, ret);
 }
 
-static int routing_policy_rule_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
-        Link *link = userdata;
+static int routing_policy_rule_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
         int r;
 
         assert(m);
@@ -355,7 +354,7 @@ static int routing_policy_rule_remove_handler(sd_netlink *rtnl, sd_netlink_messa
         return 1;
 }
 
-int routing_policy_rule_remove(RoutingPolicyRule *routing_policy_rule, Link *link, sd_netlink_message_handler_t callback) {
+int routing_policy_rule_remove(RoutingPolicyRule *routing_policy_rule, Link *link, link_netlink_message_handler_t callback) {
         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
         int r;
 
@@ -398,8 +397,9 @@ int routing_policy_rule_remove(RoutingPolicyRule *routing_policy_rule, Link *lin
                         return log_error_errno(r, "Could not set destination prefix length: %m");
         }
 
-        r = sd_netlink_call_async(link->manager->rtnl, NULL, m, callback,
-                                  link_netlink_destroy_callback, link, 0, __func__);
+        r = netlink_call_async(link->manager->rtnl, NULL, m,
+                               callback ?: routing_policy_rule_remove_handler,
+                               link_netlink_destroy_callback, link);
         if (r < 0)
                 return log_error_errno(r, "Could not send rtnetlink message: %m");
 
@@ -455,8 +455,7 @@ static int routing_policy_rule_new_static(Network *network, const char *filename
         return 0;
 }
 
-int link_routing_policy_rule_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
-        Link *link = userdata;
+static int routing_policy_rule_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
         int r;
 
         assert(rtnl);
@@ -483,7 +482,7 @@ int link_routing_policy_rule_handler(sd_netlink *rtnl, sd_netlink_message *m, vo
         return 1;
 }
 
-int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, sd_netlink_message_handler_t callback, bool update) {
+int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, link_netlink_message_handler_t callback, bool update) {
         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
         int r;
 
@@ -591,8 +590,9 @@ int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, sd_netlin
 
         rule->link = link;
 
-        r = sd_netlink_call_async(link->manager->rtnl, NULL, m, callback,
-                                  link_netlink_destroy_callback, link, 0, __func__);
+        r = netlink_call_async(link->manager->rtnl, NULL, m,
+                               callback ?: routing_policy_rule_handler,
+                               link_netlink_destroy_callback, link);
         if (r < 0)
                 return log_error_errno(r, "Could not send rtnetlink message: %m");
 
@@ -796,8 +796,8 @@ int config_parse_routing_policy_rule_prefix(
 
         _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
         Network *network = userdata;
-        union in_addr_union buffer;
-        uint8_t prefixlen;
+        union in_addr_union *buffer;
+        uint8_t *prefixlen;
         int r;
 
         assert(filename);
@@ -810,24 +810,18 @@ int config_parse_routing_policy_rule_prefix(
         if (r < 0)
                 return r;
 
-        r = in_addr_prefix_from_string(rvalue, AF_INET, &buffer, &prefixlen);
-        if (r < 0) {
-                r = in_addr_prefix_from_string(rvalue, AF_INET6, &buffer, &prefixlen);
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue);
-                        return 0;
-                }
-
-                n->family = AF_INET6;
-        } else
-                n->family = AF_INET;
-
         if (streq(lvalue, "To")) {
-                n->to = buffer;
-                n->to_prefixlen = prefixlen;
+                buffer = &n->to;
+                prefixlen = &n->to_prefixlen;
         } else {
-                n->from = buffer;
-                n->from_prefixlen = prefixlen;
+                buffer = &n->from;
+                prefixlen = &n->from_prefixlen;
+        }
+
+        r = in_addr_prefix_from_string_auto(rvalue, &n->family, buffer, prefixlen);
+        if (r < 0) {
+                log_syntax(unit, LOG_ERR, filename, line, r, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue);
+                return 0;
         }
 
         n = NULL;
@@ -926,7 +920,7 @@ int config_parse_routing_policy_rule_port_range(
         return 0;
 }
 
-int config_parse_routing_policy_rule_protocol(
+int config_parse_routing_policy_rule_ip_protocol(
                 const char *unit,
                 const char *filename,
                 unsigned line,
@@ -952,14 +946,9 @@ int config_parse_routing_policy_rule_protocol(
         if (r < 0)
                 return r;
 
-        r = socket_protocol_from_name(rvalue);
+        r = parse_ip_protocol(rvalue);
         if (r < 0) {
-                log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse routing policy rule protocol, ignoring: %s", rvalue);
-                return 0;
-        }
-
-        if (!IN_SET(r, IPPROTO_TCP, IPPROTO_UDP, IPPROTO_SCTP)) {
-                log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid protocol '%s'. Protocol should be tcp/udp/sctp, ignoring", rvalue);
+                log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IP protocol '%s' for routing policy rule, ignoring: %m", rvalue);
                 return 0;
         }
 
@@ -1117,8 +1106,6 @@ int routing_policy_load_rules(const char *state_file, Set **rules) {
 
                 for (;;) {
                         _cleanup_free_ char *word = NULL, *a = NULL, *b = NULL;
-                        union in_addr_union buffer;
-                        uint8_t prefixlen;
 
                         r = extract_first_word(&p, &word, NULL, 0);
                         if (r < 0)
@@ -1131,26 +1118,23 @@ int routing_policy_load_rules(const char *state_file, Set **rules) {
                                 continue;
 
                         if (STR_IN_SET(a, "from", "to")) {
-
-                                r = in_addr_prefix_from_string(b, AF_INET, &buffer, &prefixlen);
-                                if (r < 0) {
-                                        r = in_addr_prefix_from_string(b, AF_INET6, &buffer, &prefixlen);
-                                        if (r < 0) {
-                                                log_error_errno(r, "RPDB rule prefix is invalid, ignoring assignment: %s", b);
-                                                continue;
-                                        }
-
-                                        rule->family = AF_INET6;
-                                } else
-                                        rule->family = AF_INET;
+                                union in_addr_union *buffer;
+                                uint8_t *prefixlen;
 
                                 if (streq(a, "to")) {
-                                        rule->to = buffer;
-                                        rule->to_prefixlen = prefixlen;
+                                        buffer = &rule->to;
+                                        prefixlen = &rule->to_prefixlen;
                                 } else {
-                                        rule->from = buffer;
-                                        rule->from_prefixlen = prefixlen;
+                                        buffer = &rule->from;
+                                        prefixlen = &rule->from_prefixlen;
                                 }
+
+                                r = in_addr_prefix_from_string_auto(b, &rule->family, buffer, prefixlen);
+                                if (r < 0) {
+                                        log_error_errno(r, "RPDB rule prefix is invalid, ignoring assignment: %s", b);
+                                        continue;
+                                }
+
                         } else if (streq(a, "tos")) {
                                 r = safe_atou8(b, &rule->tos);
                                 if (r < 0) {
@@ -1233,7 +1217,7 @@ void routing_policy_rule_purge(Manager *m, Link *link) {
                 existing = set_get(m->rules_foreign, rule);
                 if (existing) {
 
-                        r = routing_policy_rule_remove(rule, link, routing_policy_rule_remove_handler);
+                        r = routing_policy_rule_remove(rule, link, NULL);
                         if (r < 0) {
                                 log_warning_errno(r, "Could not remove routing policy rules: %m");
                                 continue;