]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-route.c
network: use netlink_message_read_in_addr_union() where applicable
[thirdparty/systemd.git] / src / network / networkd-route.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
f579559b 2
b5bf6f64 3#include <linux/icmpv6.h>
8973df5c 4#include <linux/ipv6_route.h>
b5bf6f64 5
b5efdb8a 6#include "alloc-util.h"
fc2f9534 7#include "netlink-util.h"
ca5ad760 8#include "networkd-ipv4ll.h"
23f53b99 9#include "networkd-manager.h"
e2263711 10#include "networkd-network.h"
141318f7 11#include "networkd-nexthop.h"
6bedfcbb 12#include "networkd-route.h"
141318f7 13#include "networkd-routing-policy-rule.h"
6bedfcbb 14#include "parse-util.h"
d308bb99 15#include "socket-netlink.h"
7a22312d 16#include "string-table.h"
07630cea 17#include "string-util.h"
d96edb2c 18#include "strv.h"
b297e0a7 19#include "strxcpyx.h"
47d2d30d 20#include "sysctl-util.h"
c0d48bc5 21#include "vrf.h"
f579559b 22
47d2d30d
ZJS
23#define ROUTES_DEFAULT_MAX_PER_FAMILY 4096U
24
423c249c 25static uint32_t link_get_vrf_table(const Link *link) {
ac49887e
YW
26 return link->network->vrf ? VRF(link->network->vrf)->table : RT_TABLE_MAIN;
27}
28
423c249c 29uint32_t link_get_dhcp_route_table(const Link *link) {
ac49887e
YW
30 /* When the interface is part of an VRF use the VRFs routing table, unless
31 * another table is explicitly specified. */
32 if (link->network->dhcp_route_table_set)
33 return link->network->dhcp_route_table;
34 return link_get_vrf_table(link);
35}
36
423c249c 37uint32_t link_get_ipv6_accept_ra_route_table(const Link *link) {
ac49887e
YW
38 if (link->network->ipv6_accept_ra_route_table_set)
39 return link->network->ipv6_accept_ra_route_table;
40 return link_get_vrf_table(link);
41}
42
74154c2e
YW
43static const char * const route_type_table[__RTN_MAX] = {
44 [RTN_UNICAST] = "unicast",
45 [RTN_LOCAL] = "local",
46 [RTN_BROADCAST] = "broadcast",
47 [RTN_ANYCAST] = "anycast",
48 [RTN_MULTICAST] = "multicast",
49 [RTN_BLACKHOLE] = "blackhole",
50 [RTN_UNREACHABLE] = "unreachable",
51 [RTN_PROHIBIT] = "prohibit",
52 [RTN_THROW] = "throw",
53 [RTN_NAT] = "nat",
54 [RTN_XRESOLVE] = "xresolve",
55};
56
57assert_cc(__RTN_MAX <= UCHAR_MAX);
58DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_type, int);
59
60static const char * const route_scope_table[] = {
61 [RT_SCOPE_UNIVERSE] = "global",
62 [RT_SCOPE_SITE] = "site",
63 [RT_SCOPE_LINK] = "link",
64 [RT_SCOPE_HOST] = "host",
65 [RT_SCOPE_NOWHERE] = "nowhere",
66};
67
68DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope, int);
69
70#define ROUTE_SCOPE_STR_MAX CONST_MAX(DECIMAL_STR_MAX(int), STRLEN("nowhere") + 1)
71static const char *format_route_scope(int scope, char *buf, size_t size) {
72 const char *s;
73 char *p = buf;
74
75 s = route_scope_to_string(scope);
76 if (s)
77 strpcpy(&p, size, s);
78 else
79 strpcpyf(&p, size, "%d", scope);
80
81 return buf;
82}
83
84static const char * const route_table_table[] = {
85 [RT_TABLE_DEFAULT] = "default",
86 [RT_TABLE_MAIN] = "main",
87 [RT_TABLE_LOCAL] = "local",
88};
89
90DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table, int);
91
92#define ROUTE_TABLE_STR_MAX CONST_MAX(DECIMAL_STR_MAX(int), STRLEN("default") + 1)
93static const char *format_route_table(int table, char *buf, size_t size) {
94 const char *s;
95 char *p = buf;
96
97 s = route_table_to_string(table);
98 if (s)
99 strpcpy(&p, size, s);
100 else
101 strpcpyf(&p, size, "%d", table);
102
103 return buf;
104}
105
106static const char * const route_protocol_table[] = {
107 [RTPROT_KERNEL] = "kernel",
108 [RTPROT_BOOT] = "boot",
109 [RTPROT_STATIC] = "static",
110};
111
112DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol, int);
113
114static const char * const route_protocol_full_table[] = {
115 [RTPROT_REDIRECT] = "redirect",
116 [RTPROT_KERNEL] = "kernel",
117 [RTPROT_BOOT] = "boot",
118 [RTPROT_STATIC] = "static",
119 [RTPROT_GATED] = "gated",
120 [RTPROT_RA] = "ra",
121 [RTPROT_MRT] = "mrt",
122 [RTPROT_ZEBRA] = "zebra",
123 [RTPROT_BIRD] = "bird",
124 [RTPROT_DNROUTED] = "dnrouted",
125 [RTPROT_XORP] = "xorp",
126 [RTPROT_NTK] = "ntk",
127 [RTPROT_DHCP] = "dhcp",
128 [RTPROT_MROUTED] = "mrouted",
129 [RTPROT_BABEL] = "babel",
130 [RTPROT_BGP] = "bgp",
131 [RTPROT_ISIS] = "isis",
132 [RTPROT_OSPF] = "ospf",
133 [RTPROT_RIP] = "rip",
134 [RTPROT_EIGRP] = "eigrp",
135};
136
137DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full, int);
138
139#define ROUTE_PROTOCOL_STR_MAX CONST_MAX(DECIMAL_STR_MAX(int), STRLEN("redirect") + 1)
140static const char *format_route_protocol(int protocol, char *buf, size_t size) {
141 const char *s;
142 char *p = buf;
143
144 s = route_protocol_full_to_string(protocol);
145 if (s)
146 strpcpy(&p, size, s);
147 else
148 strpcpyf(&p, size, "%d", protocol);
149
150 return buf;
151}
152
47d2d30d
ZJS
153static unsigned routes_max(void) {
154 static thread_local unsigned cached = 0;
47d2d30d
ZJS
155 _cleanup_free_ char *s4 = NULL, *s6 = NULL;
156 unsigned val4 = ROUTES_DEFAULT_MAX_PER_FAMILY, val6 = ROUTES_DEFAULT_MAX_PER_FAMILY;
157
158 if (cached > 0)
159 return cached;
160
778c8795
YW
161 if (sysctl_read_ip_property(AF_INET, NULL, "route/max_size", &s4) >= 0)
162 if (safe_atou(s4, &val4) >= 0 && val4 == 2147483647U)
47d2d30d
ZJS
163 /* This is the default "no limit" value in the kernel */
164 val4 = ROUTES_DEFAULT_MAX_PER_FAMILY;
47d2d30d 165
778c8795 166 if (sysctl_read_ip_property(AF_INET6, NULL, "route/max_size", &s6) >= 0)
47d2d30d 167 (void) safe_atou(s6, &val6);
47d2d30d
ZJS
168
169 cached = MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val4) +
170 MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val6);
171 return cached;
172}
8c34b963 173
ed9e361a 174int route_new(Route **ret) {
8e766630 175 _cleanup_(route_freep) Route *route = NULL;
f0213e37 176
17f9c355 177 route = new(Route, 1);
f0213e37
TG
178 if (!route)
179 return -ENOMEM;
180
17f9c355
YW
181 *route = (Route) {
182 .family = AF_UNSPEC,
183 .scope = RT_SCOPE_UNIVERSE,
184 .protocol = RTPROT_UNSPEC,
185 .type = RTN_UNICAST,
186 .table = RT_TABLE_MAIN,
187 .lifetime = USEC_INFINITY,
188 .quickack = -1,
633c7258 189 .fast_open_no_cookie = -1,
54901fd2 190 .gateway_onlink = -1,
9b88f20a 191 .ttl_propagate = -1,
17f9c355 192 };
f0213e37 193
1cc6c93a 194 *ret = TAKE_PTR(route);
f0213e37
TG
195
196 return 0;
197}
198
9560e5b3 199static int route_new_static(Network *network, const char *filename, unsigned section_line, Route **ret) {
8e766630
LP
200 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
201 _cleanup_(route_freep) Route *route = NULL;
f0213e37 202 int r;
f579559b 203
8c34b963
LP
204 assert(network);
205 assert(ret);
2a54a044
YW
206 assert(filename);
207 assert(section_line > 0);
f4859fc7 208
2a54a044
YW
209 r = network_config_section_new(filename, section_line, &n);
210 if (r < 0)
211 return r;
6ae115c1 212
2a54a044
YW
213 route = hashmap_get(network->routes_by_section, n);
214 if (route) {
215 *ret = TAKE_PTR(route);
216 return 0;
6ae115c1
TG
217 }
218
2a54a044 219 if (hashmap_size(network->routes_by_section) >= routes_max())
8c34b963
LP
220 return -E2BIG;
221
ed9e361a 222 r = route_new(&route);
f0213e37
TG
223 if (r < 0)
224 return r;
801bd9e8 225
ed9e361a 226 route->protocol = RTPROT_STATIC;
0f7f2769 227 route->network = network;
2a54a044 228 route->section = TAKE_PTR(n);
cacc1dbf 229
2a54a044
YW
230 r = hashmap_ensure_allocated(&network->routes_by_section, &network_config_hash_ops);
231 if (r < 0)
232 return r;
3e570042 233
2a54a044
YW
234 r = hashmap_put(network->routes_by_section, route->section, route);
235 if (r < 0)
236 return r;
6ae115c1 237
1cc6c93a 238 *ret = TAKE_PTR(route);
f579559b
TG
239 return 0;
240}
241
169948e9 242Route *route_free(Route *route) {
f579559b 243 if (!route)
169948e9 244 return NULL;
f579559b 245
f048a16b 246 if (route->network) {
2a54a044
YW
247 assert(route->section);
248 hashmap_remove(route->network->routes_by_section, route->section);
f048a16b 249 }
6ae115c1 250
fd45e522
ZJS
251 network_config_section_free(route->section);
252
1c8e710c 253 if (route->link) {
50550722 254 NDiscRoute *n;
50550722 255
1c8e710c
TG
256 set_remove(route->link->routes, route);
257 set_remove(route->link->routes_foreign, route);
6e537f62
YW
258 set_remove(route->link->dhcp_routes, route);
259 set_remove(route->link->dhcp_routes_old, route);
1633c457
YW
260 set_remove(route->link->dhcp6_routes, route);
261 set_remove(route->link->dhcp6_routes_old, route);
262 set_remove(route->link->dhcp6_pd_routes, route);
263 set_remove(route->link->dhcp6_pd_routes_old, route);
90e74a66 264 SET_FOREACH(n, route->link->ndisc_routes)
50550722
YW
265 if (n->route == route)
266 free(set_remove(route->link->ndisc_routes, n));
1c8e710c
TG
267 }
268
ad208fac
YW
269 if (route->manager) {
270 set_remove(route->manager->routes, route);
271 set_remove(route->manager->routes_foreign, route);
272 }
273
6ff5cc6b
YW
274 ordered_set_free_free(route->multipath_routes);
275
f833694d
TG
276 sd_event_source_unref(route->expire);
277
169948e9 278 return mfree(route);
f579559b
TG
279}
280
501b09db 281void route_hash_func(const Route *route, struct siphash *state) {
bb7ae737
TG
282 assert(route);
283
284 siphash24_compress(&route->family, sizeof(route->family), state);
285
01aaa3df
YW
286 switch (route->family) {
287 case AF_INET:
288 case AF_INET6:
01aaa3df 289 siphash24_compress(&route->dst_prefixlen, sizeof(route->dst_prefixlen), state);
67e05dd8
ZJS
290 siphash24_compress(&route->dst, FAMILY_ADDRESS_SIZE(route->family), state);
291
01aaa3df 292 siphash24_compress(&route->src_prefixlen, sizeof(route->src_prefixlen), state);
67e05dd8
ZJS
293 siphash24_compress(&route->src, FAMILY_ADDRESS_SIZE(route->family), state);
294
6dd53981 295 siphash24_compress(&route->gw_family, sizeof(route->gw_family), state);
40075951 296 if (IN_SET(route->gw_family, AF_INET, AF_INET6)) {
6dd53981 297 siphash24_compress(&route->gw, FAMILY_ADDRESS_SIZE(route->gw_family), state);
40075951
YW
298 siphash24_compress(&route->gw_weight, sizeof(route->gw_weight), state);
299 }
67e05dd8 300
01aaa3df
YW
301 siphash24_compress(&route->prefsrc, FAMILY_ADDRESS_SIZE(route->family), state);
302
303 siphash24_compress(&route->tos, sizeof(route->tos), state);
304 siphash24_compress(&route->priority, sizeof(route->priority), state);
305 siphash24_compress(&route->table, sizeof(route->table), state);
306 siphash24_compress(&route->protocol, sizeof(route->protocol), state);
307 siphash24_compress(&route->scope, sizeof(route->scope), state);
308 siphash24_compress(&route->type, sizeof(route->type), state);
67e05dd8 309
fa3e401a
YW
310 siphash24_compress(&route->initcwnd, sizeof(route->initcwnd), state);
311 siphash24_compress(&route->initrwnd, sizeof(route->initrwnd), state);
01aaa3df
YW
312
313 break;
314 default:
315 /* treat any other address family as AF_UNSPEC */
316 break;
317 }
318}
319
501b09db 320int route_compare_func(const Route *a, const Route *b) {
01aaa3df
YW
321 int r;
322
323 r = CMP(a->family, b->family);
324 if (r != 0)
325 return r;
326
327 switch (a->family) {
328 case AF_INET:
329 case AF_INET6:
330 r = CMP(a->dst_prefixlen, b->dst_prefixlen);
331 if (r != 0)
332 return r;
333
67e05dd8
ZJS
334 r = memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
335 if (r != 0)
336 return r;
337
01aaa3df
YW
338 r = CMP(a->src_prefixlen, b->src_prefixlen);
339 if (r != 0)
340 return r;
341
67e05dd8 342 r = memcmp(&a->src, &b->src, FAMILY_ADDRESS_SIZE(a->family));
01aaa3df
YW
343 if (r != 0)
344 return r;
345
6dd53981 346 r = CMP(a->gw_family, b->gw_family);
01aaa3df
YW
347 if (r != 0)
348 return r;
349
6dd53981
YW
350 if (IN_SET(a->gw_family, AF_INET, AF_INET6)) {
351 r = memcmp(&a->gw, &b->gw, FAMILY_ADDRESS_SIZE(a->family));
352 if (r != 0)
353 return r;
40075951
YW
354
355 r = CMP(a->gw_weight, b->gw_weight);
356 if (r != 0)
357 return r;
6dd53981
YW
358 }
359
67e05dd8 360 r = memcmp(&a->prefsrc, &b->prefsrc, FAMILY_ADDRESS_SIZE(a->family));
01aaa3df
YW
361 if (r != 0)
362 return r;
363
67e05dd8 364 r = CMP(a->tos, b->tos);
01aaa3df
YW
365 if (r != 0)
366 return r;
367
67e05dd8 368 r = CMP(a->priority, b->priority);
01aaa3df
YW
369 if (r != 0)
370 return r;
371
67e05dd8 372 r = CMP(a->table, b->table);
01aaa3df
YW
373 if (r != 0)
374 return r;
375
67e05dd8 376 r = CMP(a->protocol, b->protocol);
fa3e401a
YW
377 if (r != 0)
378 return r;
379
67e05dd8 380 r = CMP(a->scope, b->scope);
fa3e401a
YW
381 if (r != 0)
382 return r;
383
67e05dd8 384 r = CMP(a->type, b->type);
01aaa3df
YW
385 if (r != 0)
386 return r;
387
67e05dd8 388 r = CMP(a->initcwnd, b->initcwnd);
01aaa3df
YW
389 if (r != 0)
390 return r;
391
67e05dd8 392 r = CMP(a->initrwnd, b->initrwnd);
01aaa3df
YW
393 if (r != 0)
394 return r;
395
67e05dd8 396 return 0;
01aaa3df
YW
397 default:
398 /* treat any other address family as AF_UNSPEC */
399 return 0;
400 }
401}
402
403DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
c077a205 404 route_hash_ops,
01aaa3df 405 Route,
c077a205
YW
406 route_hash_func,
407 route_compare_func,
01aaa3df
YW
408 route_free);
409
423c249c 410static bool route_equal(const Route *r1, const Route *r2) {
7ecf0c3e
TJ
411 if (r1 == r2)
412 return true;
413
414 if (!r1 || !r2)
415 return false;
416
417 return route_compare_func(r1, r2) == 0;
418}
419
423c249c 420static int route_get(const Manager *manager, const Link *link, const Route *in, Route **ret) {
f1368755 421 Route *existing;
1b566071 422
ad208fac 423 assert(manager || link);
f1368755 424 assert(in);
1c8e710c 425
ad208fac
YW
426 if (link) {
427 existing = set_get(link->routes, in);
428 if (existing) {
429 if (ret)
430 *ret = existing;
431 return 1;
432 }
1c8e710c 433
ad208fac
YW
434 existing = set_get(link->routes_foreign, in);
435 if (existing) {
436 if (ret)
437 *ret = existing;
438 return 0;
439 }
440 } else {
441 existing = set_get(manager->routes, in);
442 if (existing) {
443 if (ret)
444 *ret = existing;
445 return 1;
446 }
447
448 existing = set_get(manager->routes_foreign, in);
449 if (existing) {
450 if (ret)
451 *ret = existing;
452 return 0;
453 }
1b566071 454 }
1c8e710c 455
1b566071 456 return -ENOENT;
1c8e710c
TG
457}
458
f9bb3338
YW
459static void route_copy(Route *dest, const Route *src, const MultipathRoute *m) {
460 assert(dest);
461 assert(src);
462
463 dest->family = src->family;
464 dest->src = src->src;
465 dest->src_prefixlen = src->src_prefixlen;
466 dest->dst = src->dst;
467 dest->dst_prefixlen = src->dst_prefixlen;
468 dest->prefsrc = src->prefsrc;
469 dest->scope = src->scope;
470 dest->protocol = src->protocol;
471 dest->type = src->type;
472 dest->tos = src->tos;
473 dest->priority = src->priority;
474 dest->table = src->table;
475 dest->initcwnd = src->initcwnd;
476 dest->initrwnd = src->initrwnd;
477 dest->lifetime = src->lifetime;
478
479 if (m) {
480 dest->gw_family = m->gateway.family;
481 dest->gw = m->gateway.address;
482 dest->gw_weight = m->weight;
483 } else {
484 dest->gw_family = src->gw_family;
485 dest->gw = src->gw;
40075951 486 dest->gw_weight = src->gw_weight;
f9bb3338
YW
487 }
488}
889b550f 489
cc17f75f 490static int route_add_internal(Manager *manager, Link *link, Set **routes, const Route *in, Route **ret) {
8e766630 491 _cleanup_(route_freep) Route *route = NULL;
1c8e710c
TG
492 int r;
493
ad208fac 494 assert(manager || link);
1c8e710c 495 assert(routes);
f1368755 496 assert(in);
1c8e710c
TG
497
498 r = route_new(&route);
499 if (r < 0)
500 return r;
501
cc17f75f 502 route_copy(route, in, NULL);
1c8e710c 503
de7fef4b 504 r = set_ensure_put(routes, &route_hash_ops, route);
1c8e710c
TG
505 if (r < 0)
506 return r;
75a302b5
YW
507 if (r == 0)
508 return -EEXIST;
1c8e710c
TG
509
510 route->link = link;
ad208fac 511 route->manager = manager;
1c8e710c
TG
512
513 if (ret)
514 *ret = route;
515
516 route = NULL;
517
518 return 0;
519}
520
423c249c 521static int route_add_foreign(Manager *manager, Link *link, const Route *in, Route **ret) {
ad208fac 522 assert(manager || link);
cc17f75f 523 return route_add_internal(manager, link, link ? &link->routes_foreign : &manager->routes_foreign, in, ret);
1c8e710c
TG
524}
525
f9bb3338 526static int route_add(Manager *manager, Link *link, const Route *in, const MultipathRoute *m, Route **ret) {
cc17f75f 527 _cleanup_(route_freep) Route *tmp = NULL;
1c8e710c
TG
528 Route *route;
529 int r;
530
ad208fac
YW
531 assert(manager || link);
532 assert(in);
533
cc17f75f
YW
534 if (m) {
535 assert(link && (m->ifindex == 0 || m->ifindex == link->ifindex));
536
537 r = route_new(&tmp);
538 if (r < 0)
539 return r;
540
541 route_copy(tmp, in, m);
542 in = tmp;
543 }
544
ad208fac 545 r = route_get(manager, link, in, &route);
1c8e710c
TG
546 if (r == -ENOENT) {
547 /* Route does not exist, create a new one */
cc17f75f 548 r = route_add_internal(manager, link, link ? &link->routes : &manager->routes, in, &route);
1c8e710c
TG
549 if (r < 0)
550 return r;
551 } else if (r == 0) {
552 /* Take over a foreign route */
ad208fac
YW
553 if (link) {
554 r = set_ensure_put(&link->routes, &route_hash_ops, route);
555 if (r < 0)
556 return r;
1c8e710c 557
ad208fac
YW
558 set_remove(link->routes_foreign, route);
559 } else {
560 r = set_ensure_put(&manager->routes, &route_hash_ops, route);
561 if (r < 0)
562 return r;
563
564 set_remove(manager->routes_foreign, route);
565 }
1c8e710c
TG
566 } else if (r == 1) {
567 /* Route exists, do nothing */
568 ;
569 } else
570 return r;
571
856e309d
MC
572 if (ret)
573 *ret = route;
1c8e710c
TG
574
575 return 0;
576}
577
6c252588
YW
578static bool route_type_is_reject(const Route *route) {
579 assert(route);
580
581 return IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW);
582}
583
5f4d7aa4
YW
584static int route_set_netlink_message(const Route *route, sd_netlink_message *req, Link *link) {
585 unsigned flags;
586 int r;
587
588 assert(route);
589 assert(req);
590
591 /* link may be NULL */
592
593 if (in_addr_is_null(route->gw_family, &route->gw) == 0) {
594 if (route->gw_family == route->family) {
595 r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->gw_family, &route->gw);
596 if (r < 0)
597 return log_link_error_errno(link, r, "Could not append RTA_GATEWAY attribute: %m");
598 } else {
599 RouteVia rtvia = {
600 .family = route->gw_family,
601 .address = route->gw,
602 };
603
604 r = sd_netlink_message_append_data(req, RTA_VIA, &rtvia, sizeof(rtvia));
605 if (r < 0)
606 return log_link_error_errno(link, r, "Could not append RTA_VIA attribute: %m");
607 }
608 }
609
610 if (route->dst_prefixlen > 0) {
611 r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst);
612 if (r < 0)
613 return log_link_error_errno(link, r, "Could not append RTA_DST attribute: %m");
614
615 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
616 if (r < 0)
617 return log_link_error_errno(link, r, "Could not set destination prefix length: %m");
618 }
619
620 if (route->src_prefixlen > 0) {
621 r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src);
622 if (r < 0)
623 return log_link_error_errno(link, r, "Could not append RTA_SRC attribute: %m");
624
625 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
626 if (r < 0)
627 return log_link_error_errno(link, r, "Could not set source prefix length: %m");
628 }
629
630 if (in_addr_is_null(route->family, &route->prefsrc) == 0) {
631 r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc);
632 if (r < 0)
633 return log_link_error_errno(link, r, "Could not append RTA_PREFSRC attribute: %m");
634 }
635
636 r = sd_rtnl_message_route_set_scope(req, route->scope);
637 if (r < 0)
638 return log_link_error_errno(link, r, "Could not set scope: %m");
639
640 flags = route->flags;
641 if (route->gateway_onlink >= 0)
642 SET_FLAG(flags, RTNH_F_ONLINK, route->gateway_onlink);
643
644 r = sd_rtnl_message_route_set_flags(req, flags);
645 if (r < 0)
646 return log_link_error_errno(link, r, "Could not set flags: %m");
647
648 if (route->table != RT_TABLE_MAIN) {
649 if (route->table < 256) {
650 r = sd_rtnl_message_route_set_table(req, route->table);
651 if (r < 0)
652 return log_link_error_errno(link, r, "Could not set route table: %m");
653 } else {
654 r = sd_rtnl_message_route_set_table(req, RT_TABLE_UNSPEC);
655 if (r < 0)
656 return log_link_error_errno(link, r, "Could not set route table: %m");
657
658 /* Table attribute to allow more than 256. */
659 r = sd_netlink_message_append_data(req, RTA_TABLE, &route->table, sizeof(route->table));
660 if (r < 0)
661 return log_link_error_errno(link, r, "Could not append RTA_TABLE attribute: %m");
662 }
663 }
664
665 r = sd_rtnl_message_route_set_type(req, route->type);
666 if (r < 0)
667 return log_link_error_errno(link, r, "Could not set route type: %m");
668
6c252588 669 if (!route_type_is_reject(route)) {
5f4d7aa4
YW
670 assert(link); /* Those routes must be attached to a specific link */
671
672 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
673 if (r < 0)
674 return log_link_error_errno(link, r, "Could not append RTA_OIF attribute: %m");
675 }
676
677 r = sd_netlink_message_append_u8(req, RTA_PREF, route->pref);
678 if (r < 0)
679 return log_link_error_errno(link, r, "Could not append RTA_PREF attribute: %m");
680
681 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
682 if (r < 0)
683 return log_link_error_errno(link, r, "Could not append RTA_PRIORITY attribute: %m");
684
685 return 0;
686}
687
302a796f 688static int route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
4645ad47
YW
689 int r;
690
691 assert(m);
4645ad47 692
ad208fac
YW
693 /* Note that link may be NULL. */
694 if (link && IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
4645ad47
YW
695 return 1;
696
697 r = sd_netlink_message_get_errno(m);
698 if (r < 0 && r != -ESRCH)
5ecb131d 699 log_link_message_warning_errno(link, m, r, "Could not drop route, ignoring");
4645ad47
YW
700
701 return 1;
702}
703
ad208fac 704int route_remove(
423c249c 705 const Route *route,
ad208fac
YW
706 Manager *manager,
707 Link *link,
708 link_netlink_message_handler_t callback) {
27efb52b 709
4afd3348 710 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
5c1d3fc9
UTL
711 int r;
712
ad208fac 713 assert(link || manager);
4c701096 714 assert(IN_SET(route->family, AF_INET, AF_INET6));
5c1d3fc9 715
ad208fac
YW
716 if (!manager)
717 manager = link->manager;
081b3009 718 /* link may be NULL! */
ad208fac
YW
719
720 r = sd_rtnl_message_new_route(manager->rtnl, &req,
28cc555d
DW
721 RTM_DELROUTE, route->family,
722 route->protocol);
f647962d 723 if (r < 0)
7750b796 724 return log_link_error_errno(link, r, "Could not create RTM_DELROUTE message: %m");
5c1d3fc9 725
b297e0a7
YW
726 if (DEBUG_LOGGING) {
727 _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL;
d3e291fd 728 char scope[ROUTE_SCOPE_STR_MAX], table[ROUTE_TABLE_STR_MAX], protocol[ROUTE_PROTOCOL_STR_MAX];
b297e0a7
YW
729
730 if (!in_addr_is_null(route->family, &route->dst)) {
731 (void) in_addr_to_string(route->family, &route->dst, &dst);
732 (void) asprintf(&dst_prefixlen, "/%u", route->dst_prefixlen);
733 }
734 if (!in_addr_is_null(route->family, &route->src))
735 (void) in_addr_to_string(route->family, &route->src, &src);
6dd53981
YW
736 if (!in_addr_is_null(route->gw_family, &route->gw))
737 (void) in_addr_to_string(route->gw_family, &route->gw, &gw);
b297e0a7
YW
738 if (!in_addr_is_null(route->family, &route->prefsrc))
739 (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
740
d3e291fd 741 log_link_debug(link, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
b297e0a7
YW
742 strna(dst), strempty(dst_prefixlen), strna(src), strna(gw), strna(prefsrc),
743 format_route_scope(route->scope, scope, sizeof(scope)),
744 format_route_table(route->table, table, sizeof(table)),
d3e291fd 745 format_route_protocol(route->protocol, protocol, sizeof(protocol)),
b297e0a7
YW
746 strna(route_type_to_string(route->type)));
747 }
748
5f4d7aa4 749 r = route_set_netlink_message(route, req, link);
f647962d 750 if (r < 0)
5f4d7aa4 751 return r;
5c1d3fc9 752
bac319a7 753 r = netlink_call_async(manager->rtnl, NULL, req,
302a796f
YW
754 callback ?: route_remove_handler,
755 link_netlink_destroy_callback, link);
f647962d 756 if (r < 0)
7750b796 757 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
5c1d3fc9 758
081b3009 759 link_ref(link); /* link may be NULL, link_ref() is OK with that */
563c69c6 760
5c1d3fc9
UTL
761 return 0;
762}
763
1132a714 764static bool link_has_route(const Link *link, const Route *route) {
779804dd
YW
765 Route *net_route;
766
767 assert(link);
768 assert(route);
769
770 if (!link->network)
771 return false;
772
2a54a044 773 HASHMAP_FOREACH(net_route, link->network->routes_by_section)
779804dd
YW
774 if (route_equal(net_route, route))
775 return true;
776
777 return false;
778}
779
1132a714
YW
780static bool links_have_route(Manager *manager, const Route *route, const Link *except) {
781 Link *link;
782
783 assert(manager);
784
785 HASHMAP_FOREACH(link, manager->links) {
786 if (link == except)
787 continue;
788
789 if (link_has_route(link, route))
790 return true;
791 }
792
793 return false;
794}
795
796static int manager_drop_foreign_routes(Manager *manager) {
797 Route *route;
798 int k, r = 0;
799
800 assert(manager);
801
802 SET_FOREACH(route, manager->routes_foreign) {
803 /* do not touch routes managed by the kernel */
804 if (route->protocol == RTPROT_KERNEL)
805 continue;
806
807 if (links_have_route(manager, route, NULL))
808 /* The route will be configured later. */
809 continue;
810
811 /* The existing links do not have the route. Let's drop this now. It may by
812 * re-configured later. */
813 k = route_remove(route, manager, NULL, NULL);
814 if (k < 0 && r >= 0)
815 r = k;
816 }
817
818 return r;
819}
820
821static int manager_drop_routes(Manager *manager, Link *except) {
822 Route *route;
823 int k, r = 0;
824
825 assert(manager);
826
827 SET_FOREACH(route, manager->routes) {
828 /* do not touch routes managed by the kernel */
829 if (route->protocol == RTPROT_KERNEL)
830 continue;
831
832 if (links_have_route(manager, route, except))
833 /* The route will be configured later. */
834 continue;
835
836 /* The existing links do not have the route. Let's drop this now. It may by
837 * re-configured later. */
838 k = route_remove(route, manager, NULL, NULL);
839 if (k < 0 && r >= 0)
840 r = k;
841 }
842
843 return r;
844}
845
779804dd
YW
846int link_drop_foreign_routes(Link *link) {
847 Route *route;
848 int k, r = 0;
849
850 assert(link);
1132a714 851 assert(link->manager);
779804dd
YW
852
853 SET_FOREACH(route, link->routes_foreign) {
854 /* do not touch routes managed by the kernel */
855 if (route->protocol == RTPROT_KERNEL)
856 continue;
857
858 /* do not touch multicast route added by kernel */
859 /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that.
860 * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */
861 if (route->protocol == RTPROT_BOOT &&
862 route->family == AF_INET6 &&
863 route->dst_prefixlen == 8 &&
864 in_addr_equal(AF_INET6, &route->dst, &(union in_addr_union) { .in6 = {{{ 0xff,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }}} }))
865 continue;
866
867 if (route->protocol == RTPROT_STATIC && link->network &&
868 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
869 continue;
870
871 if (route->protocol == RTPROT_DHCP && link->network &&
872 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
873 continue;
874
1132a714 875 if (link_has_route(link, route))
f9bb3338 876 k = route_add(NULL, link, route, NULL, NULL);
779804dd 877 else
ad208fac 878 k = route_remove(route, NULL, link, NULL);
779804dd
YW
879 if (k < 0 && r >= 0)
880 r = k;
881 }
882
1132a714
YW
883 k = manager_drop_foreign_routes(link->manager);
884 if (k < 0 && r >= 0)
885 r = k;
886
779804dd
YW
887 return r;
888}
889
62f0ea5f
YW
890int link_drop_routes(Link *link) {
891 Route *route;
892 int k, r = 0;
893
894 assert(link);
895
896 SET_FOREACH(route, link->routes) {
897 /* do not touch routes managed by the kernel */
898 if (route->protocol == RTPROT_KERNEL)
899 continue;
900
ad208fac 901 k = route_remove(route, NULL, link, NULL);
62f0ea5f
YW
902 if (k < 0 && r >= 0)
903 r = k;
904 }
905
1132a714
YW
906 k = manager_drop_routes(link->manager, link);
907 if (k < 0 && r >= 0)
908 r = k;
909
62f0ea5f
YW
910 return r;
911}
912
74154c2e 913static int route_expire_handler(sd_event_source *s, uint64_t usec, void *userdata) {
f833694d
TG
914 Route *route = userdata;
915 int r;
916
917 assert(route);
918
ad208fac 919 r = route_remove(route, route->manager, route->link, NULL);
d6ad41e2 920 if (r < 0) {
98b02994 921 log_link_warning_errno(route->link, r, "Could not remove route: %m");
fe7ca21a 922 route_free(route);
d6ad41e2 923 }
f833694d
TG
924
925 return 1;
926}
927
f9bb3338
YW
928static int route_add_and_setup_timer(Link *link, const Route *route, const MultipathRoute *m, Route **ret) {
929 _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL;
930 Route *nr;
931 int r;
932
933 assert(link);
934 assert(route);
935
6c252588 936 if (route_type_is_reject(route))
f9bb3338
YW
937 r = route_add(link->manager, NULL, route, NULL, &nr);
938 else if (!m || m->ifindex == 0 || m->ifindex == link->ifindex)
939 r = route_add(NULL, link, route, m, &nr);
940 else {
941 Link *link_gw;
942
943 r = link_get(link->manager, m->ifindex, &link_gw);
944 if (r < 0)
945 return log_link_error_errno(link, r, "Failed to get link with ifindex %d: %m", m->ifindex);
946
947 r = route_add(NULL, link_gw, route, m, &nr);
948 }
949 if (r < 0)
950 return log_link_error_errno(link, r, "Could not add route: %m");
951
952 /* TODO: drop expiration handling once it can be pushed into the kernel */
953 if (nr->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) {
954 r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(),
955 nr->lifetime, 0, route_expire_handler, nr);
956 if (r < 0)
957 return log_link_error_errno(link, r, "Could not arm expiration timer: %m");
958 }
959
960 sd_event_source_unref(nr->expire);
961 nr->expire = TAKE_PTR(expire);
962
963 if (ret)
964 *ret = nr;
965
966 return 0;
967}
968
423c249c 969static int append_nexthop_one(const Route *route, const MultipathRoute *m, struct rtattr **rta, size_t offset) {
6ff5cc6b
YW
970 struct rtnexthop *rtnh;
971 struct rtattr *new_rta;
972 int r;
973
974 assert(route);
975 assert(m);
976 assert(rta);
977 assert(*rta);
978
979 new_rta = realloc(*rta, RTA_ALIGN((*rta)->rta_len) + RTA_SPACE(sizeof(struct rtnexthop)));
980 if (!new_rta)
981 return -ENOMEM;
982 *rta = new_rta;
983
984 rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset);
985 *rtnh = (struct rtnexthop) {
986 .rtnh_len = sizeof(*rtnh),
987 .rtnh_ifindex = m->ifindex,
988 .rtnh_hops = m->weight > 0 ? m->weight - 1 : 0,
989 };
990
991 (*rta)->rta_len += sizeof(struct rtnexthop);
992
993 if (route->family == m->gateway.family) {
994 r = rtattr_append_attribute(rta, RTA_GATEWAY, &m->gateway.address, FAMILY_ADDRESS_SIZE(m->gateway.family));
995 if (r < 0)
996 goto clear;
997 rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset);
998 rtnh->rtnh_len += RTA_SPACE(FAMILY_ADDRESS_SIZE(m->gateway.family));
999 } else {
1000 r = rtattr_append_attribute(rta, RTA_VIA, &m->gateway, FAMILY_ADDRESS_SIZE(m->gateway.family) + sizeof(m->gateway.family));
1001 if (r < 0)
1002 goto clear;
1003 rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset);
1004 rtnh->rtnh_len += RTA_SPACE(FAMILY_ADDRESS_SIZE(m->gateway.family) + sizeof(m->gateway.family));
1005 }
1006
1007 return 0;
1008
1009clear:
1010 (*rta)->rta_len -= sizeof(struct rtnexthop);
1011 return r;
1012}
1013
423c249c 1014static int append_nexthops(const Route *route, sd_netlink_message *req) {
6ff5cc6b
YW
1015 _cleanup_free_ struct rtattr *rta = NULL;
1016 struct rtnexthop *rtnh;
1017 MultipathRoute *m;
1018 size_t offset;
6ff5cc6b
YW
1019 int r;
1020
1021 if (ordered_set_isempty(route->multipath_routes))
1022 return 0;
1023
1024 rta = new(struct rtattr, 1);
1025 if (!rta)
1026 return -ENOMEM;
1027
1028 *rta = (struct rtattr) {
1029 .rta_type = RTA_MULTIPATH,
1030 .rta_len = RTA_LENGTH(0),
1031 };
1032 offset = (uint8_t *) RTA_DATA(rta) - (uint8_t *) rta;
1033
90e74a66 1034 ORDERED_SET_FOREACH(m, route->multipath_routes) {
6ff5cc6b
YW
1035 r = append_nexthop_one(route, m, &rta, offset);
1036 if (r < 0)
1037 return r;
1038
1039 rtnh = (struct rtnexthop *)((uint8_t *) rta + offset);
1040 offset = (uint8_t *) RTNH_NEXT(rtnh) - (uint8_t *) rta;
1041 }
1042
1043 r = sd_netlink_message_append_data(req, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta));
1044 if (r < 0)
1045 return r;
1046
1047 return 0;
1048}
1049
1b566071 1050int route_configure(
423c249c 1051 const Route *route,
1b566071 1052 Link *link,
80b0e860
YW
1053 link_netlink_message_handler_t callback,
1054 Route **ret) {
1b566071 1055
4afd3348 1056 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
f579559b
TG
1057 int r;
1058
f579559b 1059 assert(link);
f882c247
TG
1060 assert(link->manager);
1061 assert(link->manager->rtnl);
f579559b 1062 assert(link->ifindex > 0);
4c701096 1063 assert(IN_SET(route->family, AF_INET, AF_INET6));
bd1175bc 1064 assert(callback);
f579559b 1065
ad208fac 1066 if (route_get(link->manager, link, route, NULL) <= 0 &&
47d2d30d 1067 set_size(link->routes) >= routes_max())
7750b796
YW
1068 return log_link_error_errno(link, SYNTHETIC_ERRNO(E2BIG),
1069 "Too many routes are configured, refusing: %m");
1b566071 1070
156ed65e
YW
1071 if (DEBUG_LOGGING) {
1072 _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL;
d3e291fd 1073 char scope[ROUTE_SCOPE_STR_MAX], table[ROUTE_TABLE_STR_MAX], protocol[ROUTE_PROTOCOL_STR_MAX];
156ed65e
YW
1074
1075 if (!in_addr_is_null(route->family, &route->dst)) {
1076 (void) in_addr_to_string(route->family, &route->dst, &dst);
1077 (void) asprintf(&dst_prefixlen, "/%u", route->dst_prefixlen);
1078 }
1079 if (!in_addr_is_null(route->family, &route->src))
1080 (void) in_addr_to_string(route->family, &route->src, &src);
6dd53981
YW
1081 if (!in_addr_is_null(route->gw_family, &route->gw))
1082 (void) in_addr_to_string(route->gw_family, &route->gw, &gw);
156ed65e
YW
1083 if (!in_addr_is_null(route->family, &route->prefsrc))
1084 (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
1085
d3e291fd 1086 log_link_debug(link, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
b297e0a7
YW
1087 strna(dst), strempty(dst_prefixlen), strna(src), strna(gw), strna(prefsrc),
1088 format_route_scope(route->scope, scope, sizeof(scope)),
1089 format_route_table(route->table, table, sizeof(table)),
d3e291fd 1090 format_route_protocol(route->protocol, protocol, sizeof(protocol)),
b297e0a7 1091 strna(route_type_to_string(route->type)));
156ed65e
YW
1092 }
1093
151b9b96 1094 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
28cc555d
DW
1095 RTM_NEWROUTE, route->family,
1096 route->protocol);
f647962d 1097 if (r < 0)
7750b796 1098 return log_link_error_errno(link, r, "Could not create RTM_NEWROUTE message: %m");
f579559b 1099
5f4d7aa4 1100 r = route_set_netlink_message(route, req, link);
f647962d 1101 if (r < 0)
5f4d7aa4 1102 return r;
3b015d40 1103
f02ba163
DD
1104 if (route->lifetime != USEC_INFINITY && kernel_route_expiration_supported()) {
1105 r = sd_netlink_message_append_u32(req, RTA_EXPIRES,
1106 DIV_ROUND_UP(usec_sub_unsigned(route->lifetime, now(clock_boottime_or_monotonic())), USEC_PER_SEC));
1107 if (r < 0)
7750b796 1108 return log_link_error_errno(link, r, "Could not append RTA_EXPIRES attribute: %m");
f02ba163
DD
1109 }
1110
9b88f20a
SS
1111 if (route->ttl_propagate >= 0) {
1112 r = sd_netlink_message_append_u8(req, RTA_TTL_PROPAGATE, route->ttl_propagate);
1113 if (r < 0)
1114 return log_link_error_errno(link, r, "Could not append RTA_TTL_PROPAGATE attribute: %m");
1115 }
1116
d6fceaf1
SS
1117 r = sd_netlink_message_open_container(req, RTA_METRICS);
1118 if (r < 0)
7750b796 1119 return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m");
d6fceaf1
SS
1120
1121 if (route->mtu > 0) {
1122 r = sd_netlink_message_append_u32(req, RTAX_MTU, route->mtu);
1123 if (r < 0)
7750b796 1124 return log_link_error_errno(link, r, "Could not append RTAX_MTU attribute: %m");
d6fceaf1
SS
1125 }
1126
6b21ad33 1127 if (route->initcwnd > 0) {
323d9329
SS
1128 r = sd_netlink_message_append_u32(req, RTAX_INITCWND, route->initcwnd);
1129 if (r < 0)
7750b796 1130 return log_link_error_errno(link, r, "Could not append RTAX_INITCWND attribute: %m");
323d9329
SS
1131 }
1132
6b21ad33 1133 if (route->initrwnd > 0) {
323d9329
SS
1134 r = sd_netlink_message_append_u32(req, RTAX_INITRWND, route->initrwnd);
1135 if (r < 0)
7750b796 1136 return log_link_error_errno(link, r, "Could not append RTAX_INITRWND attribute: %m");
323d9329
SS
1137 }
1138
67c193bf 1139 if (route->quickack >= 0) {
09f5dfad
SS
1140 r = sd_netlink_message_append_u32(req, RTAX_QUICKACK, route->quickack);
1141 if (r < 0)
7750b796 1142 return log_link_error_errno(link, r, "Could not append RTAX_QUICKACK attribute: %m");
09f5dfad
SS
1143 }
1144
633c7258
SS
1145 if (route->fast_open_no_cookie >= 0) {
1146 r = sd_netlink_message_append_u32(req, RTAX_FASTOPEN_NO_COOKIE, route->fast_open_no_cookie);
1147 if (r < 0)
1148 return log_link_error_errno(link, r, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
1149 }
1150
d6fceaf1
SS
1151 r = sd_netlink_message_close_container(req);
1152 if (r < 0)
7750b796 1153 return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m");
d6fceaf1 1154
6ff5cc6b
YW
1155 r = append_nexthops(route, req);
1156 if (r < 0)
1157 return log_link_error_errno(link, r, "Could not append RTA_MULTIPATH attribute: %m");
1158
f9bb3338
YW
1159 if (ordered_set_isempty(route->multipath_routes)) {
1160 Route *nr;
1c8e710c 1161
f9bb3338 1162 r = route_add_and_setup_timer(link, route, NULL, &nr);
f833694d 1163 if (r < 0)
f9bb3338 1164 return r;
f833694d 1165
f9bb3338
YW
1166 if (ret)
1167 *ret = nr;
1168 } else {
1169 MultipathRoute *m;
f833694d 1170
f9bb3338
YW
1171 assert(!ret);
1172
1173 ORDERED_SET_FOREACH(m, route->multipath_routes) {
1174 r = route_add_and_setup_timer(link, route, m, NULL);
1175 if (r < 0)
1176 return r;
1177 }
1178 }
80b0e860 1179
0c54bfd6
YW
1180 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
1181 link_netlink_destroy_callback, link);
1182 if (r < 0)
1183 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1184
1185 link_ref(link);
1186
8c212f76 1187 return 0;
f579559b
TG
1188}
1189
141318f7
YW
1190static int route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1191 int r;
1192
1193 assert(link);
1194 assert(link->route_messages > 0);
141318f7
YW
1195
1196 link->route_messages--;
1197
1198 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1199 return 1;
1200
1201 r = sd_netlink_message_get_errno(m);
1202 if (r < 0 && r != -EEXIST) {
1203 log_link_message_warning_errno(link, m, r, "Could not set route");
1204 link_enter_failed(link);
1205 return 1;
1206 }
1207
1208 if (link->route_messages == 0) {
1209 log_link_debug(link, "Routes set");
1210 link->static_routes_configured = true;
1211 link_set_nexthop(link);
1212 }
1213
1214 return 1;
1215}
1216
1217int link_set_routes(Link *link) {
1218 enum {
1219 PHASE_NON_GATEWAY, /* First phase: Routes without a gateway */
1220 PHASE_GATEWAY, /* Second phase: Routes with a gateway */
1221 _PHASE_MAX
1222 } phase;
1223 Route *rt;
1224 int r;
1225
1226 assert(link);
1227 assert(link->network);
1228 assert(link->state != _LINK_STATE_INVALID);
1229
1230 link->static_routes_configured = false;
1231
1232 if (!link->addresses_ready)
1233 return 0;
1234
1235 if (!link_has_carrier(link) && !link->network->configure_without_carrier)
1236 /* During configuring addresses, the link lost its carrier. As networkd is dropping
1237 * the addresses now, let's not configure the routes either. */
1238 return 0;
1239
bd4733da
YW
1240 if (link->route_messages != 0) {
1241 log_link_debug(link, "Static routes are configuring.");
1242 return 0;
1243 }
1244
141318f7
YW
1245 r = link_set_routing_policy_rules(link);
1246 if (r < 0)
1247 return r;
1248
1249 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
1250 for (phase = 0; phase < _PHASE_MAX; phase++)
2a54a044 1251 HASHMAP_FOREACH(rt, link->network->routes_by_section) {
1a3a6309 1252 if (rt->gateway_from_dhcp_or_ra)
141318f7
YW
1253 continue;
1254
6dd53981 1255 if ((in_addr_is_null(rt->gw_family, &rt->gw) && ordered_set_isempty(rt->multipath_routes)) != (phase == PHASE_NON_GATEWAY))
141318f7
YW
1256 continue;
1257
1258 r = route_configure(rt, link, route_handler, NULL);
1259 if (r < 0)
1260 return log_link_warning_errno(link, r, "Could not set routes: %m");
8c212f76
YW
1261
1262 link->route_messages++;
141318f7
YW
1263 }
1264
1265 if (link->route_messages == 0) {
1266 link->static_routes_configured = true;
1267 link_set_nexthop(link);
1268 } else {
1269 log_link_debug(link, "Setting routes");
1270 link_set_state(link, LINK_STATE_CONFIGURING);
1271 }
1272
1273 return 0;
1274}
1275
f9bb3338
YW
1276static int process_route_one(Manager *manager, Link *link, uint16_t type, const Route *tmp, const MultipathRoute *m) {
1277 _cleanup_(route_freep) Route *nr = NULL;
1278 Route *route = NULL;
1279 int r;
1280
1281 assert(manager);
1282 assert(tmp);
1283 assert(IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE));
1284
1285 if (m) {
1286 if (link)
1287 return log_link_warning_errno(link, SYNTHETIC_ERRNO(EINVAL),
1288 "rtnl: received route contains both RTA_OIF and RTA_MULTIPATH, ignoring.");
1289
1290 if (m->ifindex <= 0)
1291 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1292 "rtnl: received multipath route with invalid ifindex, ignoring.");
1293
1294 r = link_get(manager, m->ifindex, &link);
1295 if (r < 0) {
1296 log_warning_errno(r, "rtnl: received multipath route for link (%d) we do not know, ignoring: %m", m->ifindex);
1297 return 0;
1298 }
1299
1300 r = route_new(&nr);
1301 if (r < 0)
1302 return log_oom();
1303
1304 route_copy(nr, tmp, m);
1305
1306 tmp = nr;
1307 }
1308
1309 (void) route_get(manager, link, tmp, &route);
1310
1311 if (DEBUG_LOGGING) {
1312 _cleanup_free_ char *buf_dst = NULL, *buf_dst_prefixlen = NULL,
1313 *buf_src = NULL, *buf_gw = NULL, *buf_prefsrc = NULL;
1314 char buf_scope[ROUTE_SCOPE_STR_MAX], buf_table[ROUTE_TABLE_STR_MAX],
1315 buf_protocol[ROUTE_PROTOCOL_STR_MAX];
1316
1317 if (!in_addr_is_null(tmp->family, &tmp->dst)) {
1318 (void) in_addr_to_string(tmp->family, &tmp->dst, &buf_dst);
1319 (void) asprintf(&buf_dst_prefixlen, "/%u", tmp->dst_prefixlen);
1320 }
1321 if (!in_addr_is_null(tmp->family, &tmp->src))
1322 (void) in_addr_to_string(tmp->family, &tmp->src, &buf_src);
1323 if (!in_addr_is_null(tmp->gw_family, &tmp->gw))
1324 (void) in_addr_to_string(tmp->gw_family, &tmp->gw, &buf_gw);
1325 if (!in_addr_is_null(tmp->family, &tmp->prefsrc))
1326 (void) in_addr_to_string(tmp->family, &tmp->prefsrc, &buf_prefsrc);
1327
1328 log_link_debug(link,
1329 "%s route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
1330 (!route && !manager->manage_foreign_routes) ? "Ignoring received foreign" :
1331 type == RTM_DELROUTE ? "Forgetting" :
1332 route ? "Received remembered" : "Remembering",
1333 strna(buf_dst), strempty(buf_dst_prefixlen),
1334 strna(buf_src), strna(buf_gw), strna(buf_prefsrc),
1335 format_route_scope(tmp->scope, buf_scope, sizeof buf_scope),
1336 format_route_table(tmp->table, buf_table, sizeof buf_table),
1337 format_route_protocol(tmp->protocol, buf_protocol, sizeof buf_protocol),
1338 strna(route_type_to_string(tmp->type)));
1339 }
1340
1341 switch (type) {
1342 case RTM_NEWROUTE:
1343 if (!route && manager->manage_foreign_routes) {
1344 /* A route appeared that we did not request */
1345 r = route_add_foreign(manager, link, tmp, NULL);
1346 if (r < 0) {
1347 log_link_warning_errno(link, r, "Failed to remember foreign route, ignoring: %m");
1348 return 0;
1349 }
1350 }
1351
1352 break;
1353
1354 case RTM_DELROUTE:
1355 route_free(route);
1356 break;
1357
1358 default:
1359 assert_not_reached("Received route message with invalid RTNL message type");
1360 }
1361
1362 return 1;
1363}
1364
4468f01b 1365int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
f9bb3338 1366 _cleanup_ordered_set_free_free_ OrderedSet *multipath_routes = NULL;
4468f01b 1367 _cleanup_(route_freep) Route *tmp = NULL;
f9bb3338 1368 _cleanup_free_ void *rta_multipath = NULL;
4468f01b
YW
1369 Link *link = NULL;
1370 uint32_t ifindex;
1371 uint16_t type;
1372 unsigned char table;
f9bb3338 1373 size_t rta_len;
4468f01b
YW
1374 int r;
1375
1376 assert(rtnl);
1377 assert(message);
1378 assert(m);
1379
1380 if (sd_netlink_message_is_error(message)) {
1381 r = sd_netlink_message_get_errno(message);
1382 if (r < 0)
1383 log_message_warning_errno(message, r, "rtnl: failed to receive route message, ignoring");
1384
1385 return 0;
1386 }
1387
1388 r = sd_netlink_message_get_type(message, &type);
1389 if (r < 0) {
1390 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1391 return 0;
1392 } else if (!IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE)) {
1393 log_warning("rtnl: received unexpected message type %u when processing route, ignoring.", type);
1394 return 0;
1395 }
1396
1397 r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex);
ad208fac 1398 if (r < 0 && r != -ENODATA) {
4468f01b
YW
1399 log_warning_errno(r, "rtnl: could not get ifindex from route message, ignoring: %m");
1400 return 0;
ad208fac
YW
1401 } else if (r >= 0) {
1402 if (ifindex <= 0) {
1403 log_warning("rtnl: received route message with invalid ifindex %d, ignoring.", ifindex);
1404 return 0;
1405 }
4468f01b 1406
ad208fac
YW
1407 r = link_get(m, ifindex, &link);
1408 if (r < 0 || !link) {
1409 /* when enumerating we might be out of sync, but we will
1410 * get the route again, so just ignore it */
1411 if (!m->enumerating)
1412 log_warning("rtnl: received route message for link (%d) we do not know about, ignoring", ifindex);
1413 return 0;
1414 }
4468f01b
YW
1415 }
1416
1417 r = route_new(&tmp);
1418 if (r < 0)
1419 return log_oom();
1420
1421 r = sd_rtnl_message_route_get_family(message, &tmp->family);
1422 if (r < 0) {
1423 log_link_warning(link, "rtnl: received route message without family, ignoring");
1424 return 0;
1425 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1426 log_link_debug(link, "rtnl: received route message with invalid family '%i', ignoring", tmp->family);
1427 return 0;
1428 }
1429
1430 r = sd_rtnl_message_route_get_protocol(message, &tmp->protocol);
1431 if (r < 0) {
1432 log_warning_errno(r, "rtnl: received route message without route protocol: %m");
1433 return 0;
1434 }
1435
ad6df717
YW
1436 r = netlink_message_read_in_addr_union(message, RTA_DST, tmp->family, &tmp->dst);
1437 if (r < 0 && r != -ENODATA) {
1438 log_link_warning_errno(link, r, "rtnl: received route message without valid destination, ignoring: %m");
1439 return 0;
1440 }
4468f01b 1441
ad6df717
YW
1442 r = netlink_message_read_in_addr_union(message, RTA_GATEWAY, tmp->family, &tmp->gw);
1443 if (r < 0 && r != -ENODATA) {
1444 log_link_warning_errno(link, r, "rtnl: received route message without valid gateway, ignoring: %m");
1445 return 0;
1446 } else if (r >= 0)
1447 tmp->gw_family = tmp->family;
1448 else if (tmp->family == AF_INET) {
1449 RouteVia via;
6dd53981
YW
1450
1451 r = sd_netlink_message_read(message, RTA_VIA, sizeof(via), &via);
1452 if (r < 0 && r != -ENODATA) {
1453 log_link_warning_errno(link, r, "rtnl: received route message without valid gateway, ignoring: %m");
1454 return 0;
1455 } else if (r >= 0) {
1456 tmp->gw_family = via.family;
1457 tmp->gw = via.address;
4468f01b 1458 }
ad6df717 1459 }
4468f01b 1460
ad6df717
YW
1461 r = netlink_message_read_in_addr_union(message, RTA_SRC, tmp->family, &tmp->src);
1462 if (r < 0 && r != -ENODATA) {
1463 log_link_warning_errno(link, r, "rtnl: received route message without valid source, ignoring: %m");
1464 return 0;
1465 }
4468f01b 1466
ad6df717
YW
1467 r = netlink_message_read_in_addr_union(message, RTA_PREFSRC, tmp->family, &tmp->prefsrc);
1468 if (r < 0 && r != -ENODATA) {
1469 log_link_warning_errno(link, r, "rtnl: received route message without valid preferred source, ignoring: %m");
4468f01b
YW
1470 return 0;
1471 }
1472
1473 r = sd_rtnl_message_route_get_dst_prefixlen(message, &tmp->dst_prefixlen);
1474 if (r < 0) {
1475 log_link_warning_errno(link, r, "rtnl: received route message with invalid destination prefixlen, ignoring: %m");
1476 return 0;
1477 }
1478
1479 r = sd_rtnl_message_route_get_src_prefixlen(message, &tmp->src_prefixlen);
1480 if (r < 0) {
1481 log_link_warning_errno(link, r, "rtnl: received route message with invalid source prefixlen, ignoring: %m");
1482 return 0;
1483 }
1484
1485 r = sd_rtnl_message_route_get_scope(message, &tmp->scope);
1486 if (r < 0) {
1487 log_link_warning_errno(link, r, "rtnl: received route message with invalid scope, ignoring: %m");
1488 return 0;
1489 }
1490
1491 r = sd_rtnl_message_route_get_tos(message, &tmp->tos);
1492 if (r < 0) {
1493 log_link_warning_errno(link, r, "rtnl: received route message with invalid tos, ignoring: %m");
1494 return 0;
1495 }
1496
1497 r = sd_rtnl_message_route_get_type(message, &tmp->type);
1498 if (r < 0) {
1499 log_link_warning_errno(link, r, "rtnl: received route message with invalid type, ignoring: %m");
1500 return 0;
1501 }
1502
1503 r = sd_rtnl_message_route_get_table(message, &table);
1504 if (r < 0) {
1505 log_link_warning_errno(link, r, "rtnl: received route message with invalid table, ignoring: %m");
1506 return 0;
1507 }
1508 tmp->table = table;
1509
1510 r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &tmp->priority);
1511 if (r < 0 && r != -ENODATA) {
1512 log_link_warning_errno(link, r, "rtnl: received route message with invalid priority, ignoring: %m");
1513 return 0;
1514 }
1515
1516 r = sd_netlink_message_enter_container(message, RTA_METRICS);
1517 if (r < 0 && r != -ENODATA) {
1518 log_link_error_errno(link, r, "rtnl: Could not enter RTA_METRICS container: %m");
1519 return 0;
1520 }
1521 if (r >= 0) {
1522 r = sd_netlink_message_read_u32(message, RTAX_INITCWND, &tmp->initcwnd);
1523 if (r < 0 && r != -ENODATA) {
1524 log_link_warning_errno(link, r, "rtnl: received route message with invalid initcwnd, ignoring: %m");
1525 return 0;
1526 }
1527
1528 r = sd_netlink_message_read_u32(message, RTAX_INITRWND, &tmp->initrwnd);
1529 if (r < 0 && r != -ENODATA) {
1530 log_link_warning_errno(link, r, "rtnl: received route message with invalid initrwnd, ignoring: %m");
1531 return 0;
1532 }
1533
1534 r = sd_netlink_message_exit_container(message);
1535 if (r < 0) {
1536 log_link_error_errno(link, r, "rtnl: Could not exit from RTA_METRICS container: %m");
1537 return 0;
1538 }
1539 }
1540
f9bb3338
YW
1541 r = sd_netlink_message_read_data(message, RTA_MULTIPATH, &rta_len, &rta_multipath);
1542 if (r < 0 && r != -ENODATA) {
1543 log_link_warning_errno(link, r, "rtnl: failed to read RTA_MULTIPATH attribute, ignoring: %m");
1544 return 0;
1545 } else if (r >= 0) {
1546 r = rtattr_read_nexthop(rta_multipath, rta_len, tmp->family, &multipath_routes);
1547 if (r < 0) {
1548 log_link_warning_errno(link, r, "rtnl: failed to parse RTA_MULTIPATH attribute, ignoring: %m");
1549 return 0;
4468f01b 1550 }
4468f01b
YW
1551 }
1552
575f14ee
YW
1553 /* IPv6 routes with reject type are always assigned to the loopback interface. See kernel's
1554 * fib6_nh_init() in net/ipv6/route.c. However, we'd like to manage them by Manager. Hence, set
1555 * link to NULL here. */
1556 if (route_type_is_reject(tmp))
1557 link = NULL;
1558
f9bb3338
YW
1559 if (ordered_set_isempty(multipath_routes))
1560 (void) process_route_one(m, link, type, tmp, NULL);
1561 else {
1562 MultipathRoute *mr;
4468f01b 1563
f9bb3338
YW
1564 ORDERED_SET_FOREACH(mr, multipath_routes) {
1565 r = process_route_one(m, link, type, tmp, mr);
1566 if (r < 0)
1567 break;
1568 }
4468f01b
YW
1569 }
1570
1571 return 1;
1572}
1573
fa7cd711 1574int network_add_ipv4ll_route(Network *network) {
fcbf4cb7 1575 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
2a54a044 1576 unsigned section_line;
fa7cd711
YW
1577 int r;
1578
1579 assert(network);
1580
1581 if (!network->ipv4ll_route)
1582 return 0;
1583
2a54a044
YW
1584 section_line = hashmap_find_free_section_line(network->routes_by_section);
1585
fa7cd711 1586 /* IPv4LLRoute= is in [Network] section. */
2a54a044 1587 r = route_new_static(network, network->filename, section_line, &n);
fa7cd711
YW
1588 if (r < 0)
1589 return r;
1590
1591 r = in_addr_from_string(AF_INET, "169.254.0.0", &n->dst);
1592 if (r < 0)
1593 return r;
1594
1595 n->family = AF_INET;
1596 n->dst_prefixlen = 16;
1597 n->scope = RT_SCOPE_LINK;
94d6e299
YW
1598 n->scope_set = true;
1599 n->table_set = true;
fa7cd711
YW
1600 n->priority = IPV4LL_ROUTE_METRIC;
1601 n->protocol = RTPROT_STATIC;
1602
1603 TAKE_PTR(n);
1604 return 0;
1605}
1606
5d5003ab
YW
1607int network_add_default_route_on_device(Network *network) {
1608 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
2a54a044 1609 unsigned section_line;
5d5003ab
YW
1610 int r;
1611
1612 assert(network);
1613
1614 if (!network->default_route_on_device)
1615 return 0;
1616
2a54a044
YW
1617 section_line = hashmap_find_free_section_line(network->routes_by_section);
1618
5d5003ab 1619 /* DefaultRouteOnDevice= is in [Network] section. */
2a54a044 1620 r = route_new_static(network, network->filename, section_line, &n);
5d5003ab
YW
1621 if (r < 0)
1622 return r;
1623
5d5003ab 1624 n->family = AF_INET;
c697db75
YW
1625 n->scope = RT_SCOPE_LINK;
1626 n->scope_set = true;
1627 n->protocol = RTPROT_STATIC;
5d5003ab
YW
1628
1629 TAKE_PTR(n);
1630 return 0;
1631}
1632
27efb52b
YW
1633int config_parse_gateway(
1634 const char *unit,
f579559b
TG
1635 const char *filename,
1636 unsigned line,
1637 const char *section,
71a61510 1638 unsigned section_line,
f579559b
TG
1639 const char *lvalue,
1640 int ltype,
1641 const char *rvalue,
1642 void *data,
1643 void *userdata) {
44e7b949 1644
6ae115c1 1645 Network *network = userdata;
fcbf4cb7 1646 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
7934dede 1647 int r;
f579559b
TG
1648
1649 assert(filename);
6ae115c1 1650 assert(section);
f579559b
TG
1651 assert(lvalue);
1652 assert(rvalue);
1653 assert(data);
1654
92fe133a 1655 if (streq(section, "Network")) {
2a54a044
YW
1656 /* we are not in an Route section, so use line number instead */
1657 r = route_new_static(network, filename, line, &n);
d96edb2c
YW
1658 if (r == -ENOMEM)
1659 return log_oom();
1660 if (r < 0) {
1661 log_syntax(unit, LOG_WARNING, filename, line, r,
1662 "Failed to allocate route, ignoring assignment: %m");
1663 return 0;
1664 }
1985c54f 1665 } else {
f4859fc7 1666 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
1667 if (r == -ENOMEM)
1668 return log_oom();
1669 if (r < 0) {
1670 log_syntax(unit, LOG_WARNING, filename, line, r,
1671 "Failed to allocate route, ignoring assignment: %m");
1672 return 0;
1673 }
1985c54f 1674
d442bb37 1675 if (isempty(rvalue)) {
1a3a6309 1676 n->gateway_from_dhcp_or_ra = false;
d442bb37
YW
1677 n->gw_family = AF_UNSPEC;
1678 n->gw = IN_ADDR_NULL;
1679 TAKE_PTR(n);
1680 return 0;
1681 }
1682
427928ca 1683 if (streq(rvalue, "_dhcp")) {
1a3a6309 1684 n->gateway_from_dhcp_or_ra = true;
1985c54f
YW
1685 TAKE_PTR(n);
1686 return 0;
1687 }
d306d1d0
YW
1688
1689 if (streq(rvalue, "_dhcp4")) {
1690 n->gw_family = AF_INET;
1a3a6309 1691 n->gateway_from_dhcp_or_ra = true;
d306d1d0
YW
1692 TAKE_PTR(n);
1693 return 0;
1694 }
1695
b8caa4ef 1696 if (streq(rvalue, "_ipv6ra")) {
d306d1d0 1697 n->gw_family = AF_INET6;
1a3a6309 1698 n->gateway_from_dhcp_or_ra = true;
d306d1d0
YW
1699 TAKE_PTR(n);
1700 return 0;
1701 }
1985c54f 1702 }
f579559b 1703
6dd53981 1704 r = in_addr_from_string_auto(rvalue, &n->gw_family, &n->gw);
f579559b 1705 if (r < 0) {
d96edb2c 1706 log_syntax(unit, LOG_WARNING, filename, line, r,
01d4e732 1707 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
f579559b
TG
1708 return 0;
1709 }
1710
1a3a6309 1711 n->gateway_from_dhcp_or_ra = false;
aff44301 1712 TAKE_PTR(n);
f579559b
TG
1713 return 0;
1714}
6ae115c1 1715
27efb52b
YW
1716int config_parse_preferred_src(
1717 const char *unit,
0d07e595
JK
1718 const char *filename,
1719 unsigned line,
1720 const char *section,
1721 unsigned section_line,
1722 const char *lvalue,
1723 int ltype,
1724 const char *rvalue,
1725 void *data,
1726 void *userdata) {
1727
1728 Network *network = userdata;
fcbf4cb7 1729 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
7934dede 1730 int r;
0d07e595
JK
1731
1732 assert(filename);
1733 assert(section);
1734 assert(lvalue);
1735 assert(rvalue);
1736 assert(data);
1737
f4859fc7 1738 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
1739 if (r == -ENOMEM)
1740 return log_oom();
1741 if (r < 0) {
1742 log_syntax(unit, LOG_WARNING, filename, line, r,
1743 "Failed to allocate route, ignoring assignment: %m");
1744 return 0;
1745 }
0d07e595 1746
01d4e732
YW
1747 if (n->family == AF_UNSPEC)
1748 r = in_addr_from_string_auto(rvalue, &n->family, &n->prefsrc);
1749 else
1750 r = in_addr_from_string(n->family, rvalue, &n->prefsrc);
0d07e595 1751 if (r < 0) {
d96edb2c 1752 log_syntax(unit, LOG_WARNING, filename, line, EINVAL,
01d4e732 1753 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
0d07e595
JK
1754 return 0;
1755 }
1756
aff44301 1757 TAKE_PTR(n);
0d07e595
JK
1758 return 0;
1759}
1760
27efb52b
YW
1761int config_parse_destination(
1762 const char *unit,
6ae115c1
TG
1763 const char *filename,
1764 unsigned line,
1765 const char *section,
1766 unsigned section_line,
1767 const char *lvalue,
1768 int ltype,
1769 const char *rvalue,
1770 void *data,
1771 void *userdata) {
44e7b949 1772
6ae115c1 1773 Network *network = userdata;
fcbf4cb7 1774 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
7934dede
YW
1775 union in_addr_union *buffer;
1776 unsigned char *prefixlen;
ca3bad65 1777 int r;
6ae115c1
TG
1778
1779 assert(filename);
1780 assert(section);
1781 assert(lvalue);
1782 assert(rvalue);
1783 assert(data);
1784
f4859fc7 1785 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
1786 if (r == -ENOMEM)
1787 return log_oom();
1788 if (r < 0) {
1789 log_syntax(unit, LOG_WARNING, filename, line, r,
1790 "Failed to allocate route, ignoring assignment: %m");
1791 return 0;
1792 }
6ae115c1 1793
9e7e4408 1794 if (streq(lvalue, "Destination")) {
7934dede
YW
1795 buffer = &n->dst;
1796 prefixlen = &n->dst_prefixlen;
9e7e4408 1797 } else if (streq(lvalue, "Source")) {
7934dede
YW
1798 buffer = &n->src;
1799 prefixlen = &n->src_prefixlen;
9e7e4408
TG
1800 } else
1801 assert_not_reached(lvalue);
1802
01d4e732
YW
1803 if (n->family == AF_UNSPEC)
1804 r = in_addr_prefix_from_string_auto(rvalue, &n->family, buffer, prefixlen);
1805 else
1806 r = in_addr_prefix_from_string(rvalue, n->family, buffer, prefixlen);
7934dede 1807 if (r < 0) {
d96edb2c 1808 log_syntax(unit, LOG_WARNING, filename, line, EINVAL,
01d4e732 1809 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
7934dede
YW
1810 return 0;
1811 }
1812
aff44301 1813 TAKE_PTR(n);
6ae115c1
TG
1814 return 0;
1815}
5d8e593d 1816
27efb52b
YW
1817int config_parse_route_priority(
1818 const char *unit,
1819 const char *filename,
1820 unsigned line,
1821 const char *section,
1822 unsigned section_line,
1823 const char *lvalue,
1824 int ltype,
1825 const char *rvalue,
1826 void *data,
1827 void *userdata) {
1828
5d8e593d 1829 Network *network = userdata;
fcbf4cb7 1830 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
5d8e593d
SS
1831 int r;
1832
1833 assert(filename);
1834 assert(section);
1835 assert(lvalue);
1836 assert(rvalue);
1837 assert(data);
1838
f4859fc7 1839 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
1840 if (r == -ENOMEM)
1841 return log_oom();
1842 if (r < 0) {
1843 log_syntax(unit, LOG_WARNING, filename, line, r,
1844 "Failed to allocate route, ignoring assignment: %m");
1845 return 0;
1846 }
5d8e593d 1847
aff44301 1848 r = safe_atou32(rvalue, &n->priority);
1c4b1179 1849 if (r < 0) {
d96edb2c 1850 log_syntax(unit, LOG_WARNING, filename, line, r,
1c4b1179
SS
1851 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue);
1852 return 0;
1853 }
5d8e593d 1854
c27abcf4 1855 n->priority_set = true;
aff44301 1856 TAKE_PTR(n);
5d8e593d
SS
1857 return 0;
1858}
769b56a3 1859
27efb52b
YW
1860int config_parse_route_scope(
1861 const char *unit,
1862 const char *filename,
1863 unsigned line,
1864 const char *section,
1865 unsigned section_line,
1866 const char *lvalue,
1867 int ltype,
1868 const char *rvalue,
1869 void *data,
1870 void *userdata) {
1871
769b56a3 1872 Network *network = userdata;
fcbf4cb7 1873 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
769b56a3
TG
1874 int r;
1875
1876 assert(filename);
1877 assert(section);
1878 assert(lvalue);
1879 assert(rvalue);
1880 assert(data);
1881
f4859fc7 1882 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
1883 if (r == -ENOMEM)
1884 return log_oom();
1885 if (r < 0) {
1886 log_syntax(unit, LOG_WARNING, filename, line, r,
1887 "Failed to allocate route, ignoring assignment: %m");
1888 return 0;
1889 }
769b56a3 1890
41b90a1e
YW
1891 r = route_scope_from_string(rvalue);
1892 if (r < 0) {
d96edb2c 1893 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown route scope: %s", rvalue);
769b56a3
TG
1894 return 0;
1895 }
1896
41b90a1e 1897 n->scope = r;
94d6e299 1898 n->scope_set = true;
aff44301 1899 TAKE_PTR(n);
769b56a3
TG
1900 return 0;
1901}
c953b24c 1902
27efb52b
YW
1903int config_parse_route_table(
1904 const char *unit,
1905 const char *filename,
1906 unsigned line,
1907 const char *section,
1908 unsigned section_line,
1909 const char *lvalue,
1910 int ltype,
1911 const char *rvalue,
1912 void *data,
1913 void *userdata) {
1914
fcbf4cb7 1915 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
c953b24c 1916 Network *network = userdata;
c953b24c
SS
1917 int r;
1918
1919 assert(filename);
1920 assert(section);
1921 assert(lvalue);
1922 assert(rvalue);
1923 assert(data);
1924
f4859fc7 1925 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
1926 if (r == -ENOMEM)
1927 return log_oom();
1928 if (r < 0) {
1929 log_syntax(unit, LOG_WARNING, filename, line, r,
1930 "Failed to allocate route, ignoring assignment: %m");
1931 return 0;
1932 }
c953b24c 1933
41b90a1e
YW
1934 r = route_table_from_string(rvalue);
1935 if (r >= 0)
1936 n->table = r;
1937 else {
1938 r = safe_atou32(rvalue, &n->table);
1939 if (r < 0) {
d96edb2c 1940 log_syntax(unit, LOG_WARNING, filename, line, r,
41b90a1e
YW
1941 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue);
1942 return 0;
1943 }
c953b24c
SS
1944 }
1945
94d6e299 1946 n->table_set = true;
aff44301 1947 TAKE_PTR(n);
c953b24c
SS
1948 return 0;
1949}
28959f7d 1950
d96edb2c 1951int config_parse_route_boolean(
27efb52b
YW
1952 const char *unit,
1953 const char *filename,
1954 unsigned line,
1955 const char *section,
1956 unsigned section_line,
1957 const char *lvalue,
1958 int ltype,
1959 const char *rvalue,
1960 void *data,
1961 void *userdata) {
1962
28959f7d 1963 Network *network = userdata;
fcbf4cb7 1964 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
28959f7d
SS
1965 int r;
1966
1967 assert(filename);
1968 assert(section);
1969 assert(lvalue);
1970 assert(rvalue);
1971 assert(data);
1972
1973 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
1974 if (r == -ENOMEM)
1975 return log_oom();
1976 if (r < 0) {
1977 log_syntax(unit, LOG_WARNING, filename, line, r,
1978 "Failed to allocate route, ignoring assignment: %m");
1979 return 0;
1980 }
28959f7d
SS
1981
1982 r = parse_boolean(rvalue);
1983 if (r < 0) {
d96edb2c 1984 log_syntax(unit, LOG_WARNING, filename, line, r,
9cb8c559 1985 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue, rvalue);
28959f7d
SS
1986 return 0;
1987 }
1988
d96edb2c
YW
1989 if (STR_IN_SET(lvalue, "GatewayOnLink", "GatewayOnlink"))
1990 n->gateway_onlink = r;
1991 else if (streq(lvalue, "QuickAck"))
1992 n->quickack = r;
1993 else if (streq(lvalue, "FastOpenNoCookie"))
1994 n->fast_open_no_cookie = r;
1995 else if (streq(lvalue, "TTLPropagate"))
1996 n->ttl_propagate = r;
1997 else
1998 assert_not_reached("Invalid lvalue");
54901fd2 1999
aff44301 2000 TAKE_PTR(n);
b5bf6f64
SS
2001 return 0;
2002}
2003
27efb52b
YW
2004int config_parse_ipv6_route_preference(
2005 const char *unit,
2006 const char *filename,
2007 unsigned line,
2008 const char *section,
2009 unsigned section_line,
2010 const char *lvalue,
2011 int ltype,
2012 const char *rvalue,
2013 void *data,
2014 void *userdata) {
2015
b5bf6f64 2016 Network *network = userdata;
fcbf4cb7 2017 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
b5bf6f64
SS
2018 int r;
2019
4c7bd9cf 2020 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
2021 if (r == -ENOMEM)
2022 return log_oom();
2023 if (r < 0) {
2024 log_syntax(unit, LOG_WARNING, filename, line, r,
2025 "Failed to allocate route, ignoring assignment: %m");
2026 return 0;
2027 }
4c7bd9cf 2028
b5bf6f64
SS
2029 if (streq(rvalue, "low"))
2030 n->pref = ICMPV6_ROUTER_PREF_LOW;
2031 else if (streq(rvalue, "medium"))
2032 n->pref = ICMPV6_ROUTER_PREF_MEDIUM;
2033 else if (streq(rvalue, "high"))
2034 n->pref = ICMPV6_ROUTER_PREF_HIGH;
2035 else {
d96edb2c 2036 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown route preference: %s", rvalue);
b5bf6f64
SS
2037 return 0;
2038 }
28959f7d 2039
c27abcf4 2040 n->pref_set = true;
aff44301 2041 TAKE_PTR(n);
28959f7d
SS
2042 return 0;
2043}
c83ecc04 2044
27efb52b
YW
2045int config_parse_route_protocol(
2046 const char *unit,
2047 const char *filename,
2048 unsigned line,
2049 const char *section,
2050 unsigned section_line,
2051 const char *lvalue,
2052 int ltype,
2053 const char *rvalue,
2054 void *data,
2055 void *userdata) {
2056
c83ecc04 2057 Network *network = userdata;
fcbf4cb7 2058 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
c83ecc04
SS
2059 int r;
2060
2061 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
2062 if (r == -ENOMEM)
2063 return log_oom();
2064 if (r < 0) {
2065 log_syntax(unit, LOG_WARNING, filename, line, r,
2066 "Failed to allocate route, ignoring assignment: %m");
2067 return 0;
2068 }
c83ecc04 2069
1b64651b
YW
2070 r = route_protocol_from_string(rvalue);
2071 if (r >= 0)
2072 n->protocol = r;
c83ecc04
SS
2073 else {
2074 r = safe_atou8(rvalue , &n->protocol);
2075 if (r < 0) {
d96edb2c 2076 log_syntax(unit, LOG_WARNING, filename, line, r,
f205a92a 2077 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue);
c83ecc04
SS
2078 return 0;
2079 }
2080 }
2081
aff44301 2082 TAKE_PTR(n);
c83ecc04
SS
2083 return 0;
2084}
983226f3 2085
27efb52b
YW
2086int config_parse_route_type(
2087 const char *unit,
2088 const char *filename,
2089 unsigned line,
2090 const char *section,
2091 unsigned section_line,
2092 const char *lvalue,
2093 int ltype,
2094 const char *rvalue,
2095 void *data,
2096 void *userdata) {
2097
983226f3 2098 Network *network = userdata;
fcbf4cb7 2099 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
7a22312d 2100 int t, r;
983226f3
SS
2101
2102 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
2103 if (r == -ENOMEM)
2104 return log_oom();
2105 if (r < 0) {
2106 log_syntax(unit, LOG_WARNING, filename, line, r,
2107 "Failed to allocate route, ignoring assignment: %m");
2108 return 0;
2109 }
983226f3 2110
7a22312d
YW
2111 t = route_type_from_string(rvalue);
2112 if (t < 0) {
d96edb2c 2113 log_syntax(unit, LOG_WARNING, filename, line, 0,
f205a92a 2114 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue);
983226f3
SS
2115 return 0;
2116 }
2117
7a22312d
YW
2118 n->type = (unsigned char) t;
2119
aff44301 2120 TAKE_PTR(n);
983226f3
SS
2121 return 0;
2122}
323d9329 2123
27efb52b
YW
2124int config_parse_tcp_window(
2125 const char *unit,
2126 const char *filename,
2127 unsigned line,
2128 const char *section,
2129 unsigned section_line,
2130 const char *lvalue,
2131 int ltype,
2132 const char *rvalue,
2133 void *data,
2134 void *userdata) {
2135
fcbf4cb7 2136 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
6b21ad33 2137 Network *network = userdata;
fef160b5 2138 uint32_t k;
323d9329
SS
2139 int r;
2140
2141 assert(filename);
2142 assert(section);
2143 assert(lvalue);
2144 assert(rvalue);
2145 assert(data);
2146
2147 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
2148 if (r == -ENOMEM)
2149 return log_oom();
2150 if (r < 0) {
2151 log_syntax(unit, LOG_WARNING, filename, line, r,
2152 "Failed to allocate route, ignoring assignment: %m");
2153 return 0;
2154 }
323d9329 2155
fef160b5 2156 r = safe_atou32(rvalue, &k);
f205a92a 2157 if (r < 0) {
d96edb2c 2158 log_syntax(unit, LOG_WARNING, filename, line, r,
f205a92a
YW
2159 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue, rvalue);
2160 return 0;
2161 }
fef160b5 2162 if (k >= 1024) {
d96edb2c 2163 log_syntax(unit, LOG_WARNING, filename, line, 0,
f205a92a 2164 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue, rvalue);
323d9329
SS
2165 return 0;
2166 }
2167
2168 if (streq(lvalue, "InitialCongestionWindow"))
2169 n->initcwnd = k;
2170 else if (streq(lvalue, "InitialAdvertisedReceiveWindow"))
2171 n->initrwnd = k;
f205a92a
YW
2172 else
2173 assert_not_reached("Invalid TCP window type.");
323d9329 2174
aff44301 2175 TAKE_PTR(n);
323d9329
SS
2176 return 0;
2177}
09f5dfad 2178
cea79e66
SS
2179int config_parse_route_mtu(
2180 const char *unit,
2181 const char *filename,
2182 unsigned line,
2183 const char *section,
2184 unsigned section_line,
2185 const char *lvalue,
2186 int ltype,
2187 const char *rvalue,
2188 void *data,
2189 void *userdata) {
2190
2191 Network *network = userdata;
fcbf4cb7 2192 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
cea79e66
SS
2193 int r;
2194
2195 assert(filename);
2196 assert(section);
2197 assert(lvalue);
2198 assert(rvalue);
2199 assert(data);
2200
2201 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
2202 if (r == -ENOMEM)
2203 return log_oom();
2204 if (r < 0) {
2205 log_syntax(unit, LOG_WARNING, filename, line, r,
2206 "Failed to allocate route, ignoring assignment: %m");
2207 return 0;
2208 }
cea79e66
SS
2209
2210 r = config_parse_mtu(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &n->mtu, userdata);
2211 if (r < 0)
2212 return r;
2213
aff44301 2214 TAKE_PTR(n);
cea79e66
SS
2215 return 0;
2216}
fcbf4cb7 2217
6ff5cc6b
YW
2218int config_parse_multipath_route(
2219 const char *unit,
2220 const char *filename,
2221 unsigned line,
2222 const char *section,
2223 unsigned section_line,
2224 const char *lvalue,
2225 int ltype,
2226 const char *rvalue,
2227 void *data,
2228 void *userdata) {
2229
2230 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
2231 _cleanup_free_ char *word = NULL, *buf = NULL;
2232 _cleanup_free_ MultipathRoute *m = NULL;
2233 Network *network = userdata;
2234 const char *p, *ip, *dev;
2235 union in_addr_union a;
2236 int family, r;
2237
2238 assert(filename);
2239 assert(section);
2240 assert(lvalue);
2241 assert(rvalue);
2242 assert(data);
2243
2244 r = route_new_static(network, filename, section_line, &n);
d96edb2c
YW
2245 if (r == -ENOMEM)
2246 return log_oom();
2247 if (r < 0) {
2248 log_syntax(unit, LOG_WARNING, filename, line, r,
2249 "Failed to allocate route, ignoring assignment: %m");
2250 return 0;
2251 }
6ff5cc6b
YW
2252
2253 if (isempty(rvalue)) {
2254 n->multipath_routes = ordered_set_free_free(n->multipath_routes);
2255 return 0;
2256 }
2257
2258 m = new0(MultipathRoute, 1);
2259 if (!m)
2260 return log_oom();
2261
2262 p = rvalue;
2263 r = extract_first_word(&p, &word, NULL, 0);
2264 if (r == -ENOMEM)
2265 return log_oom();
2266 if (r <= 0) {
d96edb2c 2267 log_syntax(unit, LOG_WARNING, filename, line, r,
6ff5cc6b
YW
2268 "Invalid multipath route option, ignoring assignment: %s", rvalue);
2269 return 0;
2270 }
2271
2272 dev = strchr(word, '@');
2273 if (dev) {
2274 buf = strndup(word, dev - word);
2275 if (!buf)
2276 return log_oom();
2277 ip = buf;
2278 dev++;
2279 } else
2280 ip = word;
2281
2282 r = in_addr_from_string_auto(ip, &family, &a);
2283 if (r < 0) {
d96edb2c 2284 log_syntax(unit, LOG_WARNING, filename, line, r,
6ff5cc6b
YW
2285 "Invalid multipath route gateway '%s', ignoring assignment: %m", rvalue);
2286 return 0;
2287 }
2288 m->gateway.address = a;
2289 m->gateway.family = family;
2290
2291 if (dev) {
d308bb99 2292 r = resolve_interface(NULL, dev);
6ff5cc6b 2293 if (r < 0) {
d96edb2c 2294 log_syntax(unit, LOG_WARNING, filename, line, r,
6ff5cc6b
YW
2295 "Invalid interface name or index, ignoring assignment: %s", dev);
2296 return 0;
2297 }
597da51b 2298 m->ifindex = r;
6ff5cc6b
YW
2299 }
2300
2301 if (!isempty(p)) {
2302 r = safe_atou32(p, &m->weight);
2303 if (r < 0) {
d96edb2c 2304 log_syntax(unit, LOG_WARNING, filename, line, r,
6ff5cc6b
YW
2305 "Invalid multipath route weight, ignoring assignment: %s", p);
2306 return 0;
2307 }
2308 if (m->weight == 0 || m->weight > 256) {
d96edb2c 2309 log_syntax(unit, LOG_WARNING, filename, line, 0,
6ff5cc6b
YW
2310 "Invalid multipath route weight, ignoring assignment: %s", p);
2311 return 0;
2312 }
2313 }
2314
2315 r = ordered_set_ensure_allocated(&n->multipath_routes, NULL);
2316 if (r < 0)
2317 return log_oom();
2318
2319 r = ordered_set_put(n->multipath_routes, m);
2320 if (r < 0) {
d96edb2c 2321 log_syntax(unit, LOG_WARNING, filename, line, r,
6ff5cc6b
YW
2322 "Failed to store multipath route, ignoring assignment: %m");
2323 return 0;
2324 }
2325
2326 TAKE_PTR(m);
2327 TAKE_PTR(n);
2328 return 0;
2329}
2330
d9940a3f 2331static int route_section_verify(Route *route, Network *network) {
fcbf4cb7
YW
2332 if (section_is_invalid(route->section))
2333 return -EINVAL;
2334
956dbf36 2335 if (route->gateway_from_dhcp_or_ra) {
c3d679c4
YW
2336 if (route->gw_family == AF_UNSPEC) {
2337 /* When deprecated Gateway=_dhcp is set, then assume gateway family based on other settings. */
2338 switch (route->family) {
2339 case AF_UNSPEC:
2340 log_warning("%s: Deprecated value \"_dhcp\" is specified for Gateway= in [Route] section from line %u. "
2341 "Please use \"_dhcp4\" or \"_ipv6ra\" instead. Assuming \"_dhcp4\".",
2342 route->section->filename, route->section->line);
2343 route->family = AF_INET;
2344 break;
2345 case AF_INET:
2346 case AF_INET6:
2347 log_warning("%s: Deprecated value \"_dhcp\" is specified for Gateway= in [Route] section from line %u. "
2348 "Assuming \"%s\" based on Destination=, Source=, or PreferredSource= setting.",
2349 route->section->filename, route->section->line, route->family == AF_INET ? "_dhcp4" : "_ipv6ra");
2350 break;
2351 default:
2352 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
2353 "%s: Invalid route family. Ignoring [Route] section from line %u.",
2354 route->section->filename, route->section->line);
2355 }
2356 route->gw_family = route->family;
2357 }
2358
956dbf36
YW
2359 if (route->gw_family == AF_INET && !FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV4))
2360 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
2361 "%s: Gateway=\"_dhcp4\" is specified but DHCPv4 client is disabled. "
2362 "Ignoring [Route] section from line %u.",
2363 route->section->filename, route->section->line);
2364
2365 if (route->gw_family == AF_INET6 && !network->ipv6_accept_ra)
2366 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
2367 "%s: Gateway=\"_ipv6ra\" is specified but IPv6AcceptRA= is disabled. "
2368 "Ignoring [Route] section from line %u.",
2369 route->section->filename, route->section->line);
2370 }
2371
c3d679c4 2372 /* When only Gateway= is specified, assume the route family based on the Gateway address. */
6dd53981
YW
2373 if (route->family == AF_UNSPEC)
2374 route->family = route->gw_family;
2375
fcbf4cb7
YW
2376 if (route->family == AF_UNSPEC) {
2377 assert(route->section);
2378
c3d679c4
YW
2379 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
2380 "%s: Route section without Gateway=, Destination=, Source=, "
2381 "or PreferredSource= field configured. "
2382 "Ignoring [Route] section from line %u.",
2383 route->section->filename, route->section->line);
fcbf4cb7
YW
2384 }
2385
6dd53981
YW
2386 if (route->family == AF_INET6 && route->gw_family == AF_INET)
2387 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
2388 "%s: IPv4 gateway is configured for IPv6 route. "
2389 "Ignoring [Route] section from line %u.",
2390 route->section->filename, route->section->line);
2391
c0d48bc5
YW
2392 if (!route->table_set && network->vrf) {
2393 route->table = VRF(network->vrf)->table;
2394 route->table_set = true;
2395 }
2396
f5c38922
YW
2397 if (!route->table_set && IN_SET(route->type, RTN_LOCAL, RTN_BROADCAST, RTN_ANYCAST, RTN_NAT))
2398 route->table = RT_TABLE_LOCAL;
2399
2400 if (!route->scope_set && route->family != AF_INET6) {
2401 if (IN_SET(route->type, RTN_LOCAL, RTN_NAT))
2402 route->scope = RT_SCOPE_HOST;
2403 else if (IN_SET(route->type, RTN_BROADCAST, RTN_ANYCAST, RTN_MULTICAST))
2404 route->scope = RT_SCOPE_LINK;
94d6e299
YW
2405 }
2406
fd7701bf
YW
2407 if (route->scope != RT_SCOPE_UNIVERSE && route->family == AF_INET6) {
2408 log_warning("%s: Scope= is specified for IPv6 route. It will be ignored.", route->section->filename);
2409 route->scope = RT_SCOPE_UNIVERSE;
2410 }
2411
8973df5c
YW
2412 if (route->family == AF_INET6 && route->priority == 0)
2413 route->priority = IP6_RT_PRIO_USER;
2414
9cd9fc8f 2415 if (ordered_hashmap_isempty(network->addresses_by_section) &&
6dd53981 2416 in_addr_is_null(route->gw_family, &route->gw) == 0 &&
fcbf4cb7
YW
2417 route->gateway_onlink < 0) {
2418 log_warning("%s: Gateway= without static address configured. "
2419 "Enabling GatewayOnLink= option.",
2420 network->filename);
2421 route->gateway_onlink = true;
2422 }
2423
2424 return 0;
2425}
d9940a3f 2426
13ffa39f 2427void network_drop_invalid_routes(Network *network) {
2a54a044 2428 Route *route;
d9940a3f
YW
2429
2430 assert(network);
2431
2a54a044 2432 HASHMAP_FOREACH(route, network->routes_by_section)
d9940a3f
YW
2433 if (route_section_verify(route, network) < 0)
2434 route_free(route);
2435}