HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/network/networkd-ndisc.c
Git blob: 55a7e8e732530f41eff357a99ddee6543d65c12f
Lines 1–1000 of 3,387
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/***
Copyright © 2014 Intel Corporation. All rights reserved.
***/
#include <linux/if_arp.h>
#include <linux/rtnetlink.h>
#include <netinet/icmp6.h>
#include "sd-ndisc.h"
#include "conf-parser.h"
#include "errno-util.h"
#include "event-util.h"
#include "missing-network.h"
#include "ndisc-router-internal.h"
#include "networkd-address.h"
#include "networkd-address-generation.h"
#include "networkd-dhcp6.h"
#include "networkd-ipv6ll.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-ndisc.h"
#include "networkd-nexthop.h"
#include "networkd-queue.h"
#include "networkd-route.h"
#include "networkd-state-file.h"
#include "networkd-sysctl.h"
#include "ordered-set.h"
#include "set.h"
#include "siphash24.h"
#include "socket-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "sysctl-util.h"
#define NDISC_DNSSL_MAX 64U
#define NDISC_RDNSS_MAX 64U
#define NDISC_ENCRYPTED_DNS_MAX 64U
/* Not defined in the RFC, but let's set an upper limit to make not consume much memory.
* This should be safe as typically there should be at most 1 portal per network. */
#define NDISC_CAPTIVE_PORTAL_MAX 64U
/* Neither defined in the RFC. Just for safety. Otherwise, malformed messages can make clients trigger OOM.
* Not sure if the threshold is high enough. Let's adjust later if not. */
#define NDISC_PREF64_MAX 64U
/* Not defined in the RFC either, but let's cap the number of routers, routes, addresses, and redirects we
* accept per link, for safety. */
#define NDISC_ROUTER_MAX 64U
#define NDISC_ROUTE_MAX 512U
#define NDISC_ADDRESS_MAX 64U
#define NDISC_REDIRECT_MAX 64U
static int ndisc_drop_outdated(Link *link, const struct in6_addr *router, usec_t timestamp_usec);
char* ndisc_dnssl_domain(const NDiscDNSSL *n) {
return ((char*) n) + ALIGN(sizeof(NDiscDNSSL));
}
bool link_ndisc_enabled(Link *link) {
assert(link);
if (!socket_ipv6_is_supported())
return false;
if (link->flags & IFF_LOOPBACK)
return false;
if (link->iftype == ARPHRD_CAN)
return false;
if (!link->network)
return false;
if (!link_multicast_enabled(link))
return false;
if (!link_ipv6ll_enabled_harder(link))
return false;
/* Honor explicitly specified value. */
if (link->network->ndisc >= 0)
return link->network->ndisc;
/* Disable if RADV is enabled. */
if (link_radv_enabled(link))
return false;
/* Accept RAs if IPv6 forwarding is disabled, and ignore RAs if IPv6 forwarding is enabled. */
int t = link_get_ip_forwarding(link, AF_INET6);
if (t >= 0)
return !t;
/* Otherwise, defaults to true. */
return true;
}
void network_adjust_ndisc(Network *network) {
assert(network);
if (!FLAGS_SET(network->link_local, ADDRESS_FAMILY_IPV6) &&
!network_has_static_ipv6ll_address(network)) {
if (network->ndisc > 0)
log_warning("%s: IPv6AcceptRA= is enabled but IPv6 link-local addressing is disabled or not supported. "
"Disabling IPv6AcceptRA=.", network->filename);
network->ndisc = false;
}
/* When RouterAllowList=, PrefixAllowList= or RouteAllowList= are specified, then
* RouterDenyList=, PrefixDenyList= or RouteDenyList= are ignored, respectively. */
if (!set_isempty(network->ndisc_allow_listed_router))
network->ndisc_deny_listed_router = set_free(network->ndisc_deny_listed_router);
if (!set_isempty(network->ndisc_allow_listed_prefix))
network->ndisc_deny_listed_prefix = set_free(network->ndisc_deny_listed_prefix);
if (!set_isempty(network->ndisc_allow_listed_route_prefix))
network->ndisc_deny_listed_route_prefix = set_free(network->ndisc_deny_listed_route_prefix);
}
static int ndisc_check_ready(Link *link);
static int ndisc_address_ready_callback(Address *address) {
Address *a;
assert(address);
assert(address->link);
SET_FOREACH(a, address->link->addresses)
if (a->source == NETWORK_CONFIG_SOURCE_NDISC)
a->callback = NULL;
return ndisc_check_ready(address->link);
}
static int ndisc_check_ready(Link *link) {
bool found = false, ready = false;
Address *address;
assert(link);
if (link->ndisc_messages > 0) {
log_link_debug(link, "%s(): SLAAC addresses and routes are not set.", __func__);
return 0;
}
SET_FOREACH(address, link->addresses) {
if (address->source != NETWORK_CONFIG_SOURCE_NDISC)
continue;
found = true;
if (address_is_ready(address)) {
ready = true;
break;
}
}
if (found && !ready) {
SET_FOREACH(address, link->addresses)
if (address->source == NETWORK_CONFIG_SOURCE_NDISC)
address->callback = ndisc_address_ready_callback;
log_link_debug(link, "%s(): no SLAAC address is ready.", __func__);
return 0;
}
link->ndisc_configured = true;
log_link_debug(link, "SLAAC addresses and routes set.");
link_check_ready(link);
return 0;
}
static int ndisc_remove_unused_nexthop(Link *link, NextHop *nexthop) {
int r;
assert(link);
assert(link->manager);
assert(link->ifindex > 0);
assert(nexthop);
if (nexthop->source != NETWORK_CONFIG_SOURCE_NDISC)
return 0;
if (nexthop->ifindex != link->ifindex)
return 0;
Route *route;
SET_FOREACH(route, nexthop->routes)
if (route_exists(route) || route_is_requesting(route))
return 0;
Request *req;
ORDERED_SET_FOREACH(req, link->manager->request_queue) {
if (req->type != REQUEST_TYPE_ROUTE)
continue;
route = ASSERT_PTR(req->userdata);
if (route->nexthop_id == nexthop->id)
return 0;
}
r = nexthop_remove_and_cancel(nexthop, link->manager);
if (r < 0)
return log_link_debug_errno(link, r, "Failed to remove unused nexthop: %m");
return 0;
}
static int ndisc_remove_unused_nexthop_by_id(Link *link, uint32_t id) {
assert(link);
assert(link->manager);
if (id == 0)
return 0;
/* The nexthop is only added to manager->nexthops_by_id once the kernel acknowledges it.
* A just-requested nexthop (e.g. via ndisc_set_route_nexthop(request=true), right before abandoning
* its route) exists only in the request queue, so handle that case too. */
NextHop *nexthop;
if (nexthop_get_by_id(link->manager, id, &nexthop) >= 0)
return ndisc_remove_unused_nexthop(link, nexthop);
Request *req;
if (nexthop_get_request_by_id(link->manager, id, &req) >= 0)
return ndisc_remove_unused_nexthop(link, ASSERT_PTR(req->userdata));
return 0;
}
static int ndisc_remove_unused_nexthops(Link *link) {
int ret = 0;
assert(link);
assert(link->manager);
NextHop *nexthop;
HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id)
RET_GATHER(ret, ndisc_remove_unused_nexthop(link, nexthop));
return ret;
}
#define NDISC_NEXTHOP_APP_ID SD_ID128_MAKE(76,d2,0f,1f,76,1e,44,d1,97,3a,52,5c,05,68,b5,0d)
static uint32_t ndisc_generate_nexthop_id(const NextHop *nexthop, Link *link, sd_id128_t app_id, uint64_t trial) {
assert(nexthop);
assert(link);
struct siphash state;
siphash24_init(&state, app_id.bytes);
siphash24_compress_typesafe(nexthop->protocol, &state);
siphash24_compress_string(link->ifname, &state);
siphash24_compress_typesafe(nexthop->gw.address.in6, &state);
siphash24_compress_typesafe(nexthop->provider.in6, &state);
uint64_t n = htole64(trial);
siphash24_compress_typesafe(n, &state);
uint64_t result = htole64(siphash24_finalize(&state));
return (uint32_t) ((result & 0xffffffff) ^ (result >> 32));
}
static bool ndisc_nexthop_equal(const NextHop *a, const NextHop *b) {
assert(a);
assert(b);
if (a->source != b->source)
return false;
if (a->protocol != b->protocol)
return false;
if (a->ifindex != b->ifindex)
return false;
if (!in6_addr_equal(&a->provider.in6, &b->provider.in6))
return false;
if (!in6_addr_equal(&a->gw.address.in6, &b->gw.address.in6))
return false;
return true;
}
static bool ndisc_take_nexthop_id(NextHop *nexthop, const NextHop *existing, Manager *manager) {
assert(nexthop);
assert(nexthop->id == 0);
assert(existing);
assert(existing->id > 0);
assert(manager);
if (!ndisc_nexthop_equal(nexthop, existing))
return false;
log_nexthop_debug(existing, "Found matching", manager);
nexthop->id = existing->id;
return true;
}
static int ndisc_nexthop_find_id(NextHop *nexthop, Link *link) {
NextHop *n;
Request *req;
int r;
assert(nexthop);
assert(link);
assert(link->manager);
sd_id128_t app_id;
r = sd_id128_get_machine_app_specific(NDISC_NEXTHOP_APP_ID, &app_id);
if (r < 0)
return r;
uint32_t id = ndisc_generate_nexthop_id(nexthop, link, app_id, 0);
if (nexthop_get_by_id(link->manager, id, &n) >= 0 &&
ndisc_take_nexthop_id(nexthop, n, link->manager))
return true;
if (nexthop_get_request_by_id(link->manager, id, &req) >= 0 &&
ndisc_take_nexthop_id(nexthop, req->userdata, link->manager))
return true;
HASHMAP_FOREACH(n, link->manager->nexthops_by_id)
if (ndisc_take_nexthop_id(nexthop, n, link->manager))
return true;
ORDERED_SET_FOREACH(req, link->manager->request_queue) {
if (req->type != REQUEST_TYPE_NEXTHOP)
continue;
if (ndisc_take_nexthop_id(nexthop, req->userdata, link->manager))
return true;
}
return false;
}
static int ndisc_nexthop_new(const Route *route, Link *link, NextHop **ret) {
_cleanup_(nexthop_unrefp) NextHop *nexthop = NULL;
int r;
assert(route);
assert(link);
assert(ret);
r = nexthop_new(&nexthop);
if (r < 0)
return r;
nexthop->source = NETWORK_CONFIG_SOURCE_NDISC;
nexthop->provider = route->provider;
nexthop->protocol = route->protocol == RTPROT_REDIRECT ? RTPROT_REDIRECT : RTPROT_RA;
nexthop->family = AF_INET6;
nexthop->gw.address = route->nexthop.gw;
nexthop->ifindex = link->ifindex;
r = ndisc_nexthop_find_id(nexthop, link);
if (r < 0)
return r;
*ret = TAKE_PTR(nexthop);
return 0;
}
static int ndisc_nexthop_acquire_id(NextHop *nexthop, Link *link) {
int r;
assert(nexthop);
assert(nexthop->id == 0);
assert(link);
assert(link->manager);
sd_id128_t app_id;
r = sd_id128_get_machine_app_specific(NDISC_NEXTHOP_APP_ID, &app_id);
if (r < 0)
return r;
for (uint64_t trial = 0; trial < 100; trial++) {
uint32_t id = ndisc_generate_nexthop_id(nexthop, link, app_id, trial);
if (id == 0)
continue;
if (set_contains(link->manager->nexthop_ids, UINT32_TO_PTR(id)))
continue; /* The ID is already used in a .network file. */
if (nexthop_get_by_id(link->manager, id, NULL) >= 0)
continue; /* The ID is already used by an existing nexthop. */
if (nexthop_get_request_by_id(link->manager, id, NULL) >= 0)
continue; /* The ID is already used by a nexthop being requested. */
log_link_debug(link, "Generated new ndisc nexthop ID for %s with trial %"PRIu64": %"PRIu32,
IN6_ADDR_TO_STRING(&nexthop->gw.address.in6), trial, id);
nexthop->id = id;
return 0;
}
return log_link_debug_errno(link, SYNTHETIC_ERRNO(EBUSY), "Cannot find free nexthop ID for %s.",
IN6_ADDR_TO_STRING(&nexthop->gw.address.in6));
}
static int ndisc_nexthop_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, NextHop *nexthop) {
int r;
assert(link);
assert(nexthop);
r = nexthop_configure_handler_internal(m, link, nexthop);
if (r <= 0)
return r;
r = ndisc_check_ready(link);
if (r < 0)
link_enter_failed(link);
return 1;
}
static int ndisc_request_nexthop(NextHop *nexthop, Link *link) {
int r;
assert(nexthop);
assert(link);
if (nexthop->id > 0)
return 0;
r = ndisc_nexthop_acquire_id(nexthop, link);
if (r < 0)
return r;
r = link_request_nexthop(link, nexthop, &link->ndisc_messages, ndisc_nexthop_handler);
if (r < 0)
return r;
if (r > 0)
link->ndisc_configured = false;
return 0;
}
static int ndisc_set_route_nexthop(Route *route, Link *link, bool request) {
_cleanup_(nexthop_unrefp) NextHop *nexthop = NULL;
int r;
assert(route);
assert(link);
assert(link->manager);
if (!link->manager->manage_foreign_nexthops)
goto finalize;
if (route->nexthop.family != AF_INET6 || in6_addr_is_null(&route->nexthop.gw.in6))
goto finalize;
r = ndisc_nexthop_new(route, link, &nexthop);
if (r < 0)
return r;
if (nexthop->id == 0 && !request)
goto finalize;
r = ndisc_request_nexthop(nexthop, link);
if (r < 0)
return r;
route->nexthop = (RouteNextHop) {};
route->nexthop_id = nexthop->id;
finalize:
return route_adjust_nexthops(route, link);
}
static int ndisc_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) {
int r;
assert(req);
assert(link);
assert(route);
r = route_configure_handler_internal(m, req, route);
if (r <= 0)
return r;
r = ndisc_check_ready(link);
if (r < 0)
link_enter_failed(link);
return 1;
}
static void ndisc_set_route_priority(Link *link, Route *route) {
assert(link);
assert(route);
if (route->priority_set)
return; /* explicitly configured. */
switch (route->pref) {
case SD_NDISC_PREFERENCE_LOW:
route->priority = link->network->ndisc_route_metric_low;
break;
case SD_NDISC_PREFERENCE_MEDIUM:
route->priority = link->network->ndisc_route_metric_medium;
break;
case SD_NDISC_PREFERENCE_HIGH:
route->priority = link->network->ndisc_route_metric_high;
break;
default:
assert_not_reached();
}
}
static size_t ndisc_route_count(Link *link) {
Route *route;
Request *req;
size_t n = 0;
assert(link);
assert(link->manager);
SET_FOREACH(route, link->manager->routes) {
if (route->source != NETWORK_CONFIG_SOURCE_NDISC)
continue;
if (!route_is_bound_to_link(route, link))
continue;
/* Skip routes pending removal (e.g. just expired by ndisc_drop_outdated() in this same RA):
* they linger in manager->routes until the kernel acknowledges the removal. */
if (!route_exists(route))
continue;
n++;
}
/* Also count already-requested but not-yet-installed routes, otherwise all RIOs of a single RA (and
* RAs arriving before the kernel acknowledges earlier requests) would be admitted past the limit. */
ORDERED_SET_FOREACH(req, link->manager->request_queue) {
Route *existing;
if (req->type != REQUEST_TYPE_ROUTE || req->link != link)
continue;
route = ASSERT_PTR(req->userdata);
if (route->source != NETWORK_CONFIG_SOURCE_NDISC)
continue;
/* Skip the route if it's already installed and live, so it's counted above, but it's also
* being requested again (e.g. on RA refresh). */
if (route_get(link->manager, route, &existing) >= 0 && route_exists(existing))
continue;
n++;
}
return n;
}
static size_t ndisc_address_count(Link *link) {
Address *address;
Request *req;
size_t n = 0;
assert(link);
assert(link->manager);
SET_FOREACH(address, link->addresses) {
if (address->source != NETWORK_CONFIG_SOURCE_NDISC)
continue;
/* Skip addresses pending removal. */
if (!address_exists(address))
continue;
n++;
}
/* Also count already-requested but not-yet-installed addresses. */
ORDERED_SET_FOREACH(req, link->manager->request_queue) {
Address *existing;
if (req->type != REQUEST_TYPE_ADDRESS || req->link != link)
continue;
address = ASSERT_PTR(req->userdata);
if (address->source != NETWORK_CONFIG_SOURCE_NDISC)
continue;
/* Skip the address if it's already installed and live, so it's counted above, but it's also
* being requested again (e.g. on RA refresh). */
if (address_get(link, address, &existing) >= 0 && address_exists(existing))
continue;
n++;
}
return n;
}
/* Returns 1 if the route was requested (or already present), 0 if it was refused (e.g. NDISC_ROUTE_MAX
* reached or a conflicting static route exists), and a negative errno on failure. */
static int ndisc_request_route(Route *route, Link *link) {
uint8_t pref, pref_original;
bool is_new;
int r;
assert(route);
assert(link);
assert(link->manager);
assert(link->network);
r = route_metric_set(&route->metric, RTAX_QUICKACK, link->network->ndisc_quickack);
if (r < 0)
return r;
r = ndisc_set_route_nexthop(route, link, /* request= */ true);
if (r < 0)
return r;
/* Reject new routes once the per-link limit is reached, *before* the conflict-resolution loop below
* calls route_remove_and_cancel() - otherwise a refused route would still tear down an existing one.
* Refreshes (the route is already installed or requested) are always allowed. The priority is set
* first so that the existence checks use the route's final identity. */
ndisc_set_route_priority(link, route);
if (route_get(link->manager, route, NULL) < 0 &&
route_get_request(link->manager, route, NULL) < 0 &&
ndisc_route_count(link) >= NDISC_ROUTE_MAX) {
log_link_debug(link, "Too many NDisc routes per link (%u), ignoring route %s.",
NDISC_ROUTE_MAX, IN6_ADDR_PREFIX_TO_STRING(&route->dst.in6, route->dst_prefixlen));
goto refuse;
}
pref_original = route->pref;
FOREACH_ARGUMENT(pref, SD_NDISC_PREFERENCE_LOW, SD_NDISC_PREFERENCE_MEDIUM, SD_NDISC_PREFERENCE_HIGH) {
Route *existing;
Request *req;
/* If the preference is specified by the user config (that is, for semi-static routes),
* rather than RA, then only search conflicting routes that have the same preference. */
if (route->pref_set && pref != pref_original)
continue;
route->pref = pref;
ndisc_set_route_priority(link, route);
/* Note, here do not call route_remove_and_cancel() with 'route' directly, otherwise
* existing route(s) may be removed needlessly. */
/* First, check if a conflicting route is already requested. If there is an existing route,
* and also an existing pending request, then the source may be updated by the request. So,
* we first need to check the source of the requested route. */
if (route_get_request(link->manager, route, &req) >= 0) {
route->pref = pref_original;
ndisc_set_route_priority(link, route);
existing = ASSERT_PTR(req->userdata);
if (!route_can_update(link->manager, existing, route)) {
if (existing->source == NETWORK_CONFIG_SOURCE_STATIC) {
log_link_debug(link, "Found a pending route request that conflicts with new request based on a received RA, ignoring request.");
goto refuse;
}
log_link_debug(link, "Found a pending route request that conflicts with new request based on a received RA, cancelling.");
r = route_remove_and_cancel(existing, link->manager);
if (r < 0)
return r;
}
}
route->pref = pref;
ndisc_set_route_priority(link, route);
/* Then, check if a conflicting route exists. */
if (route_get(link->manager, route, &existing) >= 0) {
route->pref = pref_original;
ndisc_set_route_priority(link, route);
if (!route_can_update(link->manager, existing, route)) {
if (existing->source == NETWORK_CONFIG_SOURCE_STATIC) {
log_link_debug(link, "Found an existing route that conflicts with new route based on a received RA, ignoring request.");
goto refuse;
}
log_link_debug(link, "Found an existing route that conflicts with new route based on a received RA, removing.");
r = route_remove_and_cancel(existing, link->manager);
if (r < 0)
return r;
}
}
}
/* The preference (and priority) may be changed in the above loop. Restore it. */
route->pref = pref_original;
ndisc_set_route_priority(link, route);
is_new = route_get(link->manager, route, NULL) < 0;
r = link_request_route(link, route, &link->ndisc_messages, ndisc_route_handler);
if (r < 0)
return r;
if (r > 0 && is_new)
link->ndisc_configured = false;
return 1;
refuse:
/* We may have requested a nexthop for this route above via ndisc_set_route_nexthop(request=true). As we
* are not going to install the route, drop that nexthop again if it ended up unreferenced, so that a
* refused route does not leave an orphaned nexthop in the FIB until the next periodic cleanup. This is
* a no-op if no nexthop was requested (e.g. ManageForeignNextHops=no, or an on-link route), or if the
* nexthop is still shared with another route. */
(void) ndisc_remove_unused_nexthop_by_id(link, route->nexthop_id);
return 0;
}
static void ndisc_route_prepare(Route *route, Link *link) {
assert(route);
assert(link);
route->source = NETWORK_CONFIG_SOURCE_NDISC;
if (!route->table_set)
route->table = link_get_ndisc_route_table(link);
}
static int ndisc_router_route_prepare(Route *route, Link *link, sd_ndisc_router *rt) {
assert(route);
assert(link);
assert(rt);
ndisc_route_prepare(route, link);
if (!route->protocol_set)
route->protocol = RTPROT_RA;
return sd_ndisc_router_get_sender_address(rt, &route->provider.in6);
}
static int ndisc_request_router_route(Route *route, Link *link, sd_ndisc_router *rt) {
int r;
assert(route);
assert(link);
assert(rt);
r = ndisc_router_route_prepare(route, link, rt);
if (r < 0)
return r;
return ndisc_request_route(route, link);
}
static int ndisc_remove_route(Route *route, Link *link) {
int r, ret = 0;
assert(route);
assert(link);
assert(link->manager);
r = ndisc_set_route_nexthop(route, link, /* request= */ false);
if (r < 0)
return r;
uint8_t pref, pref_original = route->pref;
FOREACH_ARGUMENT(pref, SD_NDISC_PREFERENCE_LOW, SD_NDISC_PREFERENCE_MEDIUM, SD_NDISC_PREFERENCE_HIGH) {
Route *existing;
Request *req;
/* If the preference is specified by the user config (that is, for semi-static routes),
* rather than RA, then only search conflicting routes that have the same preference. */
if (route->pref_set && pref != pref_original)
continue;
route->pref = pref;
ndisc_set_route_priority(link, route);
/* Unfortunately, we cannot directly pass 'route' to route_remove_and_cancel() here, as the
* same or similar route may be configured or requested statically. */
/* First, check if the route is already requested. If there is an existing route, and also an
* existing pending request, then the source may be updated by the request. So, we first need
* to check the source of the requested route. */
if (route_get_request(link->manager, route, &req) >= 0) {
existing = ASSERT_PTR(req->userdata);
if (existing->source == NETWORK_CONFIG_SOURCE_STATIC)
continue;
RET_GATHER(ret, route_remove_and_cancel(existing, link->manager));
}
/* Then, check if the route exists. */
if (route_get(link->manager, route, &existing) >= 0) {
if (existing->source == NETWORK_CONFIG_SOURCE_STATIC)
continue;
RET_GATHER(ret, route_remove_and_cancel(existing, link->manager));
}
}
return RET_GATHER(ret, ndisc_remove_unused_nexthop_by_id(link, route->nexthop_id));
}
static int ndisc_remove_router_route(Route *route, Link *link, sd_ndisc_router *rt) {
int r;
assert(route);
assert(link);
assert(rt);
r = ndisc_router_route_prepare(route, link, rt);
if (r < 0)
return r;
return ndisc_remove_route(route, link);
}
static int ndisc_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) {
int r;
assert(link);
assert(address);
r = address_configure_handler_internal(m, link, address);
if (r <= 0)
return r;
r = ndisc_check_ready(link);
if (r < 0)
link_enter_failed(link);
return 1;
}
static int ndisc_request_address(Address *address, Link *link) {
Address *existing;
bool is_new, remove_dhcp6 = false;
int r;
assert(address);
assert(link);
address->source = NETWORK_CONFIG_SOURCE_NDISC;
r = free_and_strdup_warn(&address->netlabel, link->network->ndisc_netlabel);
if (r < 0)
return r;
if (address_get_harder(link, address, &existing) < 0)
is_new = true;
else if (address_can_update(existing, address))
is_new = false;
else if (existing->source == NETWORK_CONFIG_SOURCE_DHCP6) {
/* SLAAC address is preferred over DHCPv6 address; the latter is removed below. */
is_new = true;
remove_dhcp6 = true;
} else {
/* Conflicting static address is configured?? */
log_link_debug(link, "Conflicting address %s exists, ignoring request.",
IN_ADDR_PREFIX_TO_STRING(existing->family, &existing->in_addr, existing->prefixlen));
return 0;
}
/* Check the limit before removing any conflicting DHCPv6 address, so that hitting the cap does not
* leave the link with neither the DHCPv6 nor the SLAAC address. */
if (is_new && ndisc_address_count(link) >= NDISC_ADDRESS_MAX) {
log_link_debug(link, "Too many NDisc addresses per link (%u), ignoring address %s.",
NDISC_ADDRESS_MAX, IN6_ADDR_TO_STRING(&address->in_addr.in6));
return 0;
}
if (remove_dhcp6) {
log_link_debug(link, "Conflicting DHCPv6 address %s exists, removing.",
IN_ADDR_PREFIX_TO_STRING(existing->family, &existing->in_addr, existing->prefixlen));
r = address_remove(existing, link);
if (r < 0)
return r;
}
r = link_request_address(link, address, &link->ndisc_messages,
ndisc_address_handler, NULL);
if (r < 0)
return r;
if (r > 0 && is_new)
link->ndisc_configured = false;
return 0;
}
int ndisc_reconfigure_address(Address *address, Link *link) {
int r;
assert(address);
assert(address->source == NETWORK_CONFIG_SOURCE_NDISC);
assert(link);
r = regenerate_address(address, link);
if (r <= 0)
return r;
r = ndisc_request_address(address, link);
if (r < 0)
return r;
if (!link->ndisc_configured)
link_set_state(link, LINK_STATE_CONFIGURING);
link_check_ready(link);
return 0;
}
static int ndisc_redirect_route_new(sd_ndisc_redirect *rd, Route **ret) {
_cleanup_(route_unrefp) Route *route = NULL;
struct in6_addr gateway, destination;
int r;
assert(rd);
assert(ret);
r = sd_ndisc_redirect_get_target_address(rd, &gateway);
if (r < 0)
return r;
r = sd_ndisc_redirect_get_destination_address(rd, &destination);
if (r < 0)
return r;
r = route_new(&route);
if (r < 0)
return r;
route->family = AF_INET6;
if (!in6_addr_equal(&gateway, &destination)) {
route->nexthop.gw.in6 = gateway;
route->nexthop.family = AF_INET6;
}
route->dst.in6 = destination;
route->dst_prefixlen = 128;
route->protocol = RTPROT_REDIRECT;
r = sd_ndisc_redirect_get_sender_address(rd, &route->provider.in6);
if (r < 0)
return r;
*ret = TAKE_PTR(route);
return 0;
}
static int ndisc_remove_redirect_route(Link *link, sd_ndisc_redirect *rd) {
_cleanup_(route_unrefp) Route *route = NULL;
int r;
assert(link);
assert(rd);
r = ndisc_redirect_route_new(rd, &route);
if (r < 0)
return r;
ndisc_route_prepare(route, link);
return ndisc_remove_route(route, link);
}
static void ndisc_redirect_hash_func(const sd_ndisc_redirect *x, struct siphash *state) {
struct in6_addr dest = {};
assert(x);
assert(state);
(void) sd_ndisc_redirect_get_destination_address((sd_ndisc_redirect*) x, &dest);
siphash24_compress_typesafe(dest, state);
}
static int ndisc_redirect_compare_func(const sd_ndisc_redirect *x, const sd_ndisc_redirect *y) {
struct in6_addr dest_x = {}, dest_y = {};
assert(x);
assert(y);
(void) sd_ndisc_redirect_get_destination_address((sd_ndisc_redirect*) x, &dest_x);
(void) sd_ndisc_redirect_get_destination_address((sd_ndisc_redirect*) y, &dest_y);
return memcmp(&dest_x, &dest_y, sizeof(dest_x));
}
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
ndisc_redirect_hash_ops,
sd_ndisc_redirect,
ndisc_redirect_hash_func,
ndisc_redirect_compare_func,
sd_ndisc_redirect_unref);
static int ndisc_redirect_equal(sd_ndisc_redirect *x, sd_ndisc_redirect *y) {
struct in6_addr a, b;
int r;
assert(x);
assert(y);
r = sd_ndisc_redirect_get_destination_address(x, &a);
if (r < 0)
return r;
r = sd_ndisc_redirect_get_destination_address(y, &b);
if (r < 0)
return r;
if (!in6_addr_equal(&a, &b))
return false;