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