]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f579559b | 2 | |
b5bf6f64 | 3 | #include <linux/icmpv6.h> |
8973df5c | 4 | #include <linux/ipv6_route.h> |
b5bf6f64 | 5 | |
b5efdb8a | 6 | #include "alloc-util.h" |
fc2f9534 | 7 | #include "netlink-util.h" |
ca5ad760 | 8 | #include "networkd-ipv4ll.h" |
23f53b99 | 9 | #include "networkd-manager.h" |
e2263711 | 10 | #include "networkd-network.h" |
141318f7 | 11 | #include "networkd-nexthop.h" |
6bedfcbb | 12 | #include "networkd-route.h" |
141318f7 | 13 | #include "networkd-routing-policy-rule.h" |
6bedfcbb | 14 | #include "parse-util.h" |
d308bb99 | 15 | #include "socket-netlink.h" |
7a22312d | 16 | #include "string-table.h" |
07630cea | 17 | #include "string-util.h" |
d96edb2c | 18 | #include "strv.h" |
b297e0a7 | 19 | #include "strxcpyx.h" |
47d2d30d | 20 | #include "sysctl-util.h" |
c0d48bc5 | 21 | #include "vrf.h" |
f579559b | 22 | |
47d2d30d ZJS |
23 | #define ROUTES_DEFAULT_MAX_PER_FAMILY 4096U |
24 | ||
ac49887e YW |
25 | static uint32_t link_get_vrf_table(Link *link) { |
26 | return link->network->vrf ? VRF(link->network->vrf)->table : RT_TABLE_MAIN; | |
27 | } | |
28 | ||
29 | uint32_t link_get_dhcp_route_table(Link *link) { | |
30 | /* When the interface is part of an VRF use the VRFs routing table, unless | |
31 | * another table is explicitly specified. */ | |
32 | if (link->network->dhcp_route_table_set) | |
33 | return link->network->dhcp_route_table; | |
34 | return link_get_vrf_table(link); | |
35 | } | |
36 | ||
37 | uint32_t link_get_ipv6_accept_ra_route_table(Link *link) { | |
38 | if (link->network->ipv6_accept_ra_route_table_set) | |
39 | return link->network->ipv6_accept_ra_route_table; | |
40 | return link_get_vrf_table(link); | |
41 | } | |
42 | ||
74154c2e YW |
43 | static const char * const route_type_table[__RTN_MAX] = { |
44 | [RTN_UNICAST] = "unicast", | |
45 | [RTN_LOCAL] = "local", | |
46 | [RTN_BROADCAST] = "broadcast", | |
47 | [RTN_ANYCAST] = "anycast", | |
48 | [RTN_MULTICAST] = "multicast", | |
49 | [RTN_BLACKHOLE] = "blackhole", | |
50 | [RTN_UNREACHABLE] = "unreachable", | |
51 | [RTN_PROHIBIT] = "prohibit", | |
52 | [RTN_THROW] = "throw", | |
53 | [RTN_NAT] = "nat", | |
54 | [RTN_XRESOLVE] = "xresolve", | |
55 | }; | |
56 | ||
57 | assert_cc(__RTN_MAX <= UCHAR_MAX); | |
58 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_type, int); | |
59 | ||
60 | static const char * const route_scope_table[] = { | |
61 | [RT_SCOPE_UNIVERSE] = "global", | |
62 | [RT_SCOPE_SITE] = "site", | |
63 | [RT_SCOPE_LINK] = "link", | |
64 | [RT_SCOPE_HOST] = "host", | |
65 | [RT_SCOPE_NOWHERE] = "nowhere", | |
66 | }; | |
67 | ||
68 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope, int); | |
69 | ||
70 | #define ROUTE_SCOPE_STR_MAX CONST_MAX(DECIMAL_STR_MAX(int), STRLEN("nowhere") + 1) | |
71 | static const char *format_route_scope(int scope, char *buf, size_t size) { | |
72 | const char *s; | |
73 | char *p = buf; | |
74 | ||
75 | s = route_scope_to_string(scope); | |
76 | if (s) | |
77 | strpcpy(&p, size, s); | |
78 | else | |
79 | strpcpyf(&p, size, "%d", scope); | |
80 | ||
81 | return buf; | |
82 | } | |
83 | ||
84 | static const char * const route_table_table[] = { | |
85 | [RT_TABLE_DEFAULT] = "default", | |
86 | [RT_TABLE_MAIN] = "main", | |
87 | [RT_TABLE_LOCAL] = "local", | |
88 | }; | |
89 | ||
90 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table, int); | |
91 | ||
92 | #define ROUTE_TABLE_STR_MAX CONST_MAX(DECIMAL_STR_MAX(int), STRLEN("default") + 1) | |
93 | static const char *format_route_table(int table, char *buf, size_t size) { | |
94 | const char *s; | |
95 | char *p = buf; | |
96 | ||
97 | s = route_table_to_string(table); | |
98 | if (s) | |
99 | strpcpy(&p, size, s); | |
100 | else | |
101 | strpcpyf(&p, size, "%d", table); | |
102 | ||
103 | return buf; | |
104 | } | |
105 | ||
106 | static const char * const route_protocol_table[] = { | |
107 | [RTPROT_KERNEL] = "kernel", | |
108 | [RTPROT_BOOT] = "boot", | |
109 | [RTPROT_STATIC] = "static", | |
110 | }; | |
111 | ||
112 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol, int); | |
113 | ||
114 | static const char * const route_protocol_full_table[] = { | |
115 | [RTPROT_REDIRECT] = "redirect", | |
116 | [RTPROT_KERNEL] = "kernel", | |
117 | [RTPROT_BOOT] = "boot", | |
118 | [RTPROT_STATIC] = "static", | |
119 | [RTPROT_GATED] = "gated", | |
120 | [RTPROT_RA] = "ra", | |
121 | [RTPROT_MRT] = "mrt", | |
122 | [RTPROT_ZEBRA] = "zebra", | |
123 | [RTPROT_BIRD] = "bird", | |
124 | [RTPROT_DNROUTED] = "dnrouted", | |
125 | [RTPROT_XORP] = "xorp", | |
126 | [RTPROT_NTK] = "ntk", | |
127 | [RTPROT_DHCP] = "dhcp", | |
128 | [RTPROT_MROUTED] = "mrouted", | |
129 | [RTPROT_BABEL] = "babel", | |
130 | [RTPROT_BGP] = "bgp", | |
131 | [RTPROT_ISIS] = "isis", | |
132 | [RTPROT_OSPF] = "ospf", | |
133 | [RTPROT_RIP] = "rip", | |
134 | [RTPROT_EIGRP] = "eigrp", | |
135 | }; | |
136 | ||
137 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full, int); | |
138 | ||
139 | #define ROUTE_PROTOCOL_STR_MAX CONST_MAX(DECIMAL_STR_MAX(int), STRLEN("redirect") + 1) | |
140 | static const char *format_route_protocol(int protocol, char *buf, size_t size) { | |
141 | const char *s; | |
142 | char *p = buf; | |
143 | ||
144 | s = route_protocol_full_to_string(protocol); | |
145 | if (s) | |
146 | strpcpy(&p, size, s); | |
147 | else | |
148 | strpcpyf(&p, size, "%d", protocol); | |
149 | ||
150 | return buf; | |
151 | } | |
152 | ||
47d2d30d ZJS |
153 | static unsigned routes_max(void) { |
154 | static thread_local unsigned cached = 0; | |
155 | ||
156 | _cleanup_free_ char *s4 = NULL, *s6 = NULL; | |
157 | unsigned val4 = ROUTES_DEFAULT_MAX_PER_FAMILY, val6 = ROUTES_DEFAULT_MAX_PER_FAMILY; | |
158 | ||
159 | if (cached > 0) | |
160 | return cached; | |
161 | ||
162 | if (sysctl_read("net/ipv4/route/max_size", &s4) >= 0) { | |
163 | truncate_nl(s4); | |
164 | if (safe_atou(s4, &val4) >= 0 && | |
165 | val4 == 2147483647U) | |
166 | /* This is the default "no limit" value in the kernel */ | |
167 | val4 = ROUTES_DEFAULT_MAX_PER_FAMILY; | |
168 | } | |
169 | ||
170 | if (sysctl_read("net/ipv6/route/max_size", &s6) >= 0) { | |
171 | truncate_nl(s6); | |
172 | (void) safe_atou(s6, &val6); | |
173 | } | |
174 | ||
175 | cached = MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val4) + | |
176 | MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val6); | |
177 | return cached; | |
178 | } | |
8c34b963 | 179 | |
ed9e361a | 180 | int route_new(Route **ret) { |
8e766630 | 181 | _cleanup_(route_freep) Route *route = NULL; |
f0213e37 | 182 | |
17f9c355 | 183 | route = new(Route, 1); |
f0213e37 TG |
184 | if (!route) |
185 | return -ENOMEM; | |
186 | ||
17f9c355 YW |
187 | *route = (Route) { |
188 | .family = AF_UNSPEC, | |
189 | .scope = RT_SCOPE_UNIVERSE, | |
190 | .protocol = RTPROT_UNSPEC, | |
191 | .type = RTN_UNICAST, | |
192 | .table = RT_TABLE_MAIN, | |
193 | .lifetime = USEC_INFINITY, | |
194 | .quickack = -1, | |
633c7258 | 195 | .fast_open_no_cookie = -1, |
54901fd2 | 196 | .gateway_onlink = -1, |
9b88f20a | 197 | .ttl_propagate = -1, |
17f9c355 | 198 | }; |
f0213e37 | 199 | |
1cc6c93a | 200 | *ret = TAKE_PTR(route); |
f0213e37 TG |
201 | |
202 | return 0; | |
203 | } | |
204 | ||
9560e5b3 | 205 | static int route_new_static(Network *network, const char *filename, unsigned section_line, Route **ret) { |
8e766630 LP |
206 | _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL; |
207 | _cleanup_(route_freep) Route *route = NULL; | |
f0213e37 | 208 | int r; |
f579559b | 209 | |
8c34b963 LP |
210 | assert(network); |
211 | assert(ret); | |
2a54a044 YW |
212 | assert(filename); |
213 | assert(section_line > 0); | |
f4859fc7 | 214 | |
2a54a044 YW |
215 | r = network_config_section_new(filename, section_line, &n); |
216 | if (r < 0) | |
217 | return r; | |
6ae115c1 | 218 | |
2a54a044 YW |
219 | route = hashmap_get(network->routes_by_section, n); |
220 | if (route) { | |
221 | *ret = TAKE_PTR(route); | |
222 | return 0; | |
6ae115c1 TG |
223 | } |
224 | ||
2a54a044 | 225 | if (hashmap_size(network->routes_by_section) >= routes_max()) |
8c34b963 LP |
226 | return -E2BIG; |
227 | ||
ed9e361a | 228 | r = route_new(&route); |
f0213e37 TG |
229 | if (r < 0) |
230 | return r; | |
801bd9e8 | 231 | |
ed9e361a | 232 | route->protocol = RTPROT_STATIC; |
0f7f2769 | 233 | route->network = network; |
2a54a044 | 234 | route->section = TAKE_PTR(n); |
cacc1dbf | 235 | |
2a54a044 YW |
236 | r = hashmap_ensure_allocated(&network->routes_by_section, &network_config_hash_ops); |
237 | if (r < 0) | |
238 | return r; | |
3e570042 | 239 | |
2a54a044 YW |
240 | r = hashmap_put(network->routes_by_section, route->section, route); |
241 | if (r < 0) | |
242 | return r; | |
6ae115c1 | 243 | |
1cc6c93a | 244 | *ret = TAKE_PTR(route); |
f579559b TG |
245 | return 0; |
246 | } | |
247 | ||
169948e9 | 248 | Route *route_free(Route *route) { |
f579559b | 249 | if (!route) |
169948e9 | 250 | return NULL; |
f579559b | 251 | |
f048a16b | 252 | if (route->network) { |
2a54a044 YW |
253 | assert(route->section); |
254 | hashmap_remove(route->network->routes_by_section, route->section); | |
f048a16b | 255 | } |
6ae115c1 | 256 | |
fd45e522 ZJS |
257 | network_config_section_free(route->section); |
258 | ||
1c8e710c | 259 | if (route->link) { |
50550722 | 260 | NDiscRoute *n; |
50550722 | 261 | |
1c8e710c TG |
262 | set_remove(route->link->routes, route); |
263 | set_remove(route->link->routes_foreign, route); | |
6e537f62 YW |
264 | set_remove(route->link->dhcp_routes, route); |
265 | set_remove(route->link->dhcp_routes_old, route); | |
1633c457 YW |
266 | set_remove(route->link->dhcp6_routes, route); |
267 | set_remove(route->link->dhcp6_routes_old, route); | |
268 | set_remove(route->link->dhcp6_pd_routes, route); | |
269 | set_remove(route->link->dhcp6_pd_routes_old, route); | |
90e74a66 | 270 | SET_FOREACH(n, route->link->ndisc_routes) |
50550722 YW |
271 | if (n->route == route) |
272 | free(set_remove(route->link->ndisc_routes, n)); | |
1c8e710c TG |
273 | } |
274 | ||
ad208fac YW |
275 | if (route->manager) { |
276 | set_remove(route->manager->routes, route); | |
277 | set_remove(route->manager->routes_foreign, route); | |
278 | } | |
279 | ||
6ff5cc6b YW |
280 | ordered_set_free_free(route->multipath_routes); |
281 | ||
f833694d TG |
282 | sd_event_source_unref(route->expire); |
283 | ||
169948e9 | 284 | return mfree(route); |
f579559b TG |
285 | } |
286 | ||
501b09db | 287 | void route_hash_func(const Route *route, struct siphash *state) { |
bb7ae737 TG |
288 | assert(route); |
289 | ||
290 | siphash24_compress(&route->family, sizeof(route->family), state); | |
291 | ||
01aaa3df YW |
292 | switch (route->family) { |
293 | case AF_INET: | |
294 | case AF_INET6: | |
01aaa3df | 295 | siphash24_compress(&route->dst_prefixlen, sizeof(route->dst_prefixlen), state); |
67e05dd8 ZJS |
296 | siphash24_compress(&route->dst, FAMILY_ADDRESS_SIZE(route->family), state); |
297 | ||
01aaa3df | 298 | siphash24_compress(&route->src_prefixlen, sizeof(route->src_prefixlen), state); |
67e05dd8 ZJS |
299 | siphash24_compress(&route->src, FAMILY_ADDRESS_SIZE(route->family), state); |
300 | ||
6dd53981 YW |
301 | siphash24_compress(&route->gw_family, sizeof(route->gw_family), state); |
302 | if (IN_SET(route->gw_family, AF_INET, AF_INET6)) | |
303 | siphash24_compress(&route->gw, FAMILY_ADDRESS_SIZE(route->gw_family), state); | |
304 | ||
67e05dd8 | 305 | |
01aaa3df YW |
306 | siphash24_compress(&route->prefsrc, FAMILY_ADDRESS_SIZE(route->family), state); |
307 | ||
308 | siphash24_compress(&route->tos, sizeof(route->tos), state); | |
309 | siphash24_compress(&route->priority, sizeof(route->priority), state); | |
310 | siphash24_compress(&route->table, sizeof(route->table), state); | |
311 | siphash24_compress(&route->protocol, sizeof(route->protocol), state); | |
312 | siphash24_compress(&route->scope, sizeof(route->scope), state); | |
313 | siphash24_compress(&route->type, sizeof(route->type), state); | |
67e05dd8 | 314 | |
fa3e401a YW |
315 | siphash24_compress(&route->initcwnd, sizeof(route->initcwnd), state); |
316 | siphash24_compress(&route->initrwnd, sizeof(route->initrwnd), state); | |
01aaa3df YW |
317 | |
318 | break; | |
319 | default: | |
320 | /* treat any other address family as AF_UNSPEC */ | |
321 | break; | |
322 | } | |
323 | } | |
324 | ||
501b09db | 325 | int route_compare_func(const Route *a, const Route *b) { |
01aaa3df YW |
326 | int r; |
327 | ||
328 | r = CMP(a->family, b->family); | |
329 | if (r != 0) | |
330 | return r; | |
331 | ||
332 | switch (a->family) { | |
333 | case AF_INET: | |
334 | case AF_INET6: | |
335 | r = CMP(a->dst_prefixlen, b->dst_prefixlen); | |
336 | if (r != 0) | |
337 | return r; | |
338 | ||
67e05dd8 ZJS |
339 | r = memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family)); |
340 | if (r != 0) | |
341 | return r; | |
342 | ||
01aaa3df YW |
343 | r = CMP(a->src_prefixlen, b->src_prefixlen); |
344 | if (r != 0) | |
345 | return r; | |
346 | ||
67e05dd8 | 347 | r = memcmp(&a->src, &b->src, FAMILY_ADDRESS_SIZE(a->family)); |
01aaa3df YW |
348 | if (r != 0) |
349 | return r; | |
350 | ||
6dd53981 | 351 | r = CMP(a->gw_family, b->gw_family); |
01aaa3df YW |
352 | if (r != 0) |
353 | return r; | |
354 | ||
6dd53981 YW |
355 | if (IN_SET(a->gw_family, AF_INET, AF_INET6)) { |
356 | r = memcmp(&a->gw, &b->gw, FAMILY_ADDRESS_SIZE(a->family)); | |
357 | if (r != 0) | |
358 | return r; | |
359 | } | |
360 | ||
67e05dd8 | 361 | r = memcmp(&a->prefsrc, &b->prefsrc, FAMILY_ADDRESS_SIZE(a->family)); |
01aaa3df YW |
362 | if (r != 0) |
363 | return r; | |
364 | ||
67e05dd8 | 365 | r = CMP(a->tos, b->tos); |
01aaa3df YW |
366 | if (r != 0) |
367 | return r; | |
368 | ||
67e05dd8 | 369 | r = CMP(a->priority, b->priority); |
01aaa3df YW |
370 | if (r != 0) |
371 | return r; | |
372 | ||
67e05dd8 | 373 | r = CMP(a->table, b->table); |
01aaa3df YW |
374 | if (r != 0) |
375 | return r; | |
376 | ||
67e05dd8 | 377 | r = CMP(a->protocol, b->protocol); |
fa3e401a YW |
378 | if (r != 0) |
379 | return r; | |
380 | ||
67e05dd8 | 381 | r = CMP(a->scope, b->scope); |
fa3e401a YW |
382 | if (r != 0) |
383 | return r; | |
384 | ||
67e05dd8 | 385 | r = CMP(a->type, b->type); |
01aaa3df YW |
386 | if (r != 0) |
387 | return r; | |
388 | ||
67e05dd8 | 389 | r = CMP(a->initcwnd, b->initcwnd); |
01aaa3df YW |
390 | if (r != 0) |
391 | return r; | |
392 | ||
67e05dd8 | 393 | r = CMP(a->initrwnd, b->initrwnd); |
01aaa3df YW |
394 | if (r != 0) |
395 | return r; | |
396 | ||
67e05dd8 | 397 | return 0; |
01aaa3df YW |
398 | default: |
399 | /* treat any other address family as AF_UNSPEC */ | |
400 | return 0; | |
401 | } | |
402 | } | |
403 | ||
404 | DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR( | |
c077a205 | 405 | route_hash_ops, |
01aaa3df | 406 | Route, |
c077a205 YW |
407 | route_hash_func, |
408 | route_compare_func, | |
01aaa3df YW |
409 | route_free); |
410 | ||
74154c2e | 411 | static bool route_equal(Route *r1, Route *r2) { |
7ecf0c3e TJ |
412 | if (r1 == r2) |
413 | return true; | |
414 | ||
415 | if (!r1 || !r2) | |
416 | return false; | |
417 | ||
418 | return route_compare_func(r1, r2) == 0; | |
419 | } | |
420 | ||
ad208fac | 421 | static int route_get(Manager *manager, Link *link, Route *in, Route **ret) { |
f1368755 | 422 | Route *existing; |
1b566071 | 423 | |
ad208fac | 424 | assert(manager || link); |
f1368755 | 425 | assert(in); |
1c8e710c | 426 | |
ad208fac YW |
427 | if (link) { |
428 | existing = set_get(link->routes, in); | |
429 | if (existing) { | |
430 | if (ret) | |
431 | *ret = existing; | |
432 | return 1; | |
433 | } | |
1c8e710c | 434 | |
ad208fac YW |
435 | existing = set_get(link->routes_foreign, in); |
436 | if (existing) { | |
437 | if (ret) | |
438 | *ret = existing; | |
439 | return 0; | |
440 | } | |
441 | } else { | |
442 | existing = set_get(manager->routes, in); | |
443 | if (existing) { | |
444 | if (ret) | |
445 | *ret = existing; | |
446 | return 1; | |
447 | } | |
448 | ||
449 | existing = set_get(manager->routes_foreign, in); | |
450 | if (existing) { | |
451 | if (ret) | |
452 | *ret = existing; | |
453 | return 0; | |
454 | } | |
1b566071 | 455 | } |
1c8e710c | 456 | |
1b566071 | 457 | return -ENOENT; |
1c8e710c TG |
458 | } |
459 | ||
ad208fac | 460 | static int route_add_internal(Manager *manager, Link *link, Set **routes, Route *in, Route **ret) { |
889b550f | 461 | |
8e766630 | 462 | _cleanup_(route_freep) Route *route = NULL; |
1c8e710c TG |
463 | int r; |
464 | ||
ad208fac | 465 | assert(manager || link); |
1c8e710c | 466 | assert(routes); |
f1368755 | 467 | assert(in); |
1c8e710c TG |
468 | |
469 | r = route_new(&route); | |
470 | if (r < 0) | |
471 | return r; | |
472 | ||
f1368755 YW |
473 | route->family = in->family; |
474 | route->src = in->src; | |
475 | route->src_prefixlen = in->src_prefixlen; | |
476 | route->dst = in->dst; | |
477 | route->dst_prefixlen = in->dst_prefixlen; | |
6dd53981 | 478 | route->gw_family = in->gw_family; |
f1368755 YW |
479 | route->gw = in->gw; |
480 | route->prefsrc = in->prefsrc; | |
481 | route->scope = in->scope; | |
482 | route->protocol = in->protocol; | |
483 | route->type = in->type; | |
484 | route->tos = in->tos; | |
485 | route->priority = in->priority; | |
486 | route->table = in->table; | |
487 | route->initcwnd = in->initcwnd; | |
488 | route->initrwnd = in->initrwnd; | |
489 | route->lifetime = in->lifetime; | |
1c8e710c | 490 | |
de7fef4b | 491 | r = set_ensure_put(routes, &route_hash_ops, route); |
1c8e710c TG |
492 | if (r < 0) |
493 | return r; | |
75a302b5 YW |
494 | if (r == 0) |
495 | return -EEXIST; | |
1c8e710c TG |
496 | |
497 | route->link = link; | |
ad208fac | 498 | route->manager = manager; |
1c8e710c TG |
499 | |
500 | if (ret) | |
501 | *ret = route; | |
502 | ||
503 | route = NULL; | |
504 | ||
505 | return 0; | |
506 | } | |
507 | ||
ad208fac YW |
508 | static int route_add_foreign(Manager *manager, Link *link, Route *in, Route **ret) { |
509 | assert(manager || link); | |
510 | return route_add_internal(manager, link, link ? &link->routes_foreign : &manager->routes_foreign, in, ret); | |
1c8e710c TG |
511 | } |
512 | ||
ad208fac | 513 | static int route_add(Manager *manager, Link *link, Route *in, Route **ret) { |
1c8e710c TG |
514 | Route *route; |
515 | int r; | |
516 | ||
ad208fac YW |
517 | assert(manager || link); |
518 | assert(in); | |
519 | ||
520 | r = route_get(manager, link, in, &route); | |
1c8e710c TG |
521 | if (r == -ENOENT) { |
522 | /* Route does not exist, create a new one */ | |
ad208fac | 523 | r = route_add_internal(manager, link, link ? &link->routes : &manager->routes, in, &route); |
1c8e710c TG |
524 | if (r < 0) |
525 | return r; | |
526 | } else if (r == 0) { | |
527 | /* Take over a foreign route */ | |
ad208fac YW |
528 | if (link) { |
529 | r = set_ensure_put(&link->routes, &route_hash_ops, route); | |
530 | if (r < 0) | |
531 | return r; | |
1c8e710c | 532 | |
ad208fac YW |
533 | set_remove(link->routes_foreign, route); |
534 | } else { | |
535 | r = set_ensure_put(&manager->routes, &route_hash_ops, route); | |
536 | if (r < 0) | |
537 | return r; | |
538 | ||
539 | set_remove(manager->routes_foreign, route); | |
540 | } | |
1c8e710c TG |
541 | } else if (r == 1) { |
542 | /* Route exists, do nothing */ | |
543 | ; | |
544 | } else | |
545 | return r; | |
546 | ||
856e309d MC |
547 | if (ret) |
548 | *ret = route; | |
1c8e710c TG |
549 | |
550 | return 0; | |
551 | } | |
552 | ||
302a796f | 553 | static int route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
4645ad47 YW |
554 | int r; |
555 | ||
556 | assert(m); | |
4645ad47 | 557 | |
ad208fac YW |
558 | /* Note that link may be NULL. */ |
559 | if (link && IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
4645ad47 YW |
560 | return 1; |
561 | ||
562 | r = sd_netlink_message_get_errno(m); | |
563 | if (r < 0 && r != -ESRCH) | |
5ecb131d | 564 | log_link_message_warning_errno(link, m, r, "Could not drop route, ignoring"); |
4645ad47 YW |
565 | |
566 | return 1; | |
567 | } | |
568 | ||
ad208fac YW |
569 | int route_remove( |
570 | Route *route, | |
571 | Manager *manager, | |
572 | Link *link, | |
573 | link_netlink_message_handler_t callback) { | |
27efb52b | 574 | |
4afd3348 | 575 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
5c1d3fc9 UTL |
576 | int r; |
577 | ||
ad208fac | 578 | assert(link || manager); |
4c701096 | 579 | assert(IN_SET(route->family, AF_INET, AF_INET6)); |
5c1d3fc9 | 580 | |
ad208fac YW |
581 | if (!manager) |
582 | manager = link->manager; | |
583 | ||
584 | r = sd_rtnl_message_new_route(manager->rtnl, &req, | |
28cc555d DW |
585 | RTM_DELROUTE, route->family, |
586 | route->protocol); | |
f647962d | 587 | if (r < 0) |
7750b796 | 588 | return log_link_error_errno(link, r, "Could not create RTM_DELROUTE message: %m"); |
5c1d3fc9 | 589 | |
b297e0a7 YW |
590 | if (DEBUG_LOGGING) { |
591 | _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL; | |
d3e291fd | 592 | char scope[ROUTE_SCOPE_STR_MAX], table[ROUTE_TABLE_STR_MAX], protocol[ROUTE_PROTOCOL_STR_MAX]; |
b297e0a7 YW |
593 | |
594 | if (!in_addr_is_null(route->family, &route->dst)) { | |
595 | (void) in_addr_to_string(route->family, &route->dst, &dst); | |
596 | (void) asprintf(&dst_prefixlen, "/%u", route->dst_prefixlen); | |
597 | } | |
598 | if (!in_addr_is_null(route->family, &route->src)) | |
599 | (void) in_addr_to_string(route->family, &route->src, &src); | |
6dd53981 YW |
600 | if (!in_addr_is_null(route->gw_family, &route->gw)) |
601 | (void) in_addr_to_string(route->gw_family, &route->gw, &gw); | |
b297e0a7 YW |
602 | if (!in_addr_is_null(route->family, &route->prefsrc)) |
603 | (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc); | |
604 | ||
d3e291fd | 605 | log_link_debug(link, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s", |
b297e0a7 YW |
606 | strna(dst), strempty(dst_prefixlen), strna(src), strna(gw), strna(prefsrc), |
607 | format_route_scope(route->scope, scope, sizeof(scope)), | |
608 | format_route_table(route->table, table, sizeof(table)), | |
d3e291fd | 609 | format_route_protocol(route->protocol, protocol, sizeof(protocol)), |
b297e0a7 YW |
610 | strna(route_type_to_string(route->type))); |
611 | } | |
612 | ||
6dd53981 YW |
613 | if (in_addr_is_null(route->gw_family, &route->gw) == 0) { |
614 | if (route->gw_family == route->family) { | |
615 | r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->gw_family, &route->gw); | |
616 | if (r < 0) | |
617 | return log_link_error_errno(link, r, "Could not append RTA_GATEWAY attribute: %m"); | |
618 | } else { | |
619 | RouteVia rtvia = { | |
620 | .family = route->gw_family, | |
621 | .address = route->gw, | |
622 | }; | |
623 | ||
624 | r = sd_netlink_message_append_data(req, RTA_VIA, &rtvia, sizeof(rtvia)); | |
625 | if (r < 0) | |
626 | return log_link_error_errno(link, r, "Could not append RTA_VIA attribute: %m"); | |
627 | } | |
5c1d3fc9 UTL |
628 | } |
629 | ||
630 | if (route->dst_prefixlen) { | |
43409486 | 631 | r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst); |
f647962d | 632 | if (r < 0) |
7750b796 | 633 | return log_link_error_errno(link, r, "Could not append RTA_DST attribute: %m"); |
5c1d3fc9 UTL |
634 | |
635 | r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen); | |
f647962d | 636 | if (r < 0) |
7750b796 | 637 | return log_link_error_errno(link, r, "Could not set destination prefix length: %m"); |
5c1d3fc9 UTL |
638 | } |
639 | ||
9e7e4408 | 640 | if (route->src_prefixlen) { |
43409486 | 641 | r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src); |
9e7e4408 | 642 | if (r < 0) |
7750b796 | 643 | return log_link_error_errno(link, r, "Could not append RTA_SRC attribute: %m"); |
9e7e4408 TG |
644 | |
645 | r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen); | |
646 | if (r < 0) | |
7750b796 | 647 | return log_link_error_errno(link, r, "Could not set source prefix length: %m"); |
9e7e4408 TG |
648 | } |
649 | ||
d40b01e4 | 650 | if (in_addr_is_null(route->family, &route->prefsrc) == 0) { |
43409486 | 651 | r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc); |
f647962d | 652 | if (r < 0) |
7750b796 | 653 | return log_link_error_errno(link, r, "Could not append RTA_PREFSRC attribute: %m"); |
46b0c76e ERB |
654 | } |
655 | ||
5c1d3fc9 | 656 | r = sd_rtnl_message_route_set_scope(req, route->scope); |
f647962d | 657 | if (r < 0) |
7750b796 | 658 | return log_link_error_errno(link, r, "Could not set scope: %m"); |
5c1d3fc9 | 659 | |
86655331 | 660 | r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority); |
f647962d | 661 | if (r < 0) |
7750b796 | 662 | return log_link_error_errno(link, r, "Could not append RTA_PRIORITY attribute: %m"); |
5c1d3fc9 | 663 | |
2d53f310 | 664 | if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW)) { |
983226f3 SS |
665 | r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex); |
666 | if (r < 0) | |
7750b796 | 667 | return log_link_error_errno(link, r, "Could not append RTA_OIF attribute: %m"); |
983226f3 | 668 | } |
5c1d3fc9 | 669 | |
302a796f YW |
670 | r = netlink_call_async(link->manager->rtnl, NULL, req, |
671 | callback ?: route_remove_handler, | |
672 | link_netlink_destroy_callback, link); | |
f647962d | 673 | if (r < 0) |
7750b796 | 674 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); |
5c1d3fc9 | 675 | |
ad208fac YW |
676 | if (link) |
677 | link_ref(link); | |
563c69c6 | 678 | |
5c1d3fc9 UTL |
679 | return 0; |
680 | } | |
681 | ||
779804dd YW |
682 | static bool link_is_static_route_configured(Link *link, Route *route) { |
683 | Route *net_route; | |
684 | ||
685 | assert(link); | |
686 | assert(route); | |
687 | ||
688 | if (!link->network) | |
689 | return false; | |
690 | ||
2a54a044 | 691 | HASHMAP_FOREACH(net_route, link->network->routes_by_section) |
779804dd YW |
692 | if (route_equal(net_route, route)) |
693 | return true; | |
694 | ||
695 | return false; | |
696 | } | |
697 | ||
698 | int link_drop_foreign_routes(Link *link) { | |
699 | Route *route; | |
700 | int k, r = 0; | |
701 | ||
702 | assert(link); | |
703 | ||
704 | SET_FOREACH(route, link->routes_foreign) { | |
705 | /* do not touch routes managed by the kernel */ | |
706 | if (route->protocol == RTPROT_KERNEL) | |
707 | continue; | |
708 | ||
709 | /* do not touch multicast route added by kernel */ | |
710 | /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that. | |
711 | * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */ | |
712 | if (route->protocol == RTPROT_BOOT && | |
713 | route->family == AF_INET6 && | |
714 | route->dst_prefixlen == 8 && | |
715 | in_addr_equal(AF_INET6, &route->dst, &(union in_addr_union) { .in6 = {{{ 0xff,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }}} })) | |
716 | continue; | |
717 | ||
718 | if (route->protocol == RTPROT_STATIC && link->network && | |
719 | FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC)) | |
720 | continue; | |
721 | ||
722 | if (route->protocol == RTPROT_DHCP && link->network && | |
723 | FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) | |
724 | continue; | |
725 | ||
726 | if (link_is_static_route_configured(link, route)) | |
ad208fac | 727 | k = route_add(NULL, link, route, NULL); |
779804dd | 728 | else |
ad208fac | 729 | k = route_remove(route, NULL, link, NULL); |
779804dd YW |
730 | if (k < 0 && r >= 0) |
731 | r = k; | |
732 | } | |
733 | ||
734 | return r; | |
735 | } | |
736 | ||
62f0ea5f YW |
737 | int link_drop_routes(Link *link) { |
738 | Route *route; | |
739 | int k, r = 0; | |
740 | ||
741 | assert(link); | |
742 | ||
743 | SET_FOREACH(route, link->routes) { | |
744 | /* do not touch routes managed by the kernel */ | |
745 | if (route->protocol == RTPROT_KERNEL) | |
746 | continue; | |
747 | ||
ad208fac | 748 | k = route_remove(route, NULL, link, NULL); |
62f0ea5f YW |
749 | if (k < 0 && r >= 0) |
750 | r = k; | |
751 | } | |
752 | ||
753 | return r; | |
754 | } | |
755 | ||
74154c2e | 756 | static int route_expire_handler(sd_event_source *s, uint64_t usec, void *userdata) { |
f833694d TG |
757 | Route *route = userdata; |
758 | int r; | |
759 | ||
760 | assert(route); | |
761 | ||
ad208fac | 762 | r = route_remove(route, route->manager, route->link, NULL); |
f833694d | 763 | if (r < 0) |
98b02994 | 764 | log_link_warning_errno(route->link, r, "Could not remove route: %m"); |
3bdccf69 | 765 | else |
fe7ca21a | 766 | route_free(route); |
f833694d TG |
767 | |
768 | return 1; | |
769 | } | |
770 | ||
6ff5cc6b YW |
771 | static int append_nexthop_one(Route *route, MultipathRoute *m, struct rtattr **rta, size_t offset) { |
772 | struct rtnexthop *rtnh; | |
773 | struct rtattr *new_rta; | |
774 | int r; | |
775 | ||
776 | assert(route); | |
777 | assert(m); | |
778 | assert(rta); | |
779 | assert(*rta); | |
780 | ||
781 | new_rta = realloc(*rta, RTA_ALIGN((*rta)->rta_len) + RTA_SPACE(sizeof(struct rtnexthop))); | |
782 | if (!new_rta) | |
783 | return -ENOMEM; | |
784 | *rta = new_rta; | |
785 | ||
786 | rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset); | |
787 | *rtnh = (struct rtnexthop) { | |
788 | .rtnh_len = sizeof(*rtnh), | |
789 | .rtnh_ifindex = m->ifindex, | |
790 | .rtnh_hops = m->weight > 0 ? m->weight - 1 : 0, | |
791 | }; | |
792 | ||
793 | (*rta)->rta_len += sizeof(struct rtnexthop); | |
794 | ||
795 | if (route->family == m->gateway.family) { | |
796 | r = rtattr_append_attribute(rta, RTA_GATEWAY, &m->gateway.address, FAMILY_ADDRESS_SIZE(m->gateway.family)); | |
797 | if (r < 0) | |
798 | goto clear; | |
799 | rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset); | |
800 | rtnh->rtnh_len += RTA_SPACE(FAMILY_ADDRESS_SIZE(m->gateway.family)); | |
801 | } else { | |
802 | r = rtattr_append_attribute(rta, RTA_VIA, &m->gateway, FAMILY_ADDRESS_SIZE(m->gateway.family) + sizeof(m->gateway.family)); | |
803 | if (r < 0) | |
804 | goto clear; | |
805 | rtnh = (struct rtnexthop *)((uint8_t *) *rta + offset); | |
806 | rtnh->rtnh_len += RTA_SPACE(FAMILY_ADDRESS_SIZE(m->gateway.family) + sizeof(m->gateway.family)); | |
807 | } | |
808 | ||
809 | return 0; | |
810 | ||
811 | clear: | |
812 | (*rta)->rta_len -= sizeof(struct rtnexthop); | |
813 | return r; | |
814 | } | |
815 | ||
816 | static int append_nexthops(Route *route, sd_netlink_message *req) { | |
817 | _cleanup_free_ struct rtattr *rta = NULL; | |
818 | struct rtnexthop *rtnh; | |
819 | MultipathRoute *m; | |
820 | size_t offset; | |
6ff5cc6b YW |
821 | int r; |
822 | ||
823 | if (ordered_set_isempty(route->multipath_routes)) | |
824 | return 0; | |
825 | ||
826 | rta = new(struct rtattr, 1); | |
827 | if (!rta) | |
828 | return -ENOMEM; | |
829 | ||
830 | *rta = (struct rtattr) { | |
831 | .rta_type = RTA_MULTIPATH, | |
832 | .rta_len = RTA_LENGTH(0), | |
833 | }; | |
834 | offset = (uint8_t *) RTA_DATA(rta) - (uint8_t *) rta; | |
835 | ||
90e74a66 | 836 | ORDERED_SET_FOREACH(m, route->multipath_routes) { |
6ff5cc6b YW |
837 | r = append_nexthop_one(route, m, &rta, offset); |
838 | if (r < 0) | |
839 | return r; | |
840 | ||
841 | rtnh = (struct rtnexthop *)((uint8_t *) rta + offset); | |
842 | offset = (uint8_t *) RTNH_NEXT(rtnh) - (uint8_t *) rta; | |
843 | } | |
844 | ||
845 | r = sd_netlink_message_append_data(req, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta)); | |
846 | if (r < 0) | |
847 | return r; | |
848 | ||
849 | return 0; | |
850 | } | |
851 | ||
1b566071 LP |
852 | int route_configure( |
853 | Route *route, | |
854 | Link *link, | |
80b0e860 YW |
855 | link_netlink_message_handler_t callback, |
856 | Route **ret) { | |
1b566071 | 857 | |
4afd3348 LP |
858 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
859 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; | |
f579559b TG |
860 | int r; |
861 | ||
f579559b | 862 | assert(link); |
f882c247 TG |
863 | assert(link->manager); |
864 | assert(link->manager->rtnl); | |
f579559b | 865 | assert(link->ifindex > 0); |
4c701096 | 866 | assert(IN_SET(route->family, AF_INET, AF_INET6)); |
bd1175bc | 867 | assert(callback); |
f579559b | 868 | |
ad208fac | 869 | if (route_get(link->manager, link, route, NULL) <= 0 && |
47d2d30d | 870 | set_size(link->routes) >= routes_max()) |
7750b796 YW |
871 | return log_link_error_errno(link, SYNTHETIC_ERRNO(E2BIG), |
872 | "Too many routes are configured, refusing: %m"); | |
1b566071 | 873 | |
156ed65e YW |
874 | if (DEBUG_LOGGING) { |
875 | _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL; | |
d3e291fd | 876 | char scope[ROUTE_SCOPE_STR_MAX], table[ROUTE_TABLE_STR_MAX], protocol[ROUTE_PROTOCOL_STR_MAX]; |
156ed65e YW |
877 | |
878 | if (!in_addr_is_null(route->family, &route->dst)) { | |
879 | (void) in_addr_to_string(route->family, &route->dst, &dst); | |
880 | (void) asprintf(&dst_prefixlen, "/%u", route->dst_prefixlen); | |
881 | } | |
882 | if (!in_addr_is_null(route->family, &route->src)) | |
883 | (void) in_addr_to_string(route->family, &route->src, &src); | |
6dd53981 YW |
884 | if (!in_addr_is_null(route->gw_family, &route->gw)) |
885 | (void) in_addr_to_string(route->gw_family, &route->gw, &gw); | |
156ed65e YW |
886 | if (!in_addr_is_null(route->family, &route->prefsrc)) |
887 | (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc); | |
888 | ||
d3e291fd | 889 | log_link_debug(link, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s", |
b297e0a7 YW |
890 | strna(dst), strempty(dst_prefixlen), strna(src), strna(gw), strna(prefsrc), |
891 | format_route_scope(route->scope, scope, sizeof(scope)), | |
892 | format_route_table(route->table, table, sizeof(table)), | |
d3e291fd | 893 | format_route_protocol(route->protocol, protocol, sizeof(protocol)), |
b297e0a7 | 894 | strna(route_type_to_string(route->type))); |
156ed65e YW |
895 | } |
896 | ||
151b9b96 | 897 | r = sd_rtnl_message_new_route(link->manager->rtnl, &req, |
28cc555d DW |
898 | RTM_NEWROUTE, route->family, |
899 | route->protocol); | |
f647962d | 900 | if (r < 0) |
7750b796 | 901 | return log_link_error_errno(link, r, "Could not create RTM_NEWROUTE message: %m"); |
f579559b | 902 | |
6dd53981 YW |
903 | if (in_addr_is_null(route->gw_family, &route->gw) == 0) { |
904 | if (route->gw_family == route->family) { | |
905 | r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->gw_family, &route->gw); | |
906 | if (r < 0) | |
907 | return log_link_error_errno(link, r, "Could not append RTA_GATEWAY attribute: %m"); | |
908 | } else { | |
909 | RouteVia rtvia = { | |
910 | .family = route->gw_family, | |
911 | .address = route->gw, | |
912 | }; | |
c953b24c | 913 | |
6dd53981 YW |
914 | r = sd_netlink_message_append_data(req, RTA_VIA, &rtvia, sizeof(rtvia)); |
915 | if (r < 0) | |
916 | return log_link_error_errno(link, r, "Could not append RTA_VIA attribute: %m"); | |
917 | } | |
f579559b TG |
918 | } |
919 | ||
70dc2362 | 920 | if (route->dst_prefixlen > 0) { |
43409486 | 921 | r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst); |
f647962d | 922 | if (r < 0) |
7750b796 | 923 | return log_link_error_errno(link, r, "Could not append RTA_DST attribute: %m"); |
6ae115c1 | 924 | |
ae4c67a7 | 925 | r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen); |
f647962d | 926 | if (r < 0) |
7750b796 | 927 | return log_link_error_errno(link, r, "Could not set destination prefix length: %m"); |
1f01fb4f TG |
928 | } |
929 | ||
70dc2362 | 930 | if (route->src_prefixlen > 0) { |
43409486 | 931 | r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src); |
9e7e4408 | 932 | if (r < 0) |
7750b796 | 933 | return log_link_error_errno(link, r, "Could not append RTA_SRC attribute: %m"); |
9e7e4408 TG |
934 | |
935 | r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen); | |
936 | if (r < 0) | |
7750b796 | 937 | return log_link_error_errno(link, r, "Could not set source prefix length: %m"); |
9e7e4408 TG |
938 | } |
939 | ||
d40b01e4 | 940 | if (in_addr_is_null(route->family, &route->prefsrc) == 0) { |
43409486 | 941 | r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc); |
f647962d | 942 | if (r < 0) |
7750b796 | 943 | return log_link_error_errno(link, r, "Could not append RTA_PREFSRC attribute: %m"); |
46b0c76e ERB |
944 | } |
945 | ||
5c1d3fc9 | 946 | r = sd_rtnl_message_route_set_scope(req, route->scope); |
f647962d | 947 | if (r < 0) |
7750b796 | 948 | return log_link_error_errno(link, r, "Could not set scope: %m"); |
5c1d3fc9 | 949 | |
54901fd2 YW |
950 | if (route->gateway_onlink >= 0) |
951 | SET_FLAG(route->flags, RTNH_F_ONLINK, route->gateway_onlink); | |
952 | ||
3b015d40 TG |
953 | r = sd_rtnl_message_route_set_flags(req, route->flags); |
954 | if (r < 0) | |
7750b796 | 955 | return log_link_error_errno(link, r, "Could not set flags: %m"); |
c953b24c | 956 | |
a0d95bbc | 957 | if (route->table != RT_TABLE_MAIN) { |
c953b24c SS |
958 | if (route->table < 256) { |
959 | r = sd_rtnl_message_route_set_table(req, route->table); | |
960 | if (r < 0) | |
7750b796 | 961 | return log_link_error_errno(link, r, "Could not set route table: %m"); |
c953b24c | 962 | } else { |
c953b24c SS |
963 | r = sd_rtnl_message_route_set_table(req, RT_TABLE_UNSPEC); |
964 | if (r < 0) | |
7750b796 | 965 | return log_link_error_errno(link, r, "Could not set route table: %m"); |
c953b24c | 966 | |
06976f5b | 967 | /* Table attribute to allow more than 256. */ |
c953b24c SS |
968 | r = sd_netlink_message_append_data(req, RTA_TABLE, &route->table, sizeof(route->table)); |
969 | if (r < 0) | |
7750b796 | 970 | return log_link_error_errno(link, r, "Could not append RTA_TABLE attribute: %m"); |
c953b24c SS |
971 | } |
972 | } | |
3b015d40 | 973 | |
86655331 | 974 | r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority); |
f647962d | 975 | if (r < 0) |
7750b796 | 976 | return log_link_error_errno(link, r, "Could not append RTA_PRIORITY attribute: %m"); |
5c1d3fc9 | 977 | |
3b015d40 TG |
978 | r = sd_netlink_message_append_u8(req, RTA_PREF, route->pref); |
979 | if (r < 0) | |
7750b796 | 980 | return log_link_error_errno(link, r, "Could not append RTA_PREF attribute: %m"); |
3b015d40 | 981 | |
f02ba163 DD |
982 | if (route->lifetime != USEC_INFINITY && kernel_route_expiration_supported()) { |
983 | r = sd_netlink_message_append_u32(req, RTA_EXPIRES, | |
984 | DIV_ROUND_UP(usec_sub_unsigned(route->lifetime, now(clock_boottime_or_monotonic())), USEC_PER_SEC)); | |
985 | if (r < 0) | |
7750b796 | 986 | return log_link_error_errno(link, r, "Could not append RTA_EXPIRES attribute: %m"); |
f02ba163 DD |
987 | } |
988 | ||
983226f3 | 989 | r = sd_rtnl_message_route_set_type(req, route->type); |
f647962d | 990 | if (r < 0) |
7750b796 | 991 | return log_link_error_errno(link, r, "Could not set route type: %m"); |
983226f3 | 992 | |
2d53f310 | 993 | if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW)) { |
983226f3 SS |
994 | r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex); |
995 | if (r < 0) | |
7750b796 | 996 | return log_link_error_errno(link, r, "Could not append RTA_OIF attribute: %m"); |
983226f3 | 997 | } |
f579559b | 998 | |
9b88f20a SS |
999 | if (route->ttl_propagate >= 0) { |
1000 | r = sd_netlink_message_append_u8(req, RTA_TTL_PROPAGATE, route->ttl_propagate); | |
1001 | if (r < 0) | |
1002 | return log_link_error_errno(link, r, "Could not append RTA_TTL_PROPAGATE attribute: %m"); | |
1003 | } | |
1004 | ||
d6fceaf1 SS |
1005 | r = sd_netlink_message_open_container(req, RTA_METRICS); |
1006 | if (r < 0) | |
7750b796 | 1007 | return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m"); |
d6fceaf1 SS |
1008 | |
1009 | if (route->mtu > 0) { | |
1010 | r = sd_netlink_message_append_u32(req, RTAX_MTU, route->mtu); | |
1011 | if (r < 0) | |
7750b796 | 1012 | return log_link_error_errno(link, r, "Could not append RTAX_MTU attribute: %m"); |
d6fceaf1 SS |
1013 | } |
1014 | ||
6b21ad33 | 1015 | if (route->initcwnd > 0) { |
323d9329 SS |
1016 | r = sd_netlink_message_append_u32(req, RTAX_INITCWND, route->initcwnd); |
1017 | if (r < 0) | |
7750b796 | 1018 | return log_link_error_errno(link, r, "Could not append RTAX_INITCWND attribute: %m"); |
323d9329 SS |
1019 | } |
1020 | ||
6b21ad33 | 1021 | if (route->initrwnd > 0) { |
323d9329 SS |
1022 | r = sd_netlink_message_append_u32(req, RTAX_INITRWND, route->initrwnd); |
1023 | if (r < 0) | |
7750b796 | 1024 | return log_link_error_errno(link, r, "Could not append RTAX_INITRWND attribute: %m"); |
323d9329 SS |
1025 | } |
1026 | ||
67c193bf | 1027 | if (route->quickack >= 0) { |
09f5dfad SS |
1028 | r = sd_netlink_message_append_u32(req, RTAX_QUICKACK, route->quickack); |
1029 | if (r < 0) | |
7750b796 | 1030 | return log_link_error_errno(link, r, "Could not append RTAX_QUICKACK attribute: %m"); |
09f5dfad SS |
1031 | } |
1032 | ||
633c7258 SS |
1033 | if (route->fast_open_no_cookie >= 0) { |
1034 | r = sd_netlink_message_append_u32(req, RTAX_FASTOPEN_NO_COOKIE, route->fast_open_no_cookie); | |
1035 | if (r < 0) | |
1036 | return log_link_error_errno(link, r, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m"); | |
1037 | } | |
1038 | ||
d6fceaf1 SS |
1039 | r = sd_netlink_message_close_container(req); |
1040 | if (r < 0) | |
7750b796 | 1041 | return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m"); |
d6fceaf1 | 1042 | |
6ff5cc6b YW |
1043 | r = append_nexthops(route, req); |
1044 | if (r < 0) | |
1045 | return log_link_error_errno(link, r, "Could not append RTA_MULTIPATH attribute: %m"); | |
1046 | ||
302a796f YW |
1047 | r = netlink_call_async(link->manager->rtnl, NULL, req, callback, |
1048 | link_netlink_destroy_callback, link); | |
f647962d | 1049 | if (r < 0) |
7750b796 | 1050 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); |
f579559b | 1051 | |
563c69c6 TG |
1052 | link_ref(link); |
1053 | ||
ad208fac YW |
1054 | if (IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW) || !ordered_set_isempty(route->multipath_routes)) |
1055 | r = route_add(link->manager, NULL, route, &route); | |
1056 | else | |
1057 | r = route_add(NULL, link, route, &route); | |
1c8e710c | 1058 | if (r < 0) |
7750b796 | 1059 | return log_link_error_errno(link, r, "Could not add route: %m"); |
1c8e710c | 1060 | |
f833694d | 1061 | /* TODO: drop expiration handling once it can be pushed into the kernel */ |
f02ba163 | 1062 | if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) { |
f833694d TG |
1063 | r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), |
1064 | route->lifetime, 0, route_expire_handler, route); | |
1065 | if (r < 0) | |
7750b796 | 1066 | return log_link_error_errno(link, r, "Could not arm expiration timer: %m"); |
f833694d TG |
1067 | } |
1068 | ||
1069 | sd_event_source_unref(route->expire); | |
1cc6c93a | 1070 | route->expire = TAKE_PTR(expire); |
f833694d | 1071 | |
80b0e860 YW |
1072 | if (ret) |
1073 | *ret = route; | |
1074 | ||
c4423317 | 1075 | return 1; |
f579559b TG |
1076 | } |
1077 | ||
141318f7 YW |
1078 | static int route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
1079 | int r; | |
1080 | ||
1081 | assert(link); | |
1082 | assert(link->route_messages > 0); | |
1083 | assert(IN_SET(link->state, LINK_STATE_CONFIGURING, | |
1084 | LINK_STATE_FAILED, LINK_STATE_LINGER)); | |
1085 | ||
1086 | link->route_messages--; | |
1087 | ||
1088 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1089 | return 1; | |
1090 | ||
1091 | r = sd_netlink_message_get_errno(m); | |
1092 | if (r < 0 && r != -EEXIST) { | |
1093 | log_link_message_warning_errno(link, m, r, "Could not set route"); | |
1094 | link_enter_failed(link); | |
1095 | return 1; | |
1096 | } | |
1097 | ||
1098 | if (link->route_messages == 0) { | |
1099 | log_link_debug(link, "Routes set"); | |
1100 | link->static_routes_configured = true; | |
1101 | link_set_nexthop(link); | |
1102 | } | |
1103 | ||
1104 | return 1; | |
1105 | } | |
1106 | ||
1107 | int link_set_routes(Link *link) { | |
1108 | enum { | |
1109 | PHASE_NON_GATEWAY, /* First phase: Routes without a gateway */ | |
1110 | PHASE_GATEWAY, /* Second phase: Routes with a gateway */ | |
1111 | _PHASE_MAX | |
1112 | } phase; | |
1113 | Route *rt; | |
1114 | int r; | |
1115 | ||
1116 | assert(link); | |
1117 | assert(link->network); | |
1118 | assert(link->state != _LINK_STATE_INVALID); | |
1119 | ||
1120 | link->static_routes_configured = false; | |
1121 | ||
1122 | if (!link->addresses_ready) | |
1123 | return 0; | |
1124 | ||
1125 | if (!link_has_carrier(link) && !link->network->configure_without_carrier) | |
1126 | /* During configuring addresses, the link lost its carrier. As networkd is dropping | |
1127 | * the addresses now, let's not configure the routes either. */ | |
1128 | return 0; | |
1129 | ||
1130 | r = link_set_routing_policy_rules(link); | |
1131 | if (r < 0) | |
1132 | return r; | |
1133 | ||
1134 | /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */ | |
1135 | for (phase = 0; phase < _PHASE_MAX; phase++) | |
2a54a044 | 1136 | HASHMAP_FOREACH(rt, link->network->routes_by_section) { |
141318f7 YW |
1137 | if (rt->gateway_from_dhcp) |
1138 | continue; | |
1139 | ||
6dd53981 | 1140 | if ((in_addr_is_null(rt->gw_family, &rt->gw) && ordered_set_isempty(rt->multipath_routes)) != (phase == PHASE_NON_GATEWAY)) |
141318f7 YW |
1141 | continue; |
1142 | ||
1143 | r = route_configure(rt, link, route_handler, NULL); | |
1144 | if (r < 0) | |
1145 | return log_link_warning_errno(link, r, "Could not set routes: %m"); | |
1146 | if (r > 0) | |
1147 | link->route_messages++; | |
1148 | } | |
1149 | ||
1150 | if (link->route_messages == 0) { | |
1151 | link->static_routes_configured = true; | |
1152 | link_set_nexthop(link); | |
1153 | } else { | |
1154 | log_link_debug(link, "Setting routes"); | |
1155 | link_set_state(link, LINK_STATE_CONFIGURING); | |
1156 | } | |
1157 | ||
1158 | return 0; | |
1159 | } | |
1160 | ||
4468f01b YW |
1161 | int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) { |
1162 | _cleanup_(route_freep) Route *tmp = NULL; | |
1163 | Route *route = NULL; | |
1164 | Link *link = NULL; | |
1165 | uint32_t ifindex; | |
1166 | uint16_t type; | |
1167 | unsigned char table; | |
6dd53981 | 1168 | RouteVia via; |
4468f01b YW |
1169 | int r; |
1170 | ||
1171 | assert(rtnl); | |
1172 | assert(message); | |
1173 | assert(m); | |
1174 | ||
1175 | if (sd_netlink_message_is_error(message)) { | |
1176 | r = sd_netlink_message_get_errno(message); | |
1177 | if (r < 0) | |
1178 | log_message_warning_errno(message, r, "rtnl: failed to receive route message, ignoring"); | |
1179 | ||
1180 | return 0; | |
1181 | } | |
1182 | ||
1183 | r = sd_netlink_message_get_type(message, &type); | |
1184 | if (r < 0) { | |
1185 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); | |
1186 | return 0; | |
1187 | } else if (!IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE)) { | |
1188 | log_warning("rtnl: received unexpected message type %u when processing route, ignoring.", type); | |
1189 | return 0; | |
1190 | } | |
1191 | ||
1192 | r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex); | |
ad208fac | 1193 | if (r < 0 && r != -ENODATA) { |
4468f01b YW |
1194 | log_warning_errno(r, "rtnl: could not get ifindex from route message, ignoring: %m"); |
1195 | return 0; | |
ad208fac YW |
1196 | } else if (r >= 0) { |
1197 | if (ifindex <= 0) { | |
1198 | log_warning("rtnl: received route message with invalid ifindex %d, ignoring.", ifindex); | |
1199 | return 0; | |
1200 | } | |
4468f01b | 1201 | |
ad208fac YW |
1202 | r = link_get(m, ifindex, &link); |
1203 | if (r < 0 || !link) { | |
1204 | /* when enumerating we might be out of sync, but we will | |
1205 | * get the route again, so just ignore it */ | |
1206 | if (!m->enumerating) | |
1207 | log_warning("rtnl: received route message for link (%d) we do not know about, ignoring", ifindex); | |
1208 | return 0; | |
1209 | } | |
4468f01b YW |
1210 | } |
1211 | ||
1212 | r = route_new(&tmp); | |
1213 | if (r < 0) | |
1214 | return log_oom(); | |
1215 | ||
1216 | r = sd_rtnl_message_route_get_family(message, &tmp->family); | |
1217 | if (r < 0) { | |
1218 | log_link_warning(link, "rtnl: received route message without family, ignoring"); | |
1219 | return 0; | |
1220 | } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) { | |
1221 | log_link_debug(link, "rtnl: received route message with invalid family '%i', ignoring", tmp->family); | |
1222 | return 0; | |
1223 | } | |
1224 | ||
1225 | r = sd_rtnl_message_route_get_protocol(message, &tmp->protocol); | |
1226 | if (r < 0) { | |
1227 | log_warning_errno(r, "rtnl: received route message without route protocol: %m"); | |
1228 | return 0; | |
1229 | } | |
1230 | ||
1231 | switch (tmp->family) { | |
1232 | case AF_INET: | |
1233 | r = sd_netlink_message_read_in_addr(message, RTA_DST, &tmp->dst.in); | |
1234 | if (r < 0 && r != -ENODATA) { | |
1235 | log_link_warning_errno(link, r, "rtnl: received route message without valid destination, ignoring: %m"); | |
1236 | return 0; | |
1237 | } | |
1238 | ||
1239 | r = sd_netlink_message_read_in_addr(message, RTA_GATEWAY, &tmp->gw.in); | |
1240 | if (r < 0 && r != -ENODATA) { | |
1241 | log_link_warning_errno(link, r, "rtnl: received route message without valid gateway, ignoring: %m"); | |
1242 | return 0; | |
6dd53981 YW |
1243 | } else if (r >= 0) |
1244 | tmp->gw_family = AF_INET; | |
1245 | ||
1246 | r = sd_netlink_message_read(message, RTA_VIA, sizeof(via), &via); | |
1247 | if (r < 0 && r != -ENODATA) { | |
1248 | log_link_warning_errno(link, r, "rtnl: received route message without valid gateway, ignoring: %m"); | |
1249 | return 0; | |
1250 | } else if (r >= 0) { | |
1251 | tmp->gw_family = via.family; | |
1252 | tmp->gw = via.address; | |
4468f01b YW |
1253 | } |
1254 | ||
1255 | r = sd_netlink_message_read_in_addr(message, RTA_SRC, &tmp->src.in); | |
1256 | if (r < 0 && r != -ENODATA) { | |
1257 | log_link_warning_errno(link, r, "rtnl: received route message without valid source, ignoring: %m"); | |
1258 | return 0; | |
1259 | } | |
1260 | ||
1261 | r = sd_netlink_message_read_in_addr(message, RTA_PREFSRC, &tmp->prefsrc.in); | |
1262 | if (r < 0 && r != -ENODATA) { | |
1263 | log_link_warning_errno(link, r, "rtnl: received route message without valid preferred source, ignoring: %m"); | |
1264 | return 0; | |
1265 | } | |
1266 | ||
1267 | break; | |
1268 | ||
1269 | case AF_INET6: | |
1270 | r = sd_netlink_message_read_in6_addr(message, RTA_DST, &tmp->dst.in6); | |
1271 | if (r < 0 && r != -ENODATA) { | |
1272 | log_link_warning_errno(link, r, "rtnl: received route message without valid destination, ignoring: %m"); | |
1273 | return 0; | |
1274 | } | |
1275 | ||
1276 | r = sd_netlink_message_read_in6_addr(message, RTA_GATEWAY, &tmp->gw.in6); | |
1277 | if (r < 0 && r != -ENODATA) { | |
1278 | log_link_warning_errno(link, r, "rtnl: received route message without valid gateway, ignoring: %m"); | |
1279 | return 0; | |
6dd53981 YW |
1280 | } else if (r >= 0) |
1281 | tmp->gw_family = AF_INET6; | |
4468f01b YW |
1282 | |
1283 | r = sd_netlink_message_read_in6_addr(message, RTA_SRC, &tmp->src.in6); | |
1284 | if (r < 0 && r != -ENODATA) { | |
1285 | log_link_warning_errno(link, r, "rtnl: received route message without valid source, ignoring: %m"); | |
1286 | return 0; | |
1287 | } | |
1288 | ||
1289 | r = sd_netlink_message_read_in6_addr(message, RTA_PREFSRC, &tmp->prefsrc.in6); | |
1290 | if (r < 0 && r != -ENODATA) { | |
1291 | log_link_warning_errno(link, r, "rtnl: received route message without valid preferred source, ignoring: %m"); | |
1292 | return 0; | |
1293 | } | |
1294 | ||
1295 | break; | |
1296 | ||
1297 | default: | |
1298 | assert_not_reached("Received route message with unsupported address family"); | |
1299 | return 0; | |
1300 | } | |
1301 | ||
1302 | r = sd_rtnl_message_route_get_dst_prefixlen(message, &tmp->dst_prefixlen); | |
1303 | if (r < 0) { | |
1304 | log_link_warning_errno(link, r, "rtnl: received route message with invalid destination prefixlen, ignoring: %m"); | |
1305 | return 0; | |
1306 | } | |
1307 | ||
1308 | r = sd_rtnl_message_route_get_src_prefixlen(message, &tmp->src_prefixlen); | |
1309 | if (r < 0) { | |
1310 | log_link_warning_errno(link, r, "rtnl: received route message with invalid source prefixlen, ignoring: %m"); | |
1311 | return 0; | |
1312 | } | |
1313 | ||
1314 | r = sd_rtnl_message_route_get_scope(message, &tmp->scope); | |
1315 | if (r < 0) { | |
1316 | log_link_warning_errno(link, r, "rtnl: received route message with invalid scope, ignoring: %m"); | |
1317 | return 0; | |
1318 | } | |
1319 | ||
1320 | r = sd_rtnl_message_route_get_tos(message, &tmp->tos); | |
1321 | if (r < 0) { | |
1322 | log_link_warning_errno(link, r, "rtnl: received route message with invalid tos, ignoring: %m"); | |
1323 | return 0; | |
1324 | } | |
1325 | ||
1326 | r = sd_rtnl_message_route_get_type(message, &tmp->type); | |
1327 | if (r < 0) { | |
1328 | log_link_warning_errno(link, r, "rtnl: received route message with invalid type, ignoring: %m"); | |
1329 | return 0; | |
1330 | } | |
1331 | ||
1332 | r = sd_rtnl_message_route_get_table(message, &table); | |
1333 | if (r < 0) { | |
1334 | log_link_warning_errno(link, r, "rtnl: received route message with invalid table, ignoring: %m"); | |
1335 | return 0; | |
1336 | } | |
1337 | tmp->table = table; | |
1338 | ||
1339 | r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &tmp->priority); | |
1340 | if (r < 0 && r != -ENODATA) { | |
1341 | log_link_warning_errno(link, r, "rtnl: received route message with invalid priority, ignoring: %m"); | |
1342 | return 0; | |
1343 | } | |
1344 | ||
1345 | r = sd_netlink_message_enter_container(message, RTA_METRICS); | |
1346 | if (r < 0 && r != -ENODATA) { | |
1347 | log_link_error_errno(link, r, "rtnl: Could not enter RTA_METRICS container: %m"); | |
1348 | return 0; | |
1349 | } | |
1350 | if (r >= 0) { | |
1351 | r = sd_netlink_message_read_u32(message, RTAX_INITCWND, &tmp->initcwnd); | |
1352 | if (r < 0 && r != -ENODATA) { | |
1353 | log_link_warning_errno(link, r, "rtnl: received route message with invalid initcwnd, ignoring: %m"); | |
1354 | return 0; | |
1355 | } | |
1356 | ||
1357 | r = sd_netlink_message_read_u32(message, RTAX_INITRWND, &tmp->initrwnd); | |
1358 | if (r < 0 && r != -ENODATA) { | |
1359 | log_link_warning_errno(link, r, "rtnl: received route message with invalid initrwnd, ignoring: %m"); | |
1360 | return 0; | |
1361 | } | |
1362 | ||
1363 | r = sd_netlink_message_exit_container(message); | |
1364 | if (r < 0) { | |
1365 | log_link_error_errno(link, r, "rtnl: Could not exit from RTA_METRICS container: %m"); | |
1366 | return 0; | |
1367 | } | |
1368 | } | |
1369 | ||
ad208fac | 1370 | (void) route_get(m, link, tmp, &route); |
4468f01b YW |
1371 | |
1372 | if (DEBUG_LOGGING) { | |
1373 | _cleanup_free_ char *buf_dst = NULL, *buf_dst_prefixlen = NULL, | |
1374 | *buf_src = NULL, *buf_gw = NULL, *buf_prefsrc = NULL; | |
1375 | char buf_scope[ROUTE_SCOPE_STR_MAX], buf_table[ROUTE_TABLE_STR_MAX], | |
1376 | buf_protocol[ROUTE_PROTOCOL_STR_MAX]; | |
1377 | ||
1378 | if (!in_addr_is_null(tmp->family, &tmp->dst)) { | |
1379 | (void) in_addr_to_string(tmp->family, &tmp->dst, &buf_dst); | |
1380 | (void) asprintf(&buf_dst_prefixlen, "/%u", tmp->dst_prefixlen); | |
1381 | } | |
1382 | if (!in_addr_is_null(tmp->family, &tmp->src)) | |
1383 | (void) in_addr_to_string(tmp->family, &tmp->src, &buf_src); | |
6dd53981 YW |
1384 | if (!in_addr_is_null(tmp->gw_family, &tmp->gw)) |
1385 | (void) in_addr_to_string(tmp->gw_family, &tmp->gw, &buf_gw); | |
4468f01b YW |
1386 | if (!in_addr_is_null(tmp->family, &tmp->prefsrc)) |
1387 | (void) in_addr_to_string(tmp->family, &tmp->prefsrc, &buf_prefsrc); | |
1388 | ||
1389 | log_link_debug(link, | |
1390 | "%s route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s", | |
ad208fac | 1391 | (!route && !m->manage_foreign_routes) ? "Ignoring received foreign" : |
4468f01b YW |
1392 | type == RTM_DELROUTE ? "Forgetting" : |
1393 | route ? "Received remembered" : "Remembering", | |
1394 | strna(buf_dst), strempty(buf_dst_prefixlen), | |
1395 | strna(buf_src), strna(buf_gw), strna(buf_prefsrc), | |
1396 | format_route_scope(tmp->scope, buf_scope, sizeof buf_scope), | |
1397 | format_route_table(tmp->table, buf_table, sizeof buf_table), | |
1398 | format_route_protocol(tmp->protocol, buf_protocol, sizeof buf_protocol), | |
1399 | strna(route_type_to_string(tmp->type))); | |
1400 | } | |
1401 | ||
1402 | switch (type) { | |
1403 | case RTM_NEWROUTE: | |
ad208fac | 1404 | if (!route && m->manage_foreign_routes) { |
4468f01b | 1405 | /* A route appeared that we did not request */ |
ad208fac | 1406 | r = route_add_foreign(m, link, tmp, &route); |
4468f01b YW |
1407 | if (r < 0) { |
1408 | log_link_warning_errno(link, r, "Failed to remember foreign route, ignoring: %m"); | |
1409 | return 0; | |
1410 | } | |
1411 | } | |
1412 | ||
1413 | break; | |
1414 | ||
1415 | case RTM_DELROUTE: | |
1416 | route_free(route); | |
1417 | break; | |
1418 | ||
1419 | default: | |
1420 | assert_not_reached("Received route message with invalid RTNL message type"); | |
1421 | } | |
1422 | ||
1423 | return 1; | |
1424 | } | |
1425 | ||
56519412 YW |
1426 | int link_serialize_routes(Link *link, FILE *f) { |
1427 | bool space = false; | |
1428 | Route *route; | |
1429 | ||
1430 | assert(link); | |
1431 | assert(link->network); | |
1432 | assert(f); | |
1433 | ||
1434 | fputs("ROUTES=", f); | |
1435 | SET_FOREACH(route, link->routes) { | |
1436 | _cleanup_free_ char *route_str = NULL; | |
1437 | ||
1438 | if (in_addr_to_string(route->family, &route->dst, &route_str) < 0) | |
1439 | continue; | |
1440 | ||
1441 | fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%"PRIu32"/"USEC_FMT, | |
1442 | space ? " " : "", route_str, | |
1443 | route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime); | |
1444 | space = true; | |
1445 | } | |
1446 | fputc('\n', f); | |
1447 | ||
1448 | return 0; | |
1449 | } | |
1450 | ||
731ff05b YW |
1451 | int link_deserialize_routes(Link *link, const char *routes) { |
1452 | int r; | |
1453 | ||
1454 | assert(link); | |
1455 | ||
1456 | for (const char *p = routes;; ) { | |
1457 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; | |
1458 | _cleanup_(route_freep) Route *tmp = NULL; | |
1459 | _cleanup_free_ char *route_str = NULL; | |
1460 | char *prefixlen_str; | |
1461 | Route *route; | |
1462 | ||
1463 | r = extract_first_word(&p, &route_str, NULL, 0); | |
1464 | if (r < 0) | |
1465 | return log_link_debug_errno(link, r, "Failed to parse ROUTES=: %m"); | |
1466 | if (r == 0) | |
1467 | return 0; | |
1468 | ||
1469 | prefixlen_str = strchr(route_str, '/'); | |
1470 | if (!prefixlen_str) { | |
1471 | log_link_debug(link, "Failed to parse route, ignoring: %s", route_str); | |
1472 | continue; | |
1473 | } | |
1474 | *prefixlen_str++ = '\0'; | |
1475 | ||
1476 | r = route_new(&tmp); | |
1477 | if (r < 0) | |
1478 | return log_oom(); | |
1479 | ||
1480 | r = sscanf(prefixlen_str, | |
1481 | "%hhu/%hhu/%"SCNu32"/%"PRIu32"/"USEC_FMT, | |
1482 | &tmp->dst_prefixlen, | |
1483 | &tmp->tos, | |
1484 | &tmp->priority, | |
1485 | &tmp->table, | |
1486 | &tmp->lifetime); | |
1487 | if (r != 5) { | |
1488 | log_link_debug(link, | |
1489 | "Failed to parse destination prefix length, tos, priority, table or expiration: %s", | |
1490 | prefixlen_str); | |
1491 | continue; | |
1492 | } | |
1493 | ||
1494 | r = in_addr_from_string_auto(route_str, &tmp->family, &tmp->dst); | |
1495 | if (r < 0) { | |
1496 | log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); | |
1497 | continue; | |
1498 | } | |
1499 | ||
ad208fac | 1500 | r = route_add(NULL, link, tmp, &route); |
731ff05b YW |
1501 | if (r < 0) |
1502 | return log_link_debug_errno(link, r, "Failed to add route: %m"); | |
1503 | ||
1504 | if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) { | |
1505 | r = sd_event_add_time(link->manager->event, &expire, | |
1506 | clock_boottime_or_monotonic(), | |
1507 | route->lifetime, 0, route_expire_handler, route); | |
1508 | if (r < 0) | |
1509 | log_link_debug_errno(link, r, "Could not arm route expiration handler: %m"); | |
1510 | } | |
1511 | ||
1512 | sd_event_source_unref(route->expire); | |
1513 | route->expire = TAKE_PTR(expire); | |
1514 | } | |
1515 | } | |
1516 | ||
fa7cd711 | 1517 | int network_add_ipv4ll_route(Network *network) { |
fcbf4cb7 | 1518 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
2a54a044 | 1519 | unsigned section_line; |
fa7cd711 YW |
1520 | int r; |
1521 | ||
1522 | assert(network); | |
1523 | ||
1524 | if (!network->ipv4ll_route) | |
1525 | return 0; | |
1526 | ||
2a54a044 YW |
1527 | section_line = hashmap_find_free_section_line(network->routes_by_section); |
1528 | ||
fa7cd711 | 1529 | /* IPv4LLRoute= is in [Network] section. */ |
2a54a044 | 1530 | r = route_new_static(network, network->filename, section_line, &n); |
fa7cd711 YW |
1531 | if (r < 0) |
1532 | return r; | |
1533 | ||
1534 | r = in_addr_from_string(AF_INET, "169.254.0.0", &n->dst); | |
1535 | if (r < 0) | |
1536 | return r; | |
1537 | ||
1538 | n->family = AF_INET; | |
1539 | n->dst_prefixlen = 16; | |
1540 | n->scope = RT_SCOPE_LINK; | |
94d6e299 YW |
1541 | n->scope_set = true; |
1542 | n->table_set = true; | |
fa7cd711 YW |
1543 | n->priority = IPV4LL_ROUTE_METRIC; |
1544 | n->protocol = RTPROT_STATIC; | |
1545 | ||
1546 | TAKE_PTR(n); | |
1547 | return 0; | |
1548 | } | |
1549 | ||
5d5003ab YW |
1550 | int network_add_default_route_on_device(Network *network) { |
1551 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; | |
2a54a044 | 1552 | unsigned section_line; |
5d5003ab YW |
1553 | int r; |
1554 | ||
1555 | assert(network); | |
1556 | ||
1557 | if (!network->default_route_on_device) | |
1558 | return 0; | |
1559 | ||
2a54a044 YW |
1560 | section_line = hashmap_find_free_section_line(network->routes_by_section); |
1561 | ||
5d5003ab | 1562 | /* DefaultRouteOnDevice= is in [Network] section. */ |
2a54a044 | 1563 | r = route_new_static(network, network->filename, section_line, &n); |
5d5003ab YW |
1564 | if (r < 0) |
1565 | return r; | |
1566 | ||
5d5003ab | 1567 | n->family = AF_INET; |
c697db75 YW |
1568 | n->scope = RT_SCOPE_LINK; |
1569 | n->scope_set = true; | |
1570 | n->protocol = RTPROT_STATIC; | |
5d5003ab YW |
1571 | |
1572 | TAKE_PTR(n); | |
1573 | return 0; | |
1574 | } | |
1575 | ||
27efb52b YW |
1576 | int config_parse_gateway( |
1577 | const char *unit, | |
f579559b TG |
1578 | const char *filename, |
1579 | unsigned line, | |
1580 | const char *section, | |
71a61510 | 1581 | unsigned section_line, |
f579559b TG |
1582 | const char *lvalue, |
1583 | int ltype, | |
1584 | const char *rvalue, | |
1585 | void *data, | |
1586 | void *userdata) { | |
44e7b949 | 1587 | |
6ae115c1 | 1588 | Network *network = userdata; |
fcbf4cb7 | 1589 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
7934dede | 1590 | int r; |
f579559b TG |
1591 | |
1592 | assert(filename); | |
6ae115c1 | 1593 | assert(section); |
f579559b TG |
1594 | assert(lvalue); |
1595 | assert(rvalue); | |
1596 | assert(data); | |
1597 | ||
92fe133a | 1598 | if (streq(section, "Network")) { |
2a54a044 YW |
1599 | /* we are not in an Route section, so use line number instead */ |
1600 | r = route_new_static(network, filename, line, &n); | |
d96edb2c YW |
1601 | if (r == -ENOMEM) |
1602 | return log_oom(); | |
1603 | if (r < 0) { | |
1604 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1605 | "Failed to allocate route, ignoring assignment: %m"); | |
1606 | return 0; | |
1607 | } | |
1985c54f | 1608 | } else { |
f4859fc7 | 1609 | r = route_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1610 | if (r == -ENOMEM) |
1611 | return log_oom(); | |
1612 | if (r < 0) { | |
1613 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1614 | "Failed to allocate route, ignoring assignment: %m"); | |
1615 | return 0; | |
1616 | } | |
1985c54f | 1617 | |
427928ca | 1618 | if (streq(rvalue, "_dhcp")) { |
1985c54f YW |
1619 | n->gateway_from_dhcp = true; |
1620 | TAKE_PTR(n); | |
1621 | return 0; | |
1622 | } | |
1623 | } | |
f579559b | 1624 | |
6dd53981 | 1625 | r = in_addr_from_string_auto(rvalue, &n->gw_family, &n->gw); |
f579559b | 1626 | if (r < 0) { |
d96edb2c | 1627 | log_syntax(unit, LOG_WARNING, filename, line, r, |
01d4e732 | 1628 | "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue); |
f579559b TG |
1629 | return 0; |
1630 | } | |
1631 | ||
aff44301 | 1632 | TAKE_PTR(n); |
f579559b TG |
1633 | return 0; |
1634 | } | |
6ae115c1 | 1635 | |
27efb52b YW |
1636 | int config_parse_preferred_src( |
1637 | const char *unit, | |
0d07e595 JK |
1638 | const char *filename, |
1639 | unsigned line, | |
1640 | const char *section, | |
1641 | unsigned section_line, | |
1642 | const char *lvalue, | |
1643 | int ltype, | |
1644 | const char *rvalue, | |
1645 | void *data, | |
1646 | void *userdata) { | |
1647 | ||
1648 | Network *network = userdata; | |
fcbf4cb7 | 1649 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
7934dede | 1650 | int r; |
0d07e595 JK |
1651 | |
1652 | assert(filename); | |
1653 | assert(section); | |
1654 | assert(lvalue); | |
1655 | assert(rvalue); | |
1656 | assert(data); | |
1657 | ||
f4859fc7 | 1658 | r = route_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1659 | if (r == -ENOMEM) |
1660 | return log_oom(); | |
1661 | if (r < 0) { | |
1662 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1663 | "Failed to allocate route, ignoring assignment: %m"); | |
1664 | return 0; | |
1665 | } | |
0d07e595 | 1666 | |
01d4e732 YW |
1667 | if (n->family == AF_UNSPEC) |
1668 | r = in_addr_from_string_auto(rvalue, &n->family, &n->prefsrc); | |
1669 | else | |
1670 | r = in_addr_from_string(n->family, rvalue, &n->prefsrc); | |
0d07e595 | 1671 | if (r < 0) { |
d96edb2c | 1672 | log_syntax(unit, LOG_WARNING, filename, line, EINVAL, |
01d4e732 | 1673 | "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue); |
0d07e595 JK |
1674 | return 0; |
1675 | } | |
1676 | ||
aff44301 | 1677 | TAKE_PTR(n); |
0d07e595 JK |
1678 | return 0; |
1679 | } | |
1680 | ||
27efb52b YW |
1681 | int config_parse_destination( |
1682 | const char *unit, | |
6ae115c1 TG |
1683 | const char *filename, |
1684 | unsigned line, | |
1685 | const char *section, | |
1686 | unsigned section_line, | |
1687 | const char *lvalue, | |
1688 | int ltype, | |
1689 | const char *rvalue, | |
1690 | void *data, | |
1691 | void *userdata) { | |
44e7b949 | 1692 | |
6ae115c1 | 1693 | Network *network = userdata; |
fcbf4cb7 | 1694 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
7934dede YW |
1695 | union in_addr_union *buffer; |
1696 | unsigned char *prefixlen; | |
ca3bad65 | 1697 | int r; |
6ae115c1 TG |
1698 | |
1699 | assert(filename); | |
1700 | assert(section); | |
1701 | assert(lvalue); | |
1702 | assert(rvalue); | |
1703 | assert(data); | |
1704 | ||
f4859fc7 | 1705 | r = route_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1706 | if (r == -ENOMEM) |
1707 | return log_oom(); | |
1708 | if (r < 0) { | |
1709 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1710 | "Failed to allocate route, ignoring assignment: %m"); | |
1711 | return 0; | |
1712 | } | |
6ae115c1 | 1713 | |
9e7e4408 | 1714 | if (streq(lvalue, "Destination")) { |
7934dede YW |
1715 | buffer = &n->dst; |
1716 | prefixlen = &n->dst_prefixlen; | |
9e7e4408 | 1717 | } else if (streq(lvalue, "Source")) { |
7934dede YW |
1718 | buffer = &n->src; |
1719 | prefixlen = &n->src_prefixlen; | |
9e7e4408 TG |
1720 | } else |
1721 | assert_not_reached(lvalue); | |
1722 | ||
01d4e732 YW |
1723 | if (n->family == AF_UNSPEC) |
1724 | r = in_addr_prefix_from_string_auto(rvalue, &n->family, buffer, prefixlen); | |
1725 | else | |
1726 | r = in_addr_prefix_from_string(rvalue, n->family, buffer, prefixlen); | |
7934dede | 1727 | if (r < 0) { |
d96edb2c | 1728 | log_syntax(unit, LOG_WARNING, filename, line, EINVAL, |
01d4e732 | 1729 | "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue); |
7934dede YW |
1730 | return 0; |
1731 | } | |
1732 | ||
aff44301 | 1733 | TAKE_PTR(n); |
6ae115c1 TG |
1734 | return 0; |
1735 | } | |
5d8e593d | 1736 | |
27efb52b YW |
1737 | int config_parse_route_priority( |
1738 | const char *unit, | |
1739 | const char *filename, | |
1740 | unsigned line, | |
1741 | const char *section, | |
1742 | unsigned section_line, | |
1743 | const char *lvalue, | |
1744 | int ltype, | |
1745 | const char *rvalue, | |
1746 | void *data, | |
1747 | void *userdata) { | |
1748 | ||
5d8e593d | 1749 | Network *network = userdata; |
fcbf4cb7 | 1750 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
5d8e593d SS |
1751 | int r; |
1752 | ||
1753 | assert(filename); | |
1754 | assert(section); | |
1755 | assert(lvalue); | |
1756 | assert(rvalue); | |
1757 | assert(data); | |
1758 | ||
f4859fc7 | 1759 | r = route_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1760 | if (r == -ENOMEM) |
1761 | return log_oom(); | |
1762 | if (r < 0) { | |
1763 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1764 | "Failed to allocate route, ignoring assignment: %m"); | |
1765 | return 0; | |
1766 | } | |
5d8e593d | 1767 | |
aff44301 | 1768 | r = safe_atou32(rvalue, &n->priority); |
1c4b1179 | 1769 | if (r < 0) { |
d96edb2c | 1770 | log_syntax(unit, LOG_WARNING, filename, line, r, |
1c4b1179 SS |
1771 | "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue); |
1772 | return 0; | |
1773 | } | |
5d8e593d | 1774 | |
aff44301 | 1775 | TAKE_PTR(n); |
5d8e593d SS |
1776 | return 0; |
1777 | } | |
769b56a3 | 1778 | |
27efb52b YW |
1779 | int config_parse_route_scope( |
1780 | const char *unit, | |
1781 | const char *filename, | |
1782 | unsigned line, | |
1783 | const char *section, | |
1784 | unsigned section_line, | |
1785 | const char *lvalue, | |
1786 | int ltype, | |
1787 | const char *rvalue, | |
1788 | void *data, | |
1789 | void *userdata) { | |
1790 | ||
769b56a3 | 1791 | Network *network = userdata; |
fcbf4cb7 | 1792 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
769b56a3 TG |
1793 | int r; |
1794 | ||
1795 | assert(filename); | |
1796 | assert(section); | |
1797 | assert(lvalue); | |
1798 | assert(rvalue); | |
1799 | assert(data); | |
1800 | ||
f4859fc7 | 1801 | r = route_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1802 | if (r == -ENOMEM) |
1803 | return log_oom(); | |
1804 | if (r < 0) { | |
1805 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1806 | "Failed to allocate route, ignoring assignment: %m"); | |
1807 | return 0; | |
1808 | } | |
769b56a3 | 1809 | |
41b90a1e YW |
1810 | r = route_scope_from_string(rvalue); |
1811 | if (r < 0) { | |
d96edb2c | 1812 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown route scope: %s", rvalue); |
769b56a3 TG |
1813 | return 0; |
1814 | } | |
1815 | ||
41b90a1e | 1816 | n->scope = r; |
94d6e299 | 1817 | n->scope_set = true; |
aff44301 | 1818 | TAKE_PTR(n); |
769b56a3 TG |
1819 | return 0; |
1820 | } | |
c953b24c | 1821 | |
27efb52b YW |
1822 | int config_parse_route_table( |
1823 | const char *unit, | |
1824 | const char *filename, | |
1825 | unsigned line, | |
1826 | const char *section, | |
1827 | unsigned section_line, | |
1828 | const char *lvalue, | |
1829 | int ltype, | |
1830 | const char *rvalue, | |
1831 | void *data, | |
1832 | void *userdata) { | |
1833 | ||
fcbf4cb7 | 1834 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
c953b24c | 1835 | Network *network = userdata; |
c953b24c SS |
1836 | int r; |
1837 | ||
1838 | assert(filename); | |
1839 | assert(section); | |
1840 | assert(lvalue); | |
1841 | assert(rvalue); | |
1842 | assert(data); | |
1843 | ||
f4859fc7 | 1844 | r = route_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1845 | if (r == -ENOMEM) |
1846 | return log_oom(); | |
1847 | if (r < 0) { | |
1848 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1849 | "Failed to allocate route, ignoring assignment: %m"); | |
1850 | return 0; | |
1851 | } | |
c953b24c | 1852 | |
41b90a1e YW |
1853 | r = route_table_from_string(rvalue); |
1854 | if (r >= 0) | |
1855 | n->table = r; | |
1856 | else { | |
1857 | r = safe_atou32(rvalue, &n->table); | |
1858 | if (r < 0) { | |
d96edb2c | 1859 | log_syntax(unit, LOG_WARNING, filename, line, r, |
41b90a1e YW |
1860 | "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue); |
1861 | return 0; | |
1862 | } | |
c953b24c SS |
1863 | } |
1864 | ||
94d6e299 | 1865 | n->table_set = true; |
aff44301 | 1866 | TAKE_PTR(n); |
c953b24c SS |
1867 | return 0; |
1868 | } | |
28959f7d | 1869 | |
d96edb2c | 1870 | int config_parse_route_boolean( |
27efb52b YW |
1871 | const char *unit, |
1872 | const char *filename, | |
1873 | unsigned line, | |
1874 | const char *section, | |
1875 | unsigned section_line, | |
1876 | const char *lvalue, | |
1877 | int ltype, | |
1878 | const char *rvalue, | |
1879 | void *data, | |
1880 | void *userdata) { | |
1881 | ||
28959f7d | 1882 | Network *network = userdata; |
fcbf4cb7 | 1883 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
28959f7d SS |
1884 | int r; |
1885 | ||
1886 | assert(filename); | |
1887 | assert(section); | |
1888 | assert(lvalue); | |
1889 | assert(rvalue); | |
1890 | assert(data); | |
1891 | ||
1892 | r = route_new_static(network, filename, section_line, &n); | |
d96edb2c YW |
1893 | if (r == -ENOMEM) |
1894 | return log_oom(); | |
1895 | if (r < 0) { | |
1896 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1897 | "Failed to allocate route, ignoring assignment: %m"); | |
1898 | return 0; | |
1899 | } | |
28959f7d SS |
1900 | |
1901 | r = parse_boolean(rvalue); | |
1902 | if (r < 0) { | |
d96edb2c | 1903 | log_syntax(unit, LOG_WARNING, filename, line, r, |
9cb8c559 | 1904 | "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue, rvalue); |
28959f7d SS |
1905 | return 0; |
1906 | } | |
1907 | ||
d96edb2c YW |
1908 | if (STR_IN_SET(lvalue, "GatewayOnLink", "GatewayOnlink")) |
1909 | n->gateway_onlink = r; | |
1910 | else if (streq(lvalue, "QuickAck")) | |
1911 | n->quickack = r; | |
1912 | else if (streq(lvalue, "FastOpenNoCookie")) | |
1913 | n->fast_open_no_cookie = r; | |
1914 | else if (streq(lvalue, "TTLPropagate")) | |
1915 | n->ttl_propagate = r; | |
1916 | else | |
1917 | assert_not_reached("Invalid lvalue"); | |
54901fd2 | 1918 | |
aff44301 | 1919 | TAKE_PTR(n); |
b5bf6f64 SS |
1920 | return 0; |
1921 | } | |
1922 | ||
27efb52b YW |
1923 | int config_parse_ipv6_route_preference( |
1924 | const char *unit, | |
1925 | const char *filename, | |
1926 | unsigned line, | |
1927 | const char *section, | |
1928 | unsigned section_line, | |
1929 | const char *lvalue, | |
1930 | int ltype, | |
1931 | const char *rvalue, | |
1932 | void *data, | |
1933 | void *userdata) { | |
1934 | ||
b5bf6f64 | 1935 | Network *network = userdata; |
fcbf4cb7 | 1936 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
b5bf6f64 SS |
1937 | int r; |
1938 | ||
4c7bd9cf | 1939 | r = route_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1940 | if (r == -ENOMEM) |
1941 | return log_oom(); | |
1942 | if (r < 0) { | |
1943 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1944 | "Failed to allocate route, ignoring assignment: %m"); | |
1945 | return 0; | |
1946 | } | |
4c7bd9cf | 1947 | |
b5bf6f64 SS |
1948 | if (streq(rvalue, "low")) |
1949 | n->pref = ICMPV6_ROUTER_PREF_LOW; | |
1950 | else if (streq(rvalue, "medium")) | |
1951 | n->pref = ICMPV6_ROUTER_PREF_MEDIUM; | |
1952 | else if (streq(rvalue, "high")) | |
1953 | n->pref = ICMPV6_ROUTER_PREF_HIGH; | |
1954 | else { | |
d96edb2c | 1955 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown route preference: %s", rvalue); |
b5bf6f64 SS |
1956 | return 0; |
1957 | } | |
28959f7d | 1958 | |
aff44301 | 1959 | TAKE_PTR(n); |
28959f7d SS |
1960 | return 0; |
1961 | } | |
c83ecc04 | 1962 | |
27efb52b YW |
1963 | int config_parse_route_protocol( |
1964 | const char *unit, | |
1965 | const char *filename, | |
1966 | unsigned line, | |
1967 | const char *section, | |
1968 | unsigned section_line, | |
1969 | const char *lvalue, | |
1970 | int ltype, | |
1971 | const char *rvalue, | |
1972 | void *data, | |
1973 | void *userdata) { | |
1974 | ||
c83ecc04 | 1975 | Network *network = userdata; |
fcbf4cb7 | 1976 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
c83ecc04 SS |
1977 | int r; |
1978 | ||
1979 | r = route_new_static(network, filename, section_line, &n); | |
d96edb2c YW |
1980 | if (r == -ENOMEM) |
1981 | return log_oom(); | |
1982 | if (r < 0) { | |
1983 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1984 | "Failed to allocate route, ignoring assignment: %m"); | |
1985 | return 0; | |
1986 | } | |
c83ecc04 | 1987 | |
1b64651b YW |
1988 | r = route_protocol_from_string(rvalue); |
1989 | if (r >= 0) | |
1990 | n->protocol = r; | |
c83ecc04 SS |
1991 | else { |
1992 | r = safe_atou8(rvalue , &n->protocol); | |
1993 | if (r < 0) { | |
d96edb2c | 1994 | log_syntax(unit, LOG_WARNING, filename, line, r, |
f205a92a | 1995 | "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue); |
c83ecc04 SS |
1996 | return 0; |
1997 | } | |
1998 | } | |
1999 | ||
aff44301 | 2000 | TAKE_PTR(n); |
c83ecc04 SS |
2001 | return 0; |
2002 | } | |
983226f3 | 2003 | |
27efb52b YW |
2004 | int config_parse_route_type( |
2005 | const char *unit, | |
2006 | const char *filename, | |
2007 | unsigned line, | |
2008 | const char *section, | |
2009 | unsigned section_line, | |
2010 | const char *lvalue, | |
2011 | int ltype, | |
2012 | const char *rvalue, | |
2013 | void *data, | |
2014 | void *userdata) { | |
2015 | ||
983226f3 | 2016 | Network *network = userdata; |
fcbf4cb7 | 2017 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
7a22312d | 2018 | int t, r; |
983226f3 SS |
2019 | |
2020 | r = route_new_static(network, filename, section_line, &n); | |
d96edb2c YW |
2021 | if (r == -ENOMEM) |
2022 | return log_oom(); | |
2023 | if (r < 0) { | |
2024 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
2025 | "Failed to allocate route, ignoring assignment: %m"); | |
2026 | return 0; | |
2027 | } | |
983226f3 | 2028 | |
7a22312d YW |
2029 | t = route_type_from_string(rvalue); |
2030 | if (t < 0) { | |
d96edb2c | 2031 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
f205a92a | 2032 | "Could not parse route type \"%s\", ignoring assignment: %m", rvalue); |
983226f3 SS |
2033 | return 0; |
2034 | } | |
2035 | ||
7a22312d YW |
2036 | n->type = (unsigned char) t; |
2037 | ||
aff44301 | 2038 | TAKE_PTR(n); |
983226f3 SS |
2039 | return 0; |
2040 | } | |
323d9329 | 2041 | |
27efb52b YW |
2042 | int config_parse_tcp_window( |
2043 | const char *unit, | |
2044 | const char *filename, | |
2045 | unsigned line, | |
2046 | const char *section, | |
2047 | unsigned section_line, | |
2048 | const char *lvalue, | |
2049 | int ltype, | |
2050 | const char *rvalue, | |
2051 | void *data, | |
2052 | void *userdata) { | |
2053 | ||
fcbf4cb7 | 2054 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
6b21ad33 | 2055 | Network *network = userdata; |
fef160b5 | 2056 | uint32_t k; |
323d9329 SS |
2057 | int r; |
2058 | ||
2059 | assert(filename); | |
2060 | assert(section); | |
2061 | assert(lvalue); | |
2062 | assert(rvalue); | |
2063 | assert(data); | |
2064 | ||
2065 | r = route_new_static(network, filename, section_line, &n); | |
d96edb2c YW |
2066 | if (r == -ENOMEM) |
2067 | return log_oom(); | |
2068 | if (r < 0) { | |
2069 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
2070 | "Failed to allocate route, ignoring assignment: %m"); | |
2071 | return 0; | |
2072 | } | |
323d9329 | 2073 | |
fef160b5 | 2074 | r = safe_atou32(rvalue, &k); |
f205a92a | 2075 | if (r < 0) { |
d96edb2c | 2076 | log_syntax(unit, LOG_WARNING, filename, line, r, |
f205a92a YW |
2077 | "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue, rvalue); |
2078 | return 0; | |
2079 | } | |
fef160b5 | 2080 | if (k >= 1024) { |
d96edb2c | 2081 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
f205a92a | 2082 | "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue, rvalue); |
323d9329 SS |
2083 | return 0; |
2084 | } | |
2085 | ||
2086 | if (streq(lvalue, "InitialCongestionWindow")) | |
2087 | n->initcwnd = k; | |
2088 | else if (streq(lvalue, "InitialAdvertisedReceiveWindow")) | |
2089 | n->initrwnd = k; | |
f205a92a YW |
2090 | else |
2091 | assert_not_reached("Invalid TCP window type."); | |
323d9329 | 2092 | |
aff44301 | 2093 | TAKE_PTR(n); |
323d9329 SS |
2094 | return 0; |
2095 | } | |
09f5dfad | 2096 | |
cea79e66 SS |
2097 | int config_parse_route_mtu( |
2098 | const char *unit, | |
2099 | const char *filename, | |
2100 | unsigned line, | |
2101 | const char *section, | |
2102 | unsigned section_line, | |
2103 | const char *lvalue, | |
2104 | int ltype, | |
2105 | const char *rvalue, | |
2106 | void *data, | |
2107 | void *userdata) { | |
2108 | ||
2109 | Network *network = userdata; | |
fcbf4cb7 | 2110 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; |
cea79e66 SS |
2111 | int r; |
2112 | ||
2113 | assert(filename); | |
2114 | assert(section); | |
2115 | assert(lvalue); | |
2116 | assert(rvalue); | |
2117 | assert(data); | |
2118 | ||
2119 | r = route_new_static(network, filename, section_line, &n); | |
d96edb2c YW |
2120 | if (r == -ENOMEM) |
2121 | return log_oom(); | |
2122 | if (r < 0) { | |
2123 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
2124 | "Failed to allocate route, ignoring assignment: %m"); | |
2125 | return 0; | |
2126 | } | |
cea79e66 SS |
2127 | |
2128 | r = config_parse_mtu(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &n->mtu, userdata); | |
2129 | if (r < 0) | |
2130 | return r; | |
2131 | ||
aff44301 | 2132 | TAKE_PTR(n); |
cea79e66 SS |
2133 | return 0; |
2134 | } | |
fcbf4cb7 | 2135 | |
6ff5cc6b YW |
2136 | int config_parse_multipath_route( |
2137 | const char *unit, | |
2138 | const char *filename, | |
2139 | unsigned line, | |
2140 | const char *section, | |
2141 | unsigned section_line, | |
2142 | const char *lvalue, | |
2143 | int ltype, | |
2144 | const char *rvalue, | |
2145 | void *data, | |
2146 | void *userdata) { | |
2147 | ||
2148 | _cleanup_(route_free_or_set_invalidp) Route *n = NULL; | |
2149 | _cleanup_free_ char *word = NULL, *buf = NULL; | |
2150 | _cleanup_free_ MultipathRoute *m = NULL; | |
2151 | Network *network = userdata; | |
2152 | const char *p, *ip, *dev; | |
2153 | union in_addr_union a; | |
2154 | int family, r; | |
2155 | ||
2156 | assert(filename); | |
2157 | assert(section); | |
2158 | assert(lvalue); | |
2159 | assert(rvalue); | |
2160 | assert(data); | |
2161 | ||
2162 | r = route_new_static(network, filename, section_line, &n); | |
d96edb2c YW |
2163 | if (r == -ENOMEM) |
2164 | return log_oom(); | |
2165 | if (r < 0) { | |
2166 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
2167 | "Failed to allocate route, ignoring assignment: %m"); | |
2168 | return 0; | |
2169 | } | |
6ff5cc6b YW |
2170 | |
2171 | if (isempty(rvalue)) { | |
2172 | n->multipath_routes = ordered_set_free_free(n->multipath_routes); | |
2173 | return 0; | |
2174 | } | |
2175 | ||
2176 | m = new0(MultipathRoute, 1); | |
2177 | if (!m) | |
2178 | return log_oom(); | |
2179 | ||
2180 | p = rvalue; | |
2181 | r = extract_first_word(&p, &word, NULL, 0); | |
2182 | if (r == -ENOMEM) | |
2183 | return log_oom(); | |
2184 | if (r <= 0) { | |
d96edb2c | 2185 | log_syntax(unit, LOG_WARNING, filename, line, r, |
6ff5cc6b YW |
2186 | "Invalid multipath route option, ignoring assignment: %s", rvalue); |
2187 | return 0; | |
2188 | } | |
2189 | ||
2190 | dev = strchr(word, '@'); | |
2191 | if (dev) { | |
2192 | buf = strndup(word, dev - word); | |
2193 | if (!buf) | |
2194 | return log_oom(); | |
2195 | ip = buf; | |
2196 | dev++; | |
2197 | } else | |
2198 | ip = word; | |
2199 | ||
2200 | r = in_addr_from_string_auto(ip, &family, &a); | |
2201 | if (r < 0) { | |
d96edb2c | 2202 | log_syntax(unit, LOG_WARNING, filename, line, r, |
6ff5cc6b YW |
2203 | "Invalid multipath route gateway '%s', ignoring assignment: %m", rvalue); |
2204 | return 0; | |
2205 | } | |
2206 | m->gateway.address = a; | |
2207 | m->gateway.family = family; | |
2208 | ||
2209 | if (dev) { | |
d308bb99 | 2210 | r = resolve_interface(NULL, dev); |
6ff5cc6b | 2211 | if (r < 0) { |
d96edb2c | 2212 | log_syntax(unit, LOG_WARNING, filename, line, r, |
6ff5cc6b YW |
2213 | "Invalid interface name or index, ignoring assignment: %s", dev); |
2214 | return 0; | |
2215 | } | |
597da51b | 2216 | m->ifindex = r; |
6ff5cc6b YW |
2217 | } |
2218 | ||
2219 | if (!isempty(p)) { | |
2220 | r = safe_atou32(p, &m->weight); | |
2221 | if (r < 0) { | |
d96edb2c | 2222 | log_syntax(unit, LOG_WARNING, filename, line, r, |
6ff5cc6b YW |
2223 | "Invalid multipath route weight, ignoring assignment: %s", p); |
2224 | return 0; | |
2225 | } | |
2226 | if (m->weight == 0 || m->weight > 256) { | |
d96edb2c | 2227 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
6ff5cc6b YW |
2228 | "Invalid multipath route weight, ignoring assignment: %s", p); |
2229 | return 0; | |
2230 | } | |
2231 | } | |
2232 | ||
2233 | r = ordered_set_ensure_allocated(&n->multipath_routes, NULL); | |
2234 | if (r < 0) | |
2235 | return log_oom(); | |
2236 | ||
2237 | r = ordered_set_put(n->multipath_routes, m); | |
2238 | if (r < 0) { | |
d96edb2c | 2239 | log_syntax(unit, LOG_WARNING, filename, line, r, |
6ff5cc6b YW |
2240 | "Failed to store multipath route, ignoring assignment: %m"); |
2241 | return 0; | |
2242 | } | |
2243 | ||
2244 | TAKE_PTR(m); | |
2245 | TAKE_PTR(n); | |
2246 | return 0; | |
2247 | } | |
2248 | ||
d9940a3f | 2249 | static int route_section_verify(Route *route, Network *network) { |
fcbf4cb7 YW |
2250 | if (section_is_invalid(route->section)) |
2251 | return -EINVAL; | |
2252 | ||
6dd53981 YW |
2253 | if (route->family == AF_UNSPEC) |
2254 | route->family = route->gw_family; | |
2255 | ||
fcbf4cb7 YW |
2256 | if (route->family == AF_UNSPEC) { |
2257 | assert(route->section); | |
2258 | ||
2259 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
2260 | "%s: Route section without Gateway=, Destination=, Source=, " | |
2261 | "or PreferredSource= field configured. " | |
2262 | "Ignoring [Route] section from line %u.", | |
2263 | route->section->filename, route->section->line); | |
2264 | } | |
2265 | ||
6dd53981 YW |
2266 | if (route->family == AF_INET6 && route->gw_family == AF_INET) |
2267 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
2268 | "%s: IPv4 gateway is configured for IPv6 route. " | |
2269 | "Ignoring [Route] section from line %u.", | |
2270 | route->section->filename, route->section->line); | |
2271 | ||
c0d48bc5 YW |
2272 | if (!route->table_set && network->vrf) { |
2273 | route->table = VRF(network->vrf)->table; | |
2274 | route->table_set = true; | |
2275 | } | |
2276 | ||
f5c38922 YW |
2277 | if (!route->table_set && IN_SET(route->type, RTN_LOCAL, RTN_BROADCAST, RTN_ANYCAST, RTN_NAT)) |
2278 | route->table = RT_TABLE_LOCAL; | |
2279 | ||
2280 | if (!route->scope_set && route->family != AF_INET6) { | |
2281 | if (IN_SET(route->type, RTN_LOCAL, RTN_NAT)) | |
2282 | route->scope = RT_SCOPE_HOST; | |
2283 | else if (IN_SET(route->type, RTN_BROADCAST, RTN_ANYCAST, RTN_MULTICAST)) | |
2284 | route->scope = RT_SCOPE_LINK; | |
94d6e299 YW |
2285 | } |
2286 | ||
fd7701bf YW |
2287 | if (route->scope != RT_SCOPE_UNIVERSE && route->family == AF_INET6) { |
2288 | log_warning("%s: Scope= is specified for IPv6 route. It will be ignored.", route->section->filename); | |
2289 | route->scope = RT_SCOPE_UNIVERSE; | |
2290 | } | |
2291 | ||
8973df5c YW |
2292 | if (route->family == AF_INET6 && route->priority == 0) |
2293 | route->priority = IP6_RT_PRIO_USER; | |
2294 | ||
9cd9fc8f | 2295 | if (ordered_hashmap_isempty(network->addresses_by_section) && |
6dd53981 | 2296 | in_addr_is_null(route->gw_family, &route->gw) == 0 && |
fcbf4cb7 YW |
2297 | route->gateway_onlink < 0) { |
2298 | log_warning("%s: Gateway= without static address configured. " | |
2299 | "Enabling GatewayOnLink= option.", | |
2300 | network->filename); | |
2301 | route->gateway_onlink = true; | |
2302 | } | |
2303 | ||
2304 | return 0; | |
2305 | } | |
d9940a3f | 2306 | |
13ffa39f | 2307 | void network_drop_invalid_routes(Network *network) { |
2a54a044 | 2308 | Route *route; |
d9940a3f YW |
2309 | |
2310 | assert(network); | |
2311 | ||
2a54a044 | 2312 | HASHMAP_FOREACH(route, network->routes_by_section) |
d9940a3f YW |
2313 | if (route_section_verify(route, network) < 0) |
2314 | route_free(route); | |
2315 | } |