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 STATIC_ROUTES_PER_NETWORK_MAX 1024U
33 int route_new(Route
**ret
) {
34 _cleanup_route_free_ Route
*route
= NULL
;
36 route
= new0(Route
, 1);
40 route
->family
= AF_UNSPEC
;
41 route
->scope
= RT_SCOPE_UNIVERSE
;
42 route
->protocol
= RTPROT_UNSPEC
;
43 route
->table
= RT_TABLE_DEFAULT
;
44 route
->lifetime
= USEC_INFINITY
;
52 int route_new_static(Network
*network
, unsigned section
, Route
**ret
) {
53 _cleanup_route_free_ Route
*route
= NULL
;
60 route
= hashmap_get(network
->routes_by_section
, UINT_TO_PTR(section
));
69 if (network
->n_static_routes
>= STATIC_ROUTES_PER_NETWORK_MAX
)
72 r
= route_new(&route
);
76 route
->protocol
= RTPROT_STATIC
;
79 route
->section
= section
;
81 r
= hashmap_put(network
->routes_by_section
, UINT_TO_PTR(route
->section
), route
);
86 route
->network
= network
;
87 LIST_PREPEND(routes
, network
->static_routes
, route
);
88 network
->n_static_routes
++;
96 void route_free(Route
*route
) {
100 if (route
->network
) {
101 LIST_REMOVE(routes
, route
->network
->static_routes
, route
);
103 assert(route
->network
->n_static_routes
> 0);
104 route
->network
->n_static_routes
--;
107 hashmap_remove(route
->network
->routes_by_section
, UINT_TO_PTR(route
->section
));
111 set_remove(route
->link
->routes
, route
);
112 set_remove(route
->link
->routes_foreign
, route
);
115 sd_event_source_unref(route
->expire
);
120 static void route_hash_func(const void *b
, struct siphash
*state
) {
121 const Route
*route
= b
;
125 siphash24_compress(&route
->family
, sizeof(route
->family
), state
);
127 switch (route
->family
) {
130 /* Equality of routes are given by the 4-touple
131 (dst_prefix,dst_prefixlen,tos,priority,table) */
132 siphash24_compress(&route
->dst
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
133 siphash24_compress(&route
->dst_prefixlen
, sizeof(route
->dst_prefixlen
), state
);
134 siphash24_compress(&route
->tos
, sizeof(route
->tos
), state
);
135 siphash24_compress(&route
->priority
, sizeof(route
->priority
), state
);
136 siphash24_compress(&route
->table
, sizeof(route
->table
), state
);
140 /* treat any other address family as AF_UNSPEC */
145 static int route_compare_func(const void *_a
, const void *_b
) {
146 const Route
*a
= _a
, *b
= _b
;
148 if (a
->family
< b
->family
)
150 if (a
->family
> b
->family
)
156 if (a
->dst_prefixlen
< b
->dst_prefixlen
)
158 if (a
->dst_prefixlen
> b
->dst_prefixlen
)
166 if (a
->priority
< b
->priority
)
168 if (a
->priority
> b
->priority
)
171 if (a
->table
< b
->table
)
173 if (a
->table
> b
->table
)
176 return memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
178 /* treat any other address family as AF_UNSPEC */
183 static const struct hash_ops route_hash_ops
= {
184 .hash
= route_hash_func
,
185 .compare
= route_compare_func
188 int route_get(Link
*link
,
190 union in_addr_union
*dst
,
191 unsigned char dst_prefixlen
,
198 .dst_prefixlen
= dst_prefixlen
,
200 .priority
= priority
,
210 existing
= set_get(link
->routes
, &route
);
215 existing
= set_get(link
->routes_foreign
, &route
);
225 static int route_add_internal(Link
*link
, Set
**routes
,
227 union in_addr_union
*dst
,
228 unsigned char dst_prefixlen
,
231 unsigned char table
, Route
**ret
) {
232 _cleanup_route_free_ Route
*route
= NULL
;
239 r
= route_new(&route
);
243 route
->family
= family
;
245 route
->dst_prefixlen
= dst_prefixlen
;
247 route
->priority
= priority
;
248 route
->table
= table
;
250 r
= set_ensure_allocated(routes
, &route_hash_ops
);
254 r
= set_put(*routes
, route
);
268 int route_add_foreign(Link
*link
,
270 union in_addr_union
*dst
,
271 unsigned char dst_prefixlen
,
274 unsigned char table
, Route
**ret
) {
275 return route_add_internal(link
, &link
->routes_foreign
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, ret
);
278 int route_add(Link
*link
,
280 union in_addr_union
*dst
,
281 unsigned char dst_prefixlen
,
284 unsigned char table
, Route
**ret
) {
288 r
= route_get(link
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
290 /* Route does not exist, create a new one */
291 r
= route_add_internal(link
, &link
->routes
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
295 /* Take over a foreign route */
296 r
= set_ensure_allocated(&link
->routes
, &route_hash_ops
);
300 r
= set_put(link
->routes
, route
);
304 set_remove(link
->routes_foreign
, route
);
306 /* Route exists, do nothing */
316 int route_update(Route
*route
,
317 union in_addr_union
*src
,
318 unsigned char src_prefixlen
,
319 union in_addr_union
*gw
,
320 union in_addr_union
*prefsrc
,
322 unsigned char protocol
) {
329 route
->src_prefixlen
= src_prefixlen
;
331 route
->prefsrc
= *prefsrc
;
332 route
->scope
= scope
;
333 route
->protocol
= protocol
;
338 int route_remove(Route
*route
, Link
*link
,
339 sd_netlink_message_handler_t callback
) {
340 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
344 assert(link
->manager
);
345 assert(link
->manager
->rtnl
);
346 assert(link
->ifindex
> 0);
347 assert(route
->family
== AF_INET
|| route
->family
== AF_INET6
);
349 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
350 RTM_DELROUTE
, route
->family
,
353 return log_error_errno(r
, "Could not create RTM_DELROUTE message: %m");
355 if (!in_addr_is_null(route
->family
, &route
->gw
)) {
356 if (route
->family
== AF_INET
)
357 r
= sd_netlink_message_append_in_addr(req
, RTA_GATEWAY
, &route
->gw
.in
);
358 else if (route
->family
== AF_INET6
)
359 r
= sd_netlink_message_append_in6_addr(req
, RTA_GATEWAY
, &route
->gw
.in6
);
361 return log_error_errno(r
, "Could not append RTA_GATEWAY attribute: %m");
364 if (route
->dst_prefixlen
) {
365 if (route
->family
== AF_INET
)
366 r
= sd_netlink_message_append_in_addr(req
, RTA_DST
, &route
->dst
.in
);
367 else if (route
->family
== AF_INET6
)
368 r
= sd_netlink_message_append_in6_addr(req
, RTA_DST
, &route
->dst
.in6
);
370 return log_error_errno(r
, "Could not append RTA_DST attribute: %m");
372 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
374 return log_error_errno(r
, "Could not set destination prefix length: %m");
377 if (route
->src_prefixlen
) {
378 if (route
->family
== AF_INET
)
379 r
= sd_netlink_message_append_in_addr(req
, RTA_SRC
, &route
->src
.in
);
380 else if (route
->family
== AF_INET6
)
381 r
= sd_netlink_message_append_in6_addr(req
, RTA_SRC
, &route
->src
.in6
);
383 return log_error_errno(r
, "Could not append RTA_SRC attribute: %m");
385 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
387 return log_error_errno(r
, "Could not set source prefix length: %m");
390 if (!in_addr_is_null(route
->family
, &route
->prefsrc
)) {
391 if (route
->family
== AF_INET
)
392 r
= sd_netlink_message_append_in_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in
);
393 else if (route
->family
== AF_INET6
)
394 r
= sd_netlink_message_append_in6_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in6
);
396 return log_error_errno(r
, "Could not append RTA_PREFSRC attribute: %m");
399 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
401 return log_error_errno(r
, "Could not set scope: %m");
403 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
405 return log_error_errno(r
, "Could not append RTA_PRIORITY attribute: %m");
407 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
409 return log_error_errno(r
, "Could not append RTA_OIF attribute: %m");
411 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, link
, 0, NULL
);
413 return log_error_errno(r
, "Could not send rtnetlink message: %m");
420 static int route_expire_callback(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
421 Link
*link
= userdata
;
427 assert(link
->ifname
);
428 assert(link
->link_messages
> 0);
430 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
433 link
->link_messages
--;
435 r
= sd_netlink_message_get_errno(m
);
436 if (r
< 0 && r
!= -EEXIST
)
437 log_link_warning_errno(link
, r
, "could not remove route: %m");
439 if (link
->link_messages
== 0)
440 log_link_debug(link
, "route removed");
445 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
446 Route
*route
= userdata
;
451 r
= route_remove(route
, route
->link
, route_expire_callback
);
453 log_warning_errno(r
, "Could not remove route: %m");
455 /* route may not be exist in kernel. If we fail still remove it */
456 route
->link
->link_messages
++;
463 int route_configure(Route
*route
, Link
*link
,
464 sd_netlink_message_handler_t callback
) {
465 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
466 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
471 assert(link
->manager
);
472 assert(link
->manager
->rtnl
);
473 assert(link
->ifindex
> 0);
474 assert(route
->family
== AF_INET
|| route
->family
== AF_INET6
);
476 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
477 RTM_NEWROUTE
, route
->family
,
480 return log_error_errno(r
, "Could not create RTM_NEWROUTE message: %m");
482 if (!in_addr_is_null(route
->family
, &route
->gw
)) {
483 if (route
->family
== AF_INET
)
484 r
= sd_netlink_message_append_in_addr(req
, RTA_GATEWAY
, &route
->gw
.in
);
485 else if (route
->family
== AF_INET6
)
486 r
= sd_netlink_message_append_in6_addr(req
, RTA_GATEWAY
, &route
->gw
.in6
);
488 return log_error_errno(r
, "Could not append RTA_GATEWAY attribute: %m");
490 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
492 return log_error_errno(r
, "Could not set route family: %m");
495 if (route
->dst_prefixlen
) {
496 if (route
->family
== AF_INET
)
497 r
= sd_netlink_message_append_in_addr(req
, RTA_DST
, &route
->dst
.in
);
498 else if (route
->family
== AF_INET6
)
499 r
= sd_netlink_message_append_in6_addr(req
, RTA_DST
, &route
->dst
.in6
);
501 return log_error_errno(r
, "Could not append RTA_DST attribute: %m");
503 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
505 return log_error_errno(r
, "Could not set destination prefix length: %m");
508 if (route
->src_prefixlen
) {
509 if (route
->family
== AF_INET
)
510 r
= sd_netlink_message_append_in_addr(req
, RTA_SRC
, &route
->src
.in
);
511 else if (route
->family
== AF_INET6
)
512 r
= sd_netlink_message_append_in6_addr(req
, RTA_SRC
, &route
->src
.in6
);
514 return log_error_errno(r
, "Could not append RTA_SRC attribute: %m");
516 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
518 return log_error_errno(r
, "Could not set source prefix length: %m");
521 if (!in_addr_is_null(route
->family
, &route
->prefsrc
)) {
522 if (route
->family
== AF_INET
)
523 r
= sd_netlink_message_append_in_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in
);
524 else if (route
->family
== AF_INET6
)
525 r
= sd_netlink_message_append_in6_addr(req
, RTA_PREFSRC
, &route
->prefsrc
.in6
);
527 return log_error_errno(r
, "Could not append RTA_PREFSRC attribute: %m");
530 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
532 return log_error_errno(r
, "Could not set scope: %m");
534 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
536 return log_error_errno(r
, "Could not set flags: %m");
538 if (route
->table
!= RT_TABLE_DEFAULT
) {
540 if (route
->table
< 256) {
541 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
543 return log_error_errno(r
, "Could not set route table: %m");
546 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
548 return log_error_errno(r
, "Could not set route table: %m");
550 /* Table attribute to allow more than 256. */
551 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
553 return log_error_errno(r
, "Could not append RTA_TABLE attribute: %m");
557 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
559 return log_error_errno(r
, "Could not append RTA_PRIORITY attribute: %m");
561 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
563 return log_error_errno(r
, "Could not append RTA_PREF attribute: %m");
565 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
567 return log_error_errno(r
, "Could not append RTA_OIF attribute: %m");
569 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, link
, 0, NULL
);
571 return log_error_errno(r
, "Could not send rtnetlink message: %m");
575 lifetime
= route
->lifetime
;
577 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, &route
);
579 return log_error_errno(r
, "Could not add route: %m");
581 /* TODO: drop expiration handling once it can be pushed into the kernel */
582 route
->lifetime
= lifetime
;
584 if (route
->lifetime
!= USEC_INFINITY
) {
585 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
586 route
->lifetime
, 0, route_expire_handler
, route
);
588 return log_error_errno(r
, "Could not arm expiration timer: %m");
591 sd_event_source_unref(route
->expire
);
592 route
->expire
= expire
;
598 int config_parse_gateway(const char *unit
,
599 const char *filename
,
602 unsigned section_line
,
609 Network
*network
= userdata
;
610 _cleanup_route_free_ Route
*n
= NULL
;
611 union in_addr_union buffer
;
620 if (streq(section
, "Network")) {
621 /* we are not in an Route section, so treat
622 * this as the special '0' section */
626 r
= route_new_static(network
, section_line
, &n
);
630 r
= in_addr_from_string_auto(rvalue
, &f
, &buffer
);
632 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Route is invalid, ignoring assignment: %s", rvalue
);
643 int config_parse_preferred_src(const char *unit
,
644 const char *filename
,
647 unsigned section_line
,
654 Network
*network
= userdata
;
655 _cleanup_route_free_ Route
*n
= NULL
;
656 union in_addr_union buffer
;
665 r
= route_new_static(network
, section_line
, &n
);
669 r
= in_addr_from_string_auto(rvalue
, &f
, &buffer
);
671 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
672 "Preferred source is invalid, ignoring assignment: %s", rvalue
);
683 int config_parse_destination(const char *unit
,
684 const char *filename
,
687 unsigned section_line
,
694 Network
*network
= userdata
;
695 _cleanup_route_free_ Route
*n
= NULL
;
696 const char *address
, *e
;
697 union in_addr_union buffer
;
698 unsigned char prefixlen
;
707 r
= route_new_static(network
, section_line
, &n
);
711 /* Destination|Source=address/prefixlen */
714 e
= strchr(rvalue
, '/');
716 address
= strndupa(rvalue
, e
- rvalue
);
720 r
= in_addr_from_string_auto(address
, &f
, &buffer
);
722 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Destination is invalid, ignoring assignment: %s", address
);
726 if (f
!= AF_INET
&& f
!= AF_INET6
) {
727 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown address family, ignoring assignment: %s", address
);
733 r
= safe_atou8(e
+ 1, &prefixlen
);
735 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Route destination prefix length is invalid, ignoring assignment: %s", e
+ 1);
750 if (streq(lvalue
, "Destination")) {
752 n
->dst_prefixlen
= prefixlen
;
753 } else if (streq(lvalue
, "Source")) {
755 n
->src_prefixlen
= prefixlen
;
757 assert_not_reached(lvalue
);
764 int config_parse_route_priority(const char *unit
,
765 const char *filename
,
768 unsigned section_line
,
774 Network
*network
= userdata
;
775 _cleanup_route_free_ Route
*n
= NULL
;
784 r
= route_new_static(network
, section_line
, &n
);
788 r
= config_parse_uint32(unit
, filename
, line
, section
,
789 section_line
, lvalue
, ltype
,
790 rvalue
, &n
->priority
, userdata
);
799 int config_parse_route_scope(const char *unit
,
800 const char *filename
,
803 unsigned section_line
,
809 Network
*network
= userdata
;
810 _cleanup_route_free_ Route
*n
= NULL
;
819 r
= route_new_static(network
, section_line
, &n
);
823 if (streq(rvalue
, "host"))
824 n
->scope
= RT_SCOPE_HOST
;
825 else if (streq(rvalue
, "link"))
826 n
->scope
= RT_SCOPE_LINK
;
827 else if (streq(rvalue
, "global"))
828 n
->scope
= RT_SCOPE_UNIVERSE
;
830 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
839 int config_parse_route_table(const char *unit
,
840 const char *filename
,
843 unsigned section_line
,
849 _cleanup_route_free_ Route
*n
= NULL
;
850 Network
*network
= userdata
;
860 r
= route_new_static(network
, section_line
, &n
);
864 r
= safe_atou32(rvalue
, &k
);
866 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
867 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);