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_DEFAULT
;
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 */
330 int route_update(Route
*route
,
331 const union in_addr_union
*src
,
332 unsigned char src_prefixlen
,
333 const union in_addr_union
*gw
,
334 const union in_addr_union
*prefsrc
,
336 unsigned char protocol
) {
344 route
->src_prefixlen
= src_prefixlen
;
346 route
->prefsrc
= *prefsrc
;
347 route
->scope
= scope
;
348 route
->protocol
= protocol
;
353 int route_remove(Route
*route
, Link
*link
,
354 sd_netlink_message_handler_t callback
) {
355 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
359 assert(link
->manager
);
360 assert(link
->manager
->rtnl
);
361 assert(link
->ifindex
> 0);
362 assert(route
->family
== AF_INET
|| route
->family
== AF_INET6
);
364 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
365 RTM_DELROUTE
, route
->family
,
368 return log_error_errno(r
, "Could not create RTM_DELROUTE message: %m");
370 if (!in_addr_is_null(route
->family
, &route
->gw
)) {
371 if (route
->family
== AF_INET
)
372 r
= sd_netlink_message_append_in_addr(req
, RTA_GATEWAY
, &route
->gw
.in
);
373 else if (route
->family
== AF_INET6
)
374 r
= sd_netlink_message_append_in6_addr(req
, RTA_GATEWAY
, &route
->gw
.in6
);
376 return log_error_errno(r
, "Could not append RTA_GATEWAY attribute: %m");
379 if (route
->dst_prefixlen
) {
380 if (route
->family
== AF_INET
)
381 r
= sd_netlink_message_append_in_addr(req
, RTA_DST
, &route
->dst
.in
);
382 else if (route
->family
== AF_INET6
)
383 r
= sd_netlink_message_append_in6_addr(req
, RTA_DST
, &route
->dst
.in6
);
385 return log_error_errno(r
, "Could not append RTA_DST attribute: %m");
387 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
389 return log_error_errno(r
, "Could not set destination prefix length: %m");
392 if (route
->src_prefixlen
) {
393 if (route
->family
== AF_INET
)
394 r
= sd_netlink_message_append_in_addr(req
, RTA_SRC
, &route
->src
.in
);
395 else if (route
->family
== AF_INET6
)
396 r
= sd_netlink_message_append_in6_addr(req
, RTA_SRC
, &route
->src
.in6
);
398 return log_error_errno(r
, "Could not append RTA_SRC attribute: %m");
400 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
402 return log_error_errno(r
, "Could not set source prefix length: %m");
405 if (!in_addr_is_null(route
->family
, &route
->prefsrc
)) {
406 if (route
->family
== AF_INET
)
407 r
= sd_netlink_message_append_in_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in
);
408 else if (route
->family
== AF_INET6
)
409 r
= sd_netlink_message_append_in6_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in6
);
411 return log_error_errno(r
, "Could not append RTA_PREFSRC attribute: %m");
414 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
416 return log_error_errno(r
, "Could not set scope: %m");
418 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
420 return log_error_errno(r
, "Could not append RTA_PRIORITY attribute: %m");
422 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
424 return log_error_errno(r
, "Could not append RTA_OIF attribute: %m");
426 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, link
, 0, NULL
);
428 return log_error_errno(r
, "Could not send rtnetlink message: %m");
435 static int route_expire_callback(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
436 Link
*link
= userdata
;
442 assert(link
->ifname
);
443 assert(link
->link_messages
> 0);
445 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
448 link
->link_messages
--;
450 r
= sd_netlink_message_get_errno(m
);
451 if (r
< 0 && r
!= -EEXIST
)
452 log_link_warning_errno(link
, r
, "could not remove route: %m");
454 if (link
->link_messages
== 0)
455 log_link_debug(link
, "route removed");
460 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
461 Route
*route
= userdata
;
466 r
= route_remove(route
, route
->link
, route_expire_callback
);
468 log_warning_errno(r
, "Could not remove route: %m");
470 /* route may not be exist in kernel. If we fail still remove it */
471 route
->link
->link_messages
++;
481 sd_netlink_message_handler_t callback
) {
483 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
484 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
489 assert(link
->manager
);
490 assert(link
->manager
->rtnl
);
491 assert(link
->ifindex
> 0);
492 assert(route
->family
== AF_INET
|| route
->family
== AF_INET6
);
494 if (route_get(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
) <= 0 &&
495 set_size(route
->link
->routes
) >= ROUTES_PER_LINK_MAX
)
498 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
499 RTM_NEWROUTE
, route
->family
,
502 return log_error_errno(r
, "Could not create RTM_NEWROUTE message: %m");
504 if (!in_addr_is_null(route
->family
, &route
->gw
)) {
505 if (route
->family
== AF_INET
)
506 r
= sd_netlink_message_append_in_addr(req
, RTA_GATEWAY
, &route
->gw
.in
);
507 else if (route
->family
== AF_INET6
)
508 r
= sd_netlink_message_append_in6_addr(req
, RTA_GATEWAY
, &route
->gw
.in6
);
510 return log_error_errno(r
, "Could not append RTA_GATEWAY attribute: %m");
512 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
514 return log_error_errno(r
, "Could not set route family: %m");
517 if (route
->dst_prefixlen
) {
518 if (route
->family
== AF_INET
)
519 r
= sd_netlink_message_append_in_addr(req
, RTA_DST
, &route
->dst
.in
);
520 else if (route
->family
== AF_INET6
)
521 r
= sd_netlink_message_append_in6_addr(req
, RTA_DST
, &route
->dst
.in6
);
523 return log_error_errno(r
, "Could not append RTA_DST attribute: %m");
525 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
527 return log_error_errno(r
, "Could not set destination prefix length: %m");
530 if (route
->src_prefixlen
) {
531 if (route
->family
== AF_INET
)
532 r
= sd_netlink_message_append_in_addr(req
, RTA_SRC
, &route
->src
.in
);
533 else if (route
->family
== AF_INET6
)
534 r
= sd_netlink_message_append_in6_addr(req
, RTA_SRC
, &route
->src
.in6
);
536 return log_error_errno(r
, "Could not append RTA_SRC attribute: %m");
538 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
540 return log_error_errno(r
, "Could not set source prefix length: %m");
543 if (!in_addr_is_null(route
->family
, &route
->prefsrc
)) {
544 if (route
->family
== AF_INET
)
545 r
= sd_netlink_message_append_in_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in
);
546 else if (route
->family
== AF_INET6
)
547 r
= sd_netlink_message_append_in6_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in6
);
549 return log_error_errno(r
, "Could not append RTA_PREFSRC attribute: %m");
552 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
554 return log_error_errno(r
, "Could not set scope: %m");
556 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
558 return log_error_errno(r
, "Could not set flags: %m");
560 if (route
->table
!= RT_TABLE_DEFAULT
) {
562 if (route
->table
< 256) {
563 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
565 return log_error_errno(r
, "Could not set route table: %m");
568 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
570 return log_error_errno(r
, "Could not set route table: %m");
572 /* Table attribute to allow more than 256. */
573 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
575 return log_error_errno(r
, "Could not append RTA_TABLE attribute: %m");
579 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
581 return log_error_errno(r
, "Could not append RTA_PRIORITY attribute: %m");
583 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
585 return log_error_errno(r
, "Could not append RTA_PREF attribute: %m");
587 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
589 return log_error_errno(r
, "Could not append RTA_OIF attribute: %m");
591 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, link
, 0, NULL
);
593 return log_error_errno(r
, "Could not send rtnetlink message: %m");
597 lifetime
= route
->lifetime
;
599 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, &route
);
601 return log_error_errno(r
, "Could not add route: %m");
603 /* TODO: drop expiration handling once it can be pushed into the kernel */
604 route
->lifetime
= lifetime
;
606 if (route
->lifetime
!= USEC_INFINITY
) {
607 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
608 route
->lifetime
, 0, route_expire_handler
, route
);
610 return log_error_errno(r
, "Could not arm expiration timer: %m");
613 sd_event_source_unref(route
->expire
);
614 route
->expire
= expire
;
620 int config_parse_gateway(const char *unit
,
621 const char *filename
,
624 unsigned section_line
,
631 Network
*network
= userdata
;
632 _cleanup_route_free_ Route
*n
= NULL
;
633 union in_addr_union buffer
;
642 if (streq(section
, "Network")) {
643 /* we are not in an Route section, so treat
644 * this as the special '0' section */
648 r
= route_new_static(network
, section_line
, &n
);
652 r
= in_addr_from_string_auto(rvalue
, &f
, &buffer
);
654 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Route is invalid, ignoring assignment: %s", rvalue
);
665 int config_parse_preferred_src(const char *unit
,
666 const char *filename
,
669 unsigned section_line
,
676 Network
*network
= userdata
;
677 _cleanup_route_free_ Route
*n
= NULL
;
678 union in_addr_union buffer
;
687 r
= route_new_static(network
, section_line
, &n
);
691 r
= in_addr_from_string_auto(rvalue
, &f
, &buffer
);
693 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
694 "Preferred source is invalid, ignoring assignment: %s", rvalue
);
705 int config_parse_destination(const char *unit
,
706 const char *filename
,
709 unsigned section_line
,
716 Network
*network
= userdata
;
717 _cleanup_route_free_ Route
*n
= NULL
;
718 const char *address
, *e
;
719 union in_addr_union buffer
;
720 unsigned char prefixlen
;
729 r
= route_new_static(network
, section_line
, &n
);
733 /* Destination|Source=address/prefixlen */
736 e
= strchr(rvalue
, '/');
738 address
= strndupa(rvalue
, e
- rvalue
);
742 r
= in_addr_from_string_auto(address
, &f
, &buffer
);
744 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Destination is invalid, ignoring assignment: %s", address
);
748 if (f
!= AF_INET
&& f
!= AF_INET6
) {
749 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown address family, ignoring assignment: %s", address
);
755 r
= safe_atou8(e
+ 1, &prefixlen
);
757 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Route destination prefix length is invalid, ignoring assignment: %s", e
+ 1);
772 if (streq(lvalue
, "Destination")) {
774 n
->dst_prefixlen
= prefixlen
;
775 } else if (streq(lvalue
, "Source")) {
777 n
->src_prefixlen
= prefixlen
;
779 assert_not_reached(lvalue
);
786 int config_parse_route_priority(const char *unit
,
787 const char *filename
,
790 unsigned section_line
,
796 Network
*network
= userdata
;
797 _cleanup_route_free_ Route
*n
= NULL
;
806 r
= route_new_static(network
, section_line
, &n
);
810 r
= config_parse_uint32(unit
, filename
, line
, section
,
811 section_line
, lvalue
, ltype
,
812 rvalue
, &n
->priority
, userdata
);
821 int config_parse_route_scope(const char *unit
,
822 const char *filename
,
825 unsigned section_line
,
831 Network
*network
= userdata
;
832 _cleanup_route_free_ Route
*n
= NULL
;
841 r
= route_new_static(network
, section_line
, &n
);
845 if (streq(rvalue
, "host"))
846 n
->scope
= RT_SCOPE_HOST
;
847 else if (streq(rvalue
, "link"))
848 n
->scope
= RT_SCOPE_LINK
;
849 else if (streq(rvalue
, "global"))
850 n
->scope
= RT_SCOPE_UNIVERSE
;
852 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
861 int config_parse_route_table(const char *unit
,
862 const char *filename
,
865 unsigned section_line
,
871 _cleanup_route_free_ Route
*n
= NULL
;
872 Network
*network
= userdata
;
882 r
= route_new_static(network
, section_line
, &n
);
886 r
= safe_atou32(rvalue
, &k
);
888 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
889 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);