Debian Codegraph @

src/network/networkd-route-nexthop.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/nexthop.h>
4 #include <stdio.h>
5
6 #include "sd-netlink.h"
7
8 #include "alloc-util.h"
9 #include "conf-parser.h"
10 #include "extract-word.h"
11 #include "netlink-util.h"
12 #include "networkd-link.h"
13 #include "networkd-manager.h"
14 #include "networkd-network.h"
15 #include "networkd-nexthop.h"
16 #include "networkd-route.h"
17 #include "networkd-route-nexthop.h"
18 #include "networkd-route-util.h"
19 #include "ordered-set.h"
20 #include "parse-util.h"
21 #include "set.h"
22 #include "siphash24.h"
23 #include "socket-util.h"
24 #include "string-util.h"
25
26 void route_detach_from_nexthop(Route *route) {
27 NextHop *nh;
28
29 assert(route);
30 assert(route->manager);
31
32 if (route->nexthop_id == 0)
33 return;
34
35 if (nexthop_get_by_id(route->manager, route->nexthop_id, &nh) < 0)
36 return;
37
38 route_unref(set_remove(nh->routes, route));
39 }
40
41 void route_attach_to_nexthop(Route *route) {
42 NextHop *nh;
43 int r;
44
45 assert(route);
46 assert(route->manager);
47
48 if (route->nexthop_id == 0)
49 return;
50
51 r = nexthop_get_by_id(route->manager, route->nexthop_id, &nh);
52 if (r < 0) {
53 if (route->manager->manage_foreign_nexthops)
54 log_debug_errno(r, "Route has unknown nexthop ID (%"PRIu32"), ignoring.",
55 route->nexthop_id);
56 return;
57 }
58
59 r = set_ensure_put(&nh->routes, &route_hash_ops_unref, route);
60 if (r < 0)
61 return (void) log_debug_errno(r, "Failed to save route to nexthop, ignoring: %m");
62 if (r == 0)
63 return (void) log_debug("Duplicated route assigned to nexthop, ignoring.");
64
65 route_ref(route);
66 }
67
68 static void route_nexthop_done(RouteNextHop *nh) {
69 assert(nh);
70
71 free(nh->ifname);
72 }
73
74 RouteNextHop* route_nexthop_free(RouteNextHop *nh) {
75 if (!nh)
76 return NULL;
77
78 route_nexthop_done(nh);
79
80 return mfree(nh);
81 }
82
83 void route_nexthops_done(Route *route) {
84 assert(route);
85
86 route_nexthop_done(&route->nexthop);
87 ordered_set_free(route->nexthops);
88 }
89
90 static void route_nexthop_hash_func_full(const RouteNextHop *nh, struct siphash *state, bool with_weight) {
91 assert(nh);
92 assert(state);
93
94 /* See nh_comp() in net/ipv4/fib_semantics.c of the kernel. */
95
96 siphash24_compress_typesafe(nh->family, state);
97
98 /* For device-only nexthops parsed from config, family is AF_UNSPEC until verification.
99 * We still need to hash weight/ifindex/ifname to distinguish different device-only entries. */
100 if (IN_SET(nh->family, AF_INET, AF_INET6))
101 in_addr_hash_func(&nh->gw, nh->family, state);
102
103 if (with_weight)
104 siphash24_compress_typesafe(nh->weight, state);
105 siphash24_compress_typesafe(nh->ifindex, state);
106 if (nh->ifindex == 0)
107 siphash24_compress_string(nh->ifname, state); /* For Network or Request object. */
108 }
109
110 static int route_nexthop_compare_func_full(const RouteNextHop *a, const RouteNextHop *b, bool with_weight) {
111 int r;
112
113 assert(a);
114 assert(b);
115
116 r = CMP(a->family, b->family);
117 if (r != 0)
118 return r;
119
120 /* For device-only nexthops parsed from config, family is AF_UNSPEC until verification.
121 * We still need to compare weight/ifindex/ifname to distinguish different device-only entries. */
122 if (IN_SET(a->family, AF_INET, AF_INET6)) {
123 r = memcmp(&a->gw, &b->gw, FAMILY_ADDRESS_SIZE(a->family));
124 if (r != 0)
125 return r;
126 }
127
128 if (with_weight) {
129 r = CMP(a->weight, b->weight);
130 if (r != 0)
131 return r;
132 }
133
134 r = CMP(a->ifindex, b->ifindex);
135 if (r != 0)
136 return r;
137
138 if (a->ifindex == 0) {
139 r = strcmp_ptr(a->ifname, b->ifname);
140 if (r != 0)
141 return r;
142 }
143
144 return 0;
145 }
146
147 static void route_nexthop_hash_func(const RouteNextHop *nh, struct siphash *state) {
148 route_nexthop_hash_func_full(nh, state, /* with_weight= */ true);
149 }
150
151 static int route_nexthop_compare_func(const RouteNextHop *a, const RouteNextHop *b) {
152 return route_nexthop_compare_func_full(a, b, /* with_weight= */ true);
153 }
154
155 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
156 route_nexthop_hash_ops,
157 RouteNextHop,
158 route_nexthop_hash_func,
159 route_nexthop_compare_func,
160 route_nexthop_free);
161
162 static size_t route_n_nexthops(const Route *route) {
163 if (route->nexthop_id != 0 || route_is_reject(route))
164 return 0;
165
166 if (ordered_set_isempty(route->nexthops))
167 return 1;
168
169 return ordered_set_size(route->nexthops);
170 }
171
172 void route_nexthops_hash_func(const Route *route, struct siphash *state) {
173 assert(route);
174
175 size_t nhs = route_n_nexthops(route);
176 siphash24_compress_typesafe(nhs, state);
177
178 switch (nhs) {
179 case 0:
180 siphash24_compress_typesafe(route->nexthop_id, state);
181 return;
182
183 case 1:
184 route_nexthop_hash_func_full(&route->nexthop, state, /* with_weight= */ false);
185 return;
186
187 default: {
188 RouteNextHop *nh;
189 ORDERED_SET_FOREACH(nh, route->nexthops)
190 route_nexthop_hash_func(nh, state);
191 }}
192 }
193
194 int route_nexthops_compare_func(const Route *a, const Route *b) {
195 int r;
196
197 assert(a);
198 assert(b);
199
200 size_t a_nhs = route_n_nexthops(a);
201 size_t b_nhs = route_n_nexthops(b);
202 r = CMP(a_nhs, b_nhs);
203 if (r != 0)
204 return r;
205
206 switch (a_nhs) {
207 case 0:
208 return CMP(a->nexthop_id, b->nexthop_id);
209
210 case 1:
211 return route_nexthop_compare_func_full(&a->nexthop, &b->nexthop, /* with_weight= */ false);
212
213 default: {
214 RouteNextHop *nh;
215 ORDERED_SET_FOREACH(nh, a->nexthops) {
216 r = CMP(nh, (RouteNextHop*) ordered_set_get(a->nexthops, nh));
217 if (r != 0)
218 return r;
219 }
220 return 0;
221 }}
222 }
223
224 static int route_nexthop_copy(const RouteNextHop *src, RouteNextHop *dest) {
225 assert(src);
226 assert(dest);
227
228 *dest = *src;
229
230 /* unset pointer copied in the above. */
231 dest->ifname = NULL;
232
233 return strdup_to(&dest->ifname, src->ifindex > 0 ? NULL : src->ifname);
234 }
235
236 static int route_nexthop_dup(const RouteNextHop *src, RouteNextHop **ret) {
237 _cleanup_(route_nexthop_freep) RouteNextHop *dest = NULL;
238 int r;
239
240 assert(src);
241 assert(ret);
242
243 dest = new(RouteNextHop, 1);
244 if (!dest)
245 return -ENOMEM;
246
247 r = route_nexthop_copy(src, dest);
248 if (r < 0)
249 return r;
250
251 *ret = TAKE_PTR(dest);
252 return 0;
253 }
254
255 int route_nexthops_copy(const Route *src, const RouteNextHop *nh, Route *dest) {
256 int r;
257
258 assert(src);
259 assert(dest);
260
261 if (src->nexthop_id != 0 || route_is_reject(src))
262 return 0;
263
264 if (nh)
265 return route_nexthop_copy(nh, &dest->nexthop);
266
267 if (ordered_set_isempty(src->nexthops))
268 return route_nexthop_copy(&src->nexthop, &dest->nexthop);
269
270 ORDERED_SET_FOREACH(nh, src->nexthops) {
271 _cleanup_(route_nexthop_freep) RouteNextHop *nh_dup = NULL;
272
273 r = route_nexthop_dup(nh, &nh_dup);
274 if (r < 0)
275 return r;
276
277 r = ordered_set_ensure_put(&dest->nexthops, &route_nexthop_hash_ops, nh_dup);
278 if (r < 0)
279 return r;
280 assert(r > 0);
281
282 TAKE_PTR(nh_dup);
283 }
284
285 return 0;
286 }
287
288 static bool multipath_routes_needs_adjust(const Route *route) {
289 assert(route);
290
291 RouteNextHop *nh;
292 ORDERED_SET_FOREACH(nh, route->nexthops)
293 if (route->nexthop.ifindex == 0)
294 return true;
295
296 return false;
297 }
298
299 bool route_nexthops_needs_adjust(const Route *route) {
300 assert(route);
301
302 if (route->nexthop_id != 0)
303 /* At this stage, the nexthop may not be configured, or may be under reconfiguring.
304 * Hence, we cannot know if the nexthop is blackhole or not. */
305 return route->type != RTN_BLACKHOLE;
306
307 if (route_is_reject(route))
308 return false;
309
310 if (ordered_set_isempty(route->nexthops))
311 return route->nexthop.ifindex == 0;
312
313 return multipath_routes_needs_adjust(route);
314 }
315
316 static bool route_nexthop_set_ifindex(RouteNextHop *nh, Link *link) {
317 assert(nh);
318 assert(link);
319 assert(link->manager);
320
321 if (nh->ifindex > 0) {
322 nh->ifname = mfree(nh->ifname);
323 return false;
324 }
325
326 /* If an interface name is specified, use it. Otherwise, use the interface that requests this route. */
327 if (nh->ifname && link_get_by_name(link->manager, nh->ifname, &link) < 0)
328 return false;
329
330 nh->ifindex = link->ifindex;
331 nh->ifname = mfree(nh->ifname);
332 return true; /* updated */
333 }
334
335 int route_adjust_nexthops(Route *route, Link *link) {
336 int r;
337
338 assert(route);
339 assert(link);
340 assert(link->manager);
341
342 /* When an IPv4 route has nexthop id and the nexthop type is blackhole, even though kernel sends
343 * RTM_NEWROUTE netlink message with blackhole type, kernel's internal route type fib_rt_info::type
344 * may not be blackhole. Thus, we cannot know the internal value. Moreover, on route removal, the
345 * matching is done with the hidden value if we set non-zero type in RTM_DELROUTE message. So,
346 * here let's set route type to BLACKHOLE when the nexthop is blackhole. */
347 if (route->nexthop_id != 0) {
348 NextHop *nexthop;
349
350 r = nexthop_is_ready(link->manager, route->nexthop_id, &nexthop);
351 if (r <= 0)
352 return r; /* r == 0 means the nexthop is under (re-)configuring.
353 * We cannot use the currently remembered information. */
354
355 if (!nexthop->blackhole)
356 return false;
357
358 if (route->type == RTN_BLACKHOLE)
359 return false;
360
361 route->type = RTN_BLACKHOLE;
362 return true; /* updated */
363 }
364
365 if (route_is_reject(route))
366 return false;
367
368 if (ordered_set_isempty(route->nexthops))
369 return route_nexthop_set_ifindex(&route->nexthop, link);
370
371 if (!multipath_routes_needs_adjust(route))
372 return false;
373
374 _cleanup_ordered_set_free_ OrderedSet *nexthops = NULL;
375 for (;;) {
376 _cleanup_(route_nexthop_freep) RouteNextHop *nh = NULL;
377
378 nh = ordered_set_steal_first(route->nexthops);
379 if (!nh)
380 break;
381
382 (void) route_nexthop_set_ifindex(nh, link);
383
384 r = ordered_set_ensure_put(&nexthops, &route_nexthop_hash_ops, nh);
385 if (r == -EEXIST)
386 continue; /* Duplicated? Let's drop the nexthop. */
387 if (r < 0)
388 return r;
389 assert(r > 0);
390
391 TAKE_PTR(nh);
392 }
393
394 ordered_set_free(route->nexthops);
395 route->nexthops = TAKE_PTR(nexthops);
396 return true; /* updated */
397 }
398
399 int route_nexthop_get_link(Manager *manager, const RouteNextHop *nh, Link **ret) {
400 assert(manager);
401 assert(nh);
402
403 if (nh->ifindex > 0)
404 return link_get_by_index(manager, nh->ifindex, ret);
405 if (nh->ifname)
406 return link_get_by_name(manager, nh->ifname, ret);
407
408 return -ENOENT;
409 }
410
411 static bool route_nexthop_is_ready_to_configure(const RouteNextHop *nh, Manager *manager, bool onlink) {
412 Link *link;
413
414 assert(nh);
415 assert(manager);
416
417 if (route_nexthop_get_link(manager, nh, &link) < 0)
418 return false;
419
420 if (!link_is_ready_to_configure(link, /* allow_unmanaged= */ true))
421 return false;
422
423 /* If the interface is not managed by us, we request that the interface has carrier.
424 * That is, ConfigureWithoutCarrier=no is the default even for unamanaged interfaces. */
425 if (!link->network && !link_has_carrier(link))
426 return false;
427
428 return gateway_is_ready(link, onlink, nh->family, &nh->gw);
429 }
430
431 int route_nexthops_is_ready_to_configure(const Route *route, Manager *manager) {
432 int r;
433
434 assert(route);
435 assert(manager);
436
437 if (route->nexthop_id != 0) {
438 struct nexthop_grp *nhg;
439 NextHop *nh;
440
441 r = nexthop_is_ready(manager, route->nexthop_id, &nh);
442 if (r <= 0)
443 return r;
444
445 HASHMAP_FOREACH(nhg, nh->group) {
446 r = nexthop_is_ready(manager, nhg->id, NULL);
447 if (r <= 0)
448 return r;
449 }
450
451 return true;
452 }
453
454 if (route_is_reject(route))
455 return true;
456
457 if (ordered_set_isempty(route->nexthops))
458 return route_nexthop_is_ready_to_configure(&route->nexthop, manager, FLAGS_SET(route->flags, RTNH_F_ONLINK));
459
460 RouteNextHop *nh;
461 ORDERED_SET_FOREACH(nh, route->nexthops)
462 if (!route_nexthop_is_ready_to_configure(nh, manager, FLAGS_SET(route->flags, RTNH_F_ONLINK)))
463 return false;
464
465 return true;
466 }
467
468 int route_nexthops_to_string(const Route *route, char **ret) {
469 _cleanup_free_ char *buf = NULL;
470 int r;
471
472 assert(route);
473 assert(ret);
474
475 if (route->nexthop_id != 0) {
476 if (asprintf(&buf, "nexthop: %"PRIu32, route->nexthop_id) < 0)
477 return -ENOMEM;
478
479 *ret = TAKE_PTR(buf);
480 return 0;
481 }
482
483 if (route_is_reject(route)) {
484 buf = strdup("gw: n/a");
485 if (!buf)
486 return -ENOMEM;
487
488 *ret = TAKE_PTR(buf);
489 return 0;
490 }
491
492 if (ordered_set_isempty(route->nexthops)) {
493 if (in_addr_is_set(route->nexthop.family, &route->nexthop.gw)) {
494 if (asprintf(&buf, "gw: %s:%"PRIu32, IN_ADDR_TO_STRING(route->nexthop.family, &route->nexthop.gw), route->nexthop.weight + 1) < 0)
495 return -ENOMEM;
496 } else if (route->gateway_from_dhcp_or_ra) {
497 if (route->nexthop.family == AF_INET)
498 buf = strdup("gw: _dhcp4");
499 else if (route->nexthop.family == AF_INET6)
500 buf = strdup("gw: _ipv6ra");
501 else
502 buf = strdup("gw: _dhcp");
503 } else
504 buf = strdup("gw: n/a");
505 if (!buf)
506 return -ENOMEM;
507
508 *ret = TAKE_PTR(buf);
509 return 0;
510 }
511
512 RouteNextHop *nh;
513 ORDERED_SET_FOREACH(nh, route->nexthops) {
514 const char *s = in_addr_is_set(nh->family, &nh->gw) ? IN_ADDR_TO_STRING(nh->family, &nh->gw) : NULL;
515
516 if (nh->ifindex > 0)
517 r = strextendf_with_separator(&buf, ",", "%s@%i:%"PRIu32, strempty(s), nh->ifindex, nh->weight + 1);
518 else if (nh->ifname)
519 r = strextendf_with_separator(&buf, ",", "%s@%s:%"PRIu32, strempty(s), nh->ifname, nh->weight + 1);
520 else
521 r = strextendf_with_separator(&buf, ",", "%s:%"PRIu32, strempty(s), nh->weight + 1);
522 if (r < 0)
523 return r;
524 }
525
526 char *p = strjoin("gw: ", strna(buf));
527 if (!p)
528 return -ENOMEM;
529
530 *ret = p;
531 return 0;
532 }
533
534 static int append_nexthop_one(const Route *route, const RouteNextHop *nh, struct rtattr **rta, size_t offset) {
535 struct rtnexthop *rtnh;
536 struct rtattr *new_rta;
537 int r;
538
539 assert(route);
540 assert(IN_SET(route->family, AF_INET, AF_INET6));
541 assert(nh);
542 assert(rta);
543 assert(*rta);
544
545 new_rta = realloc(*rta, RTA_ALIGN((*rta)->rta_len) + RTA_SPACE(sizeof(struct rtnexthop)));
546 if (!new_rta)
547 return -ENOMEM;
548 *rta = new_rta;
549
550 rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset);
551 *rtnh = (struct rtnexthop) {
552 .rtnh_len = sizeof(*rtnh),
553 .rtnh_ifindex = nh->ifindex,
554 .rtnh_hops = nh->weight,
555 };
556
557 (*rta)->rta_len += sizeof(struct rtnexthop);
558
559 /* For device-only nexthops, skip RTA_GATEWAY entirely. The kernel will use the
560 * interface specified in rtnh_ifindex without requiring a gateway address. */
561 if (in_addr_is_set(nh->family, &nh->gw)) {
562 if (nh->family == route->family) {
563 r = rtattr_append_attribute(rta, RTA_GATEWAY, &nh->gw, FAMILY_ADDRESS_SIZE(nh->family));
564 if (r < 0)
565 goto clear;
566
567 rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset);
568 rtnh->rtnh_len += RTA_SPACE(FAMILY_ADDRESS_SIZE(nh->family));
569
570 } else if (nh->family == AF_INET6) {
571 assert(route->family == AF_INET);
572
573 r = rtattr_append_attribute(rta, RTA_VIA,
574 &(RouteVia) {
575 .family = nh->family,
576 .address = nh->gw,
577 }, sizeof(RouteVia));
578 if (r < 0)
579 goto clear;
580
581 rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset);
582 rtnh->rtnh_len += RTA_SPACE(sizeof(RouteVia));
583
584 } else if (nh->family == AF_INET)
585 assert_not_reached();
586 }
587
588 return 0;
589
590 clear:
591 (*rta)->rta_len -= sizeof(struct rtnexthop);
592 return r;
593 }
594
595 static int netlink_message_append_multipath_route(const Route *route, sd_netlink_message *message) {
596 _cleanup_free_ struct rtattr *rta = NULL;
597 size_t offset;
598 int r;
599
600 assert(route);
601 assert(message);
602
603 rta = new(struct rtattr, 1);
604 if (!rta)
605 return -ENOMEM;
606
607 *rta = (struct rtattr) {
608 .rta_type = RTA_MULTIPATH,
609 .rta_len = RTA_LENGTH(0),
610 };
611 offset = (uint8_t *) RTA_DATA(rta) - (uint8_t *) rta;
612
613 if (ordered_set_isempty(route->nexthops)) {
614 r = append_nexthop_one(route, &route->nexthop, &rta, offset);
615 if (r < 0)
616 return r;
617
618 } else {
619 RouteNextHop *nh;
620 ORDERED_SET_FOREACH(nh, route->nexthops) {
621 struct rtnexthop *rtnh;
622
623 r = append_nexthop_one(route, nh, &rta, offset);
624 if (r < 0)
625 return r;
626
627 rtnh = (struct rtnexthop *)((uint8_t *) rta + offset);
628 offset = (uint8_t *) RTNH_NEXT(rtnh) - (uint8_t *) rta;
629 }
630 }
631
632 return sd_netlink_message_append_data(message, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta));
633 }
634
635 int route_nexthops_set_netlink_message(const Route *route, sd_netlink_message *message) {
636 int r;
637
638 assert(route);
639 assert(IN_SET(route->family, AF_INET, AF_INET6));
640 assert(message);
641
642 if (route->nexthop_id != 0)
643 return sd_netlink_message_append_u32(message, RTA_NH_ID, route->nexthop_id);
644
645 if (route_is_reject(route))
646 return 0;
647
648 /* We request IPv6 multipath routes separately. Even though, if weight is non-zero, we need to use
649 * RTA_MULTIPATH, as we have no way to specify the weight of the nexthop. */
650 if (ordered_set_isempty(route->nexthops) && route->nexthop.weight == 0) {
651
652 if (in_addr_is_set(route->nexthop.family, &route->nexthop.gw)) {
653 if (route->nexthop.family == route->family)
654 r = netlink_message_append_in_addr_union(message, RTA_GATEWAY, route->nexthop.family, &route->nexthop.gw);
655 else {
656 assert(route->family == AF_INET);
657 r = sd_netlink_message_append_data(message, RTA_VIA,
658 &(const RouteVia) {
659 .family = route->nexthop.family,
660 .address = route->nexthop.gw,
661 }, sizeof(RouteVia));
662 }
663 if (r < 0)
664 return r;
665 }
666
667 assert(route->nexthop.ifindex > 0);
668 return sd_netlink_message_append_u32(message, RTA_OIF, route->nexthop.ifindex);
669 }
670
671 return netlink_message_append_multipath_route(route, message);
672 }
673
674 static int route_parse_nexthops(Route *route, const struct rtnexthop *rtnh, size_t size) {
675 _cleanup_ordered_set_free_ OrderedSet *nexthops = NULL;
676 int r;
677
678 assert(route);
679 assert(IN_SET(route->family, AF_INET, AF_INET6));
680 assert(rtnh);
681
682 if (size < sizeof(struct rtnexthop))
683 return -EBADMSG;
684
685 for (; size >= sizeof(struct rtnexthop); ) {
686 _cleanup_(route_nexthop_freep) RouteNextHop *nh = NULL;
687
688 if (NLMSG_ALIGN(rtnh->rtnh_len) > size)
689 return -EBADMSG;
690
691 if (rtnh->rtnh_len < sizeof(struct rtnexthop))
692 return -EBADMSG;
693
694 nh = new(RouteNextHop, 1);
695 if (!nh)
696 return -ENOMEM;
697
698 *nh = (RouteNextHop) {
699 .ifindex = rtnh->rtnh_ifindex,
700 .weight = rtnh->rtnh_hops,
701 };
702
703 if (rtnh->rtnh_len > sizeof(struct rtnexthop)) {
704 size_t len = rtnh->rtnh_len - sizeof(struct rtnexthop);
705 bool have_gw = false;
706
707 for (struct rtattr *attr = RTNH_DATA(rtnh); RTA_OK(attr, len); attr = RTA_NEXT(attr, len)) {
708
709 switch (attr->rta_type) {
710 case RTA_GATEWAY:
711 if (have_gw)
712 return -EBADMSG;
713
714 if (attr->rta_len != RTA_LENGTH(FAMILY_ADDRESS_SIZE(route->family)))
715 return -EBADMSG;
716
717 nh->family = route->family;
718 memcpy(&nh->gw, RTA_DATA(attr), FAMILY_ADDRESS_SIZE(nh->family));
719 have_gw = true;
720 break;
721
722 case RTA_VIA:
723 if (have_gw)
724 return -EBADMSG;
725
726 if (route->family != AF_INET)
727 return -EBADMSG;
728
729 if (attr->rta_len != RTA_LENGTH(sizeof(RouteVia)))
730 return -EBADMSG;
731
732 RouteVia *via = RTA_DATA(attr);
733 if (via->family != AF_INET6)
734 return -EBADMSG;
735
736 nh->family = via->family;
737 nh->gw = via->address;
738 have_gw = true;
739 break;
740 }
741 }
742 }
743
744 r = ordered_set_ensure_put(&nexthops, &route_nexthop_hash_ops, nh);
745 assert(r != 0);
746 if (r > 0)
747 TAKE_PTR(nh);
748 else if (r != -EEXIST)
749 return r;
750
751 size -= NLMSG_ALIGN(rtnh->rtnh_len);
752 rtnh = RTNH_NEXT(rtnh);
753 }
754
755 ordered_set_free(route->nexthops);
756 route->nexthops = TAKE_PTR(nexthops);
757 return 0;
758 }
759
760 int route_nexthops_read_netlink_message(Route *route, sd_netlink_message *message) {
761 int r;
762
763 assert(route);
764 assert(message);
765
766 r = sd_netlink_message_read_u32(message, RTA_NH_ID, &route->nexthop_id);
767 if (r < 0 && r != -ENODATA)
768 return log_warning_errno(r, "rtnl: received route message with invalid nexthop id, ignoring: %m");
769
770 if (route->nexthop_id != 0 || route_is_reject(route))
771 /* IPv6 routes with reject type are always assigned to the loopback interface. See kernel's
772 * fib6_nh_init() in net/ipv6/route.c. However, we'd like to make it consistent with IPv4
773 * routes. Hence, skip reading of RTA_OIF. */
774 return 0;
775
776 uint32_t ifindex;
777 r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex);
778 if (r >= 0)
779 route->nexthop.ifindex = (int) ifindex;
780 else if (r != -ENODATA)
781 return log_warning_errno(r, "rtnl: could not get ifindex from route message, ignoring: %m");
782
783 if (route->nexthop.ifindex > 0) {
784 r = netlink_message_read_in_addr_union(message, RTA_GATEWAY, route->family, &route->nexthop.gw);
785 if (r >= 0) {
786 route->nexthop.family = route->family;
787 return 0;
788 }
789 if (r != -ENODATA)
790 return log_warning_errno(r, "rtnl: received route message without valid gateway, ignoring: %m");
791
792 if (route->family != AF_INET)
793 return 0;
794
795 RouteVia via;
796 r = sd_netlink_message_read(message, RTA_VIA, sizeof(via), &via);
797 if (r >= 0) {
798 route->nexthop.family = via.family;
799 route->nexthop.gw = via.address;
800 return 0;
801 }
802 if (r != -ENODATA)
803 return log_warning_errno(r, "rtnl: received route message without valid gateway, ignoring: %m");
804
805 return 0;
806 }
807
808 size_t rta_len;
809 _cleanup_free_ void *rta = NULL;
810 r = sd_netlink_message_read_data(message, RTA_MULTIPATH, &rta_len, &rta);
811 if (r == -ENODATA)
812 return 0;
813 if (r < 0)
814 return log_warning_errno(r, "rtnl: failed to read RTA_MULTIPATH attribute, ignoring: %m");
815
816 r = route_parse_nexthops(route, rta, rta_len);
817 if (r < 0)
818 return log_warning_errno(r, "rtnl: failed to parse RTA_MULTIPATH attribute, ignoring: %m");
819
820 return 0;
821 }
822
823 #define log_route_section(route, fmt, ...) \
824 ({ \
825 const Route *_route = (route); \
826 log_section_warning_errno( \
827 _route ? _route->section : NULL, \
828 SYNTHETIC_ERRNO(EINVAL), \
829 fmt " Ignoring [Route] section.", \
830 ##__VA_ARGS__); \
831 })
832
833 int route_section_verify_nexthops(Route *route) {
834 assert(route);
835 assert(route->section);
836
837 if (route->gateway_from_dhcp_or_ra) {
838 assert(route->network);
839
840 if (route->nexthop.family == AF_UNSPEC)
841 /* When deprecated Gateway=_dhcp is set, then assume gateway family based on other settings. */
842 switch (route->family) {
843 case AF_UNSPEC:
844 log_section_warning(route->section,
845 "Deprecated value \"_dhcp\" is specified for Gateway=. "
846 "Please use \"_dhcp4\" or \"_ipv6ra\" instead. Assuming \"_dhcp4\".");
847
848 route->nexthop.family = route->family = AF_INET;
849 break;
850 case AF_INET:
851 case AF_INET6:
852 log_section_warning(route->section,
853 "Deprecated value \"_dhcp\" is specified for Gateway=. "
854 "Assuming \"%s\" based on Destination=, Source=, or PreferredSource= setting.",
855 route->family == AF_INET ? "_dhcp4" : "_ipv6ra");
856
857 route->nexthop.family = route->family;
858 break;
859 default:
860 return log_route_section(route, "Invalid route family.");
861 }
862
863 switch (route->nexthop.family) {
864 case AF_INET:
865 if (!FLAGS_SET(route->network->dhcp, ADDRESS_FAMILY_IPV4))
866 return log_route_section(route, "Gateway=\"_dhcp4\" is specified but DHCPv4 client is disabled.");
867
868 route->source = NETWORK_CONFIG_SOURCE_DHCP4;
869 break;
870
871 case AF_INET6:
872 if (route->network->ndisc == 0)
873 return log_route_section(route, "Gateway=\"_ipv6ra\" is specified but IPv6AcceptRA= is disabled.");
874
875 route->source = NETWORK_CONFIG_SOURCE_NDISC;
876 break;
877
878 default:
879 assert_not_reached();
880 }
881 }
882
883 /* When only Gateway= is specified, assume the route family based on the Gateway address. */
884 if (route->family == AF_UNSPEC)
885 route->family = route->nexthop.family;
886
887 if (route->family == AF_UNSPEC) {
888 assert(route->section);
889
890 return log_route_section(route, "Route section without Gateway=, Destination=, Source=, or PreferredSource= field configured.");
891 }
892
893 if (route->gateway_onlink < 0 && in_addr_is_set(route->nexthop.family, &route->nexthop.gw) &&
894 route->network && ordered_hashmap_isempty(route->network->addresses_by_section)) {
895 /* If no address is configured, in most cases the gateway cannot be reachable.
896 * TODO: we may need to improve the condition above. */
897 log_section_warning(route->section, "Gateway= without static address configured. Enabling GatewayOnLink= option.");
898 route->gateway_onlink = true;
899 }
900
901 if (route->gateway_onlink >= 0)
902 SET_FLAG(route->flags, RTNH_F_ONLINK, route->gateway_onlink);
903
904 if (route->family == AF_INET6) {
905 if (route->nexthop.family == AF_INET)
906 return log_route_section(route, "IPv4 gateway is configured for IPv6 route.");
907
908 RouteNextHop *nh;
909 ORDERED_SET_FOREACH(nh, route->nexthops)
910 if (nh->family == AF_INET)
911 return log_route_section(route, "IPv4 multipath route is specified for IPv6 route.");
912 }
913
914 if (route->nexthop_id != 0 &&
915 (route->gateway_from_dhcp_or_ra ||
916 in_addr_is_set(route->nexthop.family, &route->nexthop.gw) ||
917 !ordered_set_isempty(route->nexthops)))
918 return log_route_section(route, "NextHopId= cannot be specified with Gateway= or MultiPathRoute=.");
919
920 if (route_is_reject(route) &&
921 (route->gateway_from_dhcp_or_ra ||
922 in_addr_is_set(route->nexthop.family, &route->nexthop.gw) ||
923 !ordered_set_isempty(route->nexthops)))
924 return log_route_section(route, "Reject type route cannot be specified with Gateway= or MultiPathRoute=.");
925
926 if ((route->gateway_from_dhcp_or_ra ||
927 in_addr_is_set(route->nexthop.family, &route->nexthop.gw)) &&
928 !ordered_set_isempty(route->nexthops))
929 return log_route_section(route, "Gateway= cannot be specified with MultiPathRoute=.");
930
931 if (ordered_set_size(route->nexthops) == 1) {
932 _cleanup_(route_nexthop_freep) RouteNextHop *nh = ordered_set_steal_first(route->nexthops);
933
934 route_nexthop_done(&route->nexthop);
935 route->nexthop = TAKE_STRUCT(*nh);
936
937 assert(ordered_set_isempty(route->nexthops));
938 route->nexthops = ordered_set_free(route->nexthops);
939 }
940
941 return 0;
942 }
943
944 int config_parse_gateway(
945 const char *unit,
946 const char *filename,
947 unsigned line,
948 const char *section,
949 unsigned section_line,
950 const char *lvalue,
951 int ltype, /* 0 : only address is accepted, 1 : also supports an empty string, _dhcp, and friends. */
952 const char *rvalue,
953 void *data,
954 void *userdata) {
955
956 Route *route = ASSERT_PTR(userdata);
957 int r;
958
959 if (ltype) {
960 if (isempty(rvalue)) {
961 route->gateway_from_dhcp_or_ra = false;
962 route->nexthop.family = AF_UNSPEC;
963 route->nexthop.gw = IN_ADDR_NULL;
964 return 1;
965 }
966
967 if (streq(rvalue, "_dhcp")) {
968 route->gateway_from_dhcp_or_ra = true;
969 route->nexthop.family = AF_UNSPEC;
970 route->nexthop.gw = IN_ADDR_NULL;
971 return 1;
972 }
973
974 if (streq(rvalue, "_dhcp4")) {
975 route->gateway_from_dhcp_or_ra = true;
976 route->nexthop.family = AF_INET;
977 route->nexthop.gw = IN_ADDR_NULL;
978 return 1;
979 }
980
981 if (streq(rvalue, "_ipv6ra")) {
982 route->gateway_from_dhcp_or_ra = true;
983 route->nexthop.family = AF_INET6;
984 route->nexthop.gw = IN_ADDR_NULL;
985 return 1;
986 }
987 }
988
989 assert(rvalue);
990
991 r = in_addr_from_string_auto(rvalue, &route->nexthop.family, &route->nexthop.gw);
992 if (r < 0)
993 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
994
995 route->gateway_from_dhcp_or_ra = false;
996 return 1;
997 }
998
999 int config_parse_route_nexthop(
1000 const char *unit,
1001 const char *filename,
1002 unsigned line,
1003 const char *section,
1004 unsigned section_line,
1005 const char *lvalue,
1006 int ltype,
1007 const char *rvalue,
1008 void *data,
1009 void *userdata) {
1010
1011 uint32_t id, *p = ASSERT_PTR(data);
1012 int r;
1013
1014 if (isempty(rvalue)) {
1015 *p = 0;
1016 return 1;
1017 }
1018
1019 r = safe_atou32(rvalue, &id);
1020 if (r < 0)
1021 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1022 if (id == 0) {
1023 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid nexthop ID, ignoring assignment: %s", rvalue);
1024 return 0;
1025 }
1026
1027 *p = id;
1028 return 1;
1029 }
1030
1031 int config_parse_multipath_route(
1032 const char *unit,
1033 const char *filename,
1034 unsigned line,
1035 const char *section,
1036 unsigned section_line,
1037 const char *lvalue,
1038 int ltype,
1039 const char *rvalue,
1040 void *data,
1041 void *userdata) {
1042
1043 _cleanup_(route_nexthop_freep) RouteNextHop *nh = NULL;
1044 _cleanup_free_ char *word = NULL;
1045 OrderedSet **nexthops = ASSERT_PTR(data);
1046 const char *p;
1047 char *dev;
1048 int r;
1049
1050 assert(filename);
1051 assert(section);
1052 assert(lvalue);
1053 assert(rvalue);
1054 assert(data);
1055
1056 if (isempty(rvalue)) {
1057 *nexthops = ordered_set_free(*nexthops);
1058 return 1;
1059 }
1060
1061 nh = new0(RouteNextHop, 1);
1062 if (!nh)
1063 return log_oom();
1064
1065 p = rvalue;
1066 r = extract_first_word(&p, &word, NULL, 0);
1067 if (r <= 0)
1068 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1069
1070 dev = strchr(word, '@');
1071 if (dev) {
1072 *dev++ = '\0';
1073
1074 r = parse_ifindex(dev);
1075 if (r > 0)
1076 nh->ifindex = r;
1077 else {
1078 if (!ifname_valid_full(dev, IFNAME_VALID_ALTERNATIVE)) {
1079 log_syntax(unit, LOG_WARNING, filename, line, 0,
1080 "Invalid interface name '%s' in %s=, ignoring: %s", dev, lvalue, rvalue);
1081 return 0;
1082 }
1083
1084 nh->ifname = strdup(dev);
1085 if (!nh->ifname)
1086 return log_oom();
1087 }
1088 }
1089
1090 if (isempty(word)) {
1091 if (!dev)
1092 return log_syntax_parse_error(unit, filename, line, SYNTHETIC_ERRNO(EINVAL), lvalue, rvalue);
1093 } else {
1094 r = in_addr_from_string_auto(word, &nh->family, &nh->gw);
1095 if (r < 0) {
1096 log_syntax(unit, LOG_WARNING, filename, line, r,
1097 "Invalid multipath route gateway '%s', ignoring assignment: %m", rvalue);
1098 return 0;
1099 }
1100 }
1101
1102 if (!isempty(p)) {
1103 r = safe_atou32(p, &nh->weight);
1104 if (r < 0) {
1105 log_syntax(unit, LOG_WARNING, filename, line, r,
1106 "Invalid multipath route weight, ignoring assignment: %s", p);
1107 return 0;
1108 }
1109 /* ip command takes weight in the range 1…255, while kernel takes the value in the
1110 * range 0…254. MultiPathRoute= setting also takes weight in the same range which ip
1111 * command uses, then networkd decreases by one and stores it to match the range which
1112 * kernel uses. */
1113 if (nh->weight == 0 || nh->weight > 256) {
1114 log_syntax(unit, LOG_WARNING, filename, line, 0,
1115 "Invalid multipath route weight, ignoring assignment: %s", p);
1116 return 0;
1117 }
1118 nh->weight--;
1119 }
1120
1121 r = ordered_set_ensure_put(nexthops, &route_nexthop_hash_ops, nh);
1122 if (r == -ENOMEM)
1123 return log_oom();
1124 if (r < 0) {
1125 log_syntax(unit, LOG_WARNING, filename, line, r,
1126 "Failed to store multipath route, ignoring assignment: %m");
1127 return 0;
1128 }
1129
1130 TAKE_PTR(nh);
1131 return 1;
1132 }