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