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 "networkd-routing-policy-rule.h"
10 #include "netlink-util.h"
11 #include "networkd-manager.h"
12 #include "parse-util.h"
13 #include "socket-protocol-list.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
, void *userdata
) {
339 Link
*link
= userdata
;
344 assert(link
->ifname
);
346 link
->routing_policy_rule_remove_messages
--;
348 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
351 r
= sd_netlink_message_get_errno(m
);
353 log_link_warning_errno(link
, r
, "Could not drop routing policy rule: %m");
358 int routing_policy_rule_remove(RoutingPolicyRule
*routing_policy_rule
, Link
*link
, sd_netlink_message_handler_t callback
) {
359 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
362 assert(routing_policy_rule
);
364 assert(link
->manager
);
365 assert(link
->manager
->rtnl
);
366 assert(link
->ifindex
> 0);
367 assert(IN_SET(routing_policy_rule
->family
, AF_INET
, AF_INET6
));
369 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_DELRULE
, routing_policy_rule
->family
);
371 return log_error_errno(r
, "Could not allocate RTM_DELRULE message: %m");
373 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->from
)) {
374 if (routing_policy_rule
->family
== AF_INET
)
375 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in
);
377 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in6
);
380 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
382 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, routing_policy_rule
->from_prefixlen
);
384 return log_error_errno(r
, "Could not set source prefix length: %m");
387 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->to
)) {
388 if (routing_policy_rule
->family
== AF_INET
)
389 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in
);
391 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in6
);
394 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
396 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, routing_policy_rule
->to_prefixlen
);
398 return log_error_errno(r
, "Could not set destination prefix length: %m");
401 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, m
, callback
,
402 link_netlink_destroy_callback
, link
, 0, __func__
);
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 int link_routing_policy_rule_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
459 Link
*link
= userdata
;
465 assert(link
->ifname
);
466 assert(link
->routing_policy_rule_messages
> 0);
468 link
->routing_policy_rule_messages
--;
470 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
473 r
= sd_netlink_message_get_errno(m
);
474 if (r
< 0 && r
!= -EEXIST
)
475 log_link_warning_errno(link
, r
, "Could not add routing policy rule: %m");
477 if (link
->routing_policy_rule_messages
== 0) {
478 log_link_debug(link
, "Routing policy rule configured");
479 link
->routing_policy_rules_configured
= true;
480 link_check_ready(link
);
486 int routing_policy_rule_configure(RoutingPolicyRule
*rule
, Link
*link
, sd_netlink_message_handler_t callback
, bool update
) {
487 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
492 assert(link
->ifindex
> 0);
493 assert(link
->manager
);
494 assert(link
->manager
->rtnl
);
496 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
498 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
500 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
501 if (rule
->family
== AF_INET
)
502 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &rule
->from
.in
);
504 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &rule
->from
.in6
);
507 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
509 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
511 return log_error_errno(r
, "Could not set source prefix length: %m");
514 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
515 if (rule
->family
== AF_INET
)
516 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &rule
->to
.in
);
518 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &rule
->to
.in6
);
521 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
523 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
525 return log_error_errno(r
, "Could not set destination prefix length: %m");
528 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
530 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
533 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
535 return log_error_errno(r
, "Could not set ip rule tos: %m");
538 if (rule
->table
< 256) {
539 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
541 return log_error_errno(r
, "Could not set ip rule table: %m");
543 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
545 return log_error_errno(r
, "Could not set ip rule table: %m");
547 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
549 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
552 if (rule
->fwmark
> 0) {
553 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
555 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
558 if (rule
->fwmask
> 0) {
559 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
561 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
565 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
567 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
571 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
573 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
576 r
= sd_netlink_message_append_u8(m
, FRA_IP_PROTO
, rule
->protocol
);
578 return log_error_errno(r
, "Could not append FRA_IP_PROTO attribute: %m");
580 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
581 r
= sd_netlink_message_append_data(m
, FRA_SPORT_RANGE
, &rule
->sport
, sizeof(rule
->sport
));
583 return log_error_errno(r
, "Could not append FRA_SPORT_RANGE attribute: %m");
586 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
587 r
= sd_netlink_message_append_data(m
, FRA_DPORT_RANGE
, &rule
->dport
, sizeof(rule
->dport
));
589 return log_error_errno(r
, "Could not append FRA_DPORT_RANGE attribute: %m");
594 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, m
, callback
,
595 link_netlink_destroy_callback
, link
, 0, __func__
);
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 r
= in_addr_prefix_from_string(rvalue
, AF_INET
, &buffer
, &prefixlen
);
815 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &buffer
, &prefixlen
);
817 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
821 n
->family
= AF_INET6
;
825 if (streq(lvalue
, "To")) {
827 n
->to_prefixlen
= prefixlen
;
830 n
->from_prefixlen
= prefixlen
;
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_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
= socket_protocol_from_name(rvalue
);
957 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse routing policy rule protocol, ignoring: %s", rvalue
);
961 if (!IN_SET(r
, IPPROTO_TCP
, IPPROTO_UDP
, IPPROTO_SCTP
)) {
962 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid protocol '%s'. Protocol should be tcp/udp/sctp, ignoring", rvalue
);
973 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
974 _cleanup_free_
char *s
= NULL
;
980 r
= read_full_file(state_file
, &s
, &size
);
993 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
994 RoutingPolicyRule
*rule
= NULL
;
1000 SET_FOREACH(rule
, rules
, i
) {
1001 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
1006 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
1007 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
1011 fprintf(f
, "from=%s/%hhu",
1012 from_str
, rule
->from_prefixlen
);
1016 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
1017 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
1021 fprintf(f
, "%sto=%s/%hhu",
1023 to_str
, rule
->to_prefixlen
);
1027 if (rule
->tos
!= 0) {
1028 fprintf(f
, "%stos=%hhu",
1034 if (rule
->fwmark
!= 0) {
1035 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
1037 rule
->fwmark
, rule
->fwmask
);
1042 fprintf(f
, "%siif=%s",
1049 fprintf(f
, "%soif=%s",
1055 if (rule
->protocol
!= 0) {
1056 fprintf(f
, "%sprotocol=%hhu",
1062 if (rule
->sport
.start
!= 0 || rule
->sport
.end
!= 0) {
1063 fprintf(f
, "%ssourcesport=%"PRIu16
"-%"PRIu16
,
1065 rule
->sport
.start
, rule
->sport
.end
);
1069 if (rule
->dport
.start
!= 0 || rule
->dport
.end
!= 0) {
1070 fprintf(f
, "%sdestinationport=%"PRIu16
"-%"PRIu16
,
1072 rule
->dport
.start
, rule
->dport
.end
);
1076 fprintf(f
, "%stable=%"PRIu32
"\n",
1084 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
1085 _cleanup_strv_free_
char **l
= NULL
;
1086 _cleanup_free_
char *data
= NULL
;
1087 uint16_t low
= 0, high
= 0;
1095 r
= routing_policy_rule_read_full_file(state_file
, &data
);
1099 l
= strv_split_newlines(data
);
1103 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
1107 STRV_FOREACH(i
, l
) {
1108 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
1110 p
= startswith(*i
, "RULE=");
1114 r
= routing_policy_rule_new(&rule
);
1119 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
1120 union in_addr_union buffer
;
1123 r
= extract_first_word(&p
, &word
, NULL
, 0);
1129 r
= split_pair(word
, "=", &a
, &b
);
1133 if (STR_IN_SET(a
, "from", "to")) {
1135 r
= in_addr_prefix_from_string(b
, AF_INET
, &buffer
, &prefixlen
);
1137 r
= in_addr_prefix_from_string(b
, AF_INET6
, &buffer
, &prefixlen
);
1139 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
1143 rule
->family
= AF_INET6
;
1145 rule
->family
= AF_INET
;
1147 if (streq(a
, "to")) {
1149 rule
->to_prefixlen
= prefixlen
;
1151 rule
->from
= buffer
;
1152 rule
->from_prefixlen
= prefixlen
;
1154 } else if (streq(a
, "tos")) {
1155 r
= safe_atou8(b
, &rule
->tos
);
1157 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
1160 } else if (streq(a
, "table")) {
1161 r
= safe_atou32(b
, &rule
->table
);
1163 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
1166 } else if (streq(a
, "fwmark")) {
1168 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
1170 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
1173 } else if (streq(a
, "iif")) {
1175 if (free_and_strdup(&rule
->iif
, b
) < 0)
1178 } else if (streq(a
, "oif")) {
1180 if (free_and_strdup(&rule
->oif
, b
) < 0)
1182 } else if (streq(a
, "protocol")) {
1183 r
= safe_atou8(b
, &rule
->protocol
);
1185 log_error_errno(r
, "Failed to parse RPDB rule protocol, ignoring: %s", b
);
1188 } else if (streq(a
, "sourceport")) {
1190 r
= parse_ip_port_range(b
, &low
, &high
);
1192 log_error_errno(r
, "Invalid routing policy rule source port range, ignoring assignment:'%s'", b
);
1196 rule
->sport
.start
= low
;
1197 rule
->sport
.end
= high
;
1199 } else if (streq(a
, "destinationport")) {
1201 r
= parse_ip_port_range(b
, &low
, &high
);
1203 log_error_errno(r
, "Invalid routing policy rule destination port range, ignoring assignment:'%s'", b
);
1207 rule
->dport
.start
= low
;
1208 rule
->dport
.end
= high
;
1212 r
= set_put(*rules
, rule
);
1214 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1224 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1225 RoutingPolicyRule
*rule
, *existing
;
1232 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1233 existing
= set_get(m
->rules_foreign
, rule
);
1236 r
= routing_policy_rule_remove(rule
, link
, routing_policy_rule_remove_handler
);
1238 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1242 link
->routing_policy_rule_remove_messages
++;