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