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 if (set_get(rule
->manager
->rules
, rule
) == rule
)
50 set_remove(rule
->manager
->rules
, rule
);
51 if (set_get(rule
->manager
->rules_foreign
, rule
) == rule
)
52 set_remove(rule
->manager
->rules_foreign
, rule
);
55 network_config_section_free(rule
->section
);
61 static void routing_policy_rule_hash_func(const RoutingPolicyRule
*rule
, struct siphash
*state
) {
64 siphash24_compress(&rule
->family
, sizeof(rule
->family
), state
);
66 switch (rule
->family
) {
70 siphash24_compress(&rule
->from
, FAMILY_ADDRESS_SIZE(rule
->family
), state
);
71 siphash24_compress(&rule
->from_prefixlen
, sizeof(rule
->from_prefixlen
), state
);
73 siphash24_compress(&rule
->to
, FAMILY_ADDRESS_SIZE(rule
->family
), state
);
74 siphash24_compress(&rule
->to_prefixlen
, sizeof(rule
->to_prefixlen
), state
);
76 siphash24_compress(&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 RoutingPolicyRule
*a
, const RoutingPolicyRule
*b
) {
100 r
= CMP(a
->family
, b
->family
);
107 r
= CMP(a
->from_prefixlen
, b
->from_prefixlen
);
111 r
= CMP(a
->to_prefixlen
, b
->to_prefixlen
);
115 r
= CMP(a
->tos
, b
->tos
);
119 r
= CMP(a
->fwmask
, b
->fwmask
);
123 r
= CMP(a
->table
, b
->table
);
127 r
= strcmp_ptr(a
->iif
, b
->iif
);
131 r
= strcmp_ptr(a
->oif
, b
->oif
);
135 r
= CMP(a
->protocol
, b
->protocol
);
139 r
= memcmp(&a
->sport
, &b
->sport
, sizeof(a
->sport
));
143 r
= memcmp(&a
->dport
, &b
->dport
, sizeof(a
->dport
));
147 r
= memcmp(&a
->from
, &b
->from
, FAMILY_ADDRESS_SIZE(a
->family
));
151 return memcmp(&a
->to
, &b
->to
, FAMILY_ADDRESS_SIZE(a
->family
));
154 /* treat any other address family as AF_UNSPEC */
159 DEFINE_PRIVATE_HASH_OPS(routing_policy_rule_hash_ops
, RoutingPolicyRule
, routing_policy_rule_hash_func
, routing_policy_rule_compare_func
);
161 int routing_policy_rule_get(Manager
*m
,
163 const union in_addr_union
*from
,
164 uint8_t from_prefixlen
,
165 const union in_addr_union
*to
,
166 uint8_t to_prefixlen
,
173 struct fib_rule_port_range
*sport
,
174 struct fib_rule_port_range
*dport
,
175 RoutingPolicyRule
**ret
) {
177 RoutingPolicyRule rule
, *existing
;
179 assert_return(m
, -1);
181 rule
= (RoutingPolicyRule
) {
184 .from_prefixlen
= from_prefixlen
,
186 .to_prefixlen
= to_prefixlen
,
192 .protocol
= protocol
,
197 existing
= set_get(m
->rules
, &rule
);
204 existing
= set_get(m
->rules_foreign
, &rule
);
214 int routing_policy_rule_make_local(Manager
*m
, RoutingPolicyRule
*rule
) {
219 if (set_contains(m
->rules_foreign
, rule
)) {
220 set_remove(m
->rules_foreign
, rule
);
222 r
= set_ensure_allocated(&m
->rules
, &routing_policy_rule_hash_ops
);
226 return set_put(m
->rules
, rule
);
232 static int routing_policy_rule_add_internal(Manager
*m
,
235 const union in_addr_union
*from
,
236 uint8_t from_prefixlen
,
237 const union in_addr_union
*to
,
238 uint8_t to_prefixlen
,
245 const struct fib_rule_port_range
*sport
,
246 const struct fib_rule_port_range
*dport
,
247 RoutingPolicyRule
**ret
) {
249 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
250 _cleanup_free_
char *iif
= NULL
, *oif
= NULL
;
253 assert_return(rules
, -EINVAL
);
267 r
= routing_policy_rule_new(&rule
);
272 rule
->family
= family
;
274 rule
->from_prefixlen
= from_prefixlen
;
276 rule
->to_prefixlen
= to_prefixlen
;
278 rule
->fwmark
= fwmark
;
280 rule
->iif
= TAKE_PTR(iif
);
281 rule
->oif
= TAKE_PTR(oif
);
282 rule
->protocol
= protocol
;
283 rule
->sport
= *sport
;
284 rule
->dport
= *dport
;
286 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
290 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
) == 0) {
373 r
= netlink_message_append_in_addr_union(m
, FRA_SRC
, routing_policy_rule
->family
, &routing_policy_rule
->from
);
375 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
377 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, routing_policy_rule
->from_prefixlen
);
379 return log_error_errno(r
, "Could not set source prefix length: %m");
382 if (in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->to
) == 0) {
383 r
= netlink_message_append_in_addr_union(m
, FRA_DST
, routing_policy_rule
->family
, &routing_policy_rule
->to
);
385 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
387 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, routing_policy_rule
->to_prefixlen
);
389 return log_error_errno(r
, "Could not set destination prefix length: %m");
392 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
,
393 callback
?: routing_policy_rule_remove_handler
,
394 link_netlink_destroy_callback
, link
);
396 return log_error_errno(r
, "Could not send rtnetlink message: %m");
403 static int routing_policy_rule_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutingPolicyRule
**ret
) {
404 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
405 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
410 assert(!!filename
== (section_line
> 0));
413 r
= network_config_section_new(filename
, section_line
, &n
);
417 rule
= hashmap_get(network
->rules_by_section
, n
);
419 *ret
= TAKE_PTR(rule
);
425 r
= routing_policy_rule_new(&rule
);
429 rule
->network
= network
;
430 LIST_APPEND(rules
, network
->rules
, rule
);
434 rule
->section
= TAKE_PTR(n
);
436 r
= hashmap_ensure_allocated(&network
->rules_by_section
, &network_config_hash_ops
);
440 r
= hashmap_put(network
->rules_by_section
, rule
->section
, rule
);
445 *ret
= TAKE_PTR(rule
);
450 static int routing_policy_rule_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
456 assert(link
->ifname
);
457 assert(link
->routing_policy_rule_messages
> 0);
459 link
->routing_policy_rule_messages
--;
461 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
464 r
= sd_netlink_message_get_errno(m
);
465 if (r
< 0 && r
!= -EEXIST
)
466 log_link_warning_errno(link
, r
, "Could not add routing policy rule: %m");
468 if (link
->routing_policy_rule_messages
== 0) {
469 log_link_debug(link
, "Routing policy rule configured");
470 link
->routing_policy_rules_configured
= true;
471 link_check_ready(link
);
477 int routing_policy_rule_configure(RoutingPolicyRule
*rule
, Link
*link
, link_netlink_message_handler_t callback
) {
478 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
483 assert(link
->ifindex
> 0);
484 assert(link
->manager
);
485 assert(link
->manager
->rtnl
);
487 if (rule
->family
== AF_INET6
&& manager_sysctl_ipv6_enabled(link
->manager
) == 0) {
488 log_link_warning(link
, "An IPv6 routing policy rule is requested, but IPv6 is disabled by sysctl, ignoring.");
492 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
494 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
496 if (in_addr_is_null(rule
->family
, &rule
->from
) == 0) {
497 r
= netlink_message_append_in_addr_union(m
, FRA_SRC
, rule
->family
, &rule
->from
);
499 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
501 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
503 return log_error_errno(r
, "Could not set source prefix length: %m");
506 if (in_addr_is_null(rule
->family
, &rule
->to
) == 0) {
507 r
= netlink_message_append_in_addr_union(m
, FRA_DST
, rule
->family
, &rule
->to
);
509 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
511 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
513 return log_error_errno(r
, "Could not set destination prefix length: %m");
516 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
518 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
521 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
523 return log_error_errno(r
, "Could not set ip rule tos: %m");
526 if (rule
->table
< 256) {
527 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
529 return log_error_errno(r
, "Could not set ip rule table: %m");
531 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
533 return log_error_errno(r
, "Could not set ip rule table: %m");
535 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
537 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
540 if (rule
->fwmark
> 0) {
541 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
543 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
546 if (rule
->fwmask
> 0) {
547 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
549 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
553 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
555 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
559 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
561 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
564 r
= sd_netlink_message_append_u8(m
, FRA_IP_PROTO
, rule
->protocol
);
566 return log_error_errno(r
, "Could not append FRA_IP_PROTO attribute: %m");
568 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
569 r
= sd_netlink_message_append_data(m
, FRA_SPORT_RANGE
, &rule
->sport
, sizeof(rule
->sport
));
571 return log_error_errno(r
, "Could not append FRA_SPORT_RANGE attribute: %m");
574 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
575 r
= sd_netlink_message_append_data(m
, FRA_DPORT_RANGE
, &rule
->dport
, sizeof(rule
->dport
));
577 return log_error_errno(r
, "Could not append FRA_DPORT_RANGE attribute: %m");
580 if (rule
->invert_rule
) {
581 r
= sd_rtnl_message_routing_policy_rule_set_flags(m
, FIB_RULE_INVERT
);
583 return log_error_errno(r
, "Could not append FIB_RULE_INVERT attribute: %m");
588 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
,
589 callback
?: routing_policy_rule_handler
,
590 link_netlink_destroy_callback
, link
);
592 return log_error_errno(r
, "Could not send rtnetlink message: %m");
596 r
= routing_policy_rule_add(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
597 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, rule
->protocol
, &rule
->sport
, &rule
->dport
, NULL
);
599 return log_error_errno(r
, "Could not add rule: %m");
604 static int parse_fwmark_fwmask(const char *s
, uint32_t *fwmark
, uint32_t *fwmask
) {
605 _cleanup_free_
char *f
= NULL
;
619 r
= safe_atou32(f
, fwmark
);
621 return log_error_errno(r
, "Failed to parse RPDB rule firewall mark, ignoring: %s", f
);
624 r
= safe_atou32(p
, fwmask
);
626 return log_error_errno(r
, "Failed to parse RPDB rule mask, ignoring: %s", f
);
632 int config_parse_routing_policy_rule_tos(
634 const char *filename
,
637 unsigned section_line
,
644 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
645 Network
*network
= userdata
;
654 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
658 r
= safe_atou8(rvalue
, &n
->tos
);
660 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule tos, ignoring: %s", rvalue
);
669 int config_parse_routing_policy_rule_priority(
671 const char *filename
,
674 unsigned section_line
,
681 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
682 Network
*network
= userdata
;
691 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
695 r
= safe_atou32(rvalue
, &n
->priority
);
697 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule priority, ignoring: %s", rvalue
);
706 int config_parse_routing_policy_rule_table(
708 const char *filename
,
711 unsigned section_line
,
718 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
719 Network
*network
= userdata
;
728 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
732 r
= safe_atou32(rvalue
, &n
->table
);
734 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule table, ignoring: %s", rvalue
);
743 int config_parse_routing_policy_rule_fwmark_mask(
745 const char *filename
,
748 unsigned section_line
,
755 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
756 Network
*network
= userdata
;
765 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
769 r
= parse_fwmark_fwmask(rvalue
, &n
->fwmark
, &n
->fwmask
);
771 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue
);
780 int config_parse_routing_policy_rule_prefix(
782 const char *filename
,
785 unsigned section_line
,
792 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
793 Network
*network
= userdata
;
794 union in_addr_union
*buffer
;
804 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
808 if (streq(lvalue
, "To")) {
810 prefixlen
= &n
->to_prefixlen
;
813 prefixlen
= &n
->from_prefixlen
;
816 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
818 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
827 int config_parse_routing_policy_rule_device(
829 const char *filename
,
832 unsigned section_line
,
839 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
840 Network
*network
= userdata
;
849 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
853 if (!ifname_valid(rvalue
)) {
854 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse '%s' interface name, ignoring: %s", lvalue
, rvalue
);
858 if (streq(lvalue
, "IncomingInterface")) {
859 r
= free_and_strdup(&n
->iif
, rvalue
);
863 r
= free_and_strdup(&n
->oif
, rvalue
);
873 int config_parse_routing_policy_rule_port_range(
875 const char *filename
,
878 unsigned section_line
,
884 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
885 Network
*network
= userdata
;
895 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
899 r
= parse_ip_port_range(rvalue
, &low
, &high
);
901 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse routing policy rule port range '%s'", rvalue
);
905 if (streq(lvalue
, "SourcePort")) {
906 n
->sport
.start
= low
;
909 n
->dport
.start
= low
;
918 int config_parse_routing_policy_rule_ip_protocol(
920 const char *filename
,
923 unsigned section_line
,
930 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
931 Network
*network
= userdata
;
940 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
944 r
= parse_ip_protocol(rvalue
);
946 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse IP protocol '%s' for routing policy rule, ignoring: %m", rvalue
);
957 int config_parse_routing_policy_rule_invert(
959 const char *filename
,
962 unsigned section_line
,
969 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
970 Network
*network
= userdata
;
979 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
983 r
= parse_boolean(rvalue
);
985 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule invert, ignoring: %s", rvalue
);
996 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
997 _cleanup_free_
char *s
= NULL
;
1003 r
= read_full_file(state_file
, &s
, &size
);
1016 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
1017 RoutingPolicyRule
*rule
= NULL
;
1023 SET_FOREACH(rule
, rules
, i
) {
1024 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
1029 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
1030 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
1034 fprintf(f
, "from=%s/%hhu",
1035 from_str
, rule
->from_prefixlen
);
1039 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
1040 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
1044 fprintf(f
, "%sto=%s/%hhu",
1046 to_str
, rule
->to_prefixlen
);
1050 if (rule
->tos
!= 0) {
1051 fprintf(f
, "%stos=%hhu",
1057 if (rule
->fwmark
!= 0) {
1058 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
1060 rule
->fwmark
, rule
->fwmask
);
1065 fprintf(f
, "%siif=%s",
1072 fprintf(f
, "%soif=%s",
1078 if (rule
->protocol
!= 0) {
1079 fprintf(f
, "%sprotocol=%hhu",
1085 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
1086 fprintf(f
, "%ssourcesport=%"PRIu16
"-%"PRIu16
,
1088 rule
->sport
.start
, rule
->sport
.end
);
1092 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
1093 fprintf(f
, "%sdestinationport=%"PRIu16
"-%"PRIu16
,
1095 rule
->dport
.start
, rule
->dport
.end
);
1099 fprintf(f
, "%stable=%"PRIu32
"\n",
1107 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
1108 _cleanup_strv_free_
char **l
= NULL
;
1109 _cleanup_free_
char *data
= NULL
;
1110 uint16_t low
= 0, high
= 0;
1118 r
= routing_policy_rule_read_full_file(state_file
, &data
);
1122 l
= strv_split_newlines(data
);
1126 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
1130 STRV_FOREACH(i
, l
) {
1131 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
1133 p
= startswith(*i
, "RULE=");
1137 r
= routing_policy_rule_new(&rule
);
1142 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
1144 r
= extract_first_word(&p
, &word
, NULL
, 0);
1150 r
= split_pair(word
, "=", &a
, &b
);
1154 if (STR_IN_SET(a
, "from", "to")) {
1155 union in_addr_union
*buffer
;
1158 if (streq(a
, "to")) {
1160 prefixlen
= &rule
->to_prefixlen
;
1162 buffer
= &rule
->from
;
1163 prefixlen
= &rule
->from_prefixlen
;
1166 r
= in_addr_prefix_from_string_auto(b
, &rule
->family
, buffer
, prefixlen
);
1168 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
1172 } else if (streq(a
, "tos")) {
1173 r
= safe_atou8(b
, &rule
->tos
);
1175 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
1178 } else if (streq(a
, "table")) {
1179 r
= safe_atou32(b
, &rule
->table
);
1181 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
1184 } else if (streq(a
, "fwmark")) {
1186 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
1188 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
1191 } else if (streq(a
, "iif")) {
1193 if (free_and_strdup(&rule
->iif
, b
) < 0)
1196 } else if (streq(a
, "oif")) {
1198 if (free_and_strdup(&rule
->oif
, b
) < 0)
1200 } else if (streq(a
, "protocol")) {
1201 r
= safe_atou8(b
, &rule
->protocol
);
1203 log_error_errno(r
, "Failed to parse RPDB rule protocol, ignoring: %s", b
);
1206 } else if (streq(a
, "sourceport")) {
1208 r
= parse_ip_port_range(b
, &low
, &high
);
1210 log_error_errno(r
, "Invalid routing policy rule source port range, ignoring assignment:'%s'", b
);
1214 rule
->sport
.start
= low
;
1215 rule
->sport
.end
= high
;
1217 } else if (streq(a
, "destinationport")) {
1219 r
= parse_ip_port_range(b
, &low
, &high
);
1221 log_error_errno(r
, "Invalid routing policy rule destination port range, ignoring assignment:'%s'", b
);
1225 rule
->dport
.start
= low
;
1226 rule
->dport
.end
= high
;
1230 r
= set_put(*rules
, rule
);
1232 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1242 static bool manager_links_have_routing_policy_rule(Manager
*m
, RoutingPolicyRule
*rule
) {
1243 RoutingPolicyRule
*link_rule
;
1250 HASHMAP_FOREACH(link
, m
->links
, i
) {
1254 LIST_FOREACH(rules
, link_rule
, link
->network
->rules
)
1255 if (routing_policy_rule_compare_func(link_rule
, rule
) == 0)
1262 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1263 RoutingPolicyRule
*rule
, *existing
;
1270 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1271 existing
= set_get(m
->rules_foreign
, rule
);
1273 continue; /* Saved rule does not exist anymore. */
1275 if (manager_links_have_routing_policy_rule(m
, existing
))
1276 continue; /* Existing links have the saved rule. */
1278 /* Existing links do not have the saved rule. Let's drop the rule now, and re-configure it
1279 * later when it is requested. */
1281 r
= routing_policy_rule_remove(existing
, link
, NULL
);
1283 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1287 link
->routing_policy_rule_remove_messages
++;
1289 assert_se(set_remove(m
->rules_foreign
, existing
) == existing
);
1290 routing_policy_rule_free(existing
);