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
;
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
);
301 int routing_policy_rule_add(Manager
*m
,
303 const union in_addr_union
*from
,
304 uint8_t from_prefixlen
,
305 const union in_addr_union
*to
,
306 uint8_t to_prefixlen
,
313 const struct fib_rule_port_range
*sport
,
314 const struct fib_rule_port_range
*dport
,
315 RoutingPolicyRule
**ret
) {
317 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
);
320 int routing_policy_rule_add_foreign(Manager
*m
,
322 const union in_addr_union
*from
,
323 uint8_t from_prefixlen
,
324 const union in_addr_union
*to
,
325 uint8_t to_prefixlen
,
332 const struct fib_rule_port_range
*sport
,
333 const struct fib_rule_port_range
*dport
,
334 RoutingPolicyRule
**ret
) {
335 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
);
338 static int routing_policy_rule_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
343 assert(link
->ifname
);
345 link
->routing_policy_rule_remove_messages
--;
347 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
350 r
= sd_netlink_message_get_errno(m
);
352 log_link_warning_errno(link
, r
, "Could not drop routing policy rule: %m");
357 int routing_policy_rule_remove(RoutingPolicyRule
*routing_policy_rule
, Link
*link
, link_netlink_message_handler_t callback
) {
358 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
361 assert(routing_policy_rule
);
363 assert(link
->manager
);
364 assert(link
->manager
->rtnl
);
365 assert(link
->ifindex
> 0);
366 assert(IN_SET(routing_policy_rule
->family
, AF_INET
, AF_INET6
));
368 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_DELRULE
, routing_policy_rule
->family
);
370 return log_error_errno(r
, "Could not allocate RTM_DELRULE message: %m");
372 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->from
)) {
373 if (routing_policy_rule
->family
== AF_INET
)
374 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in
);
376 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in6
);
379 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
381 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, routing_policy_rule
->from_prefixlen
);
383 return log_error_errno(r
, "Could not set source prefix length: %m");
386 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->to
)) {
387 if (routing_policy_rule
->family
== AF_INET
)
388 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in
);
390 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in6
);
393 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
395 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, routing_policy_rule
->to_prefixlen
);
397 return log_error_errno(r
, "Could not set destination prefix length: %m");
400 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
,
401 callback
?: routing_policy_rule_remove_handler
,
402 link_netlink_destroy_callback
, link
);
404 return log_error_errno(r
, "Could not send rtnetlink message: %m");
411 static int routing_policy_rule_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutingPolicyRule
**ret
) {
412 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
413 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
418 assert(!!filename
== (section_line
> 0));
421 r
= network_config_section_new(filename
, section_line
, &n
);
425 rule
= hashmap_get(network
->rules_by_section
, n
);
427 *ret
= TAKE_PTR(rule
);
433 r
= routing_policy_rule_new(&rule
);
437 rule
->network
= network
;
438 LIST_APPEND(rules
, network
->rules
, rule
);
442 rule
->section
= TAKE_PTR(n
);
444 r
= hashmap_ensure_allocated(&network
->rules_by_section
, &network_config_hash_ops
);
448 r
= hashmap_put(network
->rules_by_section
, rule
->section
, rule
);
453 *ret
= TAKE_PTR(rule
);
458 static int routing_policy_rule_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
464 assert(link
->ifname
);
465 assert(link
->routing_policy_rule_messages
> 0);
467 link
->routing_policy_rule_messages
--;
469 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
472 r
= sd_netlink_message_get_errno(m
);
473 if (r
< 0 && r
!= -EEXIST
)
474 log_link_warning_errno(link
, r
, "Could not add routing policy rule: %m");
476 if (link
->routing_policy_rule_messages
== 0) {
477 log_link_debug(link
, "Routing policy rule configured");
478 link
->routing_policy_rules_configured
= true;
479 link_check_ready(link
);
485 int routing_policy_rule_configure(RoutingPolicyRule
*rule
, Link
*link
, link_netlink_message_handler_t callback
, bool update
) {
486 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
491 assert(link
->ifindex
> 0);
492 assert(link
->manager
);
493 assert(link
->manager
->rtnl
);
495 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
497 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
499 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
500 if (rule
->family
== AF_INET
)
501 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &rule
->from
.in
);
503 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &rule
->from
.in6
);
506 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
508 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
510 return log_error_errno(r
, "Could not set source prefix length: %m");
513 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
514 if (rule
->family
== AF_INET
)
515 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &rule
->to
.in
);
517 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &rule
->to
.in6
);
520 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
522 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
524 return log_error_errno(r
, "Could not set destination prefix length: %m");
527 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
529 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
532 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
534 return log_error_errno(r
, "Could not set ip rule tos: %m");
537 if (rule
->table
< 256) {
538 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
540 return log_error_errno(r
, "Could not set ip rule table: %m");
542 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
544 return log_error_errno(r
, "Could not set ip rule table: %m");
546 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
548 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
551 if (rule
->fwmark
> 0) {
552 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
554 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
557 if (rule
->fwmask
> 0) {
558 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
560 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
564 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
566 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
570 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
572 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
575 r
= sd_netlink_message_append_u8(m
, FRA_IP_PROTO
, rule
->protocol
);
577 return log_error_errno(r
, "Could not append FRA_IP_PROTO attribute: %m");
579 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
580 r
= sd_netlink_message_append_data(m
, FRA_SPORT_RANGE
, &rule
->sport
, sizeof(rule
->sport
));
582 return log_error_errno(r
, "Could not append FRA_SPORT_RANGE attribute: %m");
585 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
586 r
= sd_netlink_message_append_data(m
, FRA_DPORT_RANGE
, &rule
->dport
, sizeof(rule
->dport
));
588 return log_error_errno(r
, "Could not append FRA_DPORT_RANGE attribute: %m");
593 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
,
594 callback
?: routing_policy_rule_handler
,
595 link_netlink_destroy_callback
, link
);
597 return log_error_errno(r
, "Could not send rtnetlink message: %m");
601 r
= routing_policy_rule_add(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
602 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, rule
->protocol
, &rule
->sport
, &rule
->dport
, NULL
);
604 return log_error_errno(r
, "Could not add rule: %m");
609 static int parse_fwmark_fwmask(const char *s
, uint32_t *fwmark
, uint32_t *fwmask
) {
610 _cleanup_free_
char *f
= NULL
;
624 r
= safe_atou32(f
, fwmark
);
626 return log_error_errno(r
, "Failed to parse RPDB rule firewall mark, ignoring: %s", f
);
629 r
= safe_atou32(p
, fwmask
);
631 return log_error_errno(r
, "Failed to parse RPDB rule mask, ignoring: %s", f
);
637 int config_parse_routing_policy_rule_tos(
639 const char *filename
,
642 unsigned section_line
,
649 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
650 Network
*network
= userdata
;
659 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
663 r
= safe_atou8(rvalue
, &n
->tos
);
665 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule tos, ignoring: %s", rvalue
);
674 int config_parse_routing_policy_rule_priority(
676 const char *filename
,
679 unsigned section_line
,
686 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
687 Network
*network
= userdata
;
696 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
700 r
= safe_atou32(rvalue
, &n
->priority
);
702 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule priority, ignoring: %s", rvalue
);
711 int config_parse_routing_policy_rule_table(
713 const char *filename
,
716 unsigned section_line
,
723 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
724 Network
*network
= userdata
;
733 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
737 r
= safe_atou32(rvalue
, &n
->table
);
739 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule table, ignoring: %s", rvalue
);
748 int config_parse_routing_policy_rule_fwmark_mask(
750 const char *filename
,
753 unsigned section_line
,
760 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
761 Network
*network
= userdata
;
770 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
774 r
= parse_fwmark_fwmask(rvalue
, &n
->fwmark
, &n
->fwmask
);
776 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue
);
785 int config_parse_routing_policy_rule_prefix(
787 const char *filename
,
790 unsigned section_line
,
797 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
798 Network
*network
= userdata
;
799 union in_addr_union
*buffer
;
809 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
813 if (streq(lvalue
, "To")) {
815 prefixlen
= &n
->to_prefixlen
;
818 prefixlen
= &n
->from_prefixlen
;
821 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
823 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
832 int config_parse_routing_policy_rule_device(
834 const char *filename
,
837 unsigned section_line
,
844 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
845 Network
*network
= userdata
;
854 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
858 if (!ifname_valid(rvalue
)) {
859 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse '%s' interface name, ignoring: %s", lvalue
, rvalue
);
863 if (streq(lvalue
, "IncomingInterface")) {
864 r
= free_and_strdup(&n
->iif
, rvalue
);
868 r
= free_and_strdup(&n
->oif
, rvalue
);
878 int config_parse_routing_policy_rule_port_range(
880 const char *filename
,
883 unsigned section_line
,
889 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
890 Network
*network
= userdata
;
900 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
904 r
= parse_ip_port_range(rvalue
, &low
, &high
);
906 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse routing policy rule port range '%s'", rvalue
);
910 if (streq(lvalue
, "SourcePort")) {
911 n
->sport
.start
= low
;
914 n
->dport
.start
= low
;
923 int config_parse_routing_policy_rule_ip_protocol(
925 const char *filename
,
928 unsigned section_line
,
935 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
936 Network
*network
= userdata
;
945 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
949 r
= parse_ip_protocol(rvalue
);
951 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse IP protocol '%s' for routing policy rule, ignoring: %m", rvalue
);
962 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
963 _cleanup_free_
char *s
= NULL
;
969 r
= read_full_file(state_file
, &s
, &size
);
982 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
983 RoutingPolicyRule
*rule
= NULL
;
989 SET_FOREACH(rule
, rules
, i
) {
990 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
995 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
996 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
1000 fprintf(f
, "from=%s/%hhu",
1001 from_str
, rule
->from_prefixlen
);
1005 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
1006 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
1010 fprintf(f
, "%sto=%s/%hhu",
1012 to_str
, rule
->to_prefixlen
);
1016 if (rule
->tos
!= 0) {
1017 fprintf(f
, "%stos=%hhu",
1023 if (rule
->fwmark
!= 0) {
1024 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
1026 rule
->fwmark
, rule
->fwmask
);
1031 fprintf(f
, "%siif=%s",
1038 fprintf(f
, "%soif=%s",
1044 if (rule
->protocol
!= 0) {
1045 fprintf(f
, "%sprotocol=%hhu",
1051 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
1052 fprintf(f
, "%ssourcesport=%"PRIu16
"-%"PRIu16
,
1054 rule
->sport
.start
, rule
->sport
.end
);
1058 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
1059 fprintf(f
, "%sdestinationport=%"PRIu16
"-%"PRIu16
,
1061 rule
->dport
.start
, rule
->dport
.end
);
1065 fprintf(f
, "%stable=%"PRIu32
"\n",
1073 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
1074 _cleanup_strv_free_
char **l
= NULL
;
1075 _cleanup_free_
char *data
= NULL
;
1076 uint16_t low
= 0, high
= 0;
1084 r
= routing_policy_rule_read_full_file(state_file
, &data
);
1088 l
= strv_split_newlines(data
);
1092 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
1096 STRV_FOREACH(i
, l
) {
1097 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
1099 p
= startswith(*i
, "RULE=");
1103 r
= routing_policy_rule_new(&rule
);
1108 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
1110 r
= extract_first_word(&p
, &word
, NULL
, 0);
1116 r
= split_pair(word
, "=", &a
, &b
);
1120 if (STR_IN_SET(a
, "from", "to")) {
1121 union in_addr_union
*buffer
;
1124 if (streq(a
, "to")) {
1126 prefixlen
= &rule
->to_prefixlen
;
1128 buffer
= &rule
->from
;
1129 prefixlen
= &rule
->from_prefixlen
;
1132 r
= in_addr_prefix_from_string_auto(b
, &rule
->family
, buffer
, prefixlen
);
1134 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
1138 } else if (streq(a
, "tos")) {
1139 r
= safe_atou8(b
, &rule
->tos
);
1141 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
1144 } else if (streq(a
, "table")) {
1145 r
= safe_atou32(b
, &rule
->table
);
1147 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
1150 } else if (streq(a
, "fwmark")) {
1152 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
1154 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
1157 } else if (streq(a
, "iif")) {
1159 if (free_and_strdup(&rule
->iif
, b
) < 0)
1162 } else if (streq(a
, "oif")) {
1164 if (free_and_strdup(&rule
->oif
, b
) < 0)
1166 } else if (streq(a
, "protocol")) {
1167 r
= safe_atou8(b
, &rule
->protocol
);
1169 log_error_errno(r
, "Failed to parse RPDB rule protocol, ignoring: %s", b
);
1172 } else if (streq(a
, "sourceport")) {
1174 r
= parse_ip_port_range(b
, &low
, &high
);
1176 log_error_errno(r
, "Invalid routing policy rule source port range, ignoring assignment:'%s'", b
);
1180 rule
->sport
.start
= low
;
1181 rule
->sport
.end
= high
;
1183 } else if (streq(a
, "destinationport")) {
1185 r
= parse_ip_port_range(b
, &low
, &high
);
1187 log_error_errno(r
, "Invalid routing policy rule destination port range, ignoring assignment:'%s'", b
);
1191 rule
->dport
.start
= low
;
1192 rule
->dport
.end
= high
;
1196 r
= set_put(*rules
, rule
);
1198 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1208 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1209 RoutingPolicyRule
*rule
, *existing
;
1216 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1217 existing
= set_get(m
->rules_foreign
, rule
);
1220 r
= routing_policy_rule_remove(rule
, link
, NULL
);
1222 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1226 link
->routing_policy_rule_remove_messages
++;