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