]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-route.c
networkd: constify more things
[thirdparty/systemd.git] / src / network / networkd-route.c
CommitLineData
f579559b
TG
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
b5efdb8a 20#include "alloc-util.h"
f579559b 21#include "conf-parser.h"
bb7ae737 22#include "in-addr-util.h"
fc2f9534 23#include "netlink-util.h"
6bedfcbb 24#include "networkd-route.h"
fc2f9534 25#include "networkd.h"
6bedfcbb 26#include "parse-util.h"
1c8e710c 27#include "set.h"
07630cea
LP
28#include "string-util.h"
29#include "util.h"
f579559b 30
1b566071 31#define ROUTES_PER_LINK_MAX 2048U
8c34b963
LP
32#define STATIC_ROUTES_PER_NETWORK_MAX 1024U
33
ed9e361a 34int route_new(Route **ret) {
f0213e37
TG
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;
ed9e361a 43 route->protocol = RTPROT_UNSPEC;
bb7ae737 44 route->table = RT_TABLE_DEFAULT;
f833694d 45 route->lifetime = USEC_INFINITY;
f0213e37
TG
46
47 *ret = route;
48 route = NULL;
49
50 return 0;
51}
52
f048a16b 53int route_new_static(Network *network, unsigned section, Route **ret) {
f579559b 54 _cleanup_route_free_ Route *route = NULL;
f0213e37 55 int r;
f579559b 56
8c34b963
LP
57 assert(network);
58 assert(ret);
59
6ae115c1 60 if (section) {
21b39268 61 route = hashmap_get(network->routes_by_section, UINT_TO_PTR(section));
6ae115c1
TG
62 if (route) {
63 *ret = route;
64 route = NULL;
65
66 return 0;
67 }
68 }
69
8c34b963
LP
70 if (network->n_static_routes >= STATIC_ROUTES_PER_NETWORK_MAX)
71 return -E2BIG;
72
ed9e361a 73 r = route_new(&route);
f0213e37
TG
74 if (r < 0)
75 return r;
801bd9e8 76
ed9e361a 77 route->protocol = RTPROT_STATIC;
f579559b 78
6ae115c1 79 if (section) {
cacc1dbf
SS
80 route->section = section;
81
21b39268
LP
82 r = hashmap_put(network->routes_by_section, UINT_TO_PTR(route->section), route);
83 if (r < 0)
84 return r;
6ae115c1
TG
85 }
86
21b39268 87 route->network = network;
cacc1dbf 88 LIST_PREPEND(routes, network->static_routes, route);
8c34b963 89 network->n_static_routes++;
21b39268 90
f579559b
TG
91 *ret = route;
92 route = NULL;
93
94 return 0;
95}
96
97void route_free(Route *route) {
98 if (!route)
99 return;
100
f048a16b 101 if (route->network) {
3d3d4255 102 LIST_REMOVE(routes, route->network->static_routes, route);
f579559b 103
8c34b963
LP
104 assert(route->network->n_static_routes > 0);
105 route->network->n_static_routes--;
106
f048a16b 107 if (route->section)
8c34b963 108 hashmap_remove(route->network->routes_by_section, UINT_TO_PTR(route->section));
f048a16b 109 }
6ae115c1 110
1c8e710c
TG
111 if (route->link) {
112 set_remove(route->link->routes, route);
113 set_remove(route->link->routes_foreign, route);
114 }
115
f833694d
TG
116 sd_event_source_unref(route->expire);
117
f579559b
TG
118 free(route);
119}
120
bb7ae737
TG
121static 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) */
2ce40956 133 siphash24_compress(&route->dst, FAMILY_ADDRESS_SIZE(route->family), state);
bb7ae737
TG
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
146static 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:
bb7ae737
TG
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
2ce40956 177 return memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
bb7ae737
TG
178 default:
179 /* treat any other address family as AF_UNSPEC */
180 return 0;
181 }
182}
183
184static const struct hash_ops route_hash_ops = {
185 .hash = route_hash_func,
186 .compare = route_compare_func
187};
188
1c8e710c
TG
189int route_get(Link *link,
190 int family,
1b566071 191 const union in_addr_union *dst,
1c8e710c
TG
192 unsigned char dst_prefixlen,
193 unsigned char tos,
194 uint32_t priority,
195 unsigned char table,
196 Route **ret) {
1b566071
LP
197
198 Route route, *existing;
199
200 assert(link);
201 assert(dst);
202
203 route = (Route) {
1c8e710c 204 .family = family,
1b566071 205 .dst = *dst,
1c8e710c
TG
206 .dst_prefixlen = dst_prefixlen,
207 .tos = tos,
208 .priority = priority,
209 .table = table,
1b566071 210 };
1c8e710c
TG
211
212 existing = set_get(link->routes, &route);
213 if (existing) {
1b566071
LP
214 if (ret)
215 *ret = existing;
1c8e710c 216 return 1;
1c8e710c
TG
217 }
218
1b566071
LP
219 existing = set_get(link->routes_foreign, &route);
220 if (existing) {
221 if (ret)
222 *ret = existing;
223 return 0;
224 }
1c8e710c 225
1b566071 226 return -ENOENT;
1c8e710c
TG
227}
228
889b550f
LP
229static 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
1c8e710c
TG
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
889b550f
LP
276int 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
1c8e710c
TG
286 return route_add_internal(link, &link->routes_foreign, family, dst, dst_prefixlen, tos, priority, table, ret);
287}
288
889b550f
LP
289int route_add(
290 Link *link,
1c8e710c 291 int family,
889b550f 292 const union in_addr_union *dst,
1c8e710c
TG
293 unsigned char dst_prefixlen,
294 unsigned char tos,
295 uint32_t priority,
889b550f
LP
296 unsigned char table,
297 Route **ret) {
298
1c8e710c
TG
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
330int route_update(Route *route,
889b550f 331 const union in_addr_union *src,
1c8e710c 332 unsigned char src_prefixlen,
889b550f
LP
333 const union in_addr_union *gw,
334 const union in_addr_union *prefsrc,
1c8e710c
TG
335 unsigned char scope,
336 unsigned char protocol) {
889b550f 337
1c8e710c
TG
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
91b5f997 353int route_remove(Route *route, Link *link,
1c4baffc 354 sd_netlink_message_handler_t callback) {
4afd3348 355 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
5c1d3fc9
UTL
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,
28cc555d
DW
365 RTM_DELROUTE, route->family,
366 route->protocol);
f647962d
MS
367 if (r < 0)
368 return log_error_errno(r, "Could not create RTM_DELROUTE message: %m");
5c1d3fc9 369
2ce40956 370 if (!in_addr_is_null(route->family, &route->gw)) {
59580681 371 if (route->family == AF_INET)
2ce40956 372 r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &route->gw.in);
59580681 373 else if (route->family == AF_INET6)
2ce40956 374 r = sd_netlink_message_append_in6_addr(req, RTA_GATEWAY, &route->gw.in6);
f647962d
MS
375 if (r < 0)
376 return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
5c1d3fc9
UTL
377 }
378
379 if (route->dst_prefixlen) {
380 if (route->family == AF_INET)
2ce40956 381 r = sd_netlink_message_append_in_addr(req, RTA_DST, &route->dst.in);
5c1d3fc9 382 else if (route->family == AF_INET6)
2ce40956 383 r = sd_netlink_message_append_in6_addr(req, RTA_DST, &route->dst.in6);
f647962d
MS
384 if (r < 0)
385 return log_error_errno(r, "Could not append RTA_DST attribute: %m");
5c1d3fc9
UTL
386
387 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
f647962d
MS
388 if (r < 0)
389 return log_error_errno(r, "Could not set destination prefix length: %m");
5c1d3fc9
UTL
390 }
391
9e7e4408
TG
392 if (route->src_prefixlen) {
393 if (route->family == AF_INET)
2ce40956 394 r = sd_netlink_message_append_in_addr(req, RTA_SRC, &route->src.in);
9e7e4408 395 else if (route->family == AF_INET6)
2ce40956 396 r = sd_netlink_message_append_in6_addr(req, RTA_SRC, &route->src.in6);
9e7e4408 397 if (r < 0)
d9d94393 398 return log_error_errno(r, "Could not append RTA_SRC attribute: %m");
9e7e4408
TG
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");
403 }
404
2ce40956 405 if (!in_addr_is_null(route->family, &route->prefsrc)) {
46b0c76e 406 if (route->family == AF_INET)
2ce40956 407 r = sd_netlink_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc.in);
46b0c76e 408 else if (route->family == AF_INET6)
2ce40956 409 r = sd_netlink_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc.in6);
f647962d
MS
410 if (r < 0)
411 return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m");
46b0c76e
ERB
412 }
413
5c1d3fc9 414 r = sd_rtnl_message_route_set_scope(req, route->scope);
f647962d
MS
415 if (r < 0)
416 return log_error_errno(r, "Could not set scope: %m");
5c1d3fc9 417
86655331 418 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
f647962d
MS
419 if (r < 0)
420 return log_error_errno(r, "Could not append RTA_PRIORITY attribute: %m");
5c1d3fc9 421
1c4baffc 422 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
f647962d
MS
423 if (r < 0)
424 return log_error_errno(r, "Could not append RTA_OIF attribute: %m");
5c1d3fc9 425
1c4baffc 426 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
f647962d
MS
427 if (r < 0)
428 return log_error_errno(r, "Could not send rtnetlink message: %m");
5c1d3fc9 429
563c69c6
TG
430 link_ref(link);
431
5c1d3fc9
UTL
432 return 0;
433}
434
fe7ca21a
SS
435static 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
f833694d
TG
460int route_expire_handler(sd_event_source *s, uint64_t usec, void *userdata) {
461 Route *route = userdata;
462 int r;
463
464 assert(route);
465
fe7ca21a 466 r = route_remove(route, route->link, route_expire_callback);
f833694d
TG
467 if (r < 0)
468 log_warning_errno(r, "Could not remove route: %m");
fe7ca21a
SS
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 }
f833694d
TG
474
475 return 1;
476}
477
1b566071
LP
478int route_configure(
479 Route *route,
480 Link *link,
481 sd_netlink_message_handler_t callback) {
482
4afd3348
LP
483 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
484 _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL;
f833694d 485 usec_t lifetime;
f579559b
TG
486 int r;
487
f579559b 488 assert(link);
f882c247
TG
489 assert(link->manager);
490 assert(link->manager->rtnl);
f579559b
TG
491 assert(link->ifindex > 0);
492 assert(route->family == AF_INET || route->family == AF_INET6);
493
1b566071
LP
494 if (route_get(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, NULL) <= 0 &&
495 set_size(route->link->routes) >= ROUTES_PER_LINK_MAX)
496 return -E2BIG;
497
151b9b96 498 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
28cc555d
DW
499 RTM_NEWROUTE, route->family,
500 route->protocol);
f647962d
MS
501 if (r < 0)
502 return log_error_errno(r, "Could not create RTM_NEWROUTE message: %m");
f579559b 503
2ce40956 504 if (!in_addr_is_null(route->family, &route->gw)) {
59580681 505 if (route->family == AF_INET)
2ce40956 506 r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &route->gw.in);
59580681 507 else if (route->family == AF_INET6)
2ce40956 508 r = sd_netlink_message_append_in6_addr(req, RTA_GATEWAY, &route->gw.in6);
f647962d
MS
509 if (r < 0)
510 return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
c953b24c
SS
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");
f579559b
TG
515 }
516
0a0dc69b
TG
517 if (route->dst_prefixlen) {
518 if (route->family == AF_INET)
2ce40956 519 r = sd_netlink_message_append_in_addr(req, RTA_DST, &route->dst.in);
0a0dc69b 520 else if (route->family == AF_INET6)
2ce40956 521 r = sd_netlink_message_append_in6_addr(req, RTA_DST, &route->dst.in6);
f647962d
MS
522 if (r < 0)
523 return log_error_errno(r, "Could not append RTA_DST attribute: %m");
6ae115c1 524
ae4c67a7 525 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
f647962d
MS
526 if (r < 0)
527 return log_error_errno(r, "Could not set destination prefix length: %m");
1f01fb4f
TG
528 }
529
9e7e4408
TG
530 if (route->src_prefixlen) {
531 if (route->family == AF_INET)
2ce40956 532 r = sd_netlink_message_append_in_addr(req, RTA_SRC, &route->src.in);
9e7e4408 533 else if (route->family == AF_INET6)
2ce40956 534 r = sd_netlink_message_append_in6_addr(req, RTA_SRC, &route->src.in6);
9e7e4408
TG
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");
541 }
542
2ce40956 543 if (!in_addr_is_null(route->family, &route->prefsrc)) {
46b0c76e 544 if (route->family == AF_INET)
2ce40956 545 r = sd_netlink_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc.in);
46b0c76e 546 else if (route->family == AF_INET6)
2ce40956 547 r = sd_netlink_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc.in6);
f647962d
MS
548 if (r < 0)
549 return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m");
46b0c76e
ERB
550 }
551
5c1d3fc9 552 r = sd_rtnl_message_route_set_scope(req, route->scope);
f647962d
MS
553 if (r < 0)
554 return log_error_errno(r, "Could not set scope: %m");
5c1d3fc9 555
3b015d40
TG
556 r = sd_rtnl_message_route_set_flags(req, route->flags);
557 if (r < 0)
c953b24c
SS
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
06976f5b 572 /* Table attribute to allow more than 256. */
c953b24c
SS
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 }
3b015d40 578
86655331 579 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
f647962d
MS
580 if (r < 0)
581 return log_error_errno(r, "Could not append RTA_PRIORITY attribute: %m");
5c1d3fc9 582
3b015d40
TG
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
1c4baffc 587 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
f647962d
MS
588 if (r < 0)
589 return log_error_errno(r, "Could not append RTA_OIF attribute: %m");
f579559b 590
1c4baffc 591 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
f647962d
MS
592 if (r < 0)
593 return log_error_errno(r, "Could not send rtnetlink message: %m");
f579559b 594
563c69c6
TG
595 link_ref(link);
596
f833694d
TG
597 lifetime = route->lifetime;
598
599 r = route_add(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, &route);
1c8e710c
TG
600 if (r < 0)
601 return log_error_errno(r, "Could not add route: %m");
602
f833694d
TG
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
f579559b
TG
617 return 0;
618}
619
620int config_parse_gateway(const char *unit,
621 const char *filename,
622 unsigned line,
623 const char *section,
71a61510 624 unsigned section_line,
f579559b
TG
625 const char *lvalue,
626 int ltype,
627 const char *rvalue,
628 void *data,
629 void *userdata) {
44e7b949 630
6ae115c1 631 Network *network = userdata;
f579559b 632 _cleanup_route_free_ Route *n = NULL;
44e7b949
LP
633 union in_addr_union buffer;
634 int r, f;
f579559b
TG
635
636 assert(filename);
6ae115c1 637 assert(section);
f579559b
TG
638 assert(lvalue);
639 assert(rvalue);
640 assert(data);
641
92fe133a
TG
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
f048a16b 648 r = route_new_static(network, section_line, &n);
f579559b
TG
649 if (r < 0)
650 return r;
651
44e7b949 652 r = in_addr_from_string_auto(rvalue, &f, &buffer);
f579559b 653 if (r < 0) {
12ca818f 654 log_syntax(unit, LOG_ERR, filename, line, r, "Route is invalid, ignoring assignment: %s", rvalue);
f579559b
TG
655 return 0;
656 }
657
44e7b949 658 n->family = f;
2ce40956 659 n->gw = buffer;
f579559b
TG
660 n = NULL;
661
662 return 0;
663}
6ae115c1 664
0d07e595
JK
665int 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;
2ce40956 699 n->prefsrc = buffer;
0d07e595
JK
700 n = NULL;
701
702 return 0;
703}
704
6ae115c1
TG
705int 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) {
44e7b949 715
6ae115c1
TG
716 Network *network = userdata;
717 _cleanup_route_free_ Route *n = NULL;
44e7b949
LP
718 const char *address, *e;
719 union in_addr_union buffer;
9e7e4408 720 unsigned char prefixlen;
44e7b949 721 int r, f;
6ae115c1
TG
722
723 assert(filename);
724 assert(section);
725 assert(lvalue);
726 assert(rvalue);
727 assert(data);
728
f048a16b 729 r = route_new_static(network, section_line, &n);
6ae115c1
TG
730 if (r < 0)
731 return r;
732
9e7e4408 733 /* Destination|Source=address/prefixlen */
6ae115c1 734
ae4c67a7 735 /* address */
6ae115c1 736 e = strchr(rvalue, '/');
44e7b949
LP
737 if (e)
738 address = strndupa(rvalue, e - rvalue);
739 else
740 address = rvalue;
6ae115c1 741
44e7b949 742 r = in_addr_from_string_auto(address, &f, &buffer);
6ae115c1 743 if (r < 0) {
12ca818f 744 log_syntax(unit, LOG_ERR, filename, line, r, "Destination is invalid, ignoring assignment: %s", address);
6ae115c1
TG
745 return 0;
746 }
747
935c0d26 748 if (f != AF_INET && f != AF_INET6) {
12ca818f 749 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown address family, ignoring assignment: %s", address);
935c0d26
TG
750 return 0;
751 }
752
ae4c67a7
TG
753 /* prefixlen */
754 if (e) {
9e7e4408 755 r = safe_atou8(e + 1, &prefixlen);
ae4c67a7 756 if (r < 0) {
12ca818f 757 log_syntax(unit, LOG_ERR, filename, line, r, "Route destination prefix length is invalid, ignoring assignment: %s", e + 1);
ae4c67a7
TG
758 return 0;
759 }
ae4c67a7 760 } else {
935c0d26 761 switch (f) {
ae4c67a7 762 case AF_INET:
9e7e4408 763 prefixlen = 32;
ae4c67a7
TG
764 break;
765 case AF_INET6:
9e7e4408 766 prefixlen = 128;
ae4c67a7
TG
767 break;
768 }
769 }
770
44e7b949 771 n->family = f;
9e7e4408 772 if (streq(lvalue, "Destination")) {
2ce40956 773 n->dst = buffer;
9e7e4408
TG
774 n->dst_prefixlen = prefixlen;
775 } else if (streq(lvalue, "Source")) {
2ce40956 776 n->src = buffer;
9e7e4408
TG
777 n->src_prefixlen = prefixlen;
778 } else
779 assert_not_reached(lvalue);
780
6ae115c1
TG
781 n = NULL;
782
783 return 0;
784}
5d8e593d
SS
785
786int 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;
5d8e593d
SS
798 int r;
799
800 assert(filename);
801 assert(section);
802 assert(lvalue);
803 assert(rvalue);
804 assert(data);
805
806 r = route_new_static(network, section_line, &n);
807 if (r < 0)
808 return r;
809
86655331
TG
810 r = config_parse_uint32(unit, filename, line, section,
811 section_line, lvalue, ltype,
812 rvalue, &n->priority, userdata);
5d8e593d
SS
813 if (r < 0)
814 return r;
815
816 n = NULL;
817
818 return 0;
819}
769b56a3
TG
820
821int config_parse_route_scope(const char *unit,
822 const char *filename,
823 unsigned line,
824 const char *section,
825 unsigned section_line,
826 const char *lvalue,
827 int ltype,
828 const char *rvalue,
829 void *data,
830 void *userdata) {
831 Network *network = userdata;
832 _cleanup_route_free_ Route *n = NULL;
833 int r;
834
835 assert(filename);
836 assert(section);
837 assert(lvalue);
838 assert(rvalue);
839 assert(data);
840
841 r = route_new_static(network, section_line, &n);
842 if (r < 0)
843 return r;
844
845 if (streq(rvalue, "host"))
846 n->scope = RT_SCOPE_HOST;
847 else if (streq(rvalue, "link"))
848 n->scope = RT_SCOPE_LINK;
849 else if (streq(rvalue, "global"))
850 n->scope = RT_SCOPE_UNIVERSE;
851 else {
12ca818f 852 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route scope: %s", rvalue);
769b56a3
TG
853 return 0;
854 }
855
856 n = NULL;
857
858 return 0;
859}
c953b24c
SS
860
861int config_parse_route_table(const char *unit,
862 const char *filename,
863 unsigned line,
864 const char *section,
865 unsigned section_line,
866 const char *lvalue,
867 int ltype,
868 const char *rvalue,
869 void *data,
870 void *userdata) {
871 _cleanup_route_free_ Route *n = NULL;
872 Network *network = userdata;
873 uint32_t k;
874 int r;
875
876 assert(filename);
877 assert(section);
878 assert(lvalue);
879 assert(rvalue);
880 assert(data);
881
882 r = route_new_static(network, section_line, &n);
883 if (r < 0)
884 return r;
885
886 r = safe_atou32(rvalue, &k);
887 if (r < 0) {
888 log_syntax(unit, LOG_ERR, filename, line, r,
889 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue);
890 return 0;
891 }
892
893 n->table = k;
894
895 n = NULL;
896
897 return 0;
898}