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