]>
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 | ||
d40b01e4 | 372 | if (in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->from) == 0) { |
43409486 | 373 | r = netlink_message_append_in_addr_union(m, FRA_SRC, routing_policy_rule->family, &routing_policy_rule->from); |
bce67bbe SS |
374 | if (r < 0) |
375 | return log_error_errno(r, "Could not append FRA_SRC attribute: %m"); | |
376 | ||
377 | r = sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m, routing_policy_rule->from_prefixlen); | |
378 | if (r < 0) | |
379 | return log_error_errno(r, "Could not set source prefix length: %m"); | |
380 | } | |
381 | ||
d40b01e4 | 382 | if (in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->to) == 0) { |
43409486 | 383 | r = netlink_message_append_in_addr_union(m, FRA_DST, routing_policy_rule->family, &routing_policy_rule->to); |
bce67bbe SS |
384 | if (r < 0) |
385 | return log_error_errno(r, "Could not append FRA_DST attribute: %m"); | |
386 | ||
387 | r = sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m, routing_policy_rule->to_prefixlen); | |
388 | if (r < 0) | |
389 | return log_error_errno(r, "Could not set destination prefix length: %m"); | |
390 | } | |
391 | ||
302a796f YW |
392 | r = netlink_call_async(link->manager->rtnl, NULL, m, |
393 | callback ?: routing_policy_rule_remove_handler, | |
394 | link_netlink_destroy_callback, link); | |
bce67bbe SS |
395 | if (r < 0) |
396 | return log_error_errno(r, "Could not send rtnetlink message: %m"); | |
397 | ||
398 | link_ref(link); | |
399 | ||
400 | return 0; | |
401 | } | |
402 | ||
403 | static int routing_policy_rule_new_static(Network *network, const char *filename, unsigned section_line, RoutingPolicyRule **ret) { | |
8e766630 LP |
404 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL; |
405 | _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL; | |
bce67bbe SS |
406 | int r; |
407 | ||
408 | assert(network); | |
409 | assert(ret); | |
410 | assert(!!filename == (section_line > 0)); | |
411 | ||
0f7f2769 YW |
412 | if (filename) { |
413 | r = network_config_section_new(filename, section_line, &n); | |
414 | if (r < 0) | |
415 | return r; | |
bce67bbe | 416 | |
0f7f2769 YW |
417 | rule = hashmap_get(network->rules_by_section, n); |
418 | if (rule) { | |
419 | *ret = TAKE_PTR(rule); | |
bce67bbe | 420 | |
0f7f2769 YW |
421 | return 0; |
422 | } | |
bce67bbe SS |
423 | } |
424 | ||
425 | r = routing_policy_rule_new(&rule); | |
426 | if (r < 0) | |
427 | return r; | |
428 | ||
bce67bbe | 429 | rule->network = network; |
bce67bbe SS |
430 | LIST_APPEND(rules, network->rules, rule); |
431 | network->n_rules++; | |
432 | ||
0f7f2769 YW |
433 | if (filename) { |
434 | rule->section = TAKE_PTR(n); | |
435 | ||
3e570042 YW |
436 | r = hashmap_ensure_allocated(&network->rules_by_section, &network_config_hash_ops); |
437 | if (r < 0) | |
438 | return r; | |
439 | ||
0f7f2769 YW |
440 | r = hashmap_put(network->rules_by_section, rule->section, rule); |
441 | if (r < 0) | |
442 | return r; | |
443 | } | |
444 | ||
1cc6c93a | 445 | *ret = TAKE_PTR(rule); |
bce67bbe SS |
446 | |
447 | return 0; | |
448 | } | |
449 | ||
302a796f | 450 | static int routing_policy_rule_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
bce67bbe SS |
451 | int r; |
452 | ||
453 | assert(rtnl); | |
454 | assert(m); | |
455 | assert(link); | |
456 | assert(link->ifname); | |
7715629e | 457 | assert(link->routing_policy_rule_messages > 0); |
bce67bbe | 458 | |
7715629e | 459 | link->routing_policy_rule_messages--; |
bce67bbe SS |
460 | |
461 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
462 | return 1; | |
463 | ||
464 | r = sd_netlink_message_get_errno(m); | |
465 | if (r < 0 && r != -EEXIST) | |
466 | log_link_warning_errno(link, r, "Could not add routing policy rule: %m"); | |
467 | ||
7715629e | 468 | if (link->routing_policy_rule_messages == 0) { |
bce67bbe | 469 | log_link_debug(link, "Routing policy rule configured"); |
7715629e ST |
470 | link->routing_policy_rules_configured = true; |
471 | link_check_ready(link); | |
472 | } | |
bce67bbe SS |
473 | |
474 | return 1; | |
475 | } | |
476 | ||
302a796f | 477 | int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, link_netlink_message_handler_t callback, bool update) { |
bce67bbe SS |
478 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
479 | int r; | |
480 | ||
481 | assert(rule); | |
482 | assert(link); | |
483 | assert(link->ifindex > 0); | |
484 | assert(link->manager); | |
485 | assert(link->manager->rtnl); | |
486 | ||
487 | r = sd_rtnl_message_new_routing_policy_rule(link->manager->rtnl, &m, RTM_NEWRULE, rule->family); | |
488 | if (r < 0) | |
489 | return log_error_errno(r, "Could not allocate RTM_NEWRULE message: %m"); | |
490 | ||
d40b01e4 | 491 | if (in_addr_is_null(rule->family, &rule->from) == 0) { |
43409486 | 492 | r = netlink_message_append_in_addr_union(m, FRA_SRC, rule->family, &rule->from); |
bce67bbe SS |
493 | if (r < 0) |
494 | return log_error_errno(r, "Could not append FRA_SRC attribute: %m"); | |
495 | ||
496 | r = sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(m, rule->from_prefixlen); | |
497 | if (r < 0) | |
498 | return log_error_errno(r, "Could not set source prefix length: %m"); | |
499 | } | |
500 | ||
d40b01e4 | 501 | if (in_addr_is_null(rule->family, &rule->to) == 0) { |
43409486 | 502 | r = netlink_message_append_in_addr_union(m, FRA_DST, rule->family, &rule->to); |
bce67bbe SS |
503 | if (r < 0) |
504 | return log_error_errno(r, "Could not append FRA_DST attribute: %m"); | |
505 | ||
506 | r = sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(m, rule->to_prefixlen); | |
507 | if (r < 0) | |
508 | return log_error_errno(r, "Could not set destination prefix length: %m"); | |
509 | } | |
510 | ||
511 | r = sd_netlink_message_append_u32(m, FRA_PRIORITY, rule->priority); | |
512 | if (r < 0) | |
513 | return log_error_errno(r, "Could not append FRA_PRIORITY attribute: %m"); | |
514 | ||
515 | if (rule->tos > 0) { | |
516 | r = sd_rtnl_message_routing_policy_rule_set_tos(m, rule->tos); | |
517 | if (r < 0) | |
518 | return log_error_errno(r, "Could not set ip rule tos: %m"); | |
519 | } | |
520 | ||
521 | if (rule->table < 256) { | |
522 | r = sd_rtnl_message_routing_policy_rule_set_table(m, rule->table); | |
523 | if (r < 0) | |
524 | return log_error_errno(r, "Could not set ip rule table: %m"); | |
525 | } else { | |
526 | r = sd_rtnl_message_routing_policy_rule_set_table(m, RT_TABLE_UNSPEC); | |
527 | if (r < 0) | |
528 | return log_error_errno(r, "Could not set ip rule table: %m"); | |
529 | ||
530 | r = sd_netlink_message_append_u32(m, FRA_TABLE, rule->table); | |
531 | if (r < 0) | |
532 | return log_error_errno(r, "Could not append FRA_TABLE attribute: %m"); | |
533 | } | |
534 | ||
535 | if (rule->fwmark > 0) { | |
536 | r = sd_netlink_message_append_u32(m, FRA_FWMARK, rule->fwmark); | |
537 | if (r < 0) | |
538 | return log_error_errno(r, "Could not append FRA_FWMARK attribute: %m"); | |
539 | } | |
540 | ||
541 | if (rule->fwmask > 0) { | |
542 | r = sd_netlink_message_append_u32(m, FRA_FWMASK, rule->fwmask); | |
543 | if (r < 0) | |
544 | return log_error_errno(r, "Could not append FRA_FWMASK attribute: %m"); | |
545 | } | |
546 | ||
762e2659 SS |
547 | if (rule->iif) { |
548 | r = sd_netlink_message_append_string(m, FRA_IFNAME, rule->iif); | |
549 | if (r < 0) | |
550 | return log_error_errno(r, "Could not append FRA_IFNAME attribute: %m"); | |
551 | } | |
552 | ||
553 | if (rule->oif) { | |
554 | r = sd_netlink_message_append_string(m, FRA_OIFNAME, rule->oif); | |
555 | if (r < 0) | |
556 | return log_error_errno(r, "Could not append FRA_OIFNAME attribute: %m"); | |
557 | } | |
558 | ||
926062f0 SS |
559 | r = sd_netlink_message_append_u8(m, FRA_IP_PROTO, rule->protocol); |
560 | if (r < 0) | |
561 | return log_error_errno(r, "Could not append FRA_IP_PROTO attribute: %m"); | |
562 | ||
563 | if (rule->sport.start != 0 || rule->sport.end != 0) { | |
564 | r = sd_netlink_message_append_data(m, FRA_SPORT_RANGE, &rule->sport, sizeof(rule->sport)); | |
565 | if (r < 0) | |
566 | return log_error_errno(r, "Could not append FRA_SPORT_RANGE attribute: %m"); | |
567 | } | |
568 | ||
569 | if (rule->dport.start != 0 || rule->dport.end != 0) { | |
570 | r = sd_netlink_message_append_data(m, FRA_DPORT_RANGE, &rule->dport, sizeof(rule->dport)); | |
571 | if (r < 0) | |
572 | return log_error_errno(r, "Could not append FRA_DPORT_RANGE attribute: %m"); | |
573 | } | |
574 | ||
8b220643 SS |
575 | if (rule->invert_rule) { |
576 | r = sd_rtnl_message_routing_policy_rule_set_flags(m, FIB_RULE_INVERT); | |
577 | if (r < 0) | |
578 | return log_error_errno(r, "Could not append FIB_RULE_INVERT attribute: %m"); | |
579 | } | |
580 | ||
bce67bbe SS |
581 | rule->link = link; |
582 | ||
302a796f YW |
583 | r = netlink_call_async(link->manager->rtnl, NULL, m, |
584 | callback ?: routing_policy_rule_handler, | |
585 | link_netlink_destroy_callback, link); | |
bce67bbe SS |
586 | if (r < 0) |
587 | return log_error_errno(r, "Could not send rtnetlink message: %m"); | |
588 | ||
589 | link_ref(link); | |
590 | ||
591 | r = routing_policy_rule_add(link->manager, rule->family, &rule->from, rule->from_prefixlen, &rule->to, | |
926062f0 | 592 | rule->to_prefixlen, rule->tos, rule->fwmark, rule->table, rule->iif, rule->oif, rule->protocol, &rule->sport, &rule->dport, NULL); |
bce67bbe | 593 | if (r < 0) |
cb700a11 | 594 | return log_error_errno(r, "Could not add rule: %m"); |
bce67bbe SS |
595 | |
596 | return 0; | |
597 | } | |
598 | ||
599 | static int parse_fwmark_fwmask(const char *s, uint32_t *fwmark, uint32_t *fwmask) { | |
600 | _cleanup_free_ char *f = NULL; | |
601 | char *p; | |
602 | int r; | |
603 | ||
604 | assert(s); | |
605 | ||
606 | f = strdup(s); | |
607 | if (!f) | |
608 | return -ENOMEM; | |
609 | ||
610 | p = strchr(f, '/'); | |
611 | if (p) | |
612 | *p++ = '\0'; | |
613 | ||
614 | r = safe_atou32(f, fwmark); | |
615 | if (r < 0) | |
616 | return log_error_errno(r, "Failed to parse RPDB rule firewall mark, ignoring: %s", f); | |
617 | ||
618 | if (p) { | |
619 | r = safe_atou32(p, fwmask); | |
620 | if (r < 0) | |
621 | return log_error_errno(r, "Failed to parse RPDB rule mask, ignoring: %s", f); | |
622 | } | |
623 | ||
624 | return 0; | |
625 | } | |
626 | ||
627 | int config_parse_routing_policy_rule_tos( | |
628 | const char *unit, | |
629 | const char *filename, | |
630 | unsigned line, | |
631 | const char *section, | |
632 | unsigned section_line, | |
633 | const char *lvalue, | |
634 | int ltype, | |
635 | const char *rvalue, | |
636 | void *data, | |
637 | void *userdata) { | |
638 | ||
8e766630 | 639 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
bce67bbe SS |
640 | Network *network = userdata; |
641 | int r; | |
642 | ||
643 | assert(filename); | |
644 | assert(section); | |
645 | assert(lvalue); | |
646 | assert(rvalue); | |
647 | assert(data); | |
648 | ||
649 | r = routing_policy_rule_new_static(network, filename, section_line, &n); | |
650 | if (r < 0) | |
651 | return r; | |
652 | ||
653 | r = safe_atou8(rvalue, &n->tos); | |
654 | if (r < 0) { | |
655 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule tos, ignoring: %s", rvalue); | |
656 | return 0; | |
657 | } | |
658 | ||
659 | n = NULL; | |
660 | ||
661 | return 0; | |
662 | } | |
663 | ||
664 | int config_parse_routing_policy_rule_priority( | |
665 | const char *unit, | |
666 | const char *filename, | |
667 | unsigned line, | |
668 | const char *section, | |
669 | unsigned section_line, | |
670 | const char *lvalue, | |
671 | int ltype, | |
672 | const char *rvalue, | |
673 | void *data, | |
674 | void *userdata) { | |
675 | ||
8e766630 | 676 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
bce67bbe SS |
677 | Network *network = userdata; |
678 | int r; | |
679 | ||
680 | assert(filename); | |
681 | assert(section); | |
682 | assert(lvalue); | |
683 | assert(rvalue); | |
684 | assert(data); | |
685 | ||
686 | r = routing_policy_rule_new_static(network, filename, section_line, &n); | |
687 | if (r < 0) | |
688 | return r; | |
689 | ||
690 | r = safe_atou32(rvalue, &n->priority); | |
691 | if (r < 0) { | |
692 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule priority, ignoring: %s", rvalue); | |
693 | return 0; | |
694 | } | |
695 | ||
696 | n = NULL; | |
697 | ||
698 | return 0; | |
699 | } | |
700 | ||
701 | int config_parse_routing_policy_rule_table( | |
702 | const char *unit, | |
703 | const char *filename, | |
704 | unsigned line, | |
705 | const char *section, | |
706 | unsigned section_line, | |
707 | const char *lvalue, | |
708 | int ltype, | |
709 | const char *rvalue, | |
710 | void *data, | |
711 | void *userdata) { | |
712 | ||
8e766630 | 713 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
bce67bbe SS |
714 | Network *network = userdata; |
715 | int r; | |
716 | ||
717 | assert(filename); | |
718 | assert(section); | |
719 | assert(lvalue); | |
720 | assert(rvalue); | |
721 | assert(data); | |
722 | ||
723 | r = routing_policy_rule_new_static(network, filename, section_line, &n); | |
724 | if (r < 0) | |
725 | return r; | |
726 | ||
727 | r = safe_atou32(rvalue, &n->table); | |
728 | if (r < 0) { | |
729 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule table, ignoring: %s", rvalue); | |
730 | return 0; | |
731 | } | |
732 | ||
733 | n = NULL; | |
734 | ||
735 | return 0; | |
736 | } | |
737 | ||
738 | int config_parse_routing_policy_rule_fwmark_mask( | |
739 | const char *unit, | |
740 | const char *filename, | |
741 | unsigned line, | |
742 | const char *section, | |
743 | unsigned section_line, | |
744 | const char *lvalue, | |
745 | int ltype, | |
746 | const char *rvalue, | |
747 | void *data, | |
748 | void *userdata) { | |
749 | ||
8e766630 | 750 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
bce67bbe SS |
751 | Network *network = userdata; |
752 | int r; | |
753 | ||
754 | assert(filename); | |
755 | assert(section); | |
756 | assert(lvalue); | |
757 | assert(rvalue); | |
758 | assert(data); | |
759 | ||
760 | r = routing_policy_rule_new_static(network, filename, section_line, &n); | |
761 | if (r < 0) | |
762 | return r; | |
763 | ||
764 | r = parse_fwmark_fwmask(rvalue, &n->fwmark, &n->fwmask); | |
765 | if (r < 0) { | |
766 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue); | |
767 | return 0; | |
768 | } | |
769 | ||
770 | n = NULL; | |
771 | ||
772 | return 0; | |
773 | } | |
774 | ||
775 | int config_parse_routing_policy_rule_prefix( | |
776 | const char *unit, | |
777 | const char *filename, | |
778 | unsigned line, | |
779 | const char *section, | |
780 | unsigned section_line, | |
781 | const char *lvalue, | |
782 | int ltype, | |
783 | const char *rvalue, | |
784 | void *data, | |
785 | void *userdata) { | |
786 | ||
8e766630 | 787 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
bce67bbe | 788 | Network *network = userdata; |
7934dede YW |
789 | union in_addr_union *buffer; |
790 | uint8_t *prefixlen; | |
bce67bbe SS |
791 | int r; |
792 | ||
793 | assert(filename); | |
794 | assert(section); | |
795 | assert(lvalue); | |
796 | assert(rvalue); | |
797 | assert(data); | |
798 | ||
799 | r = routing_policy_rule_new_static(network, filename, section_line, &n); | |
800 | if (r < 0) | |
801 | return r; | |
802 | ||
bce67bbe | 803 | if (streq(lvalue, "To")) { |
7934dede YW |
804 | buffer = &n->to; |
805 | prefixlen = &n->to_prefixlen; | |
bce67bbe | 806 | } else { |
7934dede YW |
807 | buffer = &n->from; |
808 | prefixlen = &n->from_prefixlen; | |
809 | } | |
810 | ||
811 | r = in_addr_prefix_from_string_auto(rvalue, &n->family, buffer, prefixlen); | |
812 | if (r < 0) { | |
813 | log_syntax(unit, LOG_ERR, filename, line, r, "RPDB rule prefix is invalid, ignoring assignment: %s", rvalue); | |
814 | return 0; | |
bce67bbe SS |
815 | } |
816 | ||
817 | n = NULL; | |
818 | ||
819 | return 0; | |
820 | } | |
821 | ||
762e2659 SS |
822 | int config_parse_routing_policy_rule_device( |
823 | const char *unit, | |
824 | const char *filename, | |
825 | unsigned line, | |
826 | const char *section, | |
827 | unsigned section_line, | |
828 | const char *lvalue, | |
829 | int ltype, | |
830 | const char *rvalue, | |
831 | void *data, | |
832 | void *userdata) { | |
833 | ||
8e766630 | 834 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; |
762e2659 SS |
835 | Network *network = userdata; |
836 | int r; | |
837 | ||
838 | assert(filename); | |
839 | assert(section); | |
840 | assert(lvalue); | |
841 | assert(rvalue); | |
842 | assert(data); | |
843 | ||
844 | r = routing_policy_rule_new_static(network, filename, section_line, &n); | |
845 | if (r < 0) | |
846 | return r; | |
847 | ||
848 | if (!ifname_valid(rvalue)) { | |
849 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse '%s' interface name, ignoring: %s", lvalue, rvalue); | |
850 | return 0; | |
851 | } | |
852 | ||
853 | if (streq(lvalue, "IncomingInterface")) { | |
854 | r = free_and_strdup(&n->iif, rvalue); | |
855 | if (r < 0) | |
856 | return log_oom(); | |
857 | } else { | |
858 | r = free_and_strdup(&n->oif, rvalue); | |
859 | if (r < 0) | |
860 | return log_oom(); | |
861 | } | |
862 | ||
863 | n = NULL; | |
864 | ||
865 | return 0; | |
866 | } | |
867 | ||
926062f0 SS |
868 | int config_parse_routing_policy_rule_port_range( |
869 | const char *unit, | |
870 | const char *filename, | |
871 | unsigned line, | |
872 | const char *section, | |
873 | unsigned section_line, | |
874 | const char *lvalue, | |
875 | int ltype, | |
876 | const char *rvalue, | |
877 | void *data, | |
878 | void *userdata) { | |
879 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; | |
880 | Network *network = userdata; | |
881 | uint16_t low, high; | |
882 | int r; | |
883 | ||
884 | assert(filename); | |
885 | assert(section); | |
886 | assert(lvalue); | |
887 | assert(rvalue); | |
888 | assert(data); | |
889 | ||
890 | r = routing_policy_rule_new_static(network, filename, section_line, &n); | |
891 | if (r < 0) | |
892 | return r; | |
893 | ||
894 | r = parse_ip_port_range(rvalue, &low, &high); | |
895 | if (r < 0) { | |
896 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse routing policy rule port range '%s'", rvalue); | |
897 | return 0; | |
898 | } | |
899 | ||
900 | if (streq(lvalue, "SourcePort")) { | |
901 | n->sport.start = low; | |
902 | n->sport.end = high; | |
903 | } else { | |
904 | n->dport.start = low; | |
905 | n->dport.end = high; | |
906 | } | |
907 | ||
908 | n = NULL; | |
909 | ||
910 | return 0; | |
911 | } | |
912 | ||
97f9df9e | 913 | int config_parse_routing_policy_rule_ip_protocol( |
926062f0 SS |
914 | const char *unit, |
915 | const char *filename, | |
916 | unsigned line, | |
917 | const char *section, | |
918 | unsigned section_line, | |
919 | const char *lvalue, | |
920 | int ltype, | |
921 | const char *rvalue, | |
922 | void *data, | |
923 | void *userdata) { | |
924 | ||
925 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; | |
926 | Network *network = userdata; | |
927 | int r; | |
928 | ||
929 | assert(filename); | |
930 | assert(section); | |
931 | assert(lvalue); | |
932 | assert(rvalue); | |
933 | assert(data); | |
934 | ||
935 | r = routing_policy_rule_new_static(network, filename, section_line, &n); | |
936 | if (r < 0) | |
937 | return r; | |
938 | ||
3a269dcf | 939 | r = parse_ip_protocol(rvalue); |
926062f0 | 940 | if (r < 0) { |
3a269dcf | 941 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IP protocol '%s' for routing policy rule, ignoring: %m", rvalue); |
926062f0 SS |
942 | return 0; |
943 | } | |
944 | ||
945 | n->protocol = r; | |
946 | ||
947 | n = NULL; | |
948 | ||
949 | return 0; | |
950 | } | |
951 | ||
8b220643 SS |
952 | int config_parse_routing_policy_rule_invert( |
953 | const char *unit, | |
954 | const char *filename, | |
955 | unsigned line, | |
956 | const char *section, | |
957 | unsigned section_line, | |
958 | const char *lvalue, | |
959 | int ltype, | |
960 | const char *rvalue, | |
961 | void *data, | |
962 | void *userdata) { | |
963 | ||
964 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *n = NULL; | |
965 | Network *network = userdata; | |
966 | int r; | |
967 | ||
968 | assert(filename); | |
969 | assert(section); | |
970 | assert(lvalue); | |
971 | assert(rvalue); | |
972 | assert(data); | |
973 | ||
974 | r = routing_policy_rule_new_static(network, filename, section_line, &n); | |
975 | if (r < 0) | |
976 | return r; | |
977 | ||
978 | r = parse_boolean(rvalue); | |
979 | if (r < 0) { | |
980 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse RPDB rule invert, ignoring: %s", rvalue); | |
981 | return 0; | |
982 | } | |
983 | ||
984 | n->invert_rule = r; | |
985 | ||
986 | n = NULL; | |
987 | ||
988 | return 0; | |
989 | } | |
990 | ||
458d8ae3 | 991 | static int routing_policy_rule_read_full_file(const char *state_file, char **ret) { |
ac097c84 | 992 | _cleanup_free_ char *s = NULL; |
bce67bbe SS |
993 | size_t size; |
994 | int r; | |
995 | ||
996 | assert(state_file); | |
997 | ||
998 | r = read_full_file(state_file, &s, &size); | |
999 | if (r == -ENOENT) | |
1000 | return -ENODATA; | |
1001 | if (r < 0) | |
1002 | return r; | |
1003 | if (size <= 0) | |
1004 | return -ENODATA; | |
1005 | ||
ae2a15bc | 1006 | *ret = TAKE_PTR(s); |
bce67bbe SS |
1007 | |
1008 | return size; | |
1009 | } | |
1010 | ||
458d8ae3 ZJS |
1011 | int routing_policy_serialize_rules(Set *rules, FILE *f) { |
1012 | RoutingPolicyRule *rule = NULL; | |
1013 | Iterator i; | |
1014 | int r; | |
1015 | ||
1016 | assert(f); | |
1017 | ||
1018 | SET_FOREACH(rule, rules, i) { | |
1019 | _cleanup_free_ char *from_str = NULL, *to_str = NULL; | |
1020 | bool space = false; | |
1021 | ||
1022 | fputs("RULE=", f); | |
1023 | ||
1024 | if (!in_addr_is_null(rule->family, &rule->from)) { | |
1025 | r = in_addr_to_string(rule->family, &rule->from, &from_str); | |
1026 | if (r < 0) | |
1027 | return r; | |
1028 | ||
1029 | fprintf(f, "from=%s/%hhu", | |
1030 | from_str, rule->from_prefixlen); | |
1031 | space = true; | |
1032 | } | |
1033 | ||
1034 | if (!in_addr_is_null(rule->family, &rule->to)) { | |
1035 | r = in_addr_to_string(rule->family, &rule->to, &to_str); | |
1036 | if (r < 0) | |
1037 | return r; | |
1038 | ||
1039 | fprintf(f, "%sto=%s/%hhu", | |
1040 | space ? " " : "", | |
1041 | to_str, rule->to_prefixlen); | |
1042 | space = true; | |
1043 | } | |
1044 | ||
1045 | if (rule->tos != 0) { | |
1046 | fprintf(f, "%stos=%hhu", | |
1047 | space ? " " : "", | |
1048 | rule->tos); | |
1049 | space = true; | |
1050 | } | |
1051 | ||
1052 | if (rule->fwmark != 0) { | |
1053 | fprintf(f, "%sfwmark=%"PRIu32"/%"PRIu32, | |
1054 | space ? " " : "", | |
1055 | rule->fwmark, rule->fwmask); | |
1056 | space = true; | |
1057 | } | |
1058 | ||
9491f55f ZJS |
1059 | if (rule->iif) { |
1060 | fprintf(f, "%siif=%s", | |
1061 | space ? " " : "", | |
1062 | rule->iif); | |
1063 | space = true; | |
1064 | } | |
1065 | ||
1066 | if (rule->oif) { | |
1067 | fprintf(f, "%soif=%s", | |
1068 | space ? " " : "", | |
1069 | rule->oif); | |
1070 | space = true; | |
1071 | } | |
1072 | ||
926062f0 SS |
1073 | if (rule->protocol != 0) { |
1074 | fprintf(f, "%sprotocol=%hhu", | |
1075 | space ? " " : "", | |
1076 | rule->protocol); | |
1077 | space = true; | |
1078 | } | |
1079 | ||
1080 | if (rule->sport.start != 0 || rule->sport.end != 0) { | |
58a02e4c | 1081 | fprintf(f, "%ssourcesport=%"PRIu16"-%"PRIu16, |
926062f0 SS |
1082 | space ? " " : "", |
1083 | rule->sport.start, rule->sport.end); | |
1084 | space = true; | |
1085 | } | |
1086 | ||
1087 | if (rule->dport.start != 0 || rule->dport.end != 0) { | |
58a02e4c | 1088 | fprintf(f, "%sdestinationport=%"PRIu16"-%"PRIu16, |
926062f0 SS |
1089 | space ? " " : "", |
1090 | rule->dport.start, rule->dport.end); | |
1091 | space = true; | |
1092 | } | |
1093 | ||
458d8ae3 ZJS |
1094 | fprintf(f, "%stable=%"PRIu32 "\n", |
1095 | space ? " " : "", | |
1096 | rule->table); | |
1097 | } | |
1098 | ||
1099 | return 0; | |
1100 | } | |
1101 | ||
1102 | int routing_policy_load_rules(const char *state_file, Set **rules) { | |
bce67bbe SS |
1103 | _cleanup_strv_free_ char **l = NULL; |
1104 | _cleanup_free_ char *data = NULL; | |
926062f0 | 1105 | uint16_t low = 0, high = 0; |
bce67bbe SS |
1106 | const char *p; |
1107 | char **i; | |
1108 | int r; | |
1109 | ||
458d8ae3 ZJS |
1110 | assert(state_file); |
1111 | assert(rules); | |
bce67bbe | 1112 | |
458d8ae3 | 1113 | r = routing_policy_rule_read_full_file(state_file, &data); |
bce67bbe SS |
1114 | if (r <= 0) |
1115 | return r; | |
1116 | ||
1117 | l = strv_split_newlines(data); | |
1118 | if (!l) | |
1119 | return -ENOMEM; | |
1120 | ||
458d8ae3 | 1121 | r = set_ensure_allocated(rules, &routing_policy_rule_hash_ops); |
bce67bbe SS |
1122 | if (r < 0) |
1123 | return r; | |
1124 | ||
1125 | STRV_FOREACH(i, l) { | |
8e766630 | 1126 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL; |
bce67bbe SS |
1127 | |
1128 | p = startswith(*i, "RULE="); | |
1129 | if (!p) | |
1130 | continue; | |
1131 | ||
bce67bbe SS |
1132 | r = routing_policy_rule_new(&rule); |
1133 | if (r < 0) | |
1134 | return r; | |
1135 | ||
1136 | for (;;) { | |
1137 | _cleanup_free_ char *word = NULL, *a = NULL, *b = NULL; | |
bce67bbe SS |
1138 | |
1139 | r = extract_first_word(&p, &word, NULL, 0); | |
1140 | if (r < 0) | |
1141 | return r; | |
1142 | if (r == 0) | |
1143 | break; | |
1144 | ||
1145 | r = split_pair(word, "=", &a, &b); | |
1146 | if (r < 0) | |
1147 | continue; | |
1148 | ||
1149 | if (STR_IN_SET(a, "from", "to")) { | |
7934dede YW |
1150 | union in_addr_union *buffer; |
1151 | uint8_t *prefixlen; | |
bce67bbe SS |
1152 | |
1153 | if (streq(a, "to")) { | |
7934dede YW |
1154 | buffer = &rule->to; |
1155 | prefixlen = &rule->to_prefixlen; | |
bce67bbe | 1156 | } else { |
7934dede YW |
1157 | buffer = &rule->from; |
1158 | prefixlen = &rule->from_prefixlen; | |
1159 | } | |
1160 | ||
1161 | r = in_addr_prefix_from_string_auto(b, &rule->family, buffer, prefixlen); | |
1162 | if (r < 0) { | |
1163 | log_error_errno(r, "RPDB rule prefix is invalid, ignoring assignment: %s", b); | |
1164 | continue; | |
bce67bbe | 1165 | } |
7934dede | 1166 | |
bce67bbe SS |
1167 | } else if (streq(a, "tos")) { |
1168 | r = safe_atou8(b, &rule->tos); | |
1169 | if (r < 0) { | |
1170 | log_error_errno(r, "Failed to parse RPDB rule tos, ignoring: %s", b); | |
1171 | continue; | |
1172 | } | |
1173 | } else if (streq(a, "table")) { | |
1174 | r = safe_atou32(b, &rule->table); | |
1175 | if (r < 0) { | |
1176 | log_error_errno(r, "Failed to parse RPDB rule table, ignoring: %s", b); | |
1177 | continue; | |
1178 | } | |
1179 | } else if (streq(a, "fwmark")) { | |
1180 | ||
e4aca57d | 1181 | r = parse_fwmark_fwmask(b, &rule->fwmark, &rule->fwmask); |
bce67bbe SS |
1182 | if (r < 0) { |
1183 | log_error_errno(r, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", a); | |
1184 | continue; | |
1185 | } | |
9491f55f | 1186 | } else if (streq(a, "iif")) { |
762e2659 | 1187 | |
93f9da6e | 1188 | if (free_and_strdup(&rule->iif, b) < 0) |
762e2659 | 1189 | return log_oom(); |
93f9da6e | 1190 | |
9491f55f | 1191 | } else if (streq(a, "oif")) { |
762e2659 | 1192 | |
93f9da6e | 1193 | if (free_and_strdup(&rule->oif, b) < 0) |
762e2659 | 1194 | return log_oom(); |
926062f0 SS |
1195 | } else if (streq(a, "protocol")) { |
1196 | r = safe_atou8(b, &rule->protocol); | |
1197 | if (r < 0) { | |
1198 | log_error_errno(r, "Failed to parse RPDB rule protocol, ignoring: %s", b); | |
1199 | continue; | |
1200 | } | |
1201 | } else if (streq(a, "sourceport")) { | |
1202 | ||
1203 | r = parse_ip_port_range(b, &low, &high); | |
1204 | if (r < 0) { | |
1205 | log_error_errno(r, "Invalid routing policy rule source port range, ignoring assignment:'%s'", b); | |
1206 | continue; | |
1207 | } | |
1208 | ||
1209 | rule->sport.start = low; | |
1210 | rule->sport.end = high; | |
1211 | ||
1212 | } else if (streq(a, "destinationport")) { | |
1213 | ||
1214 | r = parse_ip_port_range(b, &low, &high); | |
1215 | if (r < 0) { | |
1216 | log_error_errno(r, "Invalid routing policy rule destination port range, ignoring assignment:'%s'", b); | |
1217 | continue; | |
1218 | } | |
1219 | ||
1220 | rule->dport.start = low; | |
1221 | rule->dport.end = high; | |
bce67bbe SS |
1222 | } |
1223 | } | |
1224 | ||
458d8ae3 | 1225 | r = set_put(*rules, rule); |
bce67bbe SS |
1226 | if (r < 0) { |
1227 | log_warning_errno(r, "Failed to add RPDB rule to saved DB, ignoring: %s", p); | |
1228 | continue; | |
1229 | } | |
1230 | ||
1231 | rule = NULL; | |
1232 | } | |
1233 | ||
1234 | return 0; | |
1235 | } | |
1236 | ||
1237 | void routing_policy_rule_purge(Manager *m, Link *link) { | |
1238 | RoutingPolicyRule *rule, *existing; | |
1239 | Iterator i; | |
1240 | int r; | |
1241 | ||
1242 | assert(m); | |
1243 | assert(link); | |
1244 | ||
1245 | SET_FOREACH(rule, m->rules_saved, i) { | |
1246 | existing = set_get(m->rules_foreign, rule); | |
92cd00b9 YW |
1247 | if (!existing) |
1248 | continue; /* Saved rule does not exist anymore. */ | |
bce67bbe | 1249 | |
92cd00b9 YW |
1250 | r = routing_policy_rule_remove(existing, link, NULL); |
1251 | if (r < 0) { | |
1252 | log_warning_errno(r, "Could not remove routing policy rules: %m"); | |
1253 | continue; | |
bce67bbe | 1254 | } |
92cd00b9 YW |
1255 | |
1256 | link->routing_policy_rule_remove_messages++; | |
1257 | ||
1258 | assert_se(set_remove(m->rules_foreign, existing) == existing); | |
1259 | routing_policy_rule_free(existing); | |
bce67bbe SS |
1260 | } |
1261 | } |