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