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