]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-route.c
Merge pull request #8417 from brauner/2018-03-09/add_bind_mount_fallback_to_private_d...
[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_free_ 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_free_ NetworkConfigSection *n = NULL;
73 _cleanup_route_free_ 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_free_ 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(
314 Link *link,
315 int family,
316 const union in_addr_union *dst,
317 unsigned char dst_prefixlen,
318 unsigned char tos,
319 uint32_t priority,
320 uint32_t table,
321 Route **ret) {
322
323 Route *route;
324 int r;
325
326 r = route_get(link, family, dst, dst_prefixlen, tos, priority, table, &route);
327 if (r == -ENOENT) {
328 /* Route does not exist, create a new one */
329 r = route_add_internal(link, &link->routes, family, dst, dst_prefixlen, tos, priority, table, &route);
330 if (r < 0)
331 return r;
332 } else if (r == 0) {
333 /* Take over a foreign route */
334 r = set_ensure_allocated(&link->routes, &route_hash_ops);
335 if (r < 0)
336 return r;
337
338 r = set_put(link->routes, route);
339 if (r < 0)
340 return r;
341
342 set_remove(link->routes_foreign, route);
343 } else if (r == 1) {
344 /* Route exists, do nothing */
345 ;
346 } else
347 return r;
348
349 if (ret)
350 *ret = route;
351
352 return 0;
353 }
354
355 void route_update(Route *route,
356 const union in_addr_union *src,
357 unsigned char src_prefixlen,
358 const union in_addr_union *gw,
359 const union in_addr_union *prefsrc,
360 unsigned char scope,
361 unsigned char protocol,
362 unsigned char type) {
363
364 assert(route);
365 assert(src);
366 assert(gw);
367 assert(prefsrc);
368
369 route->src = *src;
370 route->src_prefixlen = src_prefixlen;
371 route->gw = *gw;
372 route->prefsrc = *prefsrc;
373 route->scope = scope;
374 route->protocol = protocol;
375 route->type = type;
376 }
377
378 int route_remove(Route *route, Link *link,
379 sd_netlink_message_handler_t callback) {
380 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
381 int r;
382
383 assert(link);
384 assert(link->manager);
385 assert(link->manager->rtnl);
386 assert(link->ifindex > 0);
387 assert(IN_SET(route->family, AF_INET, AF_INET6));
388
389 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
390 RTM_DELROUTE, route->family,
391 route->protocol);
392 if (r < 0)
393 return log_error_errno(r, "Could not create RTM_DELROUTE message: %m");
394
395 if (!in_addr_is_null(route->family, &route->gw)) {
396 if (route->family == AF_INET)
397 r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &route->gw.in);
398 else if (route->family == AF_INET6)
399 r = sd_netlink_message_append_in6_addr(req, RTA_GATEWAY, &route->gw.in6);
400 if (r < 0)
401 return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
402 }
403
404 if (route->dst_prefixlen) {
405 if (route->family == AF_INET)
406 r = sd_netlink_message_append_in_addr(req, RTA_DST, &route->dst.in);
407 else if (route->family == AF_INET6)
408 r = sd_netlink_message_append_in6_addr(req, RTA_DST, &route->dst.in6);
409 if (r < 0)
410 return log_error_errno(r, "Could not append RTA_DST attribute: %m");
411
412 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
413 if (r < 0)
414 return log_error_errno(r, "Could not set destination prefix length: %m");
415 }
416
417 if (route->src_prefixlen) {
418 if (route->family == AF_INET)
419 r = sd_netlink_message_append_in_addr(req, RTA_SRC, &route->src.in);
420 else if (route->family == AF_INET6)
421 r = sd_netlink_message_append_in6_addr(req, RTA_SRC, &route->src.in6);
422 if (r < 0)
423 return log_error_errno(r, "Could not append RTA_SRC attribute: %m");
424
425 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
426 if (r < 0)
427 return log_error_errno(r, "Could not set source prefix length: %m");
428 }
429
430 if (!in_addr_is_null(route->family, &route->prefsrc)) {
431 if (route->family == AF_INET)
432 r = sd_netlink_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc.in);
433 else if (route->family == AF_INET6)
434 r = sd_netlink_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc.in6);
435 if (r < 0)
436 return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m");
437 }
438
439 r = sd_rtnl_message_route_set_scope(req, route->scope);
440 if (r < 0)
441 return log_error_errno(r, "Could not set scope: %m");
442
443 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
444 if (r < 0)
445 return log_error_errno(r, "Could not append RTA_PRIORITY attribute: %m");
446
447 if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE)) {
448 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
449 if (r < 0)
450 return log_error_errno(r, "Could not append RTA_OIF attribute: %m");
451 }
452
453 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
454 if (r < 0)
455 return log_error_errno(r, "Could not send rtnetlink message: %m");
456
457 link_ref(link);
458
459 return 0;
460 }
461
462 static int route_expire_callback(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
463 Link *link = userdata;
464 int r;
465
466 assert(rtnl);
467 assert(m);
468 assert(link);
469 assert(link->ifname);
470
471 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
472 return 1;
473
474 r = sd_netlink_message_get_errno(m);
475 if (r < 0 && r != -EEXIST)
476 log_link_warning_errno(link, r, "could not remove route: %m");
477
478 return 1;
479 }
480
481 int route_expire_handler(sd_event_source *s, uint64_t usec, void *userdata) {
482 Route *route = userdata;
483 int r;
484
485 assert(route);
486
487 r = route_remove(route, route->link, route_expire_callback);
488 if (r < 0)
489 log_warning_errno(r, "Could not remove route: %m");
490 else
491 route_free(route);
492
493 return 1;
494 }
495
496 int route_configure(
497 Route *route,
498 Link *link,
499 sd_netlink_message_handler_t callback) {
500
501 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
502 _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL;
503 usec_t lifetime;
504 int r;
505
506 assert(link);
507 assert(link->manager);
508 assert(link->manager->rtnl);
509 assert(link->ifindex > 0);
510 assert(IN_SET(route->family, AF_INET, AF_INET6));
511
512 if (route_get(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, NULL) <= 0 &&
513 set_size(link->routes) >= routes_max())
514 return -E2BIG;
515
516 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
517 RTM_NEWROUTE, route->family,
518 route->protocol);
519 if (r < 0)
520 return log_error_errno(r, "Could not create RTM_NEWROUTE message: %m");
521
522 if (!in_addr_is_null(route->family, &route->gw)) {
523 if (route->family == AF_INET)
524 r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &route->gw.in);
525 else if (route->family == AF_INET6)
526 r = sd_netlink_message_append_in6_addr(req, RTA_GATEWAY, &route->gw.in6);
527 if (r < 0)
528 return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
529
530 r = sd_rtnl_message_route_set_family(req, route->family);
531 if (r < 0)
532 return log_error_errno(r, "Could not set route family: %m");
533 }
534
535 if (route->dst_prefixlen) {
536 if (route->family == AF_INET)
537 r = sd_netlink_message_append_in_addr(req, RTA_DST, &route->dst.in);
538 else if (route->family == AF_INET6)
539 r = sd_netlink_message_append_in6_addr(req, RTA_DST, &route->dst.in6);
540 if (r < 0)
541 return log_error_errno(r, "Could not append RTA_DST attribute: %m");
542
543 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
544 if (r < 0)
545 return log_error_errno(r, "Could not set destination prefix length: %m");
546 }
547
548 if (route->src_prefixlen) {
549 if (route->family == AF_INET)
550 r = sd_netlink_message_append_in_addr(req, RTA_SRC, &route->src.in);
551 else if (route->family == AF_INET6)
552 r = sd_netlink_message_append_in6_addr(req, RTA_SRC, &route->src.in6);
553 if (r < 0)
554 return log_error_errno(r, "Could not append RTA_SRC attribute: %m");
555
556 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
557 if (r < 0)
558 return log_error_errno(r, "Could not set source prefix length: %m");
559 }
560
561 if (!in_addr_is_null(route->family, &route->prefsrc)) {
562 if (route->family == AF_INET)
563 r = sd_netlink_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc.in);
564 else if (route->family == AF_INET6)
565 r = sd_netlink_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc.in6);
566 if (r < 0)
567 return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m");
568 }
569
570 r = sd_rtnl_message_route_set_scope(req, route->scope);
571 if (r < 0)
572 return log_error_errno(r, "Could not set scope: %m");
573
574 r = sd_rtnl_message_route_set_flags(req, route->flags);
575 if (r < 0)
576 return log_error_errno(r, "Could not set flags: %m");
577
578 if (route->table != RT_TABLE_MAIN) {
579 if (route->table < 256) {
580 r = sd_rtnl_message_route_set_table(req, route->table);
581 if (r < 0)
582 return log_error_errno(r, "Could not set route table: %m");
583 } else {
584 r = sd_rtnl_message_route_set_table(req, RT_TABLE_UNSPEC);
585 if (r < 0)
586 return log_error_errno(r, "Could not set route table: %m");
587
588 /* Table attribute to allow more than 256. */
589 r = sd_netlink_message_append_data(req, RTA_TABLE, &route->table, sizeof(route->table));
590 if (r < 0)
591 return log_error_errno(r, "Could not append RTA_TABLE attribute: %m");
592 }
593 }
594
595 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
596 if (r < 0)
597 return log_error_errno(r, "Could not append RTA_PRIORITY attribute: %m");
598
599 r = sd_netlink_message_append_u8(req, RTA_PREF, route->pref);
600 if (r < 0)
601 return log_error_errno(r, "Could not append RTA_PREF attribute: %m");
602
603 if (route->lifetime != USEC_INFINITY && kernel_route_expiration_supported()) {
604 r = sd_netlink_message_append_u32(req, RTA_EXPIRES,
605 DIV_ROUND_UP(usec_sub_unsigned(route->lifetime, now(clock_boottime_or_monotonic())), USEC_PER_SEC));
606 if (r < 0)
607 return log_error_errno(r, "Could not append RTA_EXPIRES attribute: %m");
608 }
609
610 r = sd_rtnl_message_route_set_type(req, route->type);
611 if (r < 0)
612 return log_error_errno(r, "Could not set route type: %m");
613
614 if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE)) {
615 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
616 if (r < 0)
617 return log_error_errno(r, "Could not append RTA_OIF attribute: %m");
618 }
619
620 r = sd_netlink_message_open_container(req, RTA_METRICS);
621 if (r < 0)
622 return log_error_errno(r, "Could not append RTA_METRICS attribute: %m");
623
624 if (route->mtu > 0) {
625 r = sd_netlink_message_append_u32(req, RTAX_MTU, route->mtu);
626 if (r < 0)
627 return log_error_errno(r, "Could not append RTAX_MTU attribute: %m");
628 }
629
630 if (route->initcwnd > 0) {
631 r = sd_netlink_message_append_u32(req, RTAX_INITCWND, route->initcwnd);
632 if (r < 0)
633 return log_error_errno(r, "Could not append RTAX_INITCWND attribute: %m");
634 }
635
636 if (route->initrwnd > 0) {
637 r = sd_netlink_message_append_u32(req, RTAX_INITRWND, route->initrwnd);
638 if (r < 0)
639 return log_error_errno(r, "Could not append RTAX_INITRWND attribute: %m");
640 }
641
642 if (route->quickack != -1) {
643 r = sd_netlink_message_append_u32(req, RTAX_QUICKACK, route->quickack);
644 if (r < 0)
645 return log_error_errno(r, "Could not append RTAX_QUICKACK attribute: %m");
646 }
647
648 r = sd_netlink_message_close_container(req);
649 if (r < 0)
650 return log_error_errno(r, "Could not append RTA_METRICS attribute: %m");
651
652 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
653 if (r < 0)
654 return log_error_errno(r, "Could not send rtnetlink message: %m");
655
656 link_ref(link);
657
658 lifetime = route->lifetime;
659
660 r = route_add(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, &route);
661 if (r < 0)
662 return log_error_errno(r, "Could not add route: %m");
663
664 /* TODO: drop expiration handling once it can be pushed into the kernel */
665 route->lifetime = lifetime;
666
667 if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) {
668 r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(),
669 route->lifetime, 0, route_expire_handler, route);
670 if (r < 0)
671 return log_error_errno(r, "Could not arm expiration timer: %m");
672 }
673
674 sd_event_source_unref(route->expire);
675 route->expire = TAKE_PTR(expire);
676
677 return 0;
678 }
679
680 int config_parse_gateway(const char *unit,
681 const char *filename,
682 unsigned line,
683 const char *section,
684 unsigned section_line,
685 const char *lvalue,
686 int ltype,
687 const char *rvalue,
688 void *data,
689 void *userdata) {
690
691 Network *network = userdata;
692 _cleanup_route_free_ Route *n = NULL;
693 union in_addr_union buffer;
694 int r, f;
695
696 assert(filename);
697 assert(section);
698 assert(lvalue);
699 assert(rvalue);
700 assert(data);
701
702 if (streq(section, "Network")) {
703 /* we are not in an Route section, so treat
704 * this as the special '0' section */
705 r = route_new_static(network, NULL, 0, &n);
706 } else
707 r = route_new_static(network, filename, section_line, &n);
708
709 if (r < 0)
710 return r;
711
712 r = in_addr_from_string_auto(rvalue, &f, &buffer);
713 if (r < 0) {
714 log_syntax(unit, LOG_ERR, filename, line, r, "Route is invalid, ignoring assignment: %s", rvalue);
715 return 0;
716 }
717
718 n->family = f;
719 n->gw = buffer;
720 n = NULL;
721
722 return 0;
723 }
724
725 int config_parse_preferred_src(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_free_ 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(const char *unit,
766 const char *filename,
767 unsigned line,
768 const char *section,
769 unsigned section_line,
770 const char *lvalue,
771 int ltype,
772 const char *rvalue,
773 void *data,
774 void *userdata) {
775
776 Network *network = userdata;
777 _cleanup_route_free_ Route *n = NULL;
778 union in_addr_union buffer;
779 unsigned char prefixlen;
780 int r;
781
782 assert(filename);
783 assert(section);
784 assert(lvalue);
785 assert(rvalue);
786 assert(data);
787
788 r = route_new_static(network, filename, section_line, &n);
789 if (r < 0)
790 return r;
791
792 r = in_addr_prefix_from_string(rvalue, AF_INET, &buffer, &prefixlen);
793 if (r < 0) {
794 r = in_addr_prefix_from_string(rvalue, AF_INET6, &buffer, &prefixlen);
795 if (r < 0) {
796 log_syntax(unit, LOG_ERR, filename, line, r,
797 "Route %s= prefix is invalid, ignoring assignment: %s",
798 lvalue, rvalue);
799 return 0;
800 }
801
802 n->family = AF_INET6;
803 } else
804 n->family = AF_INET;
805
806 if (streq(lvalue, "Destination")) {
807 n->dst = buffer;
808 n->dst_prefixlen = prefixlen;
809 } else if (streq(lvalue, "Source")) {
810 n->src = buffer;
811 n->src_prefixlen = prefixlen;
812 } else
813 assert_not_reached(lvalue);
814
815 n = NULL;
816
817 return 0;
818 }
819
820 int config_parse_route_priority(const char *unit,
821 const char *filename,
822 unsigned line,
823 const char *section,
824 unsigned section_line,
825 const char *lvalue,
826 int ltype,
827 const char *rvalue,
828 void *data,
829 void *userdata) {
830 Network *network = userdata;
831 _cleanup_route_free_ Route *n = NULL;
832 uint32_t k;
833 int r;
834
835 assert(filename);
836 assert(section);
837 assert(lvalue);
838 assert(rvalue);
839 assert(data);
840
841 r = route_new_static(network, filename, section_line, &n);
842 if (r < 0)
843 return r;
844
845 r = safe_atou32(rvalue, &k);
846 if (r < 0) {
847 log_syntax(unit, LOG_ERR, filename, line, r,
848 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue);
849 return 0;
850 }
851
852 n->priority = k;
853 n = NULL;
854
855 return 0;
856 }
857
858 int config_parse_route_scope(const char *unit,
859 const char *filename,
860 unsigned line,
861 const char *section,
862 unsigned section_line,
863 const char *lvalue,
864 int ltype,
865 const char *rvalue,
866 void *data,
867 void *userdata) {
868 Network *network = userdata;
869 _cleanup_route_free_ Route *n = NULL;
870 int r;
871
872 assert(filename);
873 assert(section);
874 assert(lvalue);
875 assert(rvalue);
876 assert(data);
877
878 r = route_new_static(network, filename, section_line, &n);
879 if (r < 0)
880 return r;
881
882 if (streq(rvalue, "host"))
883 n->scope = RT_SCOPE_HOST;
884 else if (streq(rvalue, "link"))
885 n->scope = RT_SCOPE_LINK;
886 else if (streq(rvalue, "global"))
887 n->scope = RT_SCOPE_UNIVERSE;
888 else {
889 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route scope: %s", rvalue);
890 return 0;
891 }
892
893 n = NULL;
894
895 return 0;
896 }
897
898 int config_parse_route_table(const char *unit,
899 const char *filename,
900 unsigned line,
901 const char *section,
902 unsigned section_line,
903 const char *lvalue,
904 int ltype,
905 const char *rvalue,
906 void *data,
907 void *userdata) {
908 _cleanup_route_free_ Route *n = NULL;
909 Network *network = userdata;
910 uint32_t k;
911 int r;
912
913 assert(filename);
914 assert(section);
915 assert(lvalue);
916 assert(rvalue);
917 assert(data);
918
919 r = route_new_static(network, filename, section_line, &n);
920 if (r < 0)
921 return r;
922
923 r = safe_atou32(rvalue, &k);
924 if (r < 0) {
925 log_syntax(unit, LOG_ERR, filename, line, r,
926 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue);
927 return 0;
928 }
929
930 n->table = k;
931
932 n = NULL;
933
934 return 0;
935 }
936
937 int config_parse_gateway_onlink(const char *unit,
938 const char *filename,
939 unsigned line,
940 const char *section,
941 unsigned section_line,
942 const char *lvalue,
943 int ltype,
944 const char *rvalue,
945 void *data,
946 void *userdata) {
947 Network *network = userdata;
948 _cleanup_route_free_ Route *n = NULL;
949 int r;
950
951 assert(filename);
952 assert(section);
953 assert(lvalue);
954 assert(rvalue);
955 assert(data);
956
957 r = route_new_static(network, filename, section_line, &n);
958 if (r < 0)
959 return r;
960
961 r = parse_boolean(rvalue);
962 if (r < 0) {
963 log_syntax(unit, LOG_ERR, filename, line, r,
964 "Could not parse gateway onlink \"%s\", ignoring assignment: %m", rvalue);
965 return 0;
966 }
967
968 SET_FLAG(n->flags, RTNH_F_ONLINK, r);
969 n = NULL;
970
971 return 0;
972 }
973
974 int config_parse_ipv6_route_preference(const char *unit,
975 const char *filename,
976 unsigned line,
977 const char *section,
978 unsigned section_line,
979 const char *lvalue,
980 int ltype,
981 const char *rvalue,
982 void *data,
983 void *userdata) {
984 Network *network = userdata;
985 _cleanup_route_free_ Route *n = NULL;
986 int r;
987
988 r = route_new_static(network, filename, section_line, &n);
989 if (r < 0)
990 return r;
991
992 if (streq(rvalue, "low"))
993 n->pref = ICMPV6_ROUTER_PREF_LOW;
994 else if (streq(rvalue, "medium"))
995 n->pref = ICMPV6_ROUTER_PREF_MEDIUM;
996 else if (streq(rvalue, "high"))
997 n->pref = ICMPV6_ROUTER_PREF_HIGH;
998 else {
999 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route preference: %s", rvalue);
1000 return 0;
1001 }
1002
1003 n = NULL;
1004
1005 return 0;
1006 }
1007
1008 int config_parse_route_protocol(const char *unit,
1009 const char *filename,
1010 unsigned line,
1011 const char *section,
1012 unsigned section_line,
1013 const char *lvalue,
1014 int ltype,
1015 const char *rvalue,
1016 void *data,
1017 void *userdata) {
1018 Network *network = userdata;
1019 _cleanup_route_free_ 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 n = NULL;
1041
1042 return 0;
1043 }
1044
1045 int config_parse_route_type(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 Network *network = userdata;
1056 _cleanup_route_free_ Route *n = NULL;
1057 int r;
1058
1059 r = route_new_static(network, filename, section_line, &n);
1060 if (r < 0)
1061 return r;
1062
1063 if (streq(rvalue, "unicast"))
1064 n->type = RTN_UNICAST;
1065 else if (streq(rvalue, "blackhole"))
1066 n->type = RTN_BLACKHOLE;
1067 else if (streq(rvalue, "unreachable"))
1068 n->type = RTN_UNREACHABLE;
1069 else if (streq(rvalue, "prohibit"))
1070 n->type = RTN_PROHIBIT;
1071 else {
1072 log_syntax(unit, LOG_ERR, filename, line, r, "Could not parse route type \"%s\", ignoring assignment: %m", rvalue);
1073 return 0;
1074 }
1075
1076 n = NULL;
1077
1078 return 0;
1079 }
1080
1081 int config_parse_tcp_window(const char *unit,
1082 const char *filename,
1083 unsigned line,
1084 const char *section,
1085 unsigned section_line,
1086 const char *lvalue,
1087 int ltype,
1088 const char *rvalue,
1089 void *data,
1090 void *userdata) {
1091 _cleanup_route_free_ Route *n = NULL;
1092 Network *network = userdata;
1093 uint64_t k;
1094 int r;
1095
1096 assert(filename);
1097 assert(section);
1098 assert(lvalue);
1099 assert(rvalue);
1100 assert(data);
1101
1102 r = route_new_static(network, filename, section_line, &n);
1103 if (r < 0)
1104 return r;
1105
1106 r = parse_size(rvalue, 1024, &k);
1107 if (r < 0 || k > UINT32_MAX) {
1108 log_syntax(unit, LOG_ERR, filename, line, r,
1109 "Could not parse TCP %s \"%s\" bytes, ignoring assignment: %m", rvalue, lvalue);
1110 return 0;
1111 }
1112
1113 if (streq(lvalue, "InitialCongestionWindow"))
1114 n->initcwnd = k;
1115 else if (streq(lvalue, "InitialAdvertisedReceiveWindow"))
1116 n->initrwnd = k;
1117 else {
1118 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse TCP %s: %s", lvalue, rvalue);
1119 return 0;
1120 }
1121
1122 n = NULL;
1123
1124 return 0;
1125 }
1126
1127 int config_parse_quickack(const char *unit,
1128 const char *filename,
1129 unsigned line,
1130 const char *section,
1131 unsigned section_line,
1132 const char *lvalue,
1133 int ltype,
1134 const char *rvalue,
1135 void *data,
1136 void *userdata) {
1137 _cleanup_route_free_ Route *n = NULL;
1138 Network *network = userdata;
1139 int k, r;
1140
1141 assert(filename);
1142 assert(section);
1143 assert(lvalue);
1144 assert(rvalue);
1145 assert(data);
1146
1147 r = route_new_static(network, filename, section_line, &n);
1148 if (r < 0)
1149 return r;
1150
1151 k = parse_boolean(rvalue);
1152 if (k < 0) {
1153 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse TCP quickack, ignoring: %s", rvalue);
1154 return 0;
1155 }
1156
1157 n->quickack = !!k;
1158 n = NULL;
1159
1160 return 0;
1161 }