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
);
45 network_config_section_free(rule
->section
);
51 set_remove(rule
->manager
->rules
, rule
);
52 set_remove(rule
->manager
->rules_foreign
, rule
);
60 static void routing_policy_rule_hash_func(const void *b
, struct siphash
*state
) {
61 const RoutingPolicyRule
*rule
= b
;
65 siphash24_compress(&rule
->family
, sizeof(rule
->family
), state
);
67 switch (rule
->family
) {
71 siphash24_compress(&rule
->from
, FAMILY_ADDRESS_SIZE(rule
->family
), state
);
72 siphash24_compress(&rule
->from_prefixlen
, sizeof(rule
->from_prefixlen
), state
);
74 siphash24_compress(&rule
->to
, FAMILY_ADDRESS_SIZE(rule
->family
), state
);
75 siphash24_compress(&rule
->to_prefixlen
, sizeof(rule
->to_prefixlen
), state
);
77 siphash24_compress(&rule
->tos
, sizeof(rule
->tos
), state
);
78 siphash24_compress(&rule
->fwmark
, sizeof(rule
->fwmark
), state
);
79 siphash24_compress(&rule
->table
, sizeof(rule
->table
), state
);
82 siphash24_compress(&rule
->iif
, strlen(rule
->iif
), state
);
85 siphash24_compress(&rule
->oif
, strlen(rule
->oif
), state
);
89 /* treat any other address family as AF_UNSPEC */
94 static int routing_policy_rule_compare_func(const void *_a
, const void *_b
) {
95 const RoutingPolicyRule
*a
= _a
, *b
= _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
= memcmp(&a
->from
, &b
->from
, FAMILY_ADDRESS_SIZE(a
->family
));
137 return memcmp(&a
->to
, &b
->to
, FAMILY_ADDRESS_SIZE(a
->family
));
140 /* treat any other address family as AF_UNSPEC */
145 const struct hash_ops routing_policy_rule_hash_ops
= {
146 .hash
= routing_policy_rule_hash_func
,
147 .compare
= routing_policy_rule_compare_func
150 int routing_policy_rule_get(Manager
*m
,
152 const union in_addr_union
*from
,
153 uint8_t from_prefixlen
,
154 const union in_addr_union
*to
,
155 uint8_t to_prefixlen
,
161 RoutingPolicyRule
**ret
) {
163 RoutingPolicyRule rule
, *existing
;
165 assert_return(m
, -1);
167 rule
= (RoutingPolicyRule
) {
170 .from_prefixlen
= from_prefixlen
,
172 .to_prefixlen
= to_prefixlen
,
180 existing
= set_get(m
->rules
, &rule
);
187 existing
= set_get(m
->rules_foreign
, &rule
);
197 int routing_policy_rule_make_local(Manager
*m
, RoutingPolicyRule
*rule
) {
202 if (set_contains(m
->rules_foreign
, rule
)) {
203 set_remove(m
->rules_foreign
, rule
);
205 r
= set_ensure_allocated(&m
->rules
, &routing_policy_rule_hash_ops
);
209 return set_put(m
->rules
, rule
);
215 static int routing_policy_rule_add_internal(Manager
*m
,
218 const union in_addr_union
*from
,
219 uint8_t from_prefixlen
,
220 const union in_addr_union
*to
,
221 uint8_t to_prefixlen
,
227 RoutingPolicyRule
**ret
) {
229 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
230 _cleanup_free_
char *iif
= NULL
, *oif
= NULL
;
233 assert_return(rules
, -EINVAL
);
247 r
= routing_policy_rule_new(&rule
);
252 rule
->family
= family
;
254 rule
->from_prefixlen
= from_prefixlen
;
256 rule
->to_prefixlen
= to_prefixlen
;
258 rule
->fwmark
= fwmark
;
263 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
267 r
= set_put(*rules
, rule
);
280 int routing_policy_rule_add(Manager
*m
,
282 const union in_addr_union
*from
,
283 uint8_t from_prefixlen
,
284 const union in_addr_union
*to
,
285 uint8_t to_prefixlen
,
291 RoutingPolicyRule
**ret
) {
293 return routing_policy_rule_add_internal(m
, &m
->rules
, family
, from
, from_prefixlen
, to
, to_prefixlen
, tos
, fwmark
, table
, iif
, oif
, ret
);
296 int routing_policy_rule_add_foreign(Manager
*m
,
298 const union in_addr_union
*from
,
299 uint8_t from_prefixlen
,
300 const union in_addr_union
*to
,
301 uint8_t to_prefixlen
,
307 RoutingPolicyRule
**ret
) {
308 return routing_policy_rule_add_internal(m
, &m
->rules_foreign
, family
, from
, from_prefixlen
, to
, to_prefixlen
, tos
, fwmark
, table
, iif
, oif
, ret
);
311 static int routing_policy_rule_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
312 Link
*link
= userdata
;
317 assert(link
->ifname
);
319 link
->routing_policy_rule_remove_messages
--;
321 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
324 r
= sd_netlink_message_get_errno(m
);
326 log_link_warning_errno(link
, r
, "Could not drop routing policy rule: %m");
331 int routing_policy_rule_remove(RoutingPolicyRule
*routing_policy_rule
, Link
*link
, sd_netlink_message_handler_t callback
) {
332 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
335 assert(routing_policy_rule
);
337 assert(link
->manager
);
338 assert(link
->manager
->rtnl
);
339 assert(link
->ifindex
> 0);
340 assert(IN_SET(routing_policy_rule
->family
, AF_INET
, AF_INET6
));
342 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_DELRULE
, routing_policy_rule
->family
);
344 return log_error_errno(r
, "Could not allocate RTM_DELRULE message: %m");
346 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->from
)) {
347 if (routing_policy_rule
->family
== AF_INET
)
348 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in
);
350 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in6
);
353 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
355 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, routing_policy_rule
->from_prefixlen
);
357 return log_error_errno(r
, "Could not set source prefix length: %m");
360 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->to
)) {
361 if (routing_policy_rule
->family
== AF_INET
)
362 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in
);
364 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in6
);
367 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
369 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, routing_policy_rule
->to_prefixlen
);
371 return log_error_errno(r
, "Could not set destination prefix length: %m");
374 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, m
, callback
,
375 link_netlink_destroy_callback
, link
, 0, __func__
);
377 return log_error_errno(r
, "Could not send rtnetlink message: %m");
384 static int routing_policy_rule_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutingPolicyRule
**ret
) {
385 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
386 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
391 assert(!!filename
== (section_line
> 0));
393 r
= network_config_section_new(filename
, section_line
, &n
);
397 rule
= hashmap_get(network
->rules_by_section
, n
);
399 *ret
= TAKE_PTR(rule
);
404 r
= routing_policy_rule_new(&rule
);
408 rule
->section
= TAKE_PTR(n
);
409 rule
->network
= network
;
411 r
= hashmap_put(network
->rules_by_section
, rule
->section
, rule
);
415 LIST_APPEND(rules
, network
->rules
, rule
);
418 *ret
= TAKE_PTR(rule
);
423 int link_routing_policy_rule_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
424 Link
*link
= userdata
;
430 assert(link
->ifname
);
431 assert(link
->routing_policy_rule_messages
> 0);
433 link
->routing_policy_rule_messages
--;
435 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
438 r
= sd_netlink_message_get_errno(m
);
439 if (r
< 0 && r
!= -EEXIST
)
440 log_link_warning_errno(link
, r
, "Could not add routing policy rule: %m");
442 if (link
->routing_policy_rule_messages
== 0) {
443 log_link_debug(link
, "Routing policy rule configured");
444 link
->routing_policy_rules_configured
= true;
445 link_check_ready(link
);
451 int routing_policy_rule_configure(RoutingPolicyRule
*rule
, Link
*link
, sd_netlink_message_handler_t callback
, bool update
) {
452 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
457 assert(link
->ifindex
> 0);
458 assert(link
->manager
);
459 assert(link
->manager
->rtnl
);
461 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
463 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
465 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
466 if (rule
->family
== AF_INET
)
467 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &rule
->from
.in
);
469 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &rule
->from
.in6
);
472 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
474 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
476 return log_error_errno(r
, "Could not set source prefix length: %m");
479 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
480 if (rule
->family
== AF_INET
)
481 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &rule
->to
.in
);
483 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &rule
->to
.in6
);
486 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
488 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
490 return log_error_errno(r
, "Could not set destination prefix length: %m");
493 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
495 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
498 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
500 return log_error_errno(r
, "Could not set ip rule tos: %m");
503 if (rule
->table
< 256) {
504 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
506 return log_error_errno(r
, "Could not set ip rule table: %m");
508 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
510 return log_error_errno(r
, "Could not set ip rule table: %m");
512 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
514 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
517 if (rule
->fwmark
> 0) {
518 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
520 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
523 if (rule
->fwmask
> 0) {
524 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
526 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
530 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
532 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
536 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
538 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
543 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, m
, callback
,
544 link_netlink_destroy_callback
, link
, 0, __func__
);
546 return log_error_errno(r
, "Could not send rtnetlink message: %m");
550 r
= routing_policy_rule_add(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
551 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, NULL
);
553 return log_error_errno(r
, "Could not add rule : %m");
558 static int parse_fwmark_fwmask(const char *s
, uint32_t *fwmark
, uint32_t *fwmask
) {
559 _cleanup_free_
char *f
= NULL
;
573 r
= safe_atou32(f
, fwmark
);
575 return log_error_errno(r
, "Failed to parse RPDB rule firewall mark, ignoring: %s", f
);
578 r
= safe_atou32(p
, fwmask
);
580 return log_error_errno(r
, "Failed to parse RPDB rule mask, ignoring: %s", f
);
586 int config_parse_routing_policy_rule_tos(
588 const char *filename
,
591 unsigned section_line
,
598 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
599 Network
*network
= userdata
;
608 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
612 r
= safe_atou8(rvalue
, &n
->tos
);
614 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule tos, ignoring: %s", rvalue
);
623 int config_parse_routing_policy_rule_priority(
625 const char *filename
,
628 unsigned section_line
,
635 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
636 Network
*network
= userdata
;
645 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
649 r
= safe_atou32(rvalue
, &n
->priority
);
651 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule priority, ignoring: %s", rvalue
);
660 int config_parse_routing_policy_rule_table(
662 const char *filename
,
665 unsigned section_line
,
672 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
673 Network
*network
= userdata
;
682 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
686 r
= safe_atou32(rvalue
, &n
->table
);
688 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule table, ignoring: %s", rvalue
);
697 int config_parse_routing_policy_rule_fwmark_mask(
699 const char *filename
,
702 unsigned section_line
,
709 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
710 Network
*network
= userdata
;
719 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
723 r
= parse_fwmark_fwmask(rvalue
, &n
->fwmark
, &n
->fwmask
);
725 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue
);
734 int config_parse_routing_policy_rule_prefix(
736 const char *filename
,
739 unsigned section_line
,
746 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
747 Network
*network
= userdata
;
748 union in_addr_union buffer
;
758 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
762 r
= in_addr_prefix_from_string(rvalue
, AF_INET
, &buffer
, &prefixlen
);
764 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &buffer
, &prefixlen
);
766 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
770 n
->family
= AF_INET6
;
774 if (streq(lvalue
, "To")) {
776 n
->to_prefixlen
= prefixlen
;
779 n
->from_prefixlen
= prefixlen
;
787 int config_parse_routing_policy_rule_device(
789 const char *filename
,
792 unsigned section_line
,
799 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
800 Network
*network
= userdata
;
809 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
813 if (!ifname_valid(rvalue
)) {
814 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse '%s' interface name, ignoring: %s", lvalue
, rvalue
);
818 if (streq(lvalue
, "IncomingInterface")) {
819 r
= free_and_strdup(&n
->iif
, rvalue
);
823 r
= free_and_strdup(&n
->oif
, rvalue
);
833 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
834 _cleanup_free_
char *s
= NULL
;
840 r
= read_full_file(state_file
, &s
, &size
);
853 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
854 RoutingPolicyRule
*rule
= NULL
;
860 SET_FOREACH(rule
, rules
, i
) {
861 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
866 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
867 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
871 fprintf(f
, "from=%s/%hhu",
872 from_str
, rule
->from_prefixlen
);
876 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
877 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
881 fprintf(f
, "%sto=%s/%hhu",
883 to_str
, rule
->to_prefixlen
);
887 if (rule
->tos
!= 0) {
888 fprintf(f
, "%stos=%hhu",
894 if (rule
->fwmark
!= 0) {
895 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
897 rule
->fwmark
, rule
->fwmask
);
902 fprintf(f
, "%siif=%s",
909 fprintf(f
, "%soif=%s",
915 fprintf(f
, "%stable=%"PRIu32
"\n",
923 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
924 _cleanup_strv_free_
char **l
= NULL
;
925 _cleanup_free_
char *data
= NULL
;
933 r
= routing_policy_rule_read_full_file(state_file
, &data
);
937 l
= strv_split_newlines(data
);
941 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
946 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
948 p
= startswith(*i
, "RULE=");
952 r
= routing_policy_rule_new(&rule
);
957 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
958 union in_addr_union buffer
;
961 r
= extract_first_word(&p
, &word
, NULL
, 0);
967 r
= split_pair(word
, "=", &a
, &b
);
971 if (STR_IN_SET(a
, "from", "to")) {
973 r
= in_addr_prefix_from_string(b
, AF_INET
, &buffer
, &prefixlen
);
975 r
= in_addr_prefix_from_string(b
, AF_INET6
, &buffer
, &prefixlen
);
977 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
981 rule
->family
= AF_INET6
;
983 rule
->family
= AF_INET
;
985 if (streq(a
, "to")) {
987 rule
->to_prefixlen
= prefixlen
;
990 rule
->from_prefixlen
= prefixlen
;
992 } else if (streq(a
, "tos")) {
993 r
= safe_atou8(b
, &rule
->tos
);
995 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
998 } else if (streq(a
, "table")) {
999 r
= safe_atou32(b
, &rule
->table
);
1001 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
1004 } else if (streq(a
, "fwmark")) {
1006 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
1008 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
1011 } else if (streq(a
, "iif")) {
1013 if (free_and_strdup(&rule
->iif
, b
) < 0)
1016 } else if (streq(a
, "oif")) {
1018 if (free_and_strdup(&rule
->oif
, b
) < 0)
1023 r
= set_put(*rules
, rule
);
1025 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1035 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1036 RoutingPolicyRule
*rule
, *existing
;
1043 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1044 existing
= set_get(m
->rules_foreign
, rule
);
1047 r
= routing_policy_rule_remove(rule
, link
, routing_policy_rule_remove_handler
);
1049 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1053 link
->routing_policy_rule_remove_messages
++;