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"
16 int routing_policy_rule_new(RoutingPolicyRule
**ret
) {
17 RoutingPolicyRule
*rule
;
19 rule
= new0(RoutingPolicyRule
, 1);
23 rule
->family
= AF_INET
;
24 rule
->table
= RT_TABLE_MAIN
;
30 void routing_policy_rule_free(RoutingPolicyRule
*rule
) {
36 LIST_REMOVE(rules
, rule
->network
->rules
, rule
);
37 assert(rule
->network
->n_rules
> 0);
38 rule
->network
->n_rules
--;
41 hashmap_remove(rule
->network
->rules_by_section
, rule
->section
);
42 network_config_section_free(rule
->section
);
48 set_remove(rule
->manager
->rules
, rule
);
49 set_remove(rule
->manager
->rules_foreign
, rule
);
57 static void routing_policy_rule_hash_func(const void *b
, struct siphash
*state
) {
58 const RoutingPolicyRule
*rule
= b
;
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
);
79 siphash24_compress(&rule
->iif
, strlen(rule
->iif
), state
);
82 siphash24_compress(&rule
->oif
, strlen(rule
->oif
), state
);
86 /* treat any other address family as AF_UNSPEC */
91 static int routing_policy_rule_compare_func(const void *_a
, const void *_b
) {
92 const RoutingPolicyRule
*a
= _a
, *b
= _b
;
95 r
= CMP(a
->family
, b
->family
);
102 r
= CMP(a
->from_prefixlen
, b
->from_prefixlen
);
106 r
= CMP(a
->to_prefixlen
, b
->to_prefixlen
);
110 r
= CMP(a
->tos
, b
->tos
);
114 r
= CMP(a
->fwmask
, b
->fwmask
);
118 r
= CMP(a
->table
, b
->table
);
122 r
= strcmp_ptr(a
->iif
, b
->iif
);
126 r
= strcmp_ptr(a
->oif
, b
->oif
);
130 r
= memcmp(&a
->from
, &b
->from
, FAMILY_ADDRESS_SIZE(a
->family
));
134 return memcmp(&a
->to
, &b
->to
, FAMILY_ADDRESS_SIZE(a
->family
));
137 /* treat any other address family as AF_UNSPEC */
142 const struct hash_ops routing_policy_rule_hash_ops
= {
143 .hash
= routing_policy_rule_hash_func
,
144 .compare
= routing_policy_rule_compare_func
147 int routing_policy_rule_get(Manager
*m
,
149 const union in_addr_union
*from
,
150 uint8_t from_prefixlen
,
151 const union in_addr_union
*to
,
152 uint8_t to_prefixlen
,
158 RoutingPolicyRule
**ret
) {
160 RoutingPolicyRule rule
, *existing
;
162 assert_return(m
, -1);
164 rule
= (RoutingPolicyRule
) {
167 .from_prefixlen
= from_prefixlen
,
169 .to_prefixlen
= to_prefixlen
,
178 existing
= set_get(m
->rules
, &rule
);
186 if (m
->rules_foreign
) {
187 existing
= set_get(m
->rules_foreign
, &rule
);
198 int routing_policy_rule_make_local(Manager
*m
, RoutingPolicyRule
*rule
) {
203 if (set_contains(m
->rules_foreign
, rule
)) {
204 set_remove(m
->rules_foreign
, rule
);
206 r
= set_ensure_allocated(&m
->rules
, &routing_policy_rule_hash_ops
);
210 return set_put(m
->rules
, rule
);
216 static int routing_policy_rule_add_internal(Manager
*m
,
219 const union in_addr_union
*from
,
220 uint8_t from_prefixlen
,
221 const union in_addr_union
*to
,
222 uint8_t to_prefixlen
,
228 RoutingPolicyRule
**ret
) {
230 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
233 assert_return(rules
, -EINVAL
);
235 r
= routing_policy_rule_new(&rule
);
240 rule
->family
= family
;
242 rule
->from_prefixlen
= from_prefixlen
;
244 rule
->to_prefixlen
= to_prefixlen
;
246 rule
->fwmark
= fwmark
;
251 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
255 r
= set_put(*rules
, rule
);
267 int routing_policy_rule_add(Manager
*m
,
269 const union in_addr_union
*from
,
270 uint8_t from_prefixlen
,
271 const union in_addr_union
*to
,
272 uint8_t to_prefixlen
,
278 RoutingPolicyRule
**ret
) {
280 return routing_policy_rule_add_internal(m
, &m
->rules
, family
, from
, from_prefixlen
, to
, to_prefixlen
, tos
, fwmark
, table
, iif
, oif
, ret
);
283 int routing_policy_rule_add_foreign(Manager
*m
,
285 const union in_addr_union
*from
,
286 uint8_t from_prefixlen
,
287 const union in_addr_union
*to
,
288 uint8_t to_prefixlen
,
294 RoutingPolicyRule
**ret
) {
295 return routing_policy_rule_add_internal(m
, &m
->rules_foreign
, family
, from
, from_prefixlen
, to
, to_prefixlen
, tos
, fwmark
, table
, iif
, oif
, ret
);
298 static int routing_policy_rule_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
299 _cleanup_(link_unrefp
) Link
*link
= userdata
;
304 assert(link
->ifname
);
306 link
->routing_policy_rule_remove_messages
--;
308 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
311 r
= sd_netlink_message_get_errno(m
);
313 log_link_warning_errno(link
, r
, "Could not drop routing policy rule: %m");
318 int routing_policy_rule_remove(RoutingPolicyRule
*routing_policy_rule
, Link
*link
, sd_netlink_message_handler_t callback
) {
319 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
322 assert(routing_policy_rule
);
324 assert(link
->manager
);
325 assert(link
->manager
->rtnl
);
326 assert(link
->ifindex
> 0);
327 assert(IN_SET(routing_policy_rule
->family
, AF_INET
, AF_INET6
));
329 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_DELRULE
, routing_policy_rule
->family
);
331 return log_error_errno(r
, "Could not allocate RTM_DELRULE message: %m");
333 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->from
)) {
334 if (routing_policy_rule
->family
== AF_INET
)
335 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in
);
337 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in6
);
340 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
342 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, routing_policy_rule
->from_prefixlen
);
344 return log_error_errno(r
, "Could not set source prefix length: %m");
347 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->to
)) {
348 if (routing_policy_rule
->family
== AF_INET
)
349 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in
);
351 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in6
);
354 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
356 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, routing_policy_rule
->to_prefixlen
);
358 return log_error_errno(r
, "Could not set destination prefix length: %m");
361 r
= sd_netlink_call_async(link
->manager
->rtnl
, m
, callback
, link
, 0, NULL
);
363 return log_error_errno(r
, "Could not send rtnetlink message: %m");
370 static int routing_policy_rule_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutingPolicyRule
**ret
) {
371 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
372 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
377 assert(!!filename
== (section_line
> 0));
379 r
= network_config_section_new(filename
, section_line
, &n
);
383 rule
= hashmap_get(network
->rules_by_section
, n
);
385 *ret
= TAKE_PTR(rule
);
390 r
= routing_policy_rule_new(&rule
);
395 rule
->network
= network
;
398 r
= hashmap_put(network
->rules_by_section
, rule
->section
, rule
);
402 LIST_APPEND(rules
, network
->rules
, rule
);
405 *ret
= TAKE_PTR(rule
);
410 int link_routing_policy_rule_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
411 _cleanup_(link_unrefp
) Link
*link
= userdata
;
417 assert(link
->ifname
);
418 assert(link
->routing_policy_rule_messages
> 0);
420 link
->routing_policy_rule_messages
--;
422 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
425 r
= sd_netlink_message_get_errno(m
);
426 if (r
< 0 && r
!= -EEXIST
)
427 log_link_warning_errno(link
, r
, "Could not add routing policy rule: %m");
429 if (link
->routing_policy_rule_messages
== 0) {
430 log_link_debug(link
, "Routing policy rule configured");
431 link
->routing_policy_rules_configured
= true;
432 link_check_ready(link
);
438 int routing_policy_rule_configure(RoutingPolicyRule
*rule
, Link
*link
, sd_netlink_message_handler_t callback
, bool update
) {
439 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
444 assert(link
->ifindex
> 0);
445 assert(link
->manager
);
446 assert(link
->manager
->rtnl
);
448 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
450 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
452 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
453 if (rule
->family
== AF_INET
)
454 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &rule
->from
.in
);
456 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &rule
->from
.in6
);
459 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
461 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
463 return log_error_errno(r
, "Could not set source prefix length: %m");
466 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
467 if (rule
->family
== AF_INET
)
468 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &rule
->to
.in
);
470 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &rule
->to
.in6
);
473 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
475 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
477 return log_error_errno(r
, "Could not set destination prefix length: %m");
480 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
482 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
485 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
487 return log_error_errno(r
, "Could not set ip rule tos: %m");
490 if (rule
->table
< 256) {
491 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
493 return log_error_errno(r
, "Could not set ip rule table: %m");
495 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
497 return log_error_errno(r
, "Could not set ip rule table: %m");
499 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
501 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
504 if (rule
->fwmark
> 0) {
505 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
507 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
510 if (rule
->fwmask
> 0) {
511 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
513 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
517 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
519 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
523 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
525 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
530 r
= sd_netlink_call_async(link
->manager
->rtnl
, m
, callback
, link
, 0, NULL
);
532 return log_error_errno(r
, "Could not send rtnetlink message: %m");
536 r
= routing_policy_rule_add(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
537 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, NULL
);
539 return log_error_errno(r
, "Could not add rule : %m");
544 static int parse_fwmark_fwmask(const char *s
, uint32_t *fwmark
, uint32_t *fwmask
) {
545 _cleanup_free_
char *f
= NULL
;
559 r
= safe_atou32(f
, fwmark
);
561 return log_error_errno(r
, "Failed to parse RPDB rule firewall mark, ignoring: %s", f
);
564 r
= safe_atou32(p
, fwmask
);
566 return log_error_errno(r
, "Failed to parse RPDB rule mask, ignoring: %s", f
);
572 int config_parse_routing_policy_rule_tos(
574 const char *filename
,
577 unsigned section_line
,
584 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
585 Network
*network
= userdata
;
594 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
598 r
= safe_atou8(rvalue
, &n
->tos
);
600 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule tos, ignoring: %s", rvalue
);
609 int config_parse_routing_policy_rule_priority(
611 const char *filename
,
614 unsigned section_line
,
621 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
622 Network
*network
= userdata
;
631 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
635 r
= safe_atou32(rvalue
, &n
->priority
);
637 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule priority, ignoring: %s", rvalue
);
646 int config_parse_routing_policy_rule_table(
648 const char *filename
,
651 unsigned section_line
,
658 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
659 Network
*network
= userdata
;
668 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
672 r
= safe_atou32(rvalue
, &n
->table
);
674 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule table, ignoring: %s", rvalue
);
683 int config_parse_routing_policy_rule_fwmark_mask(
685 const char *filename
,
688 unsigned section_line
,
695 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
696 Network
*network
= userdata
;
705 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
709 r
= parse_fwmark_fwmask(rvalue
, &n
->fwmark
, &n
->fwmask
);
711 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue
);
720 int config_parse_routing_policy_rule_prefix(
722 const char *filename
,
725 unsigned section_line
,
732 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
733 Network
*network
= userdata
;
734 union in_addr_union buffer
;
744 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
748 r
= in_addr_prefix_from_string(rvalue
, AF_INET
, &buffer
, &prefixlen
);
750 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &buffer
, &prefixlen
);
752 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
756 n
->family
= AF_INET6
;
760 if (streq(lvalue
, "To")) {
762 n
->to_prefixlen
= prefixlen
;
765 n
->from_prefixlen
= prefixlen
;
773 int config_parse_routing_policy_rule_device(
775 const char *filename
,
778 unsigned section_line
,
785 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
786 Network
*network
= userdata
;
795 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
799 if (!ifname_valid(rvalue
)) {
800 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse '%s' interface name, ignoring: %s", lvalue
, rvalue
);
804 if (streq(lvalue
, "IncomingInterface")) {
805 r
= free_and_strdup(&n
->iif
, rvalue
);
809 r
= free_and_strdup(&n
->oif
, rvalue
);
819 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
820 _cleanup_free_
char *s
= NULL
;
826 r
= read_full_file(state_file
, &s
, &size
);
839 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
840 RoutingPolicyRule
*rule
= NULL
;
846 SET_FOREACH(rule
, rules
, i
) {
847 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
852 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
853 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
857 fprintf(f
, "from=%s/%hhu",
858 from_str
, rule
->from_prefixlen
);
862 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
863 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
867 fprintf(f
, "%sto=%s/%hhu",
869 to_str
, rule
->to_prefixlen
);
873 if (rule
->tos
!= 0) {
874 fprintf(f
, "%stos=%hhu",
880 if (rule
->fwmark
!= 0) {
881 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
883 rule
->fwmark
, rule
->fwmask
);
888 fprintf(f
, "%siif=%s",
895 fprintf(f
, "%soif=%s",
901 fprintf(f
, "%stable=%"PRIu32
"\n",
909 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
910 _cleanup_strv_free_
char **l
= NULL
;
911 _cleanup_free_
char *data
= NULL
;
919 r
= routing_policy_rule_read_full_file(state_file
, &data
);
923 l
= strv_split_newlines(data
);
927 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
932 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
934 p
= startswith(*i
, "RULE=");
938 r
= routing_policy_rule_new(&rule
);
943 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
944 union in_addr_union buffer
;
947 r
= extract_first_word(&p
, &word
, NULL
, 0);
953 r
= split_pair(word
, "=", &a
, &b
);
957 if (STR_IN_SET(a
, "from", "to")) {
959 r
= in_addr_prefix_from_string(b
, AF_INET
, &buffer
, &prefixlen
);
961 r
= in_addr_prefix_from_string(b
, AF_INET6
, &buffer
, &prefixlen
);
963 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
967 rule
->family
= AF_INET6
;
969 rule
->family
= AF_INET
;
971 if (streq(a
, "to")) {
973 rule
->to_prefixlen
= prefixlen
;
976 rule
->from_prefixlen
= prefixlen
;
978 } else if (streq(a
, "tos")) {
979 r
= safe_atou8(b
, &rule
->tos
);
981 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
984 } else if (streq(a
, "table")) {
985 r
= safe_atou32(b
, &rule
->table
);
987 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
990 } else if (streq(a
, "fwmark")) {
992 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
994 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
997 } else if (streq(a
, "iif")) {
999 if (free_and_strdup(&rule
->iif
, b
) < 0)
1002 } else if (streq(a
, "oif")) {
1004 if (free_and_strdup(&rule
->oif
, b
) < 0)
1009 r
= set_put(*rules
, rule
);
1011 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1021 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1022 RoutingPolicyRule
*rule
, *existing
;
1029 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1030 existing
= set_get(m
->rules_foreign
, rule
);
1033 r
= routing_policy_rule_remove(rule
, link
, routing_policy_rule_remove_handler
);
1035 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1039 link
->routing_policy_rule_remove_messages
++;