X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=src%2Fnetwork%2Fnetworkd-routing-policy-rule.c;h=a1e56ec43679f2a6e88f165c2c2d14aedb4fc6db;hb=9f08a578a80a5fc759b9ce7299ab7fdd2e60c814;hp=bbab393546684b45a30a6b8c98597060ecf1b2f5;hpb=083d27b654ca579c44bcfb92792dc73ec7c80c98;p=thirdparty%2Fsystemd.git diff --git a/src/network/networkd-routing-policy-rule.c b/src/network/networkd-routing-policy-rule.c index bbab3935466..a1e56ec4367 100644 --- a/src/network/networkd-routing-policy-rule.c +++ b/src/network/networkd-routing-policy-rule.c @@ -6,6 +6,7 @@ #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" @@ -40,11 +41,8 @@ void routing_policy_rule_free(RoutingPolicyRule *rule) { assert(rule->network->n_rules > 0); rule->network->n_rules--; - if (rule->section) { + if (rule->section) hashmap_remove(rule->network->rules_by_section, rule->section); - network_config_section_free(rule->section); - } - } if (rule->manager) { @@ -52,14 +50,13 @@ void routing_policy_rule_free(RoutingPolicyRule *rule) { set_remove(rule->manager->rules_foreign, rule); } + network_config_section_free(rule->section); free(rule->iif); free(rule->oif); free(rule); } -static void routing_policy_rule_hash_func(const void *b, struct siphash *state) { - const RoutingPolicyRule *rule = b; - +static void routing_policy_rule_hash_func(const RoutingPolicyRule *rule, struct siphash *state) { assert(rule); siphash24_compress(&rule->family, sizeof(rule->family), state); @@ -78,11 +75,15 @@ static void routing_policy_rule_hash_func(const void *b, struct siphash *state) siphash24_compress(&rule->fwmark, sizeof(rule->fwmark), state); siphash24_compress(&rule->table, sizeof(rule->table), state); + siphash24_compress(&rule->protocol, sizeof(rule->protocol), state); + siphash24_compress(&rule->sport, sizeof(rule->sport), state); + siphash24_compress(&rule->dport, sizeof(rule->dport), state); + if (rule->iif) - siphash24_compress(&rule->iif, strlen(rule->iif), state); + siphash24_compress(rule->iif, strlen(rule->iif), state); if (rule->oif) - siphash24_compress(&rule->oif, strlen(rule->oif), state); + siphash24_compress(rule->oif, strlen(rule->oif), state); break; default: @@ -91,8 +92,7 @@ static void routing_policy_rule_hash_func(const void *b, struct siphash *state) } } -static int routing_policy_rule_compare_func(const void *_a, const void *_b) { - const RoutingPolicyRule *a = _a, *b = _b; +static int routing_policy_rule_compare_func(const RoutingPolicyRule *a, const RoutingPolicyRule *b) { int r; r = CMP(a->family, b->family); @@ -130,6 +130,18 @@ static int routing_policy_rule_compare_func(const void *_a, const void *_b) { if (!r) return r; + r = CMP(a->protocol, b->protocol); + if (r != 0) + return r; + + r = memcmp(&a->sport, &b->sport, sizeof(a->sport)); + if (r != 0) + return r; + + r = memcmp(&a->dport, &b->dport, sizeof(a->dport)); + if (r != 0) + return r; + r = memcmp(&a->from, &b->from, FAMILY_ADDRESS_SIZE(a->family)); if (r != 0) return r; @@ -142,10 +154,7 @@ static int routing_policy_rule_compare_func(const void *_a, const void *_b) { } } -const struct hash_ops routing_policy_rule_hash_ops = { - .hash = routing_policy_rule_hash_func, - .compare = routing_policy_rule_compare_func -}; +DEFINE_PRIVATE_HASH_OPS(routing_policy_rule_hash_ops, RoutingPolicyRule, routing_policy_rule_hash_func, routing_policy_rule_compare_func); int routing_policy_rule_get(Manager *m, int family, @@ -158,6 +167,9 @@ int routing_policy_rule_get(Manager *m, uint32_t table, const char *iif, const char *oif, + uint8_t protocol, + struct fib_rule_port_range *sport, + struct fib_rule_port_range *dport, RoutingPolicyRule **ret) { RoutingPolicyRule rule, *existing; @@ -174,7 +186,10 @@ int routing_policy_rule_get(Manager *m, .fwmark = fwmark, .table = table, .iif = (char*) iif, - .oif = (char*) oif + .oif = (char*) oif, + .protocol = protocol, + .sport = *sport, + .dport = *dport, }; existing = set_get(m->rules, &rule); @@ -188,7 +203,7 @@ int routing_policy_rule_get(Manager *m, if (existing) { if (ret) *ret = existing; - return 1; + return 0; } return -ENOENT; @@ -224,6 +239,9 @@ static int routing_policy_rule_add_internal(Manager *m, uint32_t table, const char *_iif, const char *_oif, + uint8_t protocol, + const struct fib_rule_port_range *sport, + const struct fib_rule_port_range *dport, RoutingPolicyRule **ret) { _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL; @@ -259,6 +277,9 @@ static int routing_policy_rule_add_internal(Manager *m, rule->table = table; rule->iif = TAKE_PTR(iif); rule->oif = TAKE_PTR(oif); + rule->protocol = protocol; + rule->sport = *sport; + rule->dport = *dport; r = set_ensure_allocated(rules, &routing_policy_rule_hash_ops); if (r < 0) @@ -271,8 +292,7 @@ static int routing_policy_rule_add_internal(Manager *m, if (ret) *ret = rule; - rule = NULL; - + TAKE_PTR(rule); return 0; } @@ -287,9 +307,12 @@ int routing_policy_rule_add(Manager *m, uint32_t table, const char *iif, const char *oif, + uint8_t protocol, + const struct fib_rule_port_range *sport, + const struct fib_rule_port_range *dport, RoutingPolicyRule **ret) { - return routing_policy_rule_add_internal(m, &m->rules, family, from, from_prefixlen, to, to_prefixlen, tos, fwmark, table, iif, oif, ret); + return routing_policy_rule_add_internal(m, &m->rules, family, from, from_prefixlen, to, to_prefixlen, tos, fwmark, table, iif, oif, protocol, sport, dport, ret); } int routing_policy_rule_add_foreign(Manager *m, @@ -303,12 +326,14 @@ int routing_policy_rule_add_foreign(Manager *m, uint32_t table, const char *iif, const char *oif, + uint8_t protocol, + const struct fib_rule_port_range *sport, + const struct fib_rule_port_range *dport, RoutingPolicyRule **ret) { - return routing_policy_rule_add_internal(m, &m->rules_foreign, family, from, from_prefixlen, to, to_prefixlen, tos, fwmark, table, iif, oif, ret); + 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) { - _cleanup_(link_unrefp) Link *link = userdata; +static int routing_policy_rule_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { int r; assert(m); @@ -327,7 +352,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; @@ -342,12 +367,8 @@ int routing_policy_rule_remove(RoutingPolicyRule *routing_policy_rule, Link *lin if (r < 0) return log_error_errno(r, "Could not allocate RTM_DELRULE message: %m"); - if (!in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->from)) { - if (routing_policy_rule->family == AF_INET) - r = sd_netlink_message_append_in_addr(m, FRA_SRC, &routing_policy_rule->from.in); - else - r = sd_netlink_message_append_in6_addr(m, FRA_SRC, &routing_policy_rule->from.in6); - + if (in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->from) == 0) { + r = netlink_message_append_in_addr_union(m, FRA_SRC, routing_policy_rule->family, &routing_policy_rule->from); if (r < 0) return log_error_errno(r, "Could not append FRA_SRC attribute: %m"); @@ -356,12 +377,8 @@ int routing_policy_rule_remove(RoutingPolicyRule *routing_policy_rule, Link *lin return log_error_errno(r, "Could not set source prefix length: %m"); } - if (!in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->to)) { - if (routing_policy_rule->family == AF_INET) - r = sd_netlink_message_append_in_addr(m, FRA_DST, &routing_policy_rule->to.in); - else - r = sd_netlink_message_append_in6_addr(m, FRA_DST, &routing_policy_rule->to.in6); - + if (in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->to) == 0) { + r = netlink_message_append_in_addr_union(m, FRA_DST, routing_policy_rule->family, &routing_policy_rule->to); if (r < 0) return log_error_errno(r, "Could not append FRA_DST attribute: %m"); @@ -370,7 +387,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, m, callback, link, 0, NULL); + 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"); @@ -388,38 +407,45 @@ static int routing_policy_rule_new_static(Network *network, const char *filename assert(ret); assert(!!filename == (section_line > 0)); - r = network_config_section_new(filename, section_line, &n); - if (r < 0) - return r; + if (filename) { + r = network_config_section_new(filename, section_line, &n); + if (r < 0) + return r; - rule = hashmap_get(network->rules_by_section, n); - if (rule) { - *ret = TAKE_PTR(rule); + rule = hashmap_get(network->rules_by_section, n); + if (rule) { + *ret = TAKE_PTR(rule); - return 0; + return 0; + } } r = routing_policy_rule_new(&rule); if (r < 0) return r; - rule->section = TAKE_PTR(n); rule->network = network; - - r = hashmap_put(network->rules_by_section, rule->section, rule); - if (r < 0) - return r; - LIST_APPEND(rules, network->rules, rule); network->n_rules++; + if (filename) { + rule->section = TAKE_PTR(n); + + r = hashmap_ensure_allocated(&network->rules_by_section, &network_config_hash_ops); + if (r < 0) + return r; + + r = hashmap_put(network->rules_by_section, rule->section, rule); + if (r < 0) + return r; + } + *ret = TAKE_PTR(rule); return 0; } -int link_routing_policy_rule_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { - _cleanup_(link_unrefp) Link *link = userdata; +static int routing_policy_rule_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { int r; assert(rtnl); @@ -446,7 +472,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) { _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; int r; @@ -460,12 +486,8 @@ int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, sd_netlin if (r < 0) return log_error_errno(r, "Could not allocate RTM_NEWRULE message: %m"); - if (!in_addr_is_null(rule->family, &rule->from)) { - if (rule->family == AF_INET) - r = sd_netlink_message_append_in_addr(m, FRA_SRC, &rule->from.in); - else - r = sd_netlink_message_append_in6_addr(m, FRA_SRC, &rule->from.in6); - + if (in_addr_is_null(rule->family, &rule->from) == 0) { + r = netlink_message_append_in_addr_union(m, FRA_SRC, rule->family, &rule->from); if (r < 0) return log_error_errno(r, "Could not append FRA_SRC attribute: %m"); @@ -474,12 +496,8 @@ int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, sd_netlin return log_error_errno(r, "Could not set source prefix length: %m"); } - if (!in_addr_is_null(rule->family, &rule->to)) { - if (rule->family == AF_INET) - r = sd_netlink_message_append_in_addr(m, FRA_DST, &rule->to.in); - else - r = sd_netlink_message_append_in6_addr(m, FRA_DST, &rule->to.in6); - + if (in_addr_is_null(rule->family, &rule->to) == 0) { + r = netlink_message_append_in_addr_union(m, FRA_DST, rule->family, &rule->to); if (r < 0) return log_error_errno(r, "Could not append FRA_DST attribute: %m"); @@ -536,18 +554,42 @@ int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, sd_netlin return log_error_errno(r, "Could not append FRA_OIFNAME attribute: %m"); } + r = sd_netlink_message_append_u8(m, FRA_IP_PROTO, rule->protocol); + if (r < 0) + return log_error_errno(r, "Could not append FRA_IP_PROTO attribute: %m"); + + if (rule->sport.start != 0 || rule->sport.end != 0) { + r = sd_netlink_message_append_data(m, FRA_SPORT_RANGE, &rule->sport, sizeof(rule->sport)); + if (r < 0) + return log_error_errno(r, "Could not append FRA_SPORT_RANGE attribute: %m"); + } + + if (rule->dport.start != 0 || rule->dport.end != 0) { + r = sd_netlink_message_append_data(m, FRA_DPORT_RANGE, &rule->dport, sizeof(rule->dport)); + if (r < 0) + return log_error_errno(r, "Could not append FRA_DPORT_RANGE attribute: %m"); + } + + if (rule->invert_rule) { + r = sd_rtnl_message_routing_policy_rule_set_flags(m, FIB_RULE_INVERT); + if (r < 0) + return log_error_errno(r, "Could not append FIB_RULE_INVERT attribute: %m"); + } + rule->link = link; - r = sd_netlink_call_async(link->manager->rtnl, m, callback, link, 0, NULL); + 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"); link_ref(link); r = routing_policy_rule_add(link->manager, rule->family, &rule->from, rule->from_prefixlen, &rule->to, - rule->to_prefixlen, rule->tos, rule->fwmark, rule->table, rule->iif, rule->oif, NULL); + rule->to_prefixlen, rule->tos, rule->fwmark, rule->table, rule->iif, rule->oif, rule->protocol, &rule->sport, &rule->dport, NULL); if (r < 0) - return log_error_errno(r, "Could not add rule : %m"); + return log_error_errno(r, "Could not add rule: %m"); return 0; } @@ -592,7 +634,7 @@ int config_parse_routing_policy_rule_tos( void *data, void *userdata) { - _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; + _cleanup_(routing_policy_rule_free_or_set_invalidp) RoutingPolicyRule *n = NULL; Network *network = userdata; int r; @@ -629,7 +671,7 @@ int config_parse_routing_policy_rule_priority( void *data, void *userdata) { - _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; + _cleanup_(routing_policy_rule_free_or_set_invalidp) RoutingPolicyRule *n = NULL; Network *network = userdata; int r; @@ -666,7 +708,7 @@ int config_parse_routing_policy_rule_table( void *data, void *userdata) { - _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; + _cleanup_(routing_policy_rule_free_or_set_invalidp) RoutingPolicyRule *n = NULL; Network *network = userdata; int r; @@ -703,7 +745,7 @@ int config_parse_routing_policy_rule_fwmark_mask( void *data, void *userdata) { - _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; + _cleanup_(routing_policy_rule_free_or_set_invalidp) RoutingPolicyRule *n = NULL; Network *network = userdata; int r; @@ -740,10 +782,10 @@ int config_parse_routing_policy_rule_prefix( void *data, void *userdata) { - _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; + _cleanup_(routing_policy_rule_free_or_set_invalidp) 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); @@ -756,24 +798,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; @@ -793,7 +829,7 @@ int config_parse_routing_policy_rule_device( void *data, void *userdata) { - _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; + _cleanup_(routing_policy_rule_free_or_set_invalidp) RoutingPolicyRule *n = NULL; Network *network = userdata; int r; @@ -827,6 +863,129 @@ int config_parse_routing_policy_rule_device( return 0; } +int config_parse_routing_policy_rule_port_range( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + _cleanup_(routing_policy_rule_free_or_set_invalidp) RoutingPolicyRule *n = NULL; + Network *network = userdata; + uint16_t low, high; + int r; + + assert(filename); + assert(section); + assert(lvalue); + assert(rvalue); + assert(data); + + r = routing_policy_rule_new_static(network, filename, section_line, &n); + if (r < 0) + return r; + + r = parse_ip_port_range(rvalue, &low, &high); + if (r < 0) { + log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse routing policy rule port range '%s'", rvalue); + return 0; + } + + if (streq(lvalue, "SourcePort")) { + n->sport.start = low; + n->sport.end = high; + } else { + n->dport.start = low; + n->dport.end = high; + } + + n = NULL; + + return 0; +} + +int config_parse_routing_policy_rule_ip_protocol( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + _cleanup_(routing_policy_rule_free_or_set_invalidp) RoutingPolicyRule *n = NULL; + Network *network = userdata; + int r; + + assert(filename); + assert(section); + assert(lvalue); + assert(rvalue); + assert(data); + + r = routing_policy_rule_new_static(network, filename, section_line, &n); + if (r < 0) + return r; + + r = parse_ip_protocol(rvalue); + if (r < 0) { + log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IP protocol '%s' for routing policy rule, ignoring: %m", rvalue); + return 0; + } + + n->protocol = r; + + n = NULL; + + return 0; +} + +int config_parse_routing_policy_rule_invert( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + _cleanup_(routing_policy_rule_free_or_set_invalidp) RoutingPolicyRule *n = NULL; + Network *network = userdata; + int r; + + assert(filename); + assert(section); + assert(lvalue); + assert(rvalue); + assert(data); + + r = routing_policy_rule_new_static(network, filename, section_line, &n); + if (r < 0) + return r; + + r = parse_boolean(rvalue); + if (r < 0) { + log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule invert, ignoring: %s", rvalue); + return 0; + } + + n->invert_rule = r; + + n = NULL; + + return 0; +} + static int routing_policy_rule_read_full_file(const char *state_file, char **ret) { _cleanup_free_ char *s = NULL; size_t size; @@ -909,6 +1068,27 @@ int routing_policy_serialize_rules(Set *rules, FILE *f) { space = true; } + if (rule->protocol != 0) { + fprintf(f, "%sprotocol=%hhu", + space ? " " : "", + rule->protocol); + space = true; + } + + if (rule->sport.start != 0 || rule->sport.end != 0) { + fprintf(f, "%ssourcesport=%"PRIu16"-%"PRIu16, + space ? " " : "", + rule->sport.start, rule->sport.end); + space = true; + } + + if (rule->dport.start != 0 || rule->dport.end != 0) { + fprintf(f, "%sdestinationport=%"PRIu16"-%"PRIu16, + space ? " " : "", + rule->dport.start, rule->dport.end); + space = true; + } + fprintf(f, "%stable=%"PRIu32 "\n", space ? " " : "", rule->table); @@ -920,6 +1100,7 @@ int routing_policy_serialize_rules(Set *rules, FILE *f) { int routing_policy_load_rules(const char *state_file, Set **rules) { _cleanup_strv_free_ char **l = NULL; _cleanup_free_ char *data = NULL; + uint16_t low = 0, high = 0; const char *p; char **i; int r; @@ -952,8 +1133,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) @@ -966,26 +1145,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) { @@ -1014,6 +1190,33 @@ int routing_policy_load_rules(const char *state_file, Set **rules) { if (free_and_strdup(&rule->oif, b) < 0) return log_oom(); + } else if (streq(a, "protocol")) { + r = safe_atou8(b, &rule->protocol); + if (r < 0) { + log_error_errno(r, "Failed to parse RPDB rule protocol, ignoring: %s", b); + continue; + } + } else if (streq(a, "sourceport")) { + + r = parse_ip_port_range(b, &low, &high); + if (r < 0) { + log_error_errno(r, "Invalid routing policy rule source port range, ignoring assignment:'%s'", b); + continue; + } + + rule->sport.start = low; + rule->sport.end = high; + + } else if (streq(a, "destinationport")) { + + r = parse_ip_port_range(b, &low, &high); + if (r < 0) { + log_error_errno(r, "Invalid routing policy rule destination port range, ignoring assignment:'%s'", b); + continue; + } + + rule->dport.start = low; + rule->dport.end = high; } } @@ -1029,6 +1232,26 @@ int routing_policy_load_rules(const char *state_file, Set **rules) { return 0; } +static bool manager_links_have_routing_policy_rule(Manager *m, RoutingPolicyRule *rule) { + RoutingPolicyRule *link_rule; + Iterator i; + Link *link; + + assert(m); + assert(rule); + + HASHMAP_FOREACH(link, m->links, i) { + if (!link->network) + continue; + + LIST_FOREACH(rules, link_rule, link->network->rules) + if (routing_policy_rule_compare_func(link_rule, rule) == 0) + return true; + } + + return false; +} + void routing_policy_rule_purge(Manager *m, Link *link) { RoutingPolicyRule *rule, *existing; Iterator i; @@ -1039,15 +1262,24 @@ void routing_policy_rule_purge(Manager *m, Link *link) { SET_FOREACH(rule, m->rules_saved, i) { existing = set_get(m->rules_foreign, rule); - if (existing) { + if (!existing) + continue; /* Saved rule does not exist anymore. */ - r = routing_policy_rule_remove(rule, link, routing_policy_rule_remove_handler); - if (r < 0) { - log_warning_errno(r, "Could not remove routing policy rules: %m"); - continue; - } + if (manager_links_have_routing_policy_rule(m, existing)) + continue; /* Existing links have the saved rule. */ + + /* Existing links do not have the saved rule. Let's drop the rule now, and re-configure it + * later when it is requested. */ - link->routing_policy_rule_remove_messages++; + r = routing_policy_rule_remove(existing, link, NULL); + if (r < 0) { + log_warning_errno(r, "Could not remove routing policy rules: %m"); + continue; } + + link->routing_policy_rule_remove_messages++; + + assert_se(set_remove(m->rules_foreign, existing) == existing); + routing_policy_rule_free(existing); } }