1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "alloc-util.h"
#include "hashmap.h"
#include "netlink-util.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-neighbor.h"
#include "networkd-network.h"
#include "networkd-queue.h"
#include "set.h"
Neighbor *neighbor_free(Neighbor *neighbor) {
if (!neighbor)
return NULL;
if (neighbor->network) {
assert(neighbor->section);
hashmap_remove(neighbor->network->neighbors_by_section, neighbor->section);
}
config_section_free(neighbor->section);
if (neighbor->link)
set_remove(neighbor->link->neighbors, neighbor);
return mfree(neighbor);
}
DEFINE_SECTION_CLEANUP_FUNCTIONS(Neighbor, neighbor_free);
static int neighbor_new_static(Network *network, const char *filename, unsigned section_line, Neighbor **ret) {
_cleanup_(config_section_freep) ConfigSection *n = NULL;
_cleanup_(neighbor_freep) Neighbor *neighbor = NULL;
int r;
assert(network);
assert(ret);
assert(filename);
assert(section_line > 0);
r = config_section_new(filename, section_line, &n);
if (r < 0)
return r;
neighbor = hashmap_get(network->neighbors_by_section, n);
if (neighbor) {
*ret = TAKE_PTR(neighbor);
return 0;
}
neighbor = new(Neighbor, 1);
if (!neighbor)
return -ENOMEM;
*neighbor = (Neighbor) {
.network = network,
.family = AF_UNSPEC,
.section = TAKE_PTR(n),
.source = NETWORK_CONFIG_SOURCE_STATIC,
};
r = hashmap_ensure_put(&network->neighbors_by_section, &config_section_hash_ops, neighbor->section, neighbor);
if (r < 0)
return r;
*ret = TAKE_PTR(neighbor);
return 0;
}
static int neighbor_dup(const Neighbor *neighbor, Neighbor **ret) {
_cleanup_(neighbor_freep) Neighbor *dest = NULL;
assert(neighbor);
assert(ret);
dest = newdup(Neighbor, neighbor, 1);
if (!dest)
return -ENOMEM;
/* Unset all pointers */
dest->link = NULL;
dest->network = NULL;
dest->section = NULL;
*ret = TAKE_PTR(dest);
return 0;
}
static void neighbor_hash_func(const Neighbor *neighbor, struct siphash *state) {
assert(neighbor);
siphash24_compress(&neighbor->family, sizeof(neighbor->family), state);
switch (neighbor->family) {
case AF_INET:
case AF_INET6:
/* Equality of neighbors are given by the pair (addr,lladdr) */
siphash24_compress(&neighbor->in_addr, FAMILY_ADDRESS_SIZE(neighbor->family), state);
break;
default:
/* treat any other address family as AF_UNSPEC */
break;
}
hw_addr_hash_func(&neighbor->ll_addr, state);
}
static int neighbor_compare_func(const Neighbor *a, const Neighbor *b) {
int r;
r = CMP(a->family, b->family);
if (r != 0)
return r;
switch (a->family) {
case AF_INET:
case AF_INET6:
r = memcmp(&a->in_addr, &b->in_addr, FAMILY_ADDRESS_SIZE(a->family));
if (r != 0)
return r;
}
return hw_addr_compare(&a->ll_addr, &b->ll_addr);
}
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(neighbor_hash_ops, Neighbor, neighbor_hash_func, neighbor_compare_func, neighbor_free);
static int neighbor_get(Link *link, const Neighbor *in, Neighbor **ret) {
Neighbor *existing;
assert(link);
assert(in);
existing = set_get(link->neighbors, in);
if (!existing)
return -ENOENT;
if (ret)
*ret = existing;
return 0;
}
static int neighbor_add(Link *link, Neighbor *neighbor) {
int r;
assert(link);
assert(neighbor);
r = set_ensure_put(&link->neighbors, &neighbor_hash_ops, neighbor);
if (r < 0)
return r;
if (r == 0)
return -EEXIST;
neighbor->link = link;
return 0;
}
static void log_neighbor_debug(const Neighbor *neighbor, const char *str, const Link *link) {
_cleanup_free_ char *state = NULL;
assert(neighbor);
assert(str);
if (!DEBUG_LOGGING)
return;
(void) network_config_state_to_string_alloc(neighbor->state, &state);
log_link_debug(link,
"%s %s neighbor (%s): lladdr: %s, dst: %s",
str, strna(network_config_source_to_string(neighbor->source)), strna(state),
HW_ADDR_TO_STR(&neighbor->ll_addr),
IN_ADDR_TO_STRING(neighbor->family, &neighbor->in_addr));
}
static int neighbor_configure(Neighbor *neighbor, Link *link, Request *req) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
int r;
assert(neighbor);
assert(link);
assert(link->ifindex > 0);
assert(link->manager);
assert(link->manager->rtnl);
assert(req);
log_neighbor_debug(neighbor, "Configuring", link);
r = sd_rtnl_message_new_neigh(link->manager->rtnl, &m, RTM_NEWNEIGH,
link->ifindex, neighbor->family);
if (r < 0)
return r;
r = sd_rtnl_message_neigh_set_state(m, NUD_PERMANENT);
if (r < 0)
return r;
r = netlink_message_append_hw_addr(m, NDA_LLADDR, &neighbor->ll_addr);
if (r < 0)
return r;
r = netlink_message_append_in_addr_union(m, NDA_DST, neighbor->family, &neighbor->in_addr);
if (r < 0)
return r;
return request_call_netlink_async(link->manager->rtnl, m, req);
}
static int neighbor_process_request(Request *req, Link *link, Neighbor *neighbor) {
int r;
assert(req);
assert(link);
assert(neighbor);
if (!link_is_ready_to_configure(link, false))
return 0;
r = neighbor_configure(neighbor, link, req);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to configure neighbor: %m");
neighbor_enter_configuring(neighbor);
return 1;
}
static int static_neighbor_configure_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Neighbor *neighbor) {
int r;
assert(m);
assert(link);
r = sd_netlink_message_get_errno(m);
if (r < 0 && r != -EEXIST) {
log_link_message_warning_errno(link, m, r, "Could not set neighbor");
link_enter_failed(link);
return 1;
}
if (link->static_neighbor_messages == 0) {
log_link_debug(link, "Neighbors set");
link->static_neighbors_configured = true;
link_check_ready(link);
}
return 1;
}
static int link_request_neighbor(Link *link, const Neighbor *neighbor) {
Neighbor *existing;
int r;
assert(link);
assert(neighbor);
assert(neighbor->source != NETWORK_CONFIG_SOURCE_FOREIGN);
if (neighbor_get(link, neighbor, &existing) < 0) {
_cleanup_(neighbor_freep) Neighbor *tmp = NULL;
r = neighbor_dup(neighbor, &tmp);
if (r < 0)
return r;
r = neighbor_add(link, tmp);
if (r < 0)
return r;
existing = TAKE_PTR(tmp);
} else
existing->source = neighbor->source;
log_neighbor_debug(existing, "Requesting", link);
r = link_queue_request_safe(link, REQUEST_TYPE_NEIGHBOR,
existing, NULL,
neighbor_hash_func,
neighbor_compare_func,
neighbor_process_request,
&link->static_neighbor_messages,
static_neighbor_configure_handler,
NULL);
if (r <= 0)
return r;
neighbor_enter_requesting(existing);
return 1;
}
int link_request_static_neighbors(Link *link) {
Neighbor *neighbor;
int r;
assert(link);
assert(link->network);
assert(link->state != _LINK_STATE_INVALID);
link->static_neighbors_configured = false;
HASHMAP_FOREACH(neighbor, link->network->neighbors_by_section) {
r = link_request_neighbor(link, neighbor);
if (r < 0)
return log_link_warning_errno(link, r, "Could not request neighbor: %m");
}
if (link->static_neighbor_messages == 0) {
link->static_neighbors_configured = true;
link_check_ready(link);
} else {
log_link_debug(link, "Requesting neighbors");
link_set_state(link, LINK_STATE_CONFIGURING);
}
return 0;
}
static int neighbor_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
int r;
assert(m);
assert(link);
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
return 1;
r = sd_netlink_message_get_errno(m);
if (r < 0 && r != -ESRCH)
/* Neighbor may not exist because it already got deleted, ignore that. */
log_link_message_warning_errno(link, m, r, "Could not remove neighbor");
return 1;
}
static int neighbor_remove(Neighbor *neighbor) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
Link *link;
int r;
assert(neighbor);
assert(neighbor->link);
assert(neighbor->link->manager);
assert(neighbor->link->manager->rtnl);
link = neighbor->link;
log_neighbor_debug(neighbor, "Removing", link);
r = sd_rtnl_message_new_neigh(link->manager->rtnl, &m, RTM_DELNEIGH,
link->ifindex, neighbor->family);
if (r < 0)
return log_link_error_errno(link, r, "Could not allocate RTM_DELNEIGH message: %m");
r = netlink_message_append_in_addr_union(m, NDA_DST, neighbor->family, &neighbor->in_addr);
if (r < 0)
return log_link_error_errno(link, r, "Could not append NDA_DST attribute: %m");
r = netlink_call_async(link->manager->rtnl, NULL, m, neighbor_remove_handler,
link_netlink_destroy_callback, link);
if (r < 0)
return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
link_ref(link);
neighbor_enter_removing(neighbor);
return 0;
}
int link_drop_foreign_neighbors(Link *link) {
Neighbor *neighbor;
int r = 0;
assert(link);
assert(link->network);
/* First, mark all neighbors. */
SET_FOREACH(neighbor, link->neighbors) {
/* Do not remove neighbors we configured. */
if (neighbor->source != NETWORK_CONFIG_SOURCE_FOREIGN)
continue;
/* Ignore neighbors not assigned yet or already removing. */
if (!neighbor_exists(neighbor))
continue;
neighbor_mark(neighbor);
}
/* Next, unmark requested neighbors. They will be configured later. */
HASHMAP_FOREACH(neighbor, link->network->neighbors_by_section) {
Neighbor *existing;
if (neighbor_get(link, neighbor, &existing) >= 0)
neighbor_unmark(existing);
}
SET_FOREACH(neighbor, link->neighbors) {
if (!neighbor_is_marked(neighbor))
continue;
RET_GATHER(r, neighbor_remove(neighbor));
}
return r;
}
int link_drop_managed_neighbors(Link *link) {
Neighbor *neighbor;
int r = 0;
assert(link);
SET_FOREACH(neighbor, link->neighbors) {
/* Do not touch nexthops managed by kernel or other tools. */
if (neighbor->source == NETWORK_CONFIG_SOURCE_FOREIGN)
continue;
/* Ignore neighbors not assigned yet or already removing. */
if (!neighbor_exists(neighbor))
continue;
RET_GATHER(r, neighbor_remove(neighbor));
}
return r;
}
void link_foreignize_neighbors(Link *link) {
Neighbor *neighbor;
assert(link);
SET_FOREACH(neighbor, link->neighbors)
neighbor->source = NETWORK_CONFIG_SOURCE_FOREIGN;
}
int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
_cleanup_(neighbor_freep) Neighbor *tmp = NULL;
Neighbor *neighbor = NULL;
uint16_t type, state;
int ifindex, r;
Link *link;
assert(rtnl);
assert(message);
assert(m);
if (sd_netlink_message_is_error(message)) {
r = sd_netlink_message_get_errno(message);
if (r < 0)
log_message_warning_errno(message, r, "rtnl: failed to receive neighbor message, ignoring");
return 0;
}
r = sd_netlink_message_get_type(message, &type);
if (r < 0) {
log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
return 0;
} else if (!IN_SET(type, RTM_NEWNEIGH, RTM_DELNEIGH)) {
log_warning("rtnl: received unexpected message type %u when processing neighbor, ignoring.", type);
return 0;
}
r = sd_rtnl_message_neigh_get_state(message, &state);
if (r < 0) {
log_warning_errno(r, "rtnl: received neighbor message with invalid state, ignoring: %m");
return 0;
} else if (!FLAGS_SET(state, NUD_PERMANENT)) {
log_debug("rtnl: received non-static neighbor, ignoring.");
return 0;
}
r = sd_rtnl_message_neigh_get_ifindex(message, &ifindex);
if (r < 0) {
log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
return 0;
} else if (ifindex <= 0) {
log_warning("rtnl: received neighbor message with invalid ifindex %d, ignoring.", ifindex);
return 0;
}
r = link_get_by_index(m, ifindex, &link);
if (r < 0 || !link) {
/* when enumerating we might be out of sync, but we will get the neighbor again. Also,
* kernel sends messages about neighbors after a link is removed. So, just ignore it. */
log_debug("rtnl: received neighbor for link '%d' we don't know about, ignoring.", ifindex);
return 0;
}
tmp = new0(Neighbor, 1);
r = sd_rtnl_message_neigh_get_family(message, &tmp->family);
if (r < 0) {
log_link_warning(link, "rtnl: received neighbor message without family, ignoring.");
return 0;
} else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
log_link_debug(link, "rtnl: received neighbor message with invalid family '%i', ignoring.", tmp->family);
return 0;
}
r = netlink_message_read_in_addr_union(message, NDA_DST, tmp->family, &tmp->in_addr);
if (r < 0) {
log_link_warning_errno(link, r, "rtnl: received neighbor message without valid address, ignoring: %m");
return 0;
}
r = netlink_message_read_hw_addr(message, NDA_LLADDR, &tmp->ll_addr);
if (r < 0) {
log_link_warning_errno(link, r, "rtnl: received neighbor message without valid link layer address, ignoring: %m");
return 0;
}
(void) neighbor_get(link, tmp, &neighbor);
switch (type) {
case RTM_NEWNEIGH:
if (neighbor) {
neighbor_enter_configured(neighbor);
log_neighbor_debug(neighbor, "Received remembered", link);
} else {
neighbor_enter_configured(tmp);
log_neighbor_debug(tmp, "Remembering", link);
r = neighbor_add(link, tmp);
if (r < 0) {
log_link_warning_errno(link, r, "Failed to remember foreign neighbor, ignoring: %m");
return 0;
}
TAKE_PTR(tmp);
}
break;
case RTM_DELNEIGH:
if (neighbor) {
neighbor_enter_removed(neighbor);
if (neighbor->state == 0) {
log_neighbor_debug(neighbor, "Forgetting", link);
neighbor_free(neighbor);
} else
log_neighbor_debug(neighbor, "Removed", link);
} else
log_neighbor_debug(tmp, "Kernel removed unknown", link);
break;
default:
assert_not_reached();
}
return 1;
}
static int neighbor_section_verify(Neighbor *neighbor) {
if (section_is_invalid(neighbor->section))
return -EINVAL;
if (neighbor->family == AF_UNSPEC)
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
"%s: Neighbor section without Address= configured. "
"Ignoring [Neighbor] section from line %u.",
neighbor->section->filename, neighbor->section->line);
if (neighbor->family == AF_INET6 && !socket_ipv6_is_supported())
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
"%s: Neighbor section with an IPv6 destination address configured, "
"but the kernel does not support IPv6. "
"Ignoring [Neighbor] section from line %u.",
neighbor->section->filename, neighbor->section->line);
if (neighbor->ll_addr.length == 0)
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
"%s: Neighbor section without LinkLayerAddress= configured. "
"Ignoring [Neighbor] section from line %u.",
neighbor->section->filename, neighbor->section->line);
return 0;
}
void network_drop_invalid_neighbors(Network *network) {
Neighbor *neighbor;
assert(network);
HASHMAP_FOREACH(neighbor, network->neighbors_by_section)
if (neighbor_section_verify(neighbor) < 0)
neighbor_free(neighbor);
}
int config_parse_neighbor_address(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_(neighbor_free_or_set_invalidp) Neighbor *n = NULL;
Network *network = ASSERT_PTR(userdata);
int r;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
r = neighbor_new_static(network, filename, section_line, &n);
if (r < 0)
return log_oom();
if (isempty(rvalue)) {
n->family = AF_UNSPEC;
n->in_addr = IN_ADDR_NULL;
TAKE_PTR(n);
return 0;
}
r = in_addr_from_string_auto(rvalue, &n->family, &n->in_addr);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Neighbor Address is invalid, ignoring assignment: %s", rvalue);
return 0;
}
TAKE_PTR(n);
return 0;
}
int config_parse_neighbor_lladdr(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_(neighbor_free_or_set_invalidp) Neighbor *n = NULL;
Network *network = ASSERT_PTR(userdata);
int r;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
r = neighbor_new_static(network, filename, section_line, &n);
if (r < 0)
return log_oom();
if (isempty(rvalue)) {
n->ll_addr = HW_ADDR_NULL;
TAKE_PTR(n);
return 0;
}
r = parse_hw_addr(rvalue, &n->ll_addr);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Neighbor %s= is invalid, ignoring assignment: %s",
lvalue, rvalue);
return 0;
}
TAKE_PTR(n);
return 0;
}
|