| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
* Copyright © 2019 VMware, Inc. |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <linux/nexthop.h> |
| 6 |
#include <net/if.h> |
| 7 |
#include <stdio.h> |
| 8 |
|
| 9 |
#include "sd-netlink.h" |
| 10 |
|
| 11 |
#include "alloc-util.h" |
| 12 |
#include "conf-parser.h" |
| 13 |
#include "errno-util.h" |
| 14 |
#include "extract-word.h" |
| 15 |
#include "netlink-util.h" |
| 16 |
#include "networkd-link.h" |
| 17 |
#include "networkd-manager.h" |
| 18 |
#include "networkd-network.h" |
| 19 |
#include "networkd-nexthop.h" |
| 20 |
#include "networkd-queue.h" |
| 21 |
#include "networkd-route.h" |
| 22 |
#include "networkd-route-util.h" |
| 23 |
#include "ordered-set.h" |
| 24 |
#include "parse-util.h" |
| 25 |
#include "set.h" |
| 26 |
#include "siphash24.h" |
| 27 |
#include "string-util.h" |
| 28 |
|
| 29 |
static void nexthop_detach_from_group_members(NextHop *nexthop) { |
| 30 |
assert(nexthop); |
| 31 |
assert(nexthop->manager); |
| 32 |
assert(nexthop->id > 0); |
| 33 |
|
| 34 |
struct nexthop_grp *nhg; |
| 35 |
HASHMAP_FOREACH(nhg, nexthop->group) { |
| 36 |
NextHop *nh; |
| 37 |
|
| 38 |
if (nexthop_get_by_id(nexthop->manager, nhg->id, &nh) < 0) |
| 39 |
continue; |
| 40 |
|
| 41 |
set_remove(nh->nexthops, UINT32_TO_PTR(nexthop->id)); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
static void nexthop_attach_to_group_members(NextHop *nexthop) { |
| 46 |
int r; |
| 47 |
|
| 48 |
assert(nexthop); |
| 49 |
assert(nexthop->manager); |
| 50 |
assert(nexthop->id > 0); |
| 51 |
|
| 52 |
struct nexthop_grp *nhg; |
| 53 |
HASHMAP_FOREACH(nhg, nexthop->group) { |
| 54 |
NextHop *nh; |
| 55 |
|
| 56 |
r = nexthop_get_by_id(nexthop->manager, nhg->id, &nh); |
| 57 |
if (r < 0) { |
| 58 |
if (nexthop->manager->manage_foreign_nexthops) |
| 59 |
log_debug_errno(r, "Nexthop (id=%"PRIu32") has unknown group member (%"PRIu32"), ignoring.", |
| 60 |
nexthop->id, nhg->id); |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
r = set_ensure_put(&nh->nexthops, NULL, UINT32_TO_PTR(nexthop->id)); |
| 65 |
if (r < 0) |
| 66 |
log_debug_errno(r, "Failed to save nexthop ID (%"PRIu32") to group member (%"PRIu32"), ignoring: %m", |
| 67 |
nexthop->id, nhg->id); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
static NextHop* nexthop_detach_impl(NextHop *nexthop) { |
| 72 |
assert(nexthop); |
| 73 |
assert(!nexthop->manager || !nexthop->network); |
| 74 |
|
| 75 |
if (nexthop->network) { |
| 76 |
assert(nexthop->section); |
| 77 |
ordered_hashmap_remove(nexthop->network->nexthops_by_section, nexthop->section); |
| 78 |
nexthop->network = NULL; |
| 79 |
return nexthop; |
| 80 |
} |
| 81 |
|
| 82 |
if (nexthop->manager) { |
| 83 |
assert(nexthop->id > 0); |
| 84 |
|
| 85 |
nexthop_detach_from_group_members(nexthop); |
| 86 |
|
| 87 |
hashmap_remove(nexthop->manager->nexthops_by_id, UINT32_TO_PTR(nexthop->id)); |
| 88 |
nexthop->manager = NULL; |
| 89 |
return nexthop; |
| 90 |
} |
| 91 |
|
| 92 |
return NULL; |
| 93 |
} |
| 94 |
|
| 95 |
static void nexthop_detach(NextHop *nexthop) { |
| 96 |
nexthop_unref(nexthop_detach_impl(nexthop)); |
| 97 |
} |
| 98 |
|
| 99 |
static NextHop* nexthop_free(NextHop *nexthop) { |
| 100 |
if (!nexthop) |
| 101 |
return NULL; |
| 102 |
|
| 103 |
nexthop_detach_impl(nexthop); |
| 104 |
|
| 105 |
config_section_free(nexthop->section); |
| 106 |
hashmap_free(nexthop->group); |
| 107 |
set_free(nexthop->nexthops); |
| 108 |
set_free(nexthop->routes); |
| 109 |
|
| 110 |
return mfree(nexthop); |
| 111 |
} |
| 112 |
|
| 113 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(NextHop, nexthop, nexthop_free); |
| 114 |
|
| 115 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 116 |
nexthop_hash_ops, |
| 117 |
void, |
| 118 |
trivial_hash_func, |
| 119 |
trivial_compare_func, |
| 120 |
NextHop, |
| 121 |
nexthop_detach); |
| 122 |
|
| 123 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 124 |
nexthop_section_hash_ops, |
| 125 |
ConfigSection, |
| 126 |
config_section_hash_func, |
| 127 |
config_section_compare_func, |
| 128 |
NextHop, |
| 129 |
nexthop_detach); |
| 130 |
|
| 131 |
int nexthop_new(NextHop **ret) { |
| 132 |
_cleanup_(nexthop_unrefp) NextHop *nexthop = NULL; |
| 133 |
|
| 134 |
assert(ret); |
| 135 |
|
| 136 |
nexthop = new(NextHop, 1); |
| 137 |
if (!nexthop) |
| 138 |
return -ENOMEM; |
| 139 |
|
| 140 |
*nexthop = (NextHop) { |
| 141 |
.n_ref = 1, |
| 142 |
.onlink = -1, |
| 143 |
}; |
| 144 |
|
| 145 |
*ret = TAKE_PTR(nexthop); |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
static int nexthop_new_static(Network *network, const char *filename, unsigned section_line, NextHop **ret) { |
| 151 |
_cleanup_(config_section_freep) ConfigSection *n = NULL; |
| 152 |
_cleanup_(nexthop_unrefp) NextHop *nexthop = NULL; |
| 153 |
int r; |
| 154 |
|
| 155 |
assert(network); |
| 156 |
assert(ret); |
| 157 |
assert(filename); |
| 158 |
assert(section_line > 0); |
| 159 |
|
| 160 |
r = config_section_new(filename, section_line, &n); |
| 161 |
if (r < 0) |
| 162 |
return r; |
| 163 |
|
| 164 |
nexthop = ordered_hashmap_get(network->nexthops_by_section, n); |
| 165 |
if (nexthop) { |
| 166 |
*ret = TAKE_PTR(nexthop); |
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
r = nexthop_new(&nexthop); |
| 171 |
if (r < 0) |
| 172 |
return r; |
| 173 |
|
| 174 |
nexthop->protocol = RTPROT_STATIC; |
| 175 |
nexthop->network = network; |
| 176 |
nexthop->section = TAKE_PTR(n); |
| 177 |
nexthop->source = NETWORK_CONFIG_SOURCE_STATIC; |
| 178 |
|
| 179 |
r = ordered_hashmap_ensure_put(&network->nexthops_by_section, &nexthop_section_hash_ops, nexthop->section, nexthop); |
| 180 |
if (r < 0) |
| 181 |
return r; |
| 182 |
|
| 183 |
*ret = TAKE_PTR(nexthop); |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
static void nexthop_hash_func(const NextHop *nexthop, struct siphash *state) { |
| 188 |
assert(nexthop); |
| 189 |
assert(state); |
| 190 |
|
| 191 |
siphash24_compress_typesafe(nexthop->id, state); |
| 192 |
} |
| 193 |
|
| 194 |
static int nexthop_compare_func(const NextHop *a, const NextHop *b) { |
| 195 |
assert(a); |
| 196 |
assert(b); |
| 197 |
|
| 198 |
return CMP(a->id, b->id); |
| 199 |
} |
| 200 |
|
| 201 |
static int nexthop_compare_full(const NextHop *a, const NextHop *b) { |
| 202 |
int r; |
| 203 |
|
| 204 |
assert(a); |
| 205 |
assert(b); |
| 206 |
|
| 207 |
/* This compares detailed configs, except for ID and ifindex. */ |
| 208 |
|
| 209 |
r = CMP(a->protocol, b->protocol); |
| 210 |
if (r != 0) |
| 211 |
return r; |
| 212 |
|
| 213 |
r = CMP(a->flags, b->flags); |
| 214 |
if (r != 0) |
| 215 |
return r; |
| 216 |
|
| 217 |
r = CMP(hashmap_size(a->group), hashmap_size(b->group)); |
| 218 |
if (r != 0) |
| 219 |
return r; |
| 220 |
|
| 221 |
if (!hashmap_isempty(a->group)) { |
| 222 |
struct nexthop_grp *ga; |
| 223 |
|
| 224 |
HASHMAP_FOREACH(ga, a->group) { |
| 225 |
struct nexthop_grp *gb; |
| 226 |
|
| 227 |
gb = hashmap_get(b->group, UINT32_TO_PTR(ga->id)); |
| 228 |
if (!gb) |
| 229 |
return CMP(ga, gb); |
| 230 |
|
| 231 |
r = CMP(ga->weight, gb->weight); |
| 232 |
if (r != 0) |
| 233 |
return r; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
r = CMP(a->blackhole, b->blackhole); |
| 238 |
if (r != 0) |
| 239 |
return r; |
| 240 |
|
| 241 |
r = CMP(a->family, b->family); |
| 242 |
if (r != 0) |
| 243 |
return r; |
| 244 |
|
| 245 |
if (IN_SET(a->family, AF_INET, AF_INET6)) { |
| 246 |
r = memcmp(&a->gw.address, &b->gw.address, FAMILY_ADDRESS_SIZE(a->family)); |
| 247 |
if (r != 0) |
| 248 |
return r; |
| 249 |
} |
| 250 |
|
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
static int nexthop_dup(const NextHop *src, NextHop **ret) { |
| 255 |
_cleanup_(nexthop_unrefp) NextHop *dest = NULL; |
| 256 |
struct nexthop_grp *nhg; |
| 257 |
int r; |
| 258 |
|
| 259 |
assert(src); |
| 260 |
assert(ret); |
| 261 |
|
| 262 |
dest = newdup(NextHop, src, 1); |
| 263 |
if (!dest) |
| 264 |
return -ENOMEM; |
| 265 |
|
| 266 |
/* clear the reference counter and all pointers */ |
| 267 |
dest->n_ref = 1; |
| 268 |
dest->manager = NULL; |
| 269 |
dest->network = NULL; |
| 270 |
dest->section = NULL; |
| 271 |
dest->group = NULL; |
| 272 |
dest->nexthops = NULL; |
| 273 |
dest->routes = NULL; |
| 274 |
|
| 275 |
HASHMAP_FOREACH(nhg, src->group) { |
| 276 |
_cleanup_free_ struct nexthop_grp *g = NULL; |
| 277 |
|
| 278 |
g = newdup(struct nexthop_grp, nhg, 1); |
| 279 |
if (!g) |
| 280 |
return -ENOMEM; |
| 281 |
|
| 282 |
r = hashmap_ensure_put(&dest->group, &trivial_hash_ops_value_free, UINT32_TO_PTR(g->id), g); |
| 283 |
if (r < 0) |
| 284 |
return r; |
| 285 |
if (r > 0) |
| 286 |
TAKE_PTR(g); |
| 287 |
} |
| 288 |
|
| 289 |
*ret = TAKE_PTR(dest); |
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
static bool nexthop_bound_to_link(const NextHop *nexthop) { |
| 294 |
assert(nexthop); |
| 295 |
return !nexthop->blackhole && hashmap_isempty(nexthop->group); |
| 296 |
} |
| 297 |
|
| 298 |
int nexthop_get_by_id(Manager *manager, uint32_t id, NextHop **ret) { |
| 299 |
NextHop *nh; |
| 300 |
|
| 301 |
assert(manager); |
| 302 |
|
| 303 |
if (id == 0) |
| 304 |
return -EINVAL; |
| 305 |
|
| 306 |
nh = hashmap_get(manager->nexthops_by_id, UINT32_TO_PTR(id)); |
| 307 |
if (!nh) |
| 308 |
return -ENOENT; |
| 309 |
|
| 310 |
if (ret) |
| 311 |
*ret = nh; |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
static int nexthop_get(Link *link, const NextHop *in, NextHop **ret) { |
| 316 |
NextHop *nexthop; |
| 317 |
int ifindex; |
| 318 |
|
| 319 |
assert(link); |
| 320 |
assert(link->manager); |
| 321 |
assert(in); |
| 322 |
|
| 323 |
if (in->id > 0) |
| 324 |
return nexthop_get_by_id(link->manager, in->id, ret); |
| 325 |
|
| 326 |
/* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by |
| 327 |
* nexthop_section_verify(). */ |
| 328 |
assert(link->manager->manage_foreign_nexthops); |
| 329 |
|
| 330 |
ifindex = nexthop_bound_to_link(in) ? link->ifindex : 0; |
| 331 |
|
| 332 |
HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) { |
| 333 |
if (nexthop->ifindex != ifindex) |
| 334 |
continue; |
| 335 |
if (nexthop_compare_full(nexthop, in) != 0) |
| 336 |
continue; |
| 337 |
|
| 338 |
/* Even if the configuration matches, it may be configured with another [NextHop] section |
| 339 |
* that has an explicit ID. If so, the assigned nexthop is not the one we are looking for. */ |
| 340 |
if (set_contains(link->manager->nexthop_ids, UINT32_TO_PTR(nexthop->id))) |
| 341 |
continue; |
| 342 |
|
| 343 |
if (ret) |
| 344 |
*ret = nexthop; |
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
return -ENOENT; |
| 349 |
} |
| 350 |
|
| 351 |
int nexthop_get_request_by_id(Manager *manager, uint32_t id, Request **ret) { |
| 352 |
Request *req; |
| 353 |
|
| 354 |
assert(manager); |
| 355 |
|
| 356 |
if (id == 0) |
| 357 |
return -EINVAL; |
| 358 |
|
| 359 |
req = ordered_set_get( |
| 360 |
manager->request_queue, |
| 361 |
&(Request) { |
| 362 |
.type = REQUEST_TYPE_NEXTHOP, |
| 363 |
.userdata = (void*) &(const NextHop) { .id = id }, |
| 364 |
.hash_func = (hash_func_t) nexthop_hash_func, |
| 365 |
.compare_func = (compare_func_t) nexthop_compare_func, |
| 366 |
}); |
| 367 |
if (!req) |
| 368 |
return -ENOENT; |
| 369 |
|
| 370 |
if (ret) |
| 371 |
*ret = req; |
| 372 |
return 0; |
| 373 |
} |
| 374 |
|
| 375 |
static int nexthop_get_request(Link *link, const NextHop *in, Request **ret) { |
| 376 |
Request *req; |
| 377 |
int ifindex; |
| 378 |
|
| 379 |
assert(link); |
| 380 |
assert(link->manager); |
| 381 |
assert(in); |
| 382 |
|
| 383 |
if (in->id > 0) |
| 384 |
return nexthop_get_request_by_id(link->manager, in->id, ret); |
| 385 |
|
| 386 |
/* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by |
| 387 |
* nexthop_section_verify(). */ |
| 388 |
assert(link->manager->manage_foreign_nexthops); |
| 389 |
|
| 390 |
ifindex = nexthop_bound_to_link(in) ? link->ifindex : 0; |
| 391 |
|
| 392 |
ORDERED_SET_FOREACH(req, link->manager->request_queue) { |
| 393 |
if (req->type != REQUEST_TYPE_NEXTHOP) |
| 394 |
continue; |
| 395 |
|
| 396 |
NextHop *nexthop = ASSERT_PTR(req->userdata); |
| 397 |
if (nexthop->ifindex != ifindex) |
| 398 |
continue; |
| 399 |
if (nexthop_compare_full(nexthop, in) != 0) |
| 400 |
continue; |
| 401 |
|
| 402 |
/* Even if the configuration matches, it may be requested by another [NextHop] section |
| 403 |
* that has an explicit ID. If so, the request is not the one we are looking for. */ |
| 404 |
if (set_contains(link->manager->nexthop_ids, UINT32_TO_PTR(nexthop->id))) |
| 405 |
continue; |
| 406 |
|
| 407 |
if (ret) |
| 408 |
*ret = req; |
| 409 |
return 0; |
| 410 |
} |
| 411 |
|
| 412 |
return -ENOENT; |
| 413 |
} |
| 414 |
|
| 415 |
static int nexthop_add_new(Manager *manager, uint32_t id, NextHop **ret) { |
| 416 |
_cleanup_(nexthop_unrefp) NextHop *nexthop = NULL; |
| 417 |
int r; |
| 418 |
|
| 419 |
assert(manager); |
| 420 |
assert(id > 0); |
| 421 |
|
| 422 |
r = nexthop_new(&nexthop); |
| 423 |
if (r < 0) |
| 424 |
return r; |
| 425 |
|
| 426 |
nexthop->id = id; |
| 427 |
|
| 428 |
r = hashmap_ensure_put(&manager->nexthops_by_id, &nexthop_hash_ops, UINT32_TO_PTR(nexthop->id), nexthop); |
| 429 |
if (r < 0) |
| 430 |
return r; |
| 431 |
assert(r > 0); |
| 432 |
|
| 433 |
nexthop->manager = manager; |
| 434 |
|
| 435 |
if (ret) |
| 436 |
*ret = nexthop; |
| 437 |
|
| 438 |
TAKE_PTR(nexthop); |
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
static int nexthop_acquire_id(Manager *manager, NextHop *nexthop) { |
| 443 |
assert(manager); |
| 444 |
assert(nexthop); |
| 445 |
|
| 446 |
if (nexthop->id > 0) |
| 447 |
return 0; |
| 448 |
|
| 449 |
/* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by |
| 450 |
* nexthop_section_verify(). */ |
| 451 |
assert(manager->manage_foreign_nexthops); |
| 452 |
|
| 453 |
/* Find the lowest unused ID. */ |
| 454 |
|
| 455 |
for (uint32_t id = 1; id < UINT32_MAX; id++) { |
| 456 |
if (nexthop_get_by_id(manager, id, NULL) >= 0) |
| 457 |
continue; |
| 458 |
if (nexthop_get_request_by_id(manager, id, NULL) >= 0) |
| 459 |
continue; |
| 460 |
if (set_contains(manager->nexthop_ids, UINT32_TO_PTR(id))) |
| 461 |
continue; |
| 462 |
|
| 463 |
nexthop->id = id; |
| 464 |
return 0; |
| 465 |
} |
| 466 |
|
| 467 |
return -EBUSY; |
| 468 |
} |
| 469 |
|
| 470 |
static int nexthop_to_string(const NextHop *nexthop, Manager *manager, char **ret) { |
| 471 |
_cleanup_free_ char *group = NULL, *flags = NULL; |
| 472 |
|
| 473 |
assert(nexthop); |
| 474 |
assert(manager); |
| 475 |
assert(ret); |
| 476 |
|
| 477 |
(void) route_flags_to_string_alloc(nexthop->flags, &flags); |
| 478 |
|
| 479 |
struct nexthop_grp *nhg; |
| 480 |
HASHMAP_FOREACH(nhg, nexthop->group) |
| 481 |
(void) strextendf_with_separator(&group, ",", "%"PRIu32":%"PRIu32, nhg->id, nhg->weight+1u); |
| 482 |
|
| 483 |
if (asprintf(ret, "id: %"PRIu32", gw: %s, blackhole: %s, group: %s, flags: %s", |
| 484 |
nexthop->id, |
| 485 |
IN_ADDR_TO_STRING(nexthop->family, &nexthop->gw.address), |
| 486 |
yes_no(nexthop->blackhole), strna(group), strna(flags)) < 0) |
| 487 |
return -ENOMEM; |
| 488 |
|
| 489 |
return 0; |
| 490 |
} |
| 491 |
|
| 492 |
void log_nexthop_debug(const NextHop *nexthop, const char *str, Manager *manager) { |
| 493 |
_cleanup_free_ char *state = NULL, *nexthop_str = NULL; |
| 494 |
Link *link = NULL; |
| 495 |
|
| 496 |
assert(nexthop); |
| 497 |
assert(str); |
| 498 |
assert(manager); |
| 499 |
|
| 500 |
if (!DEBUG_LOGGING) |
| 501 |
return; |
| 502 |
|
| 503 |
(void) link_get_by_index(manager, nexthop->ifindex, &link); |
| 504 |
(void) network_config_state_to_string_alloc(nexthop->state, &state); |
| 505 |
(void) nexthop_to_string(nexthop, manager, &nexthop_str); |
| 506 |
|
| 507 |
log_link_debug(link, "%s %s nexthop (%s): %s", |
| 508 |
str, strna(network_config_source_to_string(nexthop->source)), strna(state), strna(nexthop_str)); |
| 509 |
} |
| 510 |
|
| 511 |
static void nexthop_forget_dependents(NextHop *nexthop, Manager *manager) { |
| 512 |
assert(nexthop); |
| 513 |
assert(manager); |
| 514 |
|
| 515 |
/* If a nexthop is removed, the kernel silently removes routes that depend on the removed nexthop. |
| 516 |
* Let's forget them. */ |
| 517 |
|
| 518 |
for (;;) { |
| 519 |
_cleanup_(route_unrefp) Route *route = set_steal_first(nexthop->routes); |
| 520 |
if (!route) |
| 521 |
break; |
| 522 |
|
| 523 |
Request *req; |
| 524 |
if (route_get_request(manager, route, &req) >= 0) |
| 525 |
route_enter_removed(req->userdata); |
| 526 |
|
| 527 |
route_enter_removed(route); |
| 528 |
log_route_debug(route, "Forgetting silently removed", manager); |
| 529 |
route_detach(route); |
| 530 |
} |
| 531 |
|
| 532 |
nexthop->routes = set_free(nexthop->routes); |
| 533 |
} |
| 534 |
|
| 535 |
static void nexthop_forget(Manager *manager, NextHop *nexthop, const char *msg) { |
| 536 |
assert(manager); |
| 537 |
assert(nexthop); |
| 538 |
assert(msg); |
| 539 |
|
| 540 |
Request *req; |
| 541 |
if (nexthop_get_request_by_id(manager, nexthop->id, &req) >= 0) |
| 542 |
nexthop_enter_removed(req->userdata); |
| 543 |
|
| 544 |
if (!nexthop->manager && nexthop_get_by_id(manager, nexthop->id, &nexthop) < 0) |
| 545 |
return; |
| 546 |
|
| 547 |
nexthop_enter_removed(nexthop); |
| 548 |
log_nexthop_debug(nexthop, msg, manager); |
| 549 |
nexthop_forget_dependents(nexthop, nexthop->manager); |
| 550 |
nexthop_detach(nexthop); |
| 551 |
} |
| 552 |
|
| 553 |
static int nexthop_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, RemoveRequest *rreq) { |
| 554 |
int r; |
| 555 |
|
| 556 |
assert(m); |
| 557 |
assert(rreq); |
| 558 |
|
| 559 |
Manager *manager = ASSERT_PTR(rreq->manager); |
| 560 |
NextHop *nexthop = ASSERT_PTR(rreq->userdata); |
| 561 |
|
| 562 |
r = sd_netlink_message_get_errno(m); |
| 563 |
if (r < 0) { |
| 564 |
log_message_full_errno(m, |
| 565 |
(r == -ENOENT || !nexthop->manager) ? LOG_DEBUG : LOG_WARNING, |
| 566 |
r, "Could not drop nexthop, ignoring"); |
| 567 |
|
| 568 |
/* If the nexthop cannot be removed, then assume the nexthop is already removed. */ |
| 569 |
nexthop_forget(manager, nexthop, "Forgetting"); |
| 570 |
} |
| 571 |
|
| 572 |
return 1; |
| 573 |
} |
| 574 |
|
| 575 |
int nexthop_remove(NextHop *nexthop, Manager *manager) { |
| 576 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 577 |
Link *link = NULL; |
| 578 |
int r; |
| 579 |
|
| 580 |
assert(nexthop); |
| 581 |
assert(nexthop->id > 0); |
| 582 |
assert(manager); |
| 583 |
|
| 584 |
/* If the nexthop is remembered, then use the remembered object. */ |
| 585 |
(void) nexthop_get_by_id(manager, PTR_TO_UINT32(nexthop->id), &nexthop); |
| 586 |
|
| 587 |
/* link may be NULL. */ |
| 588 |
(void) link_get_by_index(manager, nexthop->ifindex, &link); |
| 589 |
|
| 590 |
log_nexthop_debug(nexthop, "Removing", manager); |
| 591 |
|
| 592 |
r = sd_rtnl_message_new_nexthop(manager->rtnl, &m, RTM_DELNEXTHOP, AF_UNSPEC, RTPROT_UNSPEC); |
| 593 |
if (r < 0) |
| 594 |
return log_link_error_errno(link, r, "Could not create RTM_DELNEXTHOP message: %m"); |
| 595 |
|
| 596 |
r = sd_netlink_message_append_u32(m, NHA_ID, nexthop->id); |
| 597 |
if (r < 0) |
| 598 |
return log_link_error_errno(link, r, "Could not append NHA_ID attribute: %m"); |
| 599 |
|
| 600 |
r = manager_remove_request_add(manager, nexthop, nexthop, manager->rtnl, m, nexthop_remove_handler); |
| 601 |
if (r < 0) |
| 602 |
return log_link_error_errno(link, r, "Could not queue rtnetlink message: %m"); |
| 603 |
|
| 604 |
nexthop_enter_removing(nexthop); |
| 605 |
return 0; |
| 606 |
} |
| 607 |
|
| 608 |
int nexthop_remove_and_cancel(NextHop *nexthop, Manager *manager) { |
| 609 |
_cleanup_(request_unrefp) Request *req = NULL; |
| 610 |
bool waiting = false; |
| 611 |
|
| 612 |
assert(nexthop); |
| 613 |
assert(nexthop->id > 0); |
| 614 |
assert(manager); |
| 615 |
|
| 616 |
/* If the nexthop is remembered by the manager, then use the remembered object. */ |
| 617 |
(void) nexthop_get_by_id(manager, nexthop->id, &nexthop); |
| 618 |
|
| 619 |
/* Cancel the request for the nexthop. If the request is already called but we have not received the |
| 620 |
* notification about the request, then explicitly remove the nexthop. */ |
| 621 |
if (nexthop_get_request_by_id(manager, nexthop->id, &req) >= 0) { |
| 622 |
request_ref(req); /* avoid the request freed by request_detach() */ |
| 623 |
waiting = req->waiting_reply; |
| 624 |
request_detach(req); |
| 625 |
nexthop_cancel_requesting(nexthop); |
| 626 |
} |
| 627 |
|
| 628 |
/* If we know that the nexthop will come or already exists, remove it. */ |
| 629 |
if (waiting || (nexthop->manager && nexthop_exists(nexthop))) |
| 630 |
return nexthop_remove(nexthop, manager); |
| 631 |
|
| 632 |
return 0; |
| 633 |
} |
| 634 |
|
| 635 |
static int nexthop_configure(NextHop *nexthop, Link *link, Request *req) { |
| 636 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 637 |
int r; |
| 638 |
|
| 639 |
assert(nexthop); |
| 640 |
assert(nexthop->id > 0); |
| 641 |
assert(IN_SET(nexthop->family, AF_UNSPEC, AF_INET, AF_INET6)); |
| 642 |
assert(link); |
| 643 |
assert(link->manager); |
| 644 |
assert(link->manager->rtnl); |
| 645 |
assert(link->ifindex > 0); |
| 646 |
assert(req); |
| 647 |
|
| 648 |
log_nexthop_debug(nexthop, "Configuring", link->manager); |
| 649 |
|
| 650 |
r = sd_rtnl_message_new_nexthop(link->manager->rtnl, &m, RTM_NEWNEXTHOP, nexthop->family, nexthop->protocol); |
| 651 |
if (r < 0) |
| 652 |
return r; |
| 653 |
|
| 654 |
r = sd_netlink_message_append_u32(m, NHA_ID, nexthop->id); |
| 655 |
if (r < 0) |
| 656 |
return r; |
| 657 |
|
| 658 |
if (!hashmap_isempty(nexthop->group)) { |
| 659 |
_cleanup_free_ struct nexthop_grp *group = NULL; |
| 660 |
struct nexthop_grp *p, *nhg; |
| 661 |
|
| 662 |
group = new(struct nexthop_grp, hashmap_size(nexthop->group)); |
| 663 |
if (!group) |
| 664 |
return log_oom(); |
| 665 |
|
| 666 |
p = group; |
| 667 |
HASHMAP_FOREACH(nhg, nexthop->group) |
| 668 |
*p++ = *nhg; |
| 669 |
|
| 670 |
r = sd_netlink_message_append_data(m, NHA_GROUP, group, sizeof(struct nexthop_grp) * hashmap_size(nexthop->group)); |
| 671 |
if (r < 0) |
| 672 |
return r; |
| 673 |
|
| 674 |
} else if (nexthop->blackhole) { |
| 675 |
r = sd_netlink_message_append_flag(m, NHA_BLACKHOLE); |
| 676 |
if (r < 0) |
| 677 |
return r; |
| 678 |
} else { |
| 679 |
assert(nexthop->ifindex == link->ifindex); |
| 680 |
|
| 681 |
r = sd_netlink_message_append_u32(m, NHA_OIF, nexthop->ifindex); |
| 682 |
if (r < 0) |
| 683 |
return r; |
| 684 |
|
| 685 |
if (in_addr_is_set(nexthop->family, &nexthop->gw.address)) { |
| 686 |
r = netlink_message_append_in_addr_union(m, NHA_GATEWAY, nexthop->family, &nexthop->gw.address); |
| 687 |
if (r < 0) |
| 688 |
return r; |
| 689 |
|
| 690 |
r = sd_rtnl_message_nexthop_set_flags(m, nexthop->flags & RTNH_F_ONLINK); |
| 691 |
if (r < 0) |
| 692 |
return r; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
return request_call_netlink_async(link->manager->rtnl, m, req); |
| 697 |
} |
| 698 |
|
| 699 |
int nexthop_configure_handler_internal(sd_netlink_message *m, Link *link, NextHop *nexthop) { |
| 700 |
int r; |
| 701 |
|
| 702 |
assert(m); |
| 703 |
assert(link); |
| 704 |
assert(nexthop); |
| 705 |
|
| 706 |
r = sd_netlink_message_get_errno(m); |
| 707 |
if (r < 0 && r != -EEXIST) { |
| 708 |
_cleanup_free_ char *str = NULL; |
| 709 |
(void) nexthop_to_string(nexthop, link->manager, &str); |
| 710 |
log_link_message_warning_errno(link, m, r, "Failed to set %s nexthop (%s)", |
| 711 |
network_config_source_to_string(nexthop->source), strna(str)); |
| 712 |
link_enter_failed(link); |
| 713 |
return 0; |
| 714 |
} |
| 715 |
|
| 716 |
return 1; |
| 717 |
} |
| 718 |
|
| 719 |
static int static_nexthop_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, NextHop *nexthop) { |
| 720 |
int r; |
| 721 |
|
| 722 |
assert(link); |
| 723 |
|
| 724 |
r = nexthop_configure_handler_internal(m, link, nexthop); |
| 725 |
if (r <= 0) |
| 726 |
return r; |
| 727 |
|
| 728 |
if (link->static_nexthop_messages == 0) { |
| 729 |
log_link_debug(link, "Nexthops set"); |
| 730 |
link->static_nexthops_configured = true; |
| 731 |
link_check_ready(link); |
| 732 |
} |
| 733 |
|
| 734 |
return 1; |
| 735 |
} |
| 736 |
|
| 737 |
int nexthop_is_ready(Manager *manager, uint32_t id, NextHop **ret) { |
| 738 |
NextHop *nexthop; |
| 739 |
|
| 740 |
assert(manager); |
| 741 |
|
| 742 |
if (id == 0) |
| 743 |
return -EINVAL; |
| 744 |
|
| 745 |
if (nexthop_get_request_by_id(manager, id, NULL) >= 0) |
| 746 |
goto not_ready; |
| 747 |
|
| 748 |
if (nexthop_get_by_id(manager, id, &nexthop) < 0) |
| 749 |
goto not_ready; |
| 750 |
|
| 751 |
if (!nexthop_exists(nexthop)) |
| 752 |
goto not_ready; |
| 753 |
|
| 754 |
if (ret) |
| 755 |
*ret = nexthop; |
| 756 |
|
| 757 |
return true; |
| 758 |
|
| 759 |
not_ready: |
| 760 |
if (ret) |
| 761 |
*ret = NULL; |
| 762 |
|
| 763 |
return false; |
| 764 |
} |
| 765 |
|
| 766 |
static bool nexthop_is_ready_to_configure(Link *link, const NextHop *nexthop) { |
| 767 |
struct nexthop_grp *nhg; |
| 768 |
int r; |
| 769 |
|
| 770 |
assert(link); |
| 771 |
assert(nexthop); |
| 772 |
assert(nexthop->id > 0); |
| 773 |
|
| 774 |
if (!link_is_ready_to_configure(link, false)) |
| 775 |
return false; |
| 776 |
|
| 777 |
/* Currently, we support the following three types of nexthops: |
| 778 |
* 1. Simple nexthop - bound to the link, requires the underlying link is up. |
| 779 |
* 2. Blackhole nexthop - not bound to the link. |
| 780 |
* 3. Group nexthop - not bound to the link, but all group members must be configured first. |
| 781 |
* |
| 782 |
* Note, the kernel also supports fdb nexthop, but currently we do not support it. Note, fdb nexthop |
| 783 |
* does not require IFF_UP. See rtm_to_nh_config() in net/ipv4/nexthop.c of kernel. */ |
| 784 |
|
| 785 |
/* Simple nexthop */ |
| 786 |
if (nexthop_bound_to_link(nexthop)) { |
| 787 |
assert(nexthop->ifindex == link->ifindex); |
| 788 |
|
| 789 |
return gateway_is_ready(link, FLAGS_SET(nexthop->flags, RTNH_F_ONLINK), nexthop->family, &nexthop->gw.address); |
| 790 |
} |
| 791 |
|
| 792 |
/* Blackhole nexthop */ |
| 793 |
if (nexthop->blackhole) |
| 794 |
return true; |
| 795 |
|
| 796 |
/* Group nexthop */ |
| 797 |
HASHMAP_FOREACH(nhg, nexthop->group) { |
| 798 |
r = nexthop_is_ready(link->manager, nhg->id, NULL); |
| 799 |
if (r <= 0) |
| 800 |
return r; |
| 801 |
} |
| 802 |
|
| 803 |
return true; |
| 804 |
} |
| 805 |
|
| 806 |
static int nexthop_process_request(Request *req, Link *link, NextHop *nexthop) { |
| 807 |
NextHop *existing; |
| 808 |
int r; |
| 809 |
|
| 810 |
assert(req); |
| 811 |
assert(link); |
| 812 |
assert(link->manager); |
| 813 |
assert(nexthop); |
| 814 |
|
| 815 |
if (!nexthop_is_ready_to_configure(link, nexthop)) |
| 816 |
return 0; |
| 817 |
|
| 818 |
r = nexthop_configure(nexthop, link, req); |
| 819 |
if (r < 0) |
| 820 |
return log_link_warning_errno(link, r, "Failed to configure nexthop"); |
| 821 |
|
| 822 |
nexthop_enter_configuring(nexthop); |
| 823 |
if (nexthop_get_by_id(link->manager, nexthop->id, &existing) >= 0) |
| 824 |
nexthop_enter_configuring(existing); |
| 825 |
|
| 826 |
return 1; |
| 827 |
} |
| 828 |
|
| 829 |
int link_request_nexthop( |
| 830 |
Link *link, |
| 831 |
const NextHop *nexthop, |
| 832 |
unsigned *message_counter, |
| 833 |
nexthop_netlink_handler_t netlink_handler) { |
| 834 |
|
| 835 |
_cleanup_(nexthop_unrefp) NextHop *tmp = NULL; |
| 836 |
NextHop *existing = NULL; |
| 837 |
int r; |
| 838 |
|
| 839 |
assert(link); |
| 840 |
assert(link->manager); |
| 841 |
assert(nexthop); |
| 842 |
assert(nexthop->source != NETWORK_CONFIG_SOURCE_FOREIGN); |
| 843 |
|
| 844 |
if (nexthop_get_request(link, nexthop, NULL) >= 0) |
| 845 |
return 0; /* already requested, skipping. */ |
| 846 |
|
| 847 |
r = nexthop_dup(nexthop, &tmp); |
| 848 |
if (r < 0) |
| 849 |
return r; |
| 850 |
|
| 851 |
if (nexthop_get(link, nexthop, &existing) < 0) { |
| 852 |
r = nexthop_acquire_id(link->manager, tmp); |
| 853 |
if (r < 0) |
| 854 |
return r; |
| 855 |
} else { |
| 856 |
/* Copy ID */ |
| 857 |
assert(tmp->id == 0 || tmp->id == existing->id); |
| 858 |
tmp->id = existing->id; |
| 859 |
|
| 860 |
/* Copy state for logging below. */ |
| 861 |
tmp->state = existing->state; |
| 862 |
} |
| 863 |
|
| 864 |
if (nexthop_bound_to_link(tmp)) |
| 865 |
tmp->ifindex = link->ifindex; |
| 866 |
|
| 867 |
log_nexthop_debug(tmp, "Requesting", link->manager); |
| 868 |
r = link_queue_request_safe(link, REQUEST_TYPE_NEXTHOP, |
| 869 |
tmp, |
| 870 |
nexthop_unref, |
| 871 |
nexthop_hash_func, |
| 872 |
nexthop_compare_func, |
| 873 |
nexthop_process_request, |
| 874 |
message_counter, |
| 875 |
netlink_handler, |
| 876 |
NULL); |
| 877 |
if (r <= 0) |
| 878 |
return r; |
| 879 |
|
| 880 |
nexthop_enter_requesting(tmp); |
| 881 |
if (existing) |
| 882 |
nexthop_enter_requesting(existing); |
| 883 |
|
| 884 |
TAKE_PTR(tmp); |
| 885 |
return 1; |
| 886 |
} |
| 887 |
|
| 888 |
int link_request_static_nexthops(Link *link, bool only_ipv4) { |
| 889 |
NextHop *nh; |
| 890 |
int r; |
| 891 |
|
| 892 |
assert(link); |
| 893 |
assert(link->network); |
| 894 |
|
| 895 |
link->static_nexthops_configured = false; |
| 896 |
|
| 897 |
ORDERED_HASHMAP_FOREACH(nh, link->network->nexthops_by_section) { |
| 898 |
if (only_ipv4 && nh->family != AF_INET) |
| 899 |
continue; |
| 900 |
|
| 901 |
r = link_request_nexthop(link, nh, &link->static_nexthop_messages, static_nexthop_handler); |
| 902 |
if (r < 0) |
| 903 |
return log_link_warning_errno(link, r, "Could not request nexthop: %m"); |
| 904 |
} |
| 905 |
|
| 906 |
if (link->static_nexthop_messages == 0) { |
| 907 |
link->static_nexthops_configured = true; |
| 908 |
link_check_ready(link); |
| 909 |
} else { |
| 910 |
log_link_debug(link, "Requesting nexthops"); |
| 911 |
link_set_state(link, LINK_STATE_CONFIGURING); |
| 912 |
} |
| 913 |
|
| 914 |
return 0; |
| 915 |
} |
| 916 |
|
| 917 |
static bool nexthop_can_update(const NextHop *assigned_nexthop, const NextHop *requested_nexthop) { |
| 918 |
assert(assigned_nexthop); |
| 919 |
assert(assigned_nexthop->manager); |
| 920 |
assert(requested_nexthop); |
| 921 |
assert(requested_nexthop->network); |
| 922 |
|
| 923 |
/* A group nexthop cannot be replaced with a non-group nexthop, and vice versa. |
| 924 |
* See replace_nexthop_grp() and replace_nexthop_single() in net/ipv4/nexthop.c of the kernel. */ |
| 925 |
if (hashmap_isempty(assigned_nexthop->group) != hashmap_isempty(requested_nexthop->group)) |
| 926 |
return false; |
| 927 |
|
| 928 |
/* There are several more conditions if we can replace a group nexthop, e.g. hash threshold and |
| 929 |
* resilience. But, currently we do not support to modify that. Let's add checks for them in the |
| 930 |
* future when we support to configure them. */ |
| 931 |
|
| 932 |
/* When a nexthop is replaced with a blackhole nexthop, and a group nexthop has multiple nexthops |
| 933 |
* including this nexthop, then the kernel refuses to replace the existing nexthop. |
| 934 |
* So, here, for simplicity, let's unconditionally refuse to replace a non-blackhole nexthop with |
| 935 |
* a blackhole nexthop. See replace_nexthop() in net/ipv4/nexthop.c of the kernel. */ |
| 936 |
if (!assigned_nexthop->blackhole && requested_nexthop->blackhole) |
| 937 |
return false; |
| 938 |
|
| 939 |
return true; |
| 940 |
} |
| 941 |
|
| 942 |
int link_drop_nexthops(Link *link, bool only_static) { |
| 943 |
NextHop *nexthop; |
| 944 |
Link *other; |
| 945 |
int r = 0; |
| 946 |
|
| 947 |
assert(link); |
| 948 |
assert(link->manager); |
| 949 |
|
| 950 |
/* First, mark all nexthops. */ |
| 951 |
HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) { |
| 952 |
/* do not touch nexthop created by the kernel */ |
| 953 |
if (nexthop->protocol == RTPROT_KERNEL) |
| 954 |
continue; |
| 955 |
|
| 956 |
/* Ignore nexthops not assigned yet or already removed. */ |
| 957 |
if (!nexthop_exists(nexthop)) |
| 958 |
continue; |
| 959 |
|
| 960 |
if (!link_should_mark_config(link, only_static, nexthop->source, nexthop->protocol)) |
| 961 |
continue; |
| 962 |
|
| 963 |
/* Ignore nexthops bound to other links. */ |
| 964 |
if (nexthop->ifindex > 0 && nexthop->ifindex != link->ifindex) |
| 965 |
continue; |
| 966 |
|
| 967 |
nexthop_mark(nexthop); |
| 968 |
} |
| 969 |
|
| 970 |
/* Then, unmark all nexthops requested by active links. */ |
| 971 |
HASHMAP_FOREACH(other, link->manager->links_by_index) { |
| 972 |
if (only_static && other == link) |
| 973 |
continue; |
| 974 |
|
| 975 |
if (!IN_SET(other->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) |
| 976 |
continue; |
| 977 |
|
| 978 |
ORDERED_HASHMAP_FOREACH(nexthop, other->network->nexthops_by_section) { |
| 979 |
NextHop *existing; |
| 980 |
|
| 981 |
if (nexthop_get(other, nexthop, &existing) < 0) |
| 982 |
continue; |
| 983 |
|
| 984 |
if (!nexthop_can_update(existing, nexthop)) |
| 985 |
continue; |
| 986 |
|
| 987 |
/* Found matching static configuration. Keep the existing nexthop. */ |
| 988 |
nexthop_unmark(existing); |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
/* Finally, remove all marked nexthops. */ |
| 993 |
HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) { |
| 994 |
if (!nexthop_is_marked(nexthop)) |
| 995 |
continue; |
| 996 |
|
| 997 |
RET_GATHER(r, nexthop_remove(nexthop, link->manager)); |
| 998 |
} |
| 999 |
|
| 1000 |
return r; |
| 1001 |
} |
| 1002 |
|
| 1003 |
void link_forget_nexthops(Link *link) { |
| 1004 |
assert(link); |
| 1005 |
assert(link->manager); |
| 1006 |
assert(link->ifindex > 0); |
| 1007 |
assert(!link_is_up(link)); |
| 1008 |
|
| 1009 |
/* See comments in link_forget_routes(). */ |
| 1010 |
|
| 1011 |
/* Remove all IPv4 nexthops. */ |
| 1012 |
NextHop *nexthop; |
| 1013 |
HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) { |
| 1014 |
if (nexthop->ifindex != link->ifindex) |
| 1015 |
continue; |
| 1016 |
if (nexthop->family != AF_INET) |
| 1017 |
continue; |
| 1018 |
|
| 1019 |
nexthop_forget(link->manager, nexthop, "Forgetting silently removed"); |
| 1020 |
} |
| 1021 |
|
| 1022 |
/* Remove all group nexthops their all members are removed in the above. */ |
| 1023 |
HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) { |
| 1024 |
if (hashmap_isempty(nexthop->group)) |
| 1025 |
continue; |
| 1026 |
|
| 1027 |
/* Update group members. */ |
| 1028 |
struct nexthop_grp *nhg; |
| 1029 |
HASHMAP_FOREACH(nhg, nexthop->group) { |
| 1030 |
if (nexthop_get_by_id(nexthop->manager, nhg->id, NULL) >= 0) |
| 1031 |
continue; |
| 1032 |
|
| 1033 |
assert_se(hashmap_remove(nexthop->group, UINT32_TO_PTR(nhg->id)) == nhg); |
| 1034 |
free(nhg); |
| 1035 |
} |
| 1036 |
|
| 1037 |
if (!hashmap_isempty(nexthop->group)) |
| 1038 |
continue; /* At least one group member still exists. */ |
| 1039 |
|
| 1040 |
nexthop_forget(link->manager, nexthop, "Forgetting silently removed"); |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
static int nexthop_update_group(NextHop *nexthop, sd_netlink_message *message) { |
| 1045 |
_cleanup_hashmap_free_ Hashmap *h = NULL; |
| 1046 |
_cleanup_free_ struct nexthop_grp *group = NULL; |
| 1047 |
size_t size = 0, n_group; |
| 1048 |
int r; |
| 1049 |
|
| 1050 |
assert(nexthop); |
| 1051 |
assert(message); |
| 1052 |
|
| 1053 |
r = sd_netlink_message_read_data(message, NHA_GROUP, &size, (void**) &group); |
| 1054 |
if (r < 0 && r != -ENODATA) |
| 1055 |
return log_debug_errno(r, "rtnl: could not get NHA_GROUP attribute, ignoring: %m"); |
| 1056 |
|
| 1057 |
nexthop_detach_from_group_members(nexthop); |
| 1058 |
|
| 1059 |
if (size % sizeof(struct nexthop_grp) != 0) |
| 1060 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1061 |
"rtnl: received nexthop message with invalid nexthop group size, ignoring."); |
| 1062 |
|
| 1063 |
if ((uintptr_t) group % alignof(struct nexthop_grp) != 0) |
| 1064 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1065 |
"rtnl: received nexthop message with invalid alignment, ignoring."); |
| 1066 |
|
| 1067 |
n_group = size / sizeof(struct nexthop_grp); |
| 1068 |
for (size_t i = 0; i < n_group; i++) { |
| 1069 |
_cleanup_free_ struct nexthop_grp *nhg = NULL; |
| 1070 |
|
| 1071 |
if (group[i].id == 0) { |
| 1072 |
log_debug("rtnl: received nexthop message with invalid ID in group, ignoring."); |
| 1073 |
continue; |
| 1074 |
} |
| 1075 |
|
| 1076 |
if (group[i].weight > 254) { |
| 1077 |
log_debug("rtnl: received nexthop message with invalid weight in group, ignoring."); |
| 1078 |
continue; |
| 1079 |
} |
| 1080 |
|
| 1081 |
nhg = newdup(struct nexthop_grp, group + i, 1); |
| 1082 |
if (!nhg) |
| 1083 |
return log_oom(); |
| 1084 |
|
| 1085 |
r = hashmap_ensure_put(&h, &trivial_hash_ops_value_free, UINT32_TO_PTR(nhg->id), nhg); |
| 1086 |
if (r == -ENOMEM) |
| 1087 |
return log_oom(); |
| 1088 |
if (r < 0) { |
| 1089 |
log_debug_errno(r, "Failed to store nexthop group, ignoring: %m"); |
| 1090 |
continue; |
| 1091 |
} |
| 1092 |
if (r > 0) |
| 1093 |
TAKE_PTR(nhg); |
| 1094 |
} |
| 1095 |
|
| 1096 |
hashmap_free_and_replace(nexthop->group, h); |
| 1097 |
nexthop_attach_to_group_members(nexthop); |
| 1098 |
return 0; |
| 1099 |
} |
| 1100 |
|
| 1101 |
int manager_rtnl_process_nexthop(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) { |
| 1102 |
int r; |
| 1103 |
|
| 1104 |
assert(rtnl); |
| 1105 |
assert(message); |
| 1106 |
assert(m); |
| 1107 |
|
| 1108 |
if (sd_netlink_message_is_error(message)) { |
| 1109 |
r = sd_netlink_message_get_errno(message); |
| 1110 |
if (r < 0) |
| 1111 |
log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring"); |
| 1112 |
|
| 1113 |
return 0; |
| 1114 |
} |
| 1115 |
|
| 1116 |
uint16_t type; |
| 1117 |
r = sd_netlink_message_get_type(message, &type); |
| 1118 |
if (r < 0) { |
| 1119 |
log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
| 1120 |
return 0; |
| 1121 |
} else if (!IN_SET(type, RTM_NEWNEXTHOP, RTM_DELNEXTHOP)) { |
| 1122 |
log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type); |
| 1123 |
return 0; |
| 1124 |
} |
| 1125 |
|
| 1126 |
uint32_t id; |
| 1127 |
r = sd_netlink_message_read_u32(message, NHA_ID, &id); |
| 1128 |
if (r == -ENODATA) { |
| 1129 |
log_warning_errno(r, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m"); |
| 1130 |
return 0; |
| 1131 |
} else if (r < 0) { |
| 1132 |
log_warning_errno(r, "rtnl: could not get NHA_ID attribute, ignoring: %m"); |
| 1133 |
return 0; |
| 1134 |
} else if (id == 0) { |
| 1135 |
log_warning("rtnl: received nexthop message with invalid nexthop ID, ignoring."); |
| 1136 |
return 0; |
| 1137 |
} |
| 1138 |
|
| 1139 |
NextHop *nexthop = NULL; |
| 1140 |
(void) nexthop_get_by_id(m, id, &nexthop); |
| 1141 |
|
| 1142 |
if (type == RTM_DELNEXTHOP) { |
| 1143 |
if (nexthop) |
| 1144 |
nexthop_forget(m, nexthop, "Forgetting removed"); |
| 1145 |
else |
| 1146 |
log_nexthop_debug(&(const NextHop) { .id = id }, "Kernel removed unknown", m); |
| 1147 |
|
| 1148 |
return 0; |
| 1149 |
} |
| 1150 |
|
| 1151 |
Request *req = NULL; |
| 1152 |
(void) nexthop_get_request_by_id(m, id, &req); |
| 1153 |
|
| 1154 |
/* If we did not know the nexthop, then save it. */ |
| 1155 |
bool is_new = false; |
| 1156 |
if (!nexthop) { |
| 1157 |
if (!req && !m->manage_foreign_nexthops) { |
| 1158 |
log_nexthop_debug(&(const NextHop) { .id = id }, "Ignoring received", m); |
| 1159 |
return 0; |
| 1160 |
} |
| 1161 |
|
| 1162 |
r = nexthop_add_new(m, id, &nexthop); |
| 1163 |
if (r < 0) { |
| 1164 |
log_warning_errno(r, "Failed to add received nexthop, ignoring: %m"); |
| 1165 |
return 0; |
| 1166 |
} |
| 1167 |
|
| 1168 |
is_new = true; |
| 1169 |
} |
| 1170 |
|
| 1171 |
/* Also update information that cannot be obtained through netlink notification. */ |
| 1172 |
if (req && req->waiting_reply) { |
| 1173 |
NextHop *n = ASSERT_PTR(req->userdata); |
| 1174 |
|
| 1175 |
nexthop->source = n->source; |
| 1176 |
nexthop->provider = n->provider; |
| 1177 |
} |
| 1178 |
|
| 1179 |
r = sd_rtnl_message_get_family(message, &nexthop->family); |
| 1180 |
if (r < 0) |
| 1181 |
log_debug_errno(r, "rtnl: could not get nexthop family, ignoring: %m"); |
| 1182 |
|
| 1183 |
r = sd_rtnl_message_nexthop_get_protocol(message, &nexthop->protocol); |
| 1184 |
if (r < 0) |
| 1185 |
log_debug_errno(r, "rtnl: could not get nexthop protocol, ignoring: %m"); |
| 1186 |
|
| 1187 |
r = sd_rtnl_message_nexthop_get_flags(message, &nexthop->flags); |
| 1188 |
if (r < 0) |
| 1189 |
log_debug_errno(r, "rtnl: could not get nexthop flags, ignoring: %m"); |
| 1190 |
|
| 1191 |
(void) nexthop_update_group(nexthop, message); |
| 1192 |
|
| 1193 |
if (nexthop->family != AF_UNSPEC) { |
| 1194 |
r = netlink_message_read_in_addr_union(message, NHA_GATEWAY, nexthop->family, &nexthop->gw.address); |
| 1195 |
if (r == -ENODATA) |
| 1196 |
nexthop->gw.address = IN_ADDR_NULL; |
| 1197 |
else if (r < 0) |
| 1198 |
log_debug_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m"); |
| 1199 |
} |
| 1200 |
|
| 1201 |
r = sd_netlink_message_has_flag(message, NHA_BLACKHOLE); |
| 1202 |
if (r < 0) |
| 1203 |
log_debug_errno(r, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m"); |
| 1204 |
else |
| 1205 |
nexthop->blackhole = r; |
| 1206 |
|
| 1207 |
uint32_t ifindex; |
| 1208 |
r = sd_netlink_message_read_u32(message, NHA_OIF, &ifindex); |
| 1209 |
if (r == -ENODATA) |
| 1210 |
nexthop->ifindex = 0; |
| 1211 |
else if (r < 0) |
| 1212 |
log_debug_errno(r, "rtnl: could not get NHA_OIF attribute, ignoring: %m"); |
| 1213 |
else if (ifindex > INT32_MAX) |
| 1214 |
log_debug_errno(r, "rtnl: received invalid NHA_OIF attribute, ignoring: %m"); |
| 1215 |
else |
| 1216 |
nexthop->ifindex = (int) ifindex; |
| 1217 |
|
| 1218 |
/* The linux kernel does not set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. |
| 1219 |
* But let's check that for safety. */ |
| 1220 |
if (!nexthop_bound_to_link(nexthop) && nexthop->ifindex != 0) { |
| 1221 |
log_debug("rtnl: received blackhole or group nexthop with NHA_OIF attribute, ignoring the attribute."); |
| 1222 |
nexthop->ifindex = 0; |
| 1223 |
} |
| 1224 |
|
| 1225 |
nexthop_enter_configured(nexthop); |
| 1226 |
if (req) |
| 1227 |
nexthop_enter_configured(req->userdata); |
| 1228 |
|
| 1229 |
log_nexthop_debug(nexthop, is_new ? "Remembering" : "Received remembered", m); |
| 1230 |
return 1; |
| 1231 |
} |
| 1232 |
|
| 1233 |
#define log_nexthop_section(nexthop, fmt, ...) \ |
| 1234 |
({ \ |
| 1235 |
const NextHop *_nexthop = (nexthop); \ |
| 1236 |
log_section_warning_errno( \ |
| 1237 |
_nexthop ? _nexthop->section : NULL, \ |
| 1238 |
SYNTHETIC_ERRNO(EINVAL), \ |
| 1239 |
fmt " Ignoring [NextHop] section.", \ |
| 1240 |
##__VA_ARGS__); \ |
| 1241 |
}) |
| 1242 |
|
| 1243 |
static int nexthop_section_verify(NextHop *nh) { |
| 1244 |
if (section_is_invalid(nh->section)) |
| 1245 |
return -EINVAL; |
| 1246 |
|
| 1247 |
if (!nh->network->manager->manage_foreign_nexthops && nh->id == 0) |
| 1248 |
return log_nexthop_section(nh, "Nexthop without specifying Id= is not supported if ManageForeignNextHops=no is set in networkd.conf."); |
| 1249 |
|
| 1250 |
if (nh->family == AF_UNSPEC) |
| 1251 |
nh->family = nh->gw.family; |
| 1252 |
else if (nh->gw.family != AF_UNSPEC && nh->gw.family != nh->family) |
| 1253 |
return log_nexthop_section(nh, "Family= and Gateway= settings for nexthop contradict each other."); |
| 1254 |
|
| 1255 |
assert(nh->gw.family == nh->family || nh->gw.family == AF_UNSPEC); |
| 1256 |
|
| 1257 |
if (!hashmap_isempty(nh->group)) { |
| 1258 |
if (in_addr_is_set(nh->family, &nh->gw.address)) |
| 1259 |
return log_nexthop_section(nh, "Nexthop group cannot have gateway address."); |
| 1260 |
|
| 1261 |
if (nh->family != AF_UNSPEC) |
| 1262 |
return log_nexthop_section(nh, "Nexthop group cannot have Family= setting."); |
| 1263 |
|
| 1264 |
if (nh->blackhole) |
| 1265 |
return log_nexthop_section(nh, "Nexthop group cannot be a blackhole."); |
| 1266 |
|
| 1267 |
if (nh->onlink > 0) |
| 1268 |
return log_nexthop_section(nh, "Nexthop group cannot have on-link flag."); |
| 1269 |
|
| 1270 |
} else if (nh->family == AF_UNSPEC) |
| 1271 |
/* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */ |
| 1272 |
nh->family = AF_INET; |
| 1273 |
|
| 1274 |
if (nh->blackhole) { |
| 1275 |
if (in_addr_is_set(nh->family, &nh->gw.address)) |
| 1276 |
return log_nexthop_section(nh, "Blackhole nexthop cannot have gateway address."); |
| 1277 |
|
| 1278 |
if (nh->onlink > 0) |
| 1279 |
return log_nexthop_section(nh, "Blackhole nexthop cannot have on-link flag."); |
| 1280 |
} |
| 1281 |
|
| 1282 |
if (nh->onlink < 0 && in_addr_is_set(nh->family, &nh->gw.address) && |
| 1283 |
ordered_hashmap_isempty(nh->network->addresses_by_section)) { |
| 1284 |
/* If no address is configured, in most cases the gateway cannot be reachable. |
| 1285 |
* TODO: we may need to improve the condition above. */ |
| 1286 |
log_section_warning(nh->section, "Nexthop with Gateway= specified, but no static address configured. Enabling OnLink= option."); |
| 1287 |
nh->onlink = true; |
| 1288 |
} |
| 1289 |
|
| 1290 |
if (nh->onlink >= 0) |
| 1291 |
SET_FLAG(nh->flags, RTNH_F_ONLINK, nh->onlink); |
| 1292 |
|
| 1293 |
return 0; |
| 1294 |
} |
| 1295 |
|
| 1296 |
int network_drop_invalid_nexthops(Network *network) { |
| 1297 |
_cleanup_hashmap_free_ Hashmap *nexthops = NULL; |
| 1298 |
_cleanup_set_free_ Set *duplicated_nexthops = NULL; |
| 1299 |
NextHop *nh; |
| 1300 |
int r; |
| 1301 |
|
| 1302 |
assert(network); |
| 1303 |
|
| 1304 |
ORDERED_HASHMAP_FOREACH(nh, network->nexthops_by_section) { |
| 1305 |
if (nexthop_section_verify(nh) < 0) { |
| 1306 |
nexthop_detach(nh); |
| 1307 |
continue; |
| 1308 |
} |
| 1309 |
|
| 1310 |
if (nh->id == 0) |
| 1311 |
continue; |
| 1312 |
|
| 1313 |
/* Always use the setting specified later. So, remove the previously assigned setting. */ |
| 1314 |
NextHop *dup = hashmap_remove(nexthops, UINT32_TO_PTR(nh->id)); |
| 1315 |
if (dup) { |
| 1316 |
log_warning("%s: Duplicated nexthop settings for ID %"PRIu32" is specified at line %u and %u, " |
| 1317 |
"dropping the nexthop setting specified at line %u.", |
| 1318 |
dup->section->filename, |
| 1319 |
nh->id, nh->section->line, |
| 1320 |
dup->section->line, dup->section->line); |
| 1321 |
|
| 1322 |
/* Do not call nexthop_detach() for 'dup' now, as we can remove only the current |
| 1323 |
* entry in the loop. We will drop the nexthop from nexthops_by_section later. */ |
| 1324 |
r = set_ensure_put(&duplicated_nexthops, &nexthop_hash_ops, dup); |
| 1325 |
if (r < 0) |
| 1326 |
return log_oom(); |
| 1327 |
assert(r > 0); |
| 1328 |
} |
| 1329 |
|
| 1330 |
r = hashmap_ensure_put(&nexthops, NULL, UINT32_TO_PTR(nh->id), nh); |
| 1331 |
if (r < 0) |
| 1332 |
return log_oom(); |
| 1333 |
assert(r > 0); |
| 1334 |
} |
| 1335 |
|
| 1336 |
return 0; |
| 1337 |
} |
| 1338 |
|
| 1339 |
int manager_build_nexthop_ids(Manager *manager) { |
| 1340 |
Network *network; |
| 1341 |
int r; |
| 1342 |
|
| 1343 |
assert(manager); |
| 1344 |
|
| 1345 |
if (!manager->manage_foreign_nexthops) |
| 1346 |
return 0; |
| 1347 |
|
| 1348 |
manager->nexthop_ids = set_free(manager->nexthop_ids); |
| 1349 |
|
| 1350 |
ORDERED_HASHMAP_FOREACH(network, manager->networks) { |
| 1351 |
NextHop *nh; |
| 1352 |
|
| 1353 |
ORDERED_HASHMAP_FOREACH(nh, network->nexthops_by_section) { |
| 1354 |
if (nh->id == 0) |
| 1355 |
continue; |
| 1356 |
|
| 1357 |
r = set_ensure_put(&manager->nexthop_ids, NULL, UINT32_TO_PTR(nh->id)); |
| 1358 |
if (r < 0) |
| 1359 |
return r; |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
return 0; |
| 1364 |
} |
| 1365 |
|
| 1366 |
static int config_parse_nexthop_family( |
| 1367 |
const char *unit, |
| 1368 |
const char *filename, |
| 1369 |
unsigned line, |
| 1370 |
const char *section, |
| 1371 |
unsigned section_line, |
| 1372 |
const char *lvalue, |
| 1373 |
int ltype, |
| 1374 |
const char *rvalue, |
| 1375 |
void *data, |
| 1376 |
void *userdata) { |
| 1377 |
|
| 1378 |
int *family = ASSERT_PTR(data); |
| 1379 |
|
| 1380 |
if (isempty(rvalue)) |
| 1381 |
*family = AF_UNSPEC; |
| 1382 |
else if (streq(rvalue, "ipv4")) |
| 1383 |
*family = AF_INET; |
| 1384 |
else if (streq(rvalue, "ipv6")) |
| 1385 |
*family = AF_INET6; |
| 1386 |
else |
| 1387 |
return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue); |
| 1388 |
|
| 1389 |
return 1; |
| 1390 |
} |
| 1391 |
|
| 1392 |
static int config_parse_nexthop_group( |
| 1393 |
const char *unit, |
| 1394 |
const char *filename, |
| 1395 |
unsigned line, |
| 1396 |
const char *section, |
| 1397 |
unsigned section_line, |
| 1398 |
const char *lvalue, |
| 1399 |
int ltype, |
| 1400 |
const char *rvalue, |
| 1401 |
void *data, |
| 1402 |
void *userdata) { |
| 1403 |
|
| 1404 |
Hashmap **group = ASSERT_PTR(data); |
| 1405 |
int r; |
| 1406 |
|
| 1407 |
if (isempty(rvalue)) { |
| 1408 |
*group = hashmap_free(*group); |
| 1409 |
return 1; |
| 1410 |
} |
| 1411 |
|
| 1412 |
for (const char *p = rvalue;;) { |
| 1413 |
_cleanup_free_ struct nexthop_grp *nhg = NULL; |
| 1414 |
_cleanup_free_ char *word = NULL; |
| 1415 |
uint32_t w; |
| 1416 |
char *sep; |
| 1417 |
|
| 1418 |
r = extract_first_word(&p, &word, NULL, 0); |
| 1419 |
if (r < 0) |
| 1420 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1421 |
if (r == 0) |
| 1422 |
return 1; |
| 1423 |
|
| 1424 |
nhg = new0(struct nexthop_grp, 1); |
| 1425 |
if (!nhg) |
| 1426 |
return log_oom(); |
| 1427 |
|
| 1428 |
sep = strchr(word, ':'); |
| 1429 |
if (sep) { |
| 1430 |
*sep++ = '\0'; |
| 1431 |
r = safe_atou32(sep, &w); |
| 1432 |
if (r < 0) { |
| 1433 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1434 |
"Failed to parse weight for nexthop group, ignoring assignment: %s:%s", |
| 1435 |
word, sep); |
| 1436 |
continue; |
| 1437 |
} |
| 1438 |
if (w == 0 || w > 256) { |
| 1439 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1440 |
"Invalid weight for nexthop group, ignoring assignment: %s:%s", |
| 1441 |
word, sep); |
| 1442 |
continue; |
| 1443 |
} |
| 1444 |
/* See comments in config_parse_multipath_route(). */ |
| 1445 |
nhg->weight = w - 1; |
| 1446 |
} |
| 1447 |
|
| 1448 |
r = safe_atou32(word, &nhg->id); |
| 1449 |
if (r < 0) { |
| 1450 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1451 |
"Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s", |
| 1452 |
lvalue, word, sep ? ":" : "", strempty(sep)); |
| 1453 |
continue; |
| 1454 |
} |
| 1455 |
if (nhg->id == 0) { |
| 1456 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1457 |
"Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s", |
| 1458 |
lvalue, word, sep ? ":" : "", strempty(sep)); |
| 1459 |
continue; |
| 1460 |
} |
| 1461 |
|
| 1462 |
r = hashmap_ensure_put(group, &trivial_hash_ops_value_free, UINT32_TO_PTR(nhg->id), nhg); |
| 1463 |
if (r == -ENOMEM) |
| 1464 |
return log_oom(); |
| 1465 |
if (r == -EEXIST) { |
| 1466 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1467 |
"Nexthop ID %"PRIu32" is specified multiple times in %s=, ignoring assignment: %s%s%s", |
| 1468 |
nhg->id, lvalue, word, sep ? ":" : "", strempty(sep)); |
| 1469 |
continue; |
| 1470 |
} |
| 1471 |
assert(r > 0); |
| 1472 |
TAKE_PTR(nhg); |
| 1473 |
} |
| 1474 |
} |
| 1475 |
|
| 1476 |
int config_parse_nexthop_section( |
| 1477 |
const char *unit, |
| 1478 |
const char *filename, |
| 1479 |
unsigned line, |
| 1480 |
const char *section, |
| 1481 |
unsigned section_line, |
| 1482 |
const char *lvalue, |
| 1483 |
int ltype, |
| 1484 |
const char *rvalue, |
| 1485 |
void *data, |
| 1486 |
void *userdata) { |
| 1487 |
|
| 1488 |
static const ConfigSectionParser table[_NEXTHOP_CONF_PARSER_MAX] = { |
| 1489 |
[NEXTHOP_ID] = { .parser = config_parse_uint32, .ltype = 0, .offset = offsetof(NextHop, id), }, |
| 1490 |
[NEXTHOP_GATEWAY] = { .parser = config_parse_in_addr_data, .ltype = 0, .offset = offsetof(NextHop, gw), }, |
| 1491 |
[NEXTHOP_FAMILY] = { .parser = config_parse_nexthop_family, .ltype = 0, .offset = offsetof(NextHop, family), }, |
| 1492 |
[NEXTHOP_ONLINK] = { .parser = config_parse_tristate, .ltype = 0, .offset = offsetof(NextHop, onlink), }, |
| 1493 |
[NEXTHOP_BLACKHOLE] = { .parser = config_parse_bool, .ltype = 0, .offset = offsetof(NextHop, blackhole), }, |
| 1494 |
[NEXTHOP_GROUP] = { .parser = config_parse_nexthop_group, .ltype = 0, .offset = offsetof(NextHop, group), }, |
| 1495 |
}; |
| 1496 |
|
| 1497 |
_cleanup_(nexthop_unref_or_set_invalidp) NextHop *nexthop = NULL; |
| 1498 |
Network *network = ASSERT_PTR(userdata); |
| 1499 |
int r; |
| 1500 |
|
| 1501 |
assert(filename); |
| 1502 |
|
| 1503 |
r = nexthop_new_static(network, filename, section_line, &nexthop); |
| 1504 |
if (r < 0) |
| 1505 |
return log_oom(); |
| 1506 |
|
| 1507 |
r = config_section_parse(table, ELEMENTSOF(table), |
| 1508 |
unit, filename, line, section, section_line, lvalue, ltype, rvalue, nexthop); |
| 1509 |
if (r <= 0) /* 0 means non-critical error, but the section will be ignored. */ |
| 1510 |
return r; |
| 1511 |
|
| 1512 |
TAKE_PTR(nexthop); |
| 1513 |
return 0; |
| 1514 |
} |