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