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");
591 if (rule
->invert_rule
) {
592 r
= sd_rtnl_message_routing_policy_rule_set_flags(m
, FIB_RULE_INVERT
);
594 return log_error_errno(r
, "Could not append FIB_RULE_INVERT 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 int config_parse_routing_policy_rule_invert(
970 const char *filename
,
973 unsigned section_line
,
980 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
981 Network
*network
= userdata
;
990 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
994 r
= parse_boolean(rvalue
);
996 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule invert, ignoring: %s", rvalue
);
1007 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
1008 _cleanup_free_
char *s
= NULL
;
1014 r
= read_full_file(state_file
, &s
, &size
);
1027 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
1028 RoutingPolicyRule
*rule
= NULL
;
1034 SET_FOREACH(rule
, rules
, i
) {
1035 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
1040 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
1041 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
1045 fprintf(f
, "from=%s/%hhu",
1046 from_str
, rule
->from_prefixlen
);
1050 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
1051 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
1055 fprintf(f
, "%sto=%s/%hhu",
1057 to_str
, rule
->to_prefixlen
);
1061 if (rule
->tos
!= 0) {
1062 fprintf(f
, "%stos=%hhu",
1068 if (rule
->fwmark
!= 0) {
1069 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
1071 rule
->fwmark
, rule
->fwmask
);
1076 fprintf(f
, "%siif=%s",
1083 fprintf(f
, "%soif=%s",
1089 if (rule
->protocol
!= 0) {
1090 fprintf(f
, "%sprotocol=%hhu",
1096 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
1097 fprintf(f
, "%ssourcesport=%"PRIu16
"-%"PRIu16
,
1099 rule
->sport
.start
, rule
->sport
.end
);
1103 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
1104 fprintf(f
, "%sdestinationport=%"PRIu16
"-%"PRIu16
,
1106 rule
->dport
.start
, rule
->dport
.end
);
1110 fprintf(f
, "%stable=%"PRIu32
"\n",
1118 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
1119 _cleanup_strv_free_
char **l
= NULL
;
1120 _cleanup_free_
char *data
= NULL
;
1121 uint16_t low
= 0, high
= 0;
1129 r
= routing_policy_rule_read_full_file(state_file
, &data
);
1133 l
= strv_split_newlines(data
);
1137 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
1141 STRV_FOREACH(i
, l
) {
1142 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
1144 p
= startswith(*i
, "RULE=");
1148 r
= routing_policy_rule_new(&rule
);
1153 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
1155 r
= extract_first_word(&p
, &word
, NULL
, 0);
1161 r
= split_pair(word
, "=", &a
, &b
);
1165 if (STR_IN_SET(a
, "from", "to")) {
1166 union in_addr_union
*buffer
;
1169 if (streq(a
, "to")) {
1171 prefixlen
= &rule
->to_prefixlen
;
1173 buffer
= &rule
->from
;
1174 prefixlen
= &rule
->from_prefixlen
;
1177 r
= in_addr_prefix_from_string_auto(b
, &rule
->family
, buffer
, prefixlen
);
1179 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
1183 } else if (streq(a
, "tos")) {
1184 r
= safe_atou8(b
, &rule
->tos
);
1186 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
1189 } else if (streq(a
, "table")) {
1190 r
= safe_atou32(b
, &rule
->table
);
1192 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
1195 } else if (streq(a
, "fwmark")) {
1197 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
1199 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
1202 } else if (streq(a
, "iif")) {
1204 if (free_and_strdup(&rule
->iif
, b
) < 0)
1207 } else if (streq(a
, "oif")) {
1209 if (free_and_strdup(&rule
->oif
, b
) < 0)
1211 } else if (streq(a
, "protocol")) {
1212 r
= safe_atou8(b
, &rule
->protocol
);
1214 log_error_errno(r
, "Failed to parse RPDB rule protocol, ignoring: %s", b
);
1217 } else if (streq(a
, "sourceport")) {
1219 r
= parse_ip_port_range(b
, &low
, &high
);
1221 log_error_errno(r
, "Invalid routing policy rule source port range, ignoring assignment:'%s'", b
);
1225 rule
->sport
.start
= low
;
1226 rule
->sport
.end
= high
;
1228 } else if (streq(a
, "destinationport")) {
1230 r
= parse_ip_port_range(b
, &low
, &high
);
1232 log_error_errno(r
, "Invalid routing policy rule destination port range, ignoring assignment:'%s'", b
);
1236 rule
->dport
.start
= low
;
1237 rule
->dport
.end
= high
;
1241 r
= set_put(*rules
, rule
);
1243 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
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
);
1265 r
= routing_policy_rule_remove(rule
, link
, NULL
);
1267 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1271 link
->routing_policy_rule_remove_messages
++;