2 This file is part of systemd.
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
21 #include "conf-parser.h"
22 #include "in-addr-util.h"
23 #include "netlink-util.h"
24 #include "networkd-route.h"
26 #include "parse-util.h"
28 #include "string-util.h"
31 #define ROUTES_PER_LINK_MAX 2048U
32 #define STATIC_ROUTES_PER_NETWORK_MAX 1024U
34 int route_new(Route
**ret
) {
35 _cleanup_route_free_ Route
*route
= NULL
;
37 route
= new0(Route
, 1);
41 route
->family
= AF_UNSPEC
;
42 route
->scope
= RT_SCOPE_UNIVERSE
;
43 route
->protocol
= RTPROT_UNSPEC
;
44 route
->table
= RT_TABLE_MAIN
;
45 route
->lifetime
= USEC_INFINITY
;
53 int route_new_static(Network
*network
, unsigned section
, Route
**ret
) {
54 _cleanup_route_free_ Route
*route
= NULL
;
61 route
= hashmap_get(network
->routes_by_section
, UINT_TO_PTR(section
));
70 if (network
->n_static_routes
>= STATIC_ROUTES_PER_NETWORK_MAX
)
73 r
= route_new(&route
);
77 route
->protocol
= RTPROT_STATIC
;
80 route
->section
= section
;
82 r
= hashmap_put(network
->routes_by_section
, UINT_TO_PTR(route
->section
), route
);
87 route
->network
= network
;
88 LIST_PREPEND(routes
, network
->static_routes
, route
);
89 network
->n_static_routes
++;
97 void route_free(Route
*route
) {
101 if (route
->network
) {
102 LIST_REMOVE(routes
, route
->network
->static_routes
, route
);
104 assert(route
->network
->n_static_routes
> 0);
105 route
->network
->n_static_routes
--;
108 hashmap_remove(route
->network
->routes_by_section
, UINT_TO_PTR(route
->section
));
112 set_remove(route
->link
->routes
, route
);
113 set_remove(route
->link
->routes_foreign
, route
);
116 sd_event_source_unref(route
->expire
);
121 static void route_hash_func(const void *b
, struct siphash
*state
) {
122 const Route
*route
= b
;
126 siphash24_compress(&route
->family
, sizeof(route
->family
), state
);
128 switch (route
->family
) {
131 /* Equality of routes are given by the 4-touple
132 (dst_prefix,dst_prefixlen,tos,priority,table) */
133 siphash24_compress(&route
->dst
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
134 siphash24_compress(&route
->dst_prefixlen
, sizeof(route
->dst_prefixlen
), state
);
135 siphash24_compress(&route
->tos
, sizeof(route
->tos
), state
);
136 siphash24_compress(&route
->priority
, sizeof(route
->priority
), state
);
137 siphash24_compress(&route
->table
, sizeof(route
->table
), state
);
141 /* treat any other address family as AF_UNSPEC */
146 static int route_compare_func(const void *_a
, const void *_b
) {
147 const Route
*a
= _a
, *b
= _b
;
149 if (a
->family
< b
->family
)
151 if (a
->family
> b
->family
)
157 if (a
->dst_prefixlen
< b
->dst_prefixlen
)
159 if (a
->dst_prefixlen
> b
->dst_prefixlen
)
167 if (a
->priority
< b
->priority
)
169 if (a
->priority
> b
->priority
)
172 if (a
->table
< b
->table
)
174 if (a
->table
> b
->table
)
177 return memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
179 /* treat any other address family as AF_UNSPEC */
184 static const struct hash_ops route_hash_ops
= {
185 .hash
= route_hash_func
,
186 .compare
= route_compare_func
189 int route_get(Link
*link
,
191 const union in_addr_union
*dst
,
192 unsigned char dst_prefixlen
,
198 Route route
, *existing
;
206 .dst_prefixlen
= dst_prefixlen
,
208 .priority
= priority
,
212 existing
= set_get(link
->routes
, &route
);
219 existing
= set_get(link
->routes_foreign
, &route
);
229 static int route_add_internal(
233 const union in_addr_union
*dst
,
234 unsigned char dst_prefixlen
,
240 _cleanup_route_free_ Route
*route
= NULL
;
247 r
= route_new(&route
);
251 route
->family
= family
;
253 route
->dst_prefixlen
= dst_prefixlen
;
255 route
->priority
= priority
;
256 route
->table
= table
;
258 r
= set_ensure_allocated(routes
, &route_hash_ops
);
262 r
= set_put(*routes
, route
);
276 int route_add_foreign(
279 const union in_addr_union
*dst
,
280 unsigned char dst_prefixlen
,
286 return route_add_internal(link
, &link
->routes_foreign
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, ret
);
292 const union in_addr_union
*dst
,
293 unsigned char dst_prefixlen
,
302 r
= route_get(link
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
304 /* Route does not exist, create a new one */
305 r
= route_add_internal(link
, &link
->routes
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
309 /* Take over a foreign route */
310 r
= set_ensure_allocated(&link
->routes
, &route_hash_ops
);
314 r
= set_put(link
->routes
, route
);
318 set_remove(link
->routes_foreign
, route
);
320 /* Route exists, do nothing */
331 int route_update(Route
*route
,
332 const union in_addr_union
*src
,
333 unsigned char src_prefixlen
,
334 const union in_addr_union
*gw
,
335 const union in_addr_union
*prefsrc
,
337 unsigned char protocol
) {
345 route
->src_prefixlen
= src_prefixlen
;
347 route
->prefsrc
= *prefsrc
;
348 route
->scope
= scope
;
349 route
->protocol
= protocol
;
354 int route_remove(Route
*route
, Link
*link
,
355 sd_netlink_message_handler_t callback
) {
356 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
360 assert(link
->manager
);
361 assert(link
->manager
->rtnl
);
362 assert(link
->ifindex
> 0);
363 assert(route
->family
== AF_INET
|| route
->family
== AF_INET6
);
365 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
366 RTM_DELROUTE
, route
->family
,
369 return log_error_errno(r
, "Could not create RTM_DELROUTE message: %m");
371 if (!in_addr_is_null(route
->family
, &route
->gw
)) {
372 if (route
->family
== AF_INET
)
373 r
= sd_netlink_message_append_in_addr(req
, RTA_GATEWAY
, &route
->gw
.in
);
374 else if (route
->family
== AF_INET6
)
375 r
= sd_netlink_message_append_in6_addr(req
, RTA_GATEWAY
, &route
->gw
.in6
);
377 return log_error_errno(r
, "Could not append RTA_GATEWAY attribute: %m");
380 if (route
->dst_prefixlen
) {
381 if (route
->family
== AF_INET
)
382 r
= sd_netlink_message_append_in_addr(req
, RTA_DST
, &route
->dst
.in
);
383 else if (route
->family
== AF_INET6
)
384 r
= sd_netlink_message_append_in6_addr(req
, RTA_DST
, &route
->dst
.in6
);
386 return log_error_errno(r
, "Could not append RTA_DST attribute: %m");
388 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
390 return log_error_errno(r
, "Could not set destination prefix length: %m");
393 if (route
->src_prefixlen
) {
394 if (route
->family
== AF_INET
)
395 r
= sd_netlink_message_append_in_addr(req
, RTA_SRC
, &route
->src
.in
);
396 else if (route
->family
== AF_INET6
)
397 r
= sd_netlink_message_append_in6_addr(req
, RTA_SRC
, &route
->src
.in6
);
399 return log_error_errno(r
, "Could not append RTA_SRC attribute: %m");
401 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
403 return log_error_errno(r
, "Could not set source prefix length: %m");
406 if (!in_addr_is_null(route
->family
, &route
->prefsrc
)) {
407 if (route
->family
== AF_INET
)
408 r
= sd_netlink_message_append_in_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in
);
409 else if (route
->family
== AF_INET6
)
410 r
= sd_netlink_message_append_in6_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in6
);
412 return log_error_errno(r
, "Could not append RTA_PREFSRC attribute: %m");
415 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
417 return log_error_errno(r
, "Could not set scope: %m");
419 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
421 return log_error_errno(r
, "Could not append RTA_PRIORITY attribute: %m");
423 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
425 return log_error_errno(r
, "Could not append RTA_OIF attribute: %m");
427 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, link
, 0, NULL
);
429 return log_error_errno(r
, "Could not send rtnetlink message: %m");
436 static int route_expire_callback(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
437 Link
*link
= userdata
;
443 assert(link
->ifname
);
445 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
448 r
= sd_netlink_message_get_errno(m
);
449 if (r
< 0 && r
!= -EEXIST
)
450 log_link_warning_errno(link
, r
, "could not remove route: %m");
455 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
456 Route
*route
= userdata
;
461 r
= route_remove(route
, route
->link
, route_expire_callback
);
463 log_warning_errno(r
, "Could not remove route: %m");
473 sd_netlink_message_handler_t callback
) {
475 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
476 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
481 assert(link
->manager
);
482 assert(link
->manager
->rtnl
);
483 assert(link
->ifindex
> 0);
484 assert(route
->family
== AF_INET
|| route
->family
== AF_INET6
);
486 if (route_get(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
) <= 0 &&
487 set_size(link
->routes
) >= ROUTES_PER_LINK_MAX
)
490 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
491 RTM_NEWROUTE
, route
->family
,
494 return log_error_errno(r
, "Could not create RTM_NEWROUTE message: %m");
496 if (!in_addr_is_null(route
->family
, &route
->gw
)) {
497 if (route
->family
== AF_INET
)
498 r
= sd_netlink_message_append_in_addr(req
, RTA_GATEWAY
, &route
->gw
.in
);
499 else if (route
->family
== AF_INET6
)
500 r
= sd_netlink_message_append_in6_addr(req
, RTA_GATEWAY
, &route
->gw
.in6
);
502 return log_error_errno(r
, "Could not append RTA_GATEWAY attribute: %m");
504 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
506 return log_error_errno(r
, "Could not set route family: %m");
509 if (route
->dst_prefixlen
) {
510 if (route
->family
== AF_INET
)
511 r
= sd_netlink_message_append_in_addr(req
, RTA_DST
, &route
->dst
.in
);
512 else if (route
->family
== AF_INET6
)
513 r
= sd_netlink_message_append_in6_addr(req
, RTA_DST
, &route
->dst
.in6
);
515 return log_error_errno(r
, "Could not append RTA_DST attribute: %m");
517 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
519 return log_error_errno(r
, "Could not set destination prefix length: %m");
522 if (route
->src_prefixlen
) {
523 if (route
->family
== AF_INET
)
524 r
= sd_netlink_message_append_in_addr(req
, RTA_SRC
, &route
->src
.in
);
525 else if (route
->family
== AF_INET6
)
526 r
= sd_netlink_message_append_in6_addr(req
, RTA_SRC
, &route
->src
.in6
);
528 return log_error_errno(r
, "Could not append RTA_SRC attribute: %m");
530 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
532 return log_error_errno(r
, "Could not set source prefix length: %m");
535 if (!in_addr_is_null(route
->family
, &route
->prefsrc
)) {
536 if (route
->family
== AF_INET
)
537 r
= sd_netlink_message_append_in_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in
);
538 else if (route
->family
== AF_INET6
)
539 r
= sd_netlink_message_append_in6_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in6
);
541 return log_error_errno(r
, "Could not append RTA_PREFSRC attribute: %m");
544 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
546 return log_error_errno(r
, "Could not set scope: %m");
548 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
550 return log_error_errno(r
, "Could not set flags: %m");
552 if (route
->table
!= RT_TABLE_MAIN
) {
553 if (route
->table
< 256) {
554 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
556 return log_error_errno(r
, "Could not set route table: %m");
558 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
560 return log_error_errno(r
, "Could not set route table: %m");
562 /* Table attribute to allow more than 256. */
563 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
565 return log_error_errno(r
, "Could not append RTA_TABLE attribute: %m");
569 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
571 return log_error_errno(r
, "Could not append RTA_PRIORITY attribute: %m");
573 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
575 return log_error_errno(r
, "Could not append RTA_PREF attribute: %m");
577 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
579 return log_error_errno(r
, "Could not append RTA_OIF attribute: %m");
581 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, link
, 0, NULL
);
583 return log_error_errno(r
, "Could not send rtnetlink message: %m");
587 lifetime
= route
->lifetime
;
589 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, &route
);
591 return log_error_errno(r
, "Could not add route: %m");
593 /* TODO: drop expiration handling once it can be pushed into the kernel */
594 route
->lifetime
= lifetime
;
596 if (route
->lifetime
!= USEC_INFINITY
) {
597 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
598 route
->lifetime
, 0, route_expire_handler
, route
);
600 return log_error_errno(r
, "Could not arm expiration timer: %m");
603 sd_event_source_unref(route
->expire
);
604 route
->expire
= expire
;
610 int config_parse_gateway(const char *unit
,
611 const char *filename
,
614 unsigned section_line
,
621 Network
*network
= userdata
;
622 _cleanup_route_free_ Route
*n
= NULL
;
623 union in_addr_union buffer
;
632 if (streq(section
, "Network")) {
633 /* we are not in an Route section, so treat
634 * this as the special '0' section */
638 r
= route_new_static(network
, section_line
, &n
);
642 r
= in_addr_from_string_auto(rvalue
, &f
, &buffer
);
644 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Route is invalid, ignoring assignment: %s", rvalue
);
655 int config_parse_preferred_src(const char *unit
,
656 const char *filename
,
659 unsigned section_line
,
666 Network
*network
= userdata
;
667 _cleanup_route_free_ Route
*n
= NULL
;
668 union in_addr_union buffer
;
677 r
= route_new_static(network
, section_line
, &n
);
681 r
= in_addr_from_string_auto(rvalue
, &f
, &buffer
);
683 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
684 "Preferred source is invalid, ignoring assignment: %s", rvalue
);
695 int config_parse_destination(const char *unit
,
696 const char *filename
,
699 unsigned section_line
,
706 Network
*network
= userdata
;
707 _cleanup_route_free_ Route
*n
= NULL
;
708 const char *address
, *e
;
709 union in_addr_union buffer
;
710 unsigned char prefixlen
;
719 r
= route_new_static(network
, section_line
, &n
);
723 /* Destination|Source=address/prefixlen */
726 e
= strchr(rvalue
, '/');
728 address
= strndupa(rvalue
, e
- rvalue
);
732 r
= in_addr_from_string_auto(address
, &f
, &buffer
);
734 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Destination is invalid, ignoring assignment: %s", address
);
738 if (f
!= AF_INET
&& f
!= AF_INET6
) {
739 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown address family, ignoring assignment: %s", address
);
745 r
= safe_atou8(e
+ 1, &prefixlen
);
747 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Route destination prefix length is invalid, ignoring assignment: %s", e
+ 1);
762 if (streq(lvalue
, "Destination")) {
764 n
->dst_prefixlen
= prefixlen
;
765 } else if (streq(lvalue
, "Source")) {
767 n
->src_prefixlen
= prefixlen
;
769 assert_not_reached(lvalue
);
776 int config_parse_route_priority(const char *unit
,
777 const char *filename
,
780 unsigned section_line
,
786 Network
*network
= userdata
;
787 _cleanup_route_free_ Route
*n
= NULL
;
797 r
= route_new_static(network
, section_line
, &n
);
801 r
= safe_atou32(rvalue
, &k
);
803 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
804 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue
);
814 int config_parse_route_scope(const char *unit
,
815 const char *filename
,
818 unsigned section_line
,
824 Network
*network
= userdata
;
825 _cleanup_route_free_ Route
*n
= NULL
;
834 r
= route_new_static(network
, section_line
, &n
);
838 if (streq(rvalue
, "host"))
839 n
->scope
= RT_SCOPE_HOST
;
840 else if (streq(rvalue
, "link"))
841 n
->scope
= RT_SCOPE_LINK
;
842 else if (streq(rvalue
, "global"))
843 n
->scope
= RT_SCOPE_UNIVERSE
;
845 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
854 int config_parse_route_table(const char *unit
,
855 const char *filename
,
858 unsigned section_line
,
864 _cleanup_route_free_ Route
*n
= NULL
;
865 Network
*network
= userdata
;
875 r
= route_new_static(network
, section_line
, &n
);
879 r
= safe_atou32(rvalue
, &k
);
881 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
882 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);