Debian Codegraph @

src/network/networkd-route-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/rtnetlink.h>
4 #include <threads.h>
5
6 #include "alloc-util.h"
7 #include "bitfield.h"
8 #include "extract-word.h"
9 #include "logarithm.h"
10 #include "networkd-address.h"
11 #include "networkd-link.h"
12 #include "networkd-manager.h"
13 #include "networkd-route.h"
14 #include "networkd-route-util.h"
15 #include "parse-util.h"
16 #include "set.h"
17 #include "string-table.h"
18 #include "string-util.h"
19 #include "sysctl-util.h"
20
21 #define ROUTES_DEFAULT_MAX_PER_FAMILY 4096
22
23 unsigned routes_max(void) {
24 static thread_local unsigned cached = 0;
25 int val4 = ROUTES_DEFAULT_MAX_PER_FAMILY, val6 = ROUTES_DEFAULT_MAX_PER_FAMILY;
26
27 if (cached > 0)
28 return cached;
29
30 /* The kernel internally stores these maximum size in int. */
31
32 if (sysctl_read_ip_property_int(AF_INET, /* ifname= */ NULL, "route/max_size", &val4) >= 0)
33 if (val4 == INT_MAX)
34 /* This is the default "no limit" value in the kernel */
35 val4 = ROUTES_DEFAULT_MAX_PER_FAMILY;
36
37 if (sysctl_read_ip_property_int(AF_INET6, /* ifname= */ NULL, "route/max_size", &val6) >= 0)
38 if (val6 == INT_MAX)
39 /* This is the default "no limit" value in the kernel */
40 val6 = ROUTES_DEFAULT_MAX_PER_FAMILY;
41
42 cached = MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val4) +
43 MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val6);
44 return cached;
45 }
46
47 bool route_type_is_reject(uint8_t type) {
48 return IN_SET(type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW);
49 }
50
51 bool route_is_reject(const Route *route) {
52 return route_type_is_reject(ASSERT_PTR(route)->type);
53 }
54
55 static bool route_lifetime_is_valid(const Route *route) {
56 assert(route);
57
58 return
59 route->lifetime_usec == USEC_INFINITY ||
60 route->lifetime_usec > now(CLOCK_BOOTTIME);
61 }
62
63 bool link_find_default_gateway(Link *link, int family, Route **gw) {
64 bool found = false;
65 Route *route;
66
67 assert(link);
68 assert(link->manager);
69
70 SET_FOREACH(route, link->manager->routes) {
71 if (route->nexthop.ifindex != link->ifindex)
72 continue;
73 if (!route_exists(route))
74 continue;
75 if (family != AF_UNSPEC && route->family != family)
76 continue;
77 if (route->dst_prefixlen != 0)
78 continue;
79 if (route->src_prefixlen != 0)
80 continue;
81 if (route->table != RT_TABLE_MAIN)
82 continue;
83 if (route->type != RTN_UNICAST)
84 continue;
85 if (route->scope != RT_SCOPE_UNIVERSE)
86 continue;
87 if (!in_addr_is_set(route->nexthop.family, &route->nexthop.gw))
88 continue;
89
90 /* Found a default gateway. */
91 if (!gw)
92 return true;
93
94 /* If we have already found another gw, then let's compare their weight and priority. */
95 if (*gw) {
96 if (route->nexthop.weight > (*gw)->nexthop.weight)
97 continue;
98 if (route->priority >= (*gw)->priority)
99 continue;
100 }
101
102 *gw = route;
103 found = true;
104 }
105
106 return found;
107 }
108
109 int manager_find_uplink(Manager *m, int family, Link *exclude, Link **ret) {
110 Route *gw = NULL;
111 Link *link;
112
113 assert(m);
114 assert(IN_SET(family, AF_UNSPEC, AF_INET, AF_INET6));
115
116 /* Looks for a suitable "uplink", via black magic: an interface that is up and where the
117 * default route with the highest priority points to. */
118
119 HASHMAP_FOREACH(link, m->links_by_index) {
120 if (link == exclude)
121 continue;
122
123 if (link->state != LINK_STATE_CONFIGURED)
124 continue;
125
126 link_find_default_gateway(link, family, &gw);
127 }
128
129 if (!gw)
130 return -ENOENT;
131
132 return link_get_by_index(m, gw->nexthop.ifindex, ret);
133 }
134
135 bool gateway_is_ready(Link *link, bool onlink, int family, const union in_addr_union *gw) {
136 Route *route;
137 Address *a;
138
139 assert(link);
140 assert(link->manager);
141
142 if (link->set_flags_messages > 0)
143 return false;
144
145 if (!link_is_up(link))
146 return false;
147
148 if (onlink)
149 return true;
150
151 if (!gw || !in_addr_is_set(family, gw))
152 return true;
153
154 if (family == AF_INET6 && in6_addr_is_link_local(&gw->in6))
155 return true;
156
157 SET_FOREACH(route, link->manager->routes) {
158 if (route->nexthop.ifindex != link->ifindex)
159 continue;
160 if (!route_exists(route))
161 continue;
162 if (!route_lifetime_is_valid(route))
163 continue;
164 if (route->family != family)
165 continue;
166 if (!in_addr_is_set(route->family, &route->dst) && route->dst_prefixlen == 0)
167 continue;
168 if (in_addr_prefix_covers(family, &route->dst, route->dst_prefixlen, gw) > 0)
169 return true;
170 }
171
172 if (link->manager->manage_foreign_routes)
173 return false;
174
175 /* If we do not manage foreign routes, then there may exist a prefix route we do not know,
176 * which was created on configuring an address. Hence, also check the addresses. */
177 SET_FOREACH(a, link->addresses) {
178 if (!address_is_ready(a))
179 continue;
180 if (a->family != family)
181 continue;
182 if (FLAGS_SET(a->flags, IFA_F_NOPREFIXROUTE))
183 continue;
184 if (in_addr_prefix_covers(a->family,
185 in_addr_is_set(a->family, &a->in_addr_peer) ? &a->in_addr_peer : &a->in_addr,
186 a->prefixlen, gw) > 0)
187 return true;
188 }
189
190 return false;
191 }
192
193 static int link_address_is_reachable_internal(
194 Link *link,
195 int family,
196 const union in_addr_union *address,
197 const union in_addr_union *prefsrc, /* optional */
198 Route **ret) {
199
200 Route *route, *found = NULL;
201
202 assert(link);
203 assert(link->manager);
204 assert(IN_SET(family, AF_INET, AF_INET6));
205 assert(address);
206
207 SET_FOREACH(route, link->manager->routes) {
208 if (route->nexthop.ifindex != link->ifindex)
209 continue;
210
211 if (!route_exists(route))
212 continue;
213
214 if (!route_lifetime_is_valid(route))
215 continue;
216
217 if (route->type != RTN_UNICAST)
218 continue;
219
220 if (route->family != family)
221 continue;
222
223 if (in_addr_prefix_covers(family, &route->dst, route->dst_prefixlen, address) <= 0)
224 continue;
225
226 if (prefsrc &&
227 in_addr_is_set(family, prefsrc) &&
228 in_addr_is_set(family, &route->prefsrc) &&
229 !in_addr_equal(family, prefsrc, &route->prefsrc))
230 continue;
231
232 if (found && found->priority <= route->priority)
233 continue;
234
235 found = route;
236 }
237
238 if (!found)
239 return -ENOENT;
240
241 if (ret)
242 *ret = found;
243
244 return 0;
245 }
246
247 int link_address_is_reachable(
248 Link *link,
249 int family,
250 const union in_addr_union *address,
251 const union in_addr_union *prefsrc, /* optional */
252 Address **ret) {
253
254 Route *route;
255 Address *a;
256 int r;
257
258 assert(link);
259 assert(IN_SET(family, AF_INET, AF_INET6));
260 assert(address);
261
262 /* This checks if the address is reachable, and optionally return the Address object of the
263 * preferred source to access the address. */
264
265 r = link_address_is_reachable_internal(link, family, address, prefsrc, &route);
266 if (r < 0)
267 return r;
268
269 if (!in_addr_is_set(route->family, &route->prefsrc)) {
270 if (ret)
271 *ret = NULL;
272 return 0;
273 }
274
275 r = link_get_address(link, route->family, &route->prefsrc, &a);
276 if (r < 0)
277 return r;
278
279 if (!address_is_ready(a))
280 return -EBUSY;
281
282 if (ret)
283 *ret = a;
284
285 return 0;
286 }
287
288 int manager_address_is_reachable(
289 Manager *manager,
290 int family,
291 const union in_addr_union *address,
292 const union in_addr_union *prefsrc, /* optional */
293 Address **ret) {
294
295 Route *route, *found = NULL;
296 Address *a;
297 Link *link;
298 int r;
299
300 assert(manager);
301
302 HASHMAP_FOREACH(link, manager->links_by_index) {
303 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
304 continue;
305
306 if (link_address_is_reachable_internal(link, family, address, prefsrc, &route) < 0)
307 continue;
308
309 if (found && found->priority <= route->priority)
310 continue;
311
312 found = route;
313 }
314
315 if (!found)
316 return -ENOENT;
317
318 if (!in_addr_is_set(found->family, &found->prefsrc)) {
319 if (ret)
320 *ret = NULL;
321 return 0;
322 }
323
324 r = link_get_by_index(manager, found->nexthop.ifindex, &link);
325 if (r < 0)
326 return r;
327
328 r = link_get_address(link, found->family, &found->prefsrc, &a);
329 if (r < 0)
330 return r;
331
332 if (!address_is_ready(a))
333 return -EBUSY;
334
335 if (ret)
336 *ret = a;
337
338 return 0;
339 }
340
341 static const char * const route_type_table[__RTN_MAX] = {
342 [RTN_UNICAST] = "unicast",
343 [RTN_LOCAL] = "local",
344 [RTN_BROADCAST] = "broadcast",
345 [RTN_ANYCAST] = "anycast",
346 [RTN_MULTICAST] = "multicast",
347 [RTN_BLACKHOLE] = "blackhole",
348 [RTN_UNREACHABLE] = "unreachable",
349 [RTN_PROHIBIT] = "prohibit",
350 [RTN_THROW] = "throw",
351 [RTN_NAT] = "nat",
352 [RTN_XRESOLVE] = "xresolve",
353 };
354
355 assert_cc(__RTN_MAX <= UCHAR_MAX);
356 DEFINE_STRING_TABLE_LOOKUP(route_type, int);
357
358 static const char * const route_scope_table[] = {
359 [RT_SCOPE_UNIVERSE] = "global",
360 [RT_SCOPE_SITE] = "site",
361 [RT_SCOPE_LINK] = "link",
362 [RT_SCOPE_HOST] = "host",
363 [RT_SCOPE_NOWHERE] = "nowhere",
364 };
365
366 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(route_scope, int, UINT8_MAX);
367
368 static const char * const route_protocol_table[] = {
369 [RTPROT_KERNEL] = "kernel",
370 [RTPROT_BOOT] = "boot",
371 [RTPROT_STATIC] = "static",
372 };
373
374 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(route_protocol, int, UINT8_MAX);
375
376 static const char * const route_protocol_full_table[] = {
377 [RTPROT_REDIRECT] = "redirect",
378 [RTPROT_KERNEL] = "kernel",
379 [RTPROT_BOOT] = "boot",
380 [RTPROT_STATIC] = "static",
381 [RTPROT_GATED] = "gated",
382 [RTPROT_RA] = "ra",
383 [RTPROT_MRT] = "mrt",
384 [RTPROT_ZEBRA] = "zebra",
385 [RTPROT_BIRD] = "bird",
386 [RTPROT_DNROUTED] = "dnrouted",
387 [RTPROT_XORP] = "xorp",
388 [RTPROT_NTK] = "ntk",
389 [RTPROT_DHCP] = "dhcp",
390 [RTPROT_MROUTED] = "mrouted",
391 [RTPROT_BABEL] = "babel",
392 [RTPROT_BGP] = "bgp",
393 [RTPROT_ISIS] = "isis",
394 [RTPROT_OSPF] = "ospf",
395 [RTPROT_RIP] = "rip",
396 [RTPROT_EIGRP] = "eigrp",
397 };
398
399 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(route_protocol_full, int, UINT8_MAX);
400
401 int route_flags_to_string_alloc(uint32_t flags, char **ret) {
402 _cleanup_free_ char *str = NULL;
403 static const char* map[] = {
404 [LOG2U(RTNH_F_DEAD)] = "dead", /* Nexthop is dead (used by multipath) */
405 [LOG2U(RTNH_F_PERVASIVE)] = "pervasive", /* Do recursive gateway lookup */
406 [LOG2U(RTNH_F_ONLINK)] = "onlink" , /* Gateway is forced on link */
407 [LOG2U(RTNH_F_OFFLOAD)] = "offload", /* Nexthop is offloaded */
408 [LOG2U(RTNH_F_LINKDOWN)] = "linkdown", /* carrier-down on nexthop */
409 [LOG2U(RTNH_F_UNRESOLVED)] = "unresolved", /* The entry is unresolved (ipmr) */
410 [LOG2U(RTNH_F_TRAP)] = "trap", /* Nexthop is trapping packets */
411 };
412
413 assert(ret);
414
415 for (size_t i = 0; i < ELEMENTSOF(map); i++)
416 if (BIT_SET(flags, i) && map[i])
417 if (!strextend_with_separator(&str, ",", map[i]))
418 return -ENOMEM;
419
420 *ret = TAKE_PTR(str);
421 return 0;
422 }
423
424 static const char * const route_table_table[] = {
425 [RT_TABLE_DEFAULT] = "default",
426 [RT_TABLE_MAIN] = "main",
427 [RT_TABLE_LOCAL] = "local",
428 };
429
430 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table, int);
431
432 int manager_get_route_table_from_string(const Manager *m, const char *s, uint32_t *ret) {
433 uint32_t t;
434 int r;
435
436 assert(m);
437 assert(s);
438 assert(ret);
439
440 r = route_table_from_string(s);
441 if (r >= 0) {
442 *ret = (uint32_t) r;
443 return 0;
444 }
445
446 t = PTR_TO_UINT32(hashmap_get(m->route_table_numbers_by_name, s));
447 if (t != 0) {
448 *ret = t;
449 return 0;
450 }
451
452 r = safe_atou32(s, &t);
453 if (r < 0)
454 return r;
455
456 if (t == 0)
457 return -ERANGE;
458
459 *ret = t;
460 return 0;
461 }
462
463 int manager_get_route_table_to_string(const Manager *m, uint32_t table, bool append_num, char **ret) {
464 _cleanup_free_ char *str = NULL;
465 const char *s;
466
467 assert(m);
468 assert(ret);
469
470 /* Unlike manager_get_route_table_from_string(), this accepts 0, as the kernel may create routes with
471 * table 0. See issue #25089. */
472
473 s = route_table_to_string(table);
474 if (!s)
475 s = hashmap_get(m->route_table_names_by_number, UINT32_TO_PTR(table));
476
477 if (s && !append_num) {
478 str = strdup(s);
479 if (!str)
480 return -ENOMEM;
481
482 } else if (asprintf(&str, "%s%s%" PRIu32 "%s",
483 strempty(s),
484 s ? "(" : "",
485 table,
486 s ? ")" : "") < 0)
487 return -ENOMEM;
488
489 *ret = TAKE_PTR(str);
490 return 0;
491 }
492
493 int config_parse_route_table_names(
494 const char *unit,
495 const char *filename,
496 unsigned line,
497 const char *section,
498 unsigned section_line,
499 const char *lvalue,
500 int ltype,
501 const char *rvalue,
502 void *data,
503 void *userdata) {
504
505 Manager *m = ASSERT_PTR(userdata);
506 int r;
507
508 assert(filename);
509 assert(lvalue);
510 assert(rvalue);
511
512 if (isempty(rvalue)) {
513 m->route_table_names_by_number = hashmap_free(m->route_table_names_by_number);
514 m->route_table_numbers_by_name = hashmap_free(m->route_table_numbers_by_name);
515 return 0;
516 }
517
518 for (const char *p = rvalue;;) {
519 _cleanup_free_ char *name = NULL;
520 uint32_t table;
521 char *num;
522
523 r = extract_first_word(&p, &name, NULL, 0);
524 if (r == -ENOMEM)
525 return log_oom();
526 if (r < 0) {
527 log_syntax(unit, LOG_WARNING, filename, line, r,
528 "Invalid RouteTable=, ignoring assignment: %s", rvalue);
529 return 0;
530 }
531 if (r == 0)
532 return 0;
533
534 num = strchr(name, ':');
535 if (!num) {
536 log_syntax(unit, LOG_WARNING, filename, line, 0,
537 "Invalid route table name and number pair, ignoring assignment: %s", name);
538 continue;
539 }
540
541 *num++ = '\0';
542
543 if (isempty(name)) {
544 log_syntax(unit, LOG_WARNING, filename, line, 0,
545 "Route table name cannot be empty. Ignoring assignment: %s:%s", name, num);
546 continue;
547 }
548 if (in_charset(name, DIGITS)) {
549 log_syntax(unit, LOG_WARNING, filename, line, 0,
550 "Route table name cannot be numeric. Ignoring assignment: %s:%s", name, num);
551 continue;
552 }
553 if (route_table_from_string(name) >= 0) {
554 log_syntax(unit, LOG_WARNING, filename, line, 0,
555 "Route table name %s is predefined for %i. Ignoring assignment: %s:%s",
556 name, route_table_from_string(name), name, num);
557 continue;
558 }
559
560 r = safe_atou32(num, &table);
561 if (r < 0) {
562 log_syntax(unit, LOG_WARNING, filename, line, r,
563 "Failed to parse route table number '%s', ignoring assignment: %s:%s", num, name, num);
564 continue;
565 }
566 if (table == 0) {
567 log_syntax(unit, LOG_WARNING, filename, line, 0,
568 "Invalid route table number, ignoring assignment: %s:%s", name, num);
569 continue;
570 }
571 if (route_table_to_string(table)) {
572 log_syntax(unit, LOG_WARNING, filename, line, 0,
573 "Route table name for %s is predefined (%s). Ignoring assignment: %s:%s",
574 num, route_table_to_string(table), name, num);
575 continue;
576 }
577
578 r = hashmap_ensure_put(&m->route_table_numbers_by_name, &string_hash_ops_free, name, UINT32_TO_PTR(table));
579 if (r == -ENOMEM)
580 return log_oom();
581 if (r == -EEXIST) {
582 log_syntax(unit, LOG_WARNING, filename, line, r,
583 "Specified route table name and number pair conflicts with others, ignoring assignment: %s:%s", name, num);
584 continue;
585 }
586 if (r < 0) {
587 log_syntax(unit, LOG_WARNING, filename, line, r,
588 "Failed to store route table name and number pair, ignoring assignment: %s:%s", name, num);
589 continue;
590 }
591 if (r == 0)
592 /* The entry is duplicated. It should not be added to route_table_names_by_number hashmap. */
593 continue;
594
595 r = hashmap_ensure_put(&m->route_table_names_by_number, NULL, UINT32_TO_PTR(table), name);
596 if (r < 0) {
597 hashmap_remove(m->route_table_numbers_by_name, name);
598
599 if (r == -ENOMEM)
600 return log_oom();
601 if (r == -EEXIST)
602 log_syntax(unit, LOG_WARNING, filename, line, r,
603 "Specified route table name and number pair conflicts with others, ignoring assignment: %s:%s", name, num);
604 else
605 log_syntax(unit, LOG_WARNING, filename, line, r,
606 "Failed to store route table name and number pair, ignoring assignment: %s:%s", name, num);
607 continue;
608 }
609 assert(r > 0);
610
611 TAKE_PTR(name);
612 }
613 }