1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2017 Susant Sahani
7 #include <linux/fib_rules.h>
9 #include "alloc-util.h"
10 #include "conf-parser.h"
12 #include "networkd-routing-policy-rule.h"
13 #include "netlink-util.h"
14 #include "networkd-manager.h"
15 #include "parse-util.h"
16 #include "socket-util.h"
17 #include "string-util.h"
19 int routing_policy_rule_new(RoutingPolicyRule
**ret
) {
20 RoutingPolicyRule
*rule
;
22 rule
= new0(RoutingPolicyRule
, 1);
26 rule
->family
= AF_INET
;
27 rule
->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 if (a
->family
< b
->family
)
100 if (a
->family
> b
->family
)
106 if (a
->from_prefixlen
< b
->from_prefixlen
)
108 if (a
->from_prefixlen
> b
->from_prefixlen
)
111 if (a
->to_prefixlen
< b
->to_prefixlen
)
113 if (a
->to_prefixlen
> b
->to_prefixlen
)
121 if (a
->fwmask
< b
->fwmark
)
123 if (a
->fwmask
> b
->fwmark
)
126 if (a
->table
< b
->table
)
128 if (a
->table
> b
->table
)
131 r
= strcmp_ptr(a
->iif
, b
->iif
);
135 r
= strcmp_ptr(a
->oif
, b
->oif
);
139 r
= memcmp(&a
->from
, &b
->from
, FAMILY_ADDRESS_SIZE(a
->family
));
143 return memcmp(&a
->to
, &b
->to
, FAMILY_ADDRESS_SIZE(a
->family
));
146 /* treat any other address family as AF_UNSPEC */
151 const struct hash_ops routing_policy_rule_hash_ops
= {
152 .hash
= routing_policy_rule_hash_func
,
153 .compare
= routing_policy_rule_compare_func
156 int routing_policy_rule_get(Manager
*m
,
158 const union in_addr_union
*from
,
159 uint8_t from_prefixlen
,
160 const union in_addr_union
*to
,
161 uint8_t to_prefixlen
,
167 RoutingPolicyRule
**ret
) {
169 RoutingPolicyRule rule
, *existing
;
171 assert_return(m
, -1);
173 rule
= (RoutingPolicyRule
) {
176 .from_prefixlen
= from_prefixlen
,
178 .to_prefixlen
= to_prefixlen
,
187 existing
= set_get(m
->rules
, &rule
);
195 if (m
->rules_foreign
) {
196 existing
= set_get(m
->rules_foreign
, &rule
);
207 int routing_policy_rule_make_local(Manager
*m
, RoutingPolicyRule
*rule
) {
212 if (set_contains(m
->rules_foreign
, rule
)) {
213 set_remove(m
->rules_foreign
, rule
);
215 r
= set_ensure_allocated(&m
->rules
, &routing_policy_rule_hash_ops
);
219 return set_put(m
->rules
, rule
);
225 static int routing_policy_rule_add_internal(Manager
*m
,
228 const union in_addr_union
*from
,
229 uint8_t from_prefixlen
,
230 const union in_addr_union
*to
,
231 uint8_t to_prefixlen
,
237 RoutingPolicyRule
**ret
) {
239 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
242 assert_return(rules
, -EINVAL
);
244 r
= routing_policy_rule_new(&rule
);
249 rule
->family
= family
;
251 rule
->from_prefixlen
= from_prefixlen
;
253 rule
->to_prefixlen
= to_prefixlen
;
255 rule
->fwmark
= fwmark
;
260 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
264 r
= set_put(*rules
, rule
);
276 int routing_policy_rule_add(Manager
*m
,
278 const union in_addr_union
*from
,
279 uint8_t from_prefixlen
,
280 const union in_addr_union
*to
,
281 uint8_t to_prefixlen
,
287 RoutingPolicyRule
**ret
) {
289 return routing_policy_rule_add_internal(m
, &m
->rules
, family
, from
, from_prefixlen
, to
, to_prefixlen
, tos
, fwmark
, table
, iif
, oif
, ret
);
292 int routing_policy_rule_add_foreign(Manager
*m
,
294 const union in_addr_union
*from
,
295 uint8_t from_prefixlen
,
296 const union in_addr_union
*to
,
297 uint8_t to_prefixlen
,
303 RoutingPolicyRule
**ret
) {
304 return routing_policy_rule_add_internal(m
, &m
->rules_foreign
, family
, from
, from_prefixlen
, to
, to_prefixlen
, tos
, fwmark
, table
, iif
, oif
, ret
);
307 static int routing_policy_rule_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
308 _cleanup_(link_unrefp
) Link
*link
= userdata
;
313 assert(link
->ifname
);
315 link
->routing_policy_rule_remove_messages
--;
317 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
320 r
= sd_netlink_message_get_errno(m
);
322 log_link_warning_errno(link
, r
, "Could not drop routing policy rule: %m");
327 int routing_policy_rule_remove(RoutingPolicyRule
*routing_policy_rule
, Link
*link
, sd_netlink_message_handler_t callback
) {
328 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
331 assert(routing_policy_rule
);
333 assert(link
->manager
);
334 assert(link
->manager
->rtnl
);
335 assert(link
->ifindex
> 0);
336 assert(IN_SET(routing_policy_rule
->family
, AF_INET
, AF_INET6
));
338 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_DELRULE
, routing_policy_rule
->family
);
340 return log_error_errno(r
, "Could not allocate RTM_DELRULE message: %m");
342 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->from
)) {
343 if (routing_policy_rule
->family
== AF_INET
)
344 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in
);
346 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &routing_policy_rule
->from
.in6
);
349 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
351 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, routing_policy_rule
->from_prefixlen
);
353 return log_error_errno(r
, "Could not set source prefix length: %m");
356 if (!in_addr_is_null(routing_policy_rule
->family
, &routing_policy_rule
->to
)) {
357 if (routing_policy_rule
->family
== AF_INET
)
358 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in
);
360 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &routing_policy_rule
->to
.in6
);
363 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
365 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, routing_policy_rule
->to_prefixlen
);
367 return log_error_errno(r
, "Could not set destination prefix length: %m");
370 r
= sd_netlink_call_async(link
->manager
->rtnl
, m
, callback
, link
, 0, NULL
);
372 return log_error_errno(r
, "Could not send rtnetlink message: %m");
379 static int routing_policy_rule_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutingPolicyRule
**ret
) {
380 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
381 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
386 assert(!!filename
== (section_line
> 0));
388 r
= network_config_section_new(filename
, section_line
, &n
);
392 rule
= hashmap_get(network
->rules_by_section
, n
);
394 *ret
= TAKE_PTR(rule
);
399 r
= routing_policy_rule_new(&rule
);
404 rule
->network
= network
;
407 r
= hashmap_put(network
->rules_by_section
, rule
->section
, rule
);
411 LIST_APPEND(rules
, network
->rules
, rule
);
414 *ret
= TAKE_PTR(rule
);
419 int link_routing_policy_rule_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
420 _cleanup_(link_unrefp
) Link
*link
= userdata
;
426 assert(link
->ifname
);
427 assert(link
->routing_policy_rule_messages
> 0);
429 link
->routing_policy_rule_messages
--;
431 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
434 r
= sd_netlink_message_get_errno(m
);
435 if (r
< 0 && r
!= -EEXIST
)
436 log_link_warning_errno(link
, r
, "Could not add routing policy rule: %m");
438 if (link
->routing_policy_rule_messages
== 0) {
439 log_link_debug(link
, "Routing policy rule configured");
440 link
->routing_policy_rules_configured
= true;
441 link_check_ready(link
);
447 int routing_policy_rule_configure(RoutingPolicyRule
*rule
, Link
*link
, sd_netlink_message_handler_t callback
, bool update
) {
448 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
453 assert(link
->ifindex
> 0);
454 assert(link
->manager
);
455 assert(link
->manager
->rtnl
);
457 r
= sd_rtnl_message_new_routing_policy_rule(link
->manager
->rtnl
, &m
, RTM_NEWRULE
, rule
->family
);
459 return log_error_errno(r
, "Could not allocate RTM_NEWRULE message: %m");
461 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
462 if (rule
->family
== AF_INET
)
463 r
= sd_netlink_message_append_in_addr(m
, FRA_SRC
, &rule
->from
.in
);
465 r
= sd_netlink_message_append_in6_addr(m
, FRA_SRC
, &rule
->from
.in6
);
468 return log_error_errno(r
, "Could not append FRA_SRC attribute: %m");
470 r
= sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m
, rule
->from_prefixlen
);
472 return log_error_errno(r
, "Could not set source prefix length: %m");
475 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
476 if (rule
->family
== AF_INET
)
477 r
= sd_netlink_message_append_in_addr(m
, FRA_DST
, &rule
->to
.in
);
479 r
= sd_netlink_message_append_in6_addr(m
, FRA_DST
, &rule
->to
.in6
);
482 return log_error_errno(r
, "Could not append FRA_DST attribute: %m");
484 r
= sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m
, rule
->to_prefixlen
);
486 return log_error_errno(r
, "Could not set destination prefix length: %m");
489 r
= sd_netlink_message_append_u32(m
, FRA_PRIORITY
, rule
->priority
);
491 return log_error_errno(r
, "Could not append FRA_PRIORITY attribute: %m");
494 r
= sd_rtnl_message_routing_policy_rule_set_tos(m
, rule
->tos
);
496 return log_error_errno(r
, "Could not set ip rule tos: %m");
499 if (rule
->table
< 256) {
500 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, rule
->table
);
502 return log_error_errno(r
, "Could not set ip rule table: %m");
504 r
= sd_rtnl_message_routing_policy_rule_set_table(m
, RT_TABLE_UNSPEC
);
506 return log_error_errno(r
, "Could not set ip rule table: %m");
508 r
= sd_netlink_message_append_u32(m
, FRA_TABLE
, rule
->table
);
510 return log_error_errno(r
, "Could not append FRA_TABLE attribute: %m");
513 if (rule
->fwmark
> 0) {
514 r
= sd_netlink_message_append_u32(m
, FRA_FWMARK
, rule
->fwmark
);
516 return log_error_errno(r
, "Could not append FRA_FWMARK attribute: %m");
519 if (rule
->fwmask
> 0) {
520 r
= sd_netlink_message_append_u32(m
, FRA_FWMASK
, rule
->fwmask
);
522 return log_error_errno(r
, "Could not append FRA_FWMASK attribute: %m");
526 r
= sd_netlink_message_append_string(m
, FRA_IFNAME
, rule
->iif
);
528 return log_error_errno(r
, "Could not append FRA_IFNAME attribute: %m");
532 r
= sd_netlink_message_append_string(m
, FRA_OIFNAME
, rule
->oif
);
534 return log_error_errno(r
, "Could not append FRA_OIFNAME attribute: %m");
539 r
= sd_netlink_call_async(link
->manager
->rtnl
, m
, callback
, link
, 0, NULL
);
541 return log_error_errno(r
, "Could not send rtnetlink message: %m");
545 r
= routing_policy_rule_add(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
546 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, NULL
);
548 return log_error_errno(r
, "Could not add rule : %m");
553 static int parse_fwmark_fwmask(const char *s
, uint32_t *fwmark
, uint32_t *fwmask
) {
554 _cleanup_free_
char *f
= NULL
;
568 r
= safe_atou32(f
, fwmark
);
570 return log_error_errno(r
, "Failed to parse RPDB rule firewall mark, ignoring: %s", f
);
573 r
= safe_atou32(p
, fwmask
);
575 return log_error_errno(r
, "Failed to parse RPDB rule mask, ignoring: %s", f
);
581 int config_parse_routing_policy_rule_tos(
583 const char *filename
,
586 unsigned section_line
,
593 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
594 Network
*network
= userdata
;
603 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
607 r
= safe_atou8(rvalue
, &n
->tos
);
609 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule tos, ignoring: %s", rvalue
);
618 int config_parse_routing_policy_rule_priority(
620 const char *filename
,
623 unsigned section_line
,
630 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
631 Network
*network
= userdata
;
640 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
644 r
= safe_atou32(rvalue
, &n
->priority
);
646 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule priority, ignoring: %s", rvalue
);
655 int config_parse_routing_policy_rule_table(
657 const char *filename
,
660 unsigned section_line
,
667 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
668 Network
*network
= userdata
;
677 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
681 r
= safe_atou32(rvalue
, &n
->table
);
683 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule table, ignoring: %s", rvalue
);
692 int config_parse_routing_policy_rule_fwmark_mask(
694 const char *filename
,
697 unsigned section_line
,
704 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
705 Network
*network
= userdata
;
714 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
718 r
= parse_fwmark_fwmask(rvalue
, &n
->fwmark
, &n
->fwmask
);
720 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue
);
729 int config_parse_routing_policy_rule_prefix(
731 const char *filename
,
734 unsigned section_line
,
741 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
742 Network
*network
= userdata
;
743 union in_addr_union buffer
;
753 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
757 r
= in_addr_prefix_from_string(rvalue
, AF_INET
, &buffer
, &prefixlen
);
759 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &buffer
, &prefixlen
);
761 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue
);
765 n
->family
= AF_INET6
;
769 if (streq(lvalue
, "To")) {
771 n
->to_prefixlen
= prefixlen
;
774 n
->from_prefixlen
= prefixlen
;
782 int config_parse_routing_policy_rule_device(
784 const char *filename
,
787 unsigned section_line
,
794 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*n
= NULL
;
795 Network
*network
= userdata
;
804 r
= routing_policy_rule_new_static(network
, filename
, section_line
, &n
);
808 if (!ifname_valid(rvalue
)) {
809 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse '%s' interface name, ignoring: %s", lvalue
, rvalue
);
813 if (streq(lvalue
, "IncomingInterface")) {
814 r
= free_and_strdup(&n
->iif
, rvalue
);
818 r
= free_and_strdup(&n
->oif
, rvalue
);
828 static int routing_policy_rule_read_full_file(const char *state_file
, char **ret
) {
829 _cleanup_free_
char *s
= NULL
;
835 r
= read_full_file(state_file
, &s
, &size
);
848 int routing_policy_serialize_rules(Set
*rules
, FILE *f
) {
849 RoutingPolicyRule
*rule
= NULL
;
855 SET_FOREACH(rule
, rules
, i
) {
856 _cleanup_free_
char *from_str
= NULL
, *to_str
= NULL
;
861 if (!in_addr_is_null(rule
->family
, &rule
->from
)) {
862 r
= in_addr_to_string(rule
->family
, &rule
->from
, &from_str
);
866 fprintf(f
, "from=%s/%hhu",
867 from_str
, rule
->from_prefixlen
);
871 if (!in_addr_is_null(rule
->family
, &rule
->to
)) {
872 r
= in_addr_to_string(rule
->family
, &rule
->to
, &to_str
);
876 fprintf(f
, "%sto=%s/%hhu",
878 to_str
, rule
->to_prefixlen
);
882 if (rule
->tos
!= 0) {
883 fprintf(f
, "%stos=%hhu",
889 if (rule
->fwmark
!= 0) {
890 fprintf(f
, "%sfwmark=%"PRIu32
"/%"PRIu32
,
892 rule
->fwmark
, rule
->fwmask
);
897 fprintf(f
, "%siif=%s",
904 fprintf(f
, "%soif=%s",
910 fprintf(f
, "%stable=%"PRIu32
"\n",
918 int routing_policy_load_rules(const char *state_file
, Set
**rules
) {
919 _cleanup_strv_free_
char **l
= NULL
;
920 _cleanup_free_
char *data
= NULL
;
928 r
= routing_policy_rule_read_full_file(state_file
, &data
);
932 l
= strv_split_newlines(data
);
936 r
= set_ensure_allocated(rules
, &routing_policy_rule_hash_ops
);
941 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*rule
= NULL
;
943 p
= startswith(*i
, "RULE=");
947 r
= routing_policy_rule_new(&rule
);
952 _cleanup_free_
char *word
= NULL
, *a
= NULL
, *b
= NULL
;
953 union in_addr_union buffer
;
956 r
= extract_first_word(&p
, &word
, NULL
, 0);
962 r
= split_pair(word
, "=", &a
, &b
);
966 if (STR_IN_SET(a
, "from", "to")) {
968 r
= in_addr_prefix_from_string(b
, AF_INET
, &buffer
, &prefixlen
);
970 r
= in_addr_prefix_from_string(b
, AF_INET6
, &buffer
, &prefixlen
);
972 log_error_errno(r
, "RPDB rule prefix is invalid, ignoring assignment: %s", b
);
976 rule
->family
= AF_INET6
;
978 rule
->family
= AF_INET
;
980 if (streq(a
, "to")) {
982 rule
->to_prefixlen
= prefixlen
;
985 rule
->from_prefixlen
= prefixlen
;
987 } else if (streq(a
, "tos")) {
988 r
= safe_atou8(b
, &rule
->tos
);
990 log_error_errno(r
, "Failed to parse RPDB rule tos, ignoring: %s", b
);
993 } else if (streq(a
, "table")) {
994 r
= safe_atou32(b
, &rule
->table
);
996 log_error_errno(r
, "Failed to parse RPDB rule table, ignoring: %s", b
);
999 } else if (streq(a
, "fwmark")) {
1001 r
= parse_fwmark_fwmask(b
, &rule
->fwmark
, &rule
->fwmask
);
1003 log_error_errno(r
, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a
);
1006 } else if (streq(a
, "iif")) {
1008 if (free_and_strdup(&rule
->iif
, b
) < 0)
1011 } else if (streq(a
, "oif")) {
1013 if (free_and_strdup(&rule
->oif
, b
) < 0)
1018 r
= set_put(*rules
, rule
);
1020 log_warning_errno(r
, "Failed to add RPDB rule to saved DB, ignoring: %s", p
);
1030 void routing_policy_rule_purge(Manager
*m
, Link
*link
) {
1031 RoutingPolicyRule
*rule
, *existing
;
1038 SET_FOREACH(rule
, m
->rules_saved
, i
) {
1039 existing
= set_get(m
->rules_foreign
, rule
);
1042 r
= routing_policy_rule_remove(rule
, link
, routing_policy_rule_remove_handler
);
1044 log_warning_errno(r
, "Could not remove routing policy rules: %m");
1048 link
->routing_policy_rule_remove_messages
++;