HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/network/networkd-route.c

76,269 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 81cfe84add80ad69bea35e0c0b1fdcc5fc052928

Lines 1–1000 of 2,102

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <linux/ipv6_route.h>
#include <net/if.h>
#include <stdio.h>
 
#include "sd-ndisc-protocol.h"
#include "sd-netlink.h"
 
#include "alloc-util.h"
#include "conf-parser.h"
#include "errno-util.h"
#include "event-util.h"
#include "netlink-util.h"
#include "networkd-address.h"
#include "networkd-ipv4ll.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-network.h"
#include "networkd-nexthop.h"
#include "networkd-queue.h"
#include "networkd-route.h"
#include "networkd-route-util.h"
#include "ordered-set.h"
#include "parse-util.h"
#include "set.h"
#include "siphash24.h"
#include "string-util.h"
#include "vrf.h"
#include "wireguard.h"
 
static Route* route_detach_impl(Route *route) {
        assert(route);
        assert(!!route->network + !!route->manager + !!route->wireguard <= 1);
 
        if (route->network) {
                assert(route->section);
                hashmap_remove(route->network->routes_by_section, route->section);
                route->network = NULL;
                return route;
        }
 
        if (route->manager) {
                route_detach_from_nexthop(route);
                set_remove(route->manager->routes, route);
                route->manager = NULL;
                return route;
        }
 
        if (route->wireguard) {
                set_remove(route->wireguard->routes, route);
                route->wireguard = NULL;
                return route;
        }
 
        return NULL;
}
 
void route_detach(Route *route) {
        route_unref(route_detach_impl(route));
}
 
static Route* route_free(Route *route) {
        if (!route)
                return NULL;
 
        route_detach_impl(route);
 
        config_section_free(route->section);
        route_nexthops_done(route);
        route_metric_done(&route->metric);
        sd_event_source_disable_unref(route->expire);
 
        return mfree(route);
}
 
DEFINE_TRIVIAL_REF_UNREF_FUNC(Route, route, route_free);
 
static void route_hash_func(const Route *route, struct siphash *state) {
        assert(route);
 
        siphash24_compress_typesafe(route->family, state);
 
        switch (route->family) {
        case AF_INET:
                /* First, the table, destination prefix, priority, and tos (dscp), are used to find routes.
                 * See fib_table_insert(), fib_find_node(), and fib_find_alias() in net/ipv4/fib_trie.c of the kernel. */
                siphash24_compress_typesafe(route->table, state);
                in_addr_hash_func(&route->dst, route->family, state);
                siphash24_compress_typesafe(route->dst_prefixlen, state);
                siphash24_compress_typesafe(route->priority, state);
                siphash24_compress_typesafe(route->tos, state);
 
                /* Then, protocol, scope, type, flags, prefsrc, metrics (RTAX_* attributes), and nexthops (gateways)
                 * are used to find routes. See fib_find_info() in net/ipv4/fib_semantics.c of the kernel. */
                siphash24_compress_typesafe(route->protocol, state);
                siphash24_compress_typesafe(route->scope, state);
                siphash24_compress_typesafe(route->type, state);
                unsigned flags = route->flags & ~RTNH_COMPARE_MASK;
                siphash24_compress_typesafe(flags, state);
                in_addr_hash_func(&route->prefsrc, route->family, state);
 
                /* nexthops (id, number of nexthops, nexthop) */
                route_nexthops_hash_func(route, state);
 
                /* metrics */
                route_metric_hash_func(&route->metric, state);
                break;
 
        case AF_INET6:
                /* First, table and destination prefix are used for classifying routes.
                 * See fib6_add() and fib6_add_1() in net/ipv6/ip6_fib.c of the kernel. */
                siphash24_compress_typesafe(route->table, state);
                in_addr_hash_func(&route->dst, route->family, state);
                siphash24_compress_typesafe(route->dst_prefixlen, state);
 
                /* Then, source prefix is used. See fib6_add(). */
                in_addr_hash_func(&route->src, route->family, state);
                siphash24_compress_typesafe(route->src_prefixlen, state);
 
                /* See fib6_add_rt2node(). */
                siphash24_compress_typesafe(route->priority, state);
 
                /* See rt6_duplicate_nexthop() in include/net/ip6_route.h of the kernel.
                 * Here, we hash nexthop in a similar way as the one for IPv4. */
                route_nexthops_hash_func(route, state);
 
                /* Unlike IPv4 routes, metrics are not taken into account. */
                break;
 
        default:
                assert_not_reached();
        }
}
 
static int route_compare_func(const Route *a, const Route *b) {
        int r;
 
        r = CMP(a->family, b->family);
        if (r != 0)
                return r;
 
        switch (a->family) {
        case AF_INET:
                r = CMP(a->table, b->table);
                if (r != 0)
                        return r;
 
                r = memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
                if (r != 0)
                        return r;
 
                r = CMP(a->dst_prefixlen, b->dst_prefixlen);
                if (r != 0)
                        return r;
 
                r = CMP(a->priority, b->priority);
                if (r != 0)
                        return r;
 
                r = CMP(a->tos, b->tos);
                if (r != 0)
                        return r;
 
                r = CMP(a->protocol, b->protocol);
                if (r != 0)
                        return r;
 
                r = CMP(a->scope, b->scope);
                if (r != 0)
                        return r;
 
                r = CMP(a->type, b->type);
                if (r != 0)
                        return r;
 
                r = CMP(a->flags & ~RTNH_COMPARE_MASK, b->flags & ~RTNH_COMPARE_MASK);
                if (r != 0)
                        return r;
 
                r = memcmp(&a->prefsrc, &b->prefsrc, FAMILY_ADDRESS_SIZE(a->family));
                if (r != 0)
                        return r;
 
                r = route_nexthops_compare_func(a, b);
                if (r != 0)
                        return r;
 
                return route_metric_compare_func(&a->metric, &b->metric);
 
        case AF_INET6:
                r = CMP(a->table, b->table);
                if (r != 0)
                        return r;
 
                r = memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
                if (r != 0)
                        return r;
 
                r = CMP(a->dst_prefixlen, b->dst_prefixlen);
                if (r != 0)
                        return r;
 
                r = memcmp(&a->src, &b->src, FAMILY_ADDRESS_SIZE(a->family));
                if (r != 0)
                        return r;
 
                r = CMP(a->src_prefixlen, b->src_prefixlen);
                if (r != 0)
                        return r;
 
                r = CMP(a->priority, b->priority);
                if (r != 0)
                        return r;
 
                return route_nexthops_compare_func(a, b);
 
        default:
                assert_not_reached();
        }
}
 
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
                route_hash_ops,
                Route,
                route_hash_func,
                route_compare_func,
                route_detach);
 
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
                route_hash_ops_unref,
                Route,
                route_hash_func,
                route_compare_func,
                route_unref);
 
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
                route_section_hash_ops,
                ConfigSection,
                config_section_hash_func,
                config_section_compare_func,
                Route,
                route_detach);
 
int route_new(Route **ret) {
        _cleanup_(route_unrefp) Route *route = NULL;
 
        assert(ret);
 
        route = new(Route, 1);
        if (!route)
                return -ENOMEM;
 
        *route = (Route) {
                .n_ref = 1,
                .family = AF_UNSPEC,
                .scope = RT_SCOPE_UNIVERSE,
                .protocol = RTPROT_UNSPEC,
                .type = RTN_UNICAST,
                .table = RT_TABLE_MAIN,
                .lifetime_usec = USEC_INFINITY,
                .gateway_onlink = -1,
        };
 
        *ret = TAKE_PTR(route);
 
        return 0;
}
 
int route_new_static(Network *network, const char *filename, unsigned section_line, Route **ret) {
        _cleanup_(config_section_freep) ConfigSection *n = NULL;
        _cleanup_(route_unrefp) Route *route = NULL;
        int r;
 
        assert(network);
        assert(ret);
        assert(filename);
        assert(section_line > 0);
 
        r = config_section_new(filename, section_line, &n);
        if (r < 0)
                return r;
 
        route = hashmap_get(network->routes_by_section, n);
        if (route) {
                *ret = TAKE_PTR(route);
                return 0;
        }
 
        if (hashmap_size(network->routes_by_section) >= routes_max())
                return -E2BIG;
 
        r = route_new(&route);
        if (r < 0)
                return r;
 
        route->protocol = RTPROT_STATIC;
        route->network = network;
        route->section = TAKE_PTR(n);
        route->source = NETWORK_CONFIG_SOURCE_STATIC;
 
        r = hashmap_ensure_put(&network->routes_by_section, &route_section_hash_ops, route->section, route);
        if (r < 0)
                return r;
 
        *ret = TAKE_PTR(route);
        return 0;
}
 
static int route_attach(Manager *manager, Route *route) {
        int r;
 
        assert(manager);
        assert(route);
        assert(!route->network);
        assert(!route->wireguard);
 
        r = set_ensure_put(&manager->routes, &route_hash_ops, route);
        if (r < 0)
                return r;
        if (r == 0)
                return -EEXIST;
 
        route->manager = manager;
        return 0;
}
 
int route_get(Manager *manager, const Route *route, Route **ret) {
        Route *existing;
 
        assert(manager);
        assert(route);
 
        existing = set_get(manager->routes, route);
        if (!existing)
                return -ENOENT;
 
        if (ret)
                *ret = existing;
 
        return 0;
}
 
static int route_get_link(Manager *manager, const Route *route, Link **ret) {
        int r;
 
        assert(manager);
        assert(route);
 
        if (route->nexthop_id != 0) {
                NextHop *nh;
 
                r = nexthop_get_by_id(manager, route->nexthop_id, &nh);
                if (r < 0)
                        return r;
 
                return link_get_by_index(manager, nh->ifindex, ret);
        }
 
        return route_nexthop_get_link(manager, &route->nexthop, ret);
}
 
bool route_is_bound_to_link(const Route *route, Link *link) {
        assert(route);
        assert(link);
        assert(link->manager);
 
        Link *route_link;
        if (route_get_link(link->manager, route, &route_link) < 0)
                return false;
 
        return route_link->ifindex == link->ifindex;
}
 
int route_get_request(Manager *manager, const Route *route, Request **ret) {
        Request *req;
 
        assert(manager);
        assert(route);
 
        req = ordered_set_get(manager->request_queue,
                              &(const Request) {
                                      .type = REQUEST_TYPE_ROUTE,
                                      .userdata = (void*) route,
                                      .hash_func = (hash_func_t) route_hash_func,
                                      .compare_func = (compare_func_t) route_compare_func,
                              });
        if (!req)
                return -ENOENT;
 
        if (ret)
                *ret = req;
        return 0;
}
 
int route_dup(const Route *src, const RouteNextHop *nh, Route **ret) {
        _cleanup_(route_unrefp) Route *dest = NULL;
        int r;
 
        assert(src);
        assert(ret);
 
        dest = newdup(Route, src, 1);
        if (!dest)
                return -ENOMEM;
 
        /* Unset number of reference and all pointers */
        dest->n_ref = 1;
        dest->manager = NULL;
        dest->network = NULL;
        dest->wireguard = NULL;
        dest->section = NULL;
        dest->nexthop = ROUTE_NEXTHOP_NULL;
        dest->nexthops = NULL;
        dest->metric = ROUTE_METRIC_NULL;
        dest->expire = NULL;
 
        r = route_nexthops_copy(src, nh, dest);
        if (r < 0)
                return r;
 
        r = route_metric_copy(&src->metric, &dest->metric);
        if (r < 0)
                return r;
 
        *ret = TAKE_PTR(dest);
        return 0;
}
 
static int route_to_string(const Route *route, Manager *manager, char **ret) {
        _cleanup_free_ char *nexthop = NULL, *prefsrc = NULL,
                *table = NULL, *scope = NULL, *proto = NULL, *flags = NULL;
        const char *dst, *src;
 
        assert(route);
        assert(manager);
        assert(ret);
 
        dst = in_addr_is_set(route->family, &route->dst) || route->dst_prefixlen > 0 ?
                IN_ADDR_PREFIX_TO_STRING(route->family, &route->dst, route->dst_prefixlen) : NULL;
        src = in_addr_is_set(route->family, &route->src) || route->src_prefixlen > 0 ?
                IN_ADDR_PREFIX_TO_STRING(route->family, &route->src, route->src_prefixlen) : NULL;
 
        (void) route_nexthops_to_string(route, &nexthop);
 
        if (in_addr_is_set(route->family, &route->prefsrc))
                (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
        (void) route_scope_to_string_alloc(route->scope, &scope);
        (void) manager_get_route_table_to_string(manager, route->table, /* append_num= */ true, &table);
        (void) route_protocol_full_to_string_alloc(route->protocol, &proto);
        (void) route_flags_to_string_alloc(route->flags, &flags);
 
        if (asprintf(ret,
                     "dst: %s, src: %s, %s, prefsrc: %s, "
                     "table: %s, priority: %"PRIu32", "
                     "proto: %s, scope: %s, "
                     "type: %s, flags: %s",
                     strna(dst), strna(src), strna(nexthop), strna(prefsrc),
                     strna(table), route->priority,
                     strna(proto), strna(scope),
                     strna(route_type_to_string(route->type)), strna(flags)) < 0)
                return -ENOMEM;
 
        return 0;
}
 
void log_route_debug(const Route *route, const char *str, Manager *manager) {
        _cleanup_free_ char *state = NULL, *route_str = NULL;
        Link *link = NULL;
 
        assert(route);
        assert(str);
        assert(manager);
 
        if (!DEBUG_LOGGING)
                return;
 
        (void) route_get_link(manager, route, &link);
        (void) network_config_state_to_string_alloc(route->state, &state);
        (void) route_to_string(route, manager, &route_str);
 
        log_link_debug(link, "%s %s route (%s): %s",
                       str, strna(network_config_source_to_string(route->source)), strna(state), strna(route_str));
}
 
static void route_forget(Manager *manager, Route *route, const char *msg) {
        assert(manager);
        assert(route);
        assert(msg);
 
        Request *req;
        if (route_get_request(manager, route, &req) >= 0)
                route_enter_removed(req->userdata);
 
        if (!route->manager && route_get(manager, route, &route) < 0)
                return;
 
        route_enter_removed(route);
        log_route_debug(route, msg, manager);
        route_detach(route);
}
 
static int route_set_netlink_message(const Route *route, sd_netlink_message *m) {
        int r;
 
        assert(route);
        assert(m);
 
        /* rtmsg header (and relevant attributes) */
        if (route->dst_prefixlen > 0) {
                r = netlink_message_append_in_addr_union(m, RTA_DST, route->family, &route->dst);
                if (r < 0)
                        return r;
 
                r = sd_rtnl_message_route_set_dst_prefixlen(m, route->dst_prefixlen);
                if (r < 0)
                        return r;
        }
 
        if (route->src_prefixlen > 0) {
                r = netlink_message_append_in_addr_union(m, RTA_SRC, route->family, &route->src);
                if (r < 0)
                        return r;
 
                r = sd_rtnl_message_route_set_src_prefixlen(m, route->src_prefixlen);
                if (r < 0)
                        return r;
        }
 
        r = sd_rtnl_message_route_set_tos(m, route->tos);
        if (r < 0)
                return r;
 
        r = sd_rtnl_message_route_set_scope(m, route->scope);
        if (r < 0)
                return r;
 
        r = sd_rtnl_message_route_set_type(m, route->type);
        if (r < 0)
                return r;
 
        r = sd_rtnl_message_route_set_flags(m, route->flags & ~RTNH_COMPARE_MASK);
        if (r < 0)
                return r;
 
        /* attributes */
        r = sd_netlink_message_append_u32(m, RTA_PRIORITY, route->priority);
        if (r < 0)
                return r;
 
        if (in_addr_is_set(route->family, &route->prefsrc)) {
                r = netlink_message_append_in_addr_union(m, RTA_PREFSRC, route->family, &route->prefsrc);
                if (r < 0)
                        return r;
        }
 
        if (route->table < 256) {
                r = sd_rtnl_message_route_set_table(m, route->table);
                if (r < 0)
                        return r;
        } else {
                r = sd_rtnl_message_route_set_table(m, RT_TABLE_UNSPEC);
                if (r < 0)
                        return r;
 
                /* Table attribute to allow more than 256. */
                r = sd_netlink_message_append_u32(m, RTA_TABLE, route->table);
                if (r < 0)
                        return r;
        }
 
        r = sd_netlink_message_append_u8(m, RTA_PREF, route->pref);
        if (r < 0)
                return r;
 
        /* nexthops */
        r = route_nexthops_set_netlink_message(route, m);
        if (r < 0)
                return r;
 
        /* metrics */
        r = route_metric_set_netlink_message(&route->metric, m);
        if (r < 0)
                return r;
 
        return 0;
}
 
static int route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, RemoveRequest *rreq) {
        int r;
 
        assert(m);
        assert(rreq);
 
        Manager *manager = ASSERT_PTR(rreq->manager);
        Route *route = ASSERT_PTR(rreq->userdata);
 
        r = sd_netlink_message_get_errno(m);
        if (r < 0) {
                log_message_full_errno(m,
                                       (r == -ESRCH || /* the route is already removed? */
                                        (r == -EINVAL && route->nexthop_id != 0) || /* The nexthop is already removed? */
                                        !route->manager) ? /* already detached? */
                                       LOG_DEBUG : LOG_WARNING,
                                       r, "Could not drop route, ignoring");
 
                /* If the route cannot be removed, then assume the route is already removed. */
                route_forget(manager, route, "Forgetting");
        }
 
        return 1;
}
 
int route_remove(Route *route, Manager *manager) {
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        Link *link = NULL;
        int r;
 
        assert(route);
        assert(manager);
 
        if (manager->state == MANAGER_STOPPED)
                return 0; /* The remove request will not be queued anyway. Suppress logging below. */
 
        /* If the route is remembered, then use the remembered object. */
        (void) route_get(manager, route, &route);
 
        log_route_debug(route, "Removing", manager);
 
        /* For logging. */
        (void) route_get_link(manager, route, &link);
 
        r = sd_rtnl_message_new_route(manager->rtnl, &m, RTM_DELROUTE, route->family, route->protocol);
        if (r < 0)
                return log_link_warning_errno(link, r, "Could not create netlink message: %m");
 
        r = route_set_netlink_message(route, m);
        if (r < 0)
                return log_link_warning_errno(link, r, "Could not fill netlink message: %m");
 
        r = manager_remove_request_add(manager, route, route, manager->rtnl, m, route_remove_handler);
        if (r < 0)
                return log_link_warning_errno(link, r, "Could not queue rtnetlink message: %m");
 
        route_enter_removing(route);
        return 0;
}
 
int route_remove_and_cancel(Route *route, Manager *manager) {
        _cleanup_(request_unrefp) Request *req = NULL;
        bool waiting = false;
 
        assert(route);
        assert(manager);
 
        /* If the route is remembered by the manager, then use the remembered object. */
        (void) route_get(manager, route, &route);
 
        /* Cancel the request for the route. If the request is already called but we have not received the
         * notification about the request, then explicitly remove the route. */
        if (route_get_request(manager, route, &req) >= 0) {
                request_ref(req); /* avoid the request freed by request_detach() */
                waiting = req->waiting_reply;
                request_detach(req);
                route_cancel_requesting(route);
        }
 
        /* If we know that the route will come or already exists, remove it. */
        if (waiting || (route->manager && route_exists(route)))
                return route_remove(route, manager);
 
        return 0;
}
 
static int route_expire_handler(sd_event_source *s, uint64_t usec, void *userdata) {
        Route *route = ASSERT_PTR(userdata);
        int r;
 
        if (!route->manager)
                return 0; /* already detached. */
 
        r = route_remove(route, route->manager);
        if (r < 0) {
                Link *link = NULL;
                (void) route_get_link(route->manager, route, &link);
                log_link_warning_errno(link, r, "Could not remove route: %m");
                if (link)
                        link_enter_failed(link);
        }
 
        return 1;
}
 
static int route_setup_timer(Route *route, const struct rta_cacheinfo *cacheinfo) {
        int r;
 
        assert(route);
 
        if (cacheinfo && cacheinfo->rta_expires != 0)
                route->expiration_managed_by_kernel = true;
 
        if (route->lifetime_usec == USEC_INFINITY || /* We do not request expiration for the route. */
            route->expiration_managed_by_kernel) {   /* We have received nonzero expiration previously. The expiration is managed by the kernel. */
                route->expire = sd_event_source_disable_unref(route->expire);
                return 0;
        }
 
        Manager *manager = ASSERT_PTR(route->manager);
        r = event_reset_time(manager->event, &route->expire, CLOCK_BOOTTIME,
                             route->lifetime_usec, 0, route_expire_handler, route, 0, "route-expiration", true);
        if (r < 0) {
                Link *link = NULL;
                (void) route_get_link(manager, route, &link);
                return log_link_warning_errno(link, r, "Failed to configure expiration timer for route, ignoring: %m");
        }
 
        log_route_debug(route, "Configured expiration timer for", manager);
        return 1;
}
 
static int route_update_by_request(Route *route, Request *req) {
        assert(route);
        assert(req);
 
        Route *rt = ASSERT_PTR(req->userdata);
 
        route->provider = rt->provider;
        route->source = rt->source;
        route->lifetime_usec = rt->lifetime_usec;
 
        return 0;
}
 
static int route_update_on_existing_one(Request *req, Route *requested) {
        Manager *manager = ASSERT_PTR(ASSERT_PTR(req)->manager);
        Route *existing;
        int r;
 
        assert(requested);
 
        if (route_get(manager, requested, &existing) < 0)
                return 0;
 
        r = route_update_by_request(existing, req);
        if (r < 0)
                return r;
 
        r = route_setup_timer(existing, NULL);
        if (r < 0)
                return r;
 
        /* This may be a bug in the kernel, but the MTU of an IPv6 route can be updated only when the
         * route has an expiration timer managed by the kernel (not by us). See fib6_add_rt2node() in
         * net/ipv6/ip6_fib.c of the kernel. */
        if (existing->family == AF_INET6 &&
            existing->expiration_managed_by_kernel) {
                r = route_metric_set(&existing->metric, RTAX_MTU, route_metric_get(&requested->metric, RTAX_MTU));
                if (r < 0)
                        return r;
        }
 
        route_enter_configured(existing);
        return 0;
}
 
static int route_update_on_existing(Request *req) {
        Route *rt = ASSERT_PTR(ASSERT_PTR(req)->userdata);
        int r;
 
        if (!req->manager)
                /* Already detached? At least there are two possibilities then.
                 * 1) The interface is removed, and all queued requests for the interface are cancelled.
                 * 2) networkd is now stopping, hence all queued requests are cancelled.
                 * Anyway, we can ignore the request, and there is nothing we can do. */
                return 0;
 
        if (rt->family == AF_INET || ordered_set_isempty(rt->nexthops))
                return route_update_on_existing_one(req, rt);
 
        RouteNextHop *nh;
        ORDERED_SET_FOREACH(nh, rt->nexthops) {
                _cleanup_(route_unrefp) Route *dup = NULL;
 
                r = route_dup(rt, nh, &dup);
                if (r < 0)
                        return r;
 
                r = route_update_on_existing_one(req, dup);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
int route_configure_handler_internal(sd_netlink_message *m, Request *req, Route *route) {
        int r;
 
        assert(m);
        assert(req);
        assert(route);
 
        Link *link = ASSERT_PTR(req->link);
 
        r = sd_netlink_message_get_errno(m);
        if (r == -EEXIST) {
                /* When re-configuring an existing route, kernel does not send RTM_NEWROUTE notification, so
                 * here we need to update the state, provider, source, timer, and so on. */
                r = route_update_on_existing(req);
                if (r < 0) {
                        log_link_warning_errno(link, r, "Failed to update existing route: %m");
                        link_enter_failed(link);
                        return 0;
                }
 
                return 1;
        }
        if (r < 0) {
                _cleanup_free_ char *str = NULL;
                (void) route_to_string(route, link->manager, &str);
                log_link_message_warning_errno(link, m, r, "Failed to configure %s route (%s)",
                                               network_config_source_to_string(route->source), strna(str));
                link_enter_failed(link);
                return 0;
        }
 
        return 1;
}
 
static int route_configure(const Route *route, uint32_t lifetime_sec, Link *link, Request *req) {
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        int r;
 
        assert(route);
        assert(link);
        assert(link->manager);
        assert(req);
 
        log_route_debug(route, "Configuring", link->manager);
 
        r = sd_rtnl_message_new_route(link->manager->rtnl, &m, RTM_NEWROUTE, route->family, route->protocol);
        if (r < 0)
                return r;
 
        r = route_set_netlink_message(route, m);
        if (r < 0)
                return r;
 
        if (lifetime_sec != UINT32_MAX) {
                r = sd_netlink_message_append_u32(m, RTA_EXPIRES, lifetime_sec);
                if (r < 0)
                        return r;
        }
 
        return request_call_netlink_async(link->manager->rtnl, m, req);
}
 
static int route_requeue_request(Request *req, Link *link, const Route *route) {
        _unused_ _cleanup_(request_unrefp) Request *req_unref = NULL;
        _cleanup_(route_unrefp) Route *tmp = NULL;
        int r;
 
        assert(req);
        assert(link);
        assert(link->manager);
        assert(route);
 
        /* It is not possible to adjust the Route object owned by Request, as it is used as a key to manage
         * Request objects in the queue. Hence, we need to re-request with the updated Route object. */
 
        if (!route_nexthops_needs_adjust(route))
                return 0; /* The Route object does not need the adjustment. Continue with it. */
 
        r = route_dup(route, NULL, &tmp);
        if (r < 0)
                return r;
 
        r = route_adjust_nexthops(tmp, link);
        if (r <= 0)
                return r;
 
        if (route_compare_func(route, tmp) == 0 && route->type == tmp->type)
                return 0; /* No effective change?? That's OK. */
 
        /* Avoid the request to be freed by request_detach(). */
        req_unref = request_ref(req);
 
        /* Detach the request from the queue, to make not the new request is deduped.
         * Why this is necessary? IPv6 routes with different type may be handled as the same,
         * As commented in route_adjust_nexthops(), we need to configure the adjusted type,
         * otherwise we cannot remove the route on reconfigure or so. If we request the new Route object
         * without detaching the current request, the new request is deduped, and the route is configured
         * with unmodified type. */
        request_detach(req);
 
        /* Request the route with the adjusted Route object combined with the same other parameters. */
        r = link_requeue_request(link, req, tmp, NULL);
        if (r < 0)
                return r;
        if (r == 0)
                return 1; /* Already queued?? That's OK. Maybe, [Route] section is effectively duplicated. */
 
        TAKE_PTR(tmp);
        return 1; /* New request is queued. Finish to process this request. */
}
 
static int route_is_ready_to_configure(const Route *route, Link *link) {
        assert(route);
        assert(link);
 
        if (!link_is_ready_to_configure(link, /* allow_unmanaged= */ false))
                return false;
 
        if (in_addr_is_set(route->family, &route->prefsrc) > 0) {
                Address *a;
 
                if (manager_get_address(link->manager, route->family, &route->prefsrc, &a) < 0)
                        return false;
 
                if (!address_is_ready(a))
                        return false;
        }
 
        return route_nexthops_is_ready_to_configure(route, link->manager);
}
 
static int route_process_request(Request *req, Link *link, Route *route) {
        Route *existing;
        int r;
 
        assert(req);
        assert(link);
        assert(link->manager);
        assert(route);
 
        r = route_is_ready_to_configure(route, link);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to check if route is ready to configure: %m");
        if (r == 0)
                return 0;
 
        usec_t now_usec;
        assert_se(sd_event_now(link->manager->event, CLOCK_BOOTTIME, &now_usec) >= 0);
        uint32_t sec = usec_to_sec(route->lifetime_usec, now_usec);
        if (sec == 0) {
                log_link_debug(link, "Refuse to configure %s route with zero lifetime.",
                               network_config_source_to_string(route->source));
 
                route_cancel_requesting(route);
                if (route_get(link->manager, route, &existing) >= 0)
                        route_cancel_requesting(existing);
                return 1;
        }
 
        r = route_requeue_request(req, link, route);
        if (r != 0)
                return r;
 
        r = route_configure(route, sec, link, req);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to configure route: %m");
 
        route_enter_configuring(route);
        if (route_get(link->manager, route, &existing) >= 0)
                route_enter_configuring(existing);
        return 1;
}
 
static int link_request_route_one(
                Link *link,
                const Route *route,
                const RouteNextHop *nh,
                unsigned *message_counter,
                route_netlink_handler_t netlink_handler) {
 
        _cleanup_(route_unrefp) Route *tmp = NULL;
        Route *existing = NULL;
        int r;
 
        assert(link);
        assert(link->manager);
        assert(route);
 
        r = route_dup(route, nh, &tmp);
        if (r < 0)
                return r;
 
        r = route_adjust_nexthops(tmp, link);
        if (r < 0)
                return r;
 
        if (route_get(link->manager, tmp, &existing) >= 0)
                /* Copy state for logging below. */
                tmp->state = existing->state;
 
        log_route_debug(tmp, "Requesting", link->manager);
        r = link_queue_request_safe(link, REQUEST_TYPE_ROUTE,
                                    tmp,
                                    route_unref,
                                    route_hash_func,