HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/network/networkd-ndisc.c

120,194 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 55a7e8e732530f41eff357a99ddee6543d65c12f

Lines 1001–2000 of 3,387

 
        r = sd_ndisc_redirect_get_target_address(x, &a);
        if (r < 0)
                return r;
 
        r = sd_ndisc_redirect_get_target_address(y, &b);
        if (r < 0)
                return r;
 
        return in6_addr_equal(&a, &b);
}
 
static int ndisc_redirect_drop_conflict(Link *link, sd_ndisc_redirect *rd) {
        _cleanup_(sd_ndisc_redirect_unrefp) sd_ndisc_redirect *existing = NULL;
        int r;
 
        assert(link);
        assert(rd);
 
        existing = set_remove(link->ndisc_redirects, rd);
        if (!existing)
                return 0;
 
        r = ndisc_redirect_equal(rd, existing);
        if (r != 0)
                return r;
 
        return ndisc_remove_redirect_route(link, existing);
}
 
static int ndisc_redirect_verify_sender(Link *link, sd_ndisc_redirect *rd) {
        int r;
 
        assert(link);
        assert(rd);
 
        /* RFC 4861 section 8.1
        * The IP source address of the Redirect is the same as the current first-hop router for the specified
        * ICMP Destination Address. */
 
        struct in6_addr sender;
        r = sd_ndisc_redirect_get_sender_address(rd, &sender);
        if (r < 0)
                return r;
 
        /* We will reuse the sender's router lifetime as the lifetime of the redirect route. Hence, if we
         * have not remembered an RA from the sender, refuse the Redirect message. */
        sd_ndisc_router *router = hashmap_get(link->ndisc_routers_by_sender, &sender);
        if (!router)
                return false;
 
        sd_ndisc_redirect *existing = set_get(link->ndisc_redirects, rd);
        if (existing) {
                struct in6_addr target, dest;
 
                /* If we have received Redirect message for the host, the sender must be the previous target. */
 
                r = sd_ndisc_redirect_get_target_address(existing, &target);
                if (r < 0)
                        return r;
 
                if (in6_addr_equal(&sender, &target))
                        return true;
 
                /* If the existing redirect route is on-link, that is, the destination and target address are
                 * equivalent, then also accept Redirect message from the current default router. This is not
                 * mentioned by the RFC, but without this, we cannot update on-link redirect route. */
                r = sd_ndisc_redirect_get_destination_address(existing, &dest);
                if (r < 0)
                        return r;
 
                if (!in6_addr_equal(&dest, &target))
                        return false;
        }
 
        /* Check if the sender is one of the known router with highest priority. */
        uint8_t preference;
        r = sd_ndisc_router_get_preference(router, &preference);
        if (r < 0)
                return r;
 
        if (preference == SD_NDISC_PREFERENCE_HIGH)
                return true;
 
        sd_ndisc_router *rt;
        HASHMAP_FOREACH(rt, link->ndisc_routers_by_sender) {
                if (rt == router)
                        continue;
 
                uint8_t pref;
                if (sd_ndisc_router_get_preference(rt, &pref) < 0)
                        continue;
 
                if (pref == SD_NDISC_PREFERENCE_HIGH ||
                    (pref == SD_NDISC_PREFERENCE_MEDIUM && preference == SD_NDISC_PREFERENCE_LOW))
                        return false;
        }
 
        return true;
}
 
static int ndisc_redirect_handler(Link *link, sd_ndisc_redirect *rd) {
        int r;
 
        assert(link);
        assert(link->network);
        assert(rd);
 
        if (!link->network->ndisc_use_redirect)
                return 0;
 
        usec_t now_usec;
        r = sd_event_now(link->manager->event, CLOCK_BOOTTIME, &now_usec);
        if (r < 0)
                return r;
 
        r = ndisc_drop_outdated(link, /* router= */ NULL, now_usec);
        if (r < 0)
                return r;
 
        r = ndisc_redirect_verify_sender(link, rd);
        if (r <= 0)
                return r;
 
        /* First, drop conflicting redirect route, if exists. */
        r = ndisc_redirect_drop_conflict(link, rd);
        if (r < 0)
                return r;
 
        if (set_size(link->ndisc_redirects) >= NDISC_REDIRECT_MAX) {
                struct in6_addr dest;
 
                if (sd_ndisc_redirect_get_destination_address(rd, &dest) >= 0)
                        log_link_debug(link, "Too many NDisc redirects per link (%u), ignoring redirect for %s.",
                                       NDISC_REDIRECT_MAX, IN6_ADDR_TO_STRING(&dest));
                return 0;
        }
 
        /* Then, remember the received message. */
        r = set_ensure_put(&link->ndisc_redirects, &ndisc_redirect_hash_ops, rd);
        if (r < 0)
                return r;
 
        sd_ndisc_redirect_ref(rd);
 
        /* Finally, request the corresponding route. */
        _cleanup_(route_unrefp) Route *route = NULL;
        r = ndisc_redirect_route_new(rd, &route);
        if (r < 0)
                return r;
 
        sd_ndisc_router *rt = hashmap_get(link->ndisc_routers_by_sender, &route->provider.in6);
        if (!rt)
                return -EADDRNOTAVAIL;
 
        r = sd_ndisc_router_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &route->lifetime_usec);
        if (r < 0)
                return r;
 
        ndisc_route_prepare(route, link);
 
        r = ndisc_request_route(route, link);
        if (r < 0)
                return r;
        if (r == 0)
                sd_ndisc_redirect_unref(set_remove(link->ndisc_redirects, rd));
 
        /* Unlike ndisc_router_handler(), the redirect path has no readiness re-check of its own. If the route
         * above was refused, ndisc_set_route_nexthop() may have already cleared link->ndisc_configured while
         * requesting a nexthop that we then cancelled - with no netlink handler left to run ndisc_check_ready().
         * Re-check here so the link does not stay unconfigured. */
        return ndisc_check_ready(link);
}
 
static int ndisc_drop_redirect(Link *link, const struct in6_addr *router) {
        int r, ret = 0;
 
        assert(link);
 
        sd_ndisc_redirect *rd;
        SET_FOREACH(rd, link->ndisc_redirects) {
                if (router) {
                        struct in6_addr a;
 
                        if (!(sd_ndisc_redirect_get_sender_address(rd, &a) >= 0 && in6_addr_equal(&a, router)) &&
                            !(sd_ndisc_redirect_get_target_address(rd, &a) >= 0 && in6_addr_equal(&a, router)))
                                continue;
                }
 
                r = ndisc_remove_redirect_route(link, rd);
                if (r < 0)
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Failed to remove redirect route, ignoring: %m"));
 
                sd_ndisc_redirect_unref(set_remove(link->ndisc_redirects, rd));
        }
 
        return ret;
}
 
static int ndisc_update_redirect_sender(Link *link, const struct in6_addr *original_address, const struct in6_addr *current_address) {
        int r;
 
        assert(link);
        assert(original_address);
        assert(current_address);
 
        sd_ndisc_redirect *rd;
        SET_FOREACH(rd, link->ndisc_redirects) {
                struct in6_addr sender;
 
                r = sd_ndisc_redirect_get_sender_address(rd, &sender);
                if (r < 0)
                        return r;
 
                if (!in6_addr_equal(&sender, original_address))
                        continue;
 
                r = sd_ndisc_redirect_set_sender_address(rd, current_address);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int ndisc_router_drop_default(Link *link, sd_ndisc_router *rt) {
        _cleanup_(route_unrefp) Route *route = NULL;
        struct in6_addr gateway;
        int r;
 
        assert(link);
        assert(link->network);
        assert(rt);
 
        r = sd_ndisc_router_get_sender_address(rt, &gateway);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get router address from RA: %m");
 
        r = route_new(&route);
        if (r < 0)
                return log_oom();
 
        route->family = AF_INET6;
        route->nexthop.family = AF_INET6;
        route->nexthop.gw.in6 = gateway;
 
        r = ndisc_remove_router_route(route, link, rt);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to remove the default gateway configured by RA: %m");
 
        Route *route_gw;
        HASHMAP_FOREACH(route_gw, link->network->routes_by_section) {
                _cleanup_(route_unrefp) Route *tmp = NULL;
 
                if (route_gw->source != NETWORK_CONFIG_SOURCE_NDISC)
                        continue;
 
                assert(route_gw->nexthop.family == AF_INET6);
 
                r = route_dup(route_gw, NULL, &tmp);
                if (r < 0)
                        return r;
 
                tmp->nexthop.gw.in6 = gateway;
 
                r = ndisc_remove_router_route(tmp, link, rt);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Could not remove semi-static gateway: %m");
        }
 
        return 0;
}
 
static int ndisc_router_process_default(Link *link, sd_ndisc_router *rt) {
        usec_t lifetime_usec;
        struct in6_addr gateway;
        uint8_t preference;
        int r;
 
        assert(link);
        assert(link->network);
        assert(rt);
 
        /* If the router lifetime is zero, the router should not be used as the default gateway. */
        r = sd_ndisc_router_get_lifetime(rt, NULL);
        if (r < 0)
                return r;
        if (r == 0)
                return ndisc_router_drop_default(link, rt);
 
        if (!link->network->ndisc_use_gateway &&
            hashmap_isempty(link->network->routes_by_section))
                return 0;
 
        r = sd_ndisc_router_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get gateway lifetime from RA: %m");
 
        r = sd_ndisc_router_get_sender_address(rt, &gateway);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get gateway address from RA: %m");
 
        /* Only remembered routers may be used as a default gateway. If this sender was refused above because
         * the per-link router limit was reached (see ndisc_remember_router()), don't install a default route
         * or gateway routes for it. */
        if (!hashmap_contains(link->ndisc_routers_by_sender, &gateway))
                return 0;
 
        r = sd_ndisc_router_get_preference(rt, &preference);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get router preference from RA: %m");
 
        if (link->network->ndisc_use_gateway) {
                _cleanup_(route_unrefp) Route *route = NULL;
 
                r = route_new(&route);
                if (r < 0)
                        return log_oom();
 
                route->family = AF_INET6;
                route->pref = preference;
                route->nexthop.family = AF_INET6;
                route->nexthop.gw.in6 = gateway;
                route->lifetime_usec = lifetime_usec;
 
                r = ndisc_request_router_route(route, link, rt);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Could not request default route: %m");
        }
 
        Route *route_gw;
        HASHMAP_FOREACH(route_gw, link->network->routes_by_section) {
                _cleanup_(route_unrefp) Route *route = NULL;
 
                if (route_gw->source != NETWORK_CONFIG_SOURCE_NDISC)
                        continue;
 
                assert(route_gw->nexthop.family == AF_INET6);
 
                r = route_dup(route_gw, NULL, &route);
                if (r < 0)
                        return r;
 
                route->nexthop.gw.in6 = gateway;
                if (!route->pref_set)
                        route->pref = preference;
                route->lifetime_usec = lifetime_usec;
 
                r = ndisc_request_router_route(route, link, rt);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Could not request gateway: %m");
        }
 
        return 0;
}
 
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
                ndisc_router_hash_ops,
                struct in6_addr,
                in6_addr_hash_func,
                in6_addr_compare_func,
                sd_ndisc_router,
                sd_ndisc_router_unref);
 
static int ndisc_update_router_address(Link *link, const struct in6_addr *original_address, const struct in6_addr *current_address) {
        _cleanup_(sd_ndisc_router_unrefp) sd_ndisc_router *rt = NULL;
        int r;
 
        assert(link);
        assert(original_address);
        assert(current_address);
 
        rt = hashmap_remove(link->ndisc_routers_by_sender, original_address);
        if (!rt)
                return 0;
 
        /* If we already received an RA from the new address, then forget the RA from the old address. */
        if (hashmap_contains(link->ndisc_routers_by_sender, current_address))
                return 0;
 
        /* Otherwise, update the sender address of the previously received RA. */
        r = sd_ndisc_router_set_sender_address(rt, current_address);
        if (r < 0)
                return r;
 
        r = hashmap_put(link->ndisc_routers_by_sender, &rt->packet->sender_address, rt);
        if (r < 0)
                return r;
 
        TAKE_PTR(rt);
        return 0;
}
 
static int ndisc_drop_router_one(Link *link, sd_ndisc_router *rt, usec_t timestamp_usec) {
        usec_t lifetime_usec;
        int r;
 
        assert(link);
        assert(rt);
        assert(rt->packet);
 
        r = sd_ndisc_router_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec);
        if (r < 0)
                return r;
 
        if (lifetime_usec > timestamp_usec)
                return 0;
 
        r = ndisc_drop_redirect(link, &rt->packet->sender_address);
 
        sd_ndisc_router_unref(hashmap_remove(link->ndisc_routers_by_sender, &rt->packet->sender_address));
 
        return r;
}
 
static int ndisc_drop_routers(Link *link, const struct in6_addr *router, usec_t timestamp_usec) {
        sd_ndisc_router *rt;
        int ret = 0;
 
        assert(link);
 
        if (router) {
                rt = hashmap_get(link->ndisc_routers_by_sender, router);
                if (!rt)
                        return 0;
 
                return ndisc_drop_router_one(link, rt, timestamp_usec);
        }
 
        HASHMAP_FOREACH_KEY(rt, router, link->ndisc_routers_by_sender)
                RET_GATHER(ret, ndisc_drop_router_one(link, rt, timestamp_usec));
 
        return ret;
}
 
static int ndisc_remember_router(Link *link, sd_ndisc_router *rt) {
        int r;
 
        assert(link);
        assert(rt);
        assert(rt->packet);
 
        sd_ndisc_router_unref(hashmap_remove(link->ndisc_routers_by_sender, &rt->packet->sender_address));
 
        /* Remember RAs with non-zero lifetime. */
        r = sd_ndisc_router_get_lifetime(rt, NULL);
        if (r <= 0)
                return r;
 
        if (hashmap_size(link->ndisc_routers_by_sender) >= NDISC_ROUTER_MAX) {
                log_link_debug(link, "Too many routers remembered per link (%u), ignoring RA from %s.",
                               NDISC_ROUTER_MAX, IN6_ADDR_TO_STRING(&rt->packet->sender_address));
                return 0;
        }
 
        r = hashmap_ensure_put(&link->ndisc_routers_by_sender, &ndisc_router_hash_ops, &rt->packet->sender_address, rt);
        if (r < 0)
                return r;
 
        sd_ndisc_router_ref(rt);
        return 0;
}
 
static int ndisc_router_process_reachable_time(Link *link, sd_ndisc_router *rt) {
        usec_t reachable_time, msec;
        int r;
 
        assert(link);
        assert(link->manager);
        assert(link->network);
        assert(rt);
 
        if (!link->network->ndisc_use_reachable_time)
                return 0;
 
        r = sd_ndisc_router_get_reachable_time(rt, &reachable_time);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get reachable time from RA: %m");
 
        /* 0 is the unspecified value and must not be set (see RFC4861, 6.3.4) */
        if (!timestamp_is_set(reachable_time))
                return 0;
 
        msec = DIV_ROUND_UP(reachable_time, USEC_PER_MSEC);
        if (msec <= 0 || msec > UINT32_MAX) {
                log_link_debug(link, "Failed to get reachable time from RA - out of range (%"PRIu64"), ignoring", msec);
                return 0;
        }
 
        /* Set the reachable time for Neighbor Solicitations. */
        r = sysctl_write_ip_neighbor_property_uint32(AF_INET6, link->ifname, "base_reachable_time_ms", (uint32_t) msec, manager_get_sysctl_shadow(link->manager));
        if (r < 0)
                log_link_warning_errno(link, r, "Failed to apply neighbor reachable time (%"PRIu64"), ignoring: %m", msec);
 
        return 0;
}
 
static int ndisc_router_process_retransmission_time(Link *link, sd_ndisc_router *rt) {
        usec_t retrans_time, msec;
        int r;
 
        assert(link);
        assert(link->manager);
        assert(link->network);
        assert(rt);
 
        if (!link->network->ndisc_use_retransmission_time)
                return 0;
 
        r = sd_ndisc_router_get_retransmission_time(rt, &retrans_time);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get retransmission time from RA: %m");
 
        /* 0 is the unspecified value and must not be set (see RFC4861, 6.3.4) */
        if (!timestamp_is_set(retrans_time))
                return 0;
 
        msec = DIV_ROUND_UP(retrans_time, USEC_PER_MSEC);
        if (msec <= 0 || msec > UINT32_MAX) {
                log_link_debug(link, "Failed to get retransmission time from RA - out of range (%"PRIu64"), ignoring", msec);
                return 0;
        }
 
        /* Set the retransmission time for Neighbor Solicitations. */
        r = sysctl_write_ip_neighbor_property_uint32(AF_INET6, link->ifname, "retrans_time_ms", (uint32_t) msec, manager_get_sysctl_shadow(link->manager));
        if (r < 0)
                log_link_warning_errno(link, r, "Failed to apply neighbor retransmission time (%"PRIu64"), ignoring: %m", msec);
 
        return 0;
}
 
static int ndisc_router_process_hop_limit(Link *link, sd_ndisc_router *rt) {
        uint8_t hop_limit;
        int r;
 
        assert(link);
        assert(link->manager);
        assert(link->network);
        assert(rt);
 
        if (!link->network->ndisc_use_hop_limit)
                return 0;
 
        r = sd_ndisc_router_get_hop_limit(rt, &hop_limit);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get hop limit from RA: %m");
 
        /* 0 is the unspecified value and must not be set (see RFC4861, 6.3.4):
         *
         * A Router Advertisement field (e.g., Cur Hop Limit, Reachable Time, and Retrans Timer) may contain
         * a value denoting that it is unspecified. In such cases, the parameter should be ignored and the
         * host should continue using whatever value it is already using. In particular, a host MUST NOT
         * interpret the unspecified value as meaning change back to the default value that was in use before
         * the first Router Advertisement was received.
         *
         * If the received Cur Hop Limit value is non-zero, the host SHOULD set
         * its CurHopLimit variable to the received value. */
        if (hop_limit <= 0)
                return 0;
 
        r = sysctl_write_ip_property_uint32(AF_INET6, link->ifname, "hop_limit", (uint32_t) hop_limit, manager_get_sysctl_shadow(link->manager));
        if (r < 0)
                log_link_warning_errno(link, r, "Failed to apply hop_limit (%u), ignoring: %m", hop_limit);
 
        return 0;
}
 
static int ndisc_router_process_mtu(Link *link, sd_ndisc_router *rt) {
        uint32_t mtu;
        int r;
 
        assert(link);
        assert(link->network);
        assert(rt);
 
        if (!link->network->ndisc_use_mtu)
                return 0;
 
        r = sd_ndisc_router_get_mtu(rt, &mtu);
        if (r == -ENODATA)
                return 0;
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get MTU from RA: %m");
 
        link->ndisc_mtu = mtu;
 
        (void) link_set_ipv6_mtu(link, LOG_DEBUG);
 
        return 0;
}
 
static int ndisc_address_set_lifetime(Address *address, Link *link, sd_ndisc_router *rt) {
        Address *existing;
        usec_t t;
        int r;
 
        assert(address);
        assert(link);
        assert(rt);
 
        /* This is mostly based on RFC 4862 section 5.5.3 (e). However, the definition of 'RemainingLifetime'
         * is ambiguous, and there is no clear explanation when the address is not assigned yet. If we assume
         * that 'RemainingLifetime' is zero in that case, then IPv6 Core Conformance test [v6LC.3.2.5 Part C]
         * fails. So, in such case, we skip the conditions about 'RemainingLifetime'. */
 
        r = sd_ndisc_router_prefix_get_valid_lifetime_timestamp(rt, CLOCK_BOOTTIME, &address->lifetime_valid_usec);
        if (r < 0)
                return r;
 
        r = sd_ndisc_router_prefix_get_preferred_lifetime_timestamp(rt, CLOCK_BOOTTIME, &address->lifetime_preferred_usec);
        if (r < 0)
                return r;
 
        /* RFC 4862 section 5.5.3 (e)
         * 1. If the received Valid Lifetime is greater than 2 hours or greater than RemainingLifetime,
         *    set the valid lifetime of the corresponding address to the advertised Valid Lifetime. */
        r = sd_ndisc_router_prefix_get_valid_lifetime(rt, &t);
        if (r < 0)
                return r;
 
        if (t > 2 * USEC_PER_HOUR)
                return 0;
 
        if (address_get(link, address, &existing) < 0 || existing->source != NETWORK_CONFIG_SOURCE_NDISC)
                return 0;
 
        if (address->lifetime_valid_usec > existing->lifetime_valid_usec)
                return 0;
 
        /* 2. If RemainingLifetime is less than or equal to 2 hours, ignore the Prefix Information option
         *    with regards to the valid lifetime, unless the Router Advertisement from which this option was
         *    obtained has been authenticated (e.g., via Secure Neighbor Discovery [RFC3971]). If the Router
         *    Advertisement was authenticated, the valid lifetime of the corresponding address should be set
         *    to the Valid Lifetime in the received option.
         *
         * Currently, authentication is not supported. So check the lifetime of the existing address. */
        r = sd_ndisc_router_get_timestamp(rt, CLOCK_BOOTTIME, &t);
        if (r < 0)
                return r;
 
        if (existing->lifetime_valid_usec <= usec_add(t, 2 * USEC_PER_HOUR)) {
                address->lifetime_valid_usec = existing->lifetime_valid_usec;
                return 0;
        }
 
        /* 3. Otherwise, reset the valid lifetime of the corresponding address to 2 hours. */
        address->lifetime_valid_usec = usec_add(t, 2 * USEC_PER_HOUR);
        return 0;
}
 
static int ndisc_router_process_autonomous_prefix(Link *link, sd_ndisc_router *rt) {
        usec_t lifetime_valid_usec, lifetime_preferred_usec;
        struct in6_addr prefix, router;
        uint8_t prefixlen;
        int r;
 
        assert(link);
        assert(link->network);
        assert(rt);
 
        if (!link->network->ndisc_use_autonomous_prefix)
                return 0;
 
        r = sd_ndisc_router_get_sender_address(rt, &router);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get router address: %m");
 
        r = sd_ndisc_router_prefix_get_address(rt, &prefix);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get prefix address: %m");
 
        r = sd_ndisc_router_prefix_get_prefixlen(rt, &prefixlen);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get prefix length: %m");
 
        /* ndisc_generate_addresses() below requires the prefix length <= 64. */
        if (prefixlen > 64) {
                log_link_debug(link, "Prefix is longer than 64, ignoring autonomous prefix %s.",
                               IN6_ADDR_PREFIX_TO_STRING(&prefix, prefixlen));
                return 0;
        }
 
        r = sd_ndisc_router_prefix_get_valid_lifetime(rt, &lifetime_valid_usec);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get prefix valid lifetime: %m");
 
        r = sd_ndisc_router_prefix_get_preferred_lifetime(rt, &lifetime_preferred_usec);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get prefix preferred lifetime: %m");
 
        /* RFC 4862 section 5.5.3 (c)
         * If the preferred lifetime is greater than the valid lifetime, silently ignore the Prefix
         * Information option. */
        if (lifetime_preferred_usec > lifetime_valid_usec)
                return 0;
 
        _cleanup_hashmap_free_ Hashmap *tokens_by_address = NULL;
        r = ndisc_generate_addresses(link, &prefix, prefixlen, &tokens_by_address);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to generate SLAAC addresses: %m");
 
        IPv6Token *token;
        struct in6_addr *a;
        HASHMAP_FOREACH_KEY(token, a, tokens_by_address) {
                _cleanup_(address_unrefp) Address *address = NULL;
 
                r = address_new(&address);
                if (r < 0)
                        return log_oom();
 
                address->provider.in6 = router;
                address->family = AF_INET6;
                address->in_addr.in6 = *a;
                address->prefixlen = prefixlen;
                address->flags = IFA_F_NOPREFIXROUTE|IFA_F_MANAGETEMPADDR;
                address->token = ipv6_token_ref(token);
 
                r = ndisc_address_set_lifetime(address, link, rt);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Failed to set lifetime of SLAAC address: %m");
 
                assert(address->lifetime_preferred_usec <= address->lifetime_valid_usec);
 
                r = ndisc_request_address(address, link);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Could not request SLAAC address: %m");
        }
 
        return 0;
}
 
static int ndisc_router_process_onlink_prefix(Link *link, sd_ndisc_router *rt) {
        _cleanup_(route_unrefp) Route *route = NULL;
        uint8_t prefixlen, preference;
        usec_t lifetime_usec;
        struct in6_addr prefix;
        int r;
 
        assert(link);
        assert(link->network);
        assert(rt);
 
        if (!link->network->ndisc_use_onlink_prefix)
                return 0;
 
        r = sd_ndisc_router_prefix_get_valid_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get prefix lifetime: %m");
 
        r = sd_ndisc_router_prefix_get_address(rt, &prefix);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get prefix address: %m");
 
        r = sd_ndisc_router_prefix_get_prefixlen(rt, &prefixlen);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get prefix length: %m");
 
        /* Prefix Information option does not have preference, hence we use the 'main' preference here */
        r = sd_ndisc_router_get_preference(rt, &preference);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get router preference from RA: %m");
 
        r = route_new(&route);
        if (r < 0)
                return log_oom();
 
        route->family = AF_INET6;
        route->dst.in6 = prefix;
        route->dst_prefixlen = prefixlen;
        route->pref = preference;
        route->lifetime_usec = lifetime_usec;
 
        /* RFC 4861 section 6.3.4:
         * - If the prefix is not already present in the Prefix List, and the Prefix Information option's
         *   Valid Lifetime field is non-zero, create a new entry for the prefix and initialize its
         *   invalidation timer to the Valid Lifetime value in the Prefix Information option.
         *
         * - If the prefix is already present in the host's Prefix List as the result of a previously
         *   received advertisement, reset its invalidation timer to the Valid Lifetime value in the Prefix
         *   Information option. If the new Lifetime value is zero, timeout the prefix immediately. */
        if (lifetime_usec == 0) {
                r = ndisc_remove_router_route(route, link, rt);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Failed to remove prefix route: %m");
        } else {
                r = ndisc_request_router_route(route, link, rt);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Failed to request prefix route: %m");
        }
 
        return 0;
}
 
static int ndisc_router_process_prefix(Link *link, sd_ndisc_router *rt, bool zero_lifetime) {
        uint8_t flags, prefixlen;
        struct in6_addr a;
        int r;
 
        assert(link);
        assert(link->network);
        assert(rt);
 
        usec_t lifetime_usec;
        r = sd_ndisc_router_prefix_get_valid_lifetime(rt, &lifetime_usec);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get prefix lifetime: %m");
 
        if ((lifetime_usec == 0) != zero_lifetime)
                return 0;
 
        r = sd_ndisc_router_prefix_get_address(rt, &a);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get prefix address: %m");
 
        /* RFC 4861 Section 4.6.2:
         * A router SHOULD NOT send a prefix option for the link-local prefix and a host SHOULD ignore such
         * a prefix option. */
        if (in6_addr_is_link_local(&a)) {
                log_link_debug(link, "Received link-local prefix, ignoring prefix.");
                return 0;
        }
 
        r = sd_ndisc_router_prefix_get_prefixlen(rt, &prefixlen);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get prefix length: %m");
 
        if (in6_prefix_is_filtered(&a, prefixlen, link->network->ndisc_allow_listed_prefix, link->network->ndisc_deny_listed_prefix)) {
                if (set_isempty(link->network->ndisc_allow_listed_prefix))
                        log_link_debug(link, "Prefix '%s' is in deny list, ignoring.",
                                       IN6_ADDR_PREFIX_TO_STRING(&a, prefixlen));
                else
                        log_link_debug(link, "Prefix '%s' is not in allow list, ignoring.",
                                       IN6_ADDR_PREFIX_TO_STRING(&a, prefixlen));
                return 0;
        }
 
        r = sd_ndisc_router_prefix_get_flags(rt, &flags);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get RA prefix flags: %m");
 
        if (FLAGS_SET(flags, ND_OPT_PI_FLAG_ONLINK)) {
                r = ndisc_router_process_onlink_prefix(link, rt);
                if (r < 0)
                        return r;
        }
 
        if (FLAGS_SET(flags, ND_OPT_PI_FLAG_AUTO)) {
                r = ndisc_router_process_autonomous_prefix(link, rt);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int ndisc_router_process_route(Link *link, sd_ndisc_router *rt, bool zero_lifetime) {
        _cleanup_(route_unrefp) Route *route = NULL;
        uint8_t preference, prefixlen;
        struct in6_addr gateway, dst;
        usec_t lifetime_usec;
        int r;
 
        assert(link);
 
        if (!link->network->ndisc_use_route_prefix)
                return 0;
 
        r = sd_ndisc_router_route_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get route lifetime from RA: %m");
 
        if ((lifetime_usec == 0) != zero_lifetime)
                return 0;
 
        r = sd_ndisc_router_route_get_address(rt, &dst);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get route destination address: %m");
 
        r = sd_ndisc_router_route_get_prefixlen(rt, &prefixlen);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get route prefix length: %m");
 
        if (in6_prefix_is_filtered(&dst, prefixlen,
                                   link->network->ndisc_allow_listed_route_prefix,
                                   link->network->ndisc_deny_listed_route_prefix)) {
                if (set_isempty(link->network->ndisc_allow_listed_route_prefix))
                        log_link_debug(link, "Route prefix '%s' is in deny list, ignoring.",
                                       IN6_ADDR_PREFIX_TO_STRING(&dst, prefixlen));
                else
                        log_link_debug(link, "Route prefix '%s' is not in allow list, ignoring.",
                                       IN6_ADDR_PREFIX_TO_STRING(&dst, prefixlen));
                return 0;
        }
 
        r = sd_ndisc_router_get_sender_address(rt, &gateway);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get gateway address from RA: %m");
 
        if (link_get_ipv6_address(link, &gateway, NULL) >= 0) {
                if (DEBUG_LOGGING)
                        log_link_debug(link, "Advertised route gateway %s is local to the link, ignoring route",
                                       IN6_ADDR_TO_STRING(&gateway));
                return 0;
        }
 
        r = sd_ndisc_router_route_get_preference(rt, &preference);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get router preference from RA: %m");
 
        r = route_new(&route);
        if (r < 0)
                return log_oom();
 
        route->family = AF_INET6;
        route->pref = preference;
        route->nexthop.gw.in6 = gateway;
        route->nexthop.family = AF_INET6;
        route->dst.in6 = dst;
        route->dst_prefixlen = prefixlen;
        route->lifetime_usec = lifetime_usec;
 
        if (lifetime_usec != 0) {
                r = ndisc_request_router_route(route, link, rt);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Could not request additional route: %m");
        } else {
                r = ndisc_remove_router_route(route, link, rt);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Could not remove additional route with zero lifetime: %m");
        }
 
        return 0;
}
 
static void ndisc_rdnss_hash_func(const NDiscRDNSS *x, struct siphash *state) {
        siphash24_compress_typesafe(x->address, state);
}
 
static int ndisc_rdnss_compare_func(const NDiscRDNSS *a, const NDiscRDNSS *b) {
        return memcmp(&a->address, &b->address, sizeof(a->address));
}
 
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
                ndisc_rdnss_hash_ops,
                NDiscRDNSS,
                ndisc_rdnss_hash_func,
                ndisc_rdnss_compare_func,
                free);
 
static int ndisc_router_process_rdnss(Link *link, sd_ndisc_router *rt, bool zero_lifetime) {
        usec_t lifetime_usec;
        const struct in6_addr *a;
        struct in6_addr router;
        bool updated = false, logged_about_too_many = false;
        int n, r;
 
        assert(link);
        assert(link->network);
        assert(rt);
 
        if (!link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC))
                return 0;
 
        r = sd_ndisc_router_get_sender_address(rt, &router);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get router address from RA: %m");
 
        r = sd_ndisc_router_rdnss_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to get RDNSS lifetime: %m");
 
        if ((lifetime_usec == 0) != zero_lifetime)
                return 0;
 
        n = sd_ndisc_router_rdnss_get_addresses(rt, &a);
        if (n < 0)
                return log_link_warning_errno(link, n, "Failed to get RDNSS addresses: %m");
 
        for (int j = 0; j < n; j++) {
                _cleanup_free_ NDiscRDNSS *x = NULL;
                NDiscRDNSS *rdnss, d = {
                        .address = a[j],
                };
 
                if (lifetime_usec == 0) {
                        /* The entry is outdated. */
                        free(set_remove(link->ndisc_rdnss, &d));
                        updated = true;
                        continue;
                }
 
                rdnss = set_get(link->ndisc_rdnss, &d);
                if (rdnss) {
                        rdnss->router = router;
                        rdnss->lifetime_usec = lifetime_usec;
                        continue;
                }
 
                if (set_size(link->ndisc_rdnss) >= NDISC_RDNSS_MAX) {
                        if (!logged_about_too_many)