1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/fib_rules.h>
6 #include "alloc-util.h"
7 #include "conf-parser.h"
9 #include "ip-protocol-list.h"
10 #include "networkd-routing-policy-rule.h"
11 #include "netlink-util.h"
12 #include "networkd-manager.h"
13 #include "parse-util.h"
14 #include "socket-util.h"
15 #include "string-util.h"
18 int routing_policy_rule_new(RoutingPolicyRule
**ret
) {
19 RoutingPolicyRule
*rule
;
21 rule
= new(RoutingPolicyRule
, 1);
25 *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 set_remove(rule
->manager
->rules
, rule
);
50 set_remove(rule
->manager
->rules_foreign
, rule
);
53 network_config_section_free(rule
->section
);
59 static void routing_policy_rule_hash_func(const RoutingPolicyRule
*rule
, struct siphash
*state
) {
62 siphash24_compress(&rule
->family
, sizeof(rule
->family
), state
);
64 switch (rule
->family
) {
68 siphash24_compress(&rule
->from
, FAMILY_ADDRESS_SIZE(rule
->family
), state
);
69 siphash24_compress(&rule
->from_prefixlen
, sizeof(rule
->from_prefixlen
), state
);
71 siphash24_compress(&rule
->to
, FAMILY_ADDRESS_SIZE(rule
->family
), state
);
72 siphash24_compress(&rule
->to_prefixlen
, sizeof(rule
->to_prefixlen
), state
);
74 siphash24_compress(&rule
->tos
, sizeof(rule
->tos
), state
);
75 siphash24_compress(&rule
->fwmark
, sizeof(rule
->fwmark
), state
);
76 siphash24_compress(&rule
->table
, sizeof(rule
->table
), state
);
78 siphash24_compress(&rule
->protocol
, sizeof(rule
->protocol
), state
);
79 siphash24_compress(&rule
->sport
, sizeof(rule
->sport
), state
);
80 siphash24_compress(&rule
->dport
, sizeof(rule
->dport
), state
);
83 siphash24_compress(rule
->iif
, strlen(rule
->iif
), state
);
86 siphash24_compress(rule
->oif
, strlen(rule
->oif
), state
);
90 /* treat any other address family as AF_UNSPEC */
95 static int routing_policy_rule_compare_func(const RoutingPolicyRule
*a
, const RoutingPolicyRule
*b
) {
98 r
= CMP(a
->family
, b
->family
);
105 r
= CMP(a
->from_prefixlen
, b
->from_prefixlen
);
109 r
= CMP(a
->to_prefixlen
, b
->to_prefixlen
);
113 r
= CMP(a
->tos
, b
->tos
);
117 r
= CMP(a
->fwmask
, b
->fwmask
);
121 r
= CMP(a
->table
, b
->table
);
125 r
= strcmp_ptr(a
->iif
, b
->iif
);
129 r
= strcmp_ptr(a
->oif
, b
->oif
);
133 r
= CMP(a
->protocol
, b
->protocol
);
137 r
= memcmp(&a
->sport
, &b
->sport
, sizeof(a
->sport
));
141 r
= memcmp(&a
->dport
, &b
->dport
, sizeof(a
->dport
));
145 r
= memcmp(&a
->from
, &b
->from
, FAMILY_ADDRESS_SIZE(a
->family
));
149 return memcmp(&a
->to
, &b
->to
, FAMILY_ADDRESS_SIZE(a
->family
));
152 /* treat any other address family as AF_UNSPEC */
157 DEFINE_PRIVATE_HASH_OPS(routing_policy_rule_hash_ops
, RoutingPolicyRule
, routing_policy_rule_hash_func
, routing_policy_rule_compare_func
);
159 int routing_policy_rule_get(Manager
*m
,
161 const union in_addr_union
*from
,
162 uint8_t from_prefixlen
,
163 const union in_addr_union
*to
,
164 uint8_t to_prefixlen
,
171 struct fib_rule_port_range
*sport
,
172 struct fib_rule_port_range
*dport
,
173 RoutingPolicyRule
**ret
) {
175 RoutingPolicyRule rule
, *existing
;
177 assert_return(m
, -1);
179 rule
= (RoutingPolicyRule
) {
182 .from_prefixlen
= from_prefixlen
,
184 .to_prefixlen
= to_prefixlen
,
190 .protocol
= protocol
,
195 existing
= set_get(m
->rules
, &rule
);
202 existing
= set_get(m
->rules_foreign
, &rule
);
212 int routing_policy_rule_make_local(Manager
*m
, RoutingPolicyRule
*rule
) {
217 if (set_contains(m
->rules_foreign
, rule
)) {
218 set_remove(m
->rules_foreign
, rule
);
220 r
= set_ensure_allocated(&m
->rules
, &routing_policy_rule_hash_ops
);
224 return set_put(m
->rules
, rule
);
230 static int routing_policy_rule_add_internal(Manager
*m
,
233 const union in_addr_union
*from
,
234 uint8_t from_prefixlen
,
235 const union in_addr_union
*to
,
236 uint8_t to_prefixlen
,
243 const struct fib_rule_port_range
*sport
,
244 const struct fib_rule_port_range
*dport
,
245 RoutingPolicyRule
**ret
) {
247 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
248 _cleanup_free_
char *iif
= NULL
, *oif
= NULL
;
251 assert_return(rules
, -EINVAL
);
265 r
= routing_policy_rule_new(&rule
);
270 rule
->family
= family
;
272 rule
->from_prefixlen
= from_prefixlen
;
274 rule
->to_prefixlen
= to_prefixlen
;
276 rule
->fwmark
= fwmark
;
278 rule
->iif
= TAKE_PTR(iif
);
279 rule
->oif
= TAKE_PTR(oif
);
280 rule
->protocol
= protocol
;
281 rule
->sport
= *sport
;
282 rule
->dport
= *dport
;
284 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
288 r
= set_put(*rules
, rule
);
299 int routing_policy_rule_add(Manager
*m
,
301 const union in_addr_union
*from
,
302 uint8_t from_prefixlen
,
303 const union in_addr_union
*to
,
304 uint8_t to_prefixlen
,
311 const struct fib_rule_port_range
*sport
,
312 const struct fib_rule_port_range
*dport
,
313 RoutingPolicyRule
**ret
) {
315 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
);
318 int routing_policy_rule_add_foreign(Manager
*m
,
320 const union in_addr_union
*from
,
321 uint8_t from_prefixlen
,
322 const union in_addr_union
*to
,
323 uint8_t to_prefixlen
,
330 const struct fib_rule_port_range
*sport
,
331 const struct fib_rule_port_range
*dport
,
332 RoutingPolicyRule
**ret
) {
333 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
);
336 static int routing_policy_rule_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
341 assert(link
->ifname
);
343 link
->routing_policy_rule_remove_messages
--;
345 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
348 r
= sd_netlink_message_get_errno(m
);
350 log_link_warning_errno(link
, r
, "Could not drop routing policy rule: %m");
355 int routing_policy_rule_remove(RoutingPolicyRule
*routing_policy_rule
, Link
*link
, link_netlink_message_handler_t callback
) {
356 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
359 assert(routing_policy_rule
);
361 assert(link
->manager
);
362 assert(link
->manager
->rtnl
);
363 assert(link
->ifindex
> 0);
364 assert(IN_SET(routing_policy_rule
->family
, AF_INET
, AF_INET6
));
366 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_DELRULE
, routing_policy_rule
->family
);
368 return log_error_errno(r
, "Could not allocate RTM_DELRULE message: %m");
370 if (in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->from
) == 0) {
371 r
= netlink_message_append_in_addr_union(m
, FRA_SRC
, routing_policy_rule
->family
, &routing_policy_rule
->from
);
373 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
375 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, routing_policy_rule
->from_prefixlen
);
377 return log_error_errno(r
, "Could not set source prefix length: %m");
380 if (in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->to
) == 0) {
381 r
= netlink_message_append_in_addr_union(m
, FRA_DST
, routing_policy_rule
->family
, &routing_policy_rule
->to
);
383 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
385 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, routing_policy_rule
->to_prefixlen
);
387 return log_error_errno(r
, "Could not set destination prefix length: %m");
390 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
,
391 callback
?: routing_policy_rule_remove_handler
,
392 link_netlink_destroy_callback
, link
);
394 return log_error_errno(r
, "Could not send rtnetlink message: %m");
401 static int routing_policy_rule_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutingPolicyRule
**ret
) {
402 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
403 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
408 assert(!!filename
== (section_line
> 0));
411 r
= network_config_section_new(filename
, section_line
, &n
);
415 rule
= hashmap_get(network
->rules_by_section
, n
);
417 *ret
= TAKE_PTR(rule
);
423 r
= routing_policy_rule_new(&rule
);
427 rule
->network
= network
;
428 LIST_APPEND(rules
, network
->rules
, rule
);
432 rule
->section
= TAKE_PTR(n
);
434 r
= hashmap_ensure_allocated(&network
->rules_by_section
, &network_config_hash_ops
);
438 r
= hashmap_put(network
->rules_by_section
, rule
->section
, rule
);
443 *ret
= TAKE_PTR(rule
);
448 static int routing_policy_rule_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
454 assert(link
->ifname
);
455 assert(link
->routing_policy_rule_messages
> 0);
457 link
->routing_policy_rule_messages
--;
459 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
462 r
= sd_netlink_message_get_errno(m
);
463 if (r
< 0 && r
!= -EEXIST
)
464 log_link_warning_errno(link
, r
, "Could not add routing policy rule: %m");
466 if (link
->routing_policy_rule_messages
== 0) {
467 log_link_debug(link
, "Routing policy rule configured");
468 link
->routing_policy_rules_configured
= true;
469 link_check_ready(link
);
475 int routing_policy_rule_configure(RoutingPolicyRule
*rule
, Link
*link
, link_netlink_message_handler_t callback
, bool update
) {
476 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
481 assert(link
->ifindex
> 0);
482 assert(link
->manager
);
483 assert(link
->manager
->rtnl
);
485 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
487 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
489 if (in_addr_is_null(rule
->family
, &rule
->from
) == 0) {
490 r
= netlink_message_append_in_addr_union(m
, FRA_SRC
, rule
->family
, &rule
->from
);
492 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
494 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
496 return log_error_errno(r
, "Could not set source prefix length: %m");
499 if (in_addr_is_null(rule
->family
, &rule
->to
) == 0) {
500 r
= netlink_message_append_in_addr_union(m
, FRA_DST
, rule
->family
, &rule
->to
);
502 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
504 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
506 return log_error_errno(r
, "Could not set destination prefix length: %m");
509 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
511 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
514 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
516 return log_error_errno(r
, "Could not set ip rule tos: %m");
519 if (rule
->table
< 256) {
520 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
522 return log_error_errno(r
, "Could not set ip rule table: %m");
524 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
526 return log_error_errno(r
, "Could not set ip rule table: %m");
528 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
530 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
533 if (rule
->fwmark
> 0) {
534 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
536 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
539 if (rule
->fwmask
> 0) {
540 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
542 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
546 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
548 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
552 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
554 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
557 r
= sd_netlink_message_append_u8(m
, FRA_IP_PROTO
, rule
->protocol
);
559 return log_error_errno(r
, "Could not append FRA_IP_PROTO attribute: %m");
561 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
562 r
= sd_netlink_message_append_data(m
, FRA_SPORT_RANGE
, &rule
->sport
, sizeof(rule
->sport
));
564 return log_error_errno(r
, "Could not append FRA_SPORT_RANGE attribute: %m");
567 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
568 r
= sd_netlink_message_append_data(m
, FRA_DPORT_RANGE
, &rule
->dport
, sizeof(rule
->dport
));
570 return log_error_errno(r
, "Could not append FRA_DPORT_RANGE attribute: %m");
573 if (rule
->invert_rule
) {
574 r
= sd_rtnl_message_routing_policy_rule_set_flags(m
, FIB_RULE_INVERT
);
576 return log_error_errno(r
, "Could not append FIB_RULE_INVERT attribute: %m");
581 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
,
582 callback
?: routing_policy_rule_handler
,
583 link_netlink_destroy_callback
, link
);
585 return log_error_errno(r
, "Could not send rtnetlink message: %m");
589 r
= routing_policy_rule_add(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
590 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, rule
->protocol
, &rule
->sport
, &rule
->dport
, NULL
);
592 return log_error_errno(r
, "Could not add rule: %m");
597 static int parse_fwmark_fwmask(const char *s
, uint32_t *fwmark
, uint32_t *fwmask
) {
598 _cleanup_free_
char *f
= NULL
;
612 r
= safe_atou32(f
, fwmark
);
614 return log_error_errno(r
, "Failed to parse RPDB rule firewall mark, ignoring: %s", f
);
617 r
= safe_atou32(p
, fwmask
);
619 return log_error_errno(r
, "Failed to parse RPDB rule mask, ignoring: %s", f
);
625 int config_parse_routing_policy_rule_tos(
627 const char *filename
,
630 unsigned section_line
,
637 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
638 Network
*network
= userdata
;
647 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
651 r
= safe_atou8(rvalue
, &n
->tos
);
653 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule tos, ignoring: %s", rvalue
);
662 int config_parse_routing_policy_rule_priority(
664 const char *filename
,
667 unsigned section_line
,
674 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
675 Network
*network
= userdata
;
684 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
688 r
= safe_atou32(rvalue
, &n
->priority
);
690 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule priority, ignoring: %s", rvalue
);
699 int config_parse_routing_policy_rule_table(
701 const char *filename
,
704 unsigned section_line
,
711 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
712 Network
*network
= userdata
;
721 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
725 r
= safe_atou32(rvalue
, &n
->table
);
727 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule table, ignoring: %s", rvalue
);
736 int config_parse_routing_policy_rule_fwmark_mask(
738 const char *filename
,
741 unsigned section_line
,
748 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
749 Network
*network
= userdata
;
758 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
762 r
= parse_fwmark_fwmask(rvalue
, &n
->fwmark
, &n
->fwmask
);
764 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue
);
773 int config_parse_routing_policy_rule_prefix(
775 const char *filename
,
778 unsigned section_line
,
785 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
786 Network
*network
= userdata
;
787 union in_addr_union
*buffer
;
797 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
801 if (streq(lvalue
, "To")) {
803 prefixlen
= &n
->to_prefixlen
;
806 prefixlen
= &n
->from_prefixlen
;
809 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
811 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
820 int config_parse_routing_policy_rule_device(
822 const char *filename
,
825 unsigned section_line
,
832 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
833 Network
*network
= userdata
;
842 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
846 if (!ifname_valid(rvalue
)) {
847 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse '%s' interface name, ignoring: %s", lvalue
, rvalue
);
851 if (streq(lvalue
, "IncomingInterface")) {
852 r
= free_and_strdup(&n
->iif
, rvalue
);
856 r
= free_and_strdup(&n
->oif
, rvalue
);
866 int config_parse_routing_policy_rule_port_range(
868 const char *filename
,
871 unsigned section_line
,
877 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
878 Network
*network
= userdata
;
888 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
892 r
= parse_ip_port_range(rvalue
, &low
, &high
);
894 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse routing policy rule port range '%s'", rvalue
);
898 if (streq(lvalue
, "SourcePort")) {
899 n
->sport
.start
= low
;
902 n
->dport
.start
= low
;
911 int config_parse_routing_policy_rule_ip_protocol(
913 const char *filename
,
916 unsigned section_line
,
923 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
924 Network
*network
= userdata
;
933 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
937 r
= parse_ip_protocol(rvalue
);
939 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse IP protocol '%s' for routing policy rule, ignoring: %m", rvalue
);
950 int config_parse_routing_policy_rule_invert(
952 const char *filename
,
955 unsigned section_line
,
962 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
963 Network
*network
= userdata
;
972 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
976 r
= parse_boolean(rvalue
);
978 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule invert, ignoring: %s", rvalue
);
989 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
990 _cleanup_free_
char *s
= NULL
;
996 r
= read_full_file(state_file
, &s
, &size
);
1009 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
1010 RoutingPolicyRule
*rule
= NULL
;
1016 SET_FOREACH(rule
, rules
, i
) {
1017 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
1022 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
1023 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
1027 fprintf(f
, "from=%s/%hhu",
1028 from_str
, rule
->from_prefixlen
);
1032 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
1033 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
1037 fprintf(f
, "%sto=%s/%hhu",
1039 to_str
, rule
->to_prefixlen
);
1043 if (rule
->tos
!= 0) {
1044 fprintf(f
, "%stos=%hhu",
1050 if (rule
->fwmark
!= 0) {
1051 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
1053 rule
->fwmark
, rule
->fwmask
);
1058 fprintf(f
, "%siif=%s",
1065 fprintf(f
, "%soif=%s",
1071 if (rule
->protocol
!= 0) {
1072 fprintf(f
, "%sprotocol=%hhu",
1078 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
1079 fprintf(f
, "%ssourcesport=%"PRIu16
"-%"PRIu16
,
1081 rule
->sport
.start
, rule
->sport
.end
);
1085 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
1086 fprintf(f
, "%sdestinationport=%"PRIu16
"-%"PRIu16
,
1088 rule
->dport
.start
, rule
->dport
.end
);
1092 fprintf(f
, "%stable=%"PRIu32
"\n",
1100 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
1101 _cleanup_strv_free_
char **l
= NULL
;
1102 _cleanup_free_
char *data
= NULL
;
1103 uint16_t low
= 0, high
= 0;
1111 r
= routing_policy_rule_read_full_file(state_file
, &data
);
1115 l
= strv_split_newlines(data
);
1119 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
1123 STRV_FOREACH(i
, l
) {
1124 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
1126 p
= startswith(*i
, "RULE=");
1130 r
= routing_policy_rule_new(&rule
);
1135 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
1137 r
= extract_first_word(&p
, &word
, NULL
, 0);
1143 r
= split_pair(word
, "=", &a
, &b
);
1147 if (STR_IN_SET(a
, "from", "to")) {
1148 union in_addr_union
*buffer
;
1151 if (streq(a
, "to")) {
1153 prefixlen
= &rule
->to_prefixlen
;
1155 buffer
= &rule
->from
;
1156 prefixlen
= &rule
->from_prefixlen
;
1159 r
= in_addr_prefix_from_string_auto(b
, &rule
->family
, buffer
, prefixlen
);
1161 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
1165 } else if (streq(a
, "tos")) {
1166 r
= safe_atou8(b
, &rule
->tos
);
1168 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
1171 } else if (streq(a
, "table")) {
1172 r
= safe_atou32(b
, &rule
->table
);
1174 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
1177 } else if (streq(a
, "fwmark")) {
1179 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
1181 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
1184 } else if (streq(a
, "iif")) {
1186 if (free_and_strdup(&rule
->iif
, b
) < 0)
1189 } else if (streq(a
, "oif")) {
1191 if (free_and_strdup(&rule
->oif
, b
) < 0)
1193 } else if (streq(a
, "protocol")) {
1194 r
= safe_atou8(b
, &rule
->protocol
);
1196 log_error_errno(r
, "Failed to parse RPDB rule protocol, ignoring: %s", b
);
1199 } else if (streq(a
, "sourceport")) {
1201 r
= parse_ip_port_range(b
, &low
, &high
);
1203 log_error_errno(r
, "Invalid routing policy rule source port range, ignoring assignment:'%s'", b
);
1207 rule
->sport
.start
= low
;
1208 rule
->sport
.end
= high
;
1210 } else if (streq(a
, "destinationport")) {
1212 r
= parse_ip_port_range(b
, &low
, &high
);
1214 log_error_errno(r
, "Invalid routing policy rule destination port range, ignoring assignment:'%s'", b
);
1218 rule
->dport
.start
= low
;
1219 rule
->dport
.end
= high
;
1223 r
= set_put(*rules
, rule
);
1225 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1235 static bool manager_links_have_routing_policy_rule(Manager
*m
, RoutingPolicyRule
*rule
) {
1236 RoutingPolicyRule
*link_rule
;
1243 HASHMAP_FOREACH(link
, m
->links
, i
) {
1247 LIST_FOREACH(rules
, link_rule
, link
->network
->rules
)
1248 if (routing_policy_rule_compare_func(link_rule
, rule
) == 0)
1255 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1256 RoutingPolicyRule
*rule
, *existing
;
1263 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1264 existing
= set_get(m
->rules_foreign
, rule
);
1266 continue; /* Saved rule does not exist anymore. */
1268 if (manager_links_have_routing_policy_rule(m
, existing
))
1269 continue; /* Existing links have the saved rule. */
1271 /* Existing links do not have the saved rule. Let's drop the rule now, and re-configure it
1272 * later when it is requested. */
1274 r
= routing_policy_rule_remove(existing
, link
, NULL
);
1276 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1280 link
->routing_policy_rule_remove_messages
++;
1282 assert_se(set_remove(m
->rules_foreign
, existing
) == existing
);
1283 routing_policy_rule_free(existing
);