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 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
489 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
491 if (in_addr_is_null(rule
->family
, &rule
->from
) == 0) {
492 r
= netlink_message_append_in_addr_union(m
, FRA_SRC
, rule
->family
, &rule
->from
);
494 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
496 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
498 return log_error_errno(r
, "Could not set source prefix length: %m");
501 if (in_addr_is_null(rule
->family
, &rule
->to
) == 0) {
502 r
= netlink_message_append_in_addr_union(m
, FRA_DST
, rule
->family
, &rule
->to
);
504 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
506 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
508 return log_error_errno(r
, "Could not set destination prefix length: %m");
511 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
513 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
516 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
518 return log_error_errno(r
, "Could not set ip rule tos: %m");
521 if (rule
->table
< 256) {
522 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
524 return log_error_errno(r
, "Could not set ip rule table: %m");
526 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
528 return log_error_errno(r
, "Could not set ip rule table: %m");
530 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
532 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
535 if (rule
->fwmark
> 0) {
536 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
538 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
541 if (rule
->fwmask
> 0) {
542 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
544 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
548 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
550 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
554 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
556 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
559 r
= sd_netlink_message_append_u8(m
, FRA_IP_PROTO
, rule
->protocol
);
561 return log_error_errno(r
, "Could not append FRA_IP_PROTO attribute: %m");
563 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
564 r
= sd_netlink_message_append_data(m
, FRA_SPORT_RANGE
, &rule
->sport
, sizeof(rule
->sport
));
566 return log_error_errno(r
, "Could not append FRA_SPORT_RANGE attribute: %m");
569 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
570 r
= sd_netlink_message_append_data(m
, FRA_DPORT_RANGE
, &rule
->dport
, sizeof(rule
->dport
));
572 return log_error_errno(r
, "Could not append FRA_DPORT_RANGE attribute: %m");
575 if (rule
->invert_rule
) {
576 r
= sd_rtnl_message_routing_policy_rule_set_flags(m
, FIB_RULE_INVERT
);
578 return log_error_errno(r
, "Could not append FIB_RULE_INVERT attribute: %m");
583 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
,
584 callback
?: routing_policy_rule_handler
,
585 link_netlink_destroy_callback
, link
);
587 return log_error_errno(r
, "Could not send rtnetlink message: %m");
591 r
= routing_policy_rule_add(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
592 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, rule
->protocol
, &rule
->sport
, &rule
->dport
, NULL
);
594 return log_error_errno(r
, "Could not add rule: %m");
599 static int parse_fwmark_fwmask(const char *s
, uint32_t *fwmark
, uint32_t *fwmask
) {
600 _cleanup_free_
char *f
= NULL
;
614 r
= safe_atou32(f
, fwmark
);
616 return log_error_errno(r
, "Failed to parse RPDB rule firewall mark, ignoring: %s", f
);
619 r
= safe_atou32(p
, fwmask
);
621 return log_error_errno(r
, "Failed to parse RPDB rule mask, ignoring: %s", f
);
627 int config_parse_routing_policy_rule_tos(
629 const char *filename
,
632 unsigned section_line
,
639 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
640 Network
*network
= userdata
;
649 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
653 r
= safe_atou8(rvalue
, &n
->tos
);
655 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule tos, ignoring: %s", rvalue
);
664 int config_parse_routing_policy_rule_priority(
666 const char *filename
,
669 unsigned section_line
,
676 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
677 Network
*network
= userdata
;
686 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
690 r
= safe_atou32(rvalue
, &n
->priority
);
692 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule priority, ignoring: %s", rvalue
);
701 int config_parse_routing_policy_rule_table(
703 const char *filename
,
706 unsigned section_line
,
713 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
714 Network
*network
= userdata
;
723 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
727 r
= safe_atou32(rvalue
, &n
->table
);
729 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule table, ignoring: %s", rvalue
);
738 int config_parse_routing_policy_rule_fwmark_mask(
740 const char *filename
,
743 unsigned section_line
,
750 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
751 Network
*network
= userdata
;
760 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
764 r
= parse_fwmark_fwmask(rvalue
, &n
->fwmark
, &n
->fwmask
);
766 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue
);
775 int config_parse_routing_policy_rule_prefix(
777 const char *filename
,
780 unsigned section_line
,
787 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
788 Network
*network
= userdata
;
789 union in_addr_union
*buffer
;
799 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
803 if (streq(lvalue
, "To")) {
805 prefixlen
= &n
->to_prefixlen
;
808 prefixlen
= &n
->from_prefixlen
;
811 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
813 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
822 int config_parse_routing_policy_rule_device(
824 const char *filename
,
827 unsigned section_line
,
834 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
835 Network
*network
= userdata
;
844 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
848 if (!ifname_valid(rvalue
)) {
849 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse '%s' interface name, ignoring: %s", lvalue
, rvalue
);
853 if (streq(lvalue
, "IncomingInterface")) {
854 r
= free_and_strdup(&n
->iif
, rvalue
);
858 r
= free_and_strdup(&n
->oif
, rvalue
);
868 int config_parse_routing_policy_rule_port_range(
870 const char *filename
,
873 unsigned section_line
,
879 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
880 Network
*network
= userdata
;
890 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
894 r
= parse_ip_port_range(rvalue
, &low
, &high
);
896 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse routing policy rule port range '%s'", rvalue
);
900 if (streq(lvalue
, "SourcePort")) {
901 n
->sport
.start
= low
;
904 n
->dport
.start
= low
;
913 int config_parse_routing_policy_rule_ip_protocol(
915 const char *filename
,
918 unsigned section_line
,
925 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
926 Network
*network
= userdata
;
935 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
939 r
= parse_ip_protocol(rvalue
);
941 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse IP protocol '%s' for routing policy rule, ignoring: %m", rvalue
);
952 int config_parse_routing_policy_rule_invert(
954 const char *filename
,
957 unsigned section_line
,
964 _cleanup_(routing_policy_rule_free_or_set_invalidp
) RoutingPolicyRule
*n
= NULL
;
965 Network
*network
= userdata
;
974 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
978 r
= parse_boolean(rvalue
);
980 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule invert, ignoring: %s", rvalue
);
991 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
992 _cleanup_free_
char *s
= NULL
;
998 r
= read_full_file(state_file
, &s
, &size
);
1011 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
1012 RoutingPolicyRule
*rule
= NULL
;
1018 SET_FOREACH(rule
, rules
, i
) {
1019 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
1024 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
1025 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
1029 fprintf(f
, "from=%s/%hhu",
1030 from_str
, rule
->from_prefixlen
);
1034 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
1035 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
1039 fprintf(f
, "%sto=%s/%hhu",
1041 to_str
, rule
->to_prefixlen
);
1045 if (rule
->tos
!= 0) {
1046 fprintf(f
, "%stos=%hhu",
1052 if (rule
->fwmark
!= 0) {
1053 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
1055 rule
->fwmark
, rule
->fwmask
);
1060 fprintf(f
, "%siif=%s",
1067 fprintf(f
, "%soif=%s",
1073 if (rule
->protocol
!= 0) {
1074 fprintf(f
, "%sprotocol=%hhu",
1080 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
1081 fprintf(f
, "%ssourcesport=%"PRIu16
"-%"PRIu16
,
1083 rule
->sport
.start
, rule
->sport
.end
);
1087 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
1088 fprintf(f
, "%sdestinationport=%"PRIu16
"-%"PRIu16
,
1090 rule
->dport
.start
, rule
->dport
.end
);
1094 fprintf(f
, "%stable=%"PRIu32
"\n",
1102 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
1103 _cleanup_strv_free_
char **l
= NULL
;
1104 _cleanup_free_
char *data
= NULL
;
1105 uint16_t low
= 0, high
= 0;
1113 r
= routing_policy_rule_read_full_file(state_file
, &data
);
1117 l
= strv_split_newlines(data
);
1121 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
1125 STRV_FOREACH(i
, l
) {
1126 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
1128 p
= startswith(*i
, "RULE=");
1132 r
= routing_policy_rule_new(&rule
);
1137 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
1139 r
= extract_first_word(&p
, &word
, NULL
, 0);
1145 r
= split_pair(word
, "=", &a
, &b
);
1149 if (STR_IN_SET(a
, "from", "to")) {
1150 union in_addr_union
*buffer
;
1153 if (streq(a
, "to")) {
1155 prefixlen
= &rule
->to_prefixlen
;
1157 buffer
= &rule
->from
;
1158 prefixlen
= &rule
->from_prefixlen
;
1161 r
= in_addr_prefix_from_string_auto(b
, &rule
->family
, buffer
, prefixlen
);
1163 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
1167 } else if (streq(a
, "tos")) {
1168 r
= safe_atou8(b
, &rule
->tos
);
1170 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
1173 } else if (streq(a
, "table")) {
1174 r
= safe_atou32(b
, &rule
->table
);
1176 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
1179 } else if (streq(a
, "fwmark")) {
1181 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
1183 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
1186 } else if (streq(a
, "iif")) {
1188 if (free_and_strdup(&rule
->iif
, b
) < 0)
1191 } else if (streq(a
, "oif")) {
1193 if (free_and_strdup(&rule
->oif
, b
) < 0)
1195 } else if (streq(a
, "protocol")) {
1196 r
= safe_atou8(b
, &rule
->protocol
);
1198 log_error_errno(r
, "Failed to parse RPDB rule protocol, ignoring: %s", b
);
1201 } else if (streq(a
, "sourceport")) {
1203 r
= parse_ip_port_range(b
, &low
, &high
);
1205 log_error_errno(r
, "Invalid routing policy rule source port range, ignoring assignment:'%s'", b
);
1209 rule
->sport
.start
= low
;
1210 rule
->sport
.end
= high
;
1212 } else if (streq(a
, "destinationport")) {
1214 r
= parse_ip_port_range(b
, &low
, &high
);
1216 log_error_errno(r
, "Invalid routing policy rule destination port range, ignoring assignment:'%s'", b
);
1220 rule
->dport
.start
= low
;
1221 rule
->dport
.end
= high
;
1225 r
= set_put(*rules
, rule
);
1227 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1237 static bool manager_links_have_routing_policy_rule(Manager
*m
, RoutingPolicyRule
*rule
) {
1238 RoutingPolicyRule
*link_rule
;
1245 HASHMAP_FOREACH(link
, m
->links
, i
) {
1249 LIST_FOREACH(rules
, link_rule
, link
->network
->rules
)
1250 if (routing_policy_rule_compare_func(link_rule
, rule
) == 0)
1257 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1258 RoutingPolicyRule
*rule
, *existing
;
1265 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1266 existing
= set_get(m
->rules_foreign
, rule
);
1268 continue; /* Saved rule does not exist anymore. */
1270 if (manager_links_have_routing_policy_rule(m
, existing
))
1271 continue; /* Existing links have the saved rule. */
1273 /* Existing links do not have the saved rule. Let's drop the rule now, and re-configure it
1274 * later when it is requested. */
1276 r
= routing_policy_rule_remove(existing
, link
, NULL
);
1278 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1282 link
->routing_policy_rule_remove_messages
++;
1284 assert_se(set_remove(m
->rules_foreign
, existing
) == existing
);
1285 routing_policy_rule_free(existing
);