]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-route.c
ethtool: add several new link modes
[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-ipv4ll.h"
11 #include "networkd-manager.h"
12 #include "networkd-ndisc.h"
13 #include "networkd-route.h"
14 #include "parse-util.h"
15 #include "set.h"
16 #include "socket-netlink.h"
17 #include "string-table.h"
18 #include "string-util.h"
19 #include "strv.h"
20 #include "strxcpyx.h"
21 #include "sysctl-util.h"
22 #include "vrf.h"
23
24 #define ROUTES_DEFAULT_MAX_PER_FAMILY 4096U
25
26 static unsigned routes_max(void) {
27 static thread_local unsigned cached = 0;
28
29 _cleanup_free_ char *s4 = NULL, *s6 = NULL;
30 unsigned val4 = ROUTES_DEFAULT_MAX_PER_FAMILY, val6 = ROUTES_DEFAULT_MAX_PER_FAMILY;
31
32 if (cached > 0)
33 return cached;
34
35 if (sysctl_read("net/ipv4/route/max_size", &s4) >= 0) {
36 truncate_nl(s4);
37 if (safe_atou(s4, &val4) >= 0 &&
38 val4 == 2147483647U)
39 /* This is the default "no limit" value in the kernel */
40 val4 = ROUTES_DEFAULT_MAX_PER_FAMILY;
41 }
42
43 if (sysctl_read("net/ipv6/route/max_size", &s6) >= 0) {
44 truncate_nl(s6);
45 (void) safe_atou(s6, &val6);
46 }
47
48 cached = MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val4) +
49 MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val6);
50 return cached;
51 }
52
53 int route_new(Route **ret) {
54 _cleanup_(route_freep) Route *route = NULL;
55
56 route = new(Route, 1);
57 if (!route)
58 return -ENOMEM;
59
60 *route = (Route) {
61 .family = AF_UNSPEC,
62 .scope = RT_SCOPE_UNIVERSE,
63 .protocol = RTPROT_UNSPEC,
64 .type = RTN_UNICAST,
65 .table = RT_TABLE_MAIN,
66 .lifetime = USEC_INFINITY,
67 .quickack = -1,
68 .fast_open_no_cookie = -1,
69 .gateway_onlink = -1,
70 .ttl_propagate = -1,
71 };
72
73 *ret = TAKE_PTR(route);
74
75 return 0;
76 }
77
78 static int route_new_static(Network *network, const char *filename, unsigned section_line, Route **ret) {
79 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
80 _cleanup_(route_freep) Route *route = NULL;
81 int r;
82
83 assert(network);
84 assert(ret);
85 assert(!!filename == (section_line > 0));
86
87 if (filename) {
88 r = network_config_section_new(filename, section_line, &n);
89 if (r < 0)
90 return r;
91
92 route = hashmap_get(network->routes_by_section, n);
93 if (route) {
94 *ret = TAKE_PTR(route);
95
96 return 0;
97 }
98 }
99
100 if (network->n_static_routes >= routes_max())
101 return -E2BIG;
102
103 r = route_new(&route);
104 if (r < 0)
105 return r;
106
107 route->protocol = RTPROT_STATIC;
108 route->network = network;
109 LIST_PREPEND(routes, network->static_routes, route);
110 network->n_static_routes++;
111
112 if (filename) {
113 route->section = TAKE_PTR(n);
114
115 r = hashmap_ensure_allocated(&network->routes_by_section, &network_config_hash_ops);
116 if (r < 0)
117 return r;
118
119 r = hashmap_put(network->routes_by_section, route->section, route);
120 if (r < 0)
121 return r;
122 }
123
124 *ret = TAKE_PTR(route);
125
126 return 0;
127 }
128
129 void route_free(Route *route) {
130 if (!route)
131 return;
132
133 if (route->network) {
134 LIST_REMOVE(routes, route->network->static_routes, route);
135
136 assert(route->network->n_static_routes > 0);
137 route->network->n_static_routes--;
138
139 if (route->section)
140 hashmap_remove(route->network->routes_by_section, route->section);
141 }
142
143 network_config_section_free(route->section);
144
145 if (route->link) {
146 NDiscRoute *n;
147
148 set_remove(route->link->routes, route);
149 set_remove(route->link->routes_foreign, route);
150 set_remove(route->link->dhcp_routes, route);
151 set_remove(route->link->dhcp_routes_old, route);
152 set_remove(route->link->dhcp6_routes, route);
153 set_remove(route->link->dhcp6_routes_old, route);
154 set_remove(route->link->dhcp6_pd_routes, route);
155 set_remove(route->link->dhcp6_pd_routes_old, route);
156 SET_FOREACH(n, route->link->ndisc_routes)
157 if (n->route == route)
158 free(set_remove(route->link->ndisc_routes, n));
159 }
160
161 ordered_set_free_free(route->multipath_routes);
162
163 sd_event_source_unref(route->expire);
164
165 free(route);
166 }
167
168 void route_hash_func(const Route *route, struct siphash *state) {
169 assert(route);
170
171 siphash24_compress(&route->family, sizeof(route->family), state);
172
173 switch (route->family) {
174 case AF_INET:
175 case AF_INET6:
176 siphash24_compress(&route->dst_prefixlen, sizeof(route->dst_prefixlen), state);
177 siphash24_compress(&route->dst, FAMILY_ADDRESS_SIZE(route->family), state);
178
179 siphash24_compress(&route->src_prefixlen, sizeof(route->src_prefixlen), state);
180 siphash24_compress(&route->src, FAMILY_ADDRESS_SIZE(route->family), state);
181
182 siphash24_compress(&route->gw, FAMILY_ADDRESS_SIZE(route->family), state);
183
184 siphash24_compress(&route->prefsrc, FAMILY_ADDRESS_SIZE(route->family), state);
185
186 siphash24_compress(&route->tos, sizeof(route->tos), state);
187 siphash24_compress(&route->priority, sizeof(route->priority), state);
188 siphash24_compress(&route->table, sizeof(route->table), state);
189 siphash24_compress(&route->protocol, sizeof(route->protocol), state);
190 siphash24_compress(&route->scope, sizeof(route->scope), state);
191 siphash24_compress(&route->type, sizeof(route->type), state);
192
193 siphash24_compress(&route->initcwnd, sizeof(route->initcwnd), state);
194 siphash24_compress(&route->initrwnd, sizeof(route->initrwnd), state);
195
196 break;
197 default:
198 /* treat any other address family as AF_UNSPEC */
199 break;
200 }
201 }
202
203 int route_compare_func(const Route *a, const Route *b) {
204 int r;
205
206 r = CMP(a->family, b->family);
207 if (r != 0)
208 return r;
209
210 switch (a->family) {
211 case AF_INET:
212 case AF_INET6:
213 r = CMP(a->dst_prefixlen, b->dst_prefixlen);
214 if (r != 0)
215 return r;
216
217 r = memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
218 if (r != 0)
219 return r;
220
221 r = CMP(a->src_prefixlen, b->src_prefixlen);
222 if (r != 0)
223 return r;
224
225 r = memcmp(&a->src, &b->src, FAMILY_ADDRESS_SIZE(a->family));
226 if (r != 0)
227 return r;
228
229 r = memcmp(&a->gw, &b->gw, FAMILY_ADDRESS_SIZE(a->family));
230 if (r != 0)
231 return r;
232
233 r = memcmp(&a->prefsrc, &b->prefsrc, FAMILY_ADDRESS_SIZE(a->family));
234 if (r != 0)
235 return r;
236
237 r = CMP(a->tos, b->tos);
238 if (r != 0)
239 return r;
240
241 r = CMP(a->priority, b->priority);
242 if (r != 0)
243 return r;
244
245 r = CMP(a->table, b->table);
246 if (r != 0)
247 return r;
248
249 r = CMP(a->protocol, b->protocol);
250 if (r != 0)
251 return r;
252
253 r = CMP(a->scope, b->scope);
254 if (r != 0)
255 return r;
256
257 r = CMP(a->type, b->type);
258 if (r != 0)
259 return r;
260
261 r = CMP(a->initcwnd, b->initcwnd);
262 if (r != 0)
263 return r;
264
265 r = CMP(a->initrwnd, b->initrwnd);
266 if (r != 0)
267 return r;
268
269 return 0;
270 default:
271 /* treat any other address family as AF_UNSPEC */
272 return 0;
273 }
274 }
275
276 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
277 route_hash_ops,
278 Route,
279 route_hash_func,
280 route_compare_func,
281 route_free);
282
283 bool route_equal(Route *r1, Route *r2) {
284 if (r1 == r2)
285 return true;
286
287 if (!r1 || !r2)
288 return false;
289
290 return route_compare_func(r1, r2) == 0;
291 }
292
293 int route_get(Link *link, Route *in, Route **ret) {
294
295 Route *existing;
296
297 assert(link);
298 assert(in);
299
300 existing = set_get(link->routes, in);
301 if (existing) {
302 if (ret)
303 *ret = existing;
304 return 1;
305 }
306
307 existing = set_get(link->routes_foreign, in);
308 if (existing) {
309 if (ret)
310 *ret = existing;
311 return 0;
312 }
313
314 return -ENOENT;
315 }
316
317 static int route_add_internal(Link *link, Set **routes, Route *in, Route **ret) {
318
319 _cleanup_(route_freep) Route *route = NULL;
320 int r;
321
322 assert(link);
323 assert(routes);
324 assert(in);
325
326 r = route_new(&route);
327 if (r < 0)
328 return r;
329
330 route->family = in->family;
331 route->src = in->src;
332 route->src_prefixlen = in->src_prefixlen;
333 route->dst = in->dst;
334 route->dst_prefixlen = in->dst_prefixlen;
335 route->gw = in->gw;
336 route->prefsrc = in->prefsrc;
337 route->scope = in->scope;
338 route->protocol = in->protocol;
339 route->type = in->type;
340 route->tos = in->tos;
341 route->priority = in->priority;
342 route->table = in->table;
343 route->initcwnd = in->initcwnd;
344 route->initrwnd = in->initrwnd;
345 route->lifetime = in->lifetime;
346
347 r = set_ensure_put(routes, &route_hash_ops, route);
348 if (r < 0)
349 return r;
350 if (r == 0)
351 return -EEXIST;
352
353 route->link = link;
354
355 if (ret)
356 *ret = route;
357
358 route = NULL;
359
360 return 0;
361 }
362
363 int route_add_foreign(Link *link, Route *in, Route **ret) {
364 return route_add_internal(link, &link->routes_foreign, in, ret);
365 }
366
367 int route_add(Link *link, Route *in, Route **ret) {
368
369 Route *route;
370 int r;
371
372 r = route_get(link, in, &route);
373 if (r == -ENOENT) {
374 /* Route does not exist, create a new one */
375 r = route_add_internal(link, &link->routes, in, &route);
376 if (r < 0)
377 return r;
378 } else if (r == 0) {
379 /* Take over a foreign route */
380 r = set_ensure_put(&link->routes, &route_hash_ops, route);
381 if (r < 0)
382 return r;
383
384 set_remove(link->routes_foreign, route);
385 } else if (r == 1) {
386 /* Route exists, do nothing */
387 ;
388 } else
389 return r;
390
391 if (ret)
392 *ret = route;
393
394 return 0;
395 }
396
397 static int route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
398 int r;
399
400 assert(m);
401 assert(link);
402 assert(link->ifname);
403
404 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
405 return 1;
406
407 r = sd_netlink_message_get_errno(m);
408 if (r < 0 && r != -ESRCH)
409 log_link_message_warning_errno(link, m, r, "Could not drop route, ignoring");
410
411 return 1;
412 }
413
414 int route_remove(Route *route, Link *link,
415 link_netlink_message_handler_t callback) {
416
417 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
418 int r;
419
420 assert(link);
421 assert(link->manager);
422 assert(link->manager->rtnl);
423 assert(link->ifindex > 0);
424 assert(IN_SET(route->family, AF_INET, AF_INET6));
425
426 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
427 RTM_DELROUTE, route->family,
428 route->protocol);
429 if (r < 0)
430 return log_link_error_errno(link, r, "Could not create RTM_DELROUTE message: %m");
431
432 if (DEBUG_LOGGING) {
433 _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL;
434 char scope[ROUTE_SCOPE_STR_MAX], table[ROUTE_TABLE_STR_MAX], protocol[ROUTE_PROTOCOL_STR_MAX];
435
436 if (!in_addr_is_null(route->family, &route->dst)) {
437 (void) in_addr_to_string(route->family, &route->dst, &dst);
438 (void) asprintf(&dst_prefixlen, "/%u", route->dst_prefixlen);
439 }
440 if (!in_addr_is_null(route->family, &route->src))
441 (void) in_addr_to_string(route->family, &route->src, &src);
442 if (!in_addr_is_null(route->family, &route->gw))
443 (void) in_addr_to_string(route->family, &route->gw, &gw);
444 if (!in_addr_is_null(route->family, &route->prefsrc))
445 (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
446
447 log_link_debug(link, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
448 strna(dst), strempty(dst_prefixlen), strna(src), strna(gw), strna(prefsrc),
449 format_route_scope(route->scope, scope, sizeof(scope)),
450 format_route_table(route->table, table, sizeof(table)),
451 format_route_protocol(route->protocol, protocol, sizeof(protocol)),
452 strna(route_type_to_string(route->type)));
453 }
454
455 if (in_addr_is_null(route->family, &route->gw) == 0) {
456 r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->family, &route->gw);
457 if (r < 0)
458 return log_link_error_errno(link, r, "Could not append RTA_GATEWAY attribute: %m");
459 }
460
461 if (route->dst_prefixlen) {
462 r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst);
463 if (r < 0)
464 return log_link_error_errno(link, r, "Could not append RTA_DST attribute: %m");
465
466 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
467 if (r < 0)
468 return log_link_error_errno(link, r, "Could not set destination prefix length: %m");
469 }
470
471 if (route->src_prefixlen) {
472 r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src);
473 if (r < 0)
474 return log_link_error_errno(link, r, "Could not append RTA_SRC attribute: %m");
475
476 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
477 if (r < 0)
478 return log_link_error_errno(link, r, "Could not set source prefix length: %m");
479 }
480
481 if (in_addr_is_null(route->family, &route->prefsrc) == 0) {
482 r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc);
483 if (r < 0)
484 return log_link_error_errno(link, r, "Could not append RTA_PREFSRC attribute: %m");
485 }
486
487 r = sd_rtnl_message_route_set_scope(req, route->scope);
488 if (r < 0)
489 return log_link_error_errno(link, r, "Could not set scope: %m");
490
491 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
492 if (r < 0)
493 return log_link_error_errno(link, r, "Could not append RTA_PRIORITY attribute: %m");
494
495 if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW)) {
496 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
497 if (r < 0)
498 return log_link_error_errno(link, r, "Could not append RTA_OIF attribute: %m");
499 }
500
501 r = netlink_call_async(link->manager->rtnl, NULL, req,
502 callback ?: route_remove_handler,
503 link_netlink_destroy_callback, link);
504 if (r < 0)
505 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
506
507 link_ref(link);
508
509 return 0;
510 }
511
512 int route_expire_handler(sd_event_source *s, uint64_t usec, void *userdata) {
513 Route *route = userdata;
514 int r;
515
516 assert(route);
517
518 r = route_remove(route, route->link, NULL);
519 if (r < 0)
520 log_link_warning_errno(route->link, r, "Could not remove route: %m");
521 else
522 route_free(route);
523
524 return 1;
525 }
526
527 static int append_nexthop_one(Route *route, MultipathRoute *m, struct rtattr **rta, size_t offset) {
528 struct rtnexthop *rtnh;
529 struct rtattr *new_rta;
530 int r;
531
532 assert(route);
533 assert(m);
534 assert(rta);
535 assert(*rta);
536
537 new_rta = realloc(*rta, RTA_ALIGN((*rta)->rta_len) + RTA_SPACE(sizeof(struct rtnexthop)));
538 if (!new_rta)
539 return -ENOMEM;
540 *rta = new_rta;
541
542 rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset);
543 *rtnh = (struct rtnexthop) {
544 .rtnh_len = sizeof(*rtnh),
545 .rtnh_ifindex = m->ifindex,
546 .rtnh_hops = m->weight > 0 ? m->weight - 1 : 0,
547 };
548
549 (*rta)->rta_len += sizeof(struct rtnexthop);
550
551 if (route->family == m->gateway.family) {
552 r = rtattr_append_attribute(rta, RTA_GATEWAY, &m->gateway.address, FAMILY_ADDRESS_SIZE(m->gateway.family));
553 if (r < 0)
554 goto clear;
555 rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset);
556 rtnh->rtnh_len += RTA_SPACE(FAMILY_ADDRESS_SIZE(m->gateway.family));
557 } else {
558 r = rtattr_append_attribute(rta, RTA_VIA, &m->gateway, FAMILY_ADDRESS_SIZE(m->gateway.family) + sizeof(m->gateway.family));
559 if (r < 0)
560 goto clear;
561 rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset);
562 rtnh->rtnh_len += RTA_SPACE(FAMILY_ADDRESS_SIZE(m->gateway.family) + sizeof(m->gateway.family));
563 }
564
565 return 0;
566
567 clear:
568 (*rta)->rta_len -= sizeof(struct rtnexthop);
569 return r;
570 }
571
572 static int append_nexthops(Route *route, sd_netlink_message *req) {
573 _cleanup_free_ struct rtattr *rta = NULL;
574 struct rtnexthop *rtnh;
575 MultipathRoute *m;
576 size_t offset;
577 int r;
578
579 if (ordered_set_isempty(route->multipath_routes))
580 return 0;
581
582 rta = new(struct rtattr, 1);
583 if (!rta)
584 return -ENOMEM;
585
586 *rta = (struct rtattr) {
587 .rta_type = RTA_MULTIPATH,
588 .rta_len = RTA_LENGTH(0),
589 };
590 offset = (uint8_t *) RTA_DATA(rta) - (uint8_t *) rta;
591
592 ORDERED_SET_FOREACH(m, route->multipath_routes) {
593 r = append_nexthop_one(route, m, &rta, offset);
594 if (r < 0)
595 return r;
596
597 rtnh = (struct rtnexthop *)((uint8_t *) rta + offset);
598 offset = (uint8_t *) RTNH_NEXT(rtnh) - (uint8_t *) rta;
599 }
600
601 r = sd_netlink_message_append_data(req, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta));
602 if (r < 0)
603 return r;
604
605 return 0;
606 }
607
608 int route_configure(
609 Route *route,
610 Link *link,
611 link_netlink_message_handler_t callback,
612 Route **ret) {
613
614 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
615 _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL;
616 int r;
617
618 assert(link);
619 assert(link->manager);
620 assert(link->manager->rtnl);
621 assert(link->ifindex > 0);
622 assert(IN_SET(route->family, AF_INET, AF_INET6));
623 assert(callback);
624
625 if (route_get(link, route, NULL) <= 0 &&
626 set_size(link->routes) >= routes_max())
627 return log_link_error_errno(link, SYNTHETIC_ERRNO(E2BIG),
628 "Too many routes are configured, refusing: %m");
629
630 if (DEBUG_LOGGING) {
631 _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL;
632 char scope[ROUTE_SCOPE_STR_MAX], table[ROUTE_TABLE_STR_MAX], protocol[ROUTE_PROTOCOL_STR_MAX];
633
634 if (!in_addr_is_null(route->family, &route->dst)) {
635 (void) in_addr_to_string(route->family, &route->dst, &dst);
636 (void) asprintf(&dst_prefixlen, "/%u", route->dst_prefixlen);
637 }
638 if (!in_addr_is_null(route->family, &route->src))
639 (void) in_addr_to_string(route->family, &route->src, &src);
640 if (!in_addr_is_null(route->family, &route->gw))
641 (void) in_addr_to_string(route->family, &route->gw, &gw);
642 if (!in_addr_is_null(route->family, &route->prefsrc))
643 (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
644
645 log_link_debug(link, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
646 strna(dst), strempty(dst_prefixlen), strna(src), strna(gw), strna(prefsrc),
647 format_route_scope(route->scope, scope, sizeof(scope)),
648 format_route_table(route->table, table, sizeof(table)),
649 format_route_protocol(route->protocol, protocol, sizeof(protocol)),
650 strna(route_type_to_string(route->type)));
651 }
652
653 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
654 RTM_NEWROUTE, route->family,
655 route->protocol);
656 if (r < 0)
657 return log_link_error_errno(link, r, "Could not create RTM_NEWROUTE message: %m");
658
659 if (in_addr_is_null(route->family, &route->gw) == 0) {
660 r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->family, &route->gw);
661 if (r < 0)
662 return log_link_error_errno(link, r, "Could not append RTA_GATEWAY attribute: %m");
663
664 r = sd_rtnl_message_route_set_family(req, route->family);
665 if (r < 0)
666 return log_link_error_errno(link, r, "Could not set route family: %m");
667 }
668
669 if (route->dst_prefixlen > 0) {
670 r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst);
671 if (r < 0)
672 return log_link_error_errno(link, r, "Could not append RTA_DST attribute: %m");
673
674 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
675 if (r < 0)
676 return log_link_error_errno(link, r, "Could not set destination prefix length: %m");
677 }
678
679 if (route->src_prefixlen > 0) {
680 r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src);
681 if (r < 0)
682 return log_link_error_errno(link, r, "Could not append RTA_SRC attribute: %m");
683
684 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
685 if (r < 0)
686 return log_link_error_errno(link, r, "Could not set source prefix length: %m");
687 }
688
689 if (in_addr_is_null(route->family, &route->prefsrc) == 0) {
690 r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc);
691 if (r < 0)
692 return log_link_error_errno(link, r, "Could not append RTA_PREFSRC attribute: %m");
693 }
694
695 r = sd_rtnl_message_route_set_scope(req, route->scope);
696 if (r < 0)
697 return log_link_error_errno(link, r, "Could not set scope: %m");
698
699 if (route->gateway_onlink >= 0)
700 SET_FLAG(route->flags, RTNH_F_ONLINK, route->gateway_onlink);
701
702 r = sd_rtnl_message_route_set_flags(req, route->flags);
703 if (r < 0)
704 return log_link_error_errno(link, r, "Could not set flags: %m");
705
706 if (route->table != RT_TABLE_MAIN) {
707 if (route->table < 256) {
708 r = sd_rtnl_message_route_set_table(req, route->table);
709 if (r < 0)
710 return log_link_error_errno(link, r, "Could not set route table: %m");
711 } else {
712 r = sd_rtnl_message_route_set_table(req, RT_TABLE_UNSPEC);
713 if (r < 0)
714 return log_link_error_errno(link, r, "Could not set route table: %m");
715
716 /* Table attribute to allow more than 256. */
717 r = sd_netlink_message_append_data(req, RTA_TABLE, &route->table, sizeof(route->table));
718 if (r < 0)
719 return log_link_error_errno(link, r, "Could not append RTA_TABLE attribute: %m");
720 }
721 }
722
723 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
724 if (r < 0)
725 return log_link_error_errno(link, r, "Could not append RTA_PRIORITY attribute: %m");
726
727 r = sd_netlink_message_append_u8(req, RTA_PREF, route->pref);
728 if (r < 0)
729 return log_link_error_errno(link, r, "Could not append RTA_PREF attribute: %m");
730
731 if (route->lifetime != USEC_INFINITY && kernel_route_expiration_supported()) {
732 r = sd_netlink_message_append_u32(req, RTA_EXPIRES,
733 DIV_ROUND_UP(usec_sub_unsigned(route->lifetime, now(clock_boottime_or_monotonic())), USEC_PER_SEC));
734 if (r < 0)
735 return log_link_error_errno(link, r, "Could not append RTA_EXPIRES attribute: %m");
736 }
737
738 r = sd_rtnl_message_route_set_type(req, route->type);
739 if (r < 0)
740 return log_link_error_errno(link, r, "Could not set route type: %m");
741
742 if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW)) {
743 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
744 if (r < 0)
745 return log_link_error_errno(link, r, "Could not append RTA_OIF attribute: %m");
746 }
747
748 if (route->ttl_propagate >= 0) {
749 r = sd_netlink_message_append_u8(req, RTA_TTL_PROPAGATE, route->ttl_propagate);
750 if (r < 0)
751 return log_link_error_errno(link, r, "Could not append RTA_TTL_PROPAGATE attribute: %m");
752 }
753
754 r = sd_netlink_message_open_container(req, RTA_METRICS);
755 if (r < 0)
756 return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m");
757
758 if (route->mtu > 0) {
759 r = sd_netlink_message_append_u32(req, RTAX_MTU, route->mtu);
760 if (r < 0)
761 return log_link_error_errno(link, r, "Could not append RTAX_MTU attribute: %m");
762 }
763
764 if (route->initcwnd > 0) {
765 r = sd_netlink_message_append_u32(req, RTAX_INITCWND, route->initcwnd);
766 if (r < 0)
767 return log_link_error_errno(link, r, "Could not append RTAX_INITCWND attribute: %m");
768 }
769
770 if (route->initrwnd > 0) {
771 r = sd_netlink_message_append_u32(req, RTAX_INITRWND, route->initrwnd);
772 if (r < 0)
773 return log_link_error_errno(link, r, "Could not append RTAX_INITRWND attribute: %m");
774 }
775
776 if (route->quickack >= 0) {
777 r = sd_netlink_message_append_u32(req, RTAX_QUICKACK, route->quickack);
778 if (r < 0)
779 return log_link_error_errno(link, r, "Could not append RTAX_QUICKACK attribute: %m");
780 }
781
782 if (route->fast_open_no_cookie >= 0) {
783 r = sd_netlink_message_append_u32(req, RTAX_FASTOPEN_NO_COOKIE, route->fast_open_no_cookie);
784 if (r < 0)
785 return log_link_error_errno(link, r, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
786 }
787
788 r = sd_netlink_message_close_container(req);
789 if (r < 0)
790 return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m");
791
792 r = append_nexthops(route, req);
793 if (r < 0)
794 return log_link_error_errno(link, r, "Could not append RTA_MULTIPATH attribute: %m");
795
796 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
797 link_netlink_destroy_callback, link);
798 if (r < 0)
799 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
800
801 link_ref(link);
802
803 r = route_add(link, route, &route);
804 if (r < 0)
805 return log_link_error_errno(link, r, "Could not add route: %m");
806
807 /* TODO: drop expiration handling once it can be pushed into the kernel */
808 if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) {
809 r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(),
810 route->lifetime, 0, route_expire_handler, route);
811 if (r < 0)
812 return log_link_error_errno(link, r, "Could not arm expiration timer: %m");
813 }
814
815 sd_event_source_unref(route->expire);
816 route->expire = TAKE_PTR(expire);
817
818 if (ret)
819 *ret = route;
820
821 return 1;
822 }
823
824 int network_add_ipv4ll_route(Network *network) {
825 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
826 int r;
827
828 assert(network);
829
830 if (!network->ipv4ll_route)
831 return 0;
832
833 /* IPv4LLRoute= is in [Network] section. */
834 r = route_new_static(network, NULL, 0, &n);
835 if (r < 0)
836 return r;
837
838 r = in_addr_from_string(AF_INET, "169.254.0.0", &n->dst);
839 if (r < 0)
840 return r;
841
842 n->family = AF_INET;
843 n->dst_prefixlen = 16;
844 n->scope = RT_SCOPE_LINK;
845 n->scope_set = true;
846 n->table_set = true;
847 n->priority = IPV4LL_ROUTE_METRIC;
848 n->protocol = RTPROT_STATIC;
849
850 TAKE_PTR(n);
851 return 0;
852 }
853
854 int network_add_default_route_on_device(Network *network) {
855 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
856 int r;
857
858 assert(network);
859
860 if (!network->default_route_on_device)
861 return 0;
862
863 /* DefaultRouteOnDevice= is in [Network] section. */
864 r = route_new_static(network, NULL, 0, &n);
865 if (r < 0)
866 return r;
867
868 n->family = AF_INET;
869 n->scope = RT_SCOPE_LINK;
870 n->scope_set = true;
871 n->protocol = RTPROT_STATIC;
872
873 TAKE_PTR(n);
874 return 0;
875 }
876
877 static const char * const route_type_table[__RTN_MAX] = {
878 [RTN_UNICAST] = "unicast",
879 [RTN_LOCAL] = "local",
880 [RTN_BROADCAST] = "broadcast",
881 [RTN_ANYCAST] = "anycast",
882 [RTN_MULTICAST] = "multicast",
883 [RTN_BLACKHOLE] = "blackhole",
884 [RTN_UNREACHABLE] = "unreachable",
885 [RTN_PROHIBIT] = "prohibit",
886 [RTN_THROW] = "throw",
887 [RTN_NAT] = "nat",
888 [RTN_XRESOLVE] = "xresolve",
889 };
890
891 assert_cc(__RTN_MAX <= UCHAR_MAX);
892 DEFINE_STRING_TABLE_LOOKUP(route_type, int);
893
894 static const char * const route_scope_table[] = {
895 [RT_SCOPE_UNIVERSE] = "global",
896 [RT_SCOPE_SITE] = "site",
897 [RT_SCOPE_LINK] = "link",
898 [RT_SCOPE_HOST] = "host",
899 [RT_SCOPE_NOWHERE] = "nowhere",
900 };
901
902 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope, int);
903
904 const char *format_route_scope(int scope, char *buf, size_t size) {
905 const char *s;
906 char *p = buf;
907
908 s = route_scope_to_string(scope);
909 if (s)
910 strpcpy(&p, size, s);
911 else
912 strpcpyf(&p, size, "%d", scope);
913
914 return buf;
915 }
916
917 static const char * const route_table_table[] = {
918 [RT_TABLE_DEFAULT] = "default",
919 [RT_TABLE_MAIN] = "main",
920 [RT_TABLE_LOCAL] = "local",
921 };
922
923 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table, int);
924
925 const char *format_route_table(int table, char *buf, size_t size) {
926 const char *s;
927 char *p = buf;
928
929 s = route_table_to_string(table);
930 if (s)
931 strpcpy(&p, size, s);
932 else
933 strpcpyf(&p, size, "%d", table);
934
935 return buf;
936 }
937
938 static const char * const route_protocol_table[] = {
939 [RTPROT_KERNEL] = "kernel",
940 [RTPROT_BOOT] = "boot",
941 [RTPROT_STATIC] = "static",
942 };
943
944 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol, int);
945
946 static const char * const route_protocol_full_table[] = {
947 [RTPROT_REDIRECT] = "redirect",
948 [RTPROT_KERNEL] = "kernel",
949 [RTPROT_BOOT] = "boot",
950 [RTPROT_STATIC] = "static",
951 [RTPROT_GATED] = "gated",
952 [RTPROT_RA] = "ra",
953 [RTPROT_MRT] = "mrt",
954 [RTPROT_ZEBRA] = "zebra",
955 [RTPROT_BIRD] = "bird",
956 [RTPROT_DNROUTED] = "dnrouted",
957 [RTPROT_XORP] = "xorp",
958 [RTPROT_NTK] = "ntk",
959 [RTPROT_DHCP] = "dhcp",
960 [RTPROT_MROUTED] = "mrouted",
961 [RTPROT_BABEL] = "babel",
962 [RTPROT_BGP] = "bgp",
963 [RTPROT_ISIS] = "isis",
964 [RTPROT_OSPF] = "ospf",
965 [RTPROT_RIP] = "rip",
966 [RTPROT_EIGRP] = "eigrp",
967 };
968
969 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full, int);
970
971 const char *format_route_protocol(int protocol, char *buf, size_t size) {
972 const char *s;
973 char *p = buf;
974
975 s = route_protocol_full_to_string(protocol);
976 if (s)
977 strpcpy(&p, size, s);
978 else
979 strpcpyf(&p, size, "%d", protocol);
980
981 return buf;
982 }
983
984 int config_parse_gateway(
985 const char *unit,
986 const char *filename,
987 unsigned line,
988 const char *section,
989 unsigned section_line,
990 const char *lvalue,
991 int ltype,
992 const char *rvalue,
993 void *data,
994 void *userdata) {
995
996 Network *network = userdata;
997 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
998 int r;
999
1000 assert(filename);
1001 assert(section);
1002 assert(lvalue);
1003 assert(rvalue);
1004 assert(data);
1005
1006 if (streq(section, "Network")) {
1007 /* we are not in an Route section, so treat
1008 * this as the special '0' section */
1009 r = route_new_static(network, NULL, 0, &n);
1010 if (r == -ENOMEM)
1011 return log_oom();
1012 if (r < 0) {
1013 log_syntax(unit, LOG_WARNING, filename, line, r,
1014 "Failed to allocate route, ignoring assignment: %m");
1015 return 0;
1016 }
1017 } else {
1018 r = route_new_static(network, filename, section_line, &n);
1019 if (r == -ENOMEM)
1020 return log_oom();
1021 if (r < 0) {
1022 log_syntax(unit, LOG_WARNING, filename, line, r,
1023 "Failed to allocate route, ignoring assignment: %m");
1024 return 0;
1025 }
1026
1027 if (streq(rvalue, "_dhcp")) {
1028 n->gateway_from_dhcp = true;
1029 TAKE_PTR(n);
1030 return 0;
1031 }
1032 }
1033
1034 if (n->family == AF_UNSPEC)
1035 r = in_addr_from_string_auto(rvalue, &n->family, &n->gw);
1036 else
1037 r = in_addr_from_string(n->family, rvalue, &n->gw);
1038 if (r < 0) {
1039 log_syntax(unit, LOG_WARNING, filename, line, r,
1040 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
1041 return 0;
1042 }
1043
1044 TAKE_PTR(n);
1045 return 0;
1046 }
1047
1048 int config_parse_preferred_src(
1049 const char *unit,
1050 const char *filename,
1051 unsigned line,
1052 const char *section,
1053 unsigned section_line,
1054 const char *lvalue,
1055 int ltype,
1056 const char *rvalue,
1057 void *data,
1058 void *userdata) {
1059
1060 Network *network = userdata;
1061 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1062 int r;
1063
1064 assert(filename);
1065 assert(section);
1066 assert(lvalue);
1067 assert(rvalue);
1068 assert(data);
1069
1070 r = route_new_static(network, filename, section_line, &n);
1071 if (r == -ENOMEM)
1072 return log_oom();
1073 if (r < 0) {
1074 log_syntax(unit, LOG_WARNING, filename, line, r,
1075 "Failed to allocate route, ignoring assignment: %m");
1076 return 0;
1077 }
1078
1079 if (n->family == AF_UNSPEC)
1080 r = in_addr_from_string_auto(rvalue, &n->family, &n->prefsrc);
1081 else
1082 r = in_addr_from_string(n->family, rvalue, &n->prefsrc);
1083 if (r < 0) {
1084 log_syntax(unit, LOG_WARNING, filename, line, EINVAL,
1085 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
1086 return 0;
1087 }
1088
1089 TAKE_PTR(n);
1090 return 0;
1091 }
1092
1093 int config_parse_destination(
1094 const char *unit,
1095 const char *filename,
1096 unsigned line,
1097 const char *section,
1098 unsigned section_line,
1099 const char *lvalue,
1100 int ltype,
1101 const char *rvalue,
1102 void *data,
1103 void *userdata) {
1104
1105 Network *network = userdata;
1106 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1107 union in_addr_union *buffer;
1108 unsigned char *prefixlen;
1109 int r;
1110
1111 assert(filename);
1112 assert(section);
1113 assert(lvalue);
1114 assert(rvalue);
1115 assert(data);
1116
1117 r = route_new_static(network, filename, section_line, &n);
1118 if (r == -ENOMEM)
1119 return log_oom();
1120 if (r < 0) {
1121 log_syntax(unit, LOG_WARNING, filename, line, r,
1122 "Failed to allocate route, ignoring assignment: %m");
1123 return 0;
1124 }
1125
1126 if (streq(lvalue, "Destination")) {
1127 buffer = &n->dst;
1128 prefixlen = &n->dst_prefixlen;
1129 } else if (streq(lvalue, "Source")) {
1130 buffer = &n->src;
1131 prefixlen = &n->src_prefixlen;
1132 } else
1133 assert_not_reached(lvalue);
1134
1135 if (n->family == AF_UNSPEC)
1136 r = in_addr_prefix_from_string_auto(rvalue, &n->family, buffer, prefixlen);
1137 else
1138 r = in_addr_prefix_from_string(rvalue, n->family, buffer, prefixlen);
1139 if (r < 0) {
1140 log_syntax(unit, LOG_WARNING, filename, line, EINVAL,
1141 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
1142 return 0;
1143 }
1144
1145 TAKE_PTR(n);
1146 return 0;
1147 }
1148
1149 int config_parse_route_priority(
1150 const char *unit,
1151 const char *filename,
1152 unsigned line,
1153 const char *section,
1154 unsigned section_line,
1155 const char *lvalue,
1156 int ltype,
1157 const char *rvalue,
1158 void *data,
1159 void *userdata) {
1160
1161 Network *network = userdata;
1162 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1163 int r;
1164
1165 assert(filename);
1166 assert(section);
1167 assert(lvalue);
1168 assert(rvalue);
1169 assert(data);
1170
1171 r = route_new_static(network, filename, section_line, &n);
1172 if (r == -ENOMEM)
1173 return log_oom();
1174 if (r < 0) {
1175 log_syntax(unit, LOG_WARNING, filename, line, r,
1176 "Failed to allocate route, ignoring assignment: %m");
1177 return 0;
1178 }
1179
1180 r = safe_atou32(rvalue, &n->priority);
1181 if (r < 0) {
1182 log_syntax(unit, LOG_WARNING, filename, line, r,
1183 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue);
1184 return 0;
1185 }
1186
1187 TAKE_PTR(n);
1188 return 0;
1189 }
1190
1191 int config_parse_route_scope(
1192 const char *unit,
1193 const char *filename,
1194 unsigned line,
1195 const char *section,
1196 unsigned section_line,
1197 const char *lvalue,
1198 int ltype,
1199 const char *rvalue,
1200 void *data,
1201 void *userdata) {
1202
1203 Network *network = userdata;
1204 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1205 int r;
1206
1207 assert(filename);
1208 assert(section);
1209 assert(lvalue);
1210 assert(rvalue);
1211 assert(data);
1212
1213 r = route_new_static(network, filename, section_line, &n);
1214 if (r == -ENOMEM)
1215 return log_oom();
1216 if (r < 0) {
1217 log_syntax(unit, LOG_WARNING, filename, line, r,
1218 "Failed to allocate route, ignoring assignment: %m");
1219 return 0;
1220 }
1221
1222 r = route_scope_from_string(rvalue);
1223 if (r < 0) {
1224 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown route scope: %s", rvalue);
1225 return 0;
1226 }
1227
1228 n->scope = r;
1229 n->scope_set = true;
1230 TAKE_PTR(n);
1231 return 0;
1232 }
1233
1234 int config_parse_route_table(
1235 const char *unit,
1236 const char *filename,
1237 unsigned line,
1238 const char *section,
1239 unsigned section_line,
1240 const char *lvalue,
1241 int ltype,
1242 const char *rvalue,
1243 void *data,
1244 void *userdata) {
1245
1246 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1247 Network *network = userdata;
1248 int r;
1249
1250 assert(filename);
1251 assert(section);
1252 assert(lvalue);
1253 assert(rvalue);
1254 assert(data);
1255
1256 r = route_new_static(network, filename, section_line, &n);
1257 if (r == -ENOMEM)
1258 return log_oom();
1259 if (r < 0) {
1260 log_syntax(unit, LOG_WARNING, filename, line, r,
1261 "Failed to allocate route, ignoring assignment: %m");
1262 return 0;
1263 }
1264
1265 r = route_table_from_string(rvalue);
1266 if (r >= 0)
1267 n->table = r;
1268 else {
1269 r = safe_atou32(rvalue, &n->table);
1270 if (r < 0) {
1271 log_syntax(unit, LOG_WARNING, filename, line, r,
1272 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue);
1273 return 0;
1274 }
1275 }
1276
1277 n->table_set = true;
1278 TAKE_PTR(n);
1279 return 0;
1280 }
1281
1282 int config_parse_route_boolean(
1283 const char *unit,
1284 const char *filename,
1285 unsigned line,
1286 const char *section,
1287 unsigned section_line,
1288 const char *lvalue,
1289 int ltype,
1290 const char *rvalue,
1291 void *data,
1292 void *userdata) {
1293
1294 Network *network = userdata;
1295 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1296 int r;
1297
1298 assert(filename);
1299 assert(section);
1300 assert(lvalue);
1301 assert(rvalue);
1302 assert(data);
1303
1304 r = route_new_static(network, filename, section_line, &n);
1305 if (r == -ENOMEM)
1306 return log_oom();
1307 if (r < 0) {
1308 log_syntax(unit, LOG_WARNING, filename, line, r,
1309 "Failed to allocate route, ignoring assignment: %m");
1310 return 0;
1311 }
1312
1313 r = parse_boolean(rvalue);
1314 if (r < 0) {
1315 log_syntax(unit, LOG_WARNING, filename, line, r,
1316 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue, rvalue);
1317 return 0;
1318 }
1319
1320 if (STR_IN_SET(lvalue, "GatewayOnLink", "GatewayOnlink"))
1321 n->gateway_onlink = r;
1322 else if (streq(lvalue, "QuickAck"))
1323 n->quickack = r;
1324 else if (streq(lvalue, "FastOpenNoCookie"))
1325 n->fast_open_no_cookie = r;
1326 else if (streq(lvalue, "TTLPropagate"))
1327 n->ttl_propagate = r;
1328 else
1329 assert_not_reached("Invalid lvalue");
1330
1331 TAKE_PTR(n);
1332 return 0;
1333 }
1334
1335 int config_parse_ipv6_route_preference(
1336 const char *unit,
1337 const char *filename,
1338 unsigned line,
1339 const char *section,
1340 unsigned section_line,
1341 const char *lvalue,
1342 int ltype,
1343 const char *rvalue,
1344 void *data,
1345 void *userdata) {
1346
1347 Network *network = userdata;
1348 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1349 int r;
1350
1351 r = route_new_static(network, filename, section_line, &n);
1352 if (r == -ENOMEM)
1353 return log_oom();
1354 if (r < 0) {
1355 log_syntax(unit, LOG_WARNING, filename, line, r,
1356 "Failed to allocate route, ignoring assignment: %m");
1357 return 0;
1358 }
1359
1360 if (streq(rvalue, "low"))
1361 n->pref = ICMPV6_ROUTER_PREF_LOW;
1362 else if (streq(rvalue, "medium"))
1363 n->pref = ICMPV6_ROUTER_PREF_MEDIUM;
1364 else if (streq(rvalue, "high"))
1365 n->pref = ICMPV6_ROUTER_PREF_HIGH;
1366 else {
1367 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown route preference: %s", rvalue);
1368 return 0;
1369 }
1370
1371 TAKE_PTR(n);
1372 return 0;
1373 }
1374
1375 int config_parse_route_protocol(
1376 const char *unit,
1377 const char *filename,
1378 unsigned line,
1379 const char *section,
1380 unsigned section_line,
1381 const char *lvalue,
1382 int ltype,
1383 const char *rvalue,
1384 void *data,
1385 void *userdata) {
1386
1387 Network *network = userdata;
1388 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1389 int r;
1390
1391 r = route_new_static(network, filename, section_line, &n);
1392 if (r == -ENOMEM)
1393 return log_oom();
1394 if (r < 0) {
1395 log_syntax(unit, LOG_WARNING, filename, line, r,
1396 "Failed to allocate route, ignoring assignment: %m");
1397 return 0;
1398 }
1399
1400 r = route_protocol_from_string(rvalue);
1401 if (r >= 0)
1402 n->protocol = r;
1403 else {
1404 r = safe_atou8(rvalue , &n->protocol);
1405 if (r < 0) {
1406 log_syntax(unit, LOG_WARNING, filename, line, r,
1407 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue);
1408 return 0;
1409 }
1410 }
1411
1412 TAKE_PTR(n);
1413 return 0;
1414 }
1415
1416 int config_parse_route_type(
1417 const char *unit,
1418 const char *filename,
1419 unsigned line,
1420 const char *section,
1421 unsigned section_line,
1422 const char *lvalue,
1423 int ltype,
1424 const char *rvalue,
1425 void *data,
1426 void *userdata) {
1427
1428 Network *network = userdata;
1429 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1430 int t, r;
1431
1432 r = route_new_static(network, filename, section_line, &n);
1433 if (r == -ENOMEM)
1434 return log_oom();
1435 if (r < 0) {
1436 log_syntax(unit, LOG_WARNING, filename, line, r,
1437 "Failed to allocate route, ignoring assignment: %m");
1438 return 0;
1439 }
1440
1441 t = route_type_from_string(rvalue);
1442 if (t < 0) {
1443 log_syntax(unit, LOG_WARNING, filename, line, 0,
1444 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue);
1445 return 0;
1446 }
1447
1448 n->type = (unsigned char) t;
1449
1450 TAKE_PTR(n);
1451 return 0;
1452 }
1453
1454 int config_parse_tcp_window(
1455 const char *unit,
1456 const char *filename,
1457 unsigned line,
1458 const char *section,
1459 unsigned section_line,
1460 const char *lvalue,
1461 int ltype,
1462 const char *rvalue,
1463 void *data,
1464 void *userdata) {
1465
1466 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1467 Network *network = userdata;
1468 uint64_t k;
1469 int r;
1470
1471 assert(filename);
1472 assert(section);
1473 assert(lvalue);
1474 assert(rvalue);
1475 assert(data);
1476
1477 r = route_new_static(network, filename, section_line, &n);
1478 if (r == -ENOMEM)
1479 return log_oom();
1480 if (r < 0) {
1481 log_syntax(unit, LOG_WARNING, filename, line, r,
1482 "Failed to allocate route, ignoring assignment: %m");
1483 return 0;
1484 }
1485
1486 r = parse_size(rvalue, 1024, &k);
1487 if (r < 0) {
1488 log_syntax(unit, LOG_WARNING, filename, line, r,
1489 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue, rvalue);
1490 return 0;
1491 }
1492 if (k > UINT32_MAX) {
1493 log_syntax(unit, LOG_WARNING, filename, line, 0,
1494 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue, rvalue);
1495 return 0;
1496 }
1497
1498 if (streq(lvalue, "InitialCongestionWindow"))
1499 n->initcwnd = k;
1500 else if (streq(lvalue, "InitialAdvertisedReceiveWindow"))
1501 n->initrwnd = k;
1502 else
1503 assert_not_reached("Invalid TCP window type.");
1504
1505 TAKE_PTR(n);
1506 return 0;
1507 }
1508
1509 int config_parse_route_mtu(
1510 const char *unit,
1511 const char *filename,
1512 unsigned line,
1513 const char *section,
1514 unsigned section_line,
1515 const char *lvalue,
1516 int ltype,
1517 const char *rvalue,
1518 void *data,
1519 void *userdata) {
1520
1521 Network *network = userdata;
1522 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1523 int r;
1524
1525 assert(filename);
1526 assert(section);
1527 assert(lvalue);
1528 assert(rvalue);
1529 assert(data);
1530
1531 r = route_new_static(network, filename, section_line, &n);
1532 if (r == -ENOMEM)
1533 return log_oom();
1534 if (r < 0) {
1535 log_syntax(unit, LOG_WARNING, filename, line, r,
1536 "Failed to allocate route, ignoring assignment: %m");
1537 return 0;
1538 }
1539
1540 r = config_parse_mtu(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &n->mtu, userdata);
1541 if (r < 0)
1542 return r;
1543
1544 TAKE_PTR(n);
1545 return 0;
1546 }
1547
1548 int config_parse_multipath_route(
1549 const char *unit,
1550 const char *filename,
1551 unsigned line,
1552 const char *section,
1553 unsigned section_line,
1554 const char *lvalue,
1555 int ltype,
1556 const char *rvalue,
1557 void *data,
1558 void *userdata) {
1559
1560 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1561 _cleanup_free_ char *word = NULL, *buf = NULL;
1562 _cleanup_free_ MultipathRoute *m = NULL;
1563 Network *network = userdata;
1564 const char *p, *ip, *dev;
1565 union in_addr_union a;
1566 int family, r;
1567
1568 assert(filename);
1569 assert(section);
1570 assert(lvalue);
1571 assert(rvalue);
1572 assert(data);
1573
1574 r = route_new_static(network, filename, section_line, &n);
1575 if (r == -ENOMEM)
1576 return log_oom();
1577 if (r < 0) {
1578 log_syntax(unit, LOG_WARNING, filename, line, r,
1579 "Failed to allocate route, ignoring assignment: %m");
1580 return 0;
1581 }
1582
1583 if (isempty(rvalue)) {
1584 n->multipath_routes = ordered_set_free_free(n->multipath_routes);
1585 return 0;
1586 }
1587
1588 m = new0(MultipathRoute, 1);
1589 if (!m)
1590 return log_oom();
1591
1592 p = rvalue;
1593 r = extract_first_word(&p, &word, NULL, 0);
1594 if (r == -ENOMEM)
1595 return log_oom();
1596 if (r <= 0) {
1597 log_syntax(unit, LOG_WARNING, filename, line, r,
1598 "Invalid multipath route option, ignoring assignment: %s", rvalue);
1599 return 0;
1600 }
1601
1602 dev = strchr(word, '@');
1603 if (dev) {
1604 buf = strndup(word, dev - word);
1605 if (!buf)
1606 return log_oom();
1607 ip = buf;
1608 dev++;
1609 } else
1610 ip = word;
1611
1612 r = in_addr_from_string_auto(ip, &family, &a);
1613 if (r < 0) {
1614 log_syntax(unit, LOG_WARNING, filename, line, r,
1615 "Invalid multipath route gateway '%s', ignoring assignment: %m", rvalue);
1616 return 0;
1617 }
1618 m->gateway.address = a;
1619 m->gateway.family = family;
1620
1621 if (dev) {
1622 r = resolve_interface(NULL, dev);
1623 if (r < 0) {
1624 log_syntax(unit, LOG_WARNING, filename, line, r,
1625 "Invalid interface name or index, ignoring assignment: %s", dev);
1626 return 0;
1627 }
1628 m->ifindex = r;
1629 }
1630
1631 if (!isempty(p)) {
1632 r = safe_atou32(p, &m->weight);
1633 if (r < 0) {
1634 log_syntax(unit, LOG_WARNING, filename, line, r,
1635 "Invalid multipath route weight, ignoring assignment: %s", p);
1636 return 0;
1637 }
1638 if (m->weight == 0 || m->weight > 256) {
1639 log_syntax(unit, LOG_WARNING, filename, line, 0,
1640 "Invalid multipath route weight, ignoring assignment: %s", p);
1641 return 0;
1642 }
1643 }
1644
1645 r = ordered_set_ensure_allocated(&n->multipath_routes, NULL);
1646 if (r < 0)
1647 return log_oom();
1648
1649 r = ordered_set_put(n->multipath_routes, m);
1650 if (r < 0) {
1651 log_syntax(unit, LOG_WARNING, filename, line, r,
1652 "Failed to store multipath route, ignoring assignment: %m");
1653 return 0;
1654 }
1655
1656 TAKE_PTR(m);
1657 TAKE_PTR(n);
1658 return 0;
1659 }
1660
1661 int route_section_verify(Route *route, Network *network) {
1662 if (section_is_invalid(route->section))
1663 return -EINVAL;
1664
1665 if (route->family == AF_UNSPEC) {
1666 assert(route->section);
1667
1668 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1669 "%s: Route section without Gateway=, Destination=, Source=, "
1670 "or PreferredSource= field configured. "
1671 "Ignoring [Route] section from line %u.",
1672 route->section->filename, route->section->line);
1673 }
1674
1675 if (!route->table_set && network->vrf) {
1676 route->table = VRF(network->vrf)->table;
1677 route->table_set = true;
1678 }
1679
1680 if (!route->table_set && IN_SET(route->type, RTN_LOCAL, RTN_BROADCAST, RTN_ANYCAST, RTN_NAT))
1681 route->table = RT_TABLE_LOCAL;
1682
1683 if (!route->scope_set && route->family != AF_INET6) {
1684 if (IN_SET(route->type, RTN_LOCAL, RTN_NAT))
1685 route->scope = RT_SCOPE_HOST;
1686 else if (IN_SET(route->type, RTN_BROADCAST, RTN_ANYCAST, RTN_MULTICAST))
1687 route->scope = RT_SCOPE_LINK;
1688 }
1689
1690 if (network->n_static_addresses == 0 &&
1691 in_addr_is_null(route->family, &route->gw) == 0 &&
1692 route->gateway_onlink < 0) {
1693 log_warning("%s: Gateway= without static address configured. "
1694 "Enabling GatewayOnLink= option.",
1695 network->filename);
1696 route->gateway_onlink = true;
1697 }
1698
1699 return 0;
1700 }