]>
Commit | Line | Data |
---|---|---|
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" | |
da96ad5a | 9 | #include "ip-protocol-list.h" |
bce67bbe SS |
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" | |
51517f9e | 16 | #include "strv.h" |
bce67bbe SS |
17 | |
18 | int routing_policy_rule_new(RoutingPolicyRule **ret) { | |
19 | RoutingPolicyRule *rule; | |
20 | ||
9d66b48c | 21 | rule = new(RoutingPolicyRule, 1); |
bce67bbe SS |
22 | if (!rule) |
23 | return -ENOMEM; | |
24 | ||
9d66b48c YW |
25 | *rule = (RoutingPolicyRule) { |
26 | .family = AF_INET, | |
27 | .table = RT_TABLE_MAIN, | |
28 | }; | |
bce67bbe SS |
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 | ||
0f7f2769 | 44 | if (rule->section) |
bce67bbe | 45 | hashmap_remove(rule->network->rules_by_section, rule->section); |
36e6e28b SS |
46 | } |
47 | ||
6964cf45 SS |
48 | if (rule->manager) { |
49 | set_remove(rule->manager->rules, rule); | |
50 | set_remove(rule->manager->rules_foreign, rule); | |
bce67bbe SS |
51 | } |
52 | ||
0f7f2769 | 53 | network_config_section_free(rule->section); |
762e2659 SS |
54 | free(rule->iif); |
55 | free(rule->oif); | |
bce67bbe SS |
56 | free(rule); |
57 | } | |
58 | ||
7a08d314 | 59 | static void routing_policy_rule_hash_func(const RoutingPolicyRule *rule, struct siphash *state) { |
bce67bbe SS |
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 | ||
926062f0 SS |
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 | ||
762e2659 | 82 | if (rule->iif) |
0d7febd0 | 83 | siphash24_compress(rule->iif, strlen(rule->iif), state); |
762e2659 SS |
84 | |
85 | if (rule->oif) | |
0d7febd0 | 86 | siphash24_compress(rule->oif, strlen(rule->oif), state); |
762e2659 | 87 | |
bce67bbe SS |
88 | break; |
89 | default: | |
90 | /* treat any other address family as AF_UNSPEC */ | |
91 | break; | |
92 | } | |
93 | } | |
94 | ||
7a08d314 | 95 | static int routing_policy_rule_compare_func(const RoutingPolicyRule *a, const RoutingPolicyRule *b) { |
bce67bbe SS |
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 | ||
926062f0 SS |
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 | ||
bce67bbe SS |
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 | ||
7a08d314 | 157 | DEFINE_PRIVATE_HASH_OPS(routing_policy_rule_hash_ops, RoutingPolicyRule, routing_policy_rule_hash_func, routing_policy_rule_compare_func); |
bce67bbe SS |
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, | |
eeab051b YW |
168 | const char *iif, |
169 | const char *oif, | |
926062f0 SS |
170 | uint8_t protocol, |
171 | struct fib_rule_port_range *sport, | |
172 | struct fib_rule_port_range *dport, | |
bce67bbe SS |
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, | |
eeab051b | 188 | .iif = (char*) iif, |
926062f0 SS |
189 | .oif = (char*) oif, |
190 | .protocol = protocol, | |
191 | .sport = *sport, | |
192 | .dport = *dport, | |
bce67bbe SS |
193 | }; |
194 | ||
b87dadcd ZJS |
195 | existing = set_get(m->rules, &rule); |
196 | if (existing) { | |
197 | if (ret) | |
198 | *ret = existing; | |
199 | return 1; | |
bce67bbe SS |
200 | } |
201 | ||
b87dadcd ZJS |
202 | existing = set_get(m->rules_foreign, &rule); |
203 | if (existing) { | |
204 | if (ret) | |
205 | *ret = existing; | |
e6b65ab7 | 206 | return 0; |
bce67bbe SS |
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 | ||
36e6e28b SS |
230 | static int routing_policy_rule_add_internal(Manager *m, |
231 | Set **rules, | |
bce67bbe SS |
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, | |
eeab051b YW |
240 | const char *_iif, |
241 | const char *_oif, | |
926062f0 SS |
242 | uint8_t protocol, |
243 | const struct fib_rule_port_range *sport, | |
244 | const struct fib_rule_port_range *dport, | |
bce67bbe SS |
245 | RoutingPolicyRule **ret) { |
246 | ||
8e766630 | 247 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL; |
eeab051b | 248 | _cleanup_free_ char *iif = NULL, *oif = NULL; |
bce67bbe SS |
249 | int r; |
250 | ||
251 | assert_return(rules, -EINVAL); | |
252 | ||
eeab051b YW |
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 | ||
bce67bbe SS |
265 | r = routing_policy_rule_new(&rule); |
266 | if (r < 0) | |
267 | return r; | |
268 | ||
6964cf45 | 269 | rule->manager = m; |
bce67bbe SS |
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; | |
a6276f0f YW |
278 | rule->iif = iif; |
279 | rule->oif = oif; | |
926062f0 SS |
280 | rule->protocol = protocol; |
281 | rule->sport = *sport; | |
282 | rule->dport = *dport; | |
bce67bbe SS |
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; | |
a6276f0f | 296 | iif = oif = NULL; |
bce67bbe SS |
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, | |
eeab051b YW |
310 | const char *iif, |
311 | const char *oif, | |
926062f0 SS |
312 | uint8_t protocol, |
313 | const struct fib_rule_port_range *sport, | |
314 | const struct fib_rule_port_range *dport, | |
bce67bbe SS |
315 | RoutingPolicyRule **ret) { |
316 | ||
926062f0 | 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); |
bce67bbe SS |
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, | |
eeab051b YW |
329 | const char *iif, |
330 | const char *oif, | |
926062f0 SS |
331 | uint8_t protocol, |
332 | const struct fib_rule_port_range *sport, | |
333 | const struct fib_rule_port_range *dport, | |
bce67bbe | 334 | RoutingPolicyRule **ret) { |
926062f0 | 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); |
bce67bbe SS |
336 | } |
337 | ||
302a796f | 338 | static int routing_policy_rule_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
bce67bbe SS |
339 | int r; |
340 | ||
341 | assert(m); | |
342 | assert(link); | |
343 | assert(link->ifname); | |
344 | ||
7715629e | 345 | link->routing_policy_rule_remove_messages--; |
bce67bbe SS |
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 | ||
302a796f | 357 | int routing_policy_rule_remove(RoutingPolicyRule *routing_policy_rule, Link *link, link_netlink_message_handler_t callback) { |
bce67bbe SS |
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 | ||
302a796f YW |
400 | r = netlink_call_async(link->manager->rtnl, NULL, m, |
401 | callback ?: routing_policy_rule_remove_handler, | |
402 | link_netlink_destroy_callback, link); | |
bce67bbe SS |
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) { | |
8e766630 LP |
412 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL; |
413 | _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL; | |
bce67bbe SS |
414 | int r; |
415 | ||
416 | assert(network); | |
417 | assert(ret); | |
418 | assert(!!filename == (section_line > 0)); | |
419 | ||
0f7f2769 YW |
420 | if (filename) { |
421 | r = network_config_section_new(filename, section_line, &n); | |
422 | if (r < 0) | |
423 | return r; | |
bce67bbe | 424 | |
0f7f2769 YW |
425 | rule = hashmap_get(network->rules_by_section, n); |
426 | if (rule) { | |
427 | *ret = TAKE_PTR(rule); | |
bce67bbe | 428 | |
0f7f2769 YW |
429 | return 0; |
430 | } | |
bce67bbe SS |
431 | } |
432 | ||
433 | r = routing_policy_rule_new(&rule); | |
434 | if (r < 0) | |
435 | return r; | |
436 | ||
bce67bbe | 437 | rule->network = network; |
bce67bbe SS |
438 | LIST_APPEND(rules, network->rules, rule); |
439 | network->n_rules++; | |
440 | ||
0f7f2769 YW |
441 | if (filename) { |
442 | rule->section = TAKE_PTR(n); | |
443 | ||
3e570042 YW |
444 | r = hashmap_ensure_allocated(&network->rules_by_section, &network_config_hash_ops); |
445 | if (r < 0) | |
446 | return r; | |
447 | ||
0f7f2769 YW |
448 | r = hashmap_put(network->rules_by_section, rule->section, rule); |
449 | if (r < 0) | |
450 | return r; | |
451 | } | |
452 | ||
1cc6c93a | 453 | *ret = TAKE_PTR(rule); |
bce67bbe SS |
454 | |
455 | return 0; | |
456 | } | |
457 | ||
302a796f | 458 | static int routing_policy_rule_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
bce67bbe SS |
459 | int r; |
460 | ||
461 | assert(rtnl); | |
462 | assert(m); | |
463 | assert(link); | |
464 | assert(link->ifname); | |
7715629e | 465 | assert(link->routing_policy_rule_messages > 0); |
bce67bbe | 466 | |
7715629e | 467 | link->routing_policy_rule_messages--; |
bce67bbe SS |
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 | ||
7715629e | 476 | if (link->routing_policy_rule_messages == 0) { |
bce67bbe | 477 | log_link_debug(link, "Routing policy rule configured"); |
7715629e ST |
478 | link->routing_policy_rules_configured = true; |
479 | link_check_ready(link); | |
480 | } | |
bce67bbe SS |
481 | |
482 | return 1; | |
483 | } | |
484 | ||
302a796f | 485 | int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, link_netlink_message_handler_t callback, bool update) { |
bce67bbe SS |
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 | ||
762e2659 SS |
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 | ||
926062f0 SS |
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 | ||
8b220643 SS |
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 | ||
bce67bbe SS |
597 | rule->link = link; |
598 | ||
302a796f YW |
599 | r = netlink_call_async(link->manager->rtnl, NULL, m, |
600 | callback ?: routing_policy_rule_handler, | |
601 | link_netlink_destroy_callback, link); | |
bce67bbe SS |
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, | |
926062f0 | 608 | rule->to_prefixlen, rule->tos, rule->fwmark, rule->table, rule->iif, rule->oif, rule->protocol, &rule->sport, &rule->dport, NULL); |
bce67bbe | 609 | if (r < 0) |
cb700a11 | 610 | return log_error_errno(r, "Could not add rule: %m"); |
bce67bbe SS |
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 | ||
8e766630 | 655 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
bce67bbe SS |
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 | ||
8e766630 | 692 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
bce67bbe SS |
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 | ||
8e766630 | 729 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
bce67bbe SS |
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 | ||
8e766630 | 766 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
bce67bbe SS |
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 | ||
8e766630 | 803 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
bce67bbe | 804 | Network *network = userdata; |
7934dede YW |
805 | union in_addr_union *buffer; |
806 | uint8_t *prefixlen; | |
bce67bbe SS |
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 | ||
bce67bbe | 819 | if (streq(lvalue, "To")) { |
7934dede YW |
820 | buffer = &n->to; |
821 | prefixlen = &n->to_prefixlen; | |
bce67bbe | 822 | } else { |
7934dede YW |
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; | |
bce67bbe SS |
831 | } |
832 | ||
833 | n = NULL; | |
834 | ||
835 | return 0; | |
836 | } | |
837 | ||
762e2659 SS |
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 | ||
8e766630 | 850 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
762e2659 SS |
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 | ||
926062f0 SS |
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 | ||
97f9df9e | 929 | int config_parse_routing_policy_rule_ip_protocol( |
926062f0 SS |
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 | ||
3a269dcf | 955 | r = parse_ip_protocol(rvalue); |
926062f0 | 956 | if (r < 0) { |
3a269dcf | 957 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IP protocol '%s' for routing policy rule, ignoring: %m", rvalue); |
926062f0 SS |
958 | return 0; |
959 | } | |
960 | ||
961 | n->protocol = r; | |
962 | ||
963 | n = NULL; | |
964 | ||
965 | return 0; | |
966 | } | |
967 | ||
8b220643 SS |
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 | ||
458d8ae3 | 1007 | static int routing_policy_rule_read_full_file(const char *state_file, char **ret) { |
ac097c84 | 1008 | _cleanup_free_ char *s = NULL; |
bce67bbe SS |
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 | ||
ae2a15bc | 1022 | *ret = TAKE_PTR(s); |
bce67bbe SS |
1023 | |
1024 | return size; | |
1025 | } | |
1026 | ||
458d8ae3 ZJS |
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 | ||
9491f55f ZJS |
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 | ||
926062f0 SS |
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) { | |
58a02e4c | 1097 | fprintf(f, "%ssourcesport=%"PRIu16"-%"PRIu16, |
926062f0 SS |
1098 | space ? " " : "", |
1099 | rule->sport.start, rule->sport.end); | |
1100 | space = true; | |
1101 | } | |
1102 | ||
1103 | if (rule->dport.start != 0 || rule->dport.end != 0) { | |
58a02e4c | 1104 | fprintf(f, "%sdestinationport=%"PRIu16"-%"PRIu16, |
926062f0 SS |
1105 | space ? " " : "", |
1106 | rule->dport.start, rule->dport.end); | |
1107 | space = true; | |
1108 | } | |
1109 | ||
458d8ae3 ZJS |
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) { | |
bce67bbe SS |
1119 | _cleanup_strv_free_ char **l = NULL; |
1120 | _cleanup_free_ char *data = NULL; | |
926062f0 | 1121 | uint16_t low = 0, high = 0; |
bce67bbe SS |
1122 | const char *p; |
1123 | char **i; | |
1124 | int r; | |
1125 | ||
458d8ae3 ZJS |
1126 | assert(state_file); |
1127 | assert(rules); | |
bce67bbe | 1128 | |
458d8ae3 | 1129 | r = routing_policy_rule_read_full_file(state_file, &data); |
bce67bbe SS |
1130 | if (r <= 0) |
1131 | return r; | |
1132 | ||
1133 | l = strv_split_newlines(data); | |
1134 | if (!l) | |
1135 | return -ENOMEM; | |
1136 | ||
458d8ae3 | 1137 | r = set_ensure_allocated(rules, &routing_policy_rule_hash_ops); |
bce67bbe SS |
1138 | if (r < 0) |
1139 | return r; | |
1140 | ||
1141 | STRV_FOREACH(i, l) { | |
8e766630 | 1142 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL; |
bce67bbe SS |
1143 | |
1144 | p = startswith(*i, "RULE="); | |
1145 | if (!p) | |
1146 | continue; | |
1147 | ||
bce67bbe SS |
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; | |
bce67bbe SS |
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")) { | |
7934dede YW |
1166 | union in_addr_union *buffer; |
1167 | uint8_t *prefixlen; | |
bce67bbe SS |
1168 | |
1169 | if (streq(a, "to")) { | |
7934dede YW |
1170 | buffer = &rule->to; |
1171 | prefixlen = &rule->to_prefixlen; | |
bce67bbe | 1172 | } else { |
7934dede YW |
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; | |
bce67bbe | 1181 | } |
7934dede | 1182 | |
bce67bbe SS |
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 | ||
e4aca57d | 1197 | r = parse_fwmark_fwmask(b, &rule->fwmark, &rule->fwmask); |
bce67bbe SS |
1198 | if (r < 0) { |
1199 | log_error_errno(r, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a); | |
1200 | continue; | |
1201 | } | |
9491f55f | 1202 | } else if (streq(a, "iif")) { |
762e2659 | 1203 | |
93f9da6e | 1204 | if (free_and_strdup(&rule->iif, b) < 0) |
762e2659 | 1205 | return log_oom(); |
93f9da6e | 1206 | |
9491f55f | 1207 | } else if (streq(a, "oif")) { |
762e2659 | 1208 | |
93f9da6e | 1209 | if (free_and_strdup(&rule->oif, b) < 0) |
762e2659 | 1210 | return log_oom(); |
926062f0 SS |
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; | |
bce67bbe SS |
1238 | } |
1239 | } | |
1240 | ||
458d8ae3 | 1241 | r = set_put(*rules, rule); |
bce67bbe SS |
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 | ||
29889b4d | 1265 | r = routing_policy_rule_remove(rule, link, NULL); |
bce67bbe SS |
1266 | if (r < 0) { |
1267 | log_warning_errno(r, "Could not remove routing policy rules: %m"); | |
1268 | continue; | |
1269 | } | |
1270 | ||
7715629e | 1271 | link->routing_policy_rule_remove_messages++; |
bce67bbe SS |
1272 | } |
1273 | } | |
1274 | } |