| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-netlink.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "conf-parser.h" |
| 7 |
#include "errno-util.h" |
| 8 |
#include "hashmap.h" |
| 9 |
#include "netlink-util.h" |
| 10 |
#include "networkd-link.h" |
| 11 |
#include "networkd-manager.h" |
| 12 |
#include "networkd-neighbor.h" |
| 13 |
#include "networkd-network.h" |
| 14 |
#include "networkd-queue.h" |
| 15 |
#include "ordered-set.h" |
| 16 |
#include "set.h" |
| 17 |
#include "siphash24.h" |
| 18 |
#include "socket-util.h" |
| 19 |
#include "string-util.h" |
| 20 |
|
| 21 |
static Neighbor* neighbor_detach_impl(Neighbor *neighbor) { |
| 22 |
assert(neighbor); |
| 23 |
assert(!neighbor->link || !neighbor->network); |
| 24 |
|
| 25 |
if (neighbor->network) { |
| 26 |
assert(neighbor->section); |
| 27 |
ordered_hashmap_remove(neighbor->network->neighbors_by_section, neighbor->section); |
| 28 |
neighbor->network = NULL; |
| 29 |
return neighbor; |
| 30 |
} |
| 31 |
|
| 32 |
if (neighbor->link) { |
| 33 |
set_remove(neighbor->link->neighbors, neighbor); |
| 34 |
neighbor->link = NULL; |
| 35 |
return neighbor; |
| 36 |
} |
| 37 |
|
| 38 |
return NULL; |
| 39 |
} |
| 40 |
|
| 41 |
static void neighbor_detach(Neighbor *neighbor) { |
| 42 |
neighbor_unref(neighbor_detach_impl(neighbor)); |
| 43 |
} |
| 44 |
|
| 45 |
static Neighbor* neighbor_free(Neighbor *neighbor) { |
| 46 |
if (!neighbor) |
| 47 |
return NULL; |
| 48 |
|
| 49 |
neighbor_detach_impl(neighbor); |
| 50 |
|
| 51 |
config_section_free(neighbor->section); |
| 52 |
return mfree(neighbor); |
| 53 |
} |
| 54 |
|
| 55 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(Neighbor, neighbor, neighbor_free); |
| 56 |
DEFINE_SECTION_CLEANUP_FUNCTIONS(Neighbor, neighbor_unref); |
| 57 |
|
| 58 |
static void neighbor_hash_func(const Neighbor *neighbor, struct siphash *state); |
| 59 |
static int neighbor_compare_func(const Neighbor *a, const Neighbor *b); |
| 60 |
|
| 61 |
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR( |
| 62 |
neighbor_hash_ops_detach, |
| 63 |
Neighbor, |
| 64 |
neighbor_hash_func, |
| 65 |
neighbor_compare_func, |
| 66 |
neighbor_detach); |
| 67 |
|
| 68 |
DEFINE_PRIVATE_HASH_OPS( |
| 69 |
neighbor_hash_ops, |
| 70 |
Neighbor, |
| 71 |
neighbor_hash_func, |
| 72 |
neighbor_compare_func); |
| 73 |
|
| 74 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 75 |
neighbor_section_hash_ops, |
| 76 |
ConfigSection, |
| 77 |
config_section_hash_func, |
| 78 |
config_section_compare_func, |
| 79 |
Neighbor, |
| 80 |
neighbor_detach); |
| 81 |
|
| 82 |
static int neighbor_new(Neighbor **ret) { |
| 83 |
Neighbor *neighbor; |
| 84 |
|
| 85 |
assert(ret); |
| 86 |
|
| 87 |
neighbor = new(Neighbor, 1); |
| 88 |
if (!neighbor) |
| 89 |
return -ENOMEM; |
| 90 |
|
| 91 |
*neighbor = (Neighbor) { |
| 92 |
.n_ref = 1, |
| 93 |
}; |
| 94 |
|
| 95 |
*ret = TAKE_PTR(neighbor); |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
static int neighbor_new_static(Network *network, const char *filename, unsigned section_line, Neighbor **ret) { |
| 100 |
_cleanup_(config_section_freep) ConfigSection *n = NULL; |
| 101 |
_cleanup_(neighbor_unrefp) Neighbor *neighbor = NULL; |
| 102 |
int r; |
| 103 |
|
| 104 |
assert(network); |
| 105 |
assert(ret); |
| 106 |
assert(filename); |
| 107 |
assert(section_line > 0); |
| 108 |
|
| 109 |
r = config_section_new(filename, section_line, &n); |
| 110 |
if (r < 0) |
| 111 |
return r; |
| 112 |
|
| 113 |
neighbor = ordered_hashmap_get(network->neighbors_by_section, n); |
| 114 |
if (neighbor) { |
| 115 |
*ret = TAKE_PTR(neighbor); |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
r = neighbor_new(&neighbor); |
| 120 |
if (r < 0) |
| 121 |
return r; |
| 122 |
|
| 123 |
neighbor->network = network; |
| 124 |
neighbor->section = TAKE_PTR(n); |
| 125 |
neighbor->source = NETWORK_CONFIG_SOURCE_STATIC; |
| 126 |
|
| 127 |
r = ordered_hashmap_ensure_put(&network->neighbors_by_section, &neighbor_section_hash_ops, neighbor->section, neighbor); |
| 128 |
if (r < 0) |
| 129 |
return r; |
| 130 |
|
| 131 |
*ret = TAKE_PTR(neighbor); |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
static int neighbor_dup(const Neighbor *neighbor, Neighbor **ret) { |
| 136 |
_cleanup_(neighbor_unrefp) Neighbor *dest = NULL; |
| 137 |
|
| 138 |
assert(neighbor); |
| 139 |
assert(ret); |
| 140 |
|
| 141 |
dest = newdup(Neighbor, neighbor, 1); |
| 142 |
if (!dest) |
| 143 |
return -ENOMEM; |
| 144 |
|
| 145 |
/* Clear the reference counter and all pointers */ |
| 146 |
dest->n_ref = 1; |
| 147 |
dest->link = NULL; |
| 148 |
dest->network = NULL; |
| 149 |
dest->section = NULL; |
| 150 |
|
| 151 |
*ret = TAKE_PTR(dest); |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
static void neighbor_hash_func(const Neighbor *neighbor, struct siphash *state) { |
| 156 |
assert(neighbor); |
| 157 |
|
| 158 |
siphash24_compress_typesafe(neighbor->dst_addr.family, state); |
| 159 |
|
| 160 |
if (!IN_SET(neighbor->dst_addr.family, AF_INET, AF_INET6)) |
| 161 |
/* treat any other address family as AF_UNSPEC */ |
| 162 |
return; |
| 163 |
|
| 164 |
/* Equality of neighbors are given by the destination address. |
| 165 |
* See neigh_lookup() in the kernel. */ |
| 166 |
in_addr_hash_func(&neighbor->dst_addr.address, neighbor->dst_addr.family, state); |
| 167 |
} |
| 168 |
|
| 169 |
static int neighbor_compare_func(const Neighbor *a, const Neighbor *b) { |
| 170 |
int r; |
| 171 |
|
| 172 |
r = CMP(a->dst_addr.family, b->dst_addr.family); |
| 173 |
if (r != 0) |
| 174 |
return r; |
| 175 |
|
| 176 |
if (!IN_SET(a->dst_addr.family, AF_INET, AF_INET6)) |
| 177 |
/* treat any other address family as AF_UNSPEC */ |
| 178 |
return 0; |
| 179 |
|
| 180 |
return memcmp(&a->dst_addr.address, &b->dst_addr.address, FAMILY_ADDRESS_SIZE(a->dst_addr.family)); |
| 181 |
} |
| 182 |
|
| 183 |
static int neighbor_get_request(Link *link, const Neighbor *neighbor, Request **ret) { |
| 184 |
Request *req; |
| 185 |
|
| 186 |
assert(link); |
| 187 |
assert(link->manager); |
| 188 |
assert(neighbor); |
| 189 |
|
| 190 |
req = ordered_set_get( |
| 191 |
link->manager->request_queue, |
| 192 |
&(Request) { |
| 193 |
.link = link, |
| 194 |
.type = REQUEST_TYPE_NEIGHBOR, |
| 195 |
.userdata = (void*) neighbor, |
| 196 |
.hash_func = (hash_func_t) neighbor_hash_func, |
| 197 |
.compare_func = (compare_func_t) neighbor_compare_func, |
| 198 |
}); |
| 199 |
if (!req) |
| 200 |
return -ENOENT; |
| 201 |
|
| 202 |
if (ret) |
| 203 |
*ret = req; |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
int neighbor_get(Link *link, const Neighbor *in, Neighbor **ret) { |
| 208 |
Neighbor *existing; |
| 209 |
|
| 210 |
assert(link); |
| 211 |
assert(in); |
| 212 |
|
| 213 |
existing = set_get(link->neighbors, in); |
| 214 |
if (!existing) |
| 215 |
return -ENOENT; |
| 216 |
|
| 217 |
if (ret) |
| 218 |
*ret = existing; |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
static int neighbor_attach(Link *link, Neighbor *neighbor) { |
| 223 |
int r; |
| 224 |
|
| 225 |
assert(link); |
| 226 |
assert(neighbor); |
| 227 |
assert(!neighbor->link); |
| 228 |
|
| 229 |
r = set_ensure_put(&link->neighbors, &neighbor_hash_ops_detach, neighbor); |
| 230 |
if (r < 0) |
| 231 |
return r; |
| 232 |
if (r == 0) |
| 233 |
return -EEXIST; |
| 234 |
|
| 235 |
neighbor->link = link; |
| 236 |
neighbor_ref(neighbor); |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
static void log_neighbor_debug(const Neighbor *neighbor, const char *str, const Link *link) { |
| 241 |
_cleanup_free_ char *state = NULL; |
| 242 |
|
| 243 |
assert(neighbor); |
| 244 |
assert(str); |
| 245 |
|
| 246 |
if (!DEBUG_LOGGING) |
| 247 |
return; |
| 248 |
|
| 249 |
(void) network_config_state_to_string_alloc(neighbor->state, &state); |
| 250 |
|
| 251 |
log_link_debug(link, |
| 252 |
"%s %s neighbor (%s): lladdr: %s, dst: %s", |
| 253 |
str, strna(network_config_source_to_string(neighbor->source)), strna(state), |
| 254 |
HW_ADDR_TO_STR(&neighbor->ll_addr), |
| 255 |
IN_ADDR_TO_STRING(neighbor->dst_addr.family, &neighbor->dst_addr.address)); |
| 256 |
} |
| 257 |
|
| 258 |
static void neighbor_forget(Link *link, Neighbor *neighbor, const char *msg) { |
| 259 |
assert(link); |
| 260 |
assert(neighbor); |
| 261 |
assert(msg); |
| 262 |
|
| 263 |
Request *req; |
| 264 |
if (neighbor_get_request(link, neighbor, &req) >= 0) |
| 265 |
neighbor_enter_removed(req->userdata); |
| 266 |
|
| 267 |
if (!neighbor->link && neighbor_get(link, neighbor, &neighbor) < 0) |
| 268 |
return; |
| 269 |
|
| 270 |
neighbor_enter_removed(neighbor); |
| 271 |
log_neighbor_debug(neighbor, msg, link); |
| 272 |
neighbor_detach(neighbor); |
| 273 |
} |
| 274 |
|
| 275 |
static int neighbor_configure(Neighbor *neighbor, Link *link, Request *req) { |
| 276 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 277 |
int r; |
| 278 |
|
| 279 |
assert(neighbor); |
| 280 |
assert(link); |
| 281 |
assert(link->ifindex > 0); |
| 282 |
assert(link->manager); |
| 283 |
assert(link->manager->rtnl); |
| 284 |
assert(req); |
| 285 |
|
| 286 |
log_neighbor_debug(neighbor, "Configuring", link); |
| 287 |
|
| 288 |
r = sd_rtnl_message_new_neigh(link->manager->rtnl, &m, RTM_NEWNEIGH, |
| 289 |
link->ifindex, neighbor->dst_addr.family); |
| 290 |
if (r < 0) |
| 291 |
return r; |
| 292 |
|
| 293 |
r = sd_rtnl_message_neigh_set_state(m, NUD_PERMANENT); |
| 294 |
if (r < 0) |
| 295 |
return r; |
| 296 |
|
| 297 |
r = netlink_message_append_hw_addr(m, NDA_LLADDR, &neighbor->ll_addr); |
| 298 |
if (r < 0) |
| 299 |
return r; |
| 300 |
|
| 301 |
r = netlink_message_append_in_addr_union(m, NDA_DST, neighbor->dst_addr.family, &neighbor->dst_addr.address); |
| 302 |
if (r < 0) |
| 303 |
return r; |
| 304 |
|
| 305 |
return request_call_netlink_async(link->manager->rtnl, m, req); |
| 306 |
} |
| 307 |
|
| 308 |
static int neighbor_process_request(Request *req, Link *link, Neighbor *neighbor) { |
| 309 |
Neighbor *existing; |
| 310 |
int r; |
| 311 |
|
| 312 |
assert(req); |
| 313 |
assert(link); |
| 314 |
assert(neighbor); |
| 315 |
|
| 316 |
if (!link_is_ready_to_configure(link, false)) |
| 317 |
return 0; |
| 318 |
|
| 319 |
r = neighbor_configure(neighbor, link, req); |
| 320 |
if (r < 0) |
| 321 |
return log_link_warning_errno(link, r, "Failed to configure neighbor: %m"); |
| 322 |
|
| 323 |
neighbor_enter_configuring(neighbor); |
| 324 |
if (neighbor_get(link, neighbor, &existing) >= 0) |
| 325 |
neighbor_enter_configuring(existing); |
| 326 |
|
| 327 |
return 1; |
| 328 |
} |
| 329 |
|
| 330 |
static int static_neighbor_configure_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Neighbor *neighbor) { |
| 331 |
int r; |
| 332 |
|
| 333 |
assert(m); |
| 334 |
assert(link); |
| 335 |
|
| 336 |
r = sd_netlink_message_get_errno(m); |
| 337 |
if (r < 0 && r != -EEXIST) { |
| 338 |
log_link_message_warning_errno(link, m, r, "Could not set neighbor"); |
| 339 |
link_enter_failed(link); |
| 340 |
return 1; |
| 341 |
} |
| 342 |
|
| 343 |
if (link->static_neighbor_messages == 0) { |
| 344 |
log_link_debug(link, "Neighbors set"); |
| 345 |
link->static_neighbors_configured = true; |
| 346 |
link_check_ready(link); |
| 347 |
} |
| 348 |
|
| 349 |
return 1; |
| 350 |
} |
| 351 |
|
| 352 |
static int link_request_neighbor(Link *link, const Neighbor *neighbor) { |
| 353 |
_cleanup_(neighbor_unrefp) Neighbor *tmp = NULL; |
| 354 |
Neighbor *existing = NULL; |
| 355 |
int r; |
| 356 |
|
| 357 |
assert(link); |
| 358 |
assert(neighbor); |
| 359 |
assert(neighbor->source != NETWORK_CONFIG_SOURCE_FOREIGN); |
| 360 |
|
| 361 |
if (neighbor->ll_addr.length != link->hw_addr.length) { |
| 362 |
log_link_debug(link, |
| 363 |
"The link layer address length (%zu) for neighbor %s does not match with " |
| 364 |
"the hardware address length (%zu), ignoring the setting.", |
| 365 |
neighbor->ll_addr.length, |
| 366 |
IN_ADDR_TO_STRING(neighbor->dst_addr.family, &neighbor->dst_addr.address), |
| 367 |
link->hw_addr.length); |
| 368 |
return 0; |
| 369 |
} |
| 370 |
|
| 371 |
if (neighbor_get_request(link, neighbor, NULL) >= 0) |
| 372 |
return 0; /* already requested, skipping. */ |
| 373 |
|
| 374 |
r = neighbor_dup(neighbor, &tmp); |
| 375 |
if (r < 0) |
| 376 |
return r; |
| 377 |
|
| 378 |
if (neighbor_get(link, neighbor, &existing) >= 0) |
| 379 |
/* Copy state for logging below. */ |
| 380 |
tmp->state = existing->state; |
| 381 |
|
| 382 |
log_neighbor_debug(tmp, "Requesting", link); |
| 383 |
r = link_queue_request_safe(link, REQUEST_TYPE_NEIGHBOR, |
| 384 |
tmp, |
| 385 |
neighbor_unref, |
| 386 |
neighbor_hash_func, |
| 387 |
neighbor_compare_func, |
| 388 |
neighbor_process_request, |
| 389 |
&link->static_neighbor_messages, |
| 390 |
static_neighbor_configure_handler, |
| 391 |
NULL); |
| 392 |
if (r <= 0) |
| 393 |
return r; |
| 394 |
|
| 395 |
neighbor_enter_requesting(tmp); |
| 396 |
if (existing) |
| 397 |
neighbor_enter_requesting(existing); |
| 398 |
|
| 399 |
TAKE_PTR(tmp); |
| 400 |
return 1; |
| 401 |
} |
| 402 |
|
| 403 |
int link_request_static_neighbors(Link *link) { |
| 404 |
Neighbor *neighbor; |
| 405 |
int r; |
| 406 |
|
| 407 |
assert(link); |
| 408 |
assert(link->network); |
| 409 |
assert(link->state != _LINK_STATE_INVALID); |
| 410 |
|
| 411 |
link->static_neighbors_configured = false; |
| 412 |
|
| 413 |
ORDERED_HASHMAP_FOREACH(neighbor, link->network->neighbors_by_section) { |
| 414 |
r = link_request_neighbor(link, neighbor); |
| 415 |
if (r < 0) |
| 416 |
return log_link_warning_errno(link, r, "Could not request neighbor: %m"); |
| 417 |
} |
| 418 |
|
| 419 |
if (link->static_neighbor_messages == 0) { |
| 420 |
link->static_neighbors_configured = true; |
| 421 |
link_check_ready(link); |
| 422 |
} else { |
| 423 |
log_link_debug(link, "Requesting neighbors"); |
| 424 |
link_set_state(link, LINK_STATE_CONFIGURING); |
| 425 |
} |
| 426 |
|
| 427 |
return 0; |
| 428 |
} |
| 429 |
|
| 430 |
static int neighbor_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, RemoveRequest *rreq) { |
| 431 |
int r; |
| 432 |
|
| 433 |
assert(m); |
| 434 |
assert(rreq); |
| 435 |
|
| 436 |
Link *link = ASSERT_PTR(rreq->link); |
| 437 |
Neighbor *neighbor = ASSERT_PTR(rreq->userdata); |
| 438 |
|
| 439 |
if (link->state == LINK_STATE_LINGER) |
| 440 |
return 0; |
| 441 |
|
| 442 |
r = sd_netlink_message_get_errno(m); |
| 443 |
if (r < 0) { |
| 444 |
/* Neighbor may not exist because it already got deleted, ignore that. */ |
| 445 |
log_link_message_full_errno(link, m, |
| 446 |
(r == -ESRCH || !neighbor->link) ? LOG_DEBUG : LOG_WARNING, |
| 447 |
r, "Could not remove neighbor"); |
| 448 |
|
| 449 |
/* If the neighbor cannot be removed, then assume the neighbor is already removed. */ |
| 450 |
neighbor_forget(link, neighbor, "Forgetting"); |
| 451 |
} |
| 452 |
|
| 453 |
return 1; |
| 454 |
} |
| 455 |
|
| 456 |
int neighbor_remove(Neighbor *neighbor, Link *link) { |
| 457 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 458 |
int r; |
| 459 |
|
| 460 |
assert(neighbor); |
| 461 |
assert(link); |
| 462 |
assert(link->manager); |
| 463 |
assert(link->manager->rtnl); |
| 464 |
|
| 465 |
/* If the neighbor is remembered, then use the remembered object. */ |
| 466 |
(void) neighbor_get(link, neighbor, &neighbor); |
| 467 |
|
| 468 |
log_neighbor_debug(neighbor, "Removing", link); |
| 469 |
|
| 470 |
r = sd_rtnl_message_new_neigh(link->manager->rtnl, &m, RTM_DELNEIGH, |
| 471 |
link->ifindex, neighbor->dst_addr.family); |
| 472 |
if (r < 0) |
| 473 |
return log_link_error_errno(link, r, "Could not allocate RTM_DELNEIGH message: %m"); |
| 474 |
|
| 475 |
r = netlink_message_append_in_addr_union(m, NDA_DST, neighbor->dst_addr.family, &neighbor->dst_addr.address); |
| 476 |
if (r < 0) |
| 477 |
return log_link_error_errno(link, r, "Could not append NDA_DST attribute: %m"); |
| 478 |
|
| 479 |
r = link_remove_request_add(link, neighbor, neighbor, link->manager->rtnl, m, neighbor_remove_handler); |
| 480 |
if (r < 0) |
| 481 |
return log_link_error_errno(link, r, "Could not queue rtnetlink message: %m"); |
| 482 |
|
| 483 |
neighbor_enter_removing(neighbor); |
| 484 |
return 0; |
| 485 |
} |
| 486 |
|
| 487 |
int link_drop_unmanaged_neighbors(Link *link) { |
| 488 |
Neighbor *neighbor; |
| 489 |
int r = 0; |
| 490 |
|
| 491 |
assert(link); |
| 492 |
assert(link->network); |
| 493 |
|
| 494 |
/* First, mark all neighbors. */ |
| 495 |
SET_FOREACH(neighbor, link->neighbors) { |
| 496 |
/* Ignore neighbors not assigned yet or already removing. */ |
| 497 |
if (!neighbor_exists(neighbor)) |
| 498 |
continue; |
| 499 |
|
| 500 |
if (!link_should_mark_config(link, /* only_static= */ false, neighbor->source, RTPROT_STATIC)) |
| 501 |
continue; |
| 502 |
|
| 503 |
neighbor_mark(neighbor); |
| 504 |
} |
| 505 |
|
| 506 |
/* Next, unmark requested neighbors. They will be configured later. */ |
| 507 |
ORDERED_HASHMAP_FOREACH(neighbor, link->network->neighbors_by_section) { |
| 508 |
Neighbor *existing; |
| 509 |
|
| 510 |
if (neighbor_get(link, neighbor, &existing) >= 0) |
| 511 |
neighbor_unmark(existing); |
| 512 |
} |
| 513 |
|
| 514 |
/* Finally, remove all marked neighbors. */ |
| 515 |
SET_FOREACH(neighbor, link->neighbors) { |
| 516 |
if (!neighbor_is_marked(neighbor)) |
| 517 |
continue; |
| 518 |
|
| 519 |
RET_GATHER(r, neighbor_remove(neighbor, link)); |
| 520 |
} |
| 521 |
|
| 522 |
return r; |
| 523 |
} |
| 524 |
|
| 525 |
int link_drop_static_neighbors(Link *link) { |
| 526 |
Neighbor *neighbor; |
| 527 |
int r = 0; |
| 528 |
|
| 529 |
assert(link); |
| 530 |
|
| 531 |
SET_FOREACH(neighbor, link->neighbors) { |
| 532 |
/* Do not touch nexthops managed by kernel or other tools. */ |
| 533 |
if (neighbor->source != NETWORK_CONFIG_SOURCE_STATIC) |
| 534 |
continue; |
| 535 |
|
| 536 |
/* Ignore neighbors not assigned yet or already removing. */ |
| 537 |
if (!neighbor_exists(neighbor)) |
| 538 |
continue; |
| 539 |
|
| 540 |
RET_GATHER(r, neighbor_remove(neighbor, link)); |
| 541 |
} |
| 542 |
|
| 543 |
return r; |
| 544 |
} |
| 545 |
|
| 546 |
int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) { |
| 547 |
int r; |
| 548 |
|
| 549 |
assert(rtnl); |
| 550 |
assert(message); |
| 551 |
assert(m); |
| 552 |
|
| 553 |
if (sd_netlink_message_is_error(message)) { |
| 554 |
r = sd_netlink_message_get_errno(message); |
| 555 |
if (r < 0) |
| 556 |
log_message_warning_errno(message, r, "rtnl: failed to receive neighbor message, ignoring"); |
| 557 |
|
| 558 |
return 0; |
| 559 |
} |
| 560 |
|
| 561 |
uint16_t type; |
| 562 |
r = sd_netlink_message_get_type(message, &type); |
| 563 |
if (r < 0) { |
| 564 |
log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
| 565 |
return 0; |
| 566 |
} else if (!IN_SET(type, RTM_NEWNEIGH, RTM_DELNEIGH)) { |
| 567 |
log_warning("rtnl: received unexpected message type %u when processing neighbor, ignoring.", type); |
| 568 |
return 0; |
| 569 |
} |
| 570 |
|
| 571 |
uint16_t state; |
| 572 |
r = sd_rtnl_message_neigh_get_state(message, &state); |
| 573 |
if (r < 0) { |
| 574 |
log_warning_errno(r, "rtnl: received neighbor message with invalid state, ignoring: %m"); |
| 575 |
return 0; |
| 576 |
} else if (!FLAGS_SET(state, NUD_PERMANENT)) |
| 577 |
/* Currently, we are interested in only static neighbors. */ |
| 578 |
return 0; |
| 579 |
|
| 580 |
int ifindex; |
| 581 |
r = sd_rtnl_message_neigh_get_ifindex(message, &ifindex); |
| 582 |
if (r < 0) { |
| 583 |
log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m"); |
| 584 |
return 0; |
| 585 |
} else if (ifindex <= 0) { |
| 586 |
log_warning("rtnl: received neighbor message with invalid ifindex %d, ignoring.", ifindex); |
| 587 |
return 0; |
| 588 |
} |
| 589 |
|
| 590 |
Link *link; |
| 591 |
r = link_get_by_index(m, ifindex, &link); |
| 592 |
if (r < 0) |
| 593 |
/* when enumerating we might be out of sync, but we will get the neighbor again. Also, |
| 594 |
* kernel sends messages about neighbors after a link is removed. So, just ignore it. */ |
| 595 |
return 0; |
| 596 |
|
| 597 |
_cleanup_(neighbor_unrefp) Neighbor *tmp = NULL; |
| 598 |
r = neighbor_new(&tmp); |
| 599 |
if (r < 0) |
| 600 |
return log_oom(); |
| 601 |
|
| 602 |
/* First, retrieve the fundamental information about the neighbor. */ |
| 603 |
r = sd_rtnl_message_neigh_get_family(message, &tmp->dst_addr.family); |
| 604 |
if (r < 0) { |
| 605 |
log_link_warning(link, "rtnl: received neighbor message without family, ignoring."); |
| 606 |
return 0; |
| 607 |
} |
| 608 |
if (tmp->dst_addr.family == AF_BRIDGE) /* Currently, we do not support it. */ |
| 609 |
return 0; |
| 610 |
if (!IN_SET(tmp->dst_addr.family, AF_INET, AF_INET6)) { |
| 611 |
log_link_debug(link, "rtnl: received neighbor message with invalid family '%i', ignoring.", tmp->dst_addr.family); |
| 612 |
return 0; |
| 613 |
} |
| 614 |
|
| 615 |
r = netlink_message_read_in_addr_union(message, NDA_DST, tmp->dst_addr.family, &tmp->dst_addr.address); |
| 616 |
if (r < 0) { |
| 617 |
log_link_warning_errno(link, r, "rtnl: received neighbor message without valid address, ignoring: %m"); |
| 618 |
return 0; |
| 619 |
} |
| 620 |
|
| 621 |
/* Then, find the managed Neighbor object corresponding to the netlink notification. */ |
| 622 |
Neighbor *neighbor = NULL; |
| 623 |
(void) neighbor_get(link, tmp, &neighbor); |
| 624 |
|
| 625 |
if (type == RTM_DELNEIGH) { |
| 626 |
if (neighbor) |
| 627 |
neighbor_forget(link, neighbor, "Forgetting removed"); |
| 628 |
else |
| 629 |
log_neighbor_debug(tmp, "Kernel removed unknown", link); |
| 630 |
return 0; |
| 631 |
} |
| 632 |
|
| 633 |
/* If we did not know the neighbor, then save it. */ |
| 634 |
bool is_new = false; |
| 635 |
if (!neighbor) { |
| 636 |
r = neighbor_attach(link, tmp); |
| 637 |
if (r < 0) { |
| 638 |
log_link_warning_errno(link, r, "Failed to save received neighbor, ignoring: %m"); |
| 639 |
return 0; |
| 640 |
} |
| 641 |
neighbor = tmp; |
| 642 |
is_new = true; |
| 643 |
} |
| 644 |
|
| 645 |
/* Also update information that cannot be obtained through netlink notification. */ |
| 646 |
Request *req = NULL; |
| 647 |
(void) neighbor_get_request(link, tmp, &req); |
| 648 |
if (req && req->waiting_reply) { |
| 649 |
Neighbor *n = ASSERT_PTR(req->userdata); |
| 650 |
|
| 651 |
neighbor->source = n->source; |
| 652 |
} |
| 653 |
|
| 654 |
/* Then, update miscellaneous info. */ |
| 655 |
r = netlink_message_read_hw_addr(message, NDA_LLADDR, &neighbor->ll_addr); |
| 656 |
if (r < 0 && r != -ENODATA) |
| 657 |
log_link_debug_errno(link, r, "rtnl: received neighbor message without valid link layer address, ignoring: %m"); |
| 658 |
|
| 659 |
neighbor_enter_configured(neighbor); |
| 660 |
if (req) |
| 661 |
neighbor_enter_configured(req->userdata); |
| 662 |
|
| 663 |
log_neighbor_debug(neighbor, is_new ? "Remembering" : "Received remembered", link); |
| 664 |
return 1; |
| 665 |
} |
| 666 |
|
| 667 |
#define log_neighbor_section(neighbor, fmt, ...) \ |
| 668 |
({ \ |
| 669 |
const Neighbor *_neighbor = (neighbor); \ |
| 670 |
log_section_warning_errno( \ |
| 671 |
_neighbor ? _neighbor->section : NULL, \ |
| 672 |
SYNTHETIC_ERRNO(EINVAL), \ |
| 673 |
fmt " Ignoring [Neighbor] section.", \ |
| 674 |
##__VA_ARGS__); \ |
| 675 |
}) |
| 676 |
|
| 677 |
static int neighbor_section_verify(Neighbor *neighbor) { |
| 678 |
if (section_is_invalid(neighbor->section)) |
| 679 |
return -EINVAL; |
| 680 |
|
| 681 |
if (neighbor->dst_addr.family == AF_UNSPEC) |
| 682 |
return log_neighbor_section(neighbor, "Neighbor section without Address= configured."); |
| 683 |
|
| 684 |
if (neighbor->dst_addr.family == AF_INET6 && !socket_ipv6_is_supported()) |
| 685 |
return log_neighbor_section(neighbor, "Neighbor section with an IPv6 destination address configured, but the kernel does not support IPv6."); |
| 686 |
|
| 687 |
if (neighbor->ll_addr.length == 0) |
| 688 |
return log_neighbor_section(neighbor, "Neighbor section without LinkLayerAddress= configured."); |
| 689 |
|
| 690 |
return 0; |
| 691 |
} |
| 692 |
|
| 693 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 694 |
trivial_hash_ops_neighbor_detach, |
| 695 |
void, |
| 696 |
trivial_hash_func, |
| 697 |
trivial_compare_func, |
| 698 |
Neighbor, |
| 699 |
neighbor_detach); |
| 700 |
|
| 701 |
int network_drop_invalid_neighbors(Network *network) { |
| 702 |
_cleanup_set_free_ Set *neighbors = NULL, *duplicated_neighbors = NULL; |
| 703 |
Neighbor *neighbor; |
| 704 |
int r; |
| 705 |
|
| 706 |
assert(network); |
| 707 |
|
| 708 |
ORDERED_HASHMAP_FOREACH(neighbor, network->neighbors_by_section) { |
| 709 |
Neighbor *dup; |
| 710 |
|
| 711 |
if (neighbor_section_verify(neighbor) < 0) { |
| 712 |
/* Drop invalid [Neighbor] sections. Note that neighbor_detach() will drop the |
| 713 |
* neighbor from neighbors_by_section. */ |
| 714 |
neighbor_detach(neighbor); |
| 715 |
continue; |
| 716 |
} |
| 717 |
|
| 718 |
/* Always use the setting specified later. So, remove the previously assigned setting. */ |
| 719 |
dup = set_remove(neighbors, neighbor); |
| 720 |
if (dup) { |
| 721 |
log_warning("%s: Duplicated neighbor settings for %s is specified at line %u and %u, " |
| 722 |
"dropping the neighbor setting specified at line %u.", |
| 723 |
dup->section->filename, |
| 724 |
IN_ADDR_TO_STRING(neighbor->dst_addr.family, &neighbor->dst_addr.address), |
| 725 |
neighbor->section->line, |
| 726 |
dup->section->line, dup->section->line); |
| 727 |
|
| 728 |
/* Do not call nexthop_detach() for 'dup' now, as we can remove only the current |
| 729 |
* entry in the loop. We will drop the nexthop from nexthops_by_section later. */ |
| 730 |
r = set_ensure_put(&duplicated_neighbors, &trivial_hash_ops_neighbor_detach, dup); |
| 731 |
if (r < 0) |
| 732 |
return log_oom(); |
| 733 |
assert(r > 0); |
| 734 |
} |
| 735 |
|
| 736 |
/* Use neighbor_hash_ops, instead of neighbor_hash_ops_detach. Otherwise, the Neighbor objects |
| 737 |
* will be detached. */ |
| 738 |
r = set_ensure_put(&neighbors, &neighbor_hash_ops, neighbor); |
| 739 |
if (r < 0) |
| 740 |
return log_oom(); |
| 741 |
assert(r > 0); |
| 742 |
} |
| 743 |
|
| 744 |
return 0; |
| 745 |
} |
| 746 |
|
| 747 |
int config_parse_neighbor_section( |
| 748 |
const char *unit, |
| 749 |
const char *filename, |
| 750 |
unsigned line, |
| 751 |
const char *section, |
| 752 |
unsigned section_line, |
| 753 |
const char *lvalue, |
| 754 |
int ltype, |
| 755 |
const char *rvalue, |
| 756 |
void *data, |
| 757 |
void *userdata) { |
| 758 |
|
| 759 |
static const ConfigSectionParser table[_NEIGHBOR_CONF_PARSER_MAX] = { |
| 760 |
[NEIGHBOR_DESTINATION_ADDRESS] = { .parser = config_parse_in_addr_data, .ltype = 0, .offset = offsetof(Neighbor, dst_addr), }, |
| 761 |
[NEIGHBOR_LINK_LAYER_ADDRESS] = { .parser = config_parse_hw_addr, .ltype = 0, .offset = offsetof(Neighbor, ll_addr), }, |
| 762 |
}; |
| 763 |
|
| 764 |
_cleanup_(neighbor_unref_or_set_invalidp) Neighbor *neighbor = NULL; |
| 765 |
Network *network = ASSERT_PTR(userdata); |
| 766 |
int r; |
| 767 |
|
| 768 |
assert(filename); |
| 769 |
|
| 770 |
r = neighbor_new_static(network, filename, section_line, &neighbor); |
| 771 |
if (r < 0) |
| 772 |
return log_oom(); |
| 773 |
|
| 774 |
r = config_section_parse(table, ELEMENTSOF(table), |
| 775 |
unit, filename, line, section, section_line, lvalue, ltype, rvalue, neighbor); |
| 776 |
if (r <= 0) /* 0 means non-critical error, but the section will be ignored. */ |
| 777 |
return r; |
| 778 |
|
| 779 |
TAKE_PTR(neighbor); |
| 780 |
return 0; |
| 781 |
} |