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-util.h"
14 #include "string-util.h"
17 int routing_policy_rule_new(RoutingPolicyRule
**ret
) {
18 RoutingPolicyRule
*rule
;
20 rule
= new(RoutingPolicyRule
, 1);
24 *rule
= (RoutingPolicyRule
) {
26 .table
= RT_TABLE_MAIN
,
33 void routing_policy_rule_free(RoutingPolicyRule
*rule
) {
39 LIST_REMOVE(rules
, rule
->network
->rules
, rule
);
40 assert(rule
->network
->n_rules
> 0);
41 rule
->network
->n_rules
--;
44 hashmap_remove(rule
->network
->rules_by_section
, rule
->section
);
48 set_remove(rule
->manager
->rules
, rule
);
49 set_remove(rule
->manager
->rules_foreign
, rule
);
52 network_config_section_free(rule
->section
);
58 static void routing_policy_rule_hash_func(const void *b
, struct siphash
*state
) {
59 const RoutingPolicyRule
*rule
= b
;
63 siphash24_compress(&rule
->family
, sizeof(rule
->family
), state
);
65 switch (rule
->family
) {
69 siphash24_compress(&rule
->from
, FAMILY_ADDRESS_SIZE(rule
->family
), state
);
70 siphash24_compress(&rule
->from_prefixlen
, sizeof(rule
->from_prefixlen
), state
);
72 siphash24_compress(&rule
->to
, FAMILY_ADDRESS_SIZE(rule
->family
), state
);
73 siphash24_compress(&rule
->to_prefixlen
, sizeof(rule
->to_prefixlen
), state
);
75 siphash24_compress(&rule
->tos
, sizeof(rule
->tos
), state
);
76 siphash24_compress(&rule
->fwmark
, sizeof(rule
->fwmark
), state
);
77 siphash24_compress(&rule
->table
, sizeof(rule
->table
), state
);
80 siphash24_compress(rule
->iif
, strlen(rule
->iif
), state
);
83 siphash24_compress(rule
->oif
, strlen(rule
->oif
), state
);
87 /* treat any other address family as AF_UNSPEC */
92 static int routing_policy_rule_compare_func(const void *_a
, const void *_b
) {
93 const RoutingPolicyRule
*a
= _a
, *b
= _b
;
96 r
= CMP(a
->family
, b
->family
);
103 r
= CMP(a
->from_prefixlen
, b
->from_prefixlen
);
107 r
= CMP(a
->to_prefixlen
, b
->to_prefixlen
);
111 r
= CMP(a
->tos
, b
->tos
);
115 r
= CMP(a
->fwmask
, b
->fwmask
);
119 r
= CMP(a
->table
, b
->table
);
123 r
= strcmp_ptr(a
->iif
, b
->iif
);
127 r
= strcmp_ptr(a
->oif
, b
->oif
);
131 r
= memcmp(&a
->from
, &b
->from
, FAMILY_ADDRESS_SIZE(a
->family
));
135 return memcmp(&a
->to
, &b
->to
, FAMILY_ADDRESS_SIZE(a
->family
));
138 /* treat any other address family as AF_UNSPEC */
143 const struct hash_ops routing_policy_rule_hash_ops
= {
144 .hash
= routing_policy_rule_hash_func
,
145 .compare
= routing_policy_rule_compare_func
148 int routing_policy_rule_get(Manager
*m
,
150 const union in_addr_union
*from
,
151 uint8_t from_prefixlen
,
152 const union in_addr_union
*to
,
153 uint8_t to_prefixlen
,
159 RoutingPolicyRule
**ret
) {
161 RoutingPolicyRule rule
, *existing
;
163 assert_return(m
, -1);
165 rule
= (RoutingPolicyRule
) {
168 .from_prefixlen
= from_prefixlen
,
170 .to_prefixlen
= to_prefixlen
,
178 existing
= set_get(m
->rules
, &rule
);
185 existing
= set_get(m
->rules_foreign
, &rule
);
195 int routing_policy_rule_make_local(Manager
*m
, RoutingPolicyRule
*rule
) {
200 if (set_contains(m
->rules_foreign
, rule
)) {
201 set_remove(m
->rules_foreign
, rule
);
203 r
= set_ensure_allocated(&m
->rules
, &routing_policy_rule_hash_ops
);
207 return set_put(m
->rules
, rule
);
213 static int routing_policy_rule_add_internal(Manager
*m
,
216 const union in_addr_union
*from
,
217 uint8_t from_prefixlen
,
218 const union in_addr_union
*to
,
219 uint8_t to_prefixlen
,
225 RoutingPolicyRule
**ret
) {
227 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
228 _cleanup_free_
char *iif
= NULL
, *oif
= NULL
;
231 assert_return(rules
, -EINVAL
);
245 r
= routing_policy_rule_new(&rule
);
250 rule
->family
= family
;
252 rule
->from_prefixlen
= from_prefixlen
;
254 rule
->to_prefixlen
= to_prefixlen
;
256 rule
->fwmark
= fwmark
;
261 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
265 r
= set_put(*rules
, rule
);
278 int routing_policy_rule_add(Manager
*m
,
280 const union in_addr_union
*from
,
281 uint8_t from_prefixlen
,
282 const union in_addr_union
*to
,
283 uint8_t to_prefixlen
,
289 RoutingPolicyRule
**ret
) {
291 return routing_policy_rule_add_internal(m
, &m
->rules
, family
, from
, from_prefixlen
, to
, to_prefixlen
, tos
, fwmark
, table
, iif
, oif
, ret
);
294 int routing_policy_rule_add_foreign(Manager
*m
,
296 const union in_addr_union
*from
,
297 uint8_t from_prefixlen
,
298 const union in_addr_union
*to
,
299 uint8_t to_prefixlen
,
305 RoutingPolicyRule
**ret
) {
306 return routing_policy_rule_add_internal(m
, &m
->rules_foreign
, family
, from
, from_prefixlen
, to
, to_prefixlen
, tos
, fwmark
, table
, iif
, oif
, ret
);
309 static int routing_policy_rule_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
310 Link
*link
= userdata
;
315 assert(link
->ifname
);
317 link
->routing_policy_rule_remove_messages
--;
319 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
322 r
= sd_netlink_message_get_errno(m
);
324 log_link_warning_errno(link
, r
, "Could not drop routing policy rule: %m");
329 int routing_policy_rule_remove(RoutingPolicyRule
*routing_policy_rule
, Link
*link
, sd_netlink_message_handler_t callback
) {
330 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
333 assert(routing_policy_rule
);
335 assert(link
->manager
);
336 assert(link
->manager
->rtnl
);
337 assert(link
->ifindex
> 0);
338 assert(IN_SET(routing_policy_rule
->family
, AF_INET
, AF_INET6
));
340 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_DELRULE
, routing_policy_rule
->family
);
342 return log_error_errno(r
, "Could not allocate RTM_DELRULE message: %m");
344 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->from
)) {
345 if (routing_policy_rule
->family
== AF_INET
)
346 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in
);
348 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in6
);
351 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
353 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, routing_policy_rule
->from_prefixlen
);
355 return log_error_errno(r
, "Could not set source prefix length: %m");
358 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->to
)) {
359 if (routing_policy_rule
->family
== AF_INET
)
360 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in
);
362 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in6
);
365 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
367 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, routing_policy_rule
->to_prefixlen
);
369 return log_error_errno(r
, "Could not set destination prefix length: %m");
372 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, m
, callback
,
373 link_netlink_destroy_callback
, link
, 0, __func__
);
375 return log_error_errno(r
, "Could not send rtnetlink message: %m");
382 static int routing_policy_rule_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutingPolicyRule
**ret
) {
383 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
384 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
389 assert(!!filename
== (section_line
> 0));
392 r
= network_config_section_new(filename
, section_line
, &n
);
396 rule
= hashmap_get(network
->rules_by_section
, n
);
398 *ret
= TAKE_PTR(rule
);
404 r
= routing_policy_rule_new(&rule
);
408 rule
->network
= network
;
409 LIST_APPEND(rules
, network
->rules
, rule
);
413 rule
->section
= TAKE_PTR(n
);
415 r
= hashmap_ensure_allocated(&network
->rules_by_section
, &network_config_hash_ops
);
419 r
= hashmap_put(network
->rules_by_section
, rule
->section
, rule
);
424 *ret
= TAKE_PTR(rule
);
429 int link_routing_policy_rule_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
430 Link
*link
= userdata
;
436 assert(link
->ifname
);
437 assert(link
->routing_policy_rule_messages
> 0);
439 link
->routing_policy_rule_messages
--;
441 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
444 r
= sd_netlink_message_get_errno(m
);
445 if (r
< 0 && r
!= -EEXIST
)
446 log_link_warning_errno(link
, r
, "Could not add routing policy rule: %m");
448 if (link
->routing_policy_rule_messages
== 0) {
449 log_link_debug(link
, "Routing policy rule configured");
450 link
->routing_policy_rules_configured
= true;
451 link_check_ready(link
);
457 int routing_policy_rule_configure(RoutingPolicyRule
*rule
, Link
*link
, sd_netlink_message_handler_t callback
, bool update
) {
458 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
463 assert(link
->ifindex
> 0);
464 assert(link
->manager
);
465 assert(link
->manager
->rtnl
);
467 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
469 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
471 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
472 if (rule
->family
== AF_INET
)
473 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &rule
->from
.in
);
475 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &rule
->from
.in6
);
478 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
480 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
482 return log_error_errno(r
, "Could not set source prefix length: %m");
485 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
486 if (rule
->family
== AF_INET
)
487 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &rule
->to
.in
);
489 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &rule
->to
.in6
);
492 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
494 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
496 return log_error_errno(r
, "Could not set destination prefix length: %m");
499 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
501 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
504 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
506 return log_error_errno(r
, "Could not set ip rule tos: %m");
509 if (rule
->table
< 256) {
510 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
512 return log_error_errno(r
, "Could not set ip rule table: %m");
514 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
516 return log_error_errno(r
, "Could not set ip rule table: %m");
518 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
520 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
523 if (rule
->fwmark
> 0) {
524 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
526 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
529 if (rule
->fwmask
> 0) {
530 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
532 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
536 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
538 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
542 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
544 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
549 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, m
, callback
,
550 link_netlink_destroy_callback
, link
, 0, __func__
);
552 return log_error_errno(r
, "Could not send rtnetlink message: %m");
556 r
= routing_policy_rule_add(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
557 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, NULL
);
559 return log_error_errno(r
, "Could not add rule: %m");
564 static int parse_fwmark_fwmask(const char *s
, uint32_t *fwmark
, uint32_t *fwmask
) {
565 _cleanup_free_
char *f
= NULL
;
579 r
= safe_atou32(f
, fwmark
);
581 return log_error_errno(r
, "Failed to parse RPDB rule firewall mark, ignoring: %s", f
);
584 r
= safe_atou32(p
, fwmask
);
586 return log_error_errno(r
, "Failed to parse RPDB rule mask, ignoring: %s", f
);
592 int config_parse_routing_policy_rule_tos(
594 const char *filename
,
597 unsigned section_line
,
604 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
605 Network
*network
= userdata
;
614 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
618 r
= safe_atou8(rvalue
, &n
->tos
);
620 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule tos, ignoring: %s", rvalue
);
629 int config_parse_routing_policy_rule_priority(
631 const char *filename
,
634 unsigned section_line
,
641 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
642 Network
*network
= userdata
;
651 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
655 r
= safe_atou32(rvalue
, &n
->priority
);
657 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule priority, ignoring: %s", rvalue
);
666 int config_parse_routing_policy_rule_table(
668 const char *filename
,
671 unsigned section_line
,
678 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
679 Network
*network
= userdata
;
688 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
692 r
= safe_atou32(rvalue
, &n
->table
);
694 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule table, ignoring: %s", rvalue
);
703 int config_parse_routing_policy_rule_fwmark_mask(
705 const char *filename
,
708 unsigned section_line
,
715 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
716 Network
*network
= userdata
;
725 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
729 r
= parse_fwmark_fwmask(rvalue
, &n
->fwmark
, &n
->fwmask
);
731 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue
);
740 int config_parse_routing_policy_rule_prefix(
742 const char *filename
,
745 unsigned section_line
,
752 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
753 Network
*network
= userdata
;
754 union in_addr_union buffer
;
764 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
768 r
= in_addr_prefix_from_string(rvalue
, AF_INET
, &buffer
, &prefixlen
);
770 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &buffer
, &prefixlen
);
772 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
776 n
->family
= AF_INET6
;
780 if (streq(lvalue
, "To")) {
782 n
->to_prefixlen
= prefixlen
;
785 n
->from_prefixlen
= prefixlen
;
793 int config_parse_routing_policy_rule_device(
795 const char *filename
,
798 unsigned section_line
,
805 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
806 Network
*network
= userdata
;
815 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
819 if (!ifname_valid(rvalue
)) {
820 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse '%s' interface name, ignoring: %s", lvalue
, rvalue
);
824 if (streq(lvalue
, "IncomingInterface")) {
825 r
= free_and_strdup(&n
->iif
, rvalue
);
829 r
= free_and_strdup(&n
->oif
, rvalue
);
839 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
840 _cleanup_free_
char *s
= NULL
;
846 r
= read_full_file(state_file
, &s
, &size
);
859 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
860 RoutingPolicyRule
*rule
= NULL
;
866 SET_FOREACH(rule
, rules
, i
) {
867 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
872 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
873 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
877 fprintf(f
, "from=%s/%hhu",
878 from_str
, rule
->from_prefixlen
);
882 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
883 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
887 fprintf(f
, "%sto=%s/%hhu",
889 to_str
, rule
->to_prefixlen
);
893 if (rule
->tos
!= 0) {
894 fprintf(f
, "%stos=%hhu",
900 if (rule
->fwmark
!= 0) {
901 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
903 rule
->fwmark
, rule
->fwmask
);
908 fprintf(f
, "%siif=%s",
915 fprintf(f
, "%soif=%s",
921 fprintf(f
, "%stable=%"PRIu32
"\n",
929 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
930 _cleanup_strv_free_
char **l
= NULL
;
931 _cleanup_free_
char *data
= NULL
;
939 r
= routing_policy_rule_read_full_file(state_file
, &data
);
943 l
= strv_split_newlines(data
);
947 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
952 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
954 p
= startswith(*i
, "RULE=");
958 r
= routing_policy_rule_new(&rule
);
963 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
964 union in_addr_union buffer
;
967 r
= extract_first_word(&p
, &word
, NULL
, 0);
973 r
= split_pair(word
, "=", &a
, &b
);
977 if (STR_IN_SET(a
, "from", "to")) {
979 r
= in_addr_prefix_from_string(b
, AF_INET
, &buffer
, &prefixlen
);
981 r
= in_addr_prefix_from_string(b
, AF_INET6
, &buffer
, &prefixlen
);
983 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
987 rule
->family
= AF_INET6
;
989 rule
->family
= AF_INET
;
991 if (streq(a
, "to")) {
993 rule
->to_prefixlen
= prefixlen
;
996 rule
->from_prefixlen
= prefixlen
;
998 } else if (streq(a
, "tos")) {
999 r
= safe_atou8(b
, &rule
->tos
);
1001 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
1004 } else if (streq(a
, "table")) {
1005 r
= safe_atou32(b
, &rule
->table
);
1007 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
1010 } else if (streq(a
, "fwmark")) {
1012 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
1014 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
1017 } else if (streq(a
, "iif")) {
1019 if (free_and_strdup(&rule
->iif
, b
) < 0)
1022 } else if (streq(a
, "oif")) {
1024 if (free_and_strdup(&rule
->oif
, b
) < 0)
1029 r
= set_put(*rules
, rule
);
1031 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1041 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1042 RoutingPolicyRule
*rule
, *existing
;
1049 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1050 existing
= set_get(m
->rules_foreign
, rule
);
1053 r
= routing_policy_rule_remove(rule
, link
, routing_policy_rule_remove_handler
);
1055 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1059 link
->routing_policy_rule_remove_messages
++;