]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-route.c
Merge pull request #8962 from floppym/issue8905
[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 n = NULL;
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 n = NULL;
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 n = NULL;
817
818 return 0;
819 }
820
821 int config_parse_route_priority(
822 const char *unit,
823 const char *filename,
824 unsigned line,
825 const char *section,
826 unsigned section_line,
827 const char *lvalue,
828 int ltype,
829 const char *rvalue,
830 void *data,
831 void *userdata) {
832
833 Network *network = userdata;
834 _cleanup_(route_freep) Route *n = NULL;
835 uint32_t k;
836 int r;
837
838 assert(filename);
839 assert(section);
840 assert(lvalue);
841 assert(rvalue);
842 assert(data);
843
844 r = route_new_static(network, filename, section_line, &n);
845 if (r < 0)
846 return r;
847
848 r = safe_atou32(rvalue, &k);
849 if (r < 0) {
850 log_syntax(unit, LOG_ERR, filename, line, r,
851 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue);
852 return 0;
853 }
854
855 n->priority = k;
856 n = NULL;
857
858 return 0;
859 }
860
861 int config_parse_route_scope(
862 const char *unit,
863 const char *filename,
864 unsigned line,
865 const char *section,
866 unsigned section_line,
867 const char *lvalue,
868 int ltype,
869 const char *rvalue,
870 void *data,
871 void *userdata) {
872
873 Network *network = userdata;
874 _cleanup_(route_freep) Route *n = NULL;
875 int r;
876
877 assert(filename);
878 assert(section);
879 assert(lvalue);
880 assert(rvalue);
881 assert(data);
882
883 r = route_new_static(network, filename, section_line, &n);
884 if (r < 0)
885 return r;
886
887 if (streq(rvalue, "host"))
888 n->scope = RT_SCOPE_HOST;
889 else if (streq(rvalue, "link"))
890 n->scope = RT_SCOPE_LINK;
891 else if (streq(rvalue, "global"))
892 n->scope = RT_SCOPE_UNIVERSE;
893 else {
894 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route scope: %s", rvalue);
895 return 0;
896 }
897
898 n = NULL;
899
900 return 0;
901 }
902
903 int config_parse_route_table(
904 const char *unit,
905 const char *filename,
906 unsigned line,
907 const char *section,
908 unsigned section_line,
909 const char *lvalue,
910 int ltype,
911 const char *rvalue,
912 void *data,
913 void *userdata) {
914
915 _cleanup_(route_freep) Route *n = NULL;
916 Network *network = userdata;
917 uint32_t k;
918 int r;
919
920 assert(filename);
921 assert(section);
922 assert(lvalue);
923 assert(rvalue);
924 assert(data);
925
926 r = route_new_static(network, filename, section_line, &n);
927 if (r < 0)
928 return r;
929
930 r = safe_atou32(rvalue, &k);
931 if (r < 0) {
932 log_syntax(unit, LOG_ERR, filename, line, r,
933 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue);
934 return 0;
935 }
936
937 n->table = k;
938 n = NULL;
939
940 return 0;
941 }
942
943 int config_parse_gateway_onlink(
944 const char *unit,
945 const char *filename,
946 unsigned line,
947 const char *section,
948 unsigned section_line,
949 const char *lvalue,
950 int ltype,
951 const char *rvalue,
952 void *data,
953 void *userdata) {
954
955 Network *network = userdata;
956 _cleanup_(route_freep) Route *n = NULL;
957 int r;
958
959 assert(filename);
960 assert(section);
961 assert(lvalue);
962 assert(rvalue);
963 assert(data);
964
965 r = route_new_static(network, filename, section_line, &n);
966 if (r < 0)
967 return r;
968
969 r = parse_boolean(rvalue);
970 if (r < 0) {
971 log_syntax(unit, LOG_ERR, filename, line, r,
972 "Could not parse gateway onlink \"%s\", ignoring assignment: %m", rvalue);
973 return 0;
974 }
975
976 SET_FLAG(n->flags, RTNH_F_ONLINK, r);
977 n = NULL;
978
979 return 0;
980 }
981
982 int config_parse_ipv6_route_preference(
983 const char *unit,
984 const char *filename,
985 unsigned line,
986 const char *section,
987 unsigned section_line,
988 const char *lvalue,
989 int ltype,
990 const char *rvalue,
991 void *data,
992 void *userdata) {
993
994 Network *network = userdata;
995 _cleanup_(route_freep) Route *n = NULL;
996 int r;
997
998 r = route_new_static(network, filename, section_line, &n);
999 if (r < 0)
1000 return r;
1001
1002 if (streq(rvalue, "low"))
1003 n->pref = ICMPV6_ROUTER_PREF_LOW;
1004 else if (streq(rvalue, "medium"))
1005 n->pref = ICMPV6_ROUTER_PREF_MEDIUM;
1006 else if (streq(rvalue, "high"))
1007 n->pref = ICMPV6_ROUTER_PREF_HIGH;
1008 else {
1009 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route preference: %s", rvalue);
1010 return 0;
1011 }
1012
1013 n = NULL;
1014
1015 return 0;
1016 }
1017
1018 int config_parse_route_protocol(
1019 const char *unit,
1020 const char *filename,
1021 unsigned line,
1022 const char *section,
1023 unsigned section_line,
1024 const char *lvalue,
1025 int ltype,
1026 const char *rvalue,
1027 void *data,
1028 void *userdata) {
1029
1030 Network *network = userdata;
1031 _cleanup_(route_freep) Route *n = NULL;
1032 int r;
1033
1034 r = route_new_static(network, filename, section_line, &n);
1035 if (r < 0)
1036 return r;
1037
1038 if (streq(rvalue, "kernel"))
1039 n->protocol = RTPROT_KERNEL;
1040 else if (streq(rvalue, "boot"))
1041 n->protocol = RTPROT_BOOT;
1042 else if (streq(rvalue, "static"))
1043 n->protocol = RTPROT_STATIC;
1044 else {
1045 r = safe_atou8(rvalue , &n->protocol);
1046 if (r < 0) {
1047 log_syntax(unit, LOG_ERR, filename, line, r, "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue);
1048 return 0;
1049 }
1050 }
1051
1052 n = NULL;
1053
1054 return 0;
1055 }
1056
1057 int config_parse_route_type(
1058 const char *unit,
1059 const char *filename,
1060 unsigned line,
1061 const char *section,
1062 unsigned section_line,
1063 const char *lvalue,
1064 int ltype,
1065 const char *rvalue,
1066 void *data,
1067 void *userdata) {
1068
1069 Network *network = userdata;
1070 _cleanup_(route_freep) Route *n = NULL;
1071 int r;
1072
1073 r = route_new_static(network, filename, section_line, &n);
1074 if (r < 0)
1075 return r;
1076
1077 if (streq(rvalue, "unicast"))
1078 n->type = RTN_UNICAST;
1079 else if (streq(rvalue, "blackhole"))
1080 n->type = RTN_BLACKHOLE;
1081 else if (streq(rvalue, "unreachable"))
1082 n->type = RTN_UNREACHABLE;
1083 else if (streq(rvalue, "prohibit"))
1084 n->type = RTN_PROHIBIT;
1085 else {
1086 log_syntax(unit, LOG_ERR, filename, line, r, "Could not parse route type \"%s\", ignoring assignment: %m", rvalue);
1087 return 0;
1088 }
1089
1090 n = NULL;
1091
1092 return 0;
1093 }
1094
1095 int config_parse_tcp_window(
1096 const char *unit,
1097 const char *filename,
1098 unsigned line,
1099 const char *section,
1100 unsigned section_line,
1101 const char *lvalue,
1102 int ltype,
1103 const char *rvalue,
1104 void *data,
1105 void *userdata) {
1106
1107 _cleanup_(route_freep) Route *n = NULL;
1108 Network *network = userdata;
1109 uint64_t k;
1110 int r;
1111
1112 assert(filename);
1113 assert(section);
1114 assert(lvalue);
1115 assert(rvalue);
1116 assert(data);
1117
1118 r = route_new_static(network, filename, section_line, &n);
1119 if (r < 0)
1120 return r;
1121
1122 r = parse_size(rvalue, 1024, &k);
1123 if (r < 0 || k > UINT32_MAX) {
1124 log_syntax(unit, LOG_ERR, filename, line, r,
1125 "Could not parse TCP %s \"%s\" bytes, ignoring assignment: %m", rvalue, lvalue);
1126 return 0;
1127 }
1128
1129 if (streq(lvalue, "InitialCongestionWindow"))
1130 n->initcwnd = k;
1131 else if (streq(lvalue, "InitialAdvertisedReceiveWindow"))
1132 n->initrwnd = k;
1133 else {
1134 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse TCP %s: %s", lvalue, rvalue);
1135 return 0;
1136 }
1137
1138 n = NULL;
1139
1140 return 0;
1141 }
1142
1143 int config_parse_quickack(
1144 const char *unit,
1145 const char *filename,
1146 unsigned line,
1147 const char *section,
1148 unsigned section_line,
1149 const char *lvalue,
1150 int ltype,
1151 const char *rvalue,
1152 void *data,
1153 void *userdata) {
1154
1155 _cleanup_(route_freep) Route *n = NULL;
1156 Network *network = userdata;
1157 int k, r;
1158
1159 assert(filename);
1160 assert(section);
1161 assert(lvalue);
1162 assert(rvalue);
1163 assert(data);
1164
1165 r = route_new_static(network, filename, section_line, &n);
1166 if (r < 0)
1167 return r;
1168
1169 k = parse_boolean(rvalue);
1170 if (k < 0) {
1171 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse TCP quickack, ignoring: %s", rvalue);
1172 return 0;
1173 }
1174
1175 n->quickack = !!k;
1176 n = NULL;
1177
1178 return 0;
1179 }