| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/*** |
| 3 |
Copyright © 2014 Intel Corporation. All rights reserved. |
| 4 |
***/ |
| 5 |
|
| 6 |
#include <linux/if_arp.h> |
| 7 |
#include <linux/rtnetlink.h> |
| 8 |
#include <netinet/icmp6.h> |
| 9 |
|
| 10 |
#include "sd-ndisc.h" |
| 11 |
|
| 12 |
#include "conf-parser.h" |
| 13 |
#include "errno-util.h" |
| 14 |
#include "event-util.h" |
| 15 |
#include "missing-network.h" |
| 16 |
#include "ndisc-router-internal.h" |
| 17 |
#include "networkd-address.h" |
| 18 |
#include "networkd-address-generation.h" |
| 19 |
#include "networkd-dhcp6.h" |
| 20 |
#include "networkd-ipv6ll.h" |
| 21 |
#include "networkd-link.h" |
| 22 |
#include "networkd-manager.h" |
| 23 |
#include "networkd-ndisc.h" |
| 24 |
#include "networkd-nexthop.h" |
| 25 |
#include "networkd-queue.h" |
| 26 |
#include "networkd-route.h" |
| 27 |
#include "networkd-state-file.h" |
| 28 |
#include "networkd-sysctl.h" |
| 29 |
#include "ordered-set.h" |
| 30 |
#include "set.h" |
| 31 |
#include "siphash24.h" |
| 32 |
#include "socket-util.h" |
| 33 |
#include "string-table.h" |
| 34 |
#include "string-util.h" |
| 35 |
#include "strv.h" |
| 36 |
#include "sysctl-util.h" |
| 37 |
|
| 38 |
#define NDISC_DNSSL_MAX 64U |
| 39 |
#define NDISC_RDNSS_MAX 64U |
| 40 |
#define NDISC_ENCRYPTED_DNS_MAX 64U |
| 41 |
/* Not defined in the RFC, but let's set an upper limit to make not consume much memory. |
| 42 |
* This should be safe as typically there should be at most 1 portal per network. */ |
| 43 |
#define NDISC_CAPTIVE_PORTAL_MAX 64U |
| 44 |
/* Neither defined in the RFC. Just for safety. Otherwise, malformed messages can make clients trigger OOM. |
| 45 |
* Not sure if the threshold is high enough. Let's adjust later if not. */ |
| 46 |
#define NDISC_PREF64_MAX 64U |
| 47 |
|
| 48 |
static int ndisc_drop_outdated(Link *link, const struct in6_addr *router, usec_t timestamp_usec); |
| 49 |
|
| 50 |
char* ndisc_dnssl_domain(const NDiscDNSSL *n) { |
| 51 |
return ((char*) n) + ALIGN(sizeof(NDiscDNSSL)); |
| 52 |
} |
| 53 |
|
| 54 |
bool link_ndisc_enabled(Link *link) { |
| 55 |
assert(link); |
| 56 |
|
| 57 |
if (!socket_ipv6_is_supported()) |
| 58 |
return false; |
| 59 |
|
| 60 |
if (link->flags & IFF_LOOPBACK) |
| 61 |
return false; |
| 62 |
|
| 63 |
if (link->iftype == ARPHRD_CAN) |
| 64 |
return false; |
| 65 |
|
| 66 |
if (!link->network) |
| 67 |
return false; |
| 68 |
|
| 69 |
if (!link_multicast_enabled(link)) |
| 70 |
return false; |
| 71 |
|
| 72 |
if (!link_ipv6ll_enabled_harder(link)) |
| 73 |
return false; |
| 74 |
|
| 75 |
/* Honor explicitly specified value. */ |
| 76 |
if (link->network->ndisc >= 0) |
| 77 |
return link->network->ndisc; |
| 78 |
|
| 79 |
/* Disable if RADV is enabled. */ |
| 80 |
if (link_radv_enabled(link)) |
| 81 |
return false; |
| 82 |
|
| 83 |
/* Accept RAs if IPv6 forwarding is disabled, and ignore RAs if IPv6 forwarding is enabled. */ |
| 84 |
int t = link_get_ip_forwarding(link, AF_INET6); |
| 85 |
if (t >= 0) |
| 86 |
return !t; |
| 87 |
|
| 88 |
/* Otherwise, defaults to true. */ |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
void network_adjust_ndisc(Network *network) { |
| 93 |
assert(network); |
| 94 |
|
| 95 |
if (!FLAGS_SET(network->link_local, ADDRESS_FAMILY_IPV6) && |
| 96 |
!network_has_static_ipv6ll_address(network)) { |
| 97 |
if (network->ndisc > 0) |
| 98 |
log_warning("%s: IPv6AcceptRA= is enabled but IPv6 link-local addressing is disabled or not supported. " |
| 99 |
"Disabling IPv6AcceptRA=.", network->filename); |
| 100 |
network->ndisc = false; |
| 101 |
} |
| 102 |
|
| 103 |
/* When RouterAllowList=, PrefixAllowList= or RouteAllowList= are specified, then |
| 104 |
* RouterDenyList=, PrefixDenyList= or RouteDenyList= are ignored, respectively. */ |
| 105 |
if (!set_isempty(network->ndisc_allow_listed_router)) |
| 106 |
network->ndisc_deny_listed_router = set_free(network->ndisc_deny_listed_router); |
| 107 |
if (!set_isempty(network->ndisc_allow_listed_prefix)) |
| 108 |
network->ndisc_deny_listed_prefix = set_free(network->ndisc_deny_listed_prefix); |
| 109 |
if (!set_isempty(network->ndisc_allow_listed_route_prefix)) |
| 110 |
network->ndisc_deny_listed_route_prefix = set_free(network->ndisc_deny_listed_route_prefix); |
| 111 |
} |
| 112 |
|
| 113 |
static int ndisc_check_ready(Link *link); |
| 114 |
|
| 115 |
static int ndisc_address_ready_callback(Address *address) { |
| 116 |
Address *a; |
| 117 |
|
| 118 |
assert(address); |
| 119 |
assert(address->link); |
| 120 |
|
| 121 |
SET_FOREACH(a, address->link->addresses) |
| 122 |
if (a->source == NETWORK_CONFIG_SOURCE_NDISC) |
| 123 |
a->callback = NULL; |
| 124 |
|
| 125 |
return ndisc_check_ready(address->link); |
| 126 |
} |
| 127 |
|
| 128 |
static int ndisc_check_ready(Link *link) { |
| 129 |
bool found = false, ready = false; |
| 130 |
Address *address; |
| 131 |
|
| 132 |
assert(link); |
| 133 |
|
| 134 |
if (link->ndisc_messages > 0) { |
| 135 |
log_link_debug(link, "%s(): SLAAC addresses and routes are not set.", __func__); |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
SET_FOREACH(address, link->addresses) { |
| 140 |
if (address->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 141 |
continue; |
| 142 |
|
| 143 |
found = true; |
| 144 |
|
| 145 |
if (address_is_ready(address)) { |
| 146 |
ready = true; |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if (found && !ready) { |
| 152 |
SET_FOREACH(address, link->addresses) |
| 153 |
if (address->source == NETWORK_CONFIG_SOURCE_NDISC) |
| 154 |
address->callback = ndisc_address_ready_callback; |
| 155 |
|
| 156 |
log_link_debug(link, "%s(): no SLAAC address is ready.", __func__); |
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
link->ndisc_configured = true; |
| 161 |
log_link_debug(link, "SLAAC addresses and routes set."); |
| 162 |
|
| 163 |
link_check_ready(link); |
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
static int ndisc_remove_unused_nexthop(Link *link, NextHop *nexthop) { |
| 168 |
int r; |
| 169 |
|
| 170 |
assert(link); |
| 171 |
assert(link->manager); |
| 172 |
assert(link->ifindex > 0); |
| 173 |
assert(nexthop); |
| 174 |
|
| 175 |
if (nexthop->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 176 |
return 0; |
| 177 |
|
| 178 |
if (nexthop->ifindex != link->ifindex) |
| 179 |
return 0; |
| 180 |
|
| 181 |
Route *route; |
| 182 |
SET_FOREACH(route, nexthop->routes) |
| 183 |
if (route_exists(route) || route_is_requesting(route)) |
| 184 |
return 0; |
| 185 |
|
| 186 |
Request *req; |
| 187 |
ORDERED_SET_FOREACH(req, link->manager->request_queue) { |
| 188 |
if (req->type != REQUEST_TYPE_ROUTE) |
| 189 |
continue; |
| 190 |
|
| 191 |
route = ASSERT_PTR(req->userdata); |
| 192 |
if (route->nexthop_id == nexthop->id) |
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
r = nexthop_remove_and_cancel(nexthop, link->manager); |
| 197 |
if (r < 0) |
| 198 |
return log_link_debug_errno(link, r, "Failed to remove unused nexthop: %m"); |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
static int ndisc_remove_unused_nexthop_by_id(Link *link, uint32_t id) { |
| 204 |
assert(link); |
| 205 |
assert(link->manager); |
| 206 |
|
| 207 |
if (id == 0) |
| 208 |
return 0; |
| 209 |
|
| 210 |
NextHop *nexthop; |
| 211 |
if (nexthop_get_by_id(link->manager, id, &nexthop) < 0) |
| 212 |
return 0; |
| 213 |
|
| 214 |
return ndisc_remove_unused_nexthop(link, nexthop); |
| 215 |
} |
| 216 |
|
| 217 |
static int ndisc_remove_unused_nexthops(Link *link) { |
| 218 |
int ret = 0; |
| 219 |
|
| 220 |
assert(link); |
| 221 |
assert(link->manager); |
| 222 |
|
| 223 |
NextHop *nexthop; |
| 224 |
HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) |
| 225 |
RET_GATHER(ret, ndisc_remove_unused_nexthop(link, nexthop)); |
| 226 |
|
| 227 |
return ret; |
| 228 |
} |
| 229 |
|
| 230 |
#define NDISC_NEXTHOP_APP_ID SD_ID128_MAKE(76,d2,0f,1f,76,1e,44,d1,97,3a,52,5c,05,68,b5,0d) |
| 231 |
|
| 232 |
static uint32_t ndisc_generate_nexthop_id(const NextHop *nexthop, Link *link, sd_id128_t app_id, uint64_t trial) { |
| 233 |
assert(nexthop); |
| 234 |
assert(link); |
| 235 |
|
| 236 |
struct siphash state; |
| 237 |
siphash24_init(&state, app_id.bytes); |
| 238 |
siphash24_compress_typesafe(nexthop->protocol, &state); |
| 239 |
siphash24_compress_string(link->ifname, &state); |
| 240 |
siphash24_compress_typesafe(nexthop->gw.address.in6, &state); |
| 241 |
siphash24_compress_typesafe(nexthop->provider.in6, &state); |
| 242 |
uint64_t n = htole64(trial); |
| 243 |
siphash24_compress_typesafe(n, &state); |
| 244 |
|
| 245 |
uint64_t result = htole64(siphash24_finalize(&state)); |
| 246 |
return (uint32_t) ((result & 0xffffffff) ^ (result >> 32)); |
| 247 |
} |
| 248 |
|
| 249 |
static bool ndisc_nexthop_equal(const NextHop *a, const NextHop *b) { |
| 250 |
assert(a); |
| 251 |
assert(b); |
| 252 |
|
| 253 |
if (a->source != b->source) |
| 254 |
return false; |
| 255 |
if (a->protocol != b->protocol) |
| 256 |
return false; |
| 257 |
if (a->ifindex != b->ifindex) |
| 258 |
return false; |
| 259 |
if (!in6_addr_equal(&a->provider.in6, &b->provider.in6)) |
| 260 |
return false; |
| 261 |
if (!in6_addr_equal(&a->gw.address.in6, &b->gw.address.in6)) |
| 262 |
return false; |
| 263 |
|
| 264 |
return true; |
| 265 |
} |
| 266 |
|
| 267 |
static bool ndisc_take_nexthop_id(NextHop *nexthop, const NextHop *existing, Manager *manager) { |
| 268 |
assert(nexthop); |
| 269 |
assert(nexthop->id == 0); |
| 270 |
assert(existing); |
| 271 |
assert(existing->id > 0); |
| 272 |
assert(manager); |
| 273 |
|
| 274 |
if (!ndisc_nexthop_equal(nexthop, existing)) |
| 275 |
return false; |
| 276 |
|
| 277 |
log_nexthop_debug(existing, "Found matching", manager); |
| 278 |
nexthop->id = existing->id; |
| 279 |
return true; |
| 280 |
} |
| 281 |
|
| 282 |
static int ndisc_nexthop_find_id(NextHop *nexthop, Link *link) { |
| 283 |
NextHop *n; |
| 284 |
Request *req; |
| 285 |
int r; |
| 286 |
|
| 287 |
assert(nexthop); |
| 288 |
assert(link); |
| 289 |
assert(link->manager); |
| 290 |
|
| 291 |
sd_id128_t app_id; |
| 292 |
r = sd_id128_get_machine_app_specific(NDISC_NEXTHOP_APP_ID, &app_id); |
| 293 |
if (r < 0) |
| 294 |
return r; |
| 295 |
|
| 296 |
uint32_t id = ndisc_generate_nexthop_id(nexthop, link, app_id, 0); |
| 297 |
if (nexthop_get_by_id(link->manager, id, &n) >= 0 && |
| 298 |
ndisc_take_nexthop_id(nexthop, n, link->manager)) |
| 299 |
return true; |
| 300 |
if (nexthop_get_request_by_id(link->manager, id, &req) >= 0 && |
| 301 |
ndisc_take_nexthop_id(nexthop, req->userdata, link->manager)) |
| 302 |
return true; |
| 303 |
|
| 304 |
HASHMAP_FOREACH(n, link->manager->nexthops_by_id) |
| 305 |
if (ndisc_take_nexthop_id(nexthop, n, link->manager)) |
| 306 |
return true; |
| 307 |
|
| 308 |
ORDERED_SET_FOREACH(req, link->manager->request_queue) { |
| 309 |
if (req->type != REQUEST_TYPE_NEXTHOP) |
| 310 |
continue; |
| 311 |
|
| 312 |
if (ndisc_take_nexthop_id(nexthop, req->userdata, link->manager)) |
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
static int ndisc_nexthop_new(const Route *route, Link *link, NextHop **ret) { |
| 320 |
_cleanup_(nexthop_unrefp) NextHop *nexthop = NULL; |
| 321 |
int r; |
| 322 |
|
| 323 |
assert(route); |
| 324 |
assert(link); |
| 325 |
assert(ret); |
| 326 |
|
| 327 |
r = nexthop_new(&nexthop); |
| 328 |
if (r < 0) |
| 329 |
return r; |
| 330 |
|
| 331 |
nexthop->source = NETWORK_CONFIG_SOURCE_NDISC; |
| 332 |
nexthop->provider = route->provider; |
| 333 |
nexthop->protocol = route->protocol == RTPROT_REDIRECT ? RTPROT_REDIRECT : RTPROT_RA; |
| 334 |
nexthop->family = AF_INET6; |
| 335 |
nexthop->gw.address = route->nexthop.gw; |
| 336 |
nexthop->ifindex = link->ifindex; |
| 337 |
|
| 338 |
r = ndisc_nexthop_find_id(nexthop, link); |
| 339 |
if (r < 0) |
| 340 |
return r; |
| 341 |
|
| 342 |
*ret = TAKE_PTR(nexthop); |
| 343 |
return 0; |
| 344 |
} |
| 345 |
|
| 346 |
static int ndisc_nexthop_acquire_id(NextHop *nexthop, Link *link) { |
| 347 |
int r; |
| 348 |
|
| 349 |
assert(nexthop); |
| 350 |
assert(nexthop->id == 0); |
| 351 |
assert(link); |
| 352 |
assert(link->manager); |
| 353 |
|
| 354 |
sd_id128_t app_id; |
| 355 |
r = sd_id128_get_machine_app_specific(NDISC_NEXTHOP_APP_ID, &app_id); |
| 356 |
if (r < 0) |
| 357 |
return r; |
| 358 |
|
| 359 |
for (uint64_t trial = 0; trial < 100; trial++) { |
| 360 |
uint32_t id = ndisc_generate_nexthop_id(nexthop, link, app_id, trial); |
| 361 |
if (id == 0) |
| 362 |
continue; |
| 363 |
|
| 364 |
if (set_contains(link->manager->nexthop_ids, UINT32_TO_PTR(id))) |
| 365 |
continue; /* The ID is already used in a .network file. */ |
| 366 |
|
| 367 |
if (nexthop_get_by_id(link->manager, id, NULL) >= 0) |
| 368 |
continue; /* The ID is already used by an existing nexthop. */ |
| 369 |
|
| 370 |
if (nexthop_get_request_by_id(link->manager, id, NULL) >= 0) |
| 371 |
continue; /* The ID is already used by a nexthop being requested. */ |
| 372 |
|
| 373 |
log_link_debug(link, "Generated new ndisc nexthop ID for %s with trial %"PRIu64": %"PRIu32, |
| 374 |
IN6_ADDR_TO_STRING(&nexthop->gw.address.in6), trial, id); |
| 375 |
nexthop->id = id; |
| 376 |
return 0; |
| 377 |
} |
| 378 |
|
| 379 |
return log_link_debug_errno(link, SYNTHETIC_ERRNO(EBUSY), "Cannot find free nexthop ID for %s.", |
| 380 |
IN6_ADDR_TO_STRING(&nexthop->gw.address.in6)); |
| 381 |
} |
| 382 |
|
| 383 |
static int ndisc_nexthop_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, NextHop *nexthop) { |
| 384 |
int r; |
| 385 |
|
| 386 |
assert(link); |
| 387 |
assert(nexthop); |
| 388 |
|
| 389 |
r = nexthop_configure_handler_internal(m, link, nexthop); |
| 390 |
if (r <= 0) |
| 391 |
return r; |
| 392 |
|
| 393 |
r = ndisc_check_ready(link); |
| 394 |
if (r < 0) |
| 395 |
link_enter_failed(link); |
| 396 |
|
| 397 |
return 1; |
| 398 |
} |
| 399 |
|
| 400 |
static int ndisc_request_nexthop(NextHop *nexthop, Link *link) { |
| 401 |
int r; |
| 402 |
|
| 403 |
assert(nexthop); |
| 404 |
assert(link); |
| 405 |
|
| 406 |
if (nexthop->id > 0) |
| 407 |
return 0; |
| 408 |
|
| 409 |
r = ndisc_nexthop_acquire_id(nexthop, link); |
| 410 |
if (r < 0) |
| 411 |
return r; |
| 412 |
|
| 413 |
r = link_request_nexthop(link, nexthop, &link->ndisc_messages, ndisc_nexthop_handler); |
| 414 |
if (r < 0) |
| 415 |
return r; |
| 416 |
if (r > 0) |
| 417 |
link->ndisc_configured = false; |
| 418 |
|
| 419 |
return 0; |
| 420 |
} |
| 421 |
|
| 422 |
static int ndisc_set_route_nexthop(Route *route, Link *link, bool request) { |
| 423 |
_cleanup_(nexthop_unrefp) NextHop *nexthop = NULL; |
| 424 |
int r; |
| 425 |
|
| 426 |
assert(route); |
| 427 |
assert(link); |
| 428 |
assert(link->manager); |
| 429 |
|
| 430 |
if (!link->manager->manage_foreign_nexthops) |
| 431 |
goto finalize; |
| 432 |
|
| 433 |
if (route->nexthop.family != AF_INET6 || in6_addr_is_null(&route->nexthop.gw.in6)) |
| 434 |
goto finalize; |
| 435 |
|
| 436 |
r = ndisc_nexthop_new(route, link, &nexthop); |
| 437 |
if (r < 0) |
| 438 |
return r; |
| 439 |
|
| 440 |
if (nexthop->id == 0 && !request) |
| 441 |
goto finalize; |
| 442 |
|
| 443 |
r = ndisc_request_nexthop(nexthop, link); |
| 444 |
if (r < 0) |
| 445 |
return r; |
| 446 |
|
| 447 |
route->nexthop = (RouteNextHop) {}; |
| 448 |
route->nexthop_id = nexthop->id; |
| 449 |
|
| 450 |
finalize: |
| 451 |
return route_adjust_nexthops(route, link); |
| 452 |
} |
| 453 |
|
| 454 |
static int ndisc_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) { |
| 455 |
int r; |
| 456 |
|
| 457 |
assert(req); |
| 458 |
assert(link); |
| 459 |
assert(route); |
| 460 |
|
| 461 |
r = route_configure_handler_internal(m, req, route); |
| 462 |
if (r <= 0) |
| 463 |
return r; |
| 464 |
|
| 465 |
r = ndisc_check_ready(link); |
| 466 |
if (r < 0) |
| 467 |
link_enter_failed(link); |
| 468 |
|
| 469 |
return 1; |
| 470 |
} |
| 471 |
|
| 472 |
static void ndisc_set_route_priority(Link *link, Route *route) { |
| 473 |
assert(link); |
| 474 |
assert(route); |
| 475 |
|
| 476 |
if (route->priority_set) |
| 477 |
return; /* explicitly configured. */ |
| 478 |
|
| 479 |
switch (route->pref) { |
| 480 |
case SD_NDISC_PREFERENCE_LOW: |
| 481 |
route->priority = link->network->ndisc_route_metric_low; |
| 482 |
break; |
| 483 |
case SD_NDISC_PREFERENCE_MEDIUM: |
| 484 |
route->priority = link->network->ndisc_route_metric_medium; |
| 485 |
break; |
| 486 |
case SD_NDISC_PREFERENCE_HIGH: |
| 487 |
route->priority = link->network->ndisc_route_metric_high; |
| 488 |
break; |
| 489 |
default: |
| 490 |
assert_not_reached(); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
static int ndisc_request_route(Route *route, Link *link) { |
| 495 |
int r; |
| 496 |
|
| 497 |
assert(route); |
| 498 |
assert(link); |
| 499 |
assert(link->manager); |
| 500 |
assert(link->network); |
| 501 |
|
| 502 |
r = route_metric_set(&route->metric, RTAX_QUICKACK, link->network->ndisc_quickack); |
| 503 |
if (r < 0) |
| 504 |
return r; |
| 505 |
|
| 506 |
r = ndisc_set_route_nexthop(route, link, /* request= */ true); |
| 507 |
if (r < 0) |
| 508 |
return r; |
| 509 |
|
| 510 |
uint8_t pref, pref_original = route->pref; |
| 511 |
FOREACH_ARGUMENT(pref, SD_NDISC_PREFERENCE_LOW, SD_NDISC_PREFERENCE_MEDIUM, SD_NDISC_PREFERENCE_HIGH) { |
| 512 |
Route *existing; |
| 513 |
Request *req; |
| 514 |
|
| 515 |
/* If the preference is specified by the user config (that is, for semi-static routes), |
| 516 |
* rather than RA, then only search conflicting routes that have the same preference. */ |
| 517 |
if (route->pref_set && pref != pref_original) |
| 518 |
continue; |
| 519 |
|
| 520 |
route->pref = pref; |
| 521 |
ndisc_set_route_priority(link, route); |
| 522 |
|
| 523 |
/* Note, here do not call route_remove_and_cancel() with 'route' directly, otherwise |
| 524 |
* existing route(s) may be removed needlessly. */ |
| 525 |
|
| 526 |
/* First, check if a conflicting route is already requested. If there is an existing route, |
| 527 |
* and also an existing pending request, then the source may be updated by the request. So, |
| 528 |
* we first need to check the source of the requested route. */ |
| 529 |
if (route_get_request(link->manager, route, &req) >= 0) { |
| 530 |
route->pref = pref_original; |
| 531 |
ndisc_set_route_priority(link, route); |
| 532 |
|
| 533 |
existing = ASSERT_PTR(req->userdata); |
| 534 |
if (!route_can_update(link->manager, existing, route)) { |
| 535 |
if (existing->source == NETWORK_CONFIG_SOURCE_STATIC) { |
| 536 |
log_link_debug(link, "Found a pending route request that conflicts with new request based on a received RA, ignoring request."); |
| 537 |
return 0; |
| 538 |
} |
| 539 |
|
| 540 |
log_link_debug(link, "Found a pending route request that conflicts with new request based on a received RA, cancelling."); |
| 541 |
r = route_remove_and_cancel(existing, link->manager); |
| 542 |
if (r < 0) |
| 543 |
return r; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
route->pref = pref; |
| 548 |
ndisc_set_route_priority(link, route); |
| 549 |
|
| 550 |
/* Then, check if a conflicting route exists. */ |
| 551 |
if (route_get(link->manager, route, &existing) >= 0) { |
| 552 |
route->pref = pref_original; |
| 553 |
ndisc_set_route_priority(link, route); |
| 554 |
|
| 555 |
if (!route_can_update(link->manager, existing, route)) { |
| 556 |
if (existing->source == NETWORK_CONFIG_SOURCE_STATIC) { |
| 557 |
log_link_debug(link, "Found an existing route that conflicts with new route based on a received RA, ignoring request."); |
| 558 |
return 0; |
| 559 |
} |
| 560 |
|
| 561 |
log_link_debug(link, "Found an existing route that conflicts with new route based on a received RA, removing."); |
| 562 |
r = route_remove_and_cancel(existing, link->manager); |
| 563 |
if (r < 0) |
| 564 |
return r; |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
/* The preference (and priority) may be changed in the above loop. Restore it. */ |
| 570 |
route->pref = pref_original; |
| 571 |
ndisc_set_route_priority(link, route); |
| 572 |
|
| 573 |
bool is_new = route_get(link->manager, route, NULL) < 0; |
| 574 |
|
| 575 |
r = link_request_route(link, route, &link->ndisc_messages, ndisc_route_handler); |
| 576 |
if (r < 0) |
| 577 |
return r; |
| 578 |
if (r > 0 && is_new) |
| 579 |
link->ndisc_configured = false; |
| 580 |
|
| 581 |
return 0; |
| 582 |
} |
| 583 |
|
| 584 |
static void ndisc_route_prepare(Route *route, Link *link) { |
| 585 |
assert(route); |
| 586 |
assert(link); |
| 587 |
|
| 588 |
route->source = NETWORK_CONFIG_SOURCE_NDISC; |
| 589 |
|
| 590 |
if (!route->table_set) |
| 591 |
route->table = link_get_ndisc_route_table(link); |
| 592 |
} |
| 593 |
|
| 594 |
static int ndisc_router_route_prepare(Route *route, Link *link, sd_ndisc_router *rt) { |
| 595 |
assert(route); |
| 596 |
assert(link); |
| 597 |
assert(rt); |
| 598 |
|
| 599 |
ndisc_route_prepare(route, link); |
| 600 |
|
| 601 |
if (!route->protocol_set) |
| 602 |
route->protocol = RTPROT_RA; |
| 603 |
|
| 604 |
return sd_ndisc_router_get_sender_address(rt, &route->provider.in6); |
| 605 |
} |
| 606 |
|
| 607 |
static int ndisc_request_router_route(Route *route, Link *link, sd_ndisc_router *rt) { |
| 608 |
int r; |
| 609 |
|
| 610 |
assert(route); |
| 611 |
assert(link); |
| 612 |
assert(rt); |
| 613 |
|
| 614 |
r = ndisc_router_route_prepare(route, link, rt); |
| 615 |
if (r < 0) |
| 616 |
return r; |
| 617 |
|
| 618 |
return ndisc_request_route(route, link); |
| 619 |
} |
| 620 |
|
| 621 |
static int ndisc_remove_route(Route *route, Link *link) { |
| 622 |
int r, ret = 0; |
| 623 |
|
| 624 |
assert(route); |
| 625 |
assert(link); |
| 626 |
assert(link->manager); |
| 627 |
|
| 628 |
r = ndisc_set_route_nexthop(route, link, /* request= */ false); |
| 629 |
if (r < 0) |
| 630 |
return r; |
| 631 |
|
| 632 |
uint8_t pref, pref_original = route->pref; |
| 633 |
FOREACH_ARGUMENT(pref, SD_NDISC_PREFERENCE_LOW, SD_NDISC_PREFERENCE_MEDIUM, SD_NDISC_PREFERENCE_HIGH) { |
| 634 |
Route *existing; |
| 635 |
Request *req; |
| 636 |
|
| 637 |
/* If the preference is specified by the user config (that is, for semi-static routes), |
| 638 |
* rather than RA, then only search conflicting routes that have the same preference. */ |
| 639 |
if (route->pref_set && pref != pref_original) |
| 640 |
continue; |
| 641 |
|
| 642 |
route->pref = pref; |
| 643 |
ndisc_set_route_priority(link, route); |
| 644 |
|
| 645 |
/* Unfortunately, we cannot directly pass 'route' to route_remove_and_cancel() here, as the |
| 646 |
* same or similar route may be configured or requested statically. */ |
| 647 |
|
| 648 |
/* First, check if the route is already requested. If there is an existing route, and also an |
| 649 |
* existing pending request, then the source may be updated by the request. So, we first need |
| 650 |
* to check the source of the requested route. */ |
| 651 |
if (route_get_request(link->manager, route, &req) >= 0) { |
| 652 |
existing = ASSERT_PTR(req->userdata); |
| 653 |
if (existing->source == NETWORK_CONFIG_SOURCE_STATIC) |
| 654 |
continue; |
| 655 |
|
| 656 |
RET_GATHER(ret, route_remove_and_cancel(existing, link->manager)); |
| 657 |
} |
| 658 |
|
| 659 |
/* Then, check if the route exists. */ |
| 660 |
if (route_get(link->manager, route, &existing) >= 0) { |
| 661 |
if (existing->source == NETWORK_CONFIG_SOURCE_STATIC) |
| 662 |
continue; |
| 663 |
|
| 664 |
RET_GATHER(ret, route_remove_and_cancel(existing, link->manager)); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
return RET_GATHER(ret, ndisc_remove_unused_nexthop_by_id(link, route->nexthop_id)); |
| 669 |
} |
| 670 |
|
| 671 |
static int ndisc_remove_router_route(Route *route, Link *link, sd_ndisc_router *rt) { |
| 672 |
int r; |
| 673 |
|
| 674 |
assert(route); |
| 675 |
assert(link); |
| 676 |
assert(rt); |
| 677 |
|
| 678 |
r = ndisc_router_route_prepare(route, link, rt); |
| 679 |
if (r < 0) |
| 680 |
return r; |
| 681 |
|
| 682 |
return ndisc_remove_route(route, link); |
| 683 |
} |
| 684 |
|
| 685 |
static int ndisc_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) { |
| 686 |
int r; |
| 687 |
|
| 688 |
assert(link); |
| 689 |
assert(address); |
| 690 |
|
| 691 |
r = address_configure_handler_internal(m, link, address); |
| 692 |
if (r <= 0) |
| 693 |
return r; |
| 694 |
|
| 695 |
r = ndisc_check_ready(link); |
| 696 |
if (r < 0) |
| 697 |
link_enter_failed(link); |
| 698 |
|
| 699 |
return 1; |
| 700 |
} |
| 701 |
|
| 702 |
static int ndisc_request_address(Address *address, Link *link) { |
| 703 |
bool is_new; |
| 704 |
int r; |
| 705 |
|
| 706 |
assert(address); |
| 707 |
assert(link); |
| 708 |
|
| 709 |
address->source = NETWORK_CONFIG_SOURCE_NDISC; |
| 710 |
|
| 711 |
r = free_and_strdup_warn(&address->netlabel, link->network->ndisc_netlabel); |
| 712 |
if (r < 0) |
| 713 |
return r; |
| 714 |
|
| 715 |
Address *existing; |
| 716 |
if (address_get_harder(link, address, &existing) < 0) |
| 717 |
is_new = true; |
| 718 |
else if (address_can_update(existing, address)) |
| 719 |
is_new = false; |
| 720 |
else if (existing->source == NETWORK_CONFIG_SOURCE_DHCP6) { |
| 721 |
/* SLAAC address is preferred over DHCPv6 address. */ |
| 722 |
log_link_debug(link, "Conflicting DHCPv6 address %s exists, removing.", |
| 723 |
IN_ADDR_PREFIX_TO_STRING(existing->family, &existing->in_addr, existing->prefixlen)); |
| 724 |
r = address_remove(existing, link); |
| 725 |
if (r < 0) |
| 726 |
return r; |
| 727 |
|
| 728 |
is_new = true; |
| 729 |
} else { |
| 730 |
/* Conflicting static address is configured?? */ |
| 731 |
log_link_debug(link, "Conflicting address %s exists, ignoring request.", |
| 732 |
IN_ADDR_PREFIX_TO_STRING(existing->family, &existing->in_addr, existing->prefixlen)); |
| 733 |
return 0; |
| 734 |
} |
| 735 |
|
| 736 |
r = link_request_address(link, address, &link->ndisc_messages, |
| 737 |
ndisc_address_handler, NULL); |
| 738 |
if (r < 0) |
| 739 |
return r; |
| 740 |
if (r > 0 && is_new) |
| 741 |
link->ndisc_configured = false; |
| 742 |
|
| 743 |
return 0; |
| 744 |
} |
| 745 |
|
| 746 |
int ndisc_reconfigure_address(Address *address, Link *link) { |
| 747 |
int r; |
| 748 |
|
| 749 |
assert(address); |
| 750 |
assert(address->source == NETWORK_CONFIG_SOURCE_NDISC); |
| 751 |
assert(link); |
| 752 |
|
| 753 |
r = regenerate_address(address, link); |
| 754 |
if (r <= 0) |
| 755 |
return r; |
| 756 |
|
| 757 |
r = ndisc_request_address(address, link); |
| 758 |
if (r < 0) |
| 759 |
return r; |
| 760 |
|
| 761 |
if (!link->ndisc_configured) |
| 762 |
link_set_state(link, LINK_STATE_CONFIGURING); |
| 763 |
|
| 764 |
link_check_ready(link); |
| 765 |
return 0; |
| 766 |
} |
| 767 |
|
| 768 |
static int ndisc_redirect_route_new(sd_ndisc_redirect *rd, Route **ret) { |
| 769 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 770 |
struct in6_addr gateway, destination; |
| 771 |
int r; |
| 772 |
|
| 773 |
assert(rd); |
| 774 |
assert(ret); |
| 775 |
|
| 776 |
r = sd_ndisc_redirect_get_target_address(rd, &gateway); |
| 777 |
if (r < 0) |
| 778 |
return r; |
| 779 |
|
| 780 |
r = sd_ndisc_redirect_get_destination_address(rd, &destination); |
| 781 |
if (r < 0) |
| 782 |
return r; |
| 783 |
|
| 784 |
r = route_new(&route); |
| 785 |
if (r < 0) |
| 786 |
return r; |
| 787 |
|
| 788 |
route->family = AF_INET6; |
| 789 |
if (!in6_addr_equal(&gateway, &destination)) { |
| 790 |
route->nexthop.gw.in6 = gateway; |
| 791 |
route->nexthop.family = AF_INET6; |
| 792 |
} |
| 793 |
route->dst.in6 = destination; |
| 794 |
route->dst_prefixlen = 128; |
| 795 |
route->protocol = RTPROT_REDIRECT; |
| 796 |
|
| 797 |
r = sd_ndisc_redirect_get_sender_address(rd, &route->provider.in6); |
| 798 |
if (r < 0) |
| 799 |
return r; |
| 800 |
|
| 801 |
*ret = TAKE_PTR(route); |
| 802 |
return 0; |
| 803 |
} |
| 804 |
|
| 805 |
static int ndisc_remove_redirect_route(Link *link, sd_ndisc_redirect *rd) { |
| 806 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 807 |
int r; |
| 808 |
|
| 809 |
assert(link); |
| 810 |
assert(rd); |
| 811 |
|
| 812 |
r = ndisc_redirect_route_new(rd, &route); |
| 813 |
if (r < 0) |
| 814 |
return r; |
| 815 |
|
| 816 |
ndisc_route_prepare(route, link); |
| 817 |
|
| 818 |
return ndisc_remove_route(route, link); |
| 819 |
} |
| 820 |
|
| 821 |
static void ndisc_redirect_hash_func(const sd_ndisc_redirect *x, struct siphash *state) { |
| 822 |
struct in6_addr dest = {}; |
| 823 |
|
| 824 |
assert(x); |
| 825 |
assert(state); |
| 826 |
|
| 827 |
(void) sd_ndisc_redirect_get_destination_address((sd_ndisc_redirect*) x, &dest); |
| 828 |
|
| 829 |
siphash24_compress_typesafe(dest, state); |
| 830 |
} |
| 831 |
|
| 832 |
static int ndisc_redirect_compare_func(const sd_ndisc_redirect *x, const sd_ndisc_redirect *y) { |
| 833 |
struct in6_addr dest_x = {}, dest_y = {}; |
| 834 |
|
| 835 |
assert(x); |
| 836 |
assert(y); |
| 837 |
|
| 838 |
(void) sd_ndisc_redirect_get_destination_address((sd_ndisc_redirect*) x, &dest_x); |
| 839 |
(void) sd_ndisc_redirect_get_destination_address((sd_ndisc_redirect*) y, &dest_y); |
| 840 |
|
| 841 |
return memcmp(&dest_x, &dest_y, sizeof(dest_x)); |
| 842 |
} |
| 843 |
|
| 844 |
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR( |
| 845 |
ndisc_redirect_hash_ops, |
| 846 |
sd_ndisc_redirect, |
| 847 |
ndisc_redirect_hash_func, |
| 848 |
ndisc_redirect_compare_func, |
| 849 |
sd_ndisc_redirect_unref); |
| 850 |
|
| 851 |
static int ndisc_redirect_equal(sd_ndisc_redirect *x, sd_ndisc_redirect *y) { |
| 852 |
struct in6_addr a, b; |
| 853 |
int r; |
| 854 |
|
| 855 |
assert(x); |
| 856 |
assert(y); |
| 857 |
|
| 858 |
r = sd_ndisc_redirect_get_destination_address(x, &a); |
| 859 |
if (r < 0) |
| 860 |
return r; |
| 861 |
|
| 862 |
r = sd_ndisc_redirect_get_destination_address(y, &b); |
| 863 |
if (r < 0) |
| 864 |
return r; |
| 865 |
|
| 866 |
if (!in6_addr_equal(&a, &b)) |
| 867 |
return false; |
| 868 |
|
| 869 |
r = sd_ndisc_redirect_get_target_address(x, &a); |
| 870 |
if (r < 0) |
| 871 |
return r; |
| 872 |
|
| 873 |
r = sd_ndisc_redirect_get_target_address(y, &b); |
| 874 |
if (r < 0) |
| 875 |
return r; |
| 876 |
|
| 877 |
return in6_addr_equal(&a, &b); |
| 878 |
} |
| 879 |
|
| 880 |
static int ndisc_redirect_drop_conflict(Link *link, sd_ndisc_redirect *rd) { |
| 881 |
_cleanup_(sd_ndisc_redirect_unrefp) sd_ndisc_redirect *existing = NULL; |
| 882 |
int r; |
| 883 |
|
| 884 |
assert(link); |
| 885 |
assert(rd); |
| 886 |
|
| 887 |
existing = set_remove(link->ndisc_redirects, rd); |
| 888 |
if (!existing) |
| 889 |
return 0; |
| 890 |
|
| 891 |
r = ndisc_redirect_equal(rd, existing); |
| 892 |
if (r != 0) |
| 893 |
return r; |
| 894 |
|
| 895 |
return ndisc_remove_redirect_route(link, existing); |
| 896 |
} |
| 897 |
|
| 898 |
static int ndisc_redirect_verify_sender(Link *link, sd_ndisc_redirect *rd) { |
| 899 |
int r; |
| 900 |
|
| 901 |
assert(link); |
| 902 |
assert(rd); |
| 903 |
|
| 904 |
/* RFC 4861 section 8.1 |
| 905 |
* The IP source address of the Redirect is the same as the current first-hop router for the specified |
| 906 |
* ICMP Destination Address. */ |
| 907 |
|
| 908 |
struct in6_addr sender; |
| 909 |
r = sd_ndisc_redirect_get_sender_address(rd, &sender); |
| 910 |
if (r < 0) |
| 911 |
return r; |
| 912 |
|
| 913 |
/* We will reuse the sender's router lifetime as the lifetime of the redirect route. Hence, if we |
| 914 |
* have not remembered an RA from the sender, refuse the Redirect message. */ |
| 915 |
sd_ndisc_router *router = hashmap_get(link->ndisc_routers_by_sender, &sender); |
| 916 |
if (!router) |
| 917 |
return false; |
| 918 |
|
| 919 |
sd_ndisc_redirect *existing = set_get(link->ndisc_redirects, rd); |
| 920 |
if (existing) { |
| 921 |
struct in6_addr target, dest; |
| 922 |
|
| 923 |
/* If we have received Redirect message for the host, the sender must be the previous target. */ |
| 924 |
|
| 925 |
r = sd_ndisc_redirect_get_target_address(existing, &target); |
| 926 |
if (r < 0) |
| 927 |
return r; |
| 928 |
|
| 929 |
if (in6_addr_equal(&sender, &target)) |
| 930 |
return true; |
| 931 |
|
| 932 |
/* If the existing redirect route is on-link, that is, the destination and target address are |
| 933 |
* equivalent, then also accept Redirect message from the current default router. This is not |
| 934 |
* mentioned by the RFC, but without this, we cannot update on-link redirect route. */ |
| 935 |
r = sd_ndisc_redirect_get_destination_address(existing, &dest); |
| 936 |
if (r < 0) |
| 937 |
return r; |
| 938 |
|
| 939 |
if (!in6_addr_equal(&dest, &target)) |
| 940 |
return false; |
| 941 |
} |
| 942 |
|
| 943 |
/* Check if the sender is one of the known router with highest priority. */ |
| 944 |
uint8_t preference; |
| 945 |
r = sd_ndisc_router_get_preference(router, &preference); |
| 946 |
if (r < 0) |
| 947 |
return r; |
| 948 |
|
| 949 |
if (preference == SD_NDISC_PREFERENCE_HIGH) |
| 950 |
return true; |
| 951 |
|
| 952 |
sd_ndisc_router *rt; |
| 953 |
HASHMAP_FOREACH(rt, link->ndisc_routers_by_sender) { |
| 954 |
if (rt == router) |
| 955 |
continue; |
| 956 |
|
| 957 |
uint8_t pref; |
| 958 |
if (sd_ndisc_router_get_preference(rt, &pref) < 0) |
| 959 |
continue; |
| 960 |
|
| 961 |
if (pref == SD_NDISC_PREFERENCE_HIGH || |
| 962 |
(pref == SD_NDISC_PREFERENCE_MEDIUM && preference == SD_NDISC_PREFERENCE_LOW)) |
| 963 |
return false; |
| 964 |
} |
| 965 |
|
| 966 |
return true; |
| 967 |
} |
| 968 |
|
| 969 |
static int ndisc_redirect_handler(Link *link, sd_ndisc_redirect *rd) { |
| 970 |
int r; |
| 971 |
|
| 972 |
assert(link); |
| 973 |
assert(link->network); |
| 974 |
assert(rd); |
| 975 |
|
| 976 |
if (!link->network->ndisc_use_redirect) |
| 977 |
return 0; |
| 978 |
|
| 979 |
usec_t now_usec; |
| 980 |
r = sd_event_now(link->manager->event, CLOCK_BOOTTIME, &now_usec); |
| 981 |
if (r < 0) |
| 982 |
return r; |
| 983 |
|
| 984 |
r = ndisc_drop_outdated(link, /* router= */ NULL, now_usec); |
| 985 |
if (r < 0) |
| 986 |
return r; |
| 987 |
|
| 988 |
r = ndisc_redirect_verify_sender(link, rd); |
| 989 |
if (r <= 0) |
| 990 |
return r; |
| 991 |
|
| 992 |
/* First, drop conflicting redirect route, if exists. */ |
| 993 |
r = ndisc_redirect_drop_conflict(link, rd); |
| 994 |
if (r < 0) |
| 995 |
return r; |
| 996 |
|
| 997 |
/* Then, remember the received message. */ |
| 998 |
r = set_ensure_put(&link->ndisc_redirects, &ndisc_redirect_hash_ops, rd); |
| 999 |
if (r < 0) |
| 1000 |
return r; |
| 1001 |
|
| 1002 |
sd_ndisc_redirect_ref(rd); |
| 1003 |
|
| 1004 |
/* Finally, request the corresponding route. */ |
| 1005 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 1006 |
r = ndisc_redirect_route_new(rd, &route); |
| 1007 |
if (r < 0) |
| 1008 |
return r; |
| 1009 |
|
| 1010 |
sd_ndisc_router *rt = hashmap_get(link->ndisc_routers_by_sender, &route->provider.in6); |
| 1011 |
if (!rt) |
| 1012 |
return -EADDRNOTAVAIL; |
| 1013 |
|
| 1014 |
r = sd_ndisc_router_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &route->lifetime_usec); |
| 1015 |
if (r < 0) |
| 1016 |
return r; |
| 1017 |
|
| 1018 |
ndisc_route_prepare(route, link); |
| 1019 |
|
| 1020 |
return ndisc_request_route(route, link); |
| 1021 |
} |
| 1022 |
|
| 1023 |
static int ndisc_drop_redirect(Link *link, const struct in6_addr *router) { |
| 1024 |
int r, ret = 0; |
| 1025 |
|
| 1026 |
assert(link); |
| 1027 |
|
| 1028 |
sd_ndisc_redirect *rd; |
| 1029 |
SET_FOREACH(rd, link->ndisc_redirects) { |
| 1030 |
if (router) { |
| 1031 |
struct in6_addr a; |
| 1032 |
|
| 1033 |
if (!(sd_ndisc_redirect_get_sender_address(rd, &a) >= 0 && in6_addr_equal(&a, router)) && |
| 1034 |
!(sd_ndisc_redirect_get_target_address(rd, &a) >= 0 && in6_addr_equal(&a, router))) |
| 1035 |
continue; |
| 1036 |
} |
| 1037 |
|
| 1038 |
r = ndisc_remove_redirect_route(link, rd); |
| 1039 |
if (r < 0) |
| 1040 |
RET_GATHER(ret, log_link_warning_errno(link, r, "Failed to remove redirect route, ignoring: %m")); |
| 1041 |
|
| 1042 |
sd_ndisc_redirect_unref(set_remove(link->ndisc_redirects, rd)); |
| 1043 |
} |
| 1044 |
|
| 1045 |
return ret; |
| 1046 |
} |
| 1047 |
|
| 1048 |
static int ndisc_update_redirect_sender(Link *link, const struct in6_addr *original_address, const struct in6_addr *current_address) { |
| 1049 |
int r; |
| 1050 |
|
| 1051 |
assert(link); |
| 1052 |
assert(original_address); |
| 1053 |
assert(current_address); |
| 1054 |
|
| 1055 |
sd_ndisc_redirect *rd; |
| 1056 |
SET_FOREACH(rd, link->ndisc_redirects) { |
| 1057 |
struct in6_addr sender; |
| 1058 |
|
| 1059 |
r = sd_ndisc_redirect_get_sender_address(rd, &sender); |
| 1060 |
if (r < 0) |
| 1061 |
return r; |
| 1062 |
|
| 1063 |
if (!in6_addr_equal(&sender, original_address)) |
| 1064 |
continue; |
| 1065 |
|
| 1066 |
r = sd_ndisc_redirect_set_sender_address(rd, current_address); |
| 1067 |
if (r < 0) |
| 1068 |
return r; |
| 1069 |
} |
| 1070 |
|
| 1071 |
return 0; |
| 1072 |
} |
| 1073 |
|
| 1074 |
static int ndisc_router_drop_default(Link *link, sd_ndisc_router *rt) { |
| 1075 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 1076 |
struct in6_addr gateway; |
| 1077 |
int r; |
| 1078 |
|
| 1079 |
assert(link); |
| 1080 |
assert(link->network); |
| 1081 |
assert(rt); |
| 1082 |
|
| 1083 |
r = sd_ndisc_router_get_sender_address(rt, &gateway); |
| 1084 |
if (r < 0) |
| 1085 |
return log_link_warning_errno(link, r, "Failed to get router address from RA: %m"); |
| 1086 |
|
| 1087 |
r = route_new(&route); |
| 1088 |
if (r < 0) |
| 1089 |
return log_oom(); |
| 1090 |
|
| 1091 |
route->family = AF_INET6; |
| 1092 |
route->nexthop.family = AF_INET6; |
| 1093 |
route->nexthop.gw.in6 = gateway; |
| 1094 |
|
| 1095 |
r = ndisc_remove_router_route(route, link, rt); |
| 1096 |
if (r < 0) |
| 1097 |
return log_link_warning_errno(link, r, "Failed to remove the default gateway configured by RA: %m"); |
| 1098 |
|
| 1099 |
Route *route_gw; |
| 1100 |
HASHMAP_FOREACH(route_gw, link->network->routes_by_section) { |
| 1101 |
_cleanup_(route_unrefp) Route *tmp = NULL; |
| 1102 |
|
| 1103 |
if (route_gw->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 1104 |
continue; |
| 1105 |
|
| 1106 |
assert(route_gw->nexthop.family == AF_INET6); |
| 1107 |
|
| 1108 |
r = route_dup(route_gw, NULL, &tmp); |
| 1109 |
if (r < 0) |
| 1110 |
return r; |
| 1111 |
|
| 1112 |
tmp->nexthop.gw.in6 = gateway; |
| 1113 |
|
| 1114 |
r = ndisc_remove_router_route(tmp, link, rt); |
| 1115 |
if (r < 0) |
| 1116 |
return log_link_warning_errno(link, r, "Could not remove semi-static gateway: %m"); |
| 1117 |
} |
| 1118 |
|
| 1119 |
return 0; |
| 1120 |
} |
| 1121 |
|
| 1122 |
static int ndisc_router_process_default(Link *link, sd_ndisc_router *rt) { |
| 1123 |
usec_t lifetime_usec; |
| 1124 |
struct in6_addr gateway; |
| 1125 |
uint8_t preference; |
| 1126 |
int r; |
| 1127 |
|
| 1128 |
assert(link); |
| 1129 |
assert(link->network); |
| 1130 |
assert(rt); |
| 1131 |
|
| 1132 |
/* If the router lifetime is zero, the router should not be used as the default gateway. */ |
| 1133 |
r = sd_ndisc_router_get_lifetime(rt, NULL); |
| 1134 |
if (r < 0) |
| 1135 |
return r; |
| 1136 |
if (r == 0) |
| 1137 |
return ndisc_router_drop_default(link, rt); |
| 1138 |
|
| 1139 |
if (!link->network->ndisc_use_gateway && |
| 1140 |
hashmap_isempty(link->network->routes_by_section)) |
| 1141 |
return 0; |
| 1142 |
|
| 1143 |
r = sd_ndisc_router_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec); |
| 1144 |
if (r < 0) |
| 1145 |
return log_link_warning_errno(link, r, "Failed to get gateway lifetime from RA: %m"); |
| 1146 |
|
| 1147 |
r = sd_ndisc_router_get_sender_address(rt, &gateway); |
| 1148 |
if (r < 0) |
| 1149 |
return log_link_warning_errno(link, r, "Failed to get gateway address from RA: %m"); |
| 1150 |
|
| 1151 |
r = sd_ndisc_router_get_preference(rt, &preference); |
| 1152 |
if (r < 0) |
| 1153 |
return log_link_warning_errno(link, r, "Failed to get router preference from RA: %m"); |
| 1154 |
|
| 1155 |
if (link->network->ndisc_use_gateway) { |
| 1156 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 1157 |
|
| 1158 |
r = route_new(&route); |
| 1159 |
if (r < 0) |
| 1160 |
return log_oom(); |
| 1161 |
|
| 1162 |
route->family = AF_INET6; |
| 1163 |
route->pref = preference; |
| 1164 |
route->nexthop.family = AF_INET6; |
| 1165 |
route->nexthop.gw.in6 = gateway; |
| 1166 |
route->lifetime_usec = lifetime_usec; |
| 1167 |
|
| 1168 |
r = ndisc_request_router_route(route, link, rt); |
| 1169 |
if (r < 0) |
| 1170 |
return log_link_warning_errno(link, r, "Could not request default route: %m"); |
| 1171 |
} |
| 1172 |
|
| 1173 |
Route *route_gw; |
| 1174 |
HASHMAP_FOREACH(route_gw, link->network->routes_by_section) { |
| 1175 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 1176 |
|
| 1177 |
if (route_gw->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 1178 |
continue; |
| 1179 |
|
| 1180 |
assert(route_gw->nexthop.family == AF_INET6); |
| 1181 |
|
| 1182 |
r = route_dup(route_gw, NULL, &route); |
| 1183 |
if (r < 0) |
| 1184 |
return r; |
| 1185 |
|
| 1186 |
route->nexthop.gw.in6 = gateway; |
| 1187 |
if (!route->pref_set) |
| 1188 |
route->pref = preference; |
| 1189 |
route->lifetime_usec = lifetime_usec; |
| 1190 |
|
| 1191 |
r = ndisc_request_router_route(route, link, rt); |
| 1192 |
if (r < 0) |
| 1193 |
return log_link_warning_errno(link, r, "Could not request gateway: %m"); |
| 1194 |
} |
| 1195 |
|
| 1196 |
return 0; |
| 1197 |
} |
| 1198 |
|
| 1199 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 1200 |
ndisc_router_hash_ops, |
| 1201 |
struct in6_addr, |
| 1202 |
in6_addr_hash_func, |
| 1203 |
in6_addr_compare_func, |
| 1204 |
sd_ndisc_router, |
| 1205 |
sd_ndisc_router_unref); |
| 1206 |
|
| 1207 |
static int ndisc_update_router_address(Link *link, const struct in6_addr *original_address, const struct in6_addr *current_address) { |
| 1208 |
_cleanup_(sd_ndisc_router_unrefp) sd_ndisc_router *rt = NULL; |
| 1209 |
int r; |
| 1210 |
|
| 1211 |
assert(link); |
| 1212 |
assert(original_address); |
| 1213 |
assert(current_address); |
| 1214 |
|
| 1215 |
rt = hashmap_remove(link->ndisc_routers_by_sender, original_address); |
| 1216 |
if (!rt) |
| 1217 |
return 0; |
| 1218 |
|
| 1219 |
/* If we already received an RA from the new address, then forget the RA from the old address. */ |
| 1220 |
if (hashmap_contains(link->ndisc_routers_by_sender, current_address)) |
| 1221 |
return 0; |
| 1222 |
|
| 1223 |
/* Otherwise, update the sender address of the previously received RA. */ |
| 1224 |
r = sd_ndisc_router_set_sender_address(rt, current_address); |
| 1225 |
if (r < 0) |
| 1226 |
return r; |
| 1227 |
|
| 1228 |
r = hashmap_put(link->ndisc_routers_by_sender, &rt->packet->sender_address, rt); |
| 1229 |
if (r < 0) |
| 1230 |
return r; |
| 1231 |
|
| 1232 |
TAKE_PTR(rt); |
| 1233 |
return 0; |
| 1234 |
} |
| 1235 |
|
| 1236 |
static int ndisc_drop_router_one(Link *link, sd_ndisc_router *rt, usec_t timestamp_usec) { |
| 1237 |
usec_t lifetime_usec; |
| 1238 |
int r; |
| 1239 |
|
| 1240 |
assert(link); |
| 1241 |
assert(rt); |
| 1242 |
assert(rt->packet); |
| 1243 |
|
| 1244 |
r = sd_ndisc_router_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec); |
| 1245 |
if (r < 0) |
| 1246 |
return r; |
| 1247 |
|
| 1248 |
if (lifetime_usec > timestamp_usec) |
| 1249 |
return 0; |
| 1250 |
|
| 1251 |
r = ndisc_drop_redirect(link, &rt->packet->sender_address); |
| 1252 |
|
| 1253 |
sd_ndisc_router_unref(hashmap_remove(link->ndisc_routers_by_sender, &rt->packet->sender_address)); |
| 1254 |
|
| 1255 |
return r; |
| 1256 |
} |
| 1257 |
|
| 1258 |
static int ndisc_drop_routers(Link *link, const struct in6_addr *router, usec_t timestamp_usec) { |
| 1259 |
sd_ndisc_router *rt; |
| 1260 |
int ret = 0; |
| 1261 |
|
| 1262 |
assert(link); |
| 1263 |
|
| 1264 |
if (router) { |
| 1265 |
rt = hashmap_get(link->ndisc_routers_by_sender, router); |
| 1266 |
if (!rt) |
| 1267 |
return 0; |
| 1268 |
|
| 1269 |
return ndisc_drop_router_one(link, rt, timestamp_usec); |
| 1270 |
} |
| 1271 |
|
| 1272 |
HASHMAP_FOREACH_KEY(rt, router, link->ndisc_routers_by_sender) |
| 1273 |
RET_GATHER(ret, ndisc_drop_router_one(link, rt, timestamp_usec)); |
| 1274 |
|
| 1275 |
return ret; |
| 1276 |
} |
| 1277 |
|
| 1278 |
static int ndisc_remember_router(Link *link, sd_ndisc_router *rt) { |
| 1279 |
int r; |
| 1280 |
|
| 1281 |
assert(link); |
| 1282 |
assert(rt); |
| 1283 |
assert(rt->packet); |
| 1284 |
|
| 1285 |
sd_ndisc_router_unref(hashmap_remove(link->ndisc_routers_by_sender, &rt->packet->sender_address)); |
| 1286 |
|
| 1287 |
/* Remember RAs with non-zero lifetime. */ |
| 1288 |
r = sd_ndisc_router_get_lifetime(rt, NULL); |
| 1289 |
if (r <= 0) |
| 1290 |
return r; |
| 1291 |
|
| 1292 |
r = hashmap_ensure_put(&link->ndisc_routers_by_sender, &ndisc_router_hash_ops, &rt->packet->sender_address, rt); |
| 1293 |
if (r < 0) |
| 1294 |
return r; |
| 1295 |
|
| 1296 |
sd_ndisc_router_ref(rt); |
| 1297 |
return 0; |
| 1298 |
} |
| 1299 |
|
| 1300 |
static int ndisc_router_process_reachable_time(Link *link, sd_ndisc_router *rt) { |
| 1301 |
usec_t reachable_time, msec; |
| 1302 |
int r; |
| 1303 |
|
| 1304 |
assert(link); |
| 1305 |
assert(link->manager); |
| 1306 |
assert(link->network); |
| 1307 |
assert(rt); |
| 1308 |
|
| 1309 |
if (!link->network->ndisc_use_reachable_time) |
| 1310 |
return 0; |
| 1311 |
|
| 1312 |
r = sd_ndisc_router_get_reachable_time(rt, &reachable_time); |
| 1313 |
if (r < 0) |
| 1314 |
return log_link_warning_errno(link, r, "Failed to get reachable time from RA: %m"); |
| 1315 |
|
| 1316 |
/* 0 is the unspecified value and must not be set (see RFC4861, 6.3.4) */ |
| 1317 |
if (!timestamp_is_set(reachable_time)) |
| 1318 |
return 0; |
| 1319 |
|
| 1320 |
msec = DIV_ROUND_UP(reachable_time, USEC_PER_MSEC); |
| 1321 |
if (msec <= 0 || msec > UINT32_MAX) { |
| 1322 |
log_link_debug(link, "Failed to get reachable time from RA - out of range (%"PRIu64"), ignoring", msec); |
| 1323 |
return 0; |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* Set the reachable time for Neighbor Solicitations. */ |
| 1327 |
r = sysctl_write_ip_neighbor_property_uint32(AF_INET6, link->ifname, "base_reachable_time_ms", (uint32_t) msec, manager_get_sysctl_shadow(link->manager)); |
| 1328 |
if (r < 0) |
| 1329 |
log_link_warning_errno(link, r, "Failed to apply neighbor reachable time (%"PRIu64"), ignoring: %m", msec); |
| 1330 |
|
| 1331 |
return 0; |
| 1332 |
} |
| 1333 |
|
| 1334 |
static int ndisc_router_process_retransmission_time(Link *link, sd_ndisc_router *rt) { |
| 1335 |
usec_t retrans_time, msec; |
| 1336 |
int r; |
| 1337 |
|
| 1338 |
assert(link); |
| 1339 |
assert(link->manager); |
| 1340 |
assert(link->network); |
| 1341 |
assert(rt); |
| 1342 |
|
| 1343 |
if (!link->network->ndisc_use_retransmission_time) |
| 1344 |
return 0; |
| 1345 |
|
| 1346 |
r = sd_ndisc_router_get_retransmission_time(rt, &retrans_time); |
| 1347 |
if (r < 0) |
| 1348 |
return log_link_warning_errno(link, r, "Failed to get retransmission time from RA: %m"); |
| 1349 |
|
| 1350 |
/* 0 is the unspecified value and must not be set (see RFC4861, 6.3.4) */ |
| 1351 |
if (!timestamp_is_set(retrans_time)) |
| 1352 |
return 0; |
| 1353 |
|
| 1354 |
msec = DIV_ROUND_UP(retrans_time, USEC_PER_MSEC); |
| 1355 |
if (msec <= 0 || msec > UINT32_MAX) { |
| 1356 |
log_link_debug(link, "Failed to get retransmission time from RA - out of range (%"PRIu64"), ignoring", msec); |
| 1357 |
return 0; |
| 1358 |
} |
| 1359 |
|
| 1360 |
/* Set the retransmission time for Neighbor Solicitations. */ |
| 1361 |
r = sysctl_write_ip_neighbor_property_uint32(AF_INET6, link->ifname, "retrans_time_ms", (uint32_t) msec, manager_get_sysctl_shadow(link->manager)); |
| 1362 |
if (r < 0) |
| 1363 |
log_link_warning_errno(link, r, "Failed to apply neighbor retransmission time (%"PRIu64"), ignoring: %m", msec); |
| 1364 |
|
| 1365 |
return 0; |
| 1366 |
} |
| 1367 |
|
| 1368 |
static int ndisc_router_process_hop_limit(Link *link, sd_ndisc_router *rt) { |
| 1369 |
uint8_t hop_limit; |
| 1370 |
int r; |
| 1371 |
|
| 1372 |
assert(link); |
| 1373 |
assert(link->manager); |
| 1374 |
assert(link->network); |
| 1375 |
assert(rt); |
| 1376 |
|
| 1377 |
if (!link->network->ndisc_use_hop_limit) |
| 1378 |
return 0; |
| 1379 |
|
| 1380 |
r = sd_ndisc_router_get_hop_limit(rt, &hop_limit); |
| 1381 |
if (r < 0) |
| 1382 |
return log_link_warning_errno(link, r, "Failed to get hop limit from RA: %m"); |
| 1383 |
|
| 1384 |
/* 0 is the unspecified value and must not be set (see RFC4861, 6.3.4): |
| 1385 |
* |
| 1386 |
* A Router Advertisement field (e.g., Cur Hop Limit, Reachable Time, and Retrans Timer) may contain |
| 1387 |
* a value denoting that it is unspecified. In such cases, the parameter should be ignored and the |
| 1388 |
* host should continue using whatever value it is already using. In particular, a host MUST NOT |
| 1389 |
* interpret the unspecified value as meaning change back to the default value that was in use before |
| 1390 |
* the first Router Advertisement was received. |
| 1391 |
* |
| 1392 |
* If the received Cur Hop Limit value is non-zero, the host SHOULD set |
| 1393 |
* its CurHopLimit variable to the received value. */ |
| 1394 |
if (hop_limit <= 0) |
| 1395 |
return 0; |
| 1396 |
|
| 1397 |
r = sysctl_write_ip_property_uint32(AF_INET6, link->ifname, "hop_limit", (uint32_t) hop_limit, manager_get_sysctl_shadow(link->manager)); |
| 1398 |
if (r < 0) |
| 1399 |
log_link_warning_errno(link, r, "Failed to apply hop_limit (%u), ignoring: %m", hop_limit); |
| 1400 |
|
| 1401 |
return 0; |
| 1402 |
} |
| 1403 |
|
| 1404 |
static int ndisc_router_process_mtu(Link *link, sd_ndisc_router *rt) { |
| 1405 |
uint32_t mtu; |
| 1406 |
int r; |
| 1407 |
|
| 1408 |
assert(link); |
| 1409 |
assert(link->network); |
| 1410 |
assert(rt); |
| 1411 |
|
| 1412 |
if (!link->network->ndisc_use_mtu) |
| 1413 |
return 0; |
| 1414 |
|
| 1415 |
r = sd_ndisc_router_get_mtu(rt, &mtu); |
| 1416 |
if (r == -ENODATA) |
| 1417 |
return 0; |
| 1418 |
if (r < 0) |
| 1419 |
return log_link_warning_errno(link, r, "Failed to get MTU from RA: %m"); |
| 1420 |
|
| 1421 |
link->ndisc_mtu = mtu; |
| 1422 |
|
| 1423 |
(void) link_set_ipv6_mtu(link, LOG_DEBUG); |
| 1424 |
|
| 1425 |
return 0; |
| 1426 |
} |
| 1427 |
|
| 1428 |
static int ndisc_address_set_lifetime(Address *address, Link *link, sd_ndisc_router *rt) { |
| 1429 |
Address *existing; |
| 1430 |
usec_t t; |
| 1431 |
int r; |
| 1432 |
|
| 1433 |
assert(address); |
| 1434 |
assert(link); |
| 1435 |
assert(rt); |
| 1436 |
|
| 1437 |
/* This is mostly based on RFC 4862 section 5.5.3 (e). However, the definition of 'RemainingLifetime' |
| 1438 |
* is ambiguous, and there is no clear explanation when the address is not assigned yet. If we assume |
| 1439 |
* that 'RemainingLifetime' is zero in that case, then IPv6 Core Conformance test [v6LC.3.2.5 Part C] |
| 1440 |
* fails. So, in such case, we skip the conditions about 'RemainingLifetime'. */ |
| 1441 |
|
| 1442 |
r = sd_ndisc_router_prefix_get_valid_lifetime_timestamp(rt, CLOCK_BOOTTIME, &address->lifetime_valid_usec); |
| 1443 |
if (r < 0) |
| 1444 |
return r; |
| 1445 |
|
| 1446 |
r = sd_ndisc_router_prefix_get_preferred_lifetime_timestamp(rt, CLOCK_BOOTTIME, &address->lifetime_preferred_usec); |
| 1447 |
if (r < 0) |
| 1448 |
return r; |
| 1449 |
|
| 1450 |
/* RFC 4862 section 5.5.3 (e) |
| 1451 |
* 1. If the received Valid Lifetime is greater than 2 hours or greater than RemainingLifetime, |
| 1452 |
* set the valid lifetime of the corresponding address to the advertised Valid Lifetime. */ |
| 1453 |
r = sd_ndisc_router_prefix_get_valid_lifetime(rt, &t); |
| 1454 |
if (r < 0) |
| 1455 |
return r; |
| 1456 |
|
| 1457 |
if (t > 2 * USEC_PER_HOUR) |
| 1458 |
return 0; |
| 1459 |
|
| 1460 |
if (address_get(link, address, &existing) < 0 || existing->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 1461 |
return 0; |
| 1462 |
|
| 1463 |
if (address->lifetime_valid_usec > existing->lifetime_valid_usec) |
| 1464 |
return 0; |
| 1465 |
|
| 1466 |
/* 2. If RemainingLifetime is less than or equal to 2 hours, ignore the Prefix Information option |
| 1467 |
* with regards to the valid lifetime, unless the Router Advertisement from which this option was |
| 1468 |
* obtained has been authenticated (e.g., via Secure Neighbor Discovery [RFC3971]). If the Router |
| 1469 |
* Advertisement was authenticated, the valid lifetime of the corresponding address should be set |
| 1470 |
* to the Valid Lifetime in the received option. |
| 1471 |
* |
| 1472 |
* Currently, authentication is not supported. So check the lifetime of the existing address. */ |
| 1473 |
r = sd_ndisc_router_get_timestamp(rt, CLOCK_BOOTTIME, &t); |
| 1474 |
if (r < 0) |
| 1475 |
return r; |
| 1476 |
|
| 1477 |
if (existing->lifetime_valid_usec <= usec_add(t, 2 * USEC_PER_HOUR)) { |
| 1478 |
address->lifetime_valid_usec = existing->lifetime_valid_usec; |
| 1479 |
return 0; |
| 1480 |
} |
| 1481 |
|
| 1482 |
/* 3. Otherwise, reset the valid lifetime of the corresponding address to 2 hours. */ |
| 1483 |
address->lifetime_valid_usec = usec_add(t, 2 * USEC_PER_HOUR); |
| 1484 |
return 0; |
| 1485 |
} |
| 1486 |
|
| 1487 |
static int ndisc_router_process_autonomous_prefix(Link *link, sd_ndisc_router *rt) { |
| 1488 |
usec_t lifetime_valid_usec, lifetime_preferred_usec; |
| 1489 |
struct in6_addr prefix, router; |
| 1490 |
uint8_t prefixlen; |
| 1491 |
int r; |
| 1492 |
|
| 1493 |
assert(link); |
| 1494 |
assert(link->network); |
| 1495 |
assert(rt); |
| 1496 |
|
| 1497 |
if (!link->network->ndisc_use_autonomous_prefix) |
| 1498 |
return 0; |
| 1499 |
|
| 1500 |
r = sd_ndisc_router_get_sender_address(rt, &router); |
| 1501 |
if (r < 0) |
| 1502 |
return log_link_warning_errno(link, r, "Failed to get router address: %m"); |
| 1503 |
|
| 1504 |
r = sd_ndisc_router_prefix_get_address(rt, &prefix); |
| 1505 |
if (r < 0) |
| 1506 |
return log_link_warning_errno(link, r, "Failed to get prefix address: %m"); |
| 1507 |
|
| 1508 |
r = sd_ndisc_router_prefix_get_prefixlen(rt, &prefixlen); |
| 1509 |
if (r < 0) |
| 1510 |
return log_link_warning_errno(link, r, "Failed to get prefix length: %m"); |
| 1511 |
|
| 1512 |
/* ndisc_generate_addresses() below requires the prefix length <= 64. */ |
| 1513 |
if (prefixlen > 64) { |
| 1514 |
log_link_debug(link, "Prefix is longer than 64, ignoring autonomous prefix %s.", |
| 1515 |
IN6_ADDR_PREFIX_TO_STRING(&prefix, prefixlen)); |
| 1516 |
return 0; |
| 1517 |
} |
| 1518 |
|
| 1519 |
r = sd_ndisc_router_prefix_get_valid_lifetime(rt, &lifetime_valid_usec); |
| 1520 |
if (r < 0) |
| 1521 |
return log_link_warning_errno(link, r, "Failed to get prefix valid lifetime: %m"); |
| 1522 |
|
| 1523 |
r = sd_ndisc_router_prefix_get_preferred_lifetime(rt, &lifetime_preferred_usec); |
| 1524 |
if (r < 0) |
| 1525 |
return log_link_warning_errno(link, r, "Failed to get prefix preferred lifetime: %m"); |
| 1526 |
|
| 1527 |
/* RFC 4862 section 5.5.3 (c) |
| 1528 |
* If the preferred lifetime is greater than the valid lifetime, silently ignore the Prefix |
| 1529 |
* Information option. */ |
| 1530 |
if (lifetime_preferred_usec > lifetime_valid_usec) |
| 1531 |
return 0; |
| 1532 |
|
| 1533 |
_cleanup_hashmap_free_ Hashmap *tokens_by_address = NULL; |
| 1534 |
r = ndisc_generate_addresses(link, &prefix, prefixlen, &tokens_by_address); |
| 1535 |
if (r < 0) |
| 1536 |
return log_link_warning_errno(link, r, "Failed to generate SLAAC addresses: %m"); |
| 1537 |
|
| 1538 |
IPv6Token *token; |
| 1539 |
struct in6_addr *a; |
| 1540 |
HASHMAP_FOREACH_KEY(token, a, tokens_by_address) { |
| 1541 |
_cleanup_(address_unrefp) Address *address = NULL; |
| 1542 |
|
| 1543 |
r = address_new(&address); |
| 1544 |
if (r < 0) |
| 1545 |
return log_oom(); |
| 1546 |
|
| 1547 |
address->provider.in6 = router; |
| 1548 |
address->family = AF_INET6; |
| 1549 |
address->in_addr.in6 = *a; |
| 1550 |
address->prefixlen = prefixlen; |
| 1551 |
address->flags = IFA_F_NOPREFIXROUTE|IFA_F_MANAGETEMPADDR; |
| 1552 |
address->token = ipv6_token_ref(token); |
| 1553 |
|
| 1554 |
r = ndisc_address_set_lifetime(address, link, rt); |
| 1555 |
if (r < 0) |
| 1556 |
return log_link_warning_errno(link, r, "Failed to set lifetime of SLAAC address: %m"); |
| 1557 |
|
| 1558 |
assert(address->lifetime_preferred_usec <= address->lifetime_valid_usec); |
| 1559 |
|
| 1560 |
r = ndisc_request_address(address, link); |
| 1561 |
if (r < 0) |
| 1562 |
return log_link_warning_errno(link, r, "Could not request SLAAC address: %m"); |
| 1563 |
} |
| 1564 |
|
| 1565 |
return 0; |
| 1566 |
} |
| 1567 |
|
| 1568 |
static int ndisc_router_process_onlink_prefix(Link *link, sd_ndisc_router *rt) { |
| 1569 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 1570 |
uint8_t prefixlen, preference; |
| 1571 |
usec_t lifetime_usec; |
| 1572 |
struct in6_addr prefix; |
| 1573 |
int r; |
| 1574 |
|
| 1575 |
assert(link); |
| 1576 |
assert(link->network); |
| 1577 |
assert(rt); |
| 1578 |
|
| 1579 |
if (!link->network->ndisc_use_onlink_prefix) |
| 1580 |
return 0; |
| 1581 |
|
| 1582 |
r = sd_ndisc_router_prefix_get_valid_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec); |
| 1583 |
if (r < 0) |
| 1584 |
return log_link_warning_errno(link, r, "Failed to get prefix lifetime: %m"); |
| 1585 |
|
| 1586 |
r = sd_ndisc_router_prefix_get_address(rt, &prefix); |
| 1587 |
if (r < 0) |
| 1588 |
return log_link_warning_errno(link, r, "Failed to get prefix address: %m"); |
| 1589 |
|
| 1590 |
r = sd_ndisc_router_prefix_get_prefixlen(rt, &prefixlen); |
| 1591 |
if (r < 0) |
| 1592 |
return log_link_warning_errno(link, r, "Failed to get prefix length: %m"); |
| 1593 |
|
| 1594 |
/* Prefix Information option does not have preference, hence we use the 'main' preference here */ |
| 1595 |
r = sd_ndisc_router_get_preference(rt, &preference); |
| 1596 |
if (r < 0) |
| 1597 |
return log_link_warning_errno(link, r, "Failed to get router preference from RA: %m"); |
| 1598 |
|
| 1599 |
r = route_new(&route); |
| 1600 |
if (r < 0) |
| 1601 |
return log_oom(); |
| 1602 |
|
| 1603 |
route->family = AF_INET6; |
| 1604 |
route->dst.in6 = prefix; |
| 1605 |
route->dst_prefixlen = prefixlen; |
| 1606 |
route->pref = preference; |
| 1607 |
route->lifetime_usec = lifetime_usec; |
| 1608 |
|
| 1609 |
/* RFC 4861 section 6.3.4: |
| 1610 |
* - If the prefix is not already present in the Prefix List, and the Prefix Information option's |
| 1611 |
* Valid Lifetime field is non-zero, create a new entry for the prefix and initialize its |
| 1612 |
* invalidation timer to the Valid Lifetime value in the Prefix Information option. |
| 1613 |
* |
| 1614 |
* - If the prefix is already present in the host's Prefix List as the result of a previously |
| 1615 |
* received advertisement, reset its invalidation timer to the Valid Lifetime value in the Prefix |
| 1616 |
* Information option. If the new Lifetime value is zero, timeout the prefix immediately. */ |
| 1617 |
if (lifetime_usec == 0) { |
| 1618 |
r = ndisc_remove_router_route(route, link, rt); |
| 1619 |
if (r < 0) |
| 1620 |
return log_link_warning_errno(link, r, "Failed to remove prefix route: %m"); |
| 1621 |
} else { |
| 1622 |
r = ndisc_request_router_route(route, link, rt); |
| 1623 |
if (r < 0) |
| 1624 |
return log_link_warning_errno(link, r, "Failed to request prefix route: %m"); |
| 1625 |
} |
| 1626 |
|
| 1627 |
return 0; |
| 1628 |
} |
| 1629 |
|
| 1630 |
static int ndisc_router_process_prefix(Link *link, sd_ndisc_router *rt, bool zero_lifetime) { |
| 1631 |
uint8_t flags, prefixlen; |
| 1632 |
struct in6_addr a; |
| 1633 |
int r; |
| 1634 |
|
| 1635 |
assert(link); |
| 1636 |
assert(link->network); |
| 1637 |
assert(rt); |
| 1638 |
|
| 1639 |
usec_t lifetime_usec; |
| 1640 |
r = sd_ndisc_router_prefix_get_valid_lifetime(rt, &lifetime_usec); |
| 1641 |
if (r < 0) |
| 1642 |
return log_link_warning_errno(link, r, "Failed to get prefix lifetime: %m"); |
| 1643 |
|
| 1644 |
if ((lifetime_usec == 0) != zero_lifetime) |
| 1645 |
return 0; |
| 1646 |
|
| 1647 |
r = sd_ndisc_router_prefix_get_address(rt, &a); |
| 1648 |
if (r < 0) |
| 1649 |
return log_link_warning_errno(link, r, "Failed to get prefix address: %m"); |
| 1650 |
|
| 1651 |
/* RFC 4861 Section 4.6.2: |
| 1652 |
* A router SHOULD NOT send a prefix option for the link-local prefix and a host SHOULD ignore such |
| 1653 |
* a prefix option. */ |
| 1654 |
if (in6_addr_is_link_local(&a)) { |
| 1655 |
log_link_debug(link, "Received link-local prefix, ignoring prefix."); |
| 1656 |
return 0; |
| 1657 |
} |
| 1658 |
|
| 1659 |
r = sd_ndisc_router_prefix_get_prefixlen(rt, &prefixlen); |
| 1660 |
if (r < 0) |
| 1661 |
return log_link_warning_errno(link, r, "Failed to get prefix length: %m"); |
| 1662 |
|
| 1663 |
if (in6_prefix_is_filtered(&a, prefixlen, link->network->ndisc_allow_listed_prefix, link->network->ndisc_deny_listed_prefix)) { |
| 1664 |
if (set_isempty(link->network->ndisc_allow_listed_prefix)) |
| 1665 |
log_link_debug(link, "Prefix '%s' is in deny list, ignoring.", |
| 1666 |
IN6_ADDR_PREFIX_TO_STRING(&a, prefixlen)); |
| 1667 |
else |
| 1668 |
log_link_debug(link, "Prefix '%s' is not in allow list, ignoring.", |
| 1669 |
IN6_ADDR_PREFIX_TO_STRING(&a, prefixlen)); |
| 1670 |
return 0; |
| 1671 |
} |
| 1672 |
|
| 1673 |
r = sd_ndisc_router_prefix_get_flags(rt, &flags); |
| 1674 |
if (r < 0) |
| 1675 |
return log_link_warning_errno(link, r, "Failed to get RA prefix flags: %m"); |
| 1676 |
|
| 1677 |
if (FLAGS_SET(flags, ND_OPT_PI_FLAG_ONLINK)) { |
| 1678 |
r = ndisc_router_process_onlink_prefix(link, rt); |
| 1679 |
if (r < 0) |
| 1680 |
return r; |
| 1681 |
} |
| 1682 |
|
| 1683 |
if (FLAGS_SET(flags, ND_OPT_PI_FLAG_AUTO)) { |
| 1684 |
r = ndisc_router_process_autonomous_prefix(link, rt); |
| 1685 |
if (r < 0) |
| 1686 |
return r; |
| 1687 |
} |
| 1688 |
|
| 1689 |
return 0; |
| 1690 |
} |
| 1691 |
|
| 1692 |
static int ndisc_router_process_route(Link *link, sd_ndisc_router *rt, bool zero_lifetime) { |
| 1693 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 1694 |
uint8_t preference, prefixlen; |
| 1695 |
struct in6_addr gateway, dst; |
| 1696 |
usec_t lifetime_usec; |
| 1697 |
int r; |
| 1698 |
|
| 1699 |
assert(link); |
| 1700 |
|
| 1701 |
if (!link->network->ndisc_use_route_prefix) |
| 1702 |
return 0; |
| 1703 |
|
| 1704 |
r = sd_ndisc_router_route_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec); |
| 1705 |
if (r < 0) |
| 1706 |
return log_link_warning_errno(link, r, "Failed to get route lifetime from RA: %m"); |
| 1707 |
|
| 1708 |
if ((lifetime_usec == 0) != zero_lifetime) |
| 1709 |
return 0; |
| 1710 |
|
| 1711 |
r = sd_ndisc_router_route_get_address(rt, &dst); |
| 1712 |
if (r < 0) |
| 1713 |
return log_link_warning_errno(link, r, "Failed to get route destination address: %m"); |
| 1714 |
|
| 1715 |
r = sd_ndisc_router_route_get_prefixlen(rt, &prefixlen); |
| 1716 |
if (r < 0) |
| 1717 |
return log_link_warning_errno(link, r, "Failed to get route prefix length: %m"); |
| 1718 |
|
| 1719 |
if (in6_prefix_is_filtered(&dst, prefixlen, |
| 1720 |
link->network->ndisc_allow_listed_route_prefix, |
| 1721 |
link->network->ndisc_deny_listed_route_prefix)) { |
| 1722 |
if (set_isempty(link->network->ndisc_allow_listed_route_prefix)) |
| 1723 |
log_link_debug(link, "Route prefix '%s' is in deny list, ignoring.", |
| 1724 |
IN6_ADDR_PREFIX_TO_STRING(&dst, prefixlen)); |
| 1725 |
else |
| 1726 |
log_link_debug(link, "Route prefix '%s' is not in allow list, ignoring.", |
| 1727 |
IN6_ADDR_PREFIX_TO_STRING(&dst, prefixlen)); |
| 1728 |
return 0; |
| 1729 |
} |
| 1730 |
|
| 1731 |
r = sd_ndisc_router_get_sender_address(rt, &gateway); |
| 1732 |
if (r < 0) |
| 1733 |
return log_link_warning_errno(link, r, "Failed to get gateway address from RA: %m"); |
| 1734 |
|
| 1735 |
if (link_get_ipv6_address(link, &gateway, NULL) >= 0) { |
| 1736 |
if (DEBUG_LOGGING) |
| 1737 |
log_link_debug(link, "Advertised route gateway %s is local to the link, ignoring route", |
| 1738 |
IN6_ADDR_TO_STRING(&gateway)); |
| 1739 |
return 0; |
| 1740 |
} |
| 1741 |
|
| 1742 |
r = sd_ndisc_router_route_get_preference(rt, &preference); |
| 1743 |
if (r < 0) |
| 1744 |
return log_link_warning_errno(link, r, "Failed to get router preference from RA: %m"); |
| 1745 |
|
| 1746 |
r = route_new(&route); |
| 1747 |
if (r < 0) |
| 1748 |
return log_oom(); |
| 1749 |
|
| 1750 |
route->family = AF_INET6; |
| 1751 |
route->pref = preference; |
| 1752 |
route->nexthop.gw.in6 = gateway; |
| 1753 |
route->nexthop.family = AF_INET6; |
| 1754 |
route->dst.in6 = dst; |
| 1755 |
route->dst_prefixlen = prefixlen; |
| 1756 |
route->lifetime_usec = lifetime_usec; |
| 1757 |
|
| 1758 |
if (lifetime_usec != 0) { |
| 1759 |
r = ndisc_request_router_route(route, link, rt); |
| 1760 |
if (r < 0) |
| 1761 |
return log_link_warning_errno(link, r, "Could not request additional route: %m"); |
| 1762 |
} else { |
| 1763 |
r = ndisc_remove_router_route(route, link, rt); |
| 1764 |
if (r < 0) |
| 1765 |
return log_link_warning_errno(link, r, "Could not remove additional route with zero lifetime: %m"); |
| 1766 |
} |
| 1767 |
|
| 1768 |
return 0; |
| 1769 |
} |
| 1770 |
|
| 1771 |
static void ndisc_rdnss_hash_func(const NDiscRDNSS *x, struct siphash *state) { |
| 1772 |
siphash24_compress_typesafe(x->address, state); |
| 1773 |
} |
| 1774 |
|
| 1775 |
static int ndisc_rdnss_compare_func(const NDiscRDNSS *a, const NDiscRDNSS *b) { |
| 1776 |
return memcmp(&a->address, &b->address, sizeof(a->address)); |
| 1777 |
} |
| 1778 |
|
| 1779 |
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR( |
| 1780 |
ndisc_rdnss_hash_ops, |
| 1781 |
NDiscRDNSS, |
| 1782 |
ndisc_rdnss_hash_func, |
| 1783 |
ndisc_rdnss_compare_func, |
| 1784 |
free); |
| 1785 |
|
| 1786 |
static int ndisc_router_process_rdnss(Link *link, sd_ndisc_router *rt, bool zero_lifetime) { |
| 1787 |
usec_t lifetime_usec; |
| 1788 |
const struct in6_addr *a; |
| 1789 |
struct in6_addr router; |
| 1790 |
bool updated = false, logged_about_too_many = false; |
| 1791 |
int n, r; |
| 1792 |
|
| 1793 |
assert(link); |
| 1794 |
assert(link->network); |
| 1795 |
assert(rt); |
| 1796 |
|
| 1797 |
if (!link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC)) |
| 1798 |
return 0; |
| 1799 |
|
| 1800 |
r = sd_ndisc_router_get_sender_address(rt, &router); |
| 1801 |
if (r < 0) |
| 1802 |
return log_link_warning_errno(link, r, "Failed to get router address from RA: %m"); |
| 1803 |
|
| 1804 |
r = sd_ndisc_router_rdnss_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec); |
| 1805 |
if (r < 0) |
| 1806 |
return log_link_warning_errno(link, r, "Failed to get RDNSS lifetime: %m"); |
| 1807 |
|
| 1808 |
if ((lifetime_usec == 0) != zero_lifetime) |
| 1809 |
return 0; |
| 1810 |
|
| 1811 |
n = sd_ndisc_router_rdnss_get_addresses(rt, &a); |
| 1812 |
if (n < 0) |
| 1813 |
return log_link_warning_errno(link, n, "Failed to get RDNSS addresses: %m"); |
| 1814 |
|
| 1815 |
for (int j = 0; j < n; j++) { |
| 1816 |
_cleanup_free_ NDiscRDNSS *x = NULL; |
| 1817 |
NDiscRDNSS *rdnss, d = { |
| 1818 |
.address = a[j], |
| 1819 |
}; |
| 1820 |
|
| 1821 |
if (lifetime_usec == 0) { |
| 1822 |
/* The entry is outdated. */ |
| 1823 |
free(set_remove(link->ndisc_rdnss, &d)); |
| 1824 |
updated = true; |
| 1825 |
continue; |
| 1826 |
} |
| 1827 |
|
| 1828 |
rdnss = set_get(link->ndisc_rdnss, &d); |
| 1829 |
if (rdnss) { |
| 1830 |
rdnss->router = router; |
| 1831 |
rdnss->lifetime_usec = lifetime_usec; |
| 1832 |
continue; |
| 1833 |
} |
| 1834 |
|
| 1835 |
if (set_size(link->ndisc_rdnss) >= NDISC_RDNSS_MAX) { |
| 1836 |
if (!logged_about_too_many) |
| 1837 |
log_link_warning(link, "Too many RDNSS records per link. Only first %u records will be used.", NDISC_RDNSS_MAX); |
| 1838 |
logged_about_too_many = true; |
| 1839 |
continue; |
| 1840 |
} |
| 1841 |
|
| 1842 |
x = new(NDiscRDNSS, 1); |
| 1843 |
if (!x) |
| 1844 |
return log_oom(); |
| 1845 |
|
| 1846 |
*x = (NDiscRDNSS) { |
| 1847 |
.address = a[j], |
| 1848 |
.router = router, |
| 1849 |
.lifetime_usec = lifetime_usec, |
| 1850 |
}; |
| 1851 |
|
| 1852 |
r = set_ensure_consume(&link->ndisc_rdnss, &ndisc_rdnss_hash_ops, TAKE_PTR(x)); |
| 1853 |
if (r < 0) |
| 1854 |
return log_oom(); |
| 1855 |
assert(r > 0); |
| 1856 |
|
| 1857 |
updated = true; |
| 1858 |
} |
| 1859 |
|
| 1860 |
if (updated) |
| 1861 |
link_dirty(link); |
| 1862 |
|
| 1863 |
return 0; |
| 1864 |
} |
| 1865 |
|
| 1866 |
static void ndisc_dnssl_hash_func(const NDiscDNSSL *x, struct siphash *state) { |
| 1867 |
siphash24_compress_string(ndisc_dnssl_domain(x), state); |
| 1868 |
} |
| 1869 |
|
| 1870 |
static int ndisc_dnssl_compare_func(const NDiscDNSSL *a, const NDiscDNSSL *b) { |
| 1871 |
return strcmp(ndisc_dnssl_domain(a), ndisc_dnssl_domain(b)); |
| 1872 |
} |
| 1873 |
|
| 1874 |
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR( |
| 1875 |
ndisc_dnssl_hash_ops, |
| 1876 |
NDiscDNSSL, |
| 1877 |
ndisc_dnssl_hash_func, |
| 1878 |
ndisc_dnssl_compare_func, |
| 1879 |
free); |
| 1880 |
|
| 1881 |
static int ndisc_router_process_dnssl(Link *link, sd_ndisc_router *rt, bool zero_lifetime) { |
| 1882 |
char **l; |
| 1883 |
usec_t lifetime_usec; |
| 1884 |
struct in6_addr router; |
| 1885 |
bool updated = false, logged_about_too_many = false; |
| 1886 |
int r; |
| 1887 |
|
| 1888 |
assert(link); |
| 1889 |
assert(link->network); |
| 1890 |
assert(rt); |
| 1891 |
|
| 1892 |
if (link_get_use_domains(link, NETWORK_CONFIG_SOURCE_NDISC) <= 0) |
| 1893 |
return 0; |
| 1894 |
|
| 1895 |
r = sd_ndisc_router_get_sender_address(rt, &router); |
| 1896 |
if (r < 0) |
| 1897 |
return log_link_warning_errno(link, r, "Failed to get router address from RA: %m"); |
| 1898 |
|
| 1899 |
r = sd_ndisc_router_dnssl_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec); |
| 1900 |
if (r < 0) |
| 1901 |
return log_link_warning_errno(link, r, "Failed to get DNSSL lifetime: %m"); |
| 1902 |
|
| 1903 |
if ((lifetime_usec == 0) != zero_lifetime) |
| 1904 |
return 0; |
| 1905 |
|
| 1906 |
r = sd_ndisc_router_dnssl_get_domains(rt, &l); |
| 1907 |
if (r < 0) |
| 1908 |
return log_link_warning_errno(link, r, "Failed to get DNSSL addresses: %m"); |
| 1909 |
|
| 1910 |
STRV_FOREACH(j, l) { |
| 1911 |
_cleanup_free_ NDiscDNSSL *s = NULL; |
| 1912 |
NDiscDNSSL *dnssl; |
| 1913 |
|
| 1914 |
/* Silence static analyzers */ |
| 1915 |
assert(strlen(*j) <= SIZE_MAX - ALIGN(sizeof(NDiscDNSSL)) - 1); |
| 1916 |
s = malloc0(ALIGN(sizeof(NDiscDNSSL)) + strlen(*j) + 1); |
| 1917 |
if (!s) |
| 1918 |
return log_oom(); |
| 1919 |
|
| 1920 |
strcpy(ndisc_dnssl_domain(s), *j); |
| 1921 |
|
| 1922 |
if (lifetime_usec == 0) { |
| 1923 |
/* The entry is outdated. */ |
| 1924 |
free(set_remove(link->ndisc_dnssl, s)); |
| 1925 |
updated = true; |
| 1926 |
continue; |
| 1927 |
} |
| 1928 |
|
| 1929 |
dnssl = set_get(link->ndisc_dnssl, s); |
| 1930 |
if (dnssl) { |
| 1931 |
dnssl->router = router; |
| 1932 |
dnssl->lifetime_usec = lifetime_usec; |
| 1933 |
continue; |
| 1934 |
} |
| 1935 |
|
| 1936 |
if (set_size(link->ndisc_dnssl) >= NDISC_DNSSL_MAX) { |
| 1937 |
if (!logged_about_too_many) |
| 1938 |
log_link_warning(link, "Too many DNSSL records per link. Only first %u records will be used.", NDISC_DNSSL_MAX); |
| 1939 |
logged_about_too_many = true; |
| 1940 |
continue; |
| 1941 |
} |
| 1942 |
|
| 1943 |
s->router = router; |
| 1944 |
s->lifetime_usec = lifetime_usec; |
| 1945 |
|
| 1946 |
r = set_ensure_consume(&link->ndisc_dnssl, &ndisc_dnssl_hash_ops, TAKE_PTR(s)); |
| 1947 |
if (r < 0) |
| 1948 |
return log_oom(); |
| 1949 |
assert(r > 0); |
| 1950 |
|
| 1951 |
updated = true; |
| 1952 |
} |
| 1953 |
|
| 1954 |
if (updated) |
| 1955 |
link_dirty(link); |
| 1956 |
|
| 1957 |
return 0; |
| 1958 |
} |
| 1959 |
|
| 1960 |
static NDiscCaptivePortal* ndisc_captive_portal_free(NDiscCaptivePortal *x) { |
| 1961 |
if (!x) |
| 1962 |
return NULL; |
| 1963 |
|
| 1964 |
free(x->captive_portal); |
| 1965 |
return mfree(x); |
| 1966 |
} |
| 1967 |
|
| 1968 |
DEFINE_TRIVIAL_CLEANUP_FUNC(NDiscCaptivePortal*, ndisc_captive_portal_free); |
| 1969 |
|
| 1970 |
static void ndisc_captive_portal_hash_func(const NDiscCaptivePortal *x, struct siphash *state) { |
| 1971 |
assert(x); |
| 1972 |
siphash24_compress_string(x->captive_portal, state); |
| 1973 |
} |
| 1974 |
|
| 1975 |
static int ndisc_captive_portal_compare_func(const NDiscCaptivePortal *a, const NDiscCaptivePortal *b) { |
| 1976 |
assert(a); |
| 1977 |
assert(b); |
| 1978 |
return strcmp_ptr(a->captive_portal, b->captive_portal); |
| 1979 |
} |
| 1980 |
|
| 1981 |
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR( |
| 1982 |
ndisc_captive_portal_hash_ops, |
| 1983 |
NDiscCaptivePortal, |
| 1984 |
ndisc_captive_portal_hash_func, |
| 1985 |
ndisc_captive_portal_compare_func, |
| 1986 |
ndisc_captive_portal_free); |
| 1987 |
|
| 1988 |
static int ndisc_router_process_captive_portal(Link *link, sd_ndisc_router *rt, bool zero_lifetime) { |
| 1989 |
_cleanup_(ndisc_captive_portal_freep) NDiscCaptivePortal *new_entry = NULL; |
| 1990 |
_cleanup_free_ char *captive_portal = NULL; |
| 1991 |
const char *uri; |
| 1992 |
usec_t lifetime_usec; |
| 1993 |
NDiscCaptivePortal *exist; |
| 1994 |
struct in6_addr router; |
| 1995 |
int r; |
| 1996 |
|
| 1997 |
assert(link); |
| 1998 |
assert(link->network); |
| 1999 |
assert(rt); |
| 2000 |
|
| 2001 |
if (!link->network->ndisc_use_captive_portal) |
| 2002 |
return 0; |
| 2003 |
|
| 2004 |
r = sd_ndisc_router_get_sender_address(rt, &router); |
| 2005 |
if (r < 0) |
| 2006 |
return log_link_warning_errno(link, r, "Failed to get router address from RA: %m"); |
| 2007 |
|
| 2008 |
/* RFC 4861 section 4.2. states that the lifetime in the message header should be used only for the |
| 2009 |
* default gateway, but the captive portal option does not have a lifetime field, hence, we use the |
| 2010 |
* main lifetime for the portal. */ |
| 2011 |
r = sd_ndisc_router_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec); |
| 2012 |
if (r < 0) |
| 2013 |
return log_link_warning_errno(link, r, "Failed to get lifetime of RA message: %m"); |
| 2014 |
|
| 2015 |
if ((lifetime_usec == 0) != zero_lifetime) |
| 2016 |
return 0; |
| 2017 |
|
| 2018 |
r = sd_ndisc_router_get_captive_portal(rt, &uri); |
| 2019 |
if (r < 0) |
| 2020 |
return log_link_warning_errno(link, r, "Failed to get captive portal from RA: %m"); |
| 2021 |
|
| 2022 |
captive_portal = strdup(uri); |
| 2023 |
if (!captive_portal) |
| 2024 |
return log_oom(); |
| 2025 |
|
| 2026 |
if (lifetime_usec == 0) { |
| 2027 |
/* Drop the portal with zero lifetime. */ |
| 2028 |
ndisc_captive_portal_free(set_remove(link->ndisc_captive_portals, |
| 2029 |
&(const NDiscCaptivePortal) { |
| 2030 |
.captive_portal = captive_portal, |
| 2031 |
})); |
| 2032 |
return 0; |
| 2033 |
} |
| 2034 |
|
| 2035 |
exist = set_get(link->ndisc_captive_portals, |
| 2036 |
&(const NDiscCaptivePortal) { |
| 2037 |
.captive_portal = captive_portal, |
| 2038 |
}); |
| 2039 |
if (exist) { |
| 2040 |
/* update existing entry */ |
| 2041 |
exist->router = router; |
| 2042 |
exist->lifetime_usec = lifetime_usec; |
| 2043 |
return 1; |
| 2044 |
} |
| 2045 |
|
| 2046 |
if (set_size(link->ndisc_captive_portals) >= NDISC_CAPTIVE_PORTAL_MAX) { |
| 2047 |
NDiscCaptivePortal *c, *target = NULL; |
| 2048 |
|
| 2049 |
/* Find the portal who has the minimal lifetime and drop it to store new one. */ |
| 2050 |
SET_FOREACH(c, link->ndisc_captive_portals) |
| 2051 |
if (!target || c->lifetime_usec < target->lifetime_usec) |
| 2052 |
target = c; |
| 2053 |
|
| 2054 |
assert(target); |
| 2055 |
assert(set_remove(link->ndisc_captive_portals, target) == target); |
| 2056 |
ndisc_captive_portal_free(target); |
| 2057 |
} |
| 2058 |
|
| 2059 |
new_entry = new(NDiscCaptivePortal, 1); |
| 2060 |
if (!new_entry) |
| 2061 |
return log_oom(); |
| 2062 |
|
| 2063 |
*new_entry = (NDiscCaptivePortal) { |
| 2064 |
.router = router, |
| 2065 |
.lifetime_usec = lifetime_usec, |
| 2066 |
.captive_portal = TAKE_PTR(captive_portal), |
| 2067 |
}; |
| 2068 |
|
| 2069 |
r = set_ensure_put(&link->ndisc_captive_portals, &ndisc_captive_portal_hash_ops, new_entry); |
| 2070 |
if (r < 0) |
| 2071 |
return log_oom(); |
| 2072 |
assert(r > 0); |
| 2073 |
TAKE_PTR(new_entry); |
| 2074 |
|
| 2075 |
link_dirty(link); |
| 2076 |
return 1; |
| 2077 |
} |
| 2078 |
|
| 2079 |
static void ndisc_pref64_hash_func(const NDiscPREF64 *x, struct siphash *state) { |
| 2080 |
assert(x); |
| 2081 |
|
| 2082 |
siphash24_compress_typesafe(x->prefix_len, state); |
| 2083 |
siphash24_compress_typesafe(x->prefix, state); |
| 2084 |
} |
| 2085 |
|
| 2086 |
static int ndisc_pref64_compare_func(const NDiscPREF64 *a, const NDiscPREF64 *b) { |
| 2087 |
int r; |
| 2088 |
|
| 2089 |
assert(a); |
| 2090 |
assert(b); |
| 2091 |
|
| 2092 |
r = CMP(a->prefix_len, b->prefix_len); |
| 2093 |
if (r != 0) |
| 2094 |
return r; |
| 2095 |
|
| 2096 |
return memcmp(&a->prefix, &b->prefix, sizeof(a->prefix)); |
| 2097 |
} |
| 2098 |
|
| 2099 |
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR( |
| 2100 |
ndisc_pref64_hash_ops, |
| 2101 |
NDiscPREF64, |
| 2102 |
ndisc_pref64_hash_func, |
| 2103 |
ndisc_pref64_compare_func, |
| 2104 |
mfree); |
| 2105 |
|
| 2106 |
static int ndisc_router_process_pref64(Link *link, sd_ndisc_router *rt, bool zero_lifetime) { |
| 2107 |
_cleanup_free_ NDiscPREF64 *new_entry = NULL; |
| 2108 |
usec_t lifetime_usec; |
| 2109 |
struct in6_addr a, router; |
| 2110 |
uint8_t prefix_len; |
| 2111 |
NDiscPREF64 *exist; |
| 2112 |
int r; |
| 2113 |
|
| 2114 |
assert(link); |
| 2115 |
assert(link->network); |
| 2116 |
assert(rt); |
| 2117 |
|
| 2118 |
if (!link->network->ndisc_use_pref64) |
| 2119 |
return 0; |
| 2120 |
|
| 2121 |
r = sd_ndisc_router_get_sender_address(rt, &router); |
| 2122 |
if (r < 0) |
| 2123 |
return log_link_warning_errno(link, r, "Failed to get router address from RA: %m"); |
| 2124 |
|
| 2125 |
r = sd_ndisc_router_prefix64_get_prefix(rt, &a); |
| 2126 |
if (r < 0) |
| 2127 |
return log_link_warning_errno(link, r, "Failed to get pref64 prefix: %m"); |
| 2128 |
|
| 2129 |
r = sd_ndisc_router_prefix64_get_prefixlen(rt, &prefix_len); |
| 2130 |
if (r < 0) |
| 2131 |
return log_link_warning_errno(link, r, "Failed to get pref64 prefix length: %m"); |
| 2132 |
|
| 2133 |
r = sd_ndisc_router_prefix64_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec); |
| 2134 |
if (r < 0) |
| 2135 |
return log_link_warning_errno(link, r, "Failed to get pref64 prefix lifetime: %m"); |
| 2136 |
|
| 2137 |
if ((lifetime_usec == 0) != zero_lifetime) |
| 2138 |
return 0; |
| 2139 |
|
| 2140 |
if (lifetime_usec == 0) { |
| 2141 |
free(set_remove(link->ndisc_pref64, |
| 2142 |
&(NDiscPREF64) { |
| 2143 |
.prefix = a, |
| 2144 |
.prefix_len = prefix_len |
| 2145 |
})); |
| 2146 |
return 0; |
| 2147 |
} |
| 2148 |
|
| 2149 |
exist = set_get(link->ndisc_pref64, |
| 2150 |
&(NDiscPREF64) { |
| 2151 |
.prefix = a, |
| 2152 |
.prefix_len = prefix_len |
| 2153 |
}); |
| 2154 |
if (exist) { |
| 2155 |
/* update existing entry */ |
| 2156 |
exist->router = router; |
| 2157 |
exist->lifetime_usec = lifetime_usec; |
| 2158 |
return 0; |
| 2159 |
} |
| 2160 |
|
| 2161 |
if (set_size(link->ndisc_pref64) >= NDISC_PREF64_MAX) { |
| 2162 |
log_link_debug(link, "Too many PREF64 records received. Only first %u records will be used.", NDISC_PREF64_MAX); |
| 2163 |
return 0; |
| 2164 |
} |
| 2165 |
|
| 2166 |
new_entry = new(NDiscPREF64, 1); |
| 2167 |
if (!new_entry) |
| 2168 |
return log_oom(); |
| 2169 |
|
| 2170 |
*new_entry = (NDiscPREF64) { |
| 2171 |
.router = router, |
| 2172 |
.lifetime_usec = lifetime_usec, |
| 2173 |
.prefix = a, |
| 2174 |
.prefix_len = prefix_len, |
| 2175 |
}; |
| 2176 |
|
| 2177 |
r = set_ensure_put(&link->ndisc_pref64, &ndisc_pref64_hash_ops, new_entry); |
| 2178 |
if (r < 0) |
| 2179 |
return log_oom(); |
| 2180 |
|
| 2181 |
assert(r > 0); |
| 2182 |
TAKE_PTR(new_entry); |
| 2183 |
|
| 2184 |
return 0; |
| 2185 |
} |
| 2186 |
|
| 2187 |
static NDiscDNR* ndisc_dnr_free(NDiscDNR *x) { |
| 2188 |
if (!x) |
| 2189 |
return NULL; |
| 2190 |
|
| 2191 |
sd_dns_resolver_done(&x->resolver); |
| 2192 |
return mfree(x); |
| 2193 |
} |
| 2194 |
|
| 2195 |
DEFINE_TRIVIAL_CLEANUP_FUNC(NDiscDNR*, ndisc_dnr_free); |
| 2196 |
|
| 2197 |
static int ndisc_dnr_compare_func(const NDiscDNR *a, const NDiscDNR *b) { |
| 2198 |
return CMP(a->resolver.priority, b->resolver.priority) || |
| 2199 |
strcmp_ptr(a->resolver.auth_name, b->resolver.auth_name) || |
| 2200 |
CMP(a->resolver.transports, b->resolver.transports) || |
| 2201 |
CMP(a->resolver.port, b->resolver.port) || |
| 2202 |
strcmp_ptr(a->resolver.dohpath, b->resolver.dohpath) || |
| 2203 |
CMP(a->resolver.family, b->resolver.family) || |
| 2204 |
CMP(a->resolver.n_addrs, b->resolver.n_addrs) || |
| 2205 |
memcmp(a->resolver.addrs, b->resolver.addrs, sizeof(a->resolver.addrs[0]) * a->resolver.n_addrs) != 0; |
| 2206 |
} |
| 2207 |
|
| 2208 |
static void ndisc_dnr_hash_func(const NDiscDNR *x, struct siphash *state) { |
| 2209 |
assert(x); |
| 2210 |
|
| 2211 |
siphash24_compress_resolver(&x->resolver, state); |
| 2212 |
} |
| 2213 |
|
| 2214 |
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR( |
| 2215 |
ndisc_dnr_hash_ops, |
| 2216 |
NDiscDNR, |
| 2217 |
ndisc_dnr_hash_func, |
| 2218 |
ndisc_dnr_compare_func, |
| 2219 |
ndisc_dnr_free); |
| 2220 |
|
| 2221 |
static int sd_dns_resolver_copy(const sd_dns_resolver *a, sd_dns_resolver *b) { |
| 2222 |
int r; |
| 2223 |
|
| 2224 |
assert(a); |
| 2225 |
assert(b); |
| 2226 |
|
| 2227 |
_cleanup_(sd_dns_resolver_done) sd_dns_resolver c = { |
| 2228 |
.priority = a->priority, |
| 2229 |
.transports = a->transports, |
| 2230 |
.port = a->port, |
| 2231 |
/* .auth_name */ |
| 2232 |
.family = a->family, |
| 2233 |
/* .addrs */ |
| 2234 |
/* .n_addrs */ |
| 2235 |
/* .dohpath */ |
| 2236 |
}; |
| 2237 |
|
| 2238 |
/* auth_name */ |
| 2239 |
r = strdup_to(&c.auth_name, a->auth_name); |
| 2240 |
if (r < 0) |
| 2241 |
return r; |
| 2242 |
|
| 2243 |
/* addrs, n_addrs */ |
| 2244 |
c.addrs = newdup(union in_addr_union, a->addrs, a->n_addrs); |
| 2245 |
if (!c.addrs) |
| 2246 |
return -ENOMEM; |
| 2247 |
c.n_addrs = a->n_addrs; |
| 2248 |
|
| 2249 |
/* dohpath */ |
| 2250 |
r = strdup_to(&c.dohpath, a->dohpath); |
| 2251 |
if (r < 0) |
| 2252 |
return r; |
| 2253 |
|
| 2254 |
*b = TAKE_STRUCT(c); |
| 2255 |
return 0; |
| 2256 |
} |
| 2257 |
|
| 2258 |
static int ndisc_router_process_encrypted_dns(Link *link, sd_ndisc_router *rt, bool zero_lifetime) { |
| 2259 |
int r; |
| 2260 |
|
| 2261 |
assert(link); |
| 2262 |
assert(link->network); |
| 2263 |
assert(rt); |
| 2264 |
|
| 2265 |
struct in6_addr router; |
| 2266 |
usec_t lifetime_usec; |
| 2267 |
sd_dns_resolver *res; |
| 2268 |
_cleanup_(ndisc_dnr_freep) NDiscDNR *new_entry = NULL; |
| 2269 |
|
| 2270 |
if (!link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_NDISC)) |
| 2271 |
return 0; |
| 2272 |
|
| 2273 |
r = sd_ndisc_router_get_sender_address(rt, &router); |
| 2274 |
if (r < 0) |
| 2275 |
return log_link_warning_errno(link, r, "Failed to get gateway address from RA: %m"); |
| 2276 |
|
| 2277 |
r = sd_ndisc_router_encrypted_dns_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &lifetime_usec); |
| 2278 |
if (r < 0) |
| 2279 |
return log_link_warning_errno(link, r, "Failed to get lifetime of RA message: %m"); |
| 2280 |
|
| 2281 |
if ((lifetime_usec == 0) != zero_lifetime) |
| 2282 |
return 0; |
| 2283 |
|
| 2284 |
r = sd_ndisc_router_encrypted_dns_get_resolver(rt, &res); |
| 2285 |
if (r < 0) |
| 2286 |
return log_link_warning_errno(link, r, "Failed to get encrypted dns resolvers: %m"); |
| 2287 |
|
| 2288 |
NDiscDNR *dnr, d = { .resolver = *res }; |
| 2289 |
if (lifetime_usec == 0) { |
| 2290 |
dnr = set_remove(link->ndisc_dnr, &d); |
| 2291 |
if (dnr) { |
| 2292 |
ndisc_dnr_free(dnr); |
| 2293 |
link_dirty(link); |
| 2294 |
} |
| 2295 |
return 0; |
| 2296 |
} |
| 2297 |
|
| 2298 |
dnr = set_get(link->ndisc_dnr, &d); |
| 2299 |
if (dnr) { |
| 2300 |
dnr->router = router; |
| 2301 |
dnr->lifetime_usec = lifetime_usec; |
| 2302 |
return 0; |
| 2303 |
} |
| 2304 |
|
| 2305 |
if (set_size(link->ndisc_dnr) >= NDISC_ENCRYPTED_DNS_MAX) { |
| 2306 |
log_link_warning(link, "Too many Encrypted DNS records received. Only first %u records will be used.", NDISC_ENCRYPTED_DNS_MAX); |
| 2307 |
return 0; |
| 2308 |
} |
| 2309 |
|
| 2310 |
new_entry = new(NDiscDNR, 1); |
| 2311 |
if (!new_entry) |
| 2312 |
return log_oom(); |
| 2313 |
|
| 2314 |
*new_entry = (NDiscDNR) { |
| 2315 |
.router = router, |
| 2316 |
/* .resolver, */ |
| 2317 |
.lifetime_usec = lifetime_usec, |
| 2318 |
}; |
| 2319 |
r = sd_dns_resolver_copy(res, &new_entry->resolver); |
| 2320 |
if (r < 0) |
| 2321 |
return log_oom(); |
| 2322 |
|
| 2323 |
/* Not sorted by priority */ |
| 2324 |
r = set_ensure_put(&link->ndisc_dnr, &ndisc_dnr_hash_ops, new_entry); |
| 2325 |
if (r < 0) |
| 2326 |
return log_oom(); |
| 2327 |
|
| 2328 |
assert(r > 0); |
| 2329 |
TAKE_PTR(new_entry); |
| 2330 |
|
| 2331 |
link_dirty(link); |
| 2332 |
|
| 2333 |
return 0; |
| 2334 |
} |
| 2335 |
|
| 2336 |
static int ndisc_router_process_options(Link *link, sd_ndisc_router *rt, bool zero_lifetime) { |
| 2337 |
size_t n_captive_portal = 0; |
| 2338 |
int r; |
| 2339 |
|
| 2340 |
assert(link); |
| 2341 |
assert(link->network); |
| 2342 |
assert(rt); |
| 2343 |
|
| 2344 |
for (r = sd_ndisc_router_option_rewind(rt); ; r = sd_ndisc_router_option_next(rt)) { |
| 2345 |
uint8_t type; |
| 2346 |
|
| 2347 |
if (r < 0) |
| 2348 |
return log_link_warning_errno(link, r, "Failed to iterate through options: %m"); |
| 2349 |
if (r == 0) /* EOF */ |
| 2350 |
return 0; |
| 2351 |
|
| 2352 |
r = sd_ndisc_router_option_get_type(rt, &type); |
| 2353 |
if (r < 0) |
| 2354 |
return log_link_warning_errno(link, r, "Failed to get RA option type: %m"); |
| 2355 |
|
| 2356 |
switch (type) { |
| 2357 |
case SD_NDISC_OPTION_PREFIX_INFORMATION: |
| 2358 |
r = ndisc_router_process_prefix(link, rt, zero_lifetime); |
| 2359 |
break; |
| 2360 |
|
| 2361 |
case SD_NDISC_OPTION_ROUTE_INFORMATION: |
| 2362 |
r = ndisc_router_process_route(link, rt, zero_lifetime); |
| 2363 |
break; |
| 2364 |
|
| 2365 |
case SD_NDISC_OPTION_RDNSS: |
| 2366 |
r = ndisc_router_process_rdnss(link, rt, zero_lifetime); |
| 2367 |
break; |
| 2368 |
|
| 2369 |
case SD_NDISC_OPTION_DNSSL: |
| 2370 |
r = ndisc_router_process_dnssl(link, rt, zero_lifetime); |
| 2371 |
break; |
| 2372 |
case SD_NDISC_OPTION_CAPTIVE_PORTAL: |
| 2373 |
if (n_captive_portal > 0) { |
| 2374 |
if (n_captive_portal == 1) |
| 2375 |
log_link_notice(link, "Received RA with multiple captive portals, only using the first one."); |
| 2376 |
|
| 2377 |
n_captive_portal++; |
| 2378 |
continue; |
| 2379 |
} |
| 2380 |
r = ndisc_router_process_captive_portal(link, rt, zero_lifetime); |
| 2381 |
if (r > 0) |
| 2382 |
n_captive_portal++; |
| 2383 |
break; |
| 2384 |
case SD_NDISC_OPTION_PREF64: |
| 2385 |
r = ndisc_router_process_pref64(link, rt, zero_lifetime); |
| 2386 |
break; |
| 2387 |
case SD_NDISC_OPTION_ENCRYPTED_DNS: |
| 2388 |
r = ndisc_router_process_encrypted_dns(link, rt, zero_lifetime); |
| 2389 |
break; |
| 2390 |
} |
| 2391 |
if (r < 0 && r != -EBADMSG) |
| 2392 |
return r; |
| 2393 |
} |
| 2394 |
} |
| 2395 |
|
| 2396 |
static int ndisc_drop_outdated(Link *link, const struct in6_addr *router, usec_t timestamp_usec) { |
| 2397 |
bool updated = false; |
| 2398 |
NDiscDNSSL *dnssl; |
| 2399 |
NDiscRDNSS *rdnss; |
| 2400 |
NDiscCaptivePortal *cp; |
| 2401 |
NDiscPREF64 *p64; |
| 2402 |
NDiscDNR *dnr; |
| 2403 |
Address *address; |
| 2404 |
Route *route; |
| 2405 |
int r, ret = 0; |
| 2406 |
|
| 2407 |
assert(link); |
| 2408 |
assert(link->manager); |
| 2409 |
|
| 2410 |
/* If an address or friends is already assigned, but not valid anymore, then refuse to update it, |
| 2411 |
* and let's immediately remove it. |
| 2412 |
* See RFC4862, section 5.5.3.e. But the following logic is deviated from RFC4862 by honoring all |
| 2413 |
* valid lifetimes to improve the reaction of SLAAC to renumbering events. |
| 2414 |
* See draft-ietf-6man-slaac-renum-02, section 4.2. */ |
| 2415 |
|
| 2416 |
r = ndisc_drop_routers(link, router, timestamp_usec); |
| 2417 |
if (r < 0) |
| 2418 |
RET_GATHER(ret, log_link_warning_errno(link, r, "Failed to drop outdated default router, ignoring: %m")); |
| 2419 |
|
| 2420 |
SET_FOREACH(route, link->manager->routes) { |
| 2421 |
if (route->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 2422 |
continue; |
| 2423 |
|
| 2424 |
if (!route_is_bound_to_link(route, link)) |
| 2425 |
continue; |
| 2426 |
|
| 2427 |
if (route->protocol == RTPROT_REDIRECT) |
| 2428 |
continue; /* redirect route will be dropped by ndisc_drop_redirect(). */ |
| 2429 |
|
| 2430 |
if (route->lifetime_usec > timestamp_usec) |
| 2431 |
continue; /* the route is still valid */ |
| 2432 |
|
| 2433 |
if (router && !in6_addr_equal(&route->provider.in6, router)) |
| 2434 |
continue; |
| 2435 |
|
| 2436 |
r = route_remove_and_cancel(route, link->manager); |
| 2437 |
if (r < 0) |
| 2438 |
RET_GATHER(ret, log_link_warning_errno(link, r, "Failed to remove outdated SLAAC route, ignoring: %m")); |
| 2439 |
} |
| 2440 |
|
| 2441 |
RET_GATHER(ret, ndisc_remove_unused_nexthops(link)); |
| 2442 |
|
| 2443 |
SET_FOREACH(address, link->addresses) { |
| 2444 |
if (address->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 2445 |
continue; |
| 2446 |
|
| 2447 |
if (address->lifetime_valid_usec > timestamp_usec) |
| 2448 |
continue; /* the address is still valid */ |
| 2449 |
|
| 2450 |
if (router && !in6_addr_equal(&address->provider.in6, router)) |
| 2451 |
continue; |
| 2452 |
|
| 2453 |
r = address_remove_and_cancel(address, link); |
| 2454 |
if (r < 0) |
| 2455 |
RET_GATHER(ret, log_link_warning_errno(link, r, "Failed to remove outdated SLAAC address, ignoring: %m")); |
| 2456 |
} |
| 2457 |
|
| 2458 |
SET_FOREACH(rdnss, link->ndisc_rdnss) { |
| 2459 |
if (rdnss->lifetime_usec > timestamp_usec) |
| 2460 |
continue; /* the DNS server is still valid */ |
| 2461 |
|
| 2462 |
if (router && !in6_addr_equal(&rdnss->router, router)) |
| 2463 |
continue; |
| 2464 |
|
| 2465 |
free(set_remove(link->ndisc_rdnss, rdnss)); |
| 2466 |
updated = true; |
| 2467 |
} |
| 2468 |
|
| 2469 |
SET_FOREACH(dnssl, link->ndisc_dnssl) { |
| 2470 |
if (dnssl->lifetime_usec > timestamp_usec) |
| 2471 |
continue; /* the DNS domain is still valid */ |
| 2472 |
|
| 2473 |
if (router && !in6_addr_equal(&dnssl->router, router)) |
| 2474 |
continue; |
| 2475 |
|
| 2476 |
free(set_remove(link->ndisc_dnssl, dnssl)); |
| 2477 |
updated = true; |
| 2478 |
} |
| 2479 |
|
| 2480 |
SET_FOREACH(cp, link->ndisc_captive_portals) { |
| 2481 |
if (cp->lifetime_usec > timestamp_usec) |
| 2482 |
continue; /* the captive portal is still valid */ |
| 2483 |
|
| 2484 |
if (router && !in6_addr_equal(&cp->router, router)) |
| 2485 |
continue; |
| 2486 |
|
| 2487 |
ndisc_captive_portal_free(set_remove(link->ndisc_captive_portals, cp)); |
| 2488 |
updated = true; |
| 2489 |
} |
| 2490 |
|
| 2491 |
SET_FOREACH(p64, link->ndisc_pref64) { |
| 2492 |
if (p64->lifetime_usec > timestamp_usec) |
| 2493 |
continue; /* the pref64 prefix is still valid */ |
| 2494 |
|
| 2495 |
if (router && !in6_addr_equal(&p64->router, router)) |
| 2496 |
continue; |
| 2497 |
|
| 2498 |
free(set_remove(link->ndisc_pref64, p64)); |
| 2499 |
/* The pref64 prefix is not exported through the state file, hence it is not necessary to set |
| 2500 |
* the 'updated' flag. */ |
| 2501 |
} |
| 2502 |
|
| 2503 |
SET_FOREACH(dnr, link->ndisc_dnr) { |
| 2504 |
if (dnr->lifetime_usec > timestamp_usec) |
| 2505 |
continue; /* The resolver is still valid */ |
| 2506 |
|
| 2507 |
ndisc_dnr_free(set_remove(link->ndisc_dnr, dnr)); |
| 2508 |
updated = true; |
| 2509 |
} |
| 2510 |
|
| 2511 |
RET_GATHER(ret, link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_SLAAC)); |
| 2512 |
|
| 2513 |
if (updated) |
| 2514 |
link_dirty(link); |
| 2515 |
|
| 2516 |
return ret; |
| 2517 |
} |
| 2518 |
|
| 2519 |
static int ndisc_setup_expire(Link *link); |
| 2520 |
|
| 2521 |
static int ndisc_expire_handler(sd_event_source *s, uint64_t usec, void *userdata) { |
| 2522 |
Link *link = ASSERT_PTR(userdata); |
| 2523 |
usec_t now_usec; |
| 2524 |
|
| 2525 |
assert(link->manager); |
| 2526 |
|
| 2527 |
assert_se(sd_event_now(link->manager->event, CLOCK_BOOTTIME, &now_usec) >= 0); |
| 2528 |
|
| 2529 |
(void) ndisc_drop_outdated(link, /* router= */ NULL, now_usec); |
| 2530 |
(void) ndisc_setup_expire(link); |
| 2531 |
return 0; |
| 2532 |
} |
| 2533 |
|
| 2534 |
static int ndisc_setup_expire(Link *link) { |
| 2535 |
usec_t lifetime_usec = USEC_INFINITY; |
| 2536 |
NDiscCaptivePortal *cp; |
| 2537 |
NDiscDNSSL *dnssl; |
| 2538 |
NDiscRDNSS *rdnss; |
| 2539 |
NDiscPREF64 *p64; |
| 2540 |
NDiscDNR *dnr; |
| 2541 |
Address *address; |
| 2542 |
Route *route; |
| 2543 |
int r; |
| 2544 |
|
| 2545 |
assert(link); |
| 2546 |
assert(link->manager); |
| 2547 |
|
| 2548 |
sd_ndisc_router *rt; |
| 2549 |
HASHMAP_FOREACH(rt, link->ndisc_routers_by_sender) { |
| 2550 |
usec_t t; |
| 2551 |
|
| 2552 |
if (sd_ndisc_router_get_lifetime_timestamp(rt, CLOCK_BOOTTIME, &t) < 0) |
| 2553 |
continue; |
| 2554 |
|
| 2555 |
lifetime_usec = MIN(lifetime_usec, t); |
| 2556 |
} |
| 2557 |
|
| 2558 |
SET_FOREACH(route, link->manager->routes) { |
| 2559 |
if (route->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 2560 |
continue; |
| 2561 |
|
| 2562 |
if (!route_is_bound_to_link(route, link)) |
| 2563 |
continue; |
| 2564 |
|
| 2565 |
if (!route_exists(route)) |
| 2566 |
continue; |
| 2567 |
|
| 2568 |
lifetime_usec = MIN(lifetime_usec, route->lifetime_usec); |
| 2569 |
} |
| 2570 |
|
| 2571 |
SET_FOREACH(address, link->addresses) { |
| 2572 |
if (address->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 2573 |
continue; |
| 2574 |
|
| 2575 |
if (!address_exists(address)) |
| 2576 |
continue; |
| 2577 |
|
| 2578 |
lifetime_usec = MIN(lifetime_usec, address->lifetime_valid_usec); |
| 2579 |
} |
| 2580 |
|
| 2581 |
SET_FOREACH(rdnss, link->ndisc_rdnss) |
| 2582 |
lifetime_usec = MIN(lifetime_usec, rdnss->lifetime_usec); |
| 2583 |
|
| 2584 |
SET_FOREACH(dnssl, link->ndisc_dnssl) |
| 2585 |
lifetime_usec = MIN(lifetime_usec, dnssl->lifetime_usec); |
| 2586 |
|
| 2587 |
SET_FOREACH(cp, link->ndisc_captive_portals) |
| 2588 |
lifetime_usec = MIN(lifetime_usec, cp->lifetime_usec); |
| 2589 |
|
| 2590 |
SET_FOREACH(p64, link->ndisc_pref64) |
| 2591 |
lifetime_usec = MIN(lifetime_usec, p64->lifetime_usec); |
| 2592 |
|
| 2593 |
SET_FOREACH(dnr, link->ndisc_dnr) |
| 2594 |
lifetime_usec = MIN(lifetime_usec, dnr->lifetime_usec); |
| 2595 |
|
| 2596 |
if (lifetime_usec == USEC_INFINITY) |
| 2597 |
return 0; |
| 2598 |
|
| 2599 |
r = event_reset_time(link->manager->event, &link->ndisc_expire, CLOCK_BOOTTIME, |
| 2600 |
lifetime_usec, 0, ndisc_expire_handler, link, 0, "ndisc-expiration", true); |
| 2601 |
if (r < 0) |
| 2602 |
return log_link_warning_errno(link, r, "Failed to update expiration timer for ndisc: %m"); |
| 2603 |
|
| 2604 |
return 0; |
| 2605 |
} |
| 2606 |
|
| 2607 |
static int ndisc_start_dhcp6_client(Link *link, sd_ndisc_router *rt) { |
| 2608 |
int r; |
| 2609 |
|
| 2610 |
assert(link); |
| 2611 |
assert(link->network); |
| 2612 |
|
| 2613 |
switch (link->network->ndisc_start_dhcp6_client) { |
| 2614 |
case IPV6_ACCEPT_RA_START_DHCP6_CLIENT_NO: |
| 2615 |
return 0; |
| 2616 |
|
| 2617 |
case IPV6_ACCEPT_RA_START_DHCP6_CLIENT_YES: { |
| 2618 |
uint64_t flags; |
| 2619 |
|
| 2620 |
r = sd_ndisc_router_get_flags(rt, &flags); |
| 2621 |
if (r < 0) |
| 2622 |
return log_link_warning_errno(link, r, "Failed to get RA flags: %m"); |
| 2623 |
|
| 2624 |
if ((flags & (ND_RA_FLAG_MANAGED | ND_RA_FLAG_OTHER)) == 0) |
| 2625 |
return 0; |
| 2626 |
|
| 2627 |
/* (re)start DHCPv6 client in stateful or stateless mode according to RA flags. |
| 2628 |
* Note, if both "managed" and "other configuration" bits are set, then ignore |
| 2629 |
* "other configuration" bit. See RFC 4861. */ |
| 2630 |
r = dhcp6_start_on_ra(link, !(flags & ND_RA_FLAG_MANAGED)); |
| 2631 |
break; |
| 2632 |
} |
| 2633 |
case IPV6_ACCEPT_RA_START_DHCP6_CLIENT_ALWAYS: |
| 2634 |
/* When IPv6AcceptRA.DHCPv6Client=always, start dhcp6 client in solicit mode |
| 2635 |
* even if the router flags have neither M nor O flags. */ |
| 2636 |
r = dhcp6_start_on_ra(link, /* information_request= */ false); |
| 2637 |
break; |
| 2638 |
|
| 2639 |
default: |
| 2640 |
assert_not_reached(); |
| 2641 |
} |
| 2642 |
|
| 2643 |
if (r < 0) |
| 2644 |
return log_link_warning_errno(link, r, "Could not acquire DHCPv6 lease on NDisc request: %m"); |
| 2645 |
|
| 2646 |
log_link_debug(link, "Acquiring DHCPv6 lease on NDisc request"); |
| 2647 |
return 0; |
| 2648 |
} |
| 2649 |
|
| 2650 |
static int ndisc_router_handler(Link *link, sd_ndisc_router *rt) { |
| 2651 |
struct in6_addr router; |
| 2652 |
usec_t timestamp_usec; |
| 2653 |
int r; |
| 2654 |
|
| 2655 |
assert(link); |
| 2656 |
assert(link->network); |
| 2657 |
assert(link->manager); |
| 2658 |
assert(rt); |
| 2659 |
|
| 2660 |
r = sd_ndisc_router_get_sender_address(rt, &router); |
| 2661 |
if (r == -ENODATA) { |
| 2662 |
log_link_debug(link, "Received RA without router address, ignoring."); |
| 2663 |
return 0; |
| 2664 |
} |
| 2665 |
if (r < 0) |
| 2666 |
return log_link_warning_errno(link, r, "Failed to get router address from RA: %m"); |
| 2667 |
|
| 2668 |
if (in6_prefix_is_filtered(&router, 128, link->network->ndisc_allow_listed_router, link->network->ndisc_deny_listed_router)) { |
| 2669 |
if (!set_isempty(link->network->ndisc_allow_listed_router)) |
| 2670 |
log_link_debug(link, "Router %s is not in allow list, ignoring.", IN6_ADDR_TO_STRING(&router)); |
| 2671 |
else |
| 2672 |
log_link_debug(link, "Router %s is in deny list, ignoring.", IN6_ADDR_TO_STRING(&router)); |
| 2673 |
return 0; |
| 2674 |
} |
| 2675 |
|
| 2676 |
r = sd_ndisc_router_get_timestamp(rt, CLOCK_BOOTTIME, ×tamp_usec); |
| 2677 |
if (r == -ENODATA) { |
| 2678 |
log_link_debug(link, "Received RA without timestamp, ignoring."); |
| 2679 |
return 0; |
| 2680 |
} |
| 2681 |
if (r < 0) |
| 2682 |
return r; |
| 2683 |
|
| 2684 |
r = ndisc_drop_outdated(link, /* router= */ NULL, timestamp_usec); |
| 2685 |
if (r < 0) |
| 2686 |
return r; |
| 2687 |
|
| 2688 |
r = ndisc_remember_router(link, rt); |
| 2689 |
if (r < 0) |
| 2690 |
return r; |
| 2691 |
|
| 2692 |
r = ndisc_start_dhcp6_client(link, rt); |
| 2693 |
if (r < 0) |
| 2694 |
return r; |
| 2695 |
|
| 2696 |
r = ndisc_router_process_reachable_time(link, rt); |
| 2697 |
if (r < 0) |
| 2698 |
return r; |
| 2699 |
|
| 2700 |
r = ndisc_router_process_retransmission_time(link, rt); |
| 2701 |
if (r < 0) |
| 2702 |
return r; |
| 2703 |
|
| 2704 |
r = ndisc_router_process_hop_limit(link, rt); |
| 2705 |
if (r < 0) |
| 2706 |
return r; |
| 2707 |
|
| 2708 |
r = ndisc_router_process_mtu(link, rt); |
| 2709 |
if (r < 0) |
| 2710 |
return r; |
| 2711 |
|
| 2712 |
r = ndisc_router_process_options(link, rt, /* zero_lifetime= */ true); |
| 2713 |
if (r < 0) |
| 2714 |
return r; |
| 2715 |
|
| 2716 |
r = ndisc_router_process_default(link, rt); |
| 2717 |
if (r < 0) |
| 2718 |
return r; |
| 2719 |
|
| 2720 |
r = ndisc_router_process_options(link, rt, /* zero_lifetime= */ false); |
| 2721 |
if (r < 0) |
| 2722 |
return r; |
| 2723 |
|
| 2724 |
r = ndisc_setup_expire(link); |
| 2725 |
if (r < 0) |
| 2726 |
return r; |
| 2727 |
|
| 2728 |
if (sd_ndisc_router_get_lifetime(rt, NULL) <= 0) |
| 2729 |
(void) ndisc_drop_redirect(link, &router); |
| 2730 |
|
| 2731 |
if (link->ndisc_messages == 0) |
| 2732 |
link->ndisc_configured = true; |
| 2733 |
else |
| 2734 |
log_link_debug(link, "Setting SLAAC addresses and router."); |
| 2735 |
|
| 2736 |
if (!link->ndisc_configured) |
| 2737 |
link_set_state(link, LINK_STATE_CONFIGURING); |
| 2738 |
|
| 2739 |
link_check_ready(link); |
| 2740 |
return 0; |
| 2741 |
} |
| 2742 |
|
| 2743 |
static int ndisc_neighbor_handle_non_router_message(Link *link, sd_ndisc_neighbor *na) { |
| 2744 |
struct in6_addr address; |
| 2745 |
int r; |
| 2746 |
|
| 2747 |
assert(link); |
| 2748 |
assert(na); |
| 2749 |
|
| 2750 |
/* Received Neighbor Advertisement message without Router flag. The node might have been a router, |
| 2751 |
* and now it is not. Let's drop all configurations based on RAs sent from the node. */ |
| 2752 |
|
| 2753 |
r = sd_ndisc_neighbor_get_target_address(na, &address); |
| 2754 |
if (r == -ENODATA) |
| 2755 |
return 0; |
| 2756 |
if (r < 0) |
| 2757 |
return r; |
| 2758 |
|
| 2759 |
/* Remove the routes configured by Redirect messages. */ |
| 2760 |
r = ndisc_drop_redirect(link, &address); |
| 2761 |
|
| 2762 |
/* Also remove the default gateway via the host, but keep the configurations based on the RA options. */ |
| 2763 |
_cleanup_(sd_ndisc_router_unrefp) sd_ndisc_router *rt = hashmap_remove(link->ndisc_routers_by_sender, &address); |
| 2764 |
if (rt) |
| 2765 |
RET_GATHER(r, ndisc_router_drop_default(link, rt)); |
| 2766 |
|
| 2767 |
return r; |
| 2768 |
} |
| 2769 |
|
| 2770 |
static int ndisc_neighbor_handle_router_message(Link *link, sd_ndisc_neighbor *na) { |
| 2771 |
struct in6_addr current_address, original_address; |
| 2772 |
int r; |
| 2773 |
|
| 2774 |
assert(link); |
| 2775 |
assert(link->manager); |
| 2776 |
assert(na); |
| 2777 |
|
| 2778 |
/* Received Neighbor Advertisement message with Router flag. If the router address is changed, update |
| 2779 |
* the provider field of configurations. */ |
| 2780 |
|
| 2781 |
r = sd_ndisc_neighbor_get_sender_address(na, ¤t_address); |
| 2782 |
if (r == -ENODATA) |
| 2783 |
return 0; |
| 2784 |
if (r < 0) |
| 2785 |
return r; |
| 2786 |
|
| 2787 |
r = sd_ndisc_neighbor_get_target_address(na, &original_address); |
| 2788 |
if (r == -ENODATA) |
| 2789 |
return 0; |
| 2790 |
if (r < 0) |
| 2791 |
return r; |
| 2792 |
|
| 2793 |
if (in6_addr_equal(¤t_address, &original_address)) |
| 2794 |
return 0; /* the router address is not changed */ |
| 2795 |
|
| 2796 |
r = ndisc_update_router_address(link, &original_address, ¤t_address); |
| 2797 |
if (r < 0) |
| 2798 |
return r; |
| 2799 |
|
| 2800 |
r = ndisc_update_redirect_sender(link, &original_address, ¤t_address); |
| 2801 |
if (r < 0) |
| 2802 |
return r; |
| 2803 |
|
| 2804 |
Route *route; |
| 2805 |
SET_FOREACH(route, link->manager->routes) { |
| 2806 |
if (route->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 2807 |
continue; |
| 2808 |
|
| 2809 |
if (!route_is_bound_to_link(route, link)) |
| 2810 |
continue; |
| 2811 |
|
| 2812 |
if (!in6_addr_equal(&route->provider.in6, &original_address)) |
| 2813 |
continue; |
| 2814 |
|
| 2815 |
route->provider.in6 = current_address; |
| 2816 |
} |
| 2817 |
|
| 2818 |
Address *address; |
| 2819 |
SET_FOREACH(address, link->addresses) { |
| 2820 |
if (address->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 2821 |
continue; |
| 2822 |
|
| 2823 |
if (!in6_addr_equal(&address->provider.in6, &original_address)) |
| 2824 |
continue; |
| 2825 |
|
| 2826 |
address->provider.in6 = current_address; |
| 2827 |
} |
| 2828 |
|
| 2829 |
NDiscRDNSS *rdnss; |
| 2830 |
SET_FOREACH(rdnss, link->ndisc_rdnss) { |
| 2831 |
if (!in6_addr_equal(&rdnss->router, &original_address)) |
| 2832 |
continue; |
| 2833 |
|
| 2834 |
rdnss->router = current_address; |
| 2835 |
} |
| 2836 |
|
| 2837 |
NDiscDNSSL *dnssl; |
| 2838 |
SET_FOREACH(dnssl, link->ndisc_dnssl) { |
| 2839 |
if (!in6_addr_equal(&dnssl->router, &original_address)) |
| 2840 |
continue; |
| 2841 |
|
| 2842 |
dnssl->router = current_address; |
| 2843 |
} |
| 2844 |
|
| 2845 |
NDiscCaptivePortal *cp; |
| 2846 |
SET_FOREACH(cp, link->ndisc_captive_portals) { |
| 2847 |
if (!in6_addr_equal(&cp->router, &original_address)) |
| 2848 |
continue; |
| 2849 |
|
| 2850 |
cp->router = current_address; |
| 2851 |
} |
| 2852 |
|
| 2853 |
NDiscPREF64 *p64; |
| 2854 |
SET_FOREACH(p64, link->ndisc_pref64) { |
| 2855 |
if (!in6_addr_equal(&p64->router, &original_address)) |
| 2856 |
continue; |
| 2857 |
|
| 2858 |
p64->router = current_address; |
| 2859 |
} |
| 2860 |
|
| 2861 |
NDiscDNR *dnr; |
| 2862 |
SET_FOREACH(dnr, link->ndisc_dnr) { |
| 2863 |
if (!in6_addr_equal(&dnr->router, &original_address)) |
| 2864 |
continue; |
| 2865 |
|
| 2866 |
dnr->router = current_address; |
| 2867 |
} |
| 2868 |
|
| 2869 |
return 0; |
| 2870 |
} |
| 2871 |
|
| 2872 |
static int ndisc_neighbor_handler(Link *link, sd_ndisc_neighbor *na) { |
| 2873 |
int r; |
| 2874 |
|
| 2875 |
assert(link); |
| 2876 |
assert(na); |
| 2877 |
|
| 2878 |
r = sd_ndisc_neighbor_is_router(na); |
| 2879 |
if (r < 0) |
| 2880 |
return r; |
| 2881 |
if (r == 0) |
| 2882 |
r = ndisc_neighbor_handle_non_router_message(link, na); |
| 2883 |
else |
| 2884 |
r = ndisc_neighbor_handle_router_message(link, na); |
| 2885 |
if (r < 0) |
| 2886 |
return r; |
| 2887 |
|
| 2888 |
return 0; |
| 2889 |
} |
| 2890 |
|
| 2891 |
static void ndisc_handler(sd_ndisc *nd, sd_ndisc_event_t event, void *message, void *userdata) { |
| 2892 |
Link *link = ASSERT_PTR(userdata); |
| 2893 |
int r; |
| 2894 |
|
| 2895 |
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
| 2896 |
return; |
| 2897 |
|
| 2898 |
switch (event) { |
| 2899 |
|
| 2900 |
case SD_NDISC_EVENT_ROUTER: |
| 2901 |
r = ndisc_router_handler(link, ASSERT_PTR(message)); |
| 2902 |
if (r < 0 && r != -EBADMSG) { |
| 2903 |
link_enter_failed(link); |
| 2904 |
return; |
| 2905 |
} |
| 2906 |
break; |
| 2907 |
|
| 2908 |
case SD_NDISC_EVENT_NEIGHBOR: |
| 2909 |
r = ndisc_neighbor_handler(link, ASSERT_PTR(message)); |
| 2910 |
if (r < 0 && r != -EBADMSG) { |
| 2911 |
link_enter_failed(link); |
| 2912 |
return; |
| 2913 |
} |
| 2914 |
break; |
| 2915 |
|
| 2916 |
case SD_NDISC_EVENT_REDIRECT: |
| 2917 |
r = ndisc_redirect_handler(link, ASSERT_PTR(message)); |
| 2918 |
if (r < 0 && r != -EBADMSG) { |
| 2919 |
log_link_warning_errno(link, r, "Failed to process Redirect message: %m"); |
| 2920 |
link_enter_failed(link); |
| 2921 |
return; |
| 2922 |
} |
| 2923 |
break; |
| 2924 |
|
| 2925 |
case SD_NDISC_EVENT_TIMEOUT: |
| 2926 |
log_link_debug(link, "NDisc handler get timeout event"); |
| 2927 |
if (link->ndisc_messages == 0) { |
| 2928 |
link->ndisc_configured = true; |
| 2929 |
link_check_ready(link); |
| 2930 |
} |
| 2931 |
break; |
| 2932 |
|
| 2933 |
default: |
| 2934 |
log_link_debug(link, "Received unsupported NDisc event, ignoring."); |
| 2935 |
} |
| 2936 |
} |
| 2937 |
|
| 2938 |
static int ndisc_configure(Link *link) { |
| 2939 |
int r; |
| 2940 |
|
| 2941 |
assert(link); |
| 2942 |
|
| 2943 |
if (!link_ndisc_enabled(link)) |
| 2944 |
return 0; |
| 2945 |
|
| 2946 |
if (link->ndisc) |
| 2947 |
return -EBUSY; /* Already configured. */ |
| 2948 |
|
| 2949 |
r = sd_ndisc_new(&link->ndisc); |
| 2950 |
if (r < 0) |
| 2951 |
return r; |
| 2952 |
|
| 2953 |
r = sd_ndisc_attach_event(link->ndisc, link->manager->event, 0); |
| 2954 |
if (r < 0) |
| 2955 |
return r; |
| 2956 |
|
| 2957 |
if (link->hw_addr.length == ETH_ALEN) { |
| 2958 |
r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether); |
| 2959 |
if (r < 0) |
| 2960 |
return r; |
| 2961 |
} |
| 2962 |
|
| 2963 |
r = sd_ndisc_set_ifindex(link->ndisc, link->ifindex); |
| 2964 |
if (r < 0) |
| 2965 |
return r; |
| 2966 |
|
| 2967 |
r = sd_ndisc_set_callback(link->ndisc, ndisc_handler, link); |
| 2968 |
if (r < 0) |
| 2969 |
return r; |
| 2970 |
|
| 2971 |
return 0; |
| 2972 |
} |
| 2973 |
|
| 2974 |
int ndisc_start(Link *link) { |
| 2975 |
int r; |
| 2976 |
|
| 2977 |
assert(link); |
| 2978 |
|
| 2979 |
if (!link->ndisc || !link->dhcp6_client) |
| 2980 |
return 0; |
| 2981 |
|
| 2982 |
if (!link_has_carrier(link)) |
| 2983 |
return 0; |
| 2984 |
|
| 2985 |
if (in6_addr_is_null(&link->ipv6ll_address)) |
| 2986 |
return 0; |
| 2987 |
|
| 2988 |
r = sd_ndisc_set_link_local_address(link->ndisc, &link->ipv6ll_address); |
| 2989 |
if (r < 0) |
| 2990 |
return r; |
| 2991 |
|
| 2992 |
log_link_debug(link, "Discovering IPv6 routers"); |
| 2993 |
|
| 2994 |
r = sd_ndisc_start(link->ndisc); |
| 2995 |
if (r < 0) |
| 2996 |
return r; |
| 2997 |
|
| 2998 |
return 1; |
| 2999 |
} |
| 3000 |
|
| 3001 |
static int ndisc_process_request(Request *req, Link *link, void *userdata) { |
| 3002 |
int r; |
| 3003 |
|
| 3004 |
assert(link); |
| 3005 |
|
| 3006 |
if (!link_is_ready_to_configure(link, /* allow_unmanaged= */ false)) |
| 3007 |
return 0; |
| 3008 |
|
| 3009 |
r = ndisc_configure(link); |
| 3010 |
if (r < 0) |
| 3011 |
return log_link_warning_errno(link, r, "Failed to configure IPv6 Router Discovery: %m"); |
| 3012 |
|
| 3013 |
r = ndisc_start(link); |
| 3014 |
if (r < 0) |
| 3015 |
return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m"); |
| 3016 |
|
| 3017 |
log_link_debug(link, "IPv6 Router Discovery is configured%s.", |
| 3018 |
r > 0 ? " and started" : ""); |
| 3019 |
return 1; |
| 3020 |
} |
| 3021 |
|
| 3022 |
int link_request_ndisc(Link *link) { |
| 3023 |
int r; |
| 3024 |
|
| 3025 |
assert(link); |
| 3026 |
|
| 3027 |
if (!link_ndisc_enabled(link)) |
| 3028 |
return 0; |
| 3029 |
|
| 3030 |
if (link->ndisc) |
| 3031 |
return 0; |
| 3032 |
|
| 3033 |
r = link_queue_request(link, REQUEST_TYPE_NDISC, ndisc_process_request, NULL); |
| 3034 |
if (r < 0) |
| 3035 |
return log_link_warning_errno(link, r, "Failed to request configuring of the IPv6 Router Discovery: %m"); |
| 3036 |
|
| 3037 |
log_link_debug(link, "Requested configuring of the IPv6 Router Discovery."); |
| 3038 |
return 0; |
| 3039 |
} |
| 3040 |
|
| 3041 |
int link_drop_ndisc_config(Link *link, Network *network) { |
| 3042 |
int r, ret = 0; |
| 3043 |
|
| 3044 |
assert(link); |
| 3045 |
assert(link->network); |
| 3046 |
|
| 3047 |
if (link->network == network) |
| 3048 |
return 0; /* .network file is unchanged. It is not necessary to reconfigure the client. */ |
| 3049 |
|
| 3050 |
if (!link_ndisc_enabled(link)) { |
| 3051 |
/* NDisc is disabled. Stop the client if it is running and flush configs. */ |
| 3052 |
ret = ndisc_stop(link); |
| 3053 |
ndisc_flush(link); |
| 3054 |
link->ndisc = sd_ndisc_unref(link->ndisc); |
| 3055 |
return ret; |
| 3056 |
} |
| 3057 |
|
| 3058 |
/* Even if the client was previously enabled and also enabled in the new .network file, detailed |
| 3059 |
* settings for the client may be different. Let's unref() the client. */ |
| 3060 |
link->ndisc = sd_ndisc_unref(link->ndisc); |
| 3061 |
|
| 3062 |
/* Get if NDisc was enabled or not. */ |
| 3063 |
Network *current = link->network; |
| 3064 |
link->network = network; |
| 3065 |
bool enabled = link_ndisc_enabled(link); |
| 3066 |
link->network = current; |
| 3067 |
|
| 3068 |
/* If previously explicitly disabled, there should be nothing to drop. |
| 3069 |
* If we do not know the previous setting of the client, e.g. when networkd is restarted, in that |
| 3070 |
* case we do not have the previous .network file assigned to the interface, then let's assume no |
| 3071 |
* detailed configuration is changed. Hopefully, unmatching configurations will be dropped after |
| 3072 |
* their lifetime. */ |
| 3073 |
if (!enabled) |
| 3074 |
return 0; |
| 3075 |
|
| 3076 |
assert(network); |
| 3077 |
|
| 3078 |
/* Redirect messages will be ignored. Drop configurations based on the previously received redirect |
| 3079 |
* messages. */ |
| 3080 |
if (!network->ndisc_use_redirect) |
| 3081 |
(void) ndisc_drop_redirect(link, /* router= */ NULL); |
| 3082 |
|
| 3083 |
/* If one of the route setting is changed, drop all routes. */ |
| 3084 |
if (link->network->ndisc_use_gateway != network->ndisc_use_gateway || |
| 3085 |
link->network->ndisc_use_route_prefix != network->ndisc_use_route_prefix || |
| 3086 |
link->network->ndisc_use_onlink_prefix != network->ndisc_use_onlink_prefix || |
| 3087 |
link->network->ndisc_quickack != network->ndisc_quickack || |
| 3088 |
link->network->ndisc_route_metric_high != network->ndisc_route_metric_high || |
| 3089 |
link->network->ndisc_route_metric_medium != network->ndisc_route_metric_medium || |
| 3090 |
link->network->ndisc_route_metric_low != network->ndisc_route_metric_low || |
| 3091 |
!set_equal(link->network->ndisc_deny_listed_router, network->ndisc_deny_listed_router) || |
| 3092 |
!set_equal(link->network->ndisc_allow_listed_router, network->ndisc_allow_listed_router) || |
| 3093 |
!set_equal(link->network->ndisc_deny_listed_prefix, network->ndisc_deny_listed_prefix) || |
| 3094 |
!set_equal(link->network->ndisc_allow_listed_prefix, network->ndisc_allow_listed_prefix) || |
| 3095 |
!set_equal(link->network->ndisc_deny_listed_route_prefix, network->ndisc_deny_listed_route_prefix) || |
| 3096 |
!set_equal(link->network->ndisc_allow_listed_route_prefix, network->ndisc_allow_listed_route_prefix)) { |
| 3097 |
Route *route; |
| 3098 |
SET_FOREACH(route, link->manager->routes) { |
| 3099 |
if (route->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 3100 |
continue; |
| 3101 |
|
| 3102 |
if (!route_is_bound_to_link(route, link)) |
| 3103 |
continue; |
| 3104 |
|
| 3105 |
if (route->protocol == RTPROT_REDIRECT) |
| 3106 |
continue; /* redirect route is handled by ndisc_drop_redirect(). */ |
| 3107 |
|
| 3108 |
r = route_remove_and_cancel(route, link->manager); |
| 3109 |
if (r < 0) |
| 3110 |
RET_GATHER(ret, log_link_warning_errno(link, r, "Failed to remove SLAAC route, ignoring: %m")); |
| 3111 |
} |
| 3112 |
|
| 3113 |
RET_GATHER(ret, ndisc_remove_unused_nexthops(link)); |
| 3114 |
} |
| 3115 |
|
| 3116 |
/* If SLAAC address is disabled, drop all addresses. */ |
| 3117 |
if (!network->ndisc_use_autonomous_prefix || |
| 3118 |
!set_equal(link->network->ndisc_tokens, network->ndisc_tokens) || |
| 3119 |
!set_equal(link->network->ndisc_deny_listed_prefix, network->ndisc_deny_listed_prefix) || |
| 3120 |
!set_equal(link->network->ndisc_allow_listed_prefix, network->ndisc_allow_listed_prefix)) { |
| 3121 |
Address *address; |
| 3122 |
SET_FOREACH(address, link->addresses) { |
| 3123 |
if (address->source != NETWORK_CONFIG_SOURCE_NDISC) |
| 3124 |
continue; |
| 3125 |
|
| 3126 |
r = address_remove_and_cancel(address, link); |
| 3127 |
if (r < 0) |
| 3128 |
RET_GATHER(ret, log_link_warning_errno(link, r, "Failed to remove SLAAC address, ignoring: %m")); |
| 3129 |
} |
| 3130 |
} |
| 3131 |
|
| 3132 |
if (!network->ndisc_use_mtu) |
| 3133 |
link->ndisc_mtu = 0; |
| 3134 |
|
| 3135 |
return ret; |
| 3136 |
} |
| 3137 |
|
| 3138 |
int ndisc_stop(Link *link) { |
| 3139 |
assert(link); |
| 3140 |
|
| 3141 |
link->ndisc_expire = sd_event_source_disable_unref(link->ndisc_expire); |
| 3142 |
|
| 3143 |
return sd_ndisc_stop(link->ndisc); |
| 3144 |
} |
| 3145 |
|
| 3146 |
void ndisc_flush(Link *link) { |
| 3147 |
assert(link); |
| 3148 |
|
| 3149 |
/* Remove all addresses, routes, RDNSS, DNSSL, DNR, and Captive Portal entries, without exception. */ |
| 3150 |
(void) ndisc_drop_outdated(link, /* router= */ NULL, /* timestamp_usec= */ USEC_INFINITY); |
| 3151 |
(void) ndisc_drop_redirect(link, /* router= */ NULL); |
| 3152 |
|
| 3153 |
link->ndisc_routers_by_sender = hashmap_free(link->ndisc_routers_by_sender); |
| 3154 |
link->ndisc_rdnss = set_free(link->ndisc_rdnss); |
| 3155 |
link->ndisc_dnssl = set_free(link->ndisc_dnssl); |
| 3156 |
link->ndisc_captive_portals = set_free(link->ndisc_captive_portals); |
| 3157 |
link->ndisc_pref64 = set_free(link->ndisc_pref64); |
| 3158 |
link->ndisc_redirects = set_free(link->ndisc_redirects); |
| 3159 |
link->ndisc_dnr = set_free(link->ndisc_dnr); |
| 3160 |
link->ndisc_mtu = 0; |
| 3161 |
} |
| 3162 |
|
| 3163 |
static const char* const ndisc_start_dhcp6_client_table[_IPV6_ACCEPT_RA_START_DHCP6_CLIENT_MAX] = { |
| 3164 |
[IPV6_ACCEPT_RA_START_DHCP6_CLIENT_NO] = "no", |
| 3165 |
[IPV6_ACCEPT_RA_START_DHCP6_CLIENT_ALWAYS] = "always", |
| 3166 |
[IPV6_ACCEPT_RA_START_DHCP6_CLIENT_YES] = "yes", |
| 3167 |
}; |
| 3168 |
|
| 3169 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(ndisc_start_dhcp6_client, IPv6AcceptRAStartDHCP6Client, IPV6_ACCEPT_RA_START_DHCP6_CLIENT_YES); |
| 3170 |
|
| 3171 |
DEFINE_CONFIG_PARSE_ENUM(config_parse_ndisc_start_dhcp6_client, ndisc_start_dhcp6_client, IPv6AcceptRAStartDHCP6Client); |