1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/fib_rules.h>
7 #include "alloc-util.h"
8 #include "conf-parser.h"
10 #include "ip-protocol-list.h"
11 #include "networkd-routing-policy-rule.h"
12 #include "netlink-util.h"
13 #include "networkd-manager.h"
14 #include "parse-util.h"
15 #include "socket-util.h"
16 #include "string-util.h"
19 int routing_policy_rule_new(RoutingPolicyRule
**ret
) {
20 RoutingPolicyRule
*rule
;
22 rule
= new(RoutingPolicyRule
, 1);
26 *rule
= (RoutingPolicyRule
) {
27 .table
= RT_TABLE_MAIN
,
34 void routing_policy_rule_free(RoutingPolicyRule
*rule
) {
40 LIST_REMOVE(rules
, rule
->network
->rules
, rule
);
41 assert(rule
->network
->n_rules
> 0);
42 rule
->network
->n_rules
--;
45 hashmap_remove(rule
->network
->rules_by_section
, rule
->section
);
49 if (set_get(rule
->manager
->rules
, rule
) == rule
)
50 set_remove(rule
->manager
->rules
, rule
);
51 if (set_get(rule
->manager
->rules_foreign
, rule
) == rule
)
52 set_remove(rule
->manager
->rules_foreign
, rule
);
55 network_config_section_free(rule
->section
);
61 static void routing_policy_rule_hash_func(const RoutingPolicyRule
*rule
, struct siphash
*state
) {
64 siphash24_compress(&rule
->family
, sizeof(rule
->family
), state
);
66 switch (rule
->family
) {
70 siphash24_compress(&rule
->from
, FAMILY_ADDRESS_SIZE(rule
->family
), state
);
71 siphash24_compress(&rule
->from_prefixlen
, sizeof(rule
->from_prefixlen
), state
);
73 siphash24_compress(&rule
->to
, FAMILY_ADDRESS_SIZE(rule
->family
), state
);
74 siphash24_compress(&rule
->to_prefixlen
, sizeof(rule
->to_prefixlen
), state
);
76 siphash24_compress_boolean(rule
->invert_rule
, state
);
78 siphash24_compress(&rule
->tos
, sizeof(rule
->tos
), state
);
79 siphash24_compress(&rule
->fwmark
, sizeof(rule
->fwmark
), state
);
80 siphash24_compress(&rule
->fwmask
, sizeof(rule
->fwmask
), state
);
81 siphash24_compress(&rule
->priority
, sizeof(rule
->priority
), state
);
82 siphash24_compress(&rule
->table
, sizeof(rule
->table
), state
);
84 siphash24_compress(&rule
->protocol
, sizeof(rule
->protocol
), state
);
85 siphash24_compress(&rule
->sport
, sizeof(rule
->sport
), state
);
86 siphash24_compress(&rule
->dport
, sizeof(rule
->dport
), state
);
89 siphash24_compress(rule
->iif
, strlen(rule
->iif
), state
);
92 siphash24_compress(rule
->oif
, strlen(rule
->oif
), state
);
96 /* treat any other address family as AF_UNSPEC */
101 static int routing_policy_rule_compare_func(const RoutingPolicyRule
*a
, const RoutingPolicyRule
*b
) {
104 r
= CMP(a
->family
, b
->family
);
111 r
= CMP(a
->from_prefixlen
, b
->from_prefixlen
);
115 r
= CMP(a
->to_prefixlen
, b
->to_prefixlen
);
119 r
= CMP(a
->invert_rule
, b
->invert_rule
);
123 r
= CMP(a
->tos
, b
->tos
);
127 r
= CMP(a
->fwmark
, b
->fwmark
);
131 r
= CMP(a
->fwmask
, b
->fwmask
);
135 r
= CMP(a
->priority
, b
->priority
);
139 r
= CMP(a
->table
, b
->table
);
143 r
= strcmp_ptr(a
->iif
, b
->iif
);
147 r
= strcmp_ptr(a
->oif
, b
->oif
);
151 r
= CMP(a
->protocol
, b
->protocol
);
155 r
= memcmp(&a
->sport
, &b
->sport
, sizeof(a
->sport
));
159 r
= memcmp(&a
->dport
, &b
->dport
, sizeof(a
->dport
));
163 r
= memcmp(&a
->from
, &b
->from
, FAMILY_ADDRESS_SIZE(a
->family
));
167 return memcmp(&a
->to
, &b
->to
, FAMILY_ADDRESS_SIZE(a
->family
));
170 /* treat any other address family as AF_UNSPEC */
175 DEFINE_PRIVATE_HASH_OPS(routing_policy_rule_hash_ops
, RoutingPolicyRule
, routing_policy_rule_hash_func
, routing_policy_rule_compare_func
);
177 int routing_policy_rule_get(Manager
*m
, RoutingPolicyRule
*rule
, RoutingPolicyRule
**ret
) {
179 RoutingPolicyRule
*existing
;
183 existing
= set_get(m
->rules
, rule
);
190 existing
= set_get(m
->rules_foreign
, rule
);
200 int routing_policy_rule_make_local(Manager
*m
, RoutingPolicyRule
*rule
) {
205 if (set_contains(m
->rules_foreign
, rule
)) {
206 set_remove(m
->rules_foreign
, rule
);
208 r
= set_ensure_allocated(&m
->rules
, &routing_policy_rule_hash_ops
);
212 r
= set_put(m
->rules
, rule
);
216 routing_policy_rule_free(rule
);
222 static int routing_policy_rule_add_internal(Manager
*m
, Set
**rules
, RoutingPolicyRule
*in
, RoutingPolicyRule
**ret
) {
223 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
224 _cleanup_free_
char *iif
= NULL
, *oif
= NULL
;
232 iif
= strdup(in
->iif
);
238 oif
= strdup(in
->oif
);
243 r
= routing_policy_rule_new(&rule
);
248 rule
->family
= in
->family
;
249 rule
->from
= in
->from
;
250 rule
->from_prefixlen
= in
->from_prefixlen
;
252 rule
->to_prefixlen
= in
->to_prefixlen
;
253 rule
->invert_rule
= in
->invert_rule
;
255 rule
->fwmark
= in
->fwmark
;
256 rule
->fwmask
= in
->fwmask
;
257 rule
->priority
= in
->priority
;
258 rule
->table
= in
->table
;
259 rule
->iif
= TAKE_PTR(iif
);
260 rule
->oif
= TAKE_PTR(oif
);
261 rule
->protocol
= in
->protocol
;
262 rule
->sport
= in
->sport
;
263 rule
->dport
= in
->dport
;
265 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
269 r
= set_put(*rules
, rule
);
282 static int routing_policy_rule_add(Manager
*m
, RoutingPolicyRule
*rule
, RoutingPolicyRule
**ret
) {
283 return routing_policy_rule_add_internal(m
, &m
->rules
, rule
, ret
);
286 int routing_policy_rule_add_foreign(Manager
*m
, RoutingPolicyRule
*rule
, RoutingPolicyRule
**ret
) {
287 return routing_policy_rule_add_internal(m
, &m
->rules_foreign
, rule
, ret
);
290 static int routing_policy_rule_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
295 assert(link
->ifname
);
297 link
->routing_policy_rule_remove_messages
--;
299 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
302 r
= sd_netlink_message_get_errno(m
);
304 log_link_warning_errno(link
, r
, "Could not drop routing policy rule: %m");
309 int routing_policy_rule_remove(RoutingPolicyRule
*routing_policy_rule
, Link
*link
, link_netlink_message_handler_t callback
) {
310 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
313 assert(routing_policy_rule
);
315 assert(link
->manager
);
316 assert(link
->manager
->rtnl
);
317 assert(link
->ifindex
> 0);
318 assert(IN_SET(routing_policy_rule
->family
, AF_INET
, AF_INET6
));
320 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_DELRULE
, routing_policy_rule
->family
);
322 return log_error_errno(r
, "Could not allocate RTM_DELRULE message: %m");
324 if (in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->from
) == 0) {
325 r
= netlink_message_append_in_addr_union(m
, FRA_SRC
, routing_policy_rule
->family
, &routing_policy_rule
->from
);
327 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
329 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, routing_policy_rule
->from_prefixlen
);
331 return log_error_errno(r
, "Could not set source prefix length: %m");
334 if (in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->to
) == 0) {
335 r
= netlink_message_append_in_addr_union(m
, FRA_DST
, routing_policy_rule
->family
, &routing_policy_rule
->to
);
337 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
339 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, routing_policy_rule
->to_prefixlen
);
341 return log_error_errno(r
, "Could not set destination prefix length: %m");
344 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
,
345 callback
?: routing_policy_rule_remove_handler
,
346 link_netlink_destroy_callback
, link
);
348 return log_error_errno(r
, "Could not send rtnetlink message: %m");
355 static int routing_policy_rule_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutingPolicyRule
**ret
) {
356 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
357 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
362 assert(!!filename
== (section_line
> 0));
365 r
= network_config_section_new(filename
, section_line
, &n
);
369 rule
= hashmap_get(network
->rules_by_section
, n
);
371 *ret
= TAKE_PTR(rule
);
377 r
= routing_policy_rule_new(&rule
);
381 rule
->network
= network
;
382 LIST_APPEND(rules
, network
->rules
, rule
);
386 rule
->section
= TAKE_PTR(n
);
388 r
= hashmap_ensure_allocated(&network
->rules_by_section
, &network_config_hash_ops
);
392 r
= hashmap_put(network
->rules_by_section
, rule
->section
, rule
);
397 *ret
= TAKE_PTR(rule
);
402 static int routing_policy_rule_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
408 assert(link
->ifname
);
409 assert(link
->routing_policy_rule_messages
> 0);
411 link
->routing_policy_rule_messages
--;
413 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
416 r
= sd_netlink_message_get_errno(m
);
417 if (r
< 0 && r
!= -EEXIST
) {
418 log_link_warning_errno(link
, r
, "Could not add routing policy rule: %m");
419 link_enter_failed(link
);
423 if (link
->routing_policy_rule_messages
== 0) {
424 log_link_debug(link
, "Routing policy rule configured");
425 link
->routing_policy_rules_configured
= true;
426 link_check_ready(link
);
432 int routing_policy_rule_configure(RoutingPolicyRule
*rule
, Link
*link
, link_netlink_message_handler_t callback
) {
433 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
438 assert(link
->ifindex
> 0);
439 assert(link
->manager
);
440 assert(link
->manager
->rtnl
);
442 if (rule
->family
== AF_INET6
&& link_sysctl_ipv6_enabled(link
) == 0) {
443 log_link_warning(link
, "An IPv6 routing policy rule is requested, but IPv6 is disabled by sysctl, ignoring.");
447 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
449 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
451 if (in_addr_is_null(rule
->family
, &rule
->from
) == 0) {
452 r
= netlink_message_append_in_addr_union(m
, FRA_SRC
, rule
->family
, &rule
->from
);
454 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
456 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
458 return log_error_errno(r
, "Could not set source prefix length: %m");
461 if (in_addr_is_null(rule
->family
, &rule
->to
) == 0) {
462 r
= netlink_message_append_in_addr_union(m
, FRA_DST
, rule
->family
, &rule
->to
);
464 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
466 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
468 return log_error_errno(r
, "Could not set destination prefix length: %m");
471 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
473 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
476 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
478 return log_error_errno(r
, "Could not set ip rule tos: %m");
481 if (rule
->table
< 256) {
482 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
484 return log_error_errno(r
, "Could not set ip rule table: %m");
486 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
488 return log_error_errno(r
, "Could not set ip rule table: %m");
490 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
492 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
495 if (rule
->fwmark
> 0) {
496 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
498 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
501 if (rule
->fwmask
> 0) {
502 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
504 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
508 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
510 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
514 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
516 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
519 r
= sd_netlink_message_append_u8(m
, FRA_IP_PROTO
, rule
->protocol
);
521 return log_error_errno(r
, "Could not append FRA_IP_PROTO attribute: %m");
523 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
524 r
= sd_netlink_message_append_data(m
, FRA_SPORT_RANGE
, &rule
->sport
, sizeof(rule
->sport
));
526 return log_error_errno(r
, "Could not append FRA_SPORT_RANGE attribute: %m");
529 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
530 r
= sd_netlink_message_append_data(m
, FRA_DPORT_RANGE
, &rule
->dport
, sizeof(rule
->dport
));
532 return log_error_errno(r
, "Could not append FRA_DPORT_RANGE attribute: %m");
535 if (rule
->invert_rule
) {
536 r
= sd_rtnl_message_routing_policy_rule_set_flags(m
, FIB_RULE_INVERT
);
538 return log_error_errno(r
, "Could not append FIB_RULE_INVERT attribute: %m");
543 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
,
544 callback
?: routing_policy_rule_handler
,
545 link_netlink_destroy_callback
, link
);
547 return log_error_errno(r
, "Could not send rtnetlink message: %m");
551 r
= routing_policy_rule_add(link
->manager
, rule
, NULL
);
553 return log_error_errno(r
, "Could not add rule: %m");
558 int routing_policy_rule_section_verify(RoutingPolicyRule
*rule
) {
559 if (section_is_invalid(rule
->section
))
562 if (rule
->family
== AF_UNSPEC
)
563 rule
->family
= AF_INET
;
568 static int parse_fwmark_fwmask(const char *s
, uint32_t *fwmark
, uint32_t *fwmask
) {
569 _cleanup_free_
char *f
= NULL
;
583 r
= safe_atou32(f
, fwmark
);
585 return log_error_errno(r
, "Failed to parse RPDB rule firewall mark, ignoring: %s", f
);
588 r
= safe_atou32(p
, fwmask
);
590 return log_error_errno(r
, "Failed to parse RPDB rule mask, ignoring: %s", f
);
596 int config_parse_routing_policy_rule_tos(
598 const char *filename
,
601 unsigned section_line
,
608 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
609 Network
*network
= userdata
;
618 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
622 r
= safe_atou8(rvalue
, &n
->tos
);
624 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule tos, ignoring: %s", rvalue
);
633 int config_parse_routing_policy_rule_priority(
635 const char *filename
,
638 unsigned section_line
,
645 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
646 Network
*network
= userdata
;
655 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
659 r
= safe_atou32(rvalue
, &n
->priority
);
661 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule priority, ignoring: %s", rvalue
);
670 int config_parse_routing_policy_rule_table(
672 const char *filename
,
675 unsigned section_line
,
682 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
683 Network
*network
= userdata
;
692 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
696 r
= safe_atou32(rvalue
, &n
->table
);
698 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule table, ignoring: %s", rvalue
);
707 int config_parse_routing_policy_rule_fwmark_mask(
709 const char *filename
,
712 unsigned section_line
,
719 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
720 Network
*network
= userdata
;
729 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
733 r
= parse_fwmark_fwmask(rvalue
, &n
->fwmark
, &n
->fwmask
);
735 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue
);
744 int config_parse_routing_policy_rule_prefix(
746 const char *filename
,
749 unsigned section_line
,
756 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
757 Network
*network
= userdata
;
758 union in_addr_union
*buffer
;
768 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
772 if (streq(lvalue
, "To")) {
774 prefixlen
= &n
->to_prefixlen
;
777 prefixlen
= &n
->from_prefixlen
;
780 if (n
->family
== AF_UNSPEC
)
781 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
783 r
= in_addr_prefix_from_string(rvalue
, n
->family
, buffer
, prefixlen
);
785 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
794 int config_parse_routing_policy_rule_device(
796 const char *filename
,
799 unsigned section_line
,
806 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
807 Network
*network
= userdata
;
816 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
820 if (!ifname_valid(rvalue
)) {
821 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse '%s' interface name, ignoring: %s", lvalue
, rvalue
);
825 if (streq(lvalue
, "IncomingInterface")) {
826 r
= free_and_strdup(&n
->iif
, rvalue
);
830 r
= free_and_strdup(&n
->oif
, rvalue
);
840 int config_parse_routing_policy_rule_port_range(
842 const char *filename
,
845 unsigned section_line
,
851 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
852 Network
*network
= userdata
;
862 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
866 r
= parse_ip_port_range(rvalue
, &low
, &high
);
868 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse routing policy rule port range '%s'", rvalue
);
872 if (streq(lvalue
, "SourcePort")) {
873 n
->sport
.start
= low
;
876 n
->dport
.start
= low
;
885 int config_parse_routing_policy_rule_ip_protocol(
887 const char *filename
,
890 unsigned section_line
,
897 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
898 Network
*network
= userdata
;
907 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
911 r
= parse_ip_protocol(rvalue
);
913 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse IP protocol '%s' for routing policy rule, ignoring: %m", rvalue
);
924 int config_parse_routing_policy_rule_invert(
926 const char *filename
,
929 unsigned section_line
,
936 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
937 Network
*network
= userdata
;
946 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
950 r
= parse_boolean(rvalue
);
952 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule invert, ignoring: %s", rvalue
);
963 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
964 _cleanup_free_
char *s
= NULL
;
970 r
= read_full_file(state_file
, &s
, &size
);
983 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
984 RoutingPolicyRule
*rule
= NULL
;
990 SET_FOREACH(rule
, rules
, i
) {
991 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
993 const char *family_str
;
997 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
998 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
1002 fprintf(f
, "from=%s/%hhu",
1003 from_str
, rule
->from_prefixlen
);
1007 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
1008 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
1012 fprintf(f
, "%sto=%s/%hhu",
1014 to_str
, rule
->to_prefixlen
);
1018 family_str
= af_to_name(rule
->family
);
1020 fprintf(f
, "%sfamily=%s",
1024 if (rule
->tos
!= 0) {
1025 fprintf(f
, "%stos=%hhu",
1031 fprintf(f
, "%spriority=%"PRIu32
,
1035 if (rule
->fwmark
!= 0) {
1036 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
1038 rule
->fwmark
, rule
->fwmask
);
1043 fprintf(f
, "%siif=%s",
1050 fprintf(f
, "%soif=%s",
1056 if (rule
->protocol
!= 0) {
1057 fprintf(f
, "%sprotocol=%hhu",
1063 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
1064 fprintf(f
, "%ssourcesport=%"PRIu16
"-%"PRIu16
,
1066 rule
->sport
.start
, rule
->sport
.end
);
1070 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
1071 fprintf(f
, "%sdestinationport=%"PRIu16
"-%"PRIu16
,
1073 rule
->dport
.start
, rule
->dport
.end
);
1077 fprintf(f
, "%stable=%"PRIu32
"\n",
1085 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
1086 _cleanup_strv_free_
char **l
= NULL
;
1087 _cleanup_free_
char *data
= NULL
;
1088 uint16_t low
= 0, high
= 0;
1096 r
= routing_policy_rule_read_full_file(state_file
, &data
);
1100 l
= strv_split_newlines(data
);
1104 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
1108 STRV_FOREACH(i
, l
) {
1109 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
1111 p
= startswith(*i
, "RULE=");
1115 r
= routing_policy_rule_new(&rule
);
1120 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
1122 r
= extract_first_word(&p
, &word
, NULL
, 0);
1128 r
= split_pair(word
, "=", &a
, &b
);
1132 if (STR_IN_SET(a
, "from", "to")) {
1133 union in_addr_union
*buffer
;
1136 if (streq(a
, "to")) {
1138 prefixlen
= &rule
->to_prefixlen
;
1140 buffer
= &rule
->from
;
1141 prefixlen
= &rule
->from_prefixlen
;
1144 r
= in_addr_prefix_from_string_auto(b
, &rule
->family
, buffer
, prefixlen
);
1146 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
1150 } else if (streq(a
, "family")) {
1151 r
= af_from_name(b
);
1153 log_error_errno(r
, "Failed to parse RPDB rule family, ignoring: %s", b
);
1157 } else if (streq(a
, "tos")) {
1158 r
= safe_atou8(b
, &rule
->tos
);
1160 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
1163 } else if (streq(a
, "table")) {
1164 r
= safe_atou32(b
, &rule
->table
);
1166 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
1169 } else if (streq(a
, "priority")) {
1170 r
= safe_atou32(b
, &rule
->priority
);
1172 log_error_errno(r
, "Failed to parse RPDB rule priority, ignoring: %s", b
);
1175 } else if (streq(a
, "fwmark")) {
1177 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
1179 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
1182 } else if (streq(a
, "iif")) {
1184 if (free_and_strdup(&rule
->iif
, b
) < 0)
1187 } else if (streq(a
, "oif")) {
1189 if (free_and_strdup(&rule
->oif
, b
) < 0)
1191 } else if (streq(a
, "protocol")) {
1192 r
= safe_atou8(b
, &rule
->protocol
);
1194 log_error_errno(r
, "Failed to parse RPDB rule protocol, ignoring: %s", b
);
1197 } else if (streq(a
, "sourceport")) {
1199 r
= parse_ip_port_range(b
, &low
, &high
);
1201 log_error_errno(r
, "Invalid routing policy rule source port range, ignoring assignment:'%s'", b
);
1205 rule
->sport
.start
= low
;
1206 rule
->sport
.end
= high
;
1208 } else if (streq(a
, "destinationport")) {
1210 r
= parse_ip_port_range(b
, &low
, &high
);
1212 log_error_errno(r
, "Invalid routing policy rule destination port range, ignoring assignment:'%s'", b
);
1216 rule
->dport
.start
= low
;
1217 rule
->dport
.end
= high
;
1221 r
= set_put(*rules
, rule
);
1223 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1233 static bool manager_links_have_routing_policy_rule(Manager
*m
, RoutingPolicyRule
*rule
) {
1234 RoutingPolicyRule
*link_rule
;
1241 HASHMAP_FOREACH(link
, m
->links
, i
) {
1245 LIST_FOREACH(rules
, link_rule
, link
->network
->rules
)
1246 if (routing_policy_rule_compare_func(link_rule
, rule
) == 0)
1253 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1254 RoutingPolicyRule
*rule
, *existing
;
1261 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1262 existing
= set_get(m
->rules_foreign
, rule
);
1264 continue; /* Saved rule does not exist anymore. */
1266 if (manager_links_have_routing_policy_rule(m
, existing
))
1267 continue; /* Existing links have the saved rule. */
1269 /* Existing links do not have the saved rule. Let's drop the rule now, and re-configure it
1270 * later when it is requested. */
1272 r
= routing_policy_rule_remove(existing
, link
, NULL
);
1274 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1278 link
->routing_policy_rule_remove_messages
++;
1280 assert_se(set_remove(m
->rules_foreign
, existing
) == existing
);
1281 routing_policy_rule_free(existing
);