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
= new0(RoutingPolicyRule
, 1);
24 rule
->family
= AF_INET
;
25 rule
->table
= RT_TABLE_MAIN
;
31 void routing_policy_rule_free(RoutingPolicyRule
*rule
) {
37 LIST_REMOVE(rules
, rule
->network
->rules
, rule
);
38 assert(rule
->network
->n_rules
> 0);
39 rule
->network
->n_rules
--;
42 hashmap_remove(rule
->network
->rules_by_section
, rule
->section
);
43 network_config_section_free(rule
->section
);
49 set_remove(rule
->manager
->rules
, rule
);
50 set_remove(rule
->manager
->rules_foreign
, rule
);
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
,
179 existing
= set_get(m
->rules
, &rule
);
187 if (m
->rules_foreign
) {
188 existing
= set_get(m
->rules_foreign
, &rule
);
199 int routing_policy_rule_make_local(Manager
*m
, RoutingPolicyRule
*rule
) {
204 if (set_contains(m
->rules_foreign
, rule
)) {
205 set_remove(m
->rules_foreign
, rule
);
207 r
= set_ensure_allocated(&m
->rules
, &routing_policy_rule_hash_ops
);
211 return set_put(m
->rules
, rule
);
217 static int routing_policy_rule_add_internal(Manager
*m
,
220 const union in_addr_union
*from
,
221 uint8_t from_prefixlen
,
222 const union in_addr_union
*to
,
223 uint8_t to_prefixlen
,
229 RoutingPolicyRule
**ret
) {
231 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
234 assert_return(rules
, -EINVAL
);
236 r
= routing_policy_rule_new(&rule
);
241 rule
->family
= family
;
243 rule
->from_prefixlen
= from_prefixlen
;
245 rule
->to_prefixlen
= to_prefixlen
;
247 rule
->fwmark
= fwmark
;
252 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
256 r
= set_put(*rules
, rule
);
268 int routing_policy_rule_add(Manager
*m
,
270 const union in_addr_union
*from
,
271 uint8_t from_prefixlen
,
272 const union in_addr_union
*to
,
273 uint8_t to_prefixlen
,
279 RoutingPolicyRule
**ret
) {
281 return routing_policy_rule_add_internal(m
, &m
->rules
, family
, from
, from_prefixlen
, to
, to_prefixlen
, tos
, fwmark
, table
, iif
, oif
, ret
);
284 int routing_policy_rule_add_foreign(Manager
*m
,
286 const union in_addr_union
*from
,
287 uint8_t from_prefixlen
,
288 const union in_addr_union
*to
,
289 uint8_t to_prefixlen
,
295 RoutingPolicyRule
**ret
) {
296 return routing_policy_rule_add_internal(m
, &m
->rules_foreign
, family
, from
, from_prefixlen
, to
, to_prefixlen
, tos
, fwmark
, table
, iif
, oif
, ret
);
299 static int routing_policy_rule_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
300 _cleanup_(link_unrefp
) Link
*link
= userdata
;
305 assert(link
->ifname
);
307 link
->routing_policy_rule_remove_messages
--;
309 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
312 r
= sd_netlink_message_get_errno(m
);
314 log_link_warning_errno(link
, r
, "Could not drop routing policy rule: %m");
319 int routing_policy_rule_remove(RoutingPolicyRule
*routing_policy_rule
, Link
*link
, sd_netlink_message_handler_t callback
) {
320 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
323 assert(routing_policy_rule
);
325 assert(link
->manager
);
326 assert(link
->manager
->rtnl
);
327 assert(link
->ifindex
> 0);
328 assert(IN_SET(routing_policy_rule
->family
, AF_INET
, AF_INET6
));
330 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_DELRULE
, routing_policy_rule
->family
);
332 return log_error_errno(r
, "Could not allocate RTM_DELRULE message: %m");
334 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->from
)) {
335 if (routing_policy_rule
->family
== AF_INET
)
336 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in
);
338 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in6
);
341 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
343 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, routing_policy_rule
->from_prefixlen
);
345 return log_error_errno(r
, "Could not set source prefix length: %m");
348 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->to
)) {
349 if (routing_policy_rule
->family
== AF_INET
)
350 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in
);
352 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in6
);
355 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
357 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, routing_policy_rule
->to_prefixlen
);
359 return log_error_errno(r
, "Could not set destination prefix length: %m");
362 r
= sd_netlink_call_async(link
->manager
->rtnl
, m
, callback
, link
, 0, NULL
);
364 return log_error_errno(r
, "Could not send rtnetlink message: %m");
371 static int routing_policy_rule_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutingPolicyRule
**ret
) {
372 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
373 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
378 assert(!!filename
== (section_line
> 0));
380 r
= network_config_section_new(filename
, section_line
, &n
);
384 rule
= hashmap_get(network
->rules_by_section
, n
);
386 *ret
= TAKE_PTR(rule
);
391 r
= routing_policy_rule_new(&rule
);
396 rule
->network
= network
;
399 r
= hashmap_put(network
->rules_by_section
, rule
->section
, rule
);
403 LIST_APPEND(rules
, network
->rules
, rule
);
406 *ret
= TAKE_PTR(rule
);
411 int link_routing_policy_rule_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
412 _cleanup_(link_unrefp
) Link
*link
= userdata
;
418 assert(link
->ifname
);
419 assert(link
->routing_policy_rule_messages
> 0);
421 link
->routing_policy_rule_messages
--;
423 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
426 r
= sd_netlink_message_get_errno(m
);
427 if (r
< 0 && r
!= -EEXIST
)
428 log_link_warning_errno(link
, r
, "Could not add routing policy rule: %m");
430 if (link
->routing_policy_rule_messages
== 0) {
431 log_link_debug(link
, "Routing policy rule configured");
432 link
->routing_policy_rules_configured
= true;
433 link_check_ready(link
);
439 int routing_policy_rule_configure(RoutingPolicyRule
*rule
, Link
*link
, sd_netlink_message_handler_t callback
, bool update
) {
440 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
445 assert(link
->ifindex
> 0);
446 assert(link
->manager
);
447 assert(link
->manager
->rtnl
);
449 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
451 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
453 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
454 if (rule
->family
== AF_INET
)
455 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &rule
->from
.in
);
457 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &rule
->from
.in6
);
460 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
462 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
464 return log_error_errno(r
, "Could not set source prefix length: %m");
467 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
468 if (rule
->family
== AF_INET
)
469 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &rule
->to
.in
);
471 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &rule
->to
.in6
);
474 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
476 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
478 return log_error_errno(r
, "Could not set destination prefix length: %m");
481 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
483 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
486 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
488 return log_error_errno(r
, "Could not set ip rule tos: %m");
491 if (rule
->table
< 256) {
492 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
494 return log_error_errno(r
, "Could not set ip rule table: %m");
496 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
498 return log_error_errno(r
, "Could not set ip rule table: %m");
500 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
502 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
505 if (rule
->fwmark
> 0) {
506 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
508 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
511 if (rule
->fwmask
> 0) {
512 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
514 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
518 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
520 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
524 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
526 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
531 r
= sd_netlink_call_async(link
->manager
->rtnl
, m
, callback
, link
, 0, NULL
);
533 return log_error_errno(r
, "Could not send rtnetlink message: %m");
537 r
= routing_policy_rule_add(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
538 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, NULL
);
540 return log_error_errno(r
, "Could not add rule : %m");
545 static int parse_fwmark_fwmask(const char *s
, uint32_t *fwmark
, uint32_t *fwmask
) {
546 _cleanup_free_
char *f
= NULL
;
560 r
= safe_atou32(f
, fwmark
);
562 return log_error_errno(r
, "Failed to parse RPDB rule firewall mark, ignoring: %s", f
);
565 r
= safe_atou32(p
, fwmask
);
567 return log_error_errno(r
, "Failed to parse RPDB rule mask, ignoring: %s", f
);
573 int config_parse_routing_policy_rule_tos(
575 const char *filename
,
578 unsigned section_line
,
585 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
586 Network
*network
= userdata
;
595 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
599 r
= safe_atou8(rvalue
, &n
->tos
);
601 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule tos, ignoring: %s", rvalue
);
610 int config_parse_routing_policy_rule_priority(
612 const char *filename
,
615 unsigned section_line
,
622 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
623 Network
*network
= userdata
;
632 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
636 r
= safe_atou32(rvalue
, &n
->priority
);
638 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule priority, ignoring: %s", rvalue
);
647 int config_parse_routing_policy_rule_table(
649 const char *filename
,
652 unsigned section_line
,
659 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
660 Network
*network
= userdata
;
669 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
673 r
= safe_atou32(rvalue
, &n
->table
);
675 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule table, ignoring: %s", rvalue
);
684 int config_parse_routing_policy_rule_fwmark_mask(
686 const char *filename
,
689 unsigned section_line
,
696 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
697 Network
*network
= userdata
;
706 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
710 r
= parse_fwmark_fwmask(rvalue
, &n
->fwmark
, &n
->fwmask
);
712 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue
);
721 int config_parse_routing_policy_rule_prefix(
723 const char *filename
,
726 unsigned section_line
,
733 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
734 Network
*network
= userdata
;
735 union in_addr_union buffer
;
745 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
749 r
= in_addr_prefix_from_string(rvalue
, AF_INET
, &buffer
, &prefixlen
);
751 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &buffer
, &prefixlen
);
753 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
757 n
->family
= AF_INET6
;
761 if (streq(lvalue
, "To")) {
763 n
->to_prefixlen
= prefixlen
;
766 n
->from_prefixlen
= prefixlen
;
774 int config_parse_routing_policy_rule_device(
776 const char *filename
,
779 unsigned section_line
,
786 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
787 Network
*network
= userdata
;
796 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
800 if (!ifname_valid(rvalue
)) {
801 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse '%s' interface name, ignoring: %s", lvalue
, rvalue
);
805 if (streq(lvalue
, "IncomingInterface")) {
806 r
= free_and_strdup(&n
->iif
, rvalue
);
810 r
= free_and_strdup(&n
->oif
, rvalue
);
820 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
821 _cleanup_free_
char *s
= NULL
;
827 r
= read_full_file(state_file
, &s
, &size
);
840 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
841 RoutingPolicyRule
*rule
= NULL
;
847 SET_FOREACH(rule
, rules
, i
) {
848 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
853 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
854 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
858 fprintf(f
, "from=%s/%hhu",
859 from_str
, rule
->from_prefixlen
);
863 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
864 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
868 fprintf(f
, "%sto=%s/%hhu",
870 to_str
, rule
->to_prefixlen
);
874 if (rule
->tos
!= 0) {
875 fprintf(f
, "%stos=%hhu",
881 if (rule
->fwmark
!= 0) {
882 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
884 rule
->fwmark
, rule
->fwmask
);
889 fprintf(f
, "%siif=%s",
896 fprintf(f
, "%soif=%s",
902 fprintf(f
, "%stable=%"PRIu32
"\n",
910 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
911 _cleanup_strv_free_
char **l
= NULL
;
912 _cleanup_free_
char *data
= NULL
;
920 r
= routing_policy_rule_read_full_file(state_file
, &data
);
924 l
= strv_split_newlines(data
);
928 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
933 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
935 p
= startswith(*i
, "RULE=");
939 r
= routing_policy_rule_new(&rule
);
944 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
945 union in_addr_union buffer
;
948 r
= extract_first_word(&p
, &word
, NULL
, 0);
954 r
= split_pair(word
, "=", &a
, &b
);
958 if (STR_IN_SET(a
, "from", "to")) {
960 r
= in_addr_prefix_from_string(b
, AF_INET
, &buffer
, &prefixlen
);
962 r
= in_addr_prefix_from_string(b
, AF_INET6
, &buffer
, &prefixlen
);
964 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
968 rule
->family
= AF_INET6
;
970 rule
->family
= AF_INET
;
972 if (streq(a
, "to")) {
974 rule
->to_prefixlen
= prefixlen
;
977 rule
->from_prefixlen
= prefixlen
;
979 } else if (streq(a
, "tos")) {
980 r
= safe_atou8(b
, &rule
->tos
);
982 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
985 } else if (streq(a
, "table")) {
986 r
= safe_atou32(b
, &rule
->table
);
988 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
991 } else if (streq(a
, "fwmark")) {
993 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
995 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
998 } else if (streq(a
, "iif")) {
1000 if (free_and_strdup(&rule
->iif
, b
) < 0)
1003 } else if (streq(a
, "oif")) {
1005 if (free_and_strdup(&rule
->oif
, b
) < 0)
1010 r
= set_put(*rules
, rule
);
1012 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1022 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1023 RoutingPolicyRule
*rule
, *existing
;
1030 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1031 existing
= set_get(m
->rules_foreign
, rule
);
1034 r
= routing_policy_rule_remove(rule
, link
, routing_policy_rule_remove_handler
);
1036 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1040 link
->routing_policy_rule_remove_messages
++;