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 void *b
, struct siphash
*state
) {
60 const RoutingPolicyRule
*rule
= b
;
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(&rule
->tos
, sizeof(rule
->tos
), state
);
77 siphash24_compress(&rule
->fwmark
, sizeof(rule
->fwmark
), state
);
78 siphash24_compress(&rule
->table
, sizeof(rule
->table
), state
);
80 siphash24_compress(&rule
->protocol
, sizeof(rule
->protocol
), state
);
81 siphash24_compress(&rule
->sport
, sizeof(rule
->sport
), state
);
82 siphash24_compress(&rule
->dport
, sizeof(rule
->dport
), state
);
85 siphash24_compress(rule
->iif
, strlen(rule
->iif
), state
);
88 siphash24_compress(rule
->oif
, strlen(rule
->oif
), state
);
92 /* treat any other address family as AF_UNSPEC */
97 static int routing_policy_rule_compare_func(const void *_a
, const void *_b
) {
98 const RoutingPolicyRule
*a
= _a
, *b
= _b
;
101 r
= CMP(a
->family
, b
->family
);
108 r
= CMP(a
->from_prefixlen
, b
->from_prefixlen
);
112 r
= CMP(a
->to_prefixlen
, b
->to_prefixlen
);
116 r
= CMP(a
->tos
, b
->tos
);
120 r
= CMP(a
->fwmask
, b
->fwmask
);
124 r
= CMP(a
->table
, b
->table
);
128 r
= strcmp_ptr(a
->iif
, b
->iif
);
132 r
= strcmp_ptr(a
->oif
, b
->oif
);
136 r
= CMP(a
->protocol
, b
->protocol
);
140 r
= memcmp(&a
->sport
, &b
->sport
, sizeof(a
->sport
));
144 r
= memcmp(&a
->dport
, &b
->dport
, sizeof(a
->dport
));
148 r
= memcmp(&a
->from
, &b
->from
, FAMILY_ADDRESS_SIZE(a
->family
));
152 return memcmp(&a
->to
, &b
->to
, FAMILY_ADDRESS_SIZE(a
->family
));
155 /* treat any other address family as AF_UNSPEC */
160 const struct hash_ops routing_policy_rule_hash_ops
= {
161 .hash
= routing_policy_rule_hash_func
,
162 .compare
= routing_policy_rule_compare_func
165 int routing_policy_rule_get(Manager
*m
,
167 const union in_addr_union
*from
,
168 uint8_t from_prefixlen
,
169 const union in_addr_union
*to
,
170 uint8_t to_prefixlen
,
177 struct fib_rule_port_range
*sport
,
178 struct fib_rule_port_range
*dport
,
179 RoutingPolicyRule
**ret
) {
181 RoutingPolicyRule rule
, *existing
;
183 assert_return(m
, -1);
185 rule
= (RoutingPolicyRule
) {
188 .from_prefixlen
= from_prefixlen
,
190 .to_prefixlen
= to_prefixlen
,
196 .protocol
= protocol
,
201 existing
= set_get(m
->rules
, &rule
);
208 existing
= set_get(m
->rules_foreign
, &rule
);
218 int routing_policy_rule_make_local(Manager
*m
, RoutingPolicyRule
*rule
) {
223 if (set_contains(m
->rules_foreign
, rule
)) {
224 set_remove(m
->rules_foreign
, rule
);
226 r
= set_ensure_allocated(&m
->rules
, &routing_policy_rule_hash_ops
);
230 return set_put(m
->rules
, rule
);
236 static int routing_policy_rule_add_internal(Manager
*m
,
239 const union in_addr_union
*from
,
240 uint8_t from_prefixlen
,
241 const union in_addr_union
*to
,
242 uint8_t to_prefixlen
,
249 const struct fib_rule_port_range
*sport
,
250 const struct fib_rule_port_range
*dport
,
251 RoutingPolicyRule
**ret
) {
253 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
254 _cleanup_free_
char *iif
= NULL
, *oif
= NULL
;
257 assert_return(rules
, -EINVAL
);
271 r
= routing_policy_rule_new(&rule
);
276 rule
->family
= family
;
278 rule
->from_prefixlen
= from_prefixlen
;
280 rule
->to_prefixlen
= to_prefixlen
;
282 rule
->fwmark
= fwmark
;
286 rule
->protocol
= protocol
;
287 rule
->sport
= *sport
;
288 rule
->dport
= *dport
;
290 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
294 r
= set_put(*rules
, rule
);
307 int routing_policy_rule_add(Manager
*m
,
309 const union in_addr_union
*from
,
310 uint8_t from_prefixlen
,
311 const union in_addr_union
*to
,
312 uint8_t to_prefixlen
,
319 const struct fib_rule_port_range
*sport
,
320 const struct fib_rule_port_range
*dport
,
321 RoutingPolicyRule
**ret
) {
323 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
);
326 int routing_policy_rule_add_foreign(Manager
*m
,
328 const union in_addr_union
*from
,
329 uint8_t from_prefixlen
,
330 const union in_addr_union
*to
,
331 uint8_t to_prefixlen
,
338 const struct fib_rule_port_range
*sport
,
339 const struct fib_rule_port_range
*dport
,
340 RoutingPolicyRule
**ret
) {
341 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
);
344 static int routing_policy_rule_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
349 assert(link
->ifname
);
351 link
->routing_policy_rule_remove_messages
--;
353 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
356 r
= sd_netlink_message_get_errno(m
);
358 log_link_warning_errno(link
, r
, "Could not drop routing policy rule: %m");
363 int routing_policy_rule_remove(RoutingPolicyRule
*routing_policy_rule
, Link
*link
, link_netlink_message_handler_t callback
) {
364 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
367 assert(routing_policy_rule
);
369 assert(link
->manager
);
370 assert(link
->manager
->rtnl
);
371 assert(link
->ifindex
> 0);
372 assert(IN_SET(routing_policy_rule
->family
, AF_INET
, AF_INET6
));
374 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_DELRULE
, routing_policy_rule
->family
);
376 return log_error_errno(r
, "Could not allocate RTM_DELRULE message: %m");
378 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->from
)) {
379 if (routing_policy_rule
->family
== AF_INET
)
380 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in
);
382 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in6
);
385 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
387 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, routing_policy_rule
->from_prefixlen
);
389 return log_error_errno(r
, "Could not set source prefix length: %m");
392 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->to
)) {
393 if (routing_policy_rule
->family
== AF_INET
)
394 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in
);
396 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in6
);
399 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
401 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, routing_policy_rule
->to_prefixlen
);
403 return log_error_errno(r
, "Could not set destination prefix length: %m");
406 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
,
407 callback
?: routing_policy_rule_remove_handler
,
408 link_netlink_destroy_callback
, link
);
410 return log_error_errno(r
, "Could not send rtnetlink message: %m");
417 static int routing_policy_rule_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutingPolicyRule
**ret
) {
418 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
419 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
424 assert(!!filename
== (section_line
> 0));
427 r
= network_config_section_new(filename
, section_line
, &n
);
431 rule
= hashmap_get(network
->rules_by_section
, n
);
433 *ret
= TAKE_PTR(rule
);
439 r
= routing_policy_rule_new(&rule
);
443 rule
->network
= network
;
444 LIST_APPEND(rules
, network
->rules
, rule
);
448 rule
->section
= TAKE_PTR(n
);
450 r
= hashmap_ensure_allocated(&network
->rules_by_section
, &network_config_hash_ops
);
454 r
= hashmap_put(network
->rules_by_section
, rule
->section
, rule
);
459 *ret
= TAKE_PTR(rule
);
464 static int routing_policy_rule_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
470 assert(link
->ifname
);
471 assert(link
->routing_policy_rule_messages
> 0);
473 link
->routing_policy_rule_messages
--;
475 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
478 r
= sd_netlink_message_get_errno(m
);
479 if (r
< 0 && r
!= -EEXIST
)
480 log_link_warning_errno(link
, r
, "Could not add routing policy rule: %m");
482 if (link
->routing_policy_rule_messages
== 0) {
483 log_link_debug(link
, "Routing policy rule configured");
484 link
->routing_policy_rules_configured
= true;
485 link_check_ready(link
);
491 int routing_policy_rule_configure(RoutingPolicyRule
*rule
, Link
*link
, link_netlink_message_handler_t callback
, bool update
) {
492 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
497 assert(link
->ifindex
> 0);
498 assert(link
->manager
);
499 assert(link
->manager
->rtnl
);
501 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
503 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
505 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
506 if (rule
->family
== AF_INET
)
507 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &rule
->from
.in
);
509 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &rule
->from
.in6
);
512 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
514 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
516 return log_error_errno(r
, "Could not set source prefix length: %m");
519 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
520 if (rule
->family
== AF_INET
)
521 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &rule
->to
.in
);
523 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &rule
->to
.in6
);
526 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
528 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
530 return log_error_errno(r
, "Could not set destination prefix length: %m");
533 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
535 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
538 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
540 return log_error_errno(r
, "Could not set ip rule tos: %m");
543 if (rule
->table
< 256) {
544 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
546 return log_error_errno(r
, "Could not set ip rule table: %m");
548 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
550 return log_error_errno(r
, "Could not set ip rule table: %m");
552 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
554 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
557 if (rule
->fwmark
> 0) {
558 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
560 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
563 if (rule
->fwmask
> 0) {
564 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
566 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
570 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
572 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
576 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
578 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
581 r
= sd_netlink_message_append_u8(m
, FRA_IP_PROTO
, rule
->protocol
);
583 return log_error_errno(r
, "Could not append FRA_IP_PROTO attribute: %m");
585 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
586 r
= sd_netlink_message_append_data(m
, FRA_SPORT_RANGE
, &rule
->sport
, sizeof(rule
->sport
));
588 return log_error_errno(r
, "Could not append FRA_SPORT_RANGE attribute: %m");
591 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
592 r
= sd_netlink_message_append_data(m
, FRA_DPORT_RANGE
, &rule
->dport
, sizeof(rule
->dport
));
594 return log_error_errno(r
, "Could not append FRA_DPORT_RANGE attribute: %m");
599 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
,
600 callback
?: routing_policy_rule_handler
,
601 link_netlink_destroy_callback
, link
);
603 return log_error_errno(r
, "Could not send rtnetlink message: %m");
607 r
= routing_policy_rule_add(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
608 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, rule
->protocol
, &rule
->sport
, &rule
->dport
, NULL
);
610 return log_error_errno(r
, "Could not add rule: %m");
615 static int parse_fwmark_fwmask(const char *s
, uint32_t *fwmark
, uint32_t *fwmask
) {
616 _cleanup_free_
char *f
= NULL
;
630 r
= safe_atou32(f
, fwmark
);
632 return log_error_errno(r
, "Failed to parse RPDB rule firewall mark, ignoring: %s", f
);
635 r
= safe_atou32(p
, fwmask
);
637 return log_error_errno(r
, "Failed to parse RPDB rule mask, ignoring: %s", f
);
643 int config_parse_routing_policy_rule_tos(
645 const char *filename
,
648 unsigned section_line
,
655 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
656 Network
*network
= userdata
;
665 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
669 r
= safe_atou8(rvalue
, &n
->tos
);
671 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule tos, ignoring: %s", rvalue
);
680 int config_parse_routing_policy_rule_priority(
682 const char *filename
,
685 unsigned section_line
,
692 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
693 Network
*network
= userdata
;
702 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
706 r
= safe_atou32(rvalue
, &n
->priority
);
708 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule priority, ignoring: %s", rvalue
);
717 int config_parse_routing_policy_rule_table(
719 const char *filename
,
722 unsigned section_line
,
729 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
730 Network
*network
= userdata
;
739 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
743 r
= safe_atou32(rvalue
, &n
->table
);
745 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule table, ignoring: %s", rvalue
);
754 int config_parse_routing_policy_rule_fwmark_mask(
756 const char *filename
,
759 unsigned section_line
,
766 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
767 Network
*network
= userdata
;
776 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
780 r
= parse_fwmark_fwmask(rvalue
, &n
->fwmark
, &n
->fwmask
);
782 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue
);
791 int config_parse_routing_policy_rule_prefix(
793 const char *filename
,
796 unsigned section_line
,
803 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
804 Network
*network
= userdata
;
805 union in_addr_union
*buffer
;
815 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
819 if (streq(lvalue
, "To")) {
821 prefixlen
= &n
->to_prefixlen
;
824 prefixlen
= &n
->from_prefixlen
;
827 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
829 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
838 int config_parse_routing_policy_rule_device(
840 const char *filename
,
843 unsigned section_line
,
850 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
851 Network
*network
= userdata
;
860 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
864 if (!ifname_valid(rvalue
)) {
865 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse '%s' interface name, ignoring: %s", lvalue
, rvalue
);
869 if (streq(lvalue
, "IncomingInterface")) {
870 r
= free_and_strdup(&n
->iif
, rvalue
);
874 r
= free_and_strdup(&n
->oif
, rvalue
);
884 int config_parse_routing_policy_rule_port_range(
886 const char *filename
,
889 unsigned section_line
,
895 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
896 Network
*network
= userdata
;
906 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
910 r
= parse_ip_port_range(rvalue
, &low
, &high
);
912 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse routing policy rule port range '%s'", rvalue
);
916 if (streq(lvalue
, "SourcePort")) {
917 n
->sport
.start
= low
;
920 n
->dport
.start
= low
;
929 int config_parse_routing_policy_rule_ip_protocol(
931 const char *filename
,
934 unsigned section_line
,
941 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
942 Network
*network
= userdata
;
951 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
955 r
= parse_ip_protocol(rvalue
);
957 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse IP protocol '%s' for routing policy rule, ignoring: %m", rvalue
);
968 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
969 _cleanup_free_
char *s
= NULL
;
975 r
= read_full_file(state_file
, &s
, &size
);
988 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
989 RoutingPolicyRule
*rule
= NULL
;
995 SET_FOREACH(rule
, rules
, i
) {
996 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
1001 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
1002 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
1006 fprintf(f
, "from=%s/%hhu",
1007 from_str
, rule
->from_prefixlen
);
1011 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
1012 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
1016 fprintf(f
, "%sto=%s/%hhu",
1018 to_str
, rule
->to_prefixlen
);
1022 if (rule
->tos
!= 0) {
1023 fprintf(f
, "%stos=%hhu",
1029 if (rule
->fwmark
!= 0) {
1030 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
1032 rule
->fwmark
, rule
->fwmask
);
1037 fprintf(f
, "%siif=%s",
1044 fprintf(f
, "%soif=%s",
1050 if (rule
->protocol
!= 0) {
1051 fprintf(f
, "%sprotocol=%hhu",
1057 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
1058 fprintf(f
, "%ssourcesport=%"PRIu16
"-%"PRIu16
,
1060 rule
->sport
.start
, rule
->sport
.end
);
1064 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
1065 fprintf(f
, "%sdestinationport=%"PRIu16
"-%"PRIu16
,
1067 rule
->dport
.start
, rule
->dport
.end
);
1071 fprintf(f
, "%stable=%"PRIu32
"\n",
1079 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
1080 _cleanup_strv_free_
char **l
= NULL
;
1081 _cleanup_free_
char *data
= NULL
;
1082 uint16_t low
= 0, high
= 0;
1090 r
= routing_policy_rule_read_full_file(state_file
, &data
);
1094 l
= strv_split_newlines(data
);
1098 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
1102 STRV_FOREACH(i
, l
) {
1103 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
1105 p
= startswith(*i
, "RULE=");
1109 r
= routing_policy_rule_new(&rule
);
1114 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
1116 r
= extract_first_word(&p
, &word
, NULL
, 0);
1122 r
= split_pair(word
, "=", &a
, &b
);
1126 if (STR_IN_SET(a
, "from", "to")) {
1127 union in_addr_union
*buffer
;
1130 if (streq(a
, "to")) {
1132 prefixlen
= &rule
->to_prefixlen
;
1134 buffer
= &rule
->from
;
1135 prefixlen
= &rule
->from_prefixlen
;
1138 r
= in_addr_prefix_from_string_auto(b
, &rule
->family
, buffer
, prefixlen
);
1140 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
1144 } else if (streq(a
, "tos")) {
1145 r
= safe_atou8(b
, &rule
->tos
);
1147 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
1150 } else if (streq(a
, "table")) {
1151 r
= safe_atou32(b
, &rule
->table
);
1153 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
1156 } else if (streq(a
, "fwmark")) {
1158 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
1160 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
1163 } else if (streq(a
, "iif")) {
1165 if (free_and_strdup(&rule
->iif
, b
) < 0)
1168 } else if (streq(a
, "oif")) {
1170 if (free_and_strdup(&rule
->oif
, b
) < 0)
1172 } else if (streq(a
, "protocol")) {
1173 r
= safe_atou8(b
, &rule
->protocol
);
1175 log_error_errno(r
, "Failed to parse RPDB rule protocol, ignoring: %s", b
);
1178 } else if (streq(a
, "sourceport")) {
1180 r
= parse_ip_port_range(b
, &low
, &high
);
1182 log_error_errno(r
, "Invalid routing policy rule source port range, ignoring assignment:'%s'", b
);
1186 rule
->sport
.start
= low
;
1187 rule
->sport
.end
= high
;
1189 } else if (streq(a
, "destinationport")) {
1191 r
= parse_ip_port_range(b
, &low
, &high
);
1193 log_error_errno(r
, "Invalid routing policy rule destination port range, ignoring assignment:'%s'", b
);
1197 rule
->dport
.start
= low
;
1198 rule
->dport
.end
= high
;
1202 r
= set_put(*rules
, rule
);
1204 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1214 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1215 RoutingPolicyRule
*rule
, *existing
;
1222 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1223 existing
= set_get(m
->rules_foreign
, rule
);
1226 r
= routing_policy_rule_remove(rule
, link
, NULL
);
1228 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1232 link
->routing_policy_rule_remove_messages
++;