]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-routing-policy-rule.c
network: use structured initializers
[thirdparty/systemd.git] / src / network / networkd-routing-policy-rule.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
bce67bbe
SS
2
3#include <net/if.h>
4#include <linux/fib_rules.h>
5
6#include "alloc-util.h"
7#include "conf-parser.h"
8#include "fileio.h"
9#include "networkd-routing-policy-rule.h"
10#include "netlink-util.h"
11#include "networkd-manager.h"
12#include "parse-util.h"
13#include "socket-util.h"
14#include "string-util.h"
51517f9e 15#include "strv.h"
bce67bbe
SS
16
17int routing_policy_rule_new(RoutingPolicyRule **ret) {
18 RoutingPolicyRule *rule;
19
9d66b48c 20 rule = new(RoutingPolicyRule, 1);
bce67bbe
SS
21 if (!rule)
22 return -ENOMEM;
23
9d66b48c
YW
24 *rule = (RoutingPolicyRule) {
25 .family = AF_INET,
26 .table = RT_TABLE_MAIN,
27 };
bce67bbe
SS
28
29 *ret = rule;
30 return 0;
31}
32
33void routing_policy_rule_free(RoutingPolicyRule *rule) {
34
35 if (!rule)
36 return;
37
38 if (rule->network) {
39 LIST_REMOVE(rules, rule->network->rules, rule);
40 assert(rule->network->n_rules > 0);
41 rule->network->n_rules--;
42
43 if (rule->section) {
44 hashmap_remove(rule->network->rules_by_section, rule->section);
45 network_config_section_free(rule->section);
46 }
47
36e6e28b
SS
48 }
49
6964cf45
SS
50 if (rule->manager) {
51 set_remove(rule->manager->rules, rule);
52 set_remove(rule->manager->rules_foreign, rule);
bce67bbe
SS
53 }
54
762e2659
SS
55 free(rule->iif);
56 free(rule->oif);
bce67bbe
SS
57 free(rule);
58}
59
60static void routing_policy_rule_hash_func(const void *b, struct siphash *state) {
61 const RoutingPolicyRule *rule = b;
62
63 assert(rule);
64
65 siphash24_compress(&rule->family, sizeof(rule->family), state);
66
67 switch (rule->family) {
68 case AF_INET:
69 case AF_INET6:
70
71 siphash24_compress(&rule->from, FAMILY_ADDRESS_SIZE(rule->family), state);
72 siphash24_compress(&rule->from_prefixlen, sizeof(rule->from_prefixlen), state);
73
74 siphash24_compress(&rule->to, FAMILY_ADDRESS_SIZE(rule->family), state);
75 siphash24_compress(&rule->to_prefixlen, sizeof(rule->to_prefixlen), state);
76
77 siphash24_compress(&rule->tos, sizeof(rule->tos), state);
78 siphash24_compress(&rule->fwmark, sizeof(rule->fwmark), state);
79 siphash24_compress(&rule->table, sizeof(rule->table), state);
80
762e2659 81 if (rule->iif)
0d7febd0 82 siphash24_compress(rule->iif, strlen(rule->iif), state);
762e2659
SS
83
84 if (rule->oif)
0d7febd0 85 siphash24_compress(rule->oif, strlen(rule->oif), state);
762e2659 86
bce67bbe
SS
87 break;
88 default:
89 /* treat any other address family as AF_UNSPEC */
90 break;
91 }
92}
93
94static int routing_policy_rule_compare_func(const void *_a, const void *_b) {
95 const RoutingPolicyRule *a = _a, *b = _b;
96 int r;
97
a0edd02e
FB
98 r = CMP(a->family, b->family);
99 if (r != 0)
100 return r;
bce67bbe
SS
101
102 switch (a->family) {
103 case AF_INET:
104 case AF_INET6:
a0edd02e
FB
105 r = CMP(a->from_prefixlen, b->from_prefixlen);
106 if (r != 0)
107 return r;
108
109 r = CMP(a->to_prefixlen, b->to_prefixlen);
110 if (r != 0)
111 return r;
112
113 r = CMP(a->tos, b->tos);
114 if (r != 0)
115 return r;
116
117 r = CMP(a->fwmask, b->fwmask);
118 if (r != 0)
119 return r;
120
121 r = CMP(a->table, b->table);
122 if (r != 0)
123 return r;
bce67bbe 124
762e2659
SS
125 r = strcmp_ptr(a->iif, b->iif);
126 if (!r)
127 return r;
128
129 r = strcmp_ptr(a->oif, b->oif);
130 if (!r)
131 return r;
132
bce67bbe
SS
133 r = memcmp(&a->from, &b->from, FAMILY_ADDRESS_SIZE(a->family));
134 if (r != 0)
135 return r;
136
137 return memcmp(&a->to, &b->to, FAMILY_ADDRESS_SIZE(a->family));
138
139 default:
140 /* treat any other address family as AF_UNSPEC */
141 return 0;
142 }
143}
144
145const struct hash_ops routing_policy_rule_hash_ops = {
146 .hash = routing_policy_rule_hash_func,
147 .compare = routing_policy_rule_compare_func
148};
149
150int routing_policy_rule_get(Manager *m,
151 int family,
152 const union in_addr_union *from,
153 uint8_t from_prefixlen,
154 const union in_addr_union *to,
155 uint8_t to_prefixlen,
156 uint8_t tos,
157 uint32_t fwmark,
158 uint32_t table,
eeab051b
YW
159 const char *iif,
160 const char *oif,
bce67bbe
SS
161 RoutingPolicyRule **ret) {
162
163 RoutingPolicyRule rule, *existing;
164
165 assert_return(m, -1);
166
167 rule = (RoutingPolicyRule) {
168 .family = family,
169 .from = *from,
170 .from_prefixlen = from_prefixlen,
171 .to = *to,
172 .to_prefixlen = to_prefixlen,
173 .tos = tos,
174 .fwmark = fwmark,
175 .table = table,
eeab051b
YW
176 .iif = (char*) iif,
177 .oif = (char*) oif
bce67bbe
SS
178 };
179
b87dadcd
ZJS
180 existing = set_get(m->rules, &rule);
181 if (existing) {
182 if (ret)
183 *ret = existing;
184 return 1;
bce67bbe
SS
185 }
186
b87dadcd
ZJS
187 existing = set_get(m->rules_foreign, &rule);
188 if (existing) {
189 if (ret)
190 *ret = existing;
e6b65ab7 191 return 0;
bce67bbe
SS
192 }
193
194 return -ENOENT;
195}
196
197int routing_policy_rule_make_local(Manager *m, RoutingPolicyRule *rule) {
198 int r;
199
200 assert(m);
201
202 if (set_contains(m->rules_foreign, rule)) {
203 set_remove(m->rules_foreign, rule);
204
205 r = set_ensure_allocated(&m->rules, &routing_policy_rule_hash_ops);
206 if (r < 0)
207 return r;
208
209 return set_put(m->rules, rule);
210 }
211
212 return -ENOENT;
213}
214
36e6e28b
SS
215static int routing_policy_rule_add_internal(Manager *m,
216 Set **rules,
bce67bbe
SS
217 int family,
218 const union in_addr_union *from,
219 uint8_t from_prefixlen,
220 const union in_addr_union *to,
221 uint8_t to_prefixlen,
222 uint8_t tos,
223 uint32_t fwmark,
224 uint32_t table,
eeab051b
YW
225 const char *_iif,
226 const char *_oif,
bce67bbe
SS
227 RoutingPolicyRule **ret) {
228
8e766630 229 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL;
eeab051b 230 _cleanup_free_ char *iif = NULL, *oif = NULL;
bce67bbe
SS
231 int r;
232
233 assert_return(rules, -EINVAL);
234
eeab051b
YW
235 if (_iif) {
236 iif = strdup(_iif);
237 if (!iif)
238 return -ENOMEM;
239 }
240
241 if (_oif) {
242 oif = strdup(_oif);
243 if (!oif)
244 return -ENOMEM;
245 }
246
bce67bbe
SS
247 r = routing_policy_rule_new(&rule);
248 if (r < 0)
249 return r;
250
6964cf45 251 rule->manager = m;
bce67bbe
SS
252 rule->family = family;
253 rule->from = *from;
254 rule->from_prefixlen = from_prefixlen;
255 rule->to = *to;
256 rule->to_prefixlen = to_prefixlen;
257 rule->tos = tos;
258 rule->fwmark = fwmark;
259 rule->table = table;
a6276f0f
YW
260 rule->iif = iif;
261 rule->oif = oif;
bce67bbe
SS
262
263 r = set_ensure_allocated(rules, &routing_policy_rule_hash_ops);
264 if (r < 0)
265 return r;
266
267 r = set_put(*rules, rule);
268 if (r < 0)
269 return r;
270
271 if (ret)
272 *ret = rule;
273
274 rule = NULL;
a6276f0f 275 iif = oif = NULL;
bce67bbe
SS
276
277 return 0;
278}
279
280int routing_policy_rule_add(Manager *m,
281 int family,
282 const union in_addr_union *from,
283 uint8_t from_prefixlen,
284 const union in_addr_union *to,
285 uint8_t to_prefixlen,
286 uint8_t tos,
287 uint32_t fwmark,
288 uint32_t table,
eeab051b
YW
289 const char *iif,
290 const char *oif,
bce67bbe
SS
291 RoutingPolicyRule **ret) {
292
36e6e28b 293 return routing_policy_rule_add_internal(m, &m->rules, family, from, from_prefixlen, to, to_prefixlen, tos, fwmark, table, iif, oif, ret);
bce67bbe
SS
294}
295
296int routing_policy_rule_add_foreign(Manager *m,
297 int family,
298 const union in_addr_union *from,
299 uint8_t from_prefixlen,
300 const union in_addr_union *to,
301 uint8_t to_prefixlen,
302 uint8_t tos,
303 uint32_t fwmark,
304 uint32_t table,
eeab051b
YW
305 const char *iif,
306 const char *oif,
bce67bbe 307 RoutingPolicyRule **ret) {
36e6e28b 308 return routing_policy_rule_add_internal(m, &m->rules_foreign, family, from, from_prefixlen, to, to_prefixlen, tos, fwmark, table, iif, oif, ret);
bce67bbe
SS
309}
310
311static int routing_policy_rule_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
1046bf9b 312 Link *link = userdata;
bce67bbe
SS
313 int r;
314
315 assert(m);
316 assert(link);
317 assert(link->ifname);
318
7715629e 319 link->routing_policy_rule_remove_messages--;
bce67bbe
SS
320
321 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
322 return 1;
323
324 r = sd_netlink_message_get_errno(m);
325 if (r < 0)
326 log_link_warning_errno(link, r, "Could not drop routing policy rule: %m");
327
328 return 1;
329}
330
331int routing_policy_rule_remove(RoutingPolicyRule *routing_policy_rule, Link *link, sd_netlink_message_handler_t callback) {
332 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
333 int r;
334
335 assert(routing_policy_rule);
336 assert(link);
337 assert(link->manager);
338 assert(link->manager->rtnl);
339 assert(link->ifindex > 0);
340 assert(IN_SET(routing_policy_rule->family, AF_INET, AF_INET6));
341
342 r = sd_rtnl_message_new_routing_policy_rule(link->manager->rtnl, &m, RTM_DELRULE, routing_policy_rule->family);
343 if (r < 0)
344 return log_error_errno(r, "Could not allocate RTM_DELRULE message: %m");
345
346 if (!in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->from)) {
347 if (routing_policy_rule->family == AF_INET)
348 r = sd_netlink_message_append_in_addr(m, FRA_SRC, &routing_policy_rule->from.in);
349 else
350 r = sd_netlink_message_append_in6_addr(m, FRA_SRC, &routing_policy_rule->from.in6);
351
352 if (r < 0)
353 return log_error_errno(r, "Could not append FRA_SRC attribute: %m");
354
355 r = sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m, routing_policy_rule->from_prefixlen);
356 if (r < 0)
357 return log_error_errno(r, "Could not set source prefix length: %m");
358 }
359
360 if (!in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->to)) {
361 if (routing_policy_rule->family == AF_INET)
362 r = sd_netlink_message_append_in_addr(m, FRA_DST, &routing_policy_rule->to.in);
363 else
364 r = sd_netlink_message_append_in6_addr(m, FRA_DST, &routing_policy_rule->to.in6);
365
366 if (r < 0)
367 return log_error_errno(r, "Could not append FRA_DST attribute: %m");
368
369 r = sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m, routing_policy_rule->to_prefixlen);
370 if (r < 0)
371 return log_error_errno(r, "Could not set destination prefix length: %m");
372 }
373
ee38400b 374 r = sd_netlink_call_async(link->manager->rtnl, NULL, m, callback,
8190a388 375 link_netlink_destroy_callback, link, 0, __func__);
bce67bbe
SS
376 if (r < 0)
377 return log_error_errno(r, "Could not send rtnetlink message: %m");
378
379 link_ref(link);
380
381 return 0;
382}
383
384static int routing_policy_rule_new_static(Network *network, const char *filename, unsigned section_line, RoutingPolicyRule **ret) {
8e766630
LP
385 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL;
386 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
bce67bbe
SS
387 int r;
388
389 assert(network);
390 assert(ret);
391 assert(!!filename == (section_line > 0));
392
393 r = network_config_section_new(filename, section_line, &n);
394 if (r < 0)
395 return r;
396
397 rule = hashmap_get(network->rules_by_section, n);
398 if (rule) {
1cc6c93a 399 *ret = TAKE_PTR(rule);
bce67bbe
SS
400
401 return 0;
402 }
403
404 r = routing_policy_rule_new(&rule);
405 if (r < 0)
406 return r;
407
8e37fcbd 408 rule->section = TAKE_PTR(n);
bce67bbe 409 rule->network = network;
bce67bbe
SS
410
411 r = hashmap_put(network->rules_by_section, rule->section, rule);
412 if (r < 0)
413 return r;
414
415 LIST_APPEND(rules, network->rules, rule);
416 network->n_rules++;
417
1cc6c93a 418 *ret = TAKE_PTR(rule);
bce67bbe
SS
419
420 return 0;
421}
422
423int link_routing_policy_rule_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
1046bf9b 424 Link *link = userdata;
bce67bbe
SS
425 int r;
426
427 assert(rtnl);
428 assert(m);
429 assert(link);
430 assert(link->ifname);
7715629e 431 assert(link->routing_policy_rule_messages > 0);
bce67bbe 432
7715629e 433 link->routing_policy_rule_messages--;
bce67bbe
SS
434
435 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
436 return 1;
437
438 r = sd_netlink_message_get_errno(m);
439 if (r < 0 && r != -EEXIST)
440 log_link_warning_errno(link, r, "Could not add routing policy rule: %m");
441
7715629e 442 if (link->routing_policy_rule_messages == 0) {
bce67bbe 443 log_link_debug(link, "Routing policy rule configured");
7715629e
ST
444 link->routing_policy_rules_configured = true;
445 link_check_ready(link);
446 }
bce67bbe
SS
447
448 return 1;
449}
450
451int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, sd_netlink_message_handler_t callback, bool update) {
452 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
453 int r;
454
455 assert(rule);
456 assert(link);
457 assert(link->ifindex > 0);
458 assert(link->manager);
459 assert(link->manager->rtnl);
460
461 r = sd_rtnl_message_new_routing_policy_rule(link->manager->rtnl, &m, RTM_NEWRULE, rule->family);
462 if (r < 0)
463 return log_error_errno(r, "Could not allocate RTM_NEWRULE message: %m");
464
465 if (!in_addr_is_null(rule->family, &rule->from)) {
466 if (rule->family == AF_INET)
467 r = sd_netlink_message_append_in_addr(m, FRA_SRC, &rule->from.in);
468 else
469 r = sd_netlink_message_append_in6_addr(m, FRA_SRC, &rule->from.in6);
470
471 if (r < 0)
472 return log_error_errno(r, "Could not append FRA_SRC attribute: %m");
473
474 r = sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m, rule->from_prefixlen);
475 if (r < 0)
476 return log_error_errno(r, "Could not set source prefix length: %m");
477 }
478
479 if (!in_addr_is_null(rule->family, &rule->to)) {
480 if (rule->family == AF_INET)
481 r = sd_netlink_message_append_in_addr(m, FRA_DST, &rule->to.in);
482 else
483 r = sd_netlink_message_append_in6_addr(m, FRA_DST, &rule->to.in6);
484
485 if (r < 0)
486 return log_error_errno(r, "Could not append FRA_DST attribute: %m");
487
488 r = sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m, rule->to_prefixlen);
489 if (r < 0)
490 return log_error_errno(r, "Could not set destination prefix length: %m");
491 }
492
493 r = sd_netlink_message_append_u32(m, FRA_PRIORITY, rule->priority);
494 if (r < 0)
495 return log_error_errno(r, "Could not append FRA_PRIORITY attribute: %m");
496
497 if (rule->tos > 0) {
498 r = sd_rtnl_message_routing_policy_rule_set_tos(m, rule->tos);
499 if (r < 0)
500 return log_error_errno(r, "Could not set ip rule tos: %m");
501 }
502
503 if (rule->table < 256) {
504 r = sd_rtnl_message_routing_policy_rule_set_table(m, rule->table);
505 if (r < 0)
506 return log_error_errno(r, "Could not set ip rule table: %m");
507 } else {
508 r = sd_rtnl_message_routing_policy_rule_set_table(m, RT_TABLE_UNSPEC);
509 if (r < 0)
510 return log_error_errno(r, "Could not set ip rule table: %m");
511
512 r = sd_netlink_message_append_u32(m, FRA_TABLE, rule->table);
513 if (r < 0)
514 return log_error_errno(r, "Could not append FRA_TABLE attribute: %m");
515 }
516
517 if (rule->fwmark > 0) {
518 r = sd_netlink_message_append_u32(m, FRA_FWMARK, rule->fwmark);
519 if (r < 0)
520 return log_error_errno(r, "Could not append FRA_FWMARK attribute: %m");
521 }
522
523 if (rule->fwmask > 0) {
524 r = sd_netlink_message_append_u32(m, FRA_FWMASK, rule->fwmask);
525 if (r < 0)
526 return log_error_errno(r, "Could not append FRA_FWMASK attribute: %m");
527 }
528
762e2659
SS
529 if (rule->iif) {
530 r = sd_netlink_message_append_string(m, FRA_IFNAME, rule->iif);
531 if (r < 0)
532 return log_error_errno(r, "Could not append FRA_IFNAME attribute: %m");
533 }
534
535 if (rule->oif) {
536 r = sd_netlink_message_append_string(m, FRA_OIFNAME, rule->oif);
537 if (r < 0)
538 return log_error_errno(r, "Could not append FRA_OIFNAME attribute: %m");
539 }
540
bce67bbe
SS
541 rule->link = link;
542
ee38400b 543 r = sd_netlink_call_async(link->manager->rtnl, NULL, m, callback,
8190a388 544 link_netlink_destroy_callback, link, 0, __func__);
bce67bbe
SS
545 if (r < 0)
546 return log_error_errno(r, "Could not send rtnetlink message: %m");
547
548 link_ref(link);
549
550 r = routing_policy_rule_add(link->manager, rule->family, &rule->from, rule->from_prefixlen, &rule->to,
762e2659 551 rule->to_prefixlen, rule->tos, rule->fwmark, rule->table, rule->iif, rule->oif, NULL);
bce67bbe 552 if (r < 0)
cb700a11 553 return log_error_errno(r, "Could not add rule: %m");
bce67bbe
SS
554
555 return 0;
556}
557
558static int parse_fwmark_fwmask(const char *s, uint32_t *fwmark, uint32_t *fwmask) {
559 _cleanup_free_ char *f = NULL;
560 char *p;
561 int r;
562
563 assert(s);
564
565 f = strdup(s);
566 if (!f)
567 return -ENOMEM;
568
569 p = strchr(f, '/');
570 if (p)
571 *p++ = '\0';
572
573 r = safe_atou32(f, fwmark);
574 if (r < 0)
575 return log_error_errno(r, "Failed to parse RPDB rule firewall mark, ignoring: %s", f);
576
577 if (p) {
578 r = safe_atou32(p, fwmask);
579 if (r < 0)
580 return log_error_errno(r, "Failed to parse RPDB rule mask, ignoring: %s", f);
581 }
582
583 return 0;
584}
585
586int config_parse_routing_policy_rule_tos(
587 const char *unit,
588 const char *filename,
589 unsigned line,
590 const char *section,
591 unsigned section_line,
592 const char *lvalue,
593 int ltype,
594 const char *rvalue,
595 void *data,
596 void *userdata) {
597
8e766630 598 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
bce67bbe
SS
599 Network *network = userdata;
600 int r;
601
602 assert(filename);
603 assert(section);
604 assert(lvalue);
605 assert(rvalue);
606 assert(data);
607
608 r = routing_policy_rule_new_static(network, filename, section_line, &n);
609 if (r < 0)
610 return r;
611
612 r = safe_atou8(rvalue, &n->tos);
613 if (r < 0) {
614 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule tos, ignoring: %s", rvalue);
615 return 0;
616 }
617
618 n = NULL;
619
620 return 0;
621}
622
623int config_parse_routing_policy_rule_priority(
624 const char *unit,
625 const char *filename,
626 unsigned line,
627 const char *section,
628 unsigned section_line,
629 const char *lvalue,
630 int ltype,
631 const char *rvalue,
632 void *data,
633 void *userdata) {
634
8e766630 635 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
bce67bbe
SS
636 Network *network = userdata;
637 int r;
638
639 assert(filename);
640 assert(section);
641 assert(lvalue);
642 assert(rvalue);
643 assert(data);
644
645 r = routing_policy_rule_new_static(network, filename, section_line, &n);
646 if (r < 0)
647 return r;
648
649 r = safe_atou32(rvalue, &n->priority);
650 if (r < 0) {
651 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule priority, ignoring: %s", rvalue);
652 return 0;
653 }
654
655 n = NULL;
656
657 return 0;
658}
659
660int config_parse_routing_policy_rule_table(
661 const char *unit,
662 const char *filename,
663 unsigned line,
664 const char *section,
665 unsigned section_line,
666 const char *lvalue,
667 int ltype,
668 const char *rvalue,
669 void *data,
670 void *userdata) {
671
8e766630 672 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
bce67bbe
SS
673 Network *network = userdata;
674 int r;
675
676 assert(filename);
677 assert(section);
678 assert(lvalue);
679 assert(rvalue);
680 assert(data);
681
682 r = routing_policy_rule_new_static(network, filename, section_line, &n);
683 if (r < 0)
684 return r;
685
686 r = safe_atou32(rvalue, &n->table);
687 if (r < 0) {
688 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule table, ignoring: %s", rvalue);
689 return 0;
690 }
691
692 n = NULL;
693
694 return 0;
695}
696
697int config_parse_routing_policy_rule_fwmark_mask(
698 const char *unit,
699 const char *filename,
700 unsigned line,
701 const char *section,
702 unsigned section_line,
703 const char *lvalue,
704 int ltype,
705 const char *rvalue,
706 void *data,
707 void *userdata) {
708
8e766630 709 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
bce67bbe
SS
710 Network *network = userdata;
711 int r;
712
713 assert(filename);
714 assert(section);
715 assert(lvalue);
716 assert(rvalue);
717 assert(data);
718
719 r = routing_policy_rule_new_static(network, filename, section_line, &n);
720 if (r < 0)
721 return r;
722
723 r = parse_fwmark_fwmask(rvalue, &n->fwmark, &n->fwmask);
724 if (r < 0) {
725 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue);
726 return 0;
727 }
728
729 n = NULL;
730
731 return 0;
732}
733
734int config_parse_routing_policy_rule_prefix(
735 const char *unit,
736 const char *filename,
737 unsigned line,
738 const char *section,
739 unsigned section_line,
740 const char *lvalue,
741 int ltype,
742 const char *rvalue,
743 void *data,
744 void *userdata) {
745
8e766630 746 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
bce67bbe
SS
747 Network *network = userdata;
748 union in_addr_union buffer;
749 uint8_t prefixlen;
750 int r;
751
752 assert(filename);
753 assert(section);
754 assert(lvalue);
755 assert(rvalue);
756 assert(data);
757
758 r = routing_policy_rule_new_static(network, filename, section_line, &n);
759 if (r < 0)
760 return r;
761
762 r = in_addr_prefix_from_string(rvalue, AF_INET, &buffer, &prefixlen);
763 if (r < 0) {
764 r = in_addr_prefix_from_string(rvalue, AF_INET6, &buffer, &prefixlen);
765 if (r < 0) {
766 log_syntax(unit, LOG_ERR, filename, line, r, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue);
767 return 0;
768 }
769
770 n->family = AF_INET6;
771 } else
772 n->family = AF_INET;
773
774 if (streq(lvalue, "To")) {
775 n->to = buffer;
776 n->to_prefixlen = prefixlen;
777 } else {
778 n->from = buffer;
779 n->from_prefixlen = prefixlen;
780 }
781
782 n = NULL;
783
784 return 0;
785}
786
762e2659
SS
787int config_parse_routing_policy_rule_device(
788 const char *unit,
789 const char *filename,
790 unsigned line,
791 const char *section,
792 unsigned section_line,
793 const char *lvalue,
794 int ltype,
795 const char *rvalue,
796 void *data,
797 void *userdata) {
798
8e766630 799 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
762e2659
SS
800 Network *network = userdata;
801 int r;
802
803 assert(filename);
804 assert(section);
805 assert(lvalue);
806 assert(rvalue);
807 assert(data);
808
809 r = routing_policy_rule_new_static(network, filename, section_line, &n);
810 if (r < 0)
811 return r;
812
813 if (!ifname_valid(rvalue)) {
814 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse '%s' interface name, ignoring: %s", lvalue, rvalue);
815 return 0;
816 }
817
818 if (streq(lvalue, "IncomingInterface")) {
819 r = free_and_strdup(&n->iif, rvalue);
820 if (r < 0)
821 return log_oom();
822 } else {
823 r = free_and_strdup(&n->oif, rvalue);
824 if (r < 0)
825 return log_oom();
826 }
827
828 n = NULL;
829
830 return 0;
831}
832
458d8ae3 833static int routing_policy_rule_read_full_file(const char *state_file, char **ret) {
ac097c84 834 _cleanup_free_ char *s = NULL;
bce67bbe
SS
835 size_t size;
836 int r;
837
838 assert(state_file);
839
840 r = read_full_file(state_file, &s, &size);
841 if (r == -ENOENT)
842 return -ENODATA;
843 if (r < 0)
844 return r;
845 if (size <= 0)
846 return -ENODATA;
847
ae2a15bc 848 *ret = TAKE_PTR(s);
bce67bbe
SS
849
850 return size;
851}
852
458d8ae3
ZJS
853int routing_policy_serialize_rules(Set *rules, FILE *f) {
854 RoutingPolicyRule *rule = NULL;
855 Iterator i;
856 int r;
857
858 assert(f);
859
860 SET_FOREACH(rule, rules, i) {
861 _cleanup_free_ char *from_str = NULL, *to_str = NULL;
862 bool space = false;
863
864 fputs("RULE=", f);
865
866 if (!in_addr_is_null(rule->family, &rule->from)) {
867 r = in_addr_to_string(rule->family, &rule->from, &from_str);
868 if (r < 0)
869 return r;
870
871 fprintf(f, "from=%s/%hhu",
872 from_str, rule->from_prefixlen);
873 space = true;
874 }
875
876 if (!in_addr_is_null(rule->family, &rule->to)) {
877 r = in_addr_to_string(rule->family, &rule->to, &to_str);
878 if (r < 0)
879 return r;
880
881 fprintf(f, "%sto=%s/%hhu",
882 space ? " " : "",
883 to_str, rule->to_prefixlen);
884 space = true;
885 }
886
887 if (rule->tos != 0) {
888 fprintf(f, "%stos=%hhu",
889 space ? " " : "",
890 rule->tos);
891 space = true;
892 }
893
894 if (rule->fwmark != 0) {
895 fprintf(f, "%sfwmark=%"PRIu32"/%"PRIu32,
896 space ? " " : "",
897 rule->fwmark, rule->fwmask);
898 space = true;
899 }
900
9491f55f
ZJS
901 if (rule->iif) {
902 fprintf(f, "%siif=%s",
903 space ? " " : "",
904 rule->iif);
905 space = true;
906 }
907
908 if (rule->oif) {
909 fprintf(f, "%soif=%s",
910 space ? " " : "",
911 rule->oif);
912 space = true;
913 }
914
458d8ae3
ZJS
915 fprintf(f, "%stable=%"PRIu32 "\n",
916 space ? " " : "",
917 rule->table);
918 }
919
920 return 0;
921}
922
923int routing_policy_load_rules(const char *state_file, Set **rules) {
bce67bbe
SS
924 _cleanup_strv_free_ char **l = NULL;
925 _cleanup_free_ char *data = NULL;
926 const char *p;
927 char **i;
928 int r;
929
458d8ae3
ZJS
930 assert(state_file);
931 assert(rules);
bce67bbe 932
458d8ae3 933 r = routing_policy_rule_read_full_file(state_file, &data);
bce67bbe
SS
934 if (r <= 0)
935 return r;
936
937 l = strv_split_newlines(data);
938 if (!l)
939 return -ENOMEM;
940
458d8ae3 941 r = set_ensure_allocated(rules, &routing_policy_rule_hash_ops);
bce67bbe
SS
942 if (r < 0)
943 return r;
944
945 STRV_FOREACH(i, l) {
8e766630 946 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL;
bce67bbe
SS
947
948 p = startswith(*i, "RULE=");
949 if (!p)
950 continue;
951
bce67bbe
SS
952 r = routing_policy_rule_new(&rule);
953 if (r < 0)
954 return r;
955
956 for (;;) {
957 _cleanup_free_ char *word = NULL, *a = NULL, *b = NULL;
958 union in_addr_union buffer;
959 uint8_t prefixlen;
960
961 r = extract_first_word(&p, &word, NULL, 0);
962 if (r < 0)
963 return r;
964 if (r == 0)
965 break;
966
967 r = split_pair(word, "=", &a, &b);
968 if (r < 0)
969 continue;
970
971 if (STR_IN_SET(a, "from", "to")) {
972
973 r = in_addr_prefix_from_string(b, AF_INET, &buffer, &prefixlen);
974 if (r < 0) {
975 r = in_addr_prefix_from_string(b, AF_INET6, &buffer, &prefixlen);
976 if (r < 0) {
977 log_error_errno(r, "RPDB rule prefix is invalid, ignoring assignment: %s", b);
978 continue;
979 }
980
981 rule->family = AF_INET6;
982 } else
983 rule->family = AF_INET;
984
985 if (streq(a, "to")) {
986 rule->to = buffer;
987 rule->to_prefixlen = prefixlen;
988 } else {
989 rule->from = buffer;
990 rule->from_prefixlen = prefixlen;
991 }
992 } else if (streq(a, "tos")) {
993 r = safe_atou8(b, &rule->tos);
994 if (r < 0) {
995 log_error_errno(r, "Failed to parse RPDB rule tos, ignoring: %s", b);
996 continue;
997 }
998 } else if (streq(a, "table")) {
999 r = safe_atou32(b, &rule->table);
1000 if (r < 0) {
1001 log_error_errno(r, "Failed to parse RPDB rule table, ignoring: %s", b);
1002 continue;
1003 }
1004 } else if (streq(a, "fwmark")) {
1005
e4aca57d 1006 r = parse_fwmark_fwmask(b, &rule->fwmark, &rule->fwmask);
bce67bbe
SS
1007 if (r < 0) {
1008 log_error_errno(r, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a);
1009 continue;
1010 }
9491f55f 1011 } else if (streq(a, "iif")) {
762e2659 1012
93f9da6e 1013 if (free_and_strdup(&rule->iif, b) < 0)
762e2659 1014 return log_oom();
93f9da6e 1015
9491f55f 1016 } else if (streq(a, "oif")) {
762e2659 1017
93f9da6e 1018 if (free_and_strdup(&rule->oif, b) < 0)
762e2659 1019 return log_oom();
bce67bbe
SS
1020 }
1021 }
1022
458d8ae3 1023 r = set_put(*rules, rule);
bce67bbe
SS
1024 if (r < 0) {
1025 log_warning_errno(r, "Failed to add RPDB rule to saved DB, ignoring: %s", p);
1026 continue;
1027 }
1028
1029 rule = NULL;
1030 }
1031
1032 return 0;
1033}
1034
1035void routing_policy_rule_purge(Manager *m, Link *link) {
1036 RoutingPolicyRule *rule, *existing;
1037 Iterator i;
1038 int r;
1039
1040 assert(m);
1041 assert(link);
1042
1043 SET_FOREACH(rule, m->rules_saved, i) {
1044 existing = set_get(m->rules_foreign, rule);
1045 if (existing) {
1046
1047 r = routing_policy_rule_remove(rule, link, routing_policy_rule_remove_handler);
1048 if (r < 0) {
1049 log_warning_errno(r, "Could not remove routing policy rules: %m");
1050 continue;
1051 }
1052
7715629e 1053 link->routing_policy_rule_remove_messages++;
bce67bbe
SS
1054 }
1055 }
1056}