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