]>
Commit | Line | Data |
---|---|---|
60f067b4 JS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2013 Tom Gundersen <teg@jklm.no> | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
db2df898 | 20 | #include "alloc-util.h" |
60f067b4 | 21 | #include "conf-parser.h" |
db2df898 | 22 | #include "in-addr-util.h" |
d9dfd233 | 23 | #include "netlink-util.h" |
d9dfd233 | 24 | #include "networkd-route.h" |
db2df898 MP |
25 | #include "networkd.h" |
26 | #include "parse-util.h" | |
27 | #include "set.h" | |
28 | #include "string-util.h" | |
29 | #include "util.h" | |
30 | ||
5a920b42 MP |
31 | #define ROUTES_PER_LINK_MAX 2048U |
32 | #define STATIC_ROUTES_PER_NETWORK_MAX 1024U | |
33 | ||
db2df898 MP |
34 | int route_new(Route **ret) { |
35 | _cleanup_route_free_ Route *route = NULL; | |
36 | ||
37 | route = new0(Route, 1); | |
38 | if (!route) | |
39 | return -ENOMEM; | |
40 | ||
41 | route->family = AF_UNSPEC; | |
42 | route->scope = RT_SCOPE_UNIVERSE; | |
43 | route->protocol = RTPROT_UNSPEC; | |
44 | route->table = RT_TABLE_DEFAULT; | |
45 | route->lifetime = USEC_INFINITY; | |
46 | ||
47 | *ret = route; | |
48 | route = NULL; | |
49 | ||
50 | return 0; | |
51 | } | |
60f067b4 JS |
52 | |
53 | int route_new_static(Network *network, unsigned section, Route **ret) { | |
54 | _cleanup_route_free_ Route *route = NULL; | |
db2df898 | 55 | int r; |
60f067b4 | 56 | |
5a920b42 MP |
57 | assert(network); |
58 | assert(ret); | |
59 | ||
60f067b4 | 60 | if (section) { |
aa27b158 | 61 | route = hashmap_get(network->routes_by_section, UINT_TO_PTR(section)); |
60f067b4 JS |
62 | if (route) { |
63 | *ret = route; | |
64 | route = NULL; | |
65 | ||
66 | return 0; | |
67 | } | |
68 | } | |
69 | ||
5a920b42 MP |
70 | if (network->n_static_routes >= STATIC_ROUTES_PER_NETWORK_MAX) |
71 | return -E2BIG; | |
72 | ||
db2df898 MP |
73 | r = route_new(&route); |
74 | if (r < 0) | |
75 | return r; | |
60f067b4 | 76 | |
5eef597e | 77 | route->protocol = RTPROT_STATIC; |
60f067b4 JS |
78 | |
79 | if (section) { | |
80 | route->section = section; | |
aa27b158 MP |
81 | |
82 | r = hashmap_put(network->routes_by_section, UINT_TO_PTR(route->section), route); | |
83 | if (r < 0) | |
84 | return r; | |
60f067b4 JS |
85 | } |
86 | ||
aa27b158 MP |
87 | route->network = network; |
88 | LIST_PREPEND(routes, network->static_routes, route); | |
5a920b42 | 89 | network->n_static_routes++; |
aa27b158 | 90 | |
60f067b4 JS |
91 | *ret = route; |
92 | route = NULL; | |
93 | ||
94 | return 0; | |
95 | } | |
96 | ||
60f067b4 JS |
97 | void route_free(Route *route) { |
98 | if (!route) | |
99 | return; | |
100 | ||
101 | if (route->network) { | |
102 | LIST_REMOVE(routes, route->network->static_routes, route); | |
103 | ||
5a920b42 MP |
104 | assert(route->network->n_static_routes > 0); |
105 | route->network->n_static_routes--; | |
106 | ||
60f067b4 | 107 | if (route->section) |
5a920b42 | 108 | hashmap_remove(route->network->routes_by_section, UINT_TO_PTR(route->section)); |
60f067b4 JS |
109 | } |
110 | ||
db2df898 MP |
111 | if (route->link) { |
112 | set_remove(route->link->routes, route); | |
113 | set_remove(route->link->routes_foreign, route); | |
114 | } | |
115 | ||
116 | sd_event_source_unref(route->expire); | |
117 | ||
60f067b4 JS |
118 | free(route); |
119 | } | |
120 | ||
db2df898 MP |
121 | static void route_hash_func(const void *b, struct siphash *state) { |
122 | const Route *route = b; | |
123 | ||
124 | assert(route); | |
125 | ||
126 | siphash24_compress(&route->family, sizeof(route->family), state); | |
127 | ||
128 | switch (route->family) { | |
129 | case AF_INET: | |
130 | case AF_INET6: | |
131 | /* Equality of routes are given by the 4-touple | |
132 | (dst_prefix,dst_prefixlen,tos,priority,table) */ | |
133 | siphash24_compress(&route->dst, FAMILY_ADDRESS_SIZE(route->family), state); | |
134 | siphash24_compress(&route->dst_prefixlen, sizeof(route->dst_prefixlen), state); | |
135 | siphash24_compress(&route->tos, sizeof(route->tos), state); | |
136 | siphash24_compress(&route->priority, sizeof(route->priority), state); | |
137 | siphash24_compress(&route->table, sizeof(route->table), state); | |
138 | ||
139 | break; | |
140 | default: | |
141 | /* treat any other address family as AF_UNSPEC */ | |
142 | break; | |
143 | } | |
144 | } | |
145 | ||
146 | static int route_compare_func(const void *_a, const void *_b) { | |
147 | const Route *a = _a, *b = _b; | |
148 | ||
149 | if (a->family < b->family) | |
150 | return -1; | |
151 | if (a->family > b->family) | |
152 | return 1; | |
153 | ||
154 | switch (a->family) { | |
155 | case AF_INET: | |
156 | case AF_INET6: | |
157 | if (a->dst_prefixlen < b->dst_prefixlen) | |
158 | return -1; | |
159 | if (a->dst_prefixlen > b->dst_prefixlen) | |
160 | return 1; | |
161 | ||
162 | if (a->tos < b->tos) | |
163 | return -1; | |
164 | if (a->tos > b->tos) | |
165 | return 1; | |
166 | ||
167 | if (a->priority < b->priority) | |
168 | return -1; | |
169 | if (a->priority > b->priority) | |
170 | return 1; | |
171 | ||
172 | if (a->table < b->table) | |
173 | return -1; | |
174 | if (a->table > b->table) | |
175 | return 1; | |
176 | ||
177 | return memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family)); | |
178 | default: | |
179 | /* treat any other address family as AF_UNSPEC */ | |
180 | return 0; | |
181 | } | |
182 | } | |
183 | ||
184 | static const struct hash_ops route_hash_ops = { | |
185 | .hash = route_hash_func, | |
186 | .compare = route_compare_func | |
187 | }; | |
188 | ||
189 | int route_get(Link *link, | |
190 | int family, | |
5a920b42 | 191 | const union in_addr_union *dst, |
db2df898 MP |
192 | unsigned char dst_prefixlen, |
193 | unsigned char tos, | |
194 | uint32_t priority, | |
195 | unsigned char table, | |
196 | Route **ret) { | |
5a920b42 MP |
197 | |
198 | Route route, *existing; | |
199 | ||
200 | assert(link); | |
201 | assert(dst); | |
202 | ||
203 | route = (Route) { | |
db2df898 | 204 | .family = family, |
5a920b42 | 205 | .dst = *dst, |
db2df898 MP |
206 | .dst_prefixlen = dst_prefixlen, |
207 | .tos = tos, | |
208 | .priority = priority, | |
209 | .table = table, | |
5a920b42 | 210 | }; |
db2df898 MP |
211 | |
212 | existing = set_get(link->routes, &route); | |
213 | if (existing) { | |
5a920b42 MP |
214 | if (ret) |
215 | *ret = existing; | |
db2df898 | 216 | return 1; |
db2df898 MP |
217 | } |
218 | ||
5a920b42 MP |
219 | existing = set_get(link->routes_foreign, &route); |
220 | if (existing) { | |
221 | if (ret) | |
222 | *ret = existing; | |
223 | return 0; | |
224 | } | |
db2df898 | 225 | |
5a920b42 | 226 | return -ENOENT; |
db2df898 MP |
227 | } |
228 | ||
5a920b42 MP |
229 | static int route_add_internal( |
230 | Link *link, | |
231 | Set **routes, | |
232 | int family, | |
233 | const union in_addr_union *dst, | |
234 | unsigned char dst_prefixlen, | |
235 | unsigned char tos, | |
236 | uint32_t priority, | |
237 | unsigned char table, | |
238 | Route **ret) { | |
239 | ||
db2df898 MP |
240 | _cleanup_route_free_ Route *route = NULL; |
241 | int r; | |
242 | ||
243 | assert(link); | |
244 | assert(routes); | |
245 | assert(dst); | |
246 | ||
247 | r = route_new(&route); | |
248 | if (r < 0) | |
249 | return r; | |
250 | ||
251 | route->family = family; | |
252 | route->dst = *dst; | |
253 | route->dst_prefixlen = dst_prefixlen; | |
254 | route->tos = tos; | |
255 | route->priority = priority; | |
256 | route->table = table; | |
257 | ||
258 | r = set_ensure_allocated(routes, &route_hash_ops); | |
259 | if (r < 0) | |
260 | return r; | |
261 | ||
262 | r = set_put(*routes, route); | |
263 | if (r < 0) | |
264 | return r; | |
265 | ||
266 | route->link = link; | |
267 | ||
268 | if (ret) | |
269 | *ret = route; | |
270 | ||
271 | route = NULL; | |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
5a920b42 MP |
276 | int route_add_foreign( |
277 | Link *link, | |
278 | int family, | |
279 | const union in_addr_union *dst, | |
280 | unsigned char dst_prefixlen, | |
281 | unsigned char tos, | |
282 | uint32_t priority, | |
283 | unsigned char table, | |
284 | Route **ret) { | |
285 | ||
db2df898 MP |
286 | return route_add_internal(link, &link->routes_foreign, family, dst, dst_prefixlen, tos, priority, table, ret); |
287 | } | |
288 | ||
5a920b42 MP |
289 | int route_add( |
290 | Link *link, | |
db2df898 | 291 | int family, |
5a920b42 | 292 | const union in_addr_union *dst, |
db2df898 MP |
293 | unsigned char dst_prefixlen, |
294 | unsigned char tos, | |
295 | uint32_t priority, | |
5a920b42 MP |
296 | unsigned char table, |
297 | Route **ret) { | |
298 | ||
db2df898 MP |
299 | Route *route; |
300 | int r; | |
301 | ||
302 | r = route_get(link, family, dst, dst_prefixlen, tos, priority, table, &route); | |
303 | if (r == -ENOENT) { | |
304 | /* Route does not exist, create a new one */ | |
305 | r = route_add_internal(link, &link->routes, family, dst, dst_prefixlen, tos, priority, table, &route); | |
306 | if (r < 0) | |
307 | return r; | |
308 | } else if (r == 0) { | |
309 | /* Take over a foreign route */ | |
310 | r = set_ensure_allocated(&link->routes, &route_hash_ops); | |
311 | if (r < 0) | |
312 | return r; | |
313 | ||
314 | r = set_put(link->routes, route); | |
315 | if (r < 0) | |
316 | return r; | |
317 | ||
318 | set_remove(link->routes_foreign, route); | |
319 | } else if (r == 1) { | |
320 | /* Route exists, do nothing */ | |
321 | ; | |
322 | } else | |
323 | return r; | |
324 | ||
325 | *ret = route; | |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
330 | int route_update(Route *route, | |
5a920b42 | 331 | const union in_addr_union *src, |
db2df898 | 332 | unsigned char src_prefixlen, |
5a920b42 MP |
333 | const union in_addr_union *gw, |
334 | const union in_addr_union *prefsrc, | |
db2df898 MP |
335 | unsigned char scope, |
336 | unsigned char protocol) { | |
5a920b42 | 337 | |
db2df898 MP |
338 | assert(route); |
339 | assert(src); | |
340 | assert(gw); | |
341 | assert(prefsrc); | |
342 | ||
343 | route->src = *src; | |
344 | route->src_prefixlen = src_prefixlen; | |
345 | route->gw = *gw; | |
346 | route->prefsrc = *prefsrc; | |
347 | route->scope = scope; | |
348 | route->protocol = protocol; | |
349 | ||
350 | return 0; | |
351 | } | |
352 | ||
db2df898 | 353 | int route_remove(Route *route, Link *link, |
86f210e9 | 354 | sd_netlink_message_handler_t callback) { |
4c89c718 | 355 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
60f067b4 JS |
356 | int r; |
357 | ||
358 | assert(link); | |
359 | assert(link->manager); | |
360 | assert(link->manager->rtnl); | |
361 | assert(link->ifindex > 0); | |
362 | assert(route->family == AF_INET || route->family == AF_INET6); | |
363 | ||
364 | r = sd_rtnl_message_new_route(link->manager->rtnl, &req, | |
5eef597e MP |
365 | RTM_DELROUTE, route->family, |
366 | route->protocol); | |
f47781d8 MP |
367 | if (r < 0) |
368 | return log_error_errno(r, "Could not create RTM_DELROUTE message: %m"); | |
60f067b4 | 369 | |
db2df898 | 370 | if (!in_addr_is_null(route->family, &route->gw)) { |
f47781d8 | 371 | if (route->family == AF_INET) |
db2df898 | 372 | r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &route->gw.in); |
f47781d8 | 373 | else if (route->family == AF_INET6) |
db2df898 | 374 | r = sd_netlink_message_append_in6_addr(req, RTA_GATEWAY, &route->gw.in6); |
f47781d8 MP |
375 | if (r < 0) |
376 | return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m"); | |
60f067b4 JS |
377 | } |
378 | ||
379 | if (route->dst_prefixlen) { | |
380 | if (route->family == AF_INET) | |
db2df898 | 381 | r = sd_netlink_message_append_in_addr(req, RTA_DST, &route->dst.in); |
60f067b4 | 382 | else if (route->family == AF_INET6) |
db2df898 | 383 | r = sd_netlink_message_append_in6_addr(req, RTA_DST, &route->dst.in6); |
f47781d8 MP |
384 | if (r < 0) |
385 | return log_error_errno(r, "Could not append RTA_DST attribute: %m"); | |
60f067b4 JS |
386 | |
387 | r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen); | |
f47781d8 MP |
388 | if (r < 0) |
389 | return log_error_errno(r, "Could not set destination prefix length: %m"); | |
390 | } | |
391 | ||
392 | if (route->src_prefixlen) { | |
393 | if (route->family == AF_INET) | |
db2df898 | 394 | r = sd_netlink_message_append_in_addr(req, RTA_SRC, &route->src.in); |
f47781d8 | 395 | else if (route->family == AF_INET6) |
db2df898 | 396 | r = sd_netlink_message_append_in6_addr(req, RTA_SRC, &route->src.in6); |
f47781d8 | 397 | if (r < 0) |
aa27b158 | 398 | return log_error_errno(r, "Could not append RTA_SRC attribute: %m"); |
f47781d8 MP |
399 | |
400 | r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen); | |
401 | if (r < 0) | |
402 | return log_error_errno(r, "Could not set source prefix length: %m"); | |
60f067b4 JS |
403 | } |
404 | ||
db2df898 | 405 | if (!in_addr_is_null(route->family, &route->prefsrc)) { |
5eef597e | 406 | if (route->family == AF_INET) |
db2df898 | 407 | r = sd_netlink_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc.in); |
5eef597e | 408 | else if (route->family == AF_INET6) |
db2df898 | 409 | r = sd_netlink_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc.in6); |
f47781d8 MP |
410 | if (r < 0) |
411 | return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m"); | |
5eef597e MP |
412 | } |
413 | ||
60f067b4 | 414 | r = sd_rtnl_message_route_set_scope(req, route->scope); |
f47781d8 MP |
415 | if (r < 0) |
416 | return log_error_errno(r, "Could not set scope: %m"); | |
60f067b4 | 417 | |
db2df898 | 418 | r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority); |
f47781d8 MP |
419 | if (r < 0) |
420 | return log_error_errno(r, "Could not append RTA_PRIORITY attribute: %m"); | |
60f067b4 | 421 | |
86f210e9 | 422 | r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex); |
f47781d8 MP |
423 | if (r < 0) |
424 | return log_error_errno(r, "Could not append RTA_OIF attribute: %m"); | |
60f067b4 | 425 | |
86f210e9 | 426 | r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL); |
f47781d8 MP |
427 | if (r < 0) |
428 | return log_error_errno(r, "Could not send rtnetlink message: %m"); | |
60f067b4 | 429 | |
e842803a MB |
430 | link_ref(link); |
431 | ||
60f067b4 JS |
432 | return 0; |
433 | } | |
434 | ||
aa27b158 MP |
435 | static int route_expire_callback(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
436 | Link *link = userdata; | |
437 | int r; | |
438 | ||
439 | assert(rtnl); | |
440 | assert(m); | |
441 | assert(link); | |
442 | assert(link->ifname); | |
443 | assert(link->link_messages > 0); | |
444 | ||
445 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
446 | return 1; | |
447 | ||
448 | link->link_messages--; | |
449 | ||
450 | r = sd_netlink_message_get_errno(m); | |
451 | if (r < 0 && r != -EEXIST) | |
452 | log_link_warning_errno(link, r, "could not remove route: %m"); | |
453 | ||
454 | if (link->link_messages == 0) | |
455 | log_link_debug(link, "route removed"); | |
456 | ||
457 | return 1; | |
458 | } | |
459 | ||
db2df898 MP |
460 | int route_expire_handler(sd_event_source *s, uint64_t usec, void *userdata) { |
461 | Route *route = userdata; | |
462 | int r; | |
463 | ||
464 | assert(route); | |
465 | ||
aa27b158 | 466 | r = route_remove(route, route->link, route_expire_callback); |
db2df898 MP |
467 | if (r < 0) |
468 | log_warning_errno(r, "Could not remove route: %m"); | |
aa27b158 MP |
469 | else { |
470 | /* route may not be exist in kernel. If we fail still remove it */ | |
471 | route->link->link_messages++; | |
472 | route_free(route); | |
473 | } | |
db2df898 MP |
474 | |
475 | return 1; | |
476 | } | |
477 | ||
5a920b42 MP |
478 | int route_configure( |
479 | Route *route, | |
480 | Link *link, | |
481 | sd_netlink_message_handler_t callback) { | |
482 | ||
4c89c718 MP |
483 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
484 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; | |
db2df898 | 485 | usec_t lifetime; |
60f067b4 JS |
486 | int r; |
487 | ||
488 | assert(link); | |
489 | assert(link->manager); | |
490 | assert(link->manager->rtnl); | |
491 | assert(link->ifindex > 0); | |
492 | assert(route->family == AF_INET || route->family == AF_INET6); | |
493 | ||
5a920b42 MP |
494 | if (route_get(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, NULL) <= 0 && |
495 | set_size(link->routes) >= ROUTES_PER_LINK_MAX) | |
496 | return -E2BIG; | |
497 | ||
60f067b4 | 498 | r = sd_rtnl_message_new_route(link->manager->rtnl, &req, |
5eef597e MP |
499 | RTM_NEWROUTE, route->family, |
500 | route->protocol); | |
f47781d8 MP |
501 | if (r < 0) |
502 | return log_error_errno(r, "Could not create RTM_NEWROUTE message: %m"); | |
60f067b4 | 503 | |
db2df898 | 504 | if (!in_addr_is_null(route->family, &route->gw)) { |
f47781d8 | 505 | if (route->family == AF_INET) |
db2df898 | 506 | r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &route->gw.in); |
f47781d8 | 507 | else if (route->family == AF_INET6) |
db2df898 | 508 | r = sd_netlink_message_append_in6_addr(req, RTA_GATEWAY, &route->gw.in6); |
f47781d8 MP |
509 | if (r < 0) |
510 | return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m"); | |
aa27b158 MP |
511 | |
512 | r = sd_rtnl_message_route_set_family(req, route->family); | |
513 | if (r < 0) | |
514 | return log_error_errno(r, "Could not set route family: %m"); | |
60f067b4 JS |
515 | } |
516 | ||
517 | if (route->dst_prefixlen) { | |
518 | if (route->family == AF_INET) | |
db2df898 | 519 | r = sd_netlink_message_append_in_addr(req, RTA_DST, &route->dst.in); |
60f067b4 | 520 | else if (route->family == AF_INET6) |
db2df898 | 521 | r = sd_netlink_message_append_in6_addr(req, RTA_DST, &route->dst.in6); |
f47781d8 MP |
522 | if (r < 0) |
523 | return log_error_errno(r, "Could not append RTA_DST attribute: %m"); | |
60f067b4 JS |
524 | |
525 | r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen); | |
f47781d8 MP |
526 | if (r < 0) |
527 | return log_error_errno(r, "Could not set destination prefix length: %m"); | |
528 | } | |
529 | ||
530 | if (route->src_prefixlen) { | |
531 | if (route->family == AF_INET) | |
db2df898 | 532 | r = sd_netlink_message_append_in_addr(req, RTA_SRC, &route->src.in); |
f47781d8 | 533 | else if (route->family == AF_INET6) |
db2df898 | 534 | r = sd_netlink_message_append_in6_addr(req, RTA_SRC, &route->src.in6); |
f47781d8 MP |
535 | if (r < 0) |
536 | return log_error_errno(r, "Could not append RTA_SRC attribute: %m"); | |
537 | ||
538 | r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen); | |
539 | if (r < 0) | |
540 | return log_error_errno(r, "Could not set source prefix length: %m"); | |
60f067b4 JS |
541 | } |
542 | ||
db2df898 | 543 | if (!in_addr_is_null(route->family, &route->prefsrc)) { |
5eef597e | 544 | if (route->family == AF_INET) |
db2df898 | 545 | r = sd_netlink_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc.in); |
5eef597e | 546 | else if (route->family == AF_INET6) |
db2df898 | 547 | r = sd_netlink_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc.in6); |
f47781d8 MP |
548 | if (r < 0) |
549 | return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m"); | |
5eef597e MP |
550 | } |
551 | ||
60f067b4 | 552 | r = sd_rtnl_message_route_set_scope(req, route->scope); |
f47781d8 MP |
553 | if (r < 0) |
554 | return log_error_errno(r, "Could not set scope: %m"); | |
60f067b4 | 555 | |
db2df898 MP |
556 | r = sd_rtnl_message_route_set_flags(req, route->flags); |
557 | if (r < 0) | |
aa27b158 MP |
558 | return log_error_errno(r, "Could not set flags: %m"); |
559 | ||
560 | if (route->table != RT_TABLE_DEFAULT) { | |
561 | ||
562 | if (route->table < 256) { | |
563 | r = sd_rtnl_message_route_set_table(req, route->table); | |
564 | if (r < 0) | |
565 | return log_error_errno(r, "Could not set route table: %m"); | |
566 | } else { | |
567 | ||
568 | r = sd_rtnl_message_route_set_table(req, RT_TABLE_UNSPEC); | |
569 | if (r < 0) | |
570 | return log_error_errno(r, "Could not set route table: %m"); | |
571 | ||
572 | /* Table attribute to allow more than 256. */ | |
573 | r = sd_netlink_message_append_data(req, RTA_TABLE, &route->table, sizeof(route->table)); | |
574 | if (r < 0) | |
575 | return log_error_errno(r, "Could not append RTA_TABLE attribute: %m"); | |
576 | } | |
577 | } | |
db2df898 MP |
578 | |
579 | r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority); | |
f47781d8 MP |
580 | if (r < 0) |
581 | return log_error_errno(r, "Could not append RTA_PRIORITY attribute: %m"); | |
60f067b4 | 582 | |
db2df898 MP |
583 | r = sd_netlink_message_append_u8(req, RTA_PREF, route->pref); |
584 | if (r < 0) | |
585 | return log_error_errno(r, "Could not append RTA_PREF attribute: %m"); | |
586 | ||
86f210e9 | 587 | r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex); |
f47781d8 MP |
588 | if (r < 0) |
589 | return log_error_errno(r, "Could not append RTA_OIF attribute: %m"); | |
60f067b4 | 590 | |
86f210e9 | 591 | r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL); |
f47781d8 MP |
592 | if (r < 0) |
593 | return log_error_errno(r, "Could not send rtnetlink message: %m"); | |
60f067b4 | 594 | |
e842803a MB |
595 | link_ref(link); |
596 | ||
db2df898 MP |
597 | lifetime = route->lifetime; |
598 | ||
599 | r = route_add(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, &route); | |
600 | if (r < 0) | |
601 | return log_error_errno(r, "Could not add route: %m"); | |
602 | ||
603 | /* TODO: drop expiration handling once it can be pushed into the kernel */ | |
604 | route->lifetime = lifetime; | |
605 | ||
606 | if (route->lifetime != USEC_INFINITY) { | |
607 | r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), | |
608 | route->lifetime, 0, route_expire_handler, route); | |
609 | if (r < 0) | |
610 | return log_error_errno(r, "Could not arm expiration timer: %m"); | |
611 | } | |
612 | ||
613 | sd_event_source_unref(route->expire); | |
614 | route->expire = expire; | |
615 | expire = NULL; | |
616 | ||
60f067b4 JS |
617 | return 0; |
618 | } | |
619 | ||
620 | int config_parse_gateway(const char *unit, | |
621 | const char *filename, | |
622 | unsigned line, | |
623 | const char *section, | |
624 | unsigned section_line, | |
625 | const char *lvalue, | |
626 | int ltype, | |
627 | const char *rvalue, | |
628 | void *data, | |
629 | void *userdata) { | |
5eef597e | 630 | |
60f067b4 JS |
631 | Network *network = userdata; |
632 | _cleanup_route_free_ Route *n = NULL; | |
5eef597e MP |
633 | union in_addr_union buffer; |
634 | int r, f; | |
60f067b4 JS |
635 | |
636 | assert(filename); | |
637 | assert(section); | |
638 | assert(lvalue); | |
639 | assert(rvalue); | |
640 | assert(data); | |
641 | ||
642 | if (streq(section, "Network")) { | |
643 | /* we are not in an Route section, so treat | |
644 | * this as the special '0' section */ | |
645 | section_line = 0; | |
646 | } | |
647 | ||
648 | r = route_new_static(network, section_line, &n); | |
649 | if (r < 0) | |
650 | return r; | |
651 | ||
5eef597e | 652 | r = in_addr_from_string_auto(rvalue, &f, &buffer); |
60f067b4 | 653 | if (r < 0) { |
6300502b | 654 | log_syntax(unit, LOG_ERR, filename, line, r, "Route is invalid, ignoring assignment: %s", rvalue); |
60f067b4 JS |
655 | return 0; |
656 | } | |
657 | ||
5eef597e | 658 | n->family = f; |
db2df898 | 659 | n->gw = buffer; |
60f067b4 JS |
660 | n = NULL; |
661 | ||
662 | return 0; | |
663 | } | |
664 | ||
6300502b MP |
665 | int config_parse_preferred_src(const char *unit, |
666 | const char *filename, | |
667 | unsigned line, | |
668 | const char *section, | |
669 | unsigned section_line, | |
670 | const char *lvalue, | |
671 | int ltype, | |
672 | const char *rvalue, | |
673 | void *data, | |
674 | void *userdata) { | |
675 | ||
676 | Network *network = userdata; | |
677 | _cleanup_route_free_ Route *n = NULL; | |
678 | union in_addr_union buffer; | |
679 | int r, f; | |
680 | ||
681 | assert(filename); | |
682 | assert(section); | |
683 | assert(lvalue); | |
684 | assert(rvalue); | |
685 | assert(data); | |
686 | ||
687 | r = route_new_static(network, section_line, &n); | |
688 | if (r < 0) | |
689 | return r; | |
690 | ||
691 | r = in_addr_from_string_auto(rvalue, &f, &buffer); | |
692 | if (r < 0) { | |
693 | log_syntax(unit, LOG_ERR, filename, line, EINVAL, | |
694 | "Preferred source is invalid, ignoring assignment: %s", rvalue); | |
695 | return 0; | |
696 | } | |
697 | ||
698 | n->family = f; | |
db2df898 | 699 | n->prefsrc = buffer; |
6300502b MP |
700 | n = NULL; |
701 | ||
702 | return 0; | |
703 | } | |
704 | ||
60f067b4 JS |
705 | int config_parse_destination(const char *unit, |
706 | const char *filename, | |
707 | unsigned line, | |
708 | const char *section, | |
709 | unsigned section_line, | |
710 | const char *lvalue, | |
711 | int ltype, | |
712 | const char *rvalue, | |
713 | void *data, | |
714 | void *userdata) { | |
5eef597e | 715 | |
60f067b4 JS |
716 | Network *network = userdata; |
717 | _cleanup_route_free_ Route *n = NULL; | |
5eef597e MP |
718 | const char *address, *e; |
719 | union in_addr_union buffer; | |
f47781d8 | 720 | unsigned char prefixlen; |
5eef597e | 721 | int r, f; |
60f067b4 JS |
722 | |
723 | assert(filename); | |
724 | assert(section); | |
725 | assert(lvalue); | |
726 | assert(rvalue); | |
727 | assert(data); | |
728 | ||
729 | r = route_new_static(network, section_line, &n); | |
730 | if (r < 0) | |
731 | return r; | |
732 | ||
f47781d8 | 733 | /* Destination|Source=address/prefixlen */ |
60f067b4 JS |
734 | |
735 | /* address */ | |
736 | e = strchr(rvalue, '/'); | |
5eef597e MP |
737 | if (e) |
738 | address = strndupa(rvalue, e - rvalue); | |
739 | else | |
740 | address = rvalue; | |
60f067b4 | 741 | |
5eef597e | 742 | r = in_addr_from_string_auto(address, &f, &buffer); |
60f067b4 | 743 | if (r < 0) { |
6300502b | 744 | log_syntax(unit, LOG_ERR, filename, line, r, "Destination is invalid, ignoring assignment: %s", address); |
60f067b4 JS |
745 | return 0; |
746 | } | |
747 | ||
f47781d8 | 748 | if (f != AF_INET && f != AF_INET6) { |
6300502b | 749 | log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown address family, ignoring assignment: %s", address); |
f47781d8 MP |
750 | return 0; |
751 | } | |
752 | ||
60f067b4 JS |
753 | /* prefixlen */ |
754 | if (e) { | |
f47781d8 | 755 | r = safe_atou8(e + 1, &prefixlen); |
60f067b4 | 756 | if (r < 0) { |
6300502b | 757 | log_syntax(unit, LOG_ERR, filename, line, r, "Route destination prefix length is invalid, ignoring assignment: %s", e + 1); |
60f067b4 JS |
758 | return 0; |
759 | } | |
60f067b4 | 760 | } else { |
f47781d8 | 761 | switch (f) { |
60f067b4 | 762 | case AF_INET: |
f47781d8 | 763 | prefixlen = 32; |
60f067b4 JS |
764 | break; |
765 | case AF_INET6: | |
f47781d8 | 766 | prefixlen = 128; |
60f067b4 JS |
767 | break; |
768 | } | |
769 | } | |
770 | ||
5eef597e | 771 | n->family = f; |
f47781d8 | 772 | if (streq(lvalue, "Destination")) { |
db2df898 | 773 | n->dst = buffer; |
f47781d8 MP |
774 | n->dst_prefixlen = prefixlen; |
775 | } else if (streq(lvalue, "Source")) { | |
db2df898 | 776 | n->src = buffer; |
f47781d8 MP |
777 | n->src_prefixlen = prefixlen; |
778 | } else | |
779 | assert_not_reached(lvalue); | |
780 | ||
5eef597e MP |
781 | n = NULL; |
782 | ||
783 | return 0; | |
784 | } | |
785 | ||
786 | int config_parse_route_priority(const char *unit, | |
787 | const char *filename, | |
788 | unsigned line, | |
789 | const char *section, | |
790 | unsigned section_line, | |
791 | const char *lvalue, | |
792 | int ltype, | |
793 | const char *rvalue, | |
794 | void *data, | |
795 | void *userdata) { | |
796 | Network *network = userdata; | |
797 | _cleanup_route_free_ Route *n = NULL; | |
5a920b42 | 798 | uint32_t k; |
5eef597e MP |
799 | int r; |
800 | ||
801 | assert(filename); | |
802 | assert(section); | |
803 | assert(lvalue); | |
804 | assert(rvalue); | |
805 | assert(data); | |
806 | ||
807 | r = route_new_static(network, section_line, &n); | |
808 | if (r < 0) | |
809 | return r; | |
810 | ||
5a920b42 MP |
811 | r = safe_atou32(rvalue, &k); |
812 | if (r < 0) { | |
813 | log_syntax(unit, LOG_ERR, filename, line, r, | |
814 | "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue); | |
815 | return 0; | |
816 | } | |
5eef597e | 817 | |
5a920b42 | 818 | n->priority = k; |
60f067b4 JS |
819 | n = NULL; |
820 | ||
821 | return 0; | |
822 | } | |
e735f4d4 MP |
823 | |
824 | int config_parse_route_scope(const char *unit, | |
825 | const char *filename, | |
826 | unsigned line, | |
827 | const char *section, | |
828 | unsigned section_line, | |
829 | const char *lvalue, | |
830 | int ltype, | |
831 | const char *rvalue, | |
832 | void *data, | |
833 | void *userdata) { | |
834 | Network *network = userdata; | |
835 | _cleanup_route_free_ Route *n = NULL; | |
836 | int r; | |
837 | ||
838 | assert(filename); | |
839 | assert(section); | |
840 | assert(lvalue); | |
841 | assert(rvalue); | |
842 | assert(data); | |
843 | ||
844 | r = route_new_static(network, section_line, &n); | |
845 | if (r < 0) | |
846 | return r; | |
847 | ||
848 | if (streq(rvalue, "host")) | |
849 | n->scope = RT_SCOPE_HOST; | |
850 | else if (streq(rvalue, "link")) | |
851 | n->scope = RT_SCOPE_LINK; | |
852 | else if (streq(rvalue, "global")) | |
853 | n->scope = RT_SCOPE_UNIVERSE; | |
854 | else { | |
6300502b | 855 | log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route scope: %s", rvalue); |
e735f4d4 MP |
856 | return 0; |
857 | } | |
858 | ||
859 | n = NULL; | |
860 | ||
861 | return 0; | |
862 | } | |
aa27b158 MP |
863 | |
864 | int config_parse_route_table(const char *unit, | |
865 | const char *filename, | |
866 | unsigned line, | |
867 | const char *section, | |
868 | unsigned section_line, | |
869 | const char *lvalue, | |
870 | int ltype, | |
871 | const char *rvalue, | |
872 | void *data, | |
873 | void *userdata) { | |
874 | _cleanup_route_free_ Route *n = NULL; | |
875 | Network *network = userdata; | |
876 | uint32_t k; | |
877 | int r; | |
878 | ||
879 | assert(filename); | |
880 | assert(section); | |
881 | assert(lvalue); | |
882 | assert(rvalue); | |
883 | assert(data); | |
884 | ||
885 | r = route_new_static(network, section_line, &n); | |
886 | if (r < 0) | |
887 | return r; | |
888 | ||
889 | r = safe_atou32(rvalue, &k); | |
890 | if (r < 0) { | |
891 | log_syntax(unit, LOG_ERR, filename, line, r, | |
892 | "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue); | |
893 | return 0; | |
894 | } | |
895 | ||
896 | n->table = k; | |
897 | ||
898 | n = NULL; | |
899 | ||
900 | return 0; | |
901 | } |