]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-routing-policy-rule.c
Merge branch 'predictable-interface-names'
[thirdparty/systemd.git] / src / network / networkd-routing-policy-rule.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 "ip-protocol-list.h"
10 #include "networkd-routing-policy-rule.h"
11 #include "netlink-util.h"
12 #include "networkd-manager.h"
13 #include "parse-util.h"
14 #include "socket-util.h"
15 #include "string-util.h"
16 #include "strv.h"
17
18 int routing_policy_rule_new(RoutingPolicyRule **ret) {
19 RoutingPolicyRule *rule;
20
21 rule = new(RoutingPolicyRule, 1);
22 if (!rule)
23 return -ENOMEM;
24
25 *rule = (RoutingPolicyRule) {
26 .family = AF_INET,
27 .table = RT_TABLE_MAIN,
28 };
29
30 *ret = rule;
31 return 0;
32 }
33
34 void routing_policy_rule_free(RoutingPolicyRule *rule) {
35
36 if (!rule)
37 return;
38
39 if (rule->network) {
40 LIST_REMOVE(rules, rule->network->rules, rule);
41 assert(rule->network->n_rules > 0);
42 rule->network->n_rules--;
43
44 if (rule->section)
45 hashmap_remove(rule->network->rules_by_section, rule->section);
46 }
47
48 if (rule->manager) {
49 set_remove(rule->manager->rules, rule);
50 set_remove(rule->manager->rules_foreign, rule);
51 }
52
53 network_config_section_free(rule->section);
54 free(rule->iif);
55 free(rule->oif);
56 free(rule);
57 }
58
59 static void routing_policy_rule_hash_func(const RoutingPolicyRule *rule, struct siphash *state) {
60 assert(rule);
61
62 siphash24_compress(&rule->family, sizeof(rule->family), state);
63
64 switch (rule->family) {
65 case AF_INET:
66 case AF_INET6:
67
68 siphash24_compress(&rule->from, FAMILY_ADDRESS_SIZE(rule->family), state);
69 siphash24_compress(&rule->from_prefixlen, sizeof(rule->from_prefixlen), state);
70
71 siphash24_compress(&rule->to, FAMILY_ADDRESS_SIZE(rule->family), state);
72 siphash24_compress(&rule->to_prefixlen, sizeof(rule->to_prefixlen), state);
73
74 siphash24_compress(&rule->tos, sizeof(rule->tos), state);
75 siphash24_compress(&rule->fwmark, sizeof(rule->fwmark), state);
76 siphash24_compress(&rule->table, sizeof(rule->table), state);
77
78 siphash24_compress(&rule->protocol, sizeof(rule->protocol), state);
79 siphash24_compress(&rule->sport, sizeof(rule->sport), state);
80 siphash24_compress(&rule->dport, sizeof(rule->dport), state);
81
82 if (rule->iif)
83 siphash24_compress(rule->iif, strlen(rule->iif), state);
84
85 if (rule->oif)
86 siphash24_compress(rule->oif, strlen(rule->oif), state);
87
88 break;
89 default:
90 /* treat any other address family as AF_UNSPEC */
91 break;
92 }
93 }
94
95 static int routing_policy_rule_compare_func(const RoutingPolicyRule *a, const RoutingPolicyRule *b) {
96 int r;
97
98 r = CMP(a->family, b->family);
99 if (r != 0)
100 return r;
101
102 switch (a->family) {
103 case AF_INET:
104 case AF_INET6:
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;
124
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
133 r = CMP(a->protocol, b->protocol);
134 if (r != 0)
135 return r;
136
137 r = memcmp(&a->sport, &b->sport, sizeof(a->sport));
138 if (r != 0)
139 return r;
140
141 r = memcmp(&a->dport, &b->dport, sizeof(a->dport));
142 if (r != 0)
143 return r;
144
145 r = memcmp(&a->from, &b->from, FAMILY_ADDRESS_SIZE(a->family));
146 if (r != 0)
147 return r;
148
149 return memcmp(&a->to, &b->to, FAMILY_ADDRESS_SIZE(a->family));
150
151 default:
152 /* treat any other address family as AF_UNSPEC */
153 return 0;
154 }
155 }
156
157 DEFINE_PRIVATE_HASH_OPS(routing_policy_rule_hash_ops, RoutingPolicyRule, routing_policy_rule_hash_func, routing_policy_rule_compare_func);
158
159 int routing_policy_rule_get(Manager *m,
160 int family,
161 const union in_addr_union *from,
162 uint8_t from_prefixlen,
163 const union in_addr_union *to,
164 uint8_t to_prefixlen,
165 uint8_t tos,
166 uint32_t fwmark,
167 uint32_t table,
168 const char *iif,
169 const char *oif,
170 uint8_t protocol,
171 struct fib_rule_port_range *sport,
172 struct fib_rule_port_range *dport,
173 RoutingPolicyRule **ret) {
174
175 RoutingPolicyRule rule, *existing;
176
177 assert_return(m, -1);
178
179 rule = (RoutingPolicyRule) {
180 .family = family,
181 .from = *from,
182 .from_prefixlen = from_prefixlen,
183 .to = *to,
184 .to_prefixlen = to_prefixlen,
185 .tos = tos,
186 .fwmark = fwmark,
187 .table = table,
188 .iif = (char*) iif,
189 .oif = (char*) oif,
190 .protocol = protocol,
191 .sport = *sport,
192 .dport = *dport,
193 };
194
195 existing = set_get(m->rules, &rule);
196 if (existing) {
197 if (ret)
198 *ret = existing;
199 return 1;
200 }
201
202 existing = set_get(m->rules_foreign, &rule);
203 if (existing) {
204 if (ret)
205 *ret = existing;
206 return 0;
207 }
208
209 return -ENOENT;
210 }
211
212 int routing_policy_rule_make_local(Manager *m, RoutingPolicyRule *rule) {
213 int r;
214
215 assert(m);
216
217 if (set_contains(m->rules_foreign, rule)) {
218 set_remove(m->rules_foreign, rule);
219
220 r = set_ensure_allocated(&m->rules, &routing_policy_rule_hash_ops);
221 if (r < 0)
222 return r;
223
224 return set_put(m->rules, rule);
225 }
226
227 return -ENOENT;
228 }
229
230 static int routing_policy_rule_add_internal(Manager *m,
231 Set **rules,
232 int family,
233 const union in_addr_union *from,
234 uint8_t from_prefixlen,
235 const union in_addr_union *to,
236 uint8_t to_prefixlen,
237 uint8_t tos,
238 uint32_t fwmark,
239 uint32_t table,
240 const char *_iif,
241 const char *_oif,
242 uint8_t protocol,
243 const struct fib_rule_port_range *sport,
244 const struct fib_rule_port_range *dport,
245 RoutingPolicyRule **ret) {
246
247 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL;
248 _cleanup_free_ char *iif = NULL, *oif = NULL;
249 int r;
250
251 assert_return(rules, -EINVAL);
252
253 if (_iif) {
254 iif = strdup(_iif);
255 if (!iif)
256 return -ENOMEM;
257 }
258
259 if (_oif) {
260 oif = strdup(_oif);
261 if (!oif)
262 return -ENOMEM;
263 }
264
265 r = routing_policy_rule_new(&rule);
266 if (r < 0)
267 return r;
268
269 rule->manager = m;
270 rule->family = family;
271 rule->from = *from;
272 rule->from_prefixlen = from_prefixlen;
273 rule->to = *to;
274 rule->to_prefixlen = to_prefixlen;
275 rule->tos = tos;
276 rule->fwmark = fwmark;
277 rule->table = table;
278 rule->iif = iif;
279 rule->oif = oif;
280 rule->protocol = protocol;
281 rule->sport = *sport;
282 rule->dport = *dport;
283
284 r = set_ensure_allocated(rules, &routing_policy_rule_hash_ops);
285 if (r < 0)
286 return r;
287
288 r = set_put(*rules, rule);
289 if (r < 0)
290 return r;
291
292 if (ret)
293 *ret = rule;
294
295 rule = NULL;
296 iif = oif = NULL;
297
298 return 0;
299 }
300
301 int routing_policy_rule_add(Manager *m,
302 int family,
303 const union in_addr_union *from,
304 uint8_t from_prefixlen,
305 const union in_addr_union *to,
306 uint8_t to_prefixlen,
307 uint8_t tos,
308 uint32_t fwmark,
309 uint32_t table,
310 const char *iif,
311 const char *oif,
312 uint8_t protocol,
313 const struct fib_rule_port_range *sport,
314 const struct fib_rule_port_range *dport,
315 RoutingPolicyRule **ret) {
316
317 return routing_policy_rule_add_internal(m, &m->rules, family, from, from_prefixlen, to, to_prefixlen, tos, fwmark, table, iif, oif, protocol, sport, dport, ret);
318 }
319
320 int routing_policy_rule_add_foreign(Manager *m,
321 int family,
322 const union in_addr_union *from,
323 uint8_t from_prefixlen,
324 const union in_addr_union *to,
325 uint8_t to_prefixlen,
326 uint8_t tos,
327 uint32_t fwmark,
328 uint32_t table,
329 const char *iif,
330 const char *oif,
331 uint8_t protocol,
332 const struct fib_rule_port_range *sport,
333 const struct fib_rule_port_range *dport,
334 RoutingPolicyRule **ret) {
335 return routing_policy_rule_add_internal(m, &m->rules_foreign, family, from, from_prefixlen, to, to_prefixlen, tos, fwmark, table, iif, oif, protocol, sport, dport, ret);
336 }
337
338 static int routing_policy_rule_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
339 int r;
340
341 assert(m);
342 assert(link);
343 assert(link->ifname);
344
345 link->routing_policy_rule_remove_messages--;
346
347 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
348 return 1;
349
350 r = sd_netlink_message_get_errno(m);
351 if (r < 0)
352 log_link_warning_errno(link, r, "Could not drop routing policy rule: %m");
353
354 return 1;
355 }
356
357 int routing_policy_rule_remove(RoutingPolicyRule *routing_policy_rule, Link *link, link_netlink_message_handler_t callback) {
358 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
359 int r;
360
361 assert(routing_policy_rule);
362 assert(link);
363 assert(link->manager);
364 assert(link->manager->rtnl);
365 assert(link->ifindex > 0);
366 assert(IN_SET(routing_policy_rule->family, AF_INET, AF_INET6));
367
368 r = sd_rtnl_message_new_routing_policy_rule(link->manager->rtnl, &m, RTM_DELRULE, routing_policy_rule->family);
369 if (r < 0)
370 return log_error_errno(r, "Could not allocate RTM_DELRULE message: %m");
371
372 if (!in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->from)) {
373 if (routing_policy_rule->family == AF_INET)
374 r = sd_netlink_message_append_in_addr(m, FRA_SRC, &routing_policy_rule->from.in);
375 else
376 r = sd_netlink_message_append_in6_addr(m, FRA_SRC, &routing_policy_rule->from.in6);
377
378 if (r < 0)
379 return log_error_errno(r, "Could not append FRA_SRC attribute: %m");
380
381 r = sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m, routing_policy_rule->from_prefixlen);
382 if (r < 0)
383 return log_error_errno(r, "Could not set source prefix length: %m");
384 }
385
386 if (!in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->to)) {
387 if (routing_policy_rule->family == AF_INET)
388 r = sd_netlink_message_append_in_addr(m, FRA_DST, &routing_policy_rule->to.in);
389 else
390 r = sd_netlink_message_append_in6_addr(m, FRA_DST, &routing_policy_rule->to.in6);
391
392 if (r < 0)
393 return log_error_errno(r, "Could not append FRA_DST attribute: %m");
394
395 r = sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m, routing_policy_rule->to_prefixlen);
396 if (r < 0)
397 return log_error_errno(r, "Could not set destination prefix length: %m");
398 }
399
400 r = netlink_call_async(link->manager->rtnl, NULL, m,
401 callback ?: routing_policy_rule_remove_handler,
402 link_netlink_destroy_callback, link);
403 if (r < 0)
404 return log_error_errno(r, "Could not send rtnetlink message: %m");
405
406 link_ref(link);
407
408 return 0;
409 }
410
411 static int routing_policy_rule_new_static(Network *network, const char *filename, unsigned section_line, RoutingPolicyRule **ret) {
412 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL;
413 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
414 int r;
415
416 assert(network);
417 assert(ret);
418 assert(!!filename == (section_line > 0));
419
420 if (filename) {
421 r = network_config_section_new(filename, section_line, &n);
422 if (r < 0)
423 return r;
424
425 rule = hashmap_get(network->rules_by_section, n);
426 if (rule) {
427 *ret = TAKE_PTR(rule);
428
429 return 0;
430 }
431 }
432
433 r = routing_policy_rule_new(&rule);
434 if (r < 0)
435 return r;
436
437 rule->network = network;
438 LIST_APPEND(rules, network->rules, rule);
439 network->n_rules++;
440
441 if (filename) {
442 rule->section = TAKE_PTR(n);
443
444 r = hashmap_ensure_allocated(&network->rules_by_section, &network_config_hash_ops);
445 if (r < 0)
446 return r;
447
448 r = hashmap_put(network->rules_by_section, rule->section, rule);
449 if (r < 0)
450 return r;
451 }
452
453 *ret = TAKE_PTR(rule);
454
455 return 0;
456 }
457
458 static int routing_policy_rule_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
459 int r;
460
461 assert(rtnl);
462 assert(m);
463 assert(link);
464 assert(link->ifname);
465 assert(link->routing_policy_rule_messages > 0);
466
467 link->routing_policy_rule_messages--;
468
469 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
470 return 1;
471
472 r = sd_netlink_message_get_errno(m);
473 if (r < 0 && r != -EEXIST)
474 log_link_warning_errno(link, r, "Could not add routing policy rule: %m");
475
476 if (link->routing_policy_rule_messages == 0) {
477 log_link_debug(link, "Routing policy rule configured");
478 link->routing_policy_rules_configured = true;
479 link_check_ready(link);
480 }
481
482 return 1;
483 }
484
485 int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, link_netlink_message_handler_t callback, bool update) {
486 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
487 int r;
488
489 assert(rule);
490 assert(link);
491 assert(link->ifindex > 0);
492 assert(link->manager);
493 assert(link->manager->rtnl);
494
495 r = sd_rtnl_message_new_routing_policy_rule(link->manager->rtnl, &m, RTM_NEWRULE, rule->family);
496 if (r < 0)
497 return log_error_errno(r, "Could not allocate RTM_NEWRULE message: %m");
498
499 if (!in_addr_is_null(rule->family, &rule->from)) {
500 if (rule->family == AF_INET)
501 r = sd_netlink_message_append_in_addr(m, FRA_SRC, &rule->from.in);
502 else
503 r = sd_netlink_message_append_in6_addr(m, FRA_SRC, &rule->from.in6);
504
505 if (r < 0)
506 return log_error_errno(r, "Could not append FRA_SRC attribute: %m");
507
508 r = sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m, rule->from_prefixlen);
509 if (r < 0)
510 return log_error_errno(r, "Could not set source prefix length: %m");
511 }
512
513 if (!in_addr_is_null(rule->family, &rule->to)) {
514 if (rule->family == AF_INET)
515 r = sd_netlink_message_append_in_addr(m, FRA_DST, &rule->to.in);
516 else
517 r = sd_netlink_message_append_in6_addr(m, FRA_DST, &rule->to.in6);
518
519 if (r < 0)
520 return log_error_errno(r, "Could not append FRA_DST attribute: %m");
521
522 r = sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m, rule->to_prefixlen);
523 if (r < 0)
524 return log_error_errno(r, "Could not set destination prefix length: %m");
525 }
526
527 r = sd_netlink_message_append_u32(m, FRA_PRIORITY, rule->priority);
528 if (r < 0)
529 return log_error_errno(r, "Could not append FRA_PRIORITY attribute: %m");
530
531 if (rule->tos > 0) {
532 r = sd_rtnl_message_routing_policy_rule_set_tos(m, rule->tos);
533 if (r < 0)
534 return log_error_errno(r, "Could not set ip rule tos: %m");
535 }
536
537 if (rule->table < 256) {
538 r = sd_rtnl_message_routing_policy_rule_set_table(m, rule->table);
539 if (r < 0)
540 return log_error_errno(r, "Could not set ip rule table: %m");
541 } else {
542 r = sd_rtnl_message_routing_policy_rule_set_table(m, RT_TABLE_UNSPEC);
543 if (r < 0)
544 return log_error_errno(r, "Could not set ip rule table: %m");
545
546 r = sd_netlink_message_append_u32(m, FRA_TABLE, rule->table);
547 if (r < 0)
548 return log_error_errno(r, "Could not append FRA_TABLE attribute: %m");
549 }
550
551 if (rule->fwmark > 0) {
552 r = sd_netlink_message_append_u32(m, FRA_FWMARK, rule->fwmark);
553 if (r < 0)
554 return log_error_errno(r, "Could not append FRA_FWMARK attribute: %m");
555 }
556
557 if (rule->fwmask > 0) {
558 r = sd_netlink_message_append_u32(m, FRA_FWMASK, rule->fwmask);
559 if (r < 0)
560 return log_error_errno(r, "Could not append FRA_FWMASK attribute: %m");
561 }
562
563 if (rule->iif) {
564 r = sd_netlink_message_append_string(m, FRA_IFNAME, rule->iif);
565 if (r < 0)
566 return log_error_errno(r, "Could not append FRA_IFNAME attribute: %m");
567 }
568
569 if (rule->oif) {
570 r = sd_netlink_message_append_string(m, FRA_OIFNAME, rule->oif);
571 if (r < 0)
572 return log_error_errno(r, "Could not append FRA_OIFNAME attribute: %m");
573 }
574
575 r = sd_netlink_message_append_u8(m, FRA_IP_PROTO, rule->protocol);
576 if (r < 0)
577 return log_error_errno(r, "Could not append FRA_IP_PROTO attribute: %m");
578
579 if (rule->sport.start != 0 || rule->sport.end != 0) {
580 r = sd_netlink_message_append_data(m, FRA_SPORT_RANGE, &rule->sport, sizeof(rule->sport));
581 if (r < 0)
582 return log_error_errno(r, "Could not append FRA_SPORT_RANGE attribute: %m");
583 }
584
585 if (rule->dport.start != 0 || rule->dport.end != 0) {
586 r = sd_netlink_message_append_data(m, FRA_DPORT_RANGE, &rule->dport, sizeof(rule->dport));
587 if (r < 0)
588 return log_error_errno(r, "Could not append FRA_DPORT_RANGE attribute: %m");
589 }
590
591 if (rule->invert_rule) {
592 r = sd_rtnl_message_routing_policy_rule_set_flags(m, FIB_RULE_INVERT);
593 if (r < 0)
594 return log_error_errno(r, "Could not append FIB_RULE_INVERT attribute: %m");
595 }
596
597 rule->link = link;
598
599 r = netlink_call_async(link->manager->rtnl, NULL, m,
600 callback ?: routing_policy_rule_handler,
601 link_netlink_destroy_callback, link);
602 if (r < 0)
603 return log_error_errno(r, "Could not send rtnetlink message: %m");
604
605 link_ref(link);
606
607 r = routing_policy_rule_add(link->manager, rule->family, &rule->from, rule->from_prefixlen, &rule->to,
608 rule->to_prefixlen, rule->tos, rule->fwmark, rule->table, rule->iif, rule->oif, rule->protocol, &rule->sport, &rule->dport, NULL);
609 if (r < 0)
610 return log_error_errno(r, "Could not add rule: %m");
611
612 return 0;
613 }
614
615 static int parse_fwmark_fwmask(const char *s, uint32_t *fwmark, uint32_t *fwmask) {
616 _cleanup_free_ char *f = NULL;
617 char *p;
618 int r;
619
620 assert(s);
621
622 f = strdup(s);
623 if (!f)
624 return -ENOMEM;
625
626 p = strchr(f, '/');
627 if (p)
628 *p++ = '\0';
629
630 r = safe_atou32(f, fwmark);
631 if (r < 0)
632 return log_error_errno(r, "Failed to parse RPDB rule firewall mark, ignoring: %s", f);
633
634 if (p) {
635 r = safe_atou32(p, fwmask);
636 if (r < 0)
637 return log_error_errno(r, "Failed to parse RPDB rule mask, ignoring: %s", f);
638 }
639
640 return 0;
641 }
642
643 int config_parse_routing_policy_rule_tos(
644 const char *unit,
645 const char *filename,
646 unsigned line,
647 const char *section,
648 unsigned section_line,
649 const char *lvalue,
650 int ltype,
651 const char *rvalue,
652 void *data,
653 void *userdata) {
654
655 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
656 Network *network = userdata;
657 int r;
658
659 assert(filename);
660 assert(section);
661 assert(lvalue);
662 assert(rvalue);
663 assert(data);
664
665 r = routing_policy_rule_new_static(network, filename, section_line, &n);
666 if (r < 0)
667 return r;
668
669 r = safe_atou8(rvalue, &n->tos);
670 if (r < 0) {
671 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule tos, ignoring: %s", rvalue);
672 return 0;
673 }
674
675 n = NULL;
676
677 return 0;
678 }
679
680 int config_parse_routing_policy_rule_priority(
681 const char *unit,
682 const char *filename,
683 unsigned line,
684 const char *section,
685 unsigned section_line,
686 const char *lvalue,
687 int ltype,
688 const char *rvalue,
689 void *data,
690 void *userdata) {
691
692 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
693 Network *network = userdata;
694 int r;
695
696 assert(filename);
697 assert(section);
698 assert(lvalue);
699 assert(rvalue);
700 assert(data);
701
702 r = routing_policy_rule_new_static(network, filename, section_line, &n);
703 if (r < 0)
704 return r;
705
706 r = safe_atou32(rvalue, &n->priority);
707 if (r < 0) {
708 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule priority, ignoring: %s", rvalue);
709 return 0;
710 }
711
712 n = NULL;
713
714 return 0;
715 }
716
717 int config_parse_routing_policy_rule_table(
718 const char *unit,
719 const char *filename,
720 unsigned line,
721 const char *section,
722 unsigned section_line,
723 const char *lvalue,
724 int ltype,
725 const char *rvalue,
726 void *data,
727 void *userdata) {
728
729 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
730 Network *network = userdata;
731 int r;
732
733 assert(filename);
734 assert(section);
735 assert(lvalue);
736 assert(rvalue);
737 assert(data);
738
739 r = routing_policy_rule_new_static(network, filename, section_line, &n);
740 if (r < 0)
741 return r;
742
743 r = safe_atou32(rvalue, &n->table);
744 if (r < 0) {
745 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule table, ignoring: %s", rvalue);
746 return 0;
747 }
748
749 n = NULL;
750
751 return 0;
752 }
753
754 int config_parse_routing_policy_rule_fwmark_mask(
755 const char *unit,
756 const char *filename,
757 unsigned line,
758 const char *section,
759 unsigned section_line,
760 const char *lvalue,
761 int ltype,
762 const char *rvalue,
763 void *data,
764 void *userdata) {
765
766 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
767 Network *network = userdata;
768 int r;
769
770 assert(filename);
771 assert(section);
772 assert(lvalue);
773 assert(rvalue);
774 assert(data);
775
776 r = routing_policy_rule_new_static(network, filename, section_line, &n);
777 if (r < 0)
778 return r;
779
780 r = parse_fwmark_fwmask(rvalue, &n->fwmark, &n->fwmask);
781 if (r < 0) {
782 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue);
783 return 0;
784 }
785
786 n = NULL;
787
788 return 0;
789 }
790
791 int config_parse_routing_policy_rule_prefix(
792 const char *unit,
793 const char *filename,
794 unsigned line,
795 const char *section,
796 unsigned section_line,
797 const char *lvalue,
798 int ltype,
799 const char *rvalue,
800 void *data,
801 void *userdata) {
802
803 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
804 Network *network = userdata;
805 union in_addr_union *buffer;
806 uint8_t *prefixlen;
807 int r;
808
809 assert(filename);
810 assert(section);
811 assert(lvalue);
812 assert(rvalue);
813 assert(data);
814
815 r = routing_policy_rule_new_static(network, filename, section_line, &n);
816 if (r < 0)
817 return r;
818
819 if (streq(lvalue, "To")) {
820 buffer = &n->to;
821 prefixlen = &n->to_prefixlen;
822 } else {
823 buffer = &n->from;
824 prefixlen = &n->from_prefixlen;
825 }
826
827 r = in_addr_prefix_from_string_auto(rvalue, &n->family, buffer, prefixlen);
828 if (r < 0) {
829 log_syntax(unit, LOG_ERR, filename, line, r, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue);
830 return 0;
831 }
832
833 n = NULL;
834
835 return 0;
836 }
837
838 int config_parse_routing_policy_rule_device(
839 const char *unit,
840 const char *filename,
841 unsigned line,
842 const char *section,
843 unsigned section_line,
844 const char *lvalue,
845 int ltype,
846 const char *rvalue,
847 void *data,
848 void *userdata) {
849
850 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
851 Network *network = userdata;
852 int r;
853
854 assert(filename);
855 assert(section);
856 assert(lvalue);
857 assert(rvalue);
858 assert(data);
859
860 r = routing_policy_rule_new_static(network, filename, section_line, &n);
861 if (r < 0)
862 return r;
863
864 if (!ifname_valid(rvalue)) {
865 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse '%s' interface name, ignoring: %s", lvalue, rvalue);
866 return 0;
867 }
868
869 if (streq(lvalue, "IncomingInterface")) {
870 r = free_and_strdup(&n->iif, rvalue);
871 if (r < 0)
872 return log_oom();
873 } else {
874 r = free_and_strdup(&n->oif, rvalue);
875 if (r < 0)
876 return log_oom();
877 }
878
879 n = NULL;
880
881 return 0;
882 }
883
884 int config_parse_routing_policy_rule_port_range(
885 const char *unit,
886 const char *filename,
887 unsigned line,
888 const char *section,
889 unsigned section_line,
890 const char *lvalue,
891 int ltype,
892 const char *rvalue,
893 void *data,
894 void *userdata) {
895 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
896 Network *network = userdata;
897 uint16_t low, high;
898 int r;
899
900 assert(filename);
901 assert(section);
902 assert(lvalue);
903 assert(rvalue);
904 assert(data);
905
906 r = routing_policy_rule_new_static(network, filename, section_line, &n);
907 if (r < 0)
908 return r;
909
910 r = parse_ip_port_range(rvalue, &low, &high);
911 if (r < 0) {
912 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse routing policy rule port range '%s'", rvalue);
913 return 0;
914 }
915
916 if (streq(lvalue, "SourcePort")) {
917 n->sport.start = low;
918 n->sport.end = high;
919 } else {
920 n->dport.start = low;
921 n->dport.end = high;
922 }
923
924 n = NULL;
925
926 return 0;
927 }
928
929 int config_parse_routing_policy_rule_ip_protocol(
930 const char *unit,
931 const char *filename,
932 unsigned line,
933 const char *section,
934 unsigned section_line,
935 const char *lvalue,
936 int ltype,
937 const char *rvalue,
938 void *data,
939 void *userdata) {
940
941 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
942 Network *network = userdata;
943 int r;
944
945 assert(filename);
946 assert(section);
947 assert(lvalue);
948 assert(rvalue);
949 assert(data);
950
951 r = routing_policy_rule_new_static(network, filename, section_line, &n);
952 if (r < 0)
953 return r;
954
955 r = parse_ip_protocol(rvalue);
956 if (r < 0) {
957 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IP protocol '%s' for routing policy rule, ignoring: %m", rvalue);
958 return 0;
959 }
960
961 n->protocol = r;
962
963 n = NULL;
964
965 return 0;
966 }
967
968 int config_parse_routing_policy_rule_invert(
969 const char *unit,
970 const char *filename,
971 unsigned line,
972 const char *section,
973 unsigned section_line,
974 const char *lvalue,
975 int ltype,
976 const char *rvalue,
977 void *data,
978 void *userdata) {
979
980 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL;
981 Network *network = userdata;
982 int r;
983
984 assert(filename);
985 assert(section);
986 assert(lvalue);
987 assert(rvalue);
988 assert(data);
989
990 r = routing_policy_rule_new_static(network, filename, section_line, &n);
991 if (r < 0)
992 return r;
993
994 r = parse_boolean(rvalue);
995 if (r < 0) {
996 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule invert, ignoring: %s", rvalue);
997 return 0;
998 }
999
1000 n->invert_rule = r;
1001
1002 n = NULL;
1003
1004 return 0;
1005 }
1006
1007 static int routing_policy_rule_read_full_file(const char *state_file, char **ret) {
1008 _cleanup_free_ char *s = NULL;
1009 size_t size;
1010 int r;
1011
1012 assert(state_file);
1013
1014 r = read_full_file(state_file, &s, &size);
1015 if (r == -ENOENT)
1016 return -ENODATA;
1017 if (r < 0)
1018 return r;
1019 if (size <= 0)
1020 return -ENODATA;
1021
1022 *ret = TAKE_PTR(s);
1023
1024 return size;
1025 }
1026
1027 int routing_policy_serialize_rules(Set *rules, FILE *f) {
1028 RoutingPolicyRule *rule = NULL;
1029 Iterator i;
1030 int r;
1031
1032 assert(f);
1033
1034 SET_FOREACH(rule, rules, i) {
1035 _cleanup_free_ char *from_str = NULL, *to_str = NULL;
1036 bool space = false;
1037
1038 fputs("RULE=", f);
1039
1040 if (!in_addr_is_null(rule->family, &rule->from)) {
1041 r = in_addr_to_string(rule->family, &rule->from, &from_str);
1042 if (r < 0)
1043 return r;
1044
1045 fprintf(f, "from=%s/%hhu",
1046 from_str, rule->from_prefixlen);
1047 space = true;
1048 }
1049
1050 if (!in_addr_is_null(rule->family, &rule->to)) {
1051 r = in_addr_to_string(rule->family, &rule->to, &to_str);
1052 if (r < 0)
1053 return r;
1054
1055 fprintf(f, "%sto=%s/%hhu",
1056 space ? " " : "",
1057 to_str, rule->to_prefixlen);
1058 space = true;
1059 }
1060
1061 if (rule->tos != 0) {
1062 fprintf(f, "%stos=%hhu",
1063 space ? " " : "",
1064 rule->tos);
1065 space = true;
1066 }
1067
1068 if (rule->fwmark != 0) {
1069 fprintf(f, "%sfwmark=%"PRIu32"/%"PRIu32,
1070 space ? " " : "",
1071 rule->fwmark, rule->fwmask);
1072 space = true;
1073 }
1074
1075 if (rule->iif) {
1076 fprintf(f, "%siif=%s",
1077 space ? " " : "",
1078 rule->iif);
1079 space = true;
1080 }
1081
1082 if (rule->oif) {
1083 fprintf(f, "%soif=%s",
1084 space ? " " : "",
1085 rule->oif);
1086 space = true;
1087 }
1088
1089 if (rule->protocol != 0) {
1090 fprintf(f, "%sprotocol=%hhu",
1091 space ? " " : "",
1092 rule->protocol);
1093 space = true;
1094 }
1095
1096 if (rule->sport.start != 0 || rule->sport.end != 0) {
1097 fprintf(f, "%ssourcesport=%"PRIu16"-%"PRIu16,
1098 space ? " " : "",
1099 rule->sport.start, rule->sport.end);
1100 space = true;
1101 }
1102
1103 if (rule->dport.start != 0 || rule->dport.end != 0) {
1104 fprintf(f, "%sdestinationport=%"PRIu16"-%"PRIu16,
1105 space ? " " : "",
1106 rule->dport.start, rule->dport.end);
1107 space = true;
1108 }
1109
1110 fprintf(f, "%stable=%"PRIu32 "\n",
1111 space ? " " : "",
1112 rule->table);
1113 }
1114
1115 return 0;
1116 }
1117
1118 int routing_policy_load_rules(const char *state_file, Set **rules) {
1119 _cleanup_strv_free_ char **l = NULL;
1120 _cleanup_free_ char *data = NULL;
1121 uint16_t low = 0, high = 0;
1122 const char *p;
1123 char **i;
1124 int r;
1125
1126 assert(state_file);
1127 assert(rules);
1128
1129 r = routing_policy_rule_read_full_file(state_file, &data);
1130 if (r <= 0)
1131 return r;
1132
1133 l = strv_split_newlines(data);
1134 if (!l)
1135 return -ENOMEM;
1136
1137 r = set_ensure_allocated(rules, &routing_policy_rule_hash_ops);
1138 if (r < 0)
1139 return r;
1140
1141 STRV_FOREACH(i, l) {
1142 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL;
1143
1144 p = startswith(*i, "RULE=");
1145 if (!p)
1146 continue;
1147
1148 r = routing_policy_rule_new(&rule);
1149 if (r < 0)
1150 return r;
1151
1152 for (;;) {
1153 _cleanup_free_ char *word = NULL, *a = NULL, *b = NULL;
1154
1155 r = extract_first_word(&p, &word, NULL, 0);
1156 if (r < 0)
1157 return r;
1158 if (r == 0)
1159 break;
1160
1161 r = split_pair(word, "=", &a, &b);
1162 if (r < 0)
1163 continue;
1164
1165 if (STR_IN_SET(a, "from", "to")) {
1166 union in_addr_union *buffer;
1167 uint8_t *prefixlen;
1168
1169 if (streq(a, "to")) {
1170 buffer = &rule->to;
1171 prefixlen = &rule->to_prefixlen;
1172 } else {
1173 buffer = &rule->from;
1174 prefixlen = &rule->from_prefixlen;
1175 }
1176
1177 r = in_addr_prefix_from_string_auto(b, &rule->family, buffer, prefixlen);
1178 if (r < 0) {
1179 log_error_errno(r, "RPDB rule prefix is invalid, ignoring assignment: %s", b);
1180 continue;
1181 }
1182
1183 } else if (streq(a, "tos")) {
1184 r = safe_atou8(b, &rule->tos);
1185 if (r < 0) {
1186 log_error_errno(r, "Failed to parse RPDB rule tos, ignoring: %s", b);
1187 continue;
1188 }
1189 } else if (streq(a, "table")) {
1190 r = safe_atou32(b, &rule->table);
1191 if (r < 0) {
1192 log_error_errno(r, "Failed to parse RPDB rule table, ignoring: %s", b);
1193 continue;
1194 }
1195 } else if (streq(a, "fwmark")) {
1196
1197 r = parse_fwmark_fwmask(b, &rule->fwmark, &rule->fwmask);
1198 if (r < 0) {
1199 log_error_errno(r, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a);
1200 continue;
1201 }
1202 } else if (streq(a, "iif")) {
1203
1204 if (free_and_strdup(&rule->iif, b) < 0)
1205 return log_oom();
1206
1207 } else if (streq(a, "oif")) {
1208
1209 if (free_and_strdup(&rule->oif, b) < 0)
1210 return log_oom();
1211 } else if (streq(a, "protocol")) {
1212 r = safe_atou8(b, &rule->protocol);
1213 if (r < 0) {
1214 log_error_errno(r, "Failed to parse RPDB rule protocol, ignoring: %s", b);
1215 continue;
1216 }
1217 } else if (streq(a, "sourceport")) {
1218
1219 r = parse_ip_port_range(b, &low, &high);
1220 if (r < 0) {
1221 log_error_errno(r, "Invalid routing policy rule source port range, ignoring assignment:'%s'", b);
1222 continue;
1223 }
1224
1225 rule->sport.start = low;
1226 rule->sport.end = high;
1227
1228 } else if (streq(a, "destinationport")) {
1229
1230 r = parse_ip_port_range(b, &low, &high);
1231 if (r < 0) {
1232 log_error_errno(r, "Invalid routing policy rule destination port range, ignoring assignment:'%s'", b);
1233 continue;
1234 }
1235
1236 rule->dport.start = low;
1237 rule->dport.end = high;
1238 }
1239 }
1240
1241 r = set_put(*rules, rule);
1242 if (r < 0) {
1243 log_warning_errno(r, "Failed to add RPDB rule to saved DB, ignoring: %s", p);
1244 continue;
1245 }
1246
1247 rule = NULL;
1248 }
1249
1250 return 0;
1251 }
1252
1253 void routing_policy_rule_purge(Manager *m, Link *link) {
1254 RoutingPolicyRule *rule, *existing;
1255 Iterator i;
1256 int r;
1257
1258 assert(m);
1259 assert(link);
1260
1261 SET_FOREACH(rule, m->rules_saved, i) {
1262 existing = set_get(m->rules_foreign, rule);
1263 if (existing) {
1264
1265 r = routing_policy_rule_remove(rule, link, NULL);
1266 if (r < 0) {
1267 log_warning_errno(r, "Could not remove routing policy rules: %m");
1268 continue;
1269 }
1270
1271 link->routing_policy_rule_remove_messages++;
1272 }
1273 }
1274 }