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