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