]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-route.c
network: drop sections contain invalid settings in network_verify()
[thirdparty/systemd.git] / src / network / networkd-route.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f579559b 2
b5bf6f64
SS
3#include <linux/icmpv6.h>
4
b5efdb8a 5#include "alloc-util.h"
f579559b 6#include "conf-parser.h"
bb7ae737 7#include "in-addr-util.h"
36dd5ffd 8#include "missing_network.h"
fc2f9534 9#include "netlink-util.h"
23f53b99 10#include "networkd-manager.h"
6bedfcbb 11#include "networkd-route.h"
6bedfcbb 12#include "parse-util.h"
1c8e710c 13#include "set.h"
07630cea 14#include "string-util.h"
47d2d30d 15#include "sysctl-util.h"
07630cea 16#include "util.h"
f579559b 17
47d2d30d
ZJS
18#define ROUTES_DEFAULT_MAX_PER_FAMILY 4096U
19
20static unsigned routes_max(void) {
21 static thread_local unsigned cached = 0;
22
23 _cleanup_free_ char *s4 = NULL, *s6 = NULL;
24 unsigned val4 = ROUTES_DEFAULT_MAX_PER_FAMILY, val6 = ROUTES_DEFAULT_MAX_PER_FAMILY;
25
26 if (cached > 0)
27 return cached;
28
29 if (sysctl_read("net/ipv4/route/max_size", &s4) >= 0) {
30 truncate_nl(s4);
31 if (safe_atou(s4, &val4) >= 0 &&
32 val4 == 2147483647U)
33 /* This is the default "no limit" value in the kernel */
34 val4 = ROUTES_DEFAULT_MAX_PER_FAMILY;
35 }
36
37 if (sysctl_read("net/ipv6/route/max_size", &s6) >= 0) {
38 truncate_nl(s6);
39 (void) safe_atou(s6, &val6);
40 }
41
42 cached = MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val4) +
43 MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val6);
44 return cached;
45}
8c34b963 46
ed9e361a 47int route_new(Route **ret) {
8e766630 48 _cleanup_(route_freep) Route *route = NULL;
f0213e37 49
17f9c355 50 route = new(Route, 1);
f0213e37
TG
51 if (!route)
52 return -ENOMEM;
53
17f9c355
YW
54 *route = (Route) {
55 .family = AF_UNSPEC,
56 .scope = RT_SCOPE_UNIVERSE,
57 .protocol = RTPROT_UNSPEC,
58 .type = RTN_UNICAST,
59 .table = RT_TABLE_MAIN,
60 .lifetime = USEC_INFINITY,
61 .quickack = -1,
54901fd2 62 .gateway_onlink = -1,
17f9c355 63 };
f0213e37 64
1cc6c93a 65 *ret = TAKE_PTR(route);
f0213e37
TG
66
67 return 0;
68}
69
9560e5b3 70static int route_new_static(Network *network, const char *filename, unsigned section_line, Route **ret) {
8e766630
LP
71 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
72 _cleanup_(route_freep) Route *route = NULL;
f0213e37 73 int r;
f579559b 74
8c34b963
LP
75 assert(network);
76 assert(ret);
48317c39 77 assert(!!filename == (section_line > 0));
8c34b963 78
48317c39 79 if (filename) {
f4859fc7
SS
80 r = network_config_section_new(filename, section_line, &n);
81 if (r < 0)
82 return r;
83
84 route = hashmap_get(network->routes_by_section, n);
6ae115c1 85 if (route) {
1cc6c93a 86 *ret = TAKE_PTR(route);
6ae115c1
TG
87
88 return 0;
89 }
90 }
91
47d2d30d 92 if (network->n_static_routes >= routes_max())
8c34b963
LP
93 return -E2BIG;
94
ed9e361a 95 r = route_new(&route);
f0213e37
TG
96 if (r < 0)
97 return r;
801bd9e8 98
ed9e361a 99 route->protocol = RTPROT_STATIC;
0f7f2769
YW
100 route->network = network;
101 LIST_PREPEND(routes, network->static_routes, route);
102 network->n_static_routes++;
f579559b 103
48317c39 104 if (filename) {
1cc6c93a 105 route->section = TAKE_PTR(n);
cacc1dbf 106
3e570042
YW
107 r = hashmap_ensure_allocated(&network->routes_by_section, &network_config_hash_ops);
108 if (r < 0)
109 return r;
110
fcc48287 111 r = hashmap_put(network->routes_by_section, route->section, route);
21b39268
LP
112 if (r < 0)
113 return r;
6ae115c1
TG
114 }
115
1cc6c93a 116 *ret = TAKE_PTR(route);
f579559b
TG
117
118 return 0;
119}
120
121void route_free(Route *route) {
122 if (!route)
123 return;
124
f048a16b 125 if (route->network) {
3d3d4255 126 LIST_REMOVE(routes, route->network->static_routes, route);
f579559b 127
8c34b963
LP
128 assert(route->network->n_static_routes > 0);
129 route->network->n_static_routes--;
130
fd45e522 131 if (route->section)
f4859fc7 132 hashmap_remove(route->network->routes_by_section, route->section);
f048a16b 133 }
6ae115c1 134
fd45e522
ZJS
135 network_config_section_free(route->section);
136
1c8e710c
TG
137 if (route->link) {
138 set_remove(route->link->routes, route);
139 set_remove(route->link->routes_foreign, route);
140 }
141
f833694d
TG
142 sd_event_source_unref(route->expire);
143
f579559b
TG
144 free(route);
145}
146
7a08d314 147static void route_hash_func(const Route *route, struct siphash *state) {
bb7ae737
TG
148 assert(route);
149
150 siphash24_compress(&route->family, sizeof(route->family), state);
151
152 switch (route->family) {
153 case AF_INET:
154 case AF_INET6:
155 /* Equality of routes are given by the 4-touple
156 (dst_prefix,dst_prefixlen,tos,priority,table) */
2ce40956 157 siphash24_compress(&route->dst, FAMILY_ADDRESS_SIZE(route->family), state);
bb7ae737
TG
158 siphash24_compress(&route->dst_prefixlen, sizeof(route->dst_prefixlen), state);
159 siphash24_compress(&route->tos, sizeof(route->tos), state);
160 siphash24_compress(&route->priority, sizeof(route->priority), state);
161 siphash24_compress(&route->table, sizeof(route->table), state);
162
163 break;
164 default:
165 /* treat any other address family as AF_UNSPEC */
166 break;
167 }
168}
169
7a08d314 170static int route_compare_func(const Route *a, const Route *b) {
a0edd02e 171 int r;
bb7ae737 172
a0edd02e
FB
173 r = CMP(a->family, b->family);
174 if (r != 0)
175 return r;
bb7ae737
TG
176
177 switch (a->family) {
178 case AF_INET:
179 case AF_INET6:
a0edd02e
FB
180 r = CMP(a->dst_prefixlen, b->dst_prefixlen);
181 if (r != 0)
182 return r;
183
184 r = CMP(a->tos, b->tos);
185 if (r != 0)
186 return r;
187
188 r = CMP(a->priority, b->priority);
189 if (r != 0)
190 return r;
191
192 r = CMP(a->table, b->table);
193 if (r != 0)
194 return r;
bb7ae737 195
2ce40956 196 return memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
bb7ae737
TG
197 default:
198 /* treat any other address family as AF_UNSPEC */
199 return 0;
200 }
201}
202
7a08d314 203DEFINE_PRIVATE_HASH_OPS(route_hash_ops, Route, route_hash_func, route_compare_func);
bb7ae737 204
7ecf0c3e
TJ
205bool route_equal(Route *r1, Route *r2) {
206 if (r1 == r2)
207 return true;
208
209 if (!r1 || !r2)
210 return false;
211
212 return route_compare_func(r1, r2) == 0;
213}
214
1c8e710c
TG
215int route_get(Link *link,
216 int family,
1b566071 217 const union in_addr_union *dst,
1c8e710c
TG
218 unsigned char dst_prefixlen,
219 unsigned char tos,
220 uint32_t priority,
14d20d2b 221 uint32_t table,
1c8e710c 222 Route **ret) {
1b566071
LP
223
224 Route route, *existing;
225
226 assert(link);
227 assert(dst);
228
229 route = (Route) {
1c8e710c 230 .family = family,
1b566071 231 .dst = *dst,
1c8e710c
TG
232 .dst_prefixlen = dst_prefixlen,
233 .tos = tos,
234 .priority = priority,
235 .table = table,
1b566071 236 };
1c8e710c
TG
237
238 existing = set_get(link->routes, &route);
239 if (existing) {
1b566071
LP
240 if (ret)
241 *ret = existing;
1c8e710c 242 return 1;
1c8e710c
TG
243 }
244
1b566071
LP
245 existing = set_get(link->routes_foreign, &route);
246 if (existing) {
247 if (ret)
248 *ret = existing;
249 return 0;
250 }
1c8e710c 251
1b566071 252 return -ENOENT;
1c8e710c
TG
253}
254
889b550f
LP
255static int route_add_internal(
256 Link *link,
257 Set **routes,
258 int family,
259 const union in_addr_union *dst,
260 unsigned char dst_prefixlen,
261 unsigned char tos,
262 uint32_t priority,
14d20d2b 263 uint32_t table,
889b550f
LP
264 Route **ret) {
265
8e766630 266 _cleanup_(route_freep) Route *route = NULL;
1c8e710c
TG
267 int r;
268
269 assert(link);
270 assert(routes);
271 assert(dst);
272
273 r = route_new(&route);
274 if (r < 0)
275 return r;
276
277 route->family = family;
278 route->dst = *dst;
279 route->dst_prefixlen = dst_prefixlen;
280 route->tos = tos;
281 route->priority = priority;
282 route->table = table;
283
284 r = set_ensure_allocated(routes, &route_hash_ops);
285 if (r < 0)
286 return r;
287
288 r = set_put(*routes, route);
289 if (r < 0)
290 return r;
291
292 route->link = link;
293
294 if (ret)
295 *ret = route;
296
297 route = NULL;
298
299 return 0;
300}
301
889b550f
LP
302int route_add_foreign(
303 Link *link,
304 int family,
305 const union in_addr_union *dst,
306 unsigned char dst_prefixlen,
307 unsigned char tos,
308 uint32_t priority,
14d20d2b 309 uint32_t table,
889b550f
LP
310 Route **ret) {
311
1c8e710c
TG
312 return route_add_internal(link, &link->routes_foreign, family, dst, dst_prefixlen, tos, priority, table, ret);
313}
314
27efb52b 315int route_add(Link *link,
1c8e710c 316 int family,
889b550f 317 const union in_addr_union *dst,
1c8e710c
TG
318 unsigned char dst_prefixlen,
319 unsigned char tos,
320 uint32_t priority,
14d20d2b 321 uint32_t table,
889b550f
LP
322 Route **ret) {
323
1c8e710c
TG
324 Route *route;
325 int r;
326
327 r = route_get(link, family, dst, dst_prefixlen, tos, priority, table, &route);
328 if (r == -ENOENT) {
329 /* Route does not exist, create a new one */
330 r = route_add_internal(link, &link->routes, family, dst, dst_prefixlen, tos, priority, table, &route);
331 if (r < 0)
332 return r;
333 } else if (r == 0) {
334 /* Take over a foreign route */
335 r = set_ensure_allocated(&link->routes, &route_hash_ops);
336 if (r < 0)
337 return r;
338
339 r = set_put(link->routes, route);
340 if (r < 0)
341 return r;
342
343 set_remove(link->routes_foreign, route);
344 } else if (r == 1) {
345 /* Route exists, do nothing */
346 ;
347 } else
348 return r;
349
856e309d
MC
350 if (ret)
351 *ret = route;
1c8e710c
TG
352
353 return 0;
354}
355
bbd15900 356void route_update(Route *route,
27efb52b
YW
357 const union in_addr_union *src,
358 unsigned char src_prefixlen,
359 const union in_addr_union *gw,
360 const union in_addr_union *prefsrc,
361 unsigned char scope,
362 unsigned char protocol,
363 unsigned char type) {
889b550f 364
1c8e710c 365 assert(route);
26db55f3 366 assert(src || src_prefixlen == 0);
1c8e710c 367
3c7911e8 368 route->src = src ? *src : IN_ADDR_NULL;
1c8e710c 369 route->src_prefixlen = src_prefixlen;
3c7911e8
YW
370 route->gw = gw ? *gw : IN_ADDR_NULL;
371 route->prefsrc = prefsrc ? *prefsrc : IN_ADDR_NULL;
1c8e710c
TG
372 route->scope = scope;
373 route->protocol = protocol;
983226f3 374 route->type = type;
1c8e710c
TG
375}
376
302a796f 377static int route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
4645ad47
YW
378 int r;
379
380 assert(m);
381 assert(link);
382 assert(link->ifname);
383
384 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
385 return 1;
386
387 r = sd_netlink_message_get_errno(m);
388 if (r < 0 && r != -ESRCH)
389 log_link_warning_errno(link, r, "Could not drop route: %m");
390
391 return 1;
392}
393
91b5f997 394int route_remove(Route *route, Link *link,
302a796f 395 link_netlink_message_handler_t callback) {
27efb52b 396
4afd3348 397 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
5c1d3fc9
UTL
398 int r;
399
400 assert(link);
401 assert(link->manager);
402 assert(link->manager->rtnl);
403 assert(link->ifindex > 0);
4c701096 404 assert(IN_SET(route->family, AF_INET, AF_INET6));
5c1d3fc9
UTL
405
406 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
28cc555d
DW
407 RTM_DELROUTE, route->family,
408 route->protocol);
f647962d 409 if (r < 0)
7750b796 410 return log_link_error_errno(link, r, "Could not create RTM_DELROUTE message: %m");
5c1d3fc9 411
d40b01e4 412 if (in_addr_is_null(route->family, &route->gw) == 0) {
43409486 413 r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->family, &route->gw);
f647962d 414 if (r < 0)
7750b796 415 return log_link_error_errno(link, r, "Could not append RTA_GATEWAY attribute: %m");
5c1d3fc9
UTL
416 }
417
418 if (route->dst_prefixlen) {
43409486 419 r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst);
f647962d 420 if (r < 0)
7750b796 421 return log_link_error_errno(link, r, "Could not append RTA_DST attribute: %m");
5c1d3fc9
UTL
422
423 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
f647962d 424 if (r < 0)
7750b796 425 return log_link_error_errno(link, r, "Could not set destination prefix length: %m");
5c1d3fc9
UTL
426 }
427
9e7e4408 428 if (route->src_prefixlen) {
43409486 429 r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src);
9e7e4408 430 if (r < 0)
7750b796 431 return log_link_error_errno(link, r, "Could not append RTA_SRC attribute: %m");
9e7e4408
TG
432
433 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
434 if (r < 0)
7750b796 435 return log_link_error_errno(link, r, "Could not set source prefix length: %m");
9e7e4408
TG
436 }
437
d40b01e4 438 if (in_addr_is_null(route->family, &route->prefsrc) == 0) {
43409486 439 r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc);
f647962d 440 if (r < 0)
7750b796 441 return log_link_error_errno(link, r, "Could not append RTA_PREFSRC attribute: %m");
46b0c76e
ERB
442 }
443
5c1d3fc9 444 r = sd_rtnl_message_route_set_scope(req, route->scope);
f647962d 445 if (r < 0)
7750b796 446 return log_link_error_errno(link, r, "Could not set scope: %m");
5c1d3fc9 447
86655331 448 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
f647962d 449 if (r < 0)
7750b796 450 return log_link_error_errno(link, r, "Could not append RTA_PRIORITY attribute: %m");
5c1d3fc9 451
2d53f310 452 if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW)) {
983226f3
SS
453 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
454 if (r < 0)
7750b796 455 return log_link_error_errno(link, r, "Could not append RTA_OIF attribute: %m");
983226f3 456 }
5c1d3fc9 457
302a796f
YW
458 r = netlink_call_async(link->manager->rtnl, NULL, req,
459 callback ?: route_remove_handler,
460 link_netlink_destroy_callback, link);
f647962d 461 if (r < 0)
7750b796 462 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
5c1d3fc9 463
563c69c6
TG
464 link_ref(link);
465
5c1d3fc9
UTL
466 return 0;
467}
468
f833694d
TG
469int route_expire_handler(sd_event_source *s, uint64_t usec, void *userdata) {
470 Route *route = userdata;
471 int r;
472
473 assert(route);
474
4645ad47 475 r = route_remove(route, route->link, NULL);
f833694d
TG
476 if (r < 0)
477 log_warning_errno(r, "Could not remove route: %m");
3bdccf69 478 else
fe7ca21a 479 route_free(route);
f833694d
TG
480
481 return 1;
482}
483
1b566071
LP
484int route_configure(
485 Route *route,
486 Link *link,
302a796f 487 link_netlink_message_handler_t callback) {
1b566071 488
4afd3348
LP
489 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
490 _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL;
f833694d 491 usec_t lifetime;
f579559b
TG
492 int r;
493
f579559b 494 assert(link);
f882c247
TG
495 assert(link->manager);
496 assert(link->manager->rtnl);
f579559b 497 assert(link->ifindex > 0);
4c701096 498 assert(IN_SET(route->family, AF_INET, AF_INET6));
bd1175bc 499 assert(callback);
f579559b 500
1b566071 501 if (route_get(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, NULL) <= 0 &&
47d2d30d 502 set_size(link->routes) >= routes_max())
7750b796
YW
503 return log_link_error_errno(link, SYNTHETIC_ERRNO(E2BIG),
504 "Too many routes are configured, refusing: %m");
1b566071 505
156ed65e
YW
506 if (DEBUG_LOGGING) {
507 _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL;
508
509 if (!in_addr_is_null(route->family, &route->dst)) {
510 (void) in_addr_to_string(route->family, &route->dst, &dst);
511 (void) asprintf(&dst_prefixlen, "/%u", route->dst_prefixlen);
512 }
513 if (!in_addr_is_null(route->family, &route->src))
514 (void) in_addr_to_string(route->family, &route->src, &src);
515 if (!in_addr_is_null(route->family, &route->gw))
516 (void) in_addr_to_string(route->family, &route->gw, &gw);
517 if (!in_addr_is_null(route->family, &route->prefsrc))
518 (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
519
520 log_link_debug(link, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s",
521 strna(dst), strempty(dst_prefixlen), strna(src), strna(gw), strna(prefsrc));
522 }
523
151b9b96 524 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
28cc555d
DW
525 RTM_NEWROUTE, route->family,
526 route->protocol);
f647962d 527 if (r < 0)
7750b796 528 return log_link_error_errno(link, r, "Could not create RTM_NEWROUTE message: %m");
f579559b 529
d40b01e4 530 if (in_addr_is_null(route->family, &route->gw) == 0) {
43409486 531 r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->family, &route->gw);
f647962d 532 if (r < 0)
7750b796 533 return log_link_error_errno(link, r, "Could not append RTA_GATEWAY attribute: %m");
c953b24c
SS
534
535 r = sd_rtnl_message_route_set_family(req, route->family);
536 if (r < 0)
7750b796 537 return log_link_error_errno(link, r, "Could not set route family: %m");
f579559b
TG
538 }
539
0a0dc69b 540 if (route->dst_prefixlen) {
43409486 541 r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst);
f647962d 542 if (r < 0)
7750b796 543 return log_link_error_errno(link, r, "Could not append RTA_DST attribute: %m");
6ae115c1 544
ae4c67a7 545 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
f647962d 546 if (r < 0)
7750b796 547 return log_link_error_errno(link, r, "Could not set destination prefix length: %m");
1f01fb4f
TG
548 }
549
9e7e4408 550 if (route->src_prefixlen) {
43409486 551 r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src);
9e7e4408 552 if (r < 0)
7750b796 553 return log_link_error_errno(link, r, "Could not append RTA_SRC attribute: %m");
9e7e4408
TG
554
555 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
556 if (r < 0)
7750b796 557 return log_link_error_errno(link, r, "Could not set source prefix length: %m");
9e7e4408
TG
558 }
559
d40b01e4 560 if (in_addr_is_null(route->family, &route->prefsrc) == 0) {
43409486 561 r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc);
f647962d 562 if (r < 0)
7750b796 563 return log_link_error_errno(link, r, "Could not append RTA_PREFSRC attribute: %m");
46b0c76e
ERB
564 }
565
5c1d3fc9 566 r = sd_rtnl_message_route_set_scope(req, route->scope);
f647962d 567 if (r < 0)
7750b796 568 return log_link_error_errno(link, r, "Could not set scope: %m");
5c1d3fc9 569
54901fd2
YW
570 if (route->gateway_onlink >= 0)
571 SET_FLAG(route->flags, RTNH_F_ONLINK, route->gateway_onlink);
572
3b015d40
TG
573 r = sd_rtnl_message_route_set_flags(req, route->flags);
574 if (r < 0)
7750b796 575 return log_link_error_errno(link, r, "Could not set flags: %m");
c953b24c 576
a0d95bbc 577 if (route->table != RT_TABLE_MAIN) {
c953b24c
SS
578 if (route->table < 256) {
579 r = sd_rtnl_message_route_set_table(req, route->table);
580 if (r < 0)
7750b796 581 return log_link_error_errno(link, r, "Could not set route table: %m");
c953b24c 582 } else {
c953b24c
SS
583 r = sd_rtnl_message_route_set_table(req, RT_TABLE_UNSPEC);
584 if (r < 0)
7750b796 585 return log_link_error_errno(link, r, "Could not set route table: %m");
c953b24c 586
06976f5b 587 /* Table attribute to allow more than 256. */
c953b24c
SS
588 r = sd_netlink_message_append_data(req, RTA_TABLE, &route->table, sizeof(route->table));
589 if (r < 0)
7750b796 590 return log_link_error_errno(link, r, "Could not append RTA_TABLE attribute: %m");
c953b24c
SS
591 }
592 }
3b015d40 593
86655331 594 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
f647962d 595 if (r < 0)
7750b796 596 return log_link_error_errno(link, r, "Could not append RTA_PRIORITY attribute: %m");
5c1d3fc9 597
3b015d40
TG
598 r = sd_netlink_message_append_u8(req, RTA_PREF, route->pref);
599 if (r < 0)
7750b796 600 return log_link_error_errno(link, r, "Could not append RTA_PREF attribute: %m");
3b015d40 601
f02ba163
DD
602 if (route->lifetime != USEC_INFINITY && kernel_route_expiration_supported()) {
603 r = sd_netlink_message_append_u32(req, RTA_EXPIRES,
604 DIV_ROUND_UP(usec_sub_unsigned(route->lifetime, now(clock_boottime_or_monotonic())), USEC_PER_SEC));
605 if (r < 0)
7750b796 606 return log_link_error_errno(link, r, "Could not append RTA_EXPIRES attribute: %m");
f02ba163
DD
607 }
608
983226f3 609 r = sd_rtnl_message_route_set_type(req, route->type);
f647962d 610 if (r < 0)
7750b796 611 return log_link_error_errno(link, r, "Could not set route type: %m");
983226f3 612
2d53f310 613 if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW)) {
983226f3
SS
614 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
615 if (r < 0)
7750b796 616 return log_link_error_errno(link, r, "Could not append RTA_OIF attribute: %m");
983226f3 617 }
f579559b 618
d6fceaf1
SS
619 r = sd_netlink_message_open_container(req, RTA_METRICS);
620 if (r < 0)
7750b796 621 return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m");
d6fceaf1
SS
622
623 if (route->mtu > 0) {
624 r = sd_netlink_message_append_u32(req, RTAX_MTU, route->mtu);
625 if (r < 0)
7750b796 626 return log_link_error_errno(link, r, "Could not append RTAX_MTU attribute: %m");
d6fceaf1
SS
627 }
628
6b21ad33 629 if (route->initcwnd > 0) {
323d9329
SS
630 r = sd_netlink_message_append_u32(req, RTAX_INITCWND, route->initcwnd);
631 if (r < 0)
7750b796 632 return log_link_error_errno(link, r, "Could not append RTAX_INITCWND attribute: %m");
323d9329
SS
633 }
634
6b21ad33 635 if (route->initrwnd > 0) {
323d9329
SS
636 r = sd_netlink_message_append_u32(req, RTAX_INITRWND, route->initrwnd);
637 if (r < 0)
7750b796 638 return log_link_error_errno(link, r, "Could not append RTAX_INITRWND attribute: %m");
323d9329
SS
639 }
640
09f5dfad
SS
641 if (route->quickack != -1) {
642 r = sd_netlink_message_append_u32(req, RTAX_QUICKACK, route->quickack);
643 if (r < 0)
7750b796 644 return log_link_error_errno(link, r, "Could not append RTAX_QUICKACK attribute: %m");
09f5dfad
SS
645 }
646
d6fceaf1
SS
647 r = sd_netlink_message_close_container(req);
648 if (r < 0)
7750b796 649 return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m");
d6fceaf1 650
302a796f
YW
651 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
652 link_netlink_destroy_callback, link);
f647962d 653 if (r < 0)
7750b796 654 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
f579559b 655
563c69c6
TG
656 link_ref(link);
657
f833694d
TG
658 lifetime = route->lifetime;
659
660 r = route_add(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, &route);
1c8e710c 661 if (r < 0)
7750b796 662 return log_link_error_errno(link, r, "Could not add route: %m");
1c8e710c 663
f833694d
TG
664 /* TODO: drop expiration handling once it can be pushed into the kernel */
665 route->lifetime = lifetime;
666
f02ba163 667 if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) {
f833694d
TG
668 r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(),
669 route->lifetime, 0, route_expire_handler, route);
670 if (r < 0)
7750b796 671 return log_link_error_errno(link, r, "Could not arm expiration timer: %m");
f833694d
TG
672 }
673
674 sd_event_source_unref(route->expire);
1cc6c93a 675 route->expire = TAKE_PTR(expire);
f833694d 676
f579559b
TG
677 return 0;
678}
679
fa7cd711 680int network_add_ipv4ll_route(Network *network) {
fcbf4cb7 681 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
fa7cd711
YW
682 int r;
683
684 assert(network);
685
686 if (!network->ipv4ll_route)
687 return 0;
688
689 /* IPv4LLRoute= is in [Network] section. */
690 r = route_new_static(network, NULL, 0, &n);
691 if (r < 0)
692 return r;
693
694 r = in_addr_from_string(AF_INET, "169.254.0.0", &n->dst);
695 if (r < 0)
696 return r;
697
698 n->family = AF_INET;
699 n->dst_prefixlen = 16;
700 n->scope = RT_SCOPE_LINK;
701 n->priority = IPV4LL_ROUTE_METRIC;
702 n->protocol = RTPROT_STATIC;
703
704 TAKE_PTR(n);
705 return 0;
706}
707
27efb52b
YW
708int config_parse_gateway(
709 const char *unit,
f579559b
TG
710 const char *filename,
711 unsigned line,
712 const char *section,
71a61510 713 unsigned section_line,
f579559b
TG
714 const char *lvalue,
715 int ltype,
716 const char *rvalue,
717 void *data,
718 void *userdata) {
44e7b949 719
6ae115c1 720 Network *network = userdata;
fcbf4cb7 721 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
7934dede 722 int r;
f579559b
TG
723
724 assert(filename);
6ae115c1 725 assert(section);
f579559b
TG
726 assert(lvalue);
727 assert(rvalue);
728 assert(data);
729
92fe133a
TG
730 if (streq(section, "Network")) {
731 /* we are not in an Route section, so treat
732 * this as the special '0' section */
f4859fc7
SS
733 r = route_new_static(network, NULL, 0, &n);
734 } else
735 r = route_new_static(network, filename, section_line, &n);
f579559b
TG
736 if (r < 0)
737 return r;
738
01d4e732
YW
739 if (n->family == AF_UNSPEC)
740 r = in_addr_from_string_auto(rvalue, &n->family, &n->gw);
741 else
742 r = in_addr_from_string(n->family, rvalue, &n->gw);
f579559b 743 if (r < 0) {
01d4e732
YW
744 log_syntax(unit, LOG_ERR, filename, line, r,
745 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
f579559b
TG
746 return 0;
747 }
748
aff44301 749 TAKE_PTR(n);
f579559b
TG
750 return 0;
751}
6ae115c1 752
27efb52b
YW
753int config_parse_preferred_src(
754 const char *unit,
0d07e595
JK
755 const char *filename,
756 unsigned line,
757 const char *section,
758 unsigned section_line,
759 const char *lvalue,
760 int ltype,
761 const char *rvalue,
762 void *data,
763 void *userdata) {
764
765 Network *network = userdata;
fcbf4cb7 766 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
7934dede 767 int r;
0d07e595
JK
768
769 assert(filename);
770 assert(section);
771 assert(lvalue);
772 assert(rvalue);
773 assert(data);
774
f4859fc7 775 r = route_new_static(network, filename, section_line, &n);
0d07e595
JK
776 if (r < 0)
777 return r;
778
01d4e732
YW
779 if (n->family == AF_UNSPEC)
780 r = in_addr_from_string_auto(rvalue, &n->family, &n->prefsrc);
781 else
782 r = in_addr_from_string(n->family, rvalue, &n->prefsrc);
0d07e595
JK
783 if (r < 0) {
784 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
01d4e732 785 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
0d07e595
JK
786 return 0;
787 }
788
aff44301 789 TAKE_PTR(n);
0d07e595
JK
790 return 0;
791}
792
27efb52b
YW
793int config_parse_destination(
794 const char *unit,
6ae115c1
TG
795 const char *filename,
796 unsigned line,
797 const char *section,
798 unsigned section_line,
799 const char *lvalue,
800 int ltype,
801 const char *rvalue,
802 void *data,
803 void *userdata) {
44e7b949 804
6ae115c1 805 Network *network = userdata;
fcbf4cb7 806 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
7934dede
YW
807 union in_addr_union *buffer;
808 unsigned char *prefixlen;
ca3bad65 809 int r;
6ae115c1
TG
810
811 assert(filename);
812 assert(section);
813 assert(lvalue);
814 assert(rvalue);
815 assert(data);
816
f4859fc7 817 r = route_new_static(network, filename, section_line, &n);
6ae115c1
TG
818 if (r < 0)
819 return r;
820
9e7e4408 821 if (streq(lvalue, "Destination")) {
7934dede
YW
822 buffer = &n->dst;
823 prefixlen = &n->dst_prefixlen;
9e7e4408 824 } else if (streq(lvalue, "Source")) {
7934dede
YW
825 buffer = &n->src;
826 prefixlen = &n->src_prefixlen;
9e7e4408
TG
827 } else
828 assert_not_reached(lvalue);
829
01d4e732
YW
830 if (n->family == AF_UNSPEC)
831 r = in_addr_prefix_from_string_auto(rvalue, &n->family, buffer, prefixlen);
832 else
833 r = in_addr_prefix_from_string(rvalue, n->family, buffer, prefixlen);
7934dede 834 if (r < 0) {
01d4e732
YW
835 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
836 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
7934dede
YW
837 return 0;
838 }
839
aff44301 840 TAKE_PTR(n);
6ae115c1
TG
841 return 0;
842}
5d8e593d 843
27efb52b
YW
844int config_parse_route_priority(
845 const char *unit,
846 const char *filename,
847 unsigned line,
848 const char *section,
849 unsigned section_line,
850 const char *lvalue,
851 int ltype,
852 const char *rvalue,
853 void *data,
854 void *userdata) {
855
5d8e593d 856 Network *network = userdata;
fcbf4cb7 857 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
5d8e593d
SS
858 int r;
859
860 assert(filename);
861 assert(section);
862 assert(lvalue);
863 assert(rvalue);
864 assert(data);
865
f4859fc7 866 r = route_new_static(network, filename, section_line, &n);
5d8e593d
SS
867 if (r < 0)
868 return r;
869
aff44301 870 r = safe_atou32(rvalue, &n->priority);
1c4b1179
SS
871 if (r < 0) {
872 log_syntax(unit, LOG_ERR, filename, line, r,
873 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue);
874 return 0;
875 }
5d8e593d 876
aff44301 877 TAKE_PTR(n);
5d8e593d
SS
878 return 0;
879}
769b56a3 880
27efb52b
YW
881int config_parse_route_scope(
882 const char *unit,
883 const char *filename,
884 unsigned line,
885 const char *section,
886 unsigned section_line,
887 const char *lvalue,
888 int ltype,
889 const char *rvalue,
890 void *data,
891 void *userdata) {
892
769b56a3 893 Network *network = userdata;
fcbf4cb7 894 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
769b56a3
TG
895 int r;
896
897 assert(filename);
898 assert(section);
899 assert(lvalue);
900 assert(rvalue);
901 assert(data);
902
f4859fc7 903 r = route_new_static(network, filename, section_line, &n);
769b56a3
TG
904 if (r < 0)
905 return r;
906
907 if (streq(rvalue, "host"))
908 n->scope = RT_SCOPE_HOST;
909 else if (streq(rvalue, "link"))
910 n->scope = RT_SCOPE_LINK;
911 else if (streq(rvalue, "global"))
912 n->scope = RT_SCOPE_UNIVERSE;
913 else {
12ca818f 914 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route scope: %s", rvalue);
769b56a3
TG
915 return 0;
916 }
917
aff44301 918 TAKE_PTR(n);
769b56a3
TG
919 return 0;
920}
c953b24c 921
27efb52b
YW
922int config_parse_route_table(
923 const char *unit,
924 const char *filename,
925 unsigned line,
926 const char *section,
927 unsigned section_line,
928 const char *lvalue,
929 int ltype,
930 const char *rvalue,
931 void *data,
932 void *userdata) {
933
fcbf4cb7 934 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
c953b24c 935 Network *network = userdata;
c953b24c
SS
936 int r;
937
938 assert(filename);
939 assert(section);
940 assert(lvalue);
941 assert(rvalue);
942 assert(data);
943
f4859fc7 944 r = route_new_static(network, filename, section_line, &n);
c953b24c
SS
945 if (r < 0)
946 return r;
947
aff44301 948 r = safe_atou32(rvalue, &n->table);
c953b24c
SS
949 if (r < 0) {
950 log_syntax(unit, LOG_ERR, filename, line, r,
951 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue);
952 return 0;
953 }
954
aff44301 955 TAKE_PTR(n);
c953b24c
SS
956 return 0;
957}
28959f7d 958
27efb52b
YW
959int config_parse_gateway_onlink(
960 const char *unit,
961 const char *filename,
962 unsigned line,
963 const char *section,
964 unsigned section_line,
965 const char *lvalue,
966 int ltype,
967 const char *rvalue,
968 void *data,
969 void *userdata) {
970
28959f7d 971 Network *network = userdata;
fcbf4cb7 972 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
28959f7d
SS
973 int r;
974
975 assert(filename);
976 assert(section);
977 assert(lvalue);
978 assert(rvalue);
979 assert(data);
980
981 r = route_new_static(network, filename, section_line, &n);
982 if (r < 0)
983 return r;
984
985 r = parse_boolean(rvalue);
986 if (r < 0) {
987 log_syntax(unit, LOG_ERR, filename, line, r,
9cb8c559 988 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue, rvalue);
28959f7d
SS
989 return 0;
990 }
991
54901fd2
YW
992 n->gateway_onlink = r;
993
aff44301 994 TAKE_PTR(n);
b5bf6f64
SS
995 return 0;
996}
997
27efb52b
YW
998int config_parse_ipv6_route_preference(
999 const char *unit,
1000 const char *filename,
1001 unsigned line,
1002 const char *section,
1003 unsigned section_line,
1004 const char *lvalue,
1005 int ltype,
1006 const char *rvalue,
1007 void *data,
1008 void *userdata) {
1009
b5bf6f64 1010 Network *network = userdata;
fcbf4cb7 1011 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
b5bf6f64
SS
1012 int r;
1013
4c7bd9cf
SS
1014 r = route_new_static(network, filename, section_line, &n);
1015 if (r < 0)
1016 return r;
1017
b5bf6f64
SS
1018 if (streq(rvalue, "low"))
1019 n->pref = ICMPV6_ROUTER_PREF_LOW;
1020 else if (streq(rvalue, "medium"))
1021 n->pref = ICMPV6_ROUTER_PREF_MEDIUM;
1022 else if (streq(rvalue, "high"))
1023 n->pref = ICMPV6_ROUTER_PREF_HIGH;
1024 else {
1025 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route preference: %s", rvalue);
1026 return 0;
1027 }
28959f7d 1028
aff44301 1029 TAKE_PTR(n);
28959f7d
SS
1030 return 0;
1031}
c83ecc04 1032
27efb52b
YW
1033int config_parse_route_protocol(
1034 const char *unit,
1035 const char *filename,
1036 unsigned line,
1037 const char *section,
1038 unsigned section_line,
1039 const char *lvalue,
1040 int ltype,
1041 const char *rvalue,
1042 void *data,
1043 void *userdata) {
1044
c83ecc04 1045 Network *network = userdata;
fcbf4cb7 1046 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
c83ecc04
SS
1047 int r;
1048
1049 r = route_new_static(network, filename, section_line, &n);
1050 if (r < 0)
1051 return r;
1052
1053 if (streq(rvalue, "kernel"))
1054 n->protocol = RTPROT_KERNEL;
1055 else if (streq(rvalue, "boot"))
1056 n->protocol = RTPROT_BOOT;
1057 else if (streq(rvalue, "static"))
1058 n->protocol = RTPROT_STATIC;
1059 else {
1060 r = safe_atou8(rvalue , &n->protocol);
1061 if (r < 0) {
f205a92a
YW
1062 log_syntax(unit, LOG_ERR, filename, line, r,
1063 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue);
c83ecc04
SS
1064 return 0;
1065 }
1066 }
1067
aff44301 1068 TAKE_PTR(n);
c83ecc04
SS
1069 return 0;
1070}
983226f3 1071
27efb52b
YW
1072int config_parse_route_type(
1073 const char *unit,
1074 const char *filename,
1075 unsigned line,
1076 const char *section,
1077 unsigned section_line,
1078 const char *lvalue,
1079 int ltype,
1080 const char *rvalue,
1081 void *data,
1082 void *userdata) {
1083
983226f3 1084 Network *network = userdata;
fcbf4cb7 1085 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
983226f3
SS
1086 int r;
1087
1088 r = route_new_static(network, filename, section_line, &n);
1089 if (r < 0)
1090 return r;
1091
1092 if (streq(rvalue, "unicast"))
1093 n->type = RTN_UNICAST;
1094 else if (streq(rvalue, "blackhole"))
1095 n->type = RTN_BLACKHOLE;
1096 else if (streq(rvalue, "unreachable"))
1097 n->type = RTN_UNREACHABLE;
1098 else if (streq(rvalue, "prohibit"))
1099 n->type = RTN_PROHIBIT;
2d53f310
HY
1100 else if (streq(rvalue, "throw"))
1101 n->type = RTN_THROW;
983226f3 1102 else {
f205a92a
YW
1103 log_syntax(unit, LOG_ERR, filename, line, r,
1104 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue);
983226f3
SS
1105 return 0;
1106 }
1107
aff44301 1108 TAKE_PTR(n);
983226f3
SS
1109 return 0;
1110}
323d9329 1111
27efb52b
YW
1112int config_parse_tcp_window(
1113 const char *unit,
1114 const char *filename,
1115 unsigned line,
1116 const char *section,
1117 unsigned section_line,
1118 const char *lvalue,
1119 int ltype,
1120 const char *rvalue,
1121 void *data,
1122 void *userdata) {
1123
fcbf4cb7 1124 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
6b21ad33
SS
1125 Network *network = userdata;
1126 uint64_t k;
323d9329
SS
1127 int r;
1128
1129 assert(filename);
1130 assert(section);
1131 assert(lvalue);
1132 assert(rvalue);
1133 assert(data);
1134
1135 r = route_new_static(network, filename, section_line, &n);
1136 if (r < 0)
1137 return r;
1138
6b21ad33 1139 r = parse_size(rvalue, 1024, &k);
f205a92a 1140 if (r < 0) {
323d9329 1141 log_syntax(unit, LOG_ERR, filename, line, r,
f205a92a
YW
1142 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue, rvalue);
1143 return 0;
1144 }
1145 if (k > UINT32_MAX) {
1146 log_syntax(unit, LOG_ERR, filename, line, 0,
1147 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue, rvalue);
323d9329
SS
1148 return 0;
1149 }
1150
1151 if (streq(lvalue, "InitialCongestionWindow"))
1152 n->initcwnd = k;
1153 else if (streq(lvalue, "InitialAdvertisedReceiveWindow"))
1154 n->initrwnd = k;
f205a92a
YW
1155 else
1156 assert_not_reached("Invalid TCP window type.");
323d9329 1157
aff44301 1158 TAKE_PTR(n);
323d9329
SS
1159 return 0;
1160}
09f5dfad 1161
27efb52b
YW
1162int config_parse_quickack(
1163 const char *unit,
1164 const char *filename,
1165 unsigned line,
1166 const char *section,
1167 unsigned section_line,
1168 const char *lvalue,
1169 int ltype,
1170 const char *rvalue,
1171 void *data,
1172 void *userdata) {
1173
fcbf4cb7 1174 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
09f5dfad
SS
1175 Network *network = userdata;
1176 int k, r;
1177
1178 assert(filename);
1179 assert(section);
1180 assert(lvalue);
1181 assert(rvalue);
1182 assert(data);
1183
1184 r = route_new_static(network, filename, section_line, &n);
1185 if (r < 0)
1186 return r;
1187
1188 k = parse_boolean(rvalue);
1189 if (k < 0) {
f205a92a
YW
1190 log_syntax(unit, LOG_ERR, filename, line, k,
1191 "Failed to parse TCP quickack, ignoring: %s", rvalue);
09f5dfad
SS
1192 return 0;
1193 }
1194
1195 n->quickack = !!k;
aff44301 1196 TAKE_PTR(n);
09f5dfad
SS
1197 return 0;
1198}
cea79e66
SS
1199
1200int config_parse_route_mtu(
1201 const char *unit,
1202 const char *filename,
1203 unsigned line,
1204 const char *section,
1205 unsigned section_line,
1206 const char *lvalue,
1207 int ltype,
1208 const char *rvalue,
1209 void *data,
1210 void *userdata) {
1211
1212 Network *network = userdata;
fcbf4cb7 1213 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
cea79e66
SS
1214 int r;
1215
1216 assert(filename);
1217 assert(section);
1218 assert(lvalue);
1219 assert(rvalue);
1220 assert(data);
1221
1222 r = route_new_static(network, filename, section_line, &n);
1223 if (r < 0)
1224 return r;
1225
1226 r = config_parse_mtu(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &n->mtu, userdata);
1227 if (r < 0)
1228 return r;
1229
aff44301 1230 TAKE_PTR(n);
cea79e66
SS
1231 return 0;
1232}
fcbf4cb7
YW
1233
1234int route_section_verify(Route *route, Network *network) {
1235 if (section_is_invalid(route->section))
1236 return -EINVAL;
1237
1238 if (route->family == AF_UNSPEC) {
1239 assert(route->section);
1240
1241 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1242 "%s: Route section without Gateway=, Destination=, Source=, "
1243 "or PreferredSource= field configured. "
1244 "Ignoring [Route] section from line %u.",
1245 route->section->filename, route->section->line);
1246 }
1247
1248 if (network->n_static_addresses == 0 &&
1249 in_addr_is_null(route->family, &route->gw) == 0 &&
1250 route->gateway_onlink < 0) {
1251 log_warning("%s: Gateway= without static address configured. "
1252 "Enabling GatewayOnLink= option.",
1253 network->filename);
1254 route->gateway_onlink = true;
1255 }
1256
1257 return 0;
1258}