]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-route.c
Merge pull request #9167 from keszybz/ellipsization
[thirdparty/systemd.git] / src / network / networkd-route.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 ***/
7
8 #include <linux/icmpv6.h>
9
10 #include "alloc-util.h"
11 #include "conf-parser.h"
12 #include "in-addr-util.h"
13 #include "netlink-util.h"
14 #include "networkd-manager.h"
15 #include "networkd-route.h"
16 #include "parse-util.h"
17 #include "set.h"
18 #include "string-util.h"
19 #include "sysctl-util.h"
20 #include "util.h"
21
22 #define ROUTES_DEFAULT_MAX_PER_FAMILY 4096U
23
24 static unsigned routes_max(void) {
25 static thread_local unsigned cached = 0;
26
27 _cleanup_free_ char *s4 = NULL, *s6 = NULL;
28 unsigned val4 = ROUTES_DEFAULT_MAX_PER_FAMILY, val6 = ROUTES_DEFAULT_MAX_PER_FAMILY;
29
30 if (cached > 0)
31 return cached;
32
33 if (sysctl_read("net/ipv4/route/max_size", &s4) >= 0) {
34 truncate_nl(s4);
35 if (safe_atou(s4, &val4) >= 0 &&
36 val4 == 2147483647U)
37 /* This is the default "no limit" value in the kernel */
38 val4 = ROUTES_DEFAULT_MAX_PER_FAMILY;
39 }
40
41 if (sysctl_read("net/ipv6/route/max_size", &s6) >= 0) {
42 truncate_nl(s6);
43 (void) safe_atou(s6, &val6);
44 }
45
46 cached = MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val4) +
47 MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val6);
48 return cached;
49 }
50
51 int route_new(Route **ret) {
52 _cleanup_(route_freep) Route *route = NULL;
53
54 route = new0(Route, 1);
55 if (!route)
56 return -ENOMEM;
57
58 route->family = AF_UNSPEC;
59 route->scope = RT_SCOPE_UNIVERSE;
60 route->protocol = RTPROT_UNSPEC;
61 route->type = RTN_UNICAST;
62 route->table = RT_TABLE_MAIN;
63 route->lifetime = USEC_INFINITY;
64 route->quickack = -1;
65
66 *ret = TAKE_PTR(route);
67
68 return 0;
69 }
70
71 int route_new_static(Network *network, const char *filename, unsigned section_line, Route **ret) {
72 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
73 _cleanup_(route_freep) Route *route = NULL;
74 int r;
75
76 assert(network);
77 assert(ret);
78 assert(!!filename == (section_line > 0));
79
80 if (filename) {
81 r = network_config_section_new(filename, section_line, &n);
82 if (r < 0)
83 return r;
84
85 route = hashmap_get(network->routes_by_section, n);
86 if (route) {
87 *ret = TAKE_PTR(route);
88
89 return 0;
90 }
91 }
92
93 if (network->n_static_routes >= routes_max())
94 return -E2BIG;
95
96 r = route_new(&route);
97 if (r < 0)
98 return r;
99
100 route->protocol = RTPROT_STATIC;
101
102 if (filename) {
103 route->section = TAKE_PTR(n);
104
105 r = hashmap_put(network->routes_by_section, route->section, route);
106 if (r < 0)
107 return r;
108 }
109
110 route->network = network;
111 LIST_PREPEND(routes, network->static_routes, route);
112 network->n_static_routes++;
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
173 if (a->family < b->family)
174 return -1;
175 if (a->family > b->family)
176 return 1;
177
178 switch (a->family) {
179 case AF_INET:
180 case AF_INET6:
181 if (a->dst_prefixlen < b->dst_prefixlen)
182 return -1;
183 if (a->dst_prefixlen > b->dst_prefixlen)
184 return 1;
185
186 if (a->tos < b->tos)
187 return -1;
188 if (a->tos > b->tos)
189 return 1;
190
191 if (a->priority < b->priority)
192 return -1;
193 if (a->priority > b->priority)
194 return 1;
195
196 if (a->table < b->table)
197 return -1;
198 if (a->table > b->table)
199 return 1;
200
201 return memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
202 default:
203 /* treat any other address family as AF_UNSPEC */
204 return 0;
205 }
206 }
207
208 static const struct hash_ops route_hash_ops = {
209 .hash = route_hash_func,
210 .compare = route_compare_func
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 : (union in_addr_union) {};
367 route->src_prefixlen = src_prefixlen;
368 route->gw = gw ? *gw : (union in_addr_union) {};
369 route->prefsrc = prefsrc ? *prefsrc : (union in_addr_union) {};
370 route->scope = scope;
371 route->protocol = protocol;
372 route->type = type;
373 }
374
375 int route_remove(Route *route, Link *link,
376 sd_netlink_message_handler_t callback) {
377
378 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
379 int r;
380
381 assert(link);
382 assert(link->manager);
383 assert(link->manager->rtnl);
384 assert(link->ifindex > 0);
385 assert(IN_SET(route->family, AF_INET, AF_INET6));
386
387 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
388 RTM_DELROUTE, route->family,
389 route->protocol);
390 if (r < 0)
391 return log_error_errno(r, "Could not create RTM_DELROUTE message: %m");
392
393 if (!in_addr_is_null(route->family, &route->gw)) {
394 if (route->family == AF_INET)
395 r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &route->gw.in);
396 else if (route->family == AF_INET6)
397 r = sd_netlink_message_append_in6_addr(req, RTA_GATEWAY, &route->gw.in6);
398 if (r < 0)
399 return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
400 }
401
402 if (route->dst_prefixlen) {
403 if (route->family == AF_INET)
404 r = sd_netlink_message_append_in_addr(req, RTA_DST, &route->dst.in);
405 else if (route->family == AF_INET6)
406 r = sd_netlink_message_append_in6_addr(req, RTA_DST, &route->dst.in6);
407 if (r < 0)
408 return log_error_errno(r, "Could not append RTA_DST attribute: %m");
409
410 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
411 if (r < 0)
412 return log_error_errno(r, "Could not set destination prefix length: %m");
413 }
414
415 if (route->src_prefixlen) {
416 if (route->family == AF_INET)
417 r = sd_netlink_message_append_in_addr(req, RTA_SRC, &route->src.in);
418 else if (route->family == AF_INET6)
419 r = sd_netlink_message_append_in6_addr(req, RTA_SRC, &route->src.in6);
420 if (r < 0)
421 return log_error_errno(r, "Could not append RTA_SRC attribute: %m");
422
423 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
424 if (r < 0)
425 return log_error_errno(r, "Could not set source prefix length: %m");
426 }
427
428 if (!in_addr_is_null(route->family, &route->prefsrc)) {
429 if (route->family == AF_INET)
430 r = sd_netlink_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc.in);
431 else if (route->family == AF_INET6)
432 r = sd_netlink_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc.in6);
433 if (r < 0)
434 return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m");
435 }
436
437 r = sd_rtnl_message_route_set_scope(req, route->scope);
438 if (r < 0)
439 return log_error_errno(r, "Could not set scope: %m");
440
441 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
442 if (r < 0)
443 return log_error_errno(r, "Could not append RTA_PRIORITY attribute: %m");
444
445 if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE)) {
446 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
447 if (r < 0)
448 return log_error_errno(r, "Could not append RTA_OIF attribute: %m");
449 }
450
451 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
452 if (r < 0)
453 return log_error_errno(r, "Could not send rtnetlink message: %m");
454
455 link_ref(link);
456
457 return 0;
458 }
459
460 static int route_expire_callback(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
461 Link *link = userdata;
462 int r;
463
464 assert(rtnl);
465 assert(m);
466 assert(link);
467 assert(link->ifname);
468
469 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
470 return 1;
471
472 r = sd_netlink_message_get_errno(m);
473 if (r < 0 && r != -EEXIST)
474 log_link_warning_errno(link, r, "could not remove route: %m");
475
476 return 1;
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, route_expire_callback);
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 sd_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
510 if (route_get(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, NULL) <= 0 &&
511 set_size(link->routes) >= routes_max())
512 return -E2BIG;
513
514 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
515 RTM_NEWROUTE, route->family,
516 route->protocol);
517 if (r < 0)
518 return log_error_errno(r, "Could not create RTM_NEWROUTE message: %m");
519
520 if (!in_addr_is_null(route->family, &route->gw)) {
521 if (route->family == AF_INET)
522 r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &route->gw.in);
523 else if (route->family == AF_INET6)
524 r = sd_netlink_message_append_in6_addr(req, RTA_GATEWAY, &route->gw.in6);
525 if (r < 0)
526 return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
527
528 r = sd_rtnl_message_route_set_family(req, route->family);
529 if (r < 0)
530 return log_error_errno(r, "Could not set route family: %m");
531 }
532
533 if (route->dst_prefixlen) {
534 if (route->family == AF_INET)
535 r = sd_netlink_message_append_in_addr(req, RTA_DST, &route->dst.in);
536 else if (route->family == AF_INET6)
537 r = sd_netlink_message_append_in6_addr(req, RTA_DST, &route->dst.in6);
538 if (r < 0)
539 return log_error_errno(r, "Could not append RTA_DST attribute: %m");
540
541 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
542 if (r < 0)
543 return log_error_errno(r, "Could not set destination prefix length: %m");
544 }
545
546 if (route->src_prefixlen) {
547 if (route->family == AF_INET)
548 r = sd_netlink_message_append_in_addr(req, RTA_SRC, &route->src.in);
549 else if (route->family == AF_INET6)
550 r = sd_netlink_message_append_in6_addr(req, RTA_SRC, &route->src.in6);
551 if (r < 0)
552 return log_error_errno(r, "Could not append RTA_SRC attribute: %m");
553
554 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
555 if (r < 0)
556 return log_error_errno(r, "Could not set source prefix length: %m");
557 }
558
559 if (!in_addr_is_null(route->family, &route->prefsrc)) {
560 if (route->family == AF_INET)
561 r = sd_netlink_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc.in);
562 else if (route->family == AF_INET6)
563 r = sd_netlink_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc.in6);
564 if (r < 0)
565 return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m");
566 }
567
568 r = sd_rtnl_message_route_set_scope(req, route->scope);
569 if (r < 0)
570 return log_error_errno(r, "Could not set scope: %m");
571
572 r = sd_rtnl_message_route_set_flags(req, route->flags);
573 if (r < 0)
574 return log_error_errno(r, "Could not set flags: %m");
575
576 if (route->table != RT_TABLE_MAIN) {
577 if (route->table < 256) {
578 r = sd_rtnl_message_route_set_table(req, route->table);
579 if (r < 0)
580 return log_error_errno(r, "Could not set route table: %m");
581 } else {
582 r = sd_rtnl_message_route_set_table(req, RT_TABLE_UNSPEC);
583 if (r < 0)
584 return log_error_errno(r, "Could not set route table: %m");
585
586 /* Table attribute to allow more than 256. */
587 r = sd_netlink_message_append_data(req, RTA_TABLE, &route->table, sizeof(route->table));
588 if (r < 0)
589 return log_error_errno(r, "Could not append RTA_TABLE attribute: %m");
590 }
591 }
592
593 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
594 if (r < 0)
595 return log_error_errno(r, "Could not append RTA_PRIORITY attribute: %m");
596
597 r = sd_netlink_message_append_u8(req, RTA_PREF, route->pref);
598 if (r < 0)
599 return log_error_errno(r, "Could not append RTA_PREF attribute: %m");
600
601 if (route->lifetime != USEC_INFINITY && kernel_route_expiration_supported()) {
602 r = sd_netlink_message_append_u32(req, RTA_EXPIRES,
603 DIV_ROUND_UP(usec_sub_unsigned(route->lifetime, now(clock_boottime_or_monotonic())), USEC_PER_SEC));
604 if (r < 0)
605 return log_error_errno(r, "Could not append RTA_EXPIRES attribute: %m");
606 }
607
608 r = sd_rtnl_message_route_set_type(req, route->type);
609 if (r < 0)
610 return log_error_errno(r, "Could not set route type: %m");
611
612 if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE)) {
613 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
614 if (r < 0)
615 return log_error_errno(r, "Could not append RTA_OIF attribute: %m");
616 }
617
618 r = sd_netlink_message_open_container(req, RTA_METRICS);
619 if (r < 0)
620 return log_error_errno(r, "Could not append RTA_METRICS attribute: %m");
621
622 if (route->mtu > 0) {
623 r = sd_netlink_message_append_u32(req, RTAX_MTU, route->mtu);
624 if (r < 0)
625 return log_error_errno(r, "Could not append RTAX_MTU attribute: %m");
626 }
627
628 if (route->initcwnd > 0) {
629 r = sd_netlink_message_append_u32(req, RTAX_INITCWND, route->initcwnd);
630 if (r < 0)
631 return log_error_errno(r, "Could not append RTAX_INITCWND attribute: %m");
632 }
633
634 if (route->initrwnd > 0) {
635 r = sd_netlink_message_append_u32(req, RTAX_INITRWND, route->initrwnd);
636 if (r < 0)
637 return log_error_errno(r, "Could not append RTAX_INITRWND attribute: %m");
638 }
639
640 if (route->quickack != -1) {
641 r = sd_netlink_message_append_u32(req, RTAX_QUICKACK, route->quickack);
642 if (r < 0)
643 return log_error_errno(r, "Could not append RTAX_QUICKACK attribute: %m");
644 }
645
646 r = sd_netlink_message_close_container(req);
647 if (r < 0)
648 return log_error_errno(r, "Could not append RTA_METRICS attribute: %m");
649
650 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
651 if (r < 0)
652 return log_error_errno(r, "Could not send rtnetlink message: %m");
653
654 link_ref(link);
655
656 lifetime = route->lifetime;
657
658 r = route_add(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, &route);
659 if (r < 0)
660 return log_error_errno(r, "Could not add route: %m");
661
662 /* TODO: drop expiration handling once it can be pushed into the kernel */
663 route->lifetime = lifetime;
664
665 if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) {
666 r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(),
667 route->lifetime, 0, route_expire_handler, route);
668 if (r < 0)
669 return log_error_errno(r, "Could not arm expiration timer: %m");
670 }
671
672 sd_event_source_unref(route->expire);
673 route->expire = TAKE_PTR(expire);
674
675 return 0;
676 }
677
678 int config_parse_gateway(
679 const char *unit,
680 const char *filename,
681 unsigned line,
682 const char *section,
683 unsigned section_line,
684 const char *lvalue,
685 int ltype,
686 const char *rvalue,
687 void *data,
688 void *userdata) {
689
690 Network *network = userdata;
691 _cleanup_(route_freep) Route *n = NULL;
692 union in_addr_union buffer;
693 int r, f;
694
695 assert(filename);
696 assert(section);
697 assert(lvalue);
698 assert(rvalue);
699 assert(data);
700
701 if (streq(section, "Network")) {
702 /* we are not in an Route section, so treat
703 * this as the special '0' section */
704 r = route_new_static(network, NULL, 0, &n);
705 } else
706 r = route_new_static(network, filename, section_line, &n);
707
708 if (r < 0)
709 return r;
710
711 r = in_addr_from_string_auto(rvalue, &f, &buffer);
712 if (r < 0) {
713 log_syntax(unit, LOG_ERR, filename, line, r, "Route is invalid, ignoring assignment: %s", rvalue);
714 return 0;
715 }
716
717 n->family = f;
718 n->gw = buffer;
719 TAKE_PTR(n);
720
721 return 0;
722 }
723
724 int config_parse_preferred_src(
725 const char *unit,
726 const char *filename,
727 unsigned line,
728 const char *section,
729 unsigned section_line,
730 const char *lvalue,
731 int ltype,
732 const char *rvalue,
733 void *data,
734 void *userdata) {
735
736 Network *network = userdata;
737 _cleanup_(route_freep) Route *n = NULL;
738 union in_addr_union buffer;
739 int r, f;
740
741 assert(filename);
742 assert(section);
743 assert(lvalue);
744 assert(rvalue);
745 assert(data);
746
747 r = route_new_static(network, filename, section_line, &n);
748 if (r < 0)
749 return r;
750
751 r = in_addr_from_string_auto(rvalue, &f, &buffer);
752 if (r < 0) {
753 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
754 "Preferred source is invalid, ignoring assignment: %s", rvalue);
755 return 0;
756 }
757
758 n->family = f;
759 n->prefsrc = buffer;
760 TAKE_PTR(n);
761
762 return 0;
763 }
764
765 int config_parse_destination(
766 const char *unit,
767 const char *filename,
768 unsigned line,
769 const char *section,
770 unsigned section_line,
771 const char *lvalue,
772 int ltype,
773 const char *rvalue,
774 void *data,
775 void *userdata) {
776
777 Network *network = userdata;
778 _cleanup_(route_freep) Route *n = NULL;
779 union in_addr_union buffer;
780 unsigned char prefixlen;
781 int r;
782
783 assert(filename);
784 assert(section);
785 assert(lvalue);
786 assert(rvalue);
787 assert(data);
788
789 r = route_new_static(network, filename, section_line, &n);
790 if (r < 0)
791 return r;
792
793 r = in_addr_prefix_from_string(rvalue, AF_INET, &buffer, &prefixlen);
794 if (r < 0) {
795 r = in_addr_prefix_from_string(rvalue, AF_INET6, &buffer, &prefixlen);
796 if (r < 0) {
797 log_syntax(unit, LOG_ERR, filename, line, r,
798 "Route %s= prefix is invalid, ignoring assignment: %s",
799 lvalue, rvalue);
800 return 0;
801 }
802
803 n->family = AF_INET6;
804 } else
805 n->family = AF_INET;
806
807 if (streq(lvalue, "Destination")) {
808 n->dst = buffer;
809 n->dst_prefixlen = prefixlen;
810 } else if (streq(lvalue, "Source")) {
811 n->src = buffer;
812 n->src_prefixlen = prefixlen;
813 } else
814 assert_not_reached(lvalue);
815
816 TAKE_PTR(n);
817 return 0;
818 }
819
820 int config_parse_route_priority(
821 const char *unit,
822 const char *filename,
823 unsigned line,
824 const char *section,
825 unsigned section_line,
826 const char *lvalue,
827 int ltype,
828 const char *rvalue,
829 void *data,
830 void *userdata) {
831
832 Network *network = userdata;
833 _cleanup_(route_freep) Route *n = NULL;
834 int r;
835
836 assert(filename);
837 assert(section);
838 assert(lvalue);
839 assert(rvalue);
840 assert(data);
841
842 r = route_new_static(network, filename, section_line, &n);
843 if (r < 0)
844 return r;
845
846 r = safe_atou32(rvalue, &n->priority);
847 if (r < 0) {
848 log_syntax(unit, LOG_ERR, filename, line, r,
849 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue);
850 return 0;
851 }
852
853 TAKE_PTR(n);
854 return 0;
855 }
856
857 int config_parse_route_scope(
858 const char *unit,
859 const char *filename,
860 unsigned line,
861 const char *section,
862 unsigned section_line,
863 const char *lvalue,
864 int ltype,
865 const char *rvalue,
866 void *data,
867 void *userdata) {
868
869 Network *network = userdata;
870 _cleanup_(route_freep) Route *n = NULL;
871 int r;
872
873 assert(filename);
874 assert(section);
875 assert(lvalue);
876 assert(rvalue);
877 assert(data);
878
879 r = route_new_static(network, filename, section_line, &n);
880 if (r < 0)
881 return r;
882
883 if (streq(rvalue, "host"))
884 n->scope = RT_SCOPE_HOST;
885 else if (streq(rvalue, "link"))
886 n->scope = RT_SCOPE_LINK;
887 else if (streq(rvalue, "global"))
888 n->scope = RT_SCOPE_UNIVERSE;
889 else {
890 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route scope: %s", rvalue);
891 return 0;
892 }
893
894 TAKE_PTR(n);
895 return 0;
896 }
897
898 int config_parse_route_table(
899 const char *unit,
900 const char *filename,
901 unsigned line,
902 const char *section,
903 unsigned section_line,
904 const char *lvalue,
905 int ltype,
906 const char *rvalue,
907 void *data,
908 void *userdata) {
909
910 _cleanup_(route_freep) Route *n = NULL;
911 Network *network = userdata;
912 int r;
913
914 assert(filename);
915 assert(section);
916 assert(lvalue);
917 assert(rvalue);
918 assert(data);
919
920 r = route_new_static(network, filename, section_line, &n);
921 if (r < 0)
922 return r;
923
924 r = safe_atou32(rvalue, &n->table);
925 if (r < 0) {
926 log_syntax(unit, LOG_ERR, filename, line, r,
927 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue);
928 return 0;
929 }
930
931 TAKE_PTR(n);
932 return 0;
933 }
934
935 int config_parse_gateway_onlink(
936 const char *unit,
937 const char *filename,
938 unsigned line,
939 const char *section,
940 unsigned section_line,
941 const char *lvalue,
942 int ltype,
943 const char *rvalue,
944 void *data,
945 void *userdata) {
946
947 Network *network = userdata;
948 _cleanup_(route_freep) Route *n = NULL;
949 int r;
950
951 assert(filename);
952 assert(section);
953 assert(lvalue);
954 assert(rvalue);
955 assert(data);
956
957 r = route_new_static(network, filename, section_line, &n);
958 if (r < 0)
959 return r;
960
961 r = parse_boolean(rvalue);
962 if (r < 0) {
963 log_syntax(unit, LOG_ERR, filename, line, r,
964 "Could not parse gateway onlink \"%s\", ignoring assignment: %m", rvalue);
965 return 0;
966 }
967
968 SET_FLAG(n->flags, RTNH_F_ONLINK, r);
969 TAKE_PTR(n);
970 return 0;
971 }
972
973 int config_parse_ipv6_route_preference(
974 const char *unit,
975 const char *filename,
976 unsigned line,
977 const char *section,
978 unsigned section_line,
979 const char *lvalue,
980 int ltype,
981 const char *rvalue,
982 void *data,
983 void *userdata) {
984
985 Network *network = userdata;
986 _cleanup_(route_freep) Route *n = NULL;
987 int r;
988
989 r = route_new_static(network, filename, section_line, &n);
990 if (r < 0)
991 return r;
992
993 if (streq(rvalue, "low"))
994 n->pref = ICMPV6_ROUTER_PREF_LOW;
995 else if (streq(rvalue, "medium"))
996 n->pref = ICMPV6_ROUTER_PREF_MEDIUM;
997 else if (streq(rvalue, "high"))
998 n->pref = ICMPV6_ROUTER_PREF_HIGH;
999 else {
1000 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route preference: %s", rvalue);
1001 return 0;
1002 }
1003
1004 TAKE_PTR(n);
1005 return 0;
1006 }
1007
1008 int config_parse_route_protocol(
1009 const char *unit,
1010 const char *filename,
1011 unsigned line,
1012 const char *section,
1013 unsigned section_line,
1014 const char *lvalue,
1015 int ltype,
1016 const char *rvalue,
1017 void *data,
1018 void *userdata) {
1019
1020 Network *network = userdata;
1021 _cleanup_(route_freep) Route *n = NULL;
1022 int r;
1023
1024 r = route_new_static(network, filename, section_line, &n);
1025 if (r < 0)
1026 return r;
1027
1028 if (streq(rvalue, "kernel"))
1029 n->protocol = RTPROT_KERNEL;
1030 else if (streq(rvalue, "boot"))
1031 n->protocol = RTPROT_BOOT;
1032 else if (streq(rvalue, "static"))
1033 n->protocol = RTPROT_STATIC;
1034 else {
1035 r = safe_atou8(rvalue , &n->protocol);
1036 if (r < 0) {
1037 log_syntax(unit, LOG_ERR, filename, line, r, "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue);
1038 return 0;
1039 }
1040 }
1041
1042 TAKE_PTR(n);
1043 return 0;
1044 }
1045
1046 int config_parse_route_type(
1047 const char *unit,
1048 const char *filename,
1049 unsigned line,
1050 const char *section,
1051 unsigned section_line,
1052 const char *lvalue,
1053 int ltype,
1054 const char *rvalue,
1055 void *data,
1056 void *userdata) {
1057
1058 Network *network = userdata;
1059 _cleanup_(route_freep) Route *n = NULL;
1060 int r;
1061
1062 r = route_new_static(network, filename, section_line, &n);
1063 if (r < 0)
1064 return r;
1065
1066 if (streq(rvalue, "unicast"))
1067 n->type = RTN_UNICAST;
1068 else if (streq(rvalue, "blackhole"))
1069 n->type = RTN_BLACKHOLE;
1070 else if (streq(rvalue, "unreachable"))
1071 n->type = RTN_UNREACHABLE;
1072 else if (streq(rvalue, "prohibit"))
1073 n->type = RTN_PROHIBIT;
1074 else {
1075 log_syntax(unit, LOG_ERR, filename, line, r, "Could not parse route type \"%s\", ignoring assignment: %m", rvalue);
1076 return 0;
1077 }
1078
1079 TAKE_PTR(n);
1080 return 0;
1081 }
1082
1083 int config_parse_tcp_window(
1084 const char *unit,
1085 const char *filename,
1086 unsigned line,
1087 const char *section,
1088 unsigned section_line,
1089 const char *lvalue,
1090 int ltype,
1091 const char *rvalue,
1092 void *data,
1093 void *userdata) {
1094
1095 _cleanup_(route_freep) Route *n = NULL;
1096 Network *network = userdata;
1097 uint64_t k;
1098 int r;
1099
1100 assert(filename);
1101 assert(section);
1102 assert(lvalue);
1103 assert(rvalue);
1104 assert(data);
1105
1106 r = route_new_static(network, filename, section_line, &n);
1107 if (r < 0)
1108 return r;
1109
1110 r = parse_size(rvalue, 1024, &k);
1111 if (r < 0 || k > UINT32_MAX) {
1112 log_syntax(unit, LOG_ERR, filename, line, r,
1113 "Could not parse TCP %s \"%s\" bytes, ignoring assignment: %m", rvalue, lvalue);
1114 return 0;
1115 }
1116
1117 if (streq(lvalue, "InitialCongestionWindow"))
1118 n->initcwnd = k;
1119 else if (streq(lvalue, "InitialAdvertisedReceiveWindow"))
1120 n->initrwnd = k;
1121 else {
1122 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse TCP %s: %s", lvalue, rvalue);
1123 return 0;
1124 }
1125
1126 TAKE_PTR(n);
1127 return 0;
1128 }
1129
1130 int config_parse_quickack(
1131 const char *unit,
1132 const char *filename,
1133 unsigned line,
1134 const char *section,
1135 unsigned section_line,
1136 const char *lvalue,
1137 int ltype,
1138 const char *rvalue,
1139 void *data,
1140 void *userdata) {
1141
1142 _cleanup_(route_freep) Route *n = NULL;
1143 Network *network = userdata;
1144 int k, r;
1145
1146 assert(filename);
1147 assert(section);
1148 assert(lvalue);
1149 assert(rvalue);
1150 assert(data);
1151
1152 r = route_new_static(network, filename, section_line, &n);
1153 if (r < 0)
1154 return r;
1155
1156 k = parse_boolean(rvalue);
1157 if (k < 0) {
1158 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse TCP quickack, ignoring: %s", rvalue);
1159 return 0;
1160 }
1161
1162 n->quickack = !!k;
1163 TAKE_PTR(n);
1164 return 0;
1165 }
1166
1167 int config_parse_route_mtu(
1168 const char *unit,
1169 const char *filename,
1170 unsigned line,
1171 const char *section,
1172 unsigned section_line,
1173 const char *lvalue,
1174 int ltype,
1175 const char *rvalue,
1176 void *data,
1177 void *userdata) {
1178
1179 Network *network = userdata;
1180 _cleanup_(route_freep) Route *n = NULL;
1181 int r;
1182
1183 assert(filename);
1184 assert(section);
1185 assert(lvalue);
1186 assert(rvalue);
1187 assert(data);
1188
1189 r = route_new_static(network, filename, section_line, &n);
1190 if (r < 0)
1191 return r;
1192
1193 r = config_parse_mtu(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &n->mtu, userdata);
1194 if (r < 0)
1195 return r;
1196
1197 TAKE_PTR(n);
1198 return 0;
1199 }