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