| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/fib_rules.h> |
| 4 |
|
| 5 |
#include "sd-netlink.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "conf-parser.h" |
| 9 |
#include "errno-util.h" |
| 10 |
#include "hashmap.h" |
| 11 |
#include "netlink-util.h" |
| 12 |
#include "network-util.h" |
| 13 |
#include "networkd-link.h" |
| 14 |
#include "networkd-manager.h" |
| 15 |
#include "networkd-queue.h" |
| 16 |
#include "networkd-route-util.h" |
| 17 |
#include "networkd-routing-policy-rule.h" |
| 18 |
#include "networkd-util.h" |
| 19 |
#include "ordered-set.h" |
| 20 |
#include "parse-util.h" |
| 21 |
#include "set.h" |
| 22 |
#include "siphash24.h" |
| 23 |
#include "string-table.h" |
| 24 |
#include "string-util.h" |
| 25 |
#include "user-util.h" |
| 26 |
|
| 27 |
static const char *const fr_act_type_table[__FR_ACT_MAX] = { |
| 28 |
[FR_ACT_TO_TBL] = "table", |
| 29 |
[FR_ACT_GOTO] = "goto", |
| 30 |
[FR_ACT_NOP] = "nop", |
| 31 |
[FR_ACT_BLACKHOLE] = "blackhole", |
| 32 |
[FR_ACT_UNREACHABLE] = "unreachable", |
| 33 |
[FR_ACT_PROHIBIT] = "prohibit", |
| 34 |
}; |
| 35 |
|
| 36 |
assert_cc(__FR_ACT_MAX <= UINT8_MAX); |
| 37 |
DEFINE_STRING_TABLE_LOOKUP(fr_act_type, int); |
| 38 |
|
| 39 |
static RoutingPolicyRule* routing_policy_rule_detach_impl(RoutingPolicyRule *rule) { |
| 40 |
assert(rule); |
| 41 |
assert(!!rule->manager + !!rule->network <= 1); |
| 42 |
|
| 43 |
if (rule->network) { |
| 44 |
assert(rule->section); |
| 45 |
hashmap_remove(rule->network->rules_by_section, rule->section); |
| 46 |
rule->network = NULL; |
| 47 |
return rule; |
| 48 |
} |
| 49 |
|
| 50 |
if (rule->manager) { |
| 51 |
set_remove(rule->manager->rules, rule); |
| 52 |
rule->manager = NULL; |
| 53 |
return rule; |
| 54 |
} |
| 55 |
|
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
static void routing_policy_rule_detach(RoutingPolicyRule *rule) { |
| 60 |
assert(rule); |
| 61 |
routing_policy_rule_unref(routing_policy_rule_detach_impl(rule)); |
| 62 |
} |
| 63 |
|
| 64 |
static RoutingPolicyRule* routing_policy_rule_free(RoutingPolicyRule *rule) { |
| 65 |
if (!rule) |
| 66 |
return NULL; |
| 67 |
|
| 68 |
routing_policy_rule_detach_impl(rule); |
| 69 |
|
| 70 |
config_section_free(rule->section); |
| 71 |
free(rule->iif); |
| 72 |
free(rule->oif); |
| 73 |
|
| 74 |
return mfree(rule); |
| 75 |
} |
| 76 |
|
| 77 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(RoutingPolicyRule, routing_policy_rule, routing_policy_rule_free); |
| 78 |
|
| 79 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 80 |
routing_policy_rule_section_hash_ops, |
| 81 |
ConfigSection, |
| 82 |
config_section_hash_func, |
| 83 |
config_section_compare_func, |
| 84 |
RoutingPolicyRule, |
| 85 |
routing_policy_rule_detach); |
| 86 |
|
| 87 |
static void routing_policy_rule_hash_func(const RoutingPolicyRule *rule, struct siphash *state); |
| 88 |
static int routing_policy_rule_compare_func(const RoutingPolicyRule *a, const RoutingPolicyRule *b); |
| 89 |
|
| 90 |
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR( |
| 91 |
routing_policy_rule_hash_ops_detach, |
| 92 |
RoutingPolicyRule, |
| 93 |
routing_policy_rule_hash_func, |
| 94 |
routing_policy_rule_compare_func, |
| 95 |
routing_policy_rule_detach); |
| 96 |
|
| 97 |
DEFINE_SECTION_CLEANUP_FUNCTIONS(RoutingPolicyRule, routing_policy_rule_unref); |
| 98 |
|
| 99 |
static int routing_policy_rule_new(RoutingPolicyRule **ret) { |
| 100 |
RoutingPolicyRule *rule; |
| 101 |
|
| 102 |
assert(ret); |
| 103 |
|
| 104 |
rule = new(RoutingPolicyRule, 1); |
| 105 |
if (!rule) |
| 106 |
return -ENOMEM; |
| 107 |
|
| 108 |
*rule = (RoutingPolicyRule) { |
| 109 |
.n_ref = 1, |
| 110 |
.table = RT_TABLE_MAIN, |
| 111 |
.uid_range.start = UID_INVALID, |
| 112 |
.uid_range.end = UID_INVALID, |
| 113 |
.suppress_prefixlen = -1, |
| 114 |
.suppress_ifgroup = -1, |
| 115 |
.protocol = RTPROT_UNSPEC, |
| 116 |
.action = FR_ACT_TO_TBL, |
| 117 |
}; |
| 118 |
|
| 119 |
*ret = rule; |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
static int routing_policy_rule_new_static(Network *network, const char *filename, unsigned section_line, RoutingPolicyRule **ret) { |
| 124 |
_cleanup_(routing_policy_rule_unrefp) RoutingPolicyRule *rule = NULL; |
| 125 |
_cleanup_(config_section_freep) ConfigSection *n = NULL; |
| 126 |
int r; |
| 127 |
|
| 128 |
assert(network); |
| 129 |
assert(ret); |
| 130 |
assert(filename); |
| 131 |
assert(section_line > 0); |
| 132 |
|
| 133 |
r = config_section_new(filename, section_line, &n); |
| 134 |
if (r < 0) |
| 135 |
return r; |
| 136 |
|
| 137 |
rule = hashmap_get(network->rules_by_section, n); |
| 138 |
if (rule) { |
| 139 |
*ret = TAKE_PTR(rule); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
r = routing_policy_rule_new(&rule); |
| 144 |
if (r < 0) |
| 145 |
return r; |
| 146 |
|
| 147 |
rule->network = network; |
| 148 |
rule->section = TAKE_PTR(n); |
| 149 |
rule->source = NETWORK_CONFIG_SOURCE_STATIC; |
| 150 |
rule->protocol = RTPROT_STATIC; |
| 151 |
|
| 152 |
r = hashmap_ensure_put(&network->rules_by_section, &routing_policy_rule_section_hash_ops, rule->section, rule); |
| 153 |
if (r < 0) |
| 154 |
return r; |
| 155 |
|
| 156 |
*ret = TAKE_PTR(rule); |
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
static int routing_policy_rule_dup(const RoutingPolicyRule *src, int family, RoutingPolicyRule **ret) { |
| 161 |
_cleanup_(routing_policy_rule_unrefp) RoutingPolicyRule *dest = NULL; |
| 162 |
|
| 163 |
assert(src); |
| 164 |
assert(ret); |
| 165 |
|
| 166 |
dest = newdup(RoutingPolicyRule, src, 1); |
| 167 |
if (!dest) |
| 168 |
return -ENOMEM; |
| 169 |
|
| 170 |
/* Clear the reference counter and all pointers. */ |
| 171 |
dest->n_ref = 1; |
| 172 |
dest->manager = NULL; |
| 173 |
dest->network = NULL; |
| 174 |
dest->section = NULL; |
| 175 |
dest->iif = dest->oif = NULL; |
| 176 |
|
| 177 |
/* Set family. */ |
| 178 |
dest->family = family; |
| 179 |
|
| 180 |
if (src->iif) { |
| 181 |
dest->iif = strdup(src->iif); |
| 182 |
if (!dest->iif) |
| 183 |
return -ENOMEM; |
| 184 |
} |
| 185 |
|
| 186 |
if (src->oif) { |
| 187 |
dest->oif = strdup(src->oif); |
| 188 |
if (!dest->oif) |
| 189 |
return -ENOMEM; |
| 190 |
} |
| 191 |
|
| 192 |
*ret = TAKE_PTR(dest); |
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
static void routing_policy_rule_hash_func(const RoutingPolicyRule *rule, struct siphash *state) { |
| 197 |
assert(rule); |
| 198 |
|
| 199 |
/* See rule_exists() in net/core/fib_rules.c of the kernel. */ |
| 200 |
siphash24_compress_typesafe(rule->family, state); |
| 201 |
siphash24_compress_typesafe(rule->action, state); |
| 202 |
siphash24_compress_typesafe(rule->table, state); |
| 203 |
siphash24_compress_typesafe(rule->priority, state); |
| 204 |
siphash24_compress_string(rule->iif, state); |
| 205 |
siphash24_compress_string(rule->oif, state); |
| 206 |
siphash24_compress_typesafe(rule->fwmark, state); |
| 207 |
siphash24_compress_typesafe(rule->suppress_ifgroup, state); |
| 208 |
siphash24_compress_typesafe(rule->suppress_prefixlen, state); |
| 209 |
siphash24_compress_typesafe(rule->fwmask, state); |
| 210 |
siphash24_compress_typesafe(rule->tunnel_id, state); |
| 211 |
/* fr_net (network namespace) */ |
| 212 |
siphash24_compress_typesafe(rule->l3mdev, state); |
| 213 |
siphash24_compress_typesafe(rule->uid_range, state); |
| 214 |
siphash24_compress_typesafe(rule->ipproto, state); |
| 215 |
siphash24_compress_typesafe(rule->protocol, state); |
| 216 |
siphash24_compress_typesafe(rule->sport, state); |
| 217 |
siphash24_compress_typesafe(rule->dport, state); |
| 218 |
|
| 219 |
/* See fib4_rule_compare() in net/ipv4/fib_rules.c, and fib6_rule_compare() in net/ipv6/fib6_rules.c. */ |
| 220 |
siphash24_compress_typesafe(rule->from.prefixlen, state); |
| 221 |
siphash24_compress_typesafe(rule->to.prefixlen, state); |
| 222 |
siphash24_compress_typesafe(rule->tos, state); |
| 223 |
siphash24_compress_typesafe(rule->realms, state); |
| 224 |
in_addr_hash_func(&rule->from.address, rule->family, state); |
| 225 |
in_addr_hash_func(&rule->to.address, rule->family, state); |
| 226 |
} |
| 227 |
|
| 228 |
static int routing_policy_rule_compare_func_full(const RoutingPolicyRule *a, const RoutingPolicyRule *b, bool all) { |
| 229 |
int r; |
| 230 |
|
| 231 |
assert(a); |
| 232 |
assert(b); |
| 233 |
|
| 234 |
if (all) { |
| 235 |
r = CMP(a->family, b->family); |
| 236 |
if (r != 0) |
| 237 |
return r; |
| 238 |
} |
| 239 |
|
| 240 |
r = CMP(a->action, b->action); |
| 241 |
if (r != 0) |
| 242 |
return r; |
| 243 |
|
| 244 |
r = CMP(a->table, b->table); |
| 245 |
if (r != 0) |
| 246 |
return r; |
| 247 |
|
| 248 |
if (all) { |
| 249 |
r = CMP(a->priority, b->priority); |
| 250 |
if (r != 0) |
| 251 |
return r; |
| 252 |
} |
| 253 |
|
| 254 |
r = strcmp_ptr(a->iif, b->iif); |
| 255 |
if (r != 0) |
| 256 |
return r; |
| 257 |
|
| 258 |
r = strcmp_ptr(a->oif, b->oif); |
| 259 |
if (r != 0) |
| 260 |
return r; |
| 261 |
|
| 262 |
r = CMP(a->fwmark, b->fwmark); |
| 263 |
if (r != 0) |
| 264 |
return r; |
| 265 |
|
| 266 |
r = CMP(a->suppress_ifgroup, b->suppress_ifgroup); |
| 267 |
if (r != 0) |
| 268 |
return r; |
| 269 |
|
| 270 |
r = CMP(a->suppress_prefixlen, b->suppress_prefixlen); |
| 271 |
if (r != 0) |
| 272 |
return r; |
| 273 |
|
| 274 |
r = CMP(a->fwmask, b->fwmask); |
| 275 |
if (r != 0) |
| 276 |
return r; |
| 277 |
|
| 278 |
r = CMP(a->tunnel_id, b->tunnel_id); |
| 279 |
if (r != 0) |
| 280 |
return r; |
| 281 |
|
| 282 |
r = CMP(a->l3mdev, b->l3mdev); |
| 283 |
if (r != 0) |
| 284 |
return r; |
| 285 |
|
| 286 |
r = memcmp(&a->uid_range, &b->uid_range, sizeof(a->uid_range)); |
| 287 |
if (r != 0) |
| 288 |
return r; |
| 289 |
|
| 290 |
r = CMP(a->ipproto, b->ipproto); |
| 291 |
if (r != 0) |
| 292 |
return r; |
| 293 |
|
| 294 |
r = CMP(a->protocol, b->protocol); |
| 295 |
if (r != 0) |
| 296 |
return r; |
| 297 |
|
| 298 |
r = memcmp(&a->sport, &b->sport, sizeof(a->sport)); |
| 299 |
if (r != 0) |
| 300 |
return r; |
| 301 |
|
| 302 |
r = memcmp(&a->dport, &b->dport, sizeof(a->dport)); |
| 303 |
if (r != 0) |
| 304 |
return r; |
| 305 |
|
| 306 |
r = CMP(a->from.prefixlen, b->from.prefixlen); |
| 307 |
if (r != 0) |
| 308 |
return r; |
| 309 |
|
| 310 |
r = CMP(a->to.prefixlen, b->to.prefixlen); |
| 311 |
if (r != 0) |
| 312 |
return r; |
| 313 |
|
| 314 |
r = CMP(a->tos, b->tos); |
| 315 |
if (r != 0) |
| 316 |
return r; |
| 317 |
|
| 318 |
r = CMP(a->realms, b->realms); |
| 319 |
if (r != 0) |
| 320 |
return r; |
| 321 |
|
| 322 |
if (a->family == b->family && a->family != AF_UNSPEC) { |
| 323 |
r = memcmp(&a->from.address, &b->from.address, FAMILY_ADDRESS_SIZE(a->family)); |
| 324 |
if (r != 0) |
| 325 |
return r; |
| 326 |
|
| 327 |
r = memcmp(&a->to.address, &b->to.address, FAMILY_ADDRESS_SIZE(a->family)); |
| 328 |
if (r != 0) |
| 329 |
return r; |
| 330 |
} |
| 331 |
|
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
static int routing_policy_rule_compare_func(const RoutingPolicyRule *a, const RoutingPolicyRule *b) { |
| 336 |
return routing_policy_rule_compare_func_full(a, b, /* all= */ true); |
| 337 |
} |
| 338 |
|
| 339 |
static bool routing_policy_rule_equal(const RoutingPolicyRule *a, const RoutingPolicyRule *b, int family, uint32_t priority) { |
| 340 |
assert(a); |
| 341 |
assert(b); |
| 342 |
|
| 343 |
if (a->family != AF_UNSPEC && a->family != family) |
| 344 |
return false; |
| 345 |
if (b->family != AF_UNSPEC && b->family != family) |
| 346 |
return false; |
| 347 |
|
| 348 |
if (a->priority_set && a->priority != priority) |
| 349 |
return false; |
| 350 |
if (b->priority_set && b->priority != priority) |
| 351 |
return false; |
| 352 |
|
| 353 |
return routing_policy_rule_compare_func_full(a, b, /* all= */ false) == 0; |
| 354 |
} |
| 355 |
|
| 356 |
static bool routing_policy_rule_can_update(const RoutingPolicyRule *existing, const RoutingPolicyRule *requesting) { |
| 357 |
assert(existing); |
| 358 |
assert(IN_SET(existing->family, AF_INET, AF_INET6)); |
| 359 |
assert(requesting); |
| 360 |
assert(IN_SET(requesting->family, AF_INET, AF_INET6) || requesting->address_family == ADDRESS_FAMILY_YES); |
| 361 |
|
| 362 |
if (!routing_policy_rule_equal(existing, requesting, existing->family, existing->priority)) |
| 363 |
return false; |
| 364 |
|
| 365 |
/* These flags cannot be updated. */ |
| 366 |
if ((existing->flags ^ requesting->flags) & (FIB_RULE_PERMANENT|FIB_RULE_INVERT)) |
| 367 |
return false; |
| 368 |
|
| 369 |
/* GOTO target cannot be updated. */ |
| 370 |
if (existing->action == FR_ACT_GOTO && existing->priority_goto != requesting->priority_goto) |
| 371 |
return false; |
| 372 |
|
| 373 |
return true; |
| 374 |
} |
| 375 |
|
| 376 |
static int routing_policy_rule_get(Manager *m, const RoutingPolicyRule *in, int family, RoutingPolicyRule **ret) { |
| 377 |
RoutingPolicyRule *rule; |
| 378 |
|
| 379 |
assert(m); |
| 380 |
assert(in); |
| 381 |
assert(in->family == AF_UNSPEC || in->family == family); |
| 382 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 383 |
|
| 384 |
if (in->priority_set && in->family != AF_UNSPEC) { |
| 385 |
rule = set_get(m->rules, in); |
| 386 |
if (!rule) |
| 387 |
return -ENOENT; |
| 388 |
|
| 389 |
if (ret) |
| 390 |
*ret = rule; |
| 391 |
return 0; |
| 392 |
} |
| 393 |
|
| 394 |
SET_FOREACH(rule, m->rules) |
| 395 |
if (routing_policy_rule_equal(in, rule, family, rule->priority)) { |
| 396 |
if (ret) |
| 397 |
*ret = rule; |
| 398 |
return 0; |
| 399 |
} |
| 400 |
|
| 401 |
return -ENOENT; |
| 402 |
} |
| 403 |
|
| 404 |
static int routing_policy_rule_get_request(Manager *m, const RoutingPolicyRule *in, int family, Request **ret) { |
| 405 |
Request *req; |
| 406 |
|
| 407 |
assert(m); |
| 408 |
assert(in); |
| 409 |
assert(in->family == AF_UNSPEC || in->family == family); |
| 410 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 411 |
|
| 412 |
if (in->priority_set && in->family != AF_UNSPEC) { |
| 413 |
req = ordered_set_get( |
| 414 |
m->request_queue, |
| 415 |
&(Request) { |
| 416 |
.type = REQUEST_TYPE_ROUTING_POLICY_RULE, |
| 417 |
.userdata = (void*) in, |
| 418 |
.hash_func = (hash_func_t) routing_policy_rule_hash_func, |
| 419 |
.compare_func = (compare_func_t) routing_policy_rule_compare_func, |
| 420 |
}); |
| 421 |
if (!req) |
| 422 |
return -ENOENT; |
| 423 |
|
| 424 |
if (ret) |
| 425 |
*ret = req; |
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
ORDERED_SET_FOREACH(req, m->request_queue) { |
| 430 |
|
| 431 |
if (req->type != REQUEST_TYPE_ROUTING_POLICY_RULE) |
| 432 |
continue; |
| 433 |
|
| 434 |
RoutingPolicyRule *rule = ASSERT_PTR(req->userdata); |
| 435 |
if (routing_policy_rule_equal(in, rule, family, rule->priority)) { |
| 436 |
if (ret) |
| 437 |
*ret = req; |
| 438 |
return 0; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
return -ENOENT; |
| 443 |
} |
| 444 |
|
| 445 |
static int routing_policy_rule_attach(Manager *m, RoutingPolicyRule *rule) { |
| 446 |
int r; |
| 447 |
|
| 448 |
assert(m); |
| 449 |
assert(rule); |
| 450 |
assert(IN_SET(rule->family, AF_INET, AF_INET6)); |
| 451 |
assert(!rule->manager); |
| 452 |
|
| 453 |
r = set_ensure_put(&m->rules, &routing_policy_rule_hash_ops_detach, rule); |
| 454 |
if (r < 0) |
| 455 |
return r; |
| 456 |
if (r == 0) |
| 457 |
return -EEXIST; |
| 458 |
|
| 459 |
rule->manager = m; |
| 460 |
routing_policy_rule_ref(rule); |
| 461 |
return 0; |
| 462 |
} |
| 463 |
|
| 464 |
static int routing_policy_rule_acquire_priority(Manager *manager, RoutingPolicyRule *rule) { |
| 465 |
_cleanup_set_free_ Set *priorities = NULL; |
| 466 |
RoutingPolicyRule *tmp; |
| 467 |
uint32_t priority; |
| 468 |
Network *network; |
| 469 |
int r; |
| 470 |
|
| 471 |
assert(manager); |
| 472 |
assert(rule); |
| 473 |
assert(IN_SET(rule->family, AF_INET, AF_INET6)); |
| 474 |
|
| 475 |
if (rule->priority_set) |
| 476 |
return 0; |
| 477 |
|
| 478 |
/* Find the highest unused priority. Note that 32766 is already used by kernel. |
| 479 |
* See kernel_rules[] below. */ |
| 480 |
|
| 481 |
SET_FOREACH(tmp, manager->rules) { |
| 482 |
if (tmp->family != rule->family) |
| 483 |
continue; |
| 484 |
if (tmp->priority == 0 || tmp->priority > 32765) |
| 485 |
continue; |
| 486 |
r = set_ensure_put(&priorities, NULL, UINT32_TO_PTR(tmp->priority)); |
| 487 |
if (r < 0) |
| 488 |
return r; |
| 489 |
} |
| 490 |
|
| 491 |
Request *req; |
| 492 |
ORDERED_SET_FOREACH(req, manager->request_queue) { |
| 493 |
if (req->type != REQUEST_TYPE_ROUTING_POLICY_RULE) |
| 494 |
continue; |
| 495 |
|
| 496 |
tmp = ASSERT_PTR(req->userdata); |
| 497 |
if (tmp->family != rule->family) |
| 498 |
continue; |
| 499 |
if (tmp->priority == 0 || tmp->priority > 32765) |
| 500 |
continue; |
| 501 |
r = set_ensure_put(&priorities, NULL, UINT32_TO_PTR(tmp->priority)); |
| 502 |
if (r < 0) |
| 503 |
return r; |
| 504 |
} |
| 505 |
|
| 506 |
ORDERED_HASHMAP_FOREACH(network, manager->networks) |
| 507 |
HASHMAP_FOREACH(tmp, network->rules_by_section) { |
| 508 |
if (tmp->family != AF_UNSPEC && tmp->family != rule->family) |
| 509 |
continue; |
| 510 |
if (!tmp->priority_set) |
| 511 |
continue; |
| 512 |
if (tmp->priority == 0 || tmp->priority > 32765) |
| 513 |
continue; |
| 514 |
r = set_ensure_put(&priorities, NULL, UINT32_TO_PTR(tmp->priority)); |
| 515 |
if (r < 0) |
| 516 |
return r; |
| 517 |
} |
| 518 |
|
| 519 |
/* priority must be smaller than goto target */ |
| 520 |
for (priority = rule->action == FR_ACT_GOTO ? rule->priority_goto - 1 : 32765; priority > 0; priority--) |
| 521 |
if (!set_contains(priorities, UINT32_TO_PTR(priority))) |
| 522 |
break; |
| 523 |
|
| 524 |
rule->priority = priority; |
| 525 |
rule->priority_set = true; |
| 526 |
return 0; |
| 527 |
} |
| 528 |
|
| 529 |
static void log_routing_policy_rule_debug(const RoutingPolicyRule *rule, const char *str, const Link *link, const Manager *m) { |
| 530 |
_cleanup_free_ char *state = NULL, *table = NULL; |
| 531 |
|
| 532 |
assert(rule); |
| 533 |
assert(IN_SET(rule->family, AF_INET, AF_INET6)); |
| 534 |
assert(str); |
| 535 |
assert(m); |
| 536 |
|
| 537 |
/* link may be NULL. */ |
| 538 |
|
| 539 |
if (!DEBUG_LOGGING) |
| 540 |
return; |
| 541 |
|
| 542 |
(void) network_config_state_to_string_alloc(rule->state, &state); |
| 543 |
(void) manager_get_route_table_to_string(m, rule->table, /* append_num= */ true, &table); |
| 544 |
|
| 545 |
log_link_debug(link, |
| 546 |
"%s %s routing policy rule (%s): priority: %"PRIu32", %s -> %s, iif: %s, oif: %s, table: %s", |
| 547 |
str, strna(network_config_source_to_string(rule->source)), strna(state), |
| 548 |
rule->priority, |
| 549 |
IN_ADDR_PREFIX_TO_STRING(rule->family, &rule->from.address, rule->from.prefixlen), |
| 550 |
IN_ADDR_PREFIX_TO_STRING(rule->family, &rule->to.address, rule->to.prefixlen), |
| 551 |
strna(rule->iif), strna(rule->oif), strna(table)); |
| 552 |
} |
| 553 |
|
| 554 |
static void routing_policy_rule_forget(Manager *manager, RoutingPolicyRule *rule, const char *msg) { |
| 555 |
assert(manager); |
| 556 |
assert(rule); |
| 557 |
assert(msg); |
| 558 |
|
| 559 |
Request *req; |
| 560 |
if (routing_policy_rule_get_request(manager, rule, rule->family, &req) >= 0) |
| 561 |
routing_policy_rule_enter_removed(req->userdata); |
| 562 |
|
| 563 |
if (!rule->manager && routing_policy_rule_get(manager, rule, rule->family, &rule) < 0) |
| 564 |
return; |
| 565 |
|
| 566 |
routing_policy_rule_enter_removed(rule); |
| 567 |
log_routing_policy_rule_debug(rule, msg, NULL, manager); |
| 568 |
routing_policy_rule_detach(rule); |
| 569 |
} |
| 570 |
|
| 571 |
static int routing_policy_rule_set_netlink_message(const RoutingPolicyRule *rule, sd_netlink_message *m) { |
| 572 |
int r; |
| 573 |
|
| 574 |
assert(rule); |
| 575 |
assert(m); |
| 576 |
|
| 577 |
if (rule->from.prefixlen > 0) { |
| 578 |
r = netlink_message_append_in_addr_union(m, FRA_SRC, rule->family, &rule->from.address); |
| 579 |
if (r < 0) |
| 580 |
return r; |
| 581 |
|
| 582 |
r = sd_rtnl_message_routing_policy_rule_set_src_prefixlen(m, rule->from.prefixlen); |
| 583 |
if (r < 0) |
| 584 |
return r; |
| 585 |
} |
| 586 |
|
| 587 |
if (rule->to.prefixlen > 0) { |
| 588 |
r = netlink_message_append_in_addr_union(m, FRA_DST, rule->family, &rule->to.address); |
| 589 |
if (r < 0) |
| 590 |
return r; |
| 591 |
|
| 592 |
r = sd_rtnl_message_routing_policy_rule_set_dst_prefixlen(m, rule->to.prefixlen); |
| 593 |
if (r < 0) |
| 594 |
return r; |
| 595 |
} |
| 596 |
|
| 597 |
r = sd_netlink_message_append_u32(m, FRA_PRIORITY, rule->priority); |
| 598 |
if (r < 0) |
| 599 |
return r; |
| 600 |
|
| 601 |
if (rule->tos > 0) { |
| 602 |
r = sd_rtnl_message_routing_policy_rule_set_tos(m, rule->tos); |
| 603 |
if (r < 0) |
| 604 |
return r; |
| 605 |
} |
| 606 |
|
| 607 |
if (rule->table < 256) |
| 608 |
r = sd_rtnl_message_routing_policy_rule_set_table(m, rule->table); |
| 609 |
else { |
| 610 |
r = sd_rtnl_message_routing_policy_rule_set_table(m, RT_TABLE_UNSPEC); |
| 611 |
if (r < 0) |
| 612 |
return r; |
| 613 |
|
| 614 |
r = sd_netlink_message_append_u32(m, FRA_TABLE, rule->table); |
| 615 |
} |
| 616 |
if (r < 0) |
| 617 |
return r; |
| 618 |
|
| 619 |
if (rule->fwmark > 0 || rule->fwmask > 0) { |
| 620 |
r = sd_netlink_message_append_u32(m, FRA_FWMARK, rule->fwmark); |
| 621 |
if (r < 0) |
| 622 |
return r; |
| 623 |
|
| 624 |
r = sd_netlink_message_append_u32(m, FRA_FWMASK, rule->fwmask); |
| 625 |
if (r < 0) |
| 626 |
return r; |
| 627 |
} |
| 628 |
|
| 629 |
if (rule->iif) { |
| 630 |
r = sd_netlink_message_append_string(m, FRA_IIFNAME, rule->iif); |
| 631 |
if (r < 0) |
| 632 |
return r; |
| 633 |
} |
| 634 |
|
| 635 |
if (rule->oif) { |
| 636 |
r = sd_netlink_message_append_string(m, FRA_OIFNAME, rule->oif); |
| 637 |
if (r < 0) |
| 638 |
return r; |
| 639 |
} |
| 640 |
|
| 641 |
r = sd_netlink_message_append_u8(m, FRA_IP_PROTO, rule->ipproto); |
| 642 |
if (r < 0) |
| 643 |
return r; |
| 644 |
|
| 645 |
r = sd_netlink_message_append_u8(m, FRA_PROTOCOL, rule->protocol); |
| 646 |
if (r < 0) |
| 647 |
return r; |
| 648 |
|
| 649 |
if (rule->sport.start != 0 || rule->sport.end != 0) { |
| 650 |
r = sd_netlink_message_append_data(m, FRA_SPORT_RANGE, &rule->sport, sizeof(rule->sport)); |
| 651 |
if (r < 0) |
| 652 |
return r; |
| 653 |
} |
| 654 |
|
| 655 |
if (rule->dport.start != 0 || rule->dport.end != 0) { |
| 656 |
r = sd_netlink_message_append_data(m, FRA_DPORT_RANGE, &rule->dport, sizeof(rule->dport)); |
| 657 |
if (r < 0) |
| 658 |
return r; |
| 659 |
} |
| 660 |
|
| 661 |
if (rule->uid_range.start != UID_INVALID && rule->uid_range.end != UID_INVALID) { |
| 662 |
r = sd_netlink_message_append_data(m, FRA_UID_RANGE, &rule->uid_range, sizeof(rule->uid_range)); |
| 663 |
if (r < 0) |
| 664 |
return r; |
| 665 |
} |
| 666 |
|
| 667 |
r = sd_rtnl_message_routing_policy_rule_set_flags(m, rule->flags); |
| 668 |
if (r < 0) |
| 669 |
return r; |
| 670 |
|
| 671 |
if (rule->l3mdev) { |
| 672 |
r = sd_netlink_message_append_u8(m, FRA_L3MDEV, 1); |
| 673 |
if (r < 0) |
| 674 |
return r; |
| 675 |
} |
| 676 |
|
| 677 |
if (rule->suppress_prefixlen >= 0) { |
| 678 |
r = sd_netlink_message_append_u32(m, FRA_SUPPRESS_PREFIXLEN, (uint32_t) rule->suppress_prefixlen); |
| 679 |
if (r < 0) |
| 680 |
return r; |
| 681 |
} |
| 682 |
|
| 683 |
if (rule->suppress_ifgroup >= 0) { |
| 684 |
r = sd_netlink_message_append_u32(m, FRA_SUPPRESS_IFGROUP, (uint32_t) rule->suppress_ifgroup); |
| 685 |
if (r < 0) |
| 686 |
return r; |
| 687 |
} |
| 688 |
|
| 689 |
r = sd_rtnl_message_routing_policy_rule_set_action(m, rule->action); |
| 690 |
if (r < 0) |
| 691 |
return r; |
| 692 |
|
| 693 |
if (rule->action == FR_ACT_GOTO) { |
| 694 |
r = sd_netlink_message_append_u32(m, FRA_GOTO, rule->priority_goto); |
| 695 |
if (r < 0) |
| 696 |
return r; |
| 697 |
} |
| 698 |
|
| 699 |
if (rule->realms > 0) { |
| 700 |
r = sd_netlink_message_append_u32(m, FRA_FLOW, rule->realms); |
| 701 |
if (r < 0) |
| 702 |
return r; |
| 703 |
} |
| 704 |
|
| 705 |
if (rule->tunnel_id > 0) { |
| 706 |
r = sd_netlink_message_append_u64(m, FRA_TUN_ID, htobe64(rule->tunnel_id)); |
| 707 |
if (r < 0) |
| 708 |
return r; |
| 709 |
} |
| 710 |
|
| 711 |
return 0; |
| 712 |
} |
| 713 |
|
| 714 |
static int routing_policy_rule_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, RemoveRequest *rreq) { |
| 715 |
int r; |
| 716 |
|
| 717 |
assert(m); |
| 718 |
assert(rreq); |
| 719 |
|
| 720 |
Manager *manager = ASSERT_PTR(rreq->manager); |
| 721 |
RoutingPolicyRule *rule = ASSERT_PTR(rreq->userdata); |
| 722 |
|
| 723 |
r = sd_netlink_message_get_errno(m); |
| 724 |
if (r < 0) { |
| 725 |
log_message_full_errno(m, |
| 726 |
(r == -ENOENT || !rule->manager) ? LOG_DEBUG : LOG_WARNING, |
| 727 |
r, "Could not drop routing policy rule, ignoring"); |
| 728 |
|
| 729 |
/* If the rule cannot be removed, then assume the rule is already removed. */ |
| 730 |
routing_policy_rule_forget(manager, rule, "Forgetting"); |
| 731 |
} |
| 732 |
|
| 733 |
return 1; |
| 734 |
} |
| 735 |
|
| 736 |
static int routing_policy_rule_remove(RoutingPolicyRule *rule, Manager *manager) { |
| 737 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 738 |
int r; |
| 739 |
|
| 740 |
assert(rule); |
| 741 |
assert(IN_SET(rule->family, AF_INET, AF_INET6)); |
| 742 |
assert(manager); |
| 743 |
assert(manager->rtnl); |
| 744 |
|
| 745 |
/* If the rule is remembered, then use the remembered object. */ |
| 746 |
(void) routing_policy_rule_get(manager, rule, rule->family, &rule); |
| 747 |
|
| 748 |
/* We cannot remove rules with the permanent flag. */ |
| 749 |
if (FLAGS_SET(rule->flags, FIB_RULE_PERMANENT)) |
| 750 |
return 0; |
| 751 |
|
| 752 |
log_routing_policy_rule_debug(rule, "Removing", NULL, manager); |
| 753 |
|
| 754 |
r = sd_rtnl_message_new_routing_policy_rule(manager->rtnl, &m, RTM_DELRULE, rule->family); |
| 755 |
if (r < 0) |
| 756 |
return log_warning_errno(r, "Could not allocate netlink message: %m"); |
| 757 |
|
| 758 |
r = routing_policy_rule_set_netlink_message(rule, m); |
| 759 |
if (r < 0) |
| 760 |
return log_warning_errno(r, "Could not create netlink message: %m"); |
| 761 |
|
| 762 |
r = manager_remove_request_add(manager, rule, routing_policy_rule, |
| 763 |
manager->rtnl, m, routing_policy_rule_remove_handler); |
| 764 |
if (r < 0) |
| 765 |
return log_warning_errno(r, "Could not queue rtnetlink message: %m"); |
| 766 |
|
| 767 |
routing_policy_rule_enter_removing(rule); |
| 768 |
return 0; |
| 769 |
} |
| 770 |
|
| 771 |
static int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, Request *req) { |
| 772 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 773 |
int r; |
| 774 |
|
| 775 |
assert(rule); |
| 776 |
assert(IN_SET(rule->family, AF_INET, AF_INET6)); |
| 777 |
assert(link); |
| 778 |
assert(link->ifindex > 0); |
| 779 |
assert(link->manager); |
| 780 |
assert(link->manager->rtnl); |
| 781 |
assert(req); |
| 782 |
|
| 783 |
log_routing_policy_rule_debug(rule, "Configuring", link, link->manager); |
| 784 |
|
| 785 |
r = sd_rtnl_message_new_routing_policy_rule(link->manager->rtnl, &m, RTM_NEWRULE, rule->family); |
| 786 |
if (r < 0) |
| 787 |
return r; |
| 788 |
|
| 789 |
r = routing_policy_rule_set_netlink_message(rule, m); |
| 790 |
if (r < 0) |
| 791 |
return r; |
| 792 |
|
| 793 |
return request_call_netlink_async(link->manager->rtnl, m, req); |
| 794 |
} |
| 795 |
|
| 796 |
static void manager_unmark_routing_policy_rule(Manager *m, const RoutingPolicyRule *rule, int family) { |
| 797 |
RoutingPolicyRule *existing; |
| 798 |
|
| 799 |
assert(m); |
| 800 |
assert(rule); |
| 801 |
assert(rule->family == AF_UNSPEC || rule->family == family); |
| 802 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 803 |
|
| 804 |
if (routing_policy_rule_get(m, rule, family, &existing) < 0) |
| 805 |
return; |
| 806 |
|
| 807 |
if (!routing_policy_rule_can_update(existing, rule)) |
| 808 |
return; |
| 809 |
|
| 810 |
routing_policy_rule_unmark(existing); |
| 811 |
} |
| 812 |
|
| 813 |
int link_drop_routing_policy_rules(Link *link, bool only_static) { |
| 814 |
RoutingPolicyRule *rule; |
| 815 |
int r = 0; |
| 816 |
|
| 817 |
assert(link); |
| 818 |
assert(link->manager); |
| 819 |
|
| 820 |
/* First, mark all existing rules. */ |
| 821 |
SET_FOREACH(rule, link->manager->rules) { |
| 822 |
/* Do not touch rules managed by kernel. */ |
| 823 |
if (rule->protocol == RTPROT_KERNEL) |
| 824 |
continue; |
| 825 |
|
| 826 |
/* Ignore rules not assigned yet or already removing. */ |
| 827 |
if (!routing_policy_rule_exists(rule)) |
| 828 |
continue; |
| 829 |
|
| 830 |
if (!link_should_mark_config(link, only_static, rule->source, rule->protocol)) |
| 831 |
continue; |
| 832 |
|
| 833 |
routing_policy_rule_mark(rule); |
| 834 |
} |
| 835 |
|
| 836 |
/* Then, unmark all rules requested by active links. */ |
| 837 |
Link *other; |
| 838 |
HASHMAP_FOREACH(other, link->manager->links_by_index) { |
| 839 |
if (only_static && other == link) |
| 840 |
continue; |
| 841 |
|
| 842 |
if (!IN_SET(other->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) |
| 843 |
continue; |
| 844 |
|
| 845 |
HASHMAP_FOREACH(rule, other->network->rules_by_section) { |
| 846 |
if (IN_SET(rule->family, AF_INET, AF_INET6)) |
| 847 |
manager_unmark_routing_policy_rule(link->manager, rule, rule->family); |
| 848 |
else { |
| 849 |
assert(rule->address_family == ADDRESS_FAMILY_YES); |
| 850 |
manager_unmark_routing_policy_rule(link->manager, rule, AF_INET); |
| 851 |
manager_unmark_routing_policy_rule(link->manager, rule, AF_INET6); |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
/* Finally, remove all marked rules. */ |
| 857 |
SET_FOREACH(rule, link->manager->rules) { |
| 858 |
if (!routing_policy_rule_is_marked(rule)) |
| 859 |
continue; |
| 860 |
|
| 861 |
RET_GATHER(r, routing_policy_rule_remove(rule, link->manager)); |
| 862 |
} |
| 863 |
|
| 864 |
return r; |
| 865 |
} |
| 866 |
|
| 867 |
static int routing_policy_rule_is_ready_to_configure(const RoutingPolicyRule *rule, Link *link) { |
| 868 |
assert(rule); |
| 869 |
assert(link); |
| 870 |
assert(link->manager); |
| 871 |
|
| 872 |
/* For routing policy rules, it is not necessary to check operstate and friends of the interface. |
| 873 |
* Hence, here we refuse to configure rules only when the interface is already removed, or in the |
| 874 |
* failed state. */ |
| 875 |
if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) |
| 876 |
return false; |
| 877 |
|
| 878 |
/* Strictly speaking checking existence of IIF and OIF below is not necessary. But, configuring |
| 879 |
* routing policy rule with non-existent IIF or OIF is mostly meaningless, and 'ip rule' command |
| 880 |
* shows [detached] for such rules, that may confuse users. Let's postpone to configure if one of |
| 881 |
* IIF/OIF does not exist. */ |
| 882 |
|
| 883 |
if (rule->iif && !link_is_ready_to_configure_by_name(link->manager, rule->iif, /* allow_unmanaged= */ true)) |
| 884 |
return false; |
| 885 |
|
| 886 |
if (rule->oif && !link_is_ready_to_configure_by_name(link->manager, rule->oif, /* allow_unmanaged= */ true)) |
| 887 |
return false; |
| 888 |
|
| 889 |
return true; |
| 890 |
} |
| 891 |
|
| 892 |
static int routing_policy_rule_process_request(Request *req, Link *link, RoutingPolicyRule *rule) { |
| 893 |
RoutingPolicyRule *existing; |
| 894 |
int r; |
| 895 |
|
| 896 |
assert(req); |
| 897 |
assert(link); |
| 898 |
assert(link->manager); |
| 899 |
assert(rule); |
| 900 |
|
| 901 |
if (!routing_policy_rule_is_ready_to_configure(rule, link)) |
| 902 |
return 0; |
| 903 |
|
| 904 |
r = routing_policy_rule_configure(rule, link, req); |
| 905 |
if (r < 0) |
| 906 |
return log_link_warning_errno(link, r, "Failed to configure routing policy rule: %m"); |
| 907 |
|
| 908 |
routing_policy_rule_enter_configuring(rule); |
| 909 |
if (routing_policy_rule_get(link->manager, rule, rule->family, &existing) >= 0) |
| 910 |
routing_policy_rule_enter_configuring(existing); |
| 911 |
|
| 912 |
return 1; |
| 913 |
} |
| 914 |
|
| 915 |
static int static_routing_policy_rule_configure_handler( |
| 916 |
sd_netlink *rtnl, |
| 917 |
sd_netlink_message *m, |
| 918 |
Request *req, |
| 919 |
Link *link, |
| 920 |
RoutingPolicyRule *rule) { |
| 921 |
|
| 922 |
int r; |
| 923 |
|
| 924 |
assert(m); |
| 925 |
assert(link); |
| 926 |
assert(rule); |
| 927 |
|
| 928 |
r = sd_netlink_message_get_errno(m); |
| 929 |
if (r == -EEXIST) { |
| 930 |
RoutingPolicyRule *existing; |
| 931 |
|
| 932 |
if (routing_policy_rule_get(link->manager, rule, rule->family, &existing) >= 0) { |
| 933 |
existing->source = rule->source; |
| 934 |
routing_policy_rule_enter_configured(existing); |
| 935 |
} |
| 936 |
} else if (r < 0) { |
| 937 |
log_link_message_warning_errno(link, m, r, "Could not add routing policy rule"); |
| 938 |
link_enter_failed(link); |
| 939 |
return 1; |
| 940 |
} |
| 941 |
|
| 942 |
if (link->static_routing_policy_rule_messages == 0) { |
| 943 |
log_link_debug(link, "Routing policy rule configured."); |
| 944 |
link->static_routing_policy_rules_configured = true; |
| 945 |
link_check_ready(link); |
| 946 |
} |
| 947 |
|
| 948 |
return 1; |
| 949 |
} |
| 950 |
|
| 951 |
static int link_request_routing_policy_rule(Link *link, const RoutingPolicyRule *rule, int family) { |
| 952 |
_cleanup_(routing_policy_rule_unrefp) RoutingPolicyRule *tmp = NULL; |
| 953 |
RoutingPolicyRule *existing = NULL; |
| 954 |
int r; |
| 955 |
|
| 956 |
assert(link); |
| 957 |
assert(link->manager); |
| 958 |
assert(rule); |
| 959 |
assert(rule->source != NETWORK_CONFIG_SOURCE_FOREIGN); |
| 960 |
assert(rule->family == AF_UNSPEC || rule->family == family); |
| 961 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 962 |
|
| 963 |
if (routing_policy_rule_get_request(link->manager, rule, family, NULL) >= 0) |
| 964 |
return 0; /* already requested, skipping. */ |
| 965 |
|
| 966 |
r = routing_policy_rule_dup(rule, family, &tmp); |
| 967 |
if (r < 0) |
| 968 |
return r; |
| 969 |
|
| 970 |
if (routing_policy_rule_get(link->manager, tmp, family, &existing) < 0) { |
| 971 |
r = routing_policy_rule_acquire_priority(link->manager, tmp); |
| 972 |
if (r < 0) |
| 973 |
return r; |
| 974 |
} else { |
| 975 |
/* Copy priority from existing rule. */ |
| 976 |
if (!tmp->priority_set) { |
| 977 |
tmp->priority_set = true; |
| 978 |
tmp->priority = existing->priority; |
| 979 |
} |
| 980 |
|
| 981 |
/* Copy state for logging below. */ |
| 982 |
tmp->state = existing->state; |
| 983 |
} |
| 984 |
|
| 985 |
log_routing_policy_rule_debug(tmp, "Requesting", link, link->manager); |
| 986 |
r = link_queue_request_safe(link, REQUEST_TYPE_ROUTING_POLICY_RULE, |
| 987 |
tmp, |
| 988 |
routing_policy_rule_unref, |
| 989 |
routing_policy_rule_hash_func, |
| 990 |
routing_policy_rule_compare_func, |
| 991 |
routing_policy_rule_process_request, |
| 992 |
&link->static_routing_policy_rule_messages, |
| 993 |
static_routing_policy_rule_configure_handler, |
| 994 |
NULL); |
| 995 |
if (r <= 0) |
| 996 |
return r; |
| 997 |
|
| 998 |
routing_policy_rule_enter_requesting(tmp); |
| 999 |
if (existing) |
| 1000 |
routing_policy_rule_enter_requesting(existing); |
| 1001 |
|
| 1002 |
TAKE_PTR(tmp); |
| 1003 |
return 1; |
| 1004 |
} |
| 1005 |
|
| 1006 |
static int link_request_static_routing_policy_rule(Link *link, const RoutingPolicyRule *rule) { |
| 1007 |
int r; |
| 1008 |
|
| 1009 |
assert(link); |
| 1010 |
assert(rule); |
| 1011 |
|
| 1012 |
if (IN_SET(rule->family, AF_INET, AF_INET6)) |
| 1013 |
return link_request_routing_policy_rule(link, rule, rule->family); |
| 1014 |
|
| 1015 |
assert(rule->address_family == ADDRESS_FAMILY_YES); |
| 1016 |
|
| 1017 |
r = link_request_routing_policy_rule(link, rule, AF_INET); |
| 1018 |
if (r < 0) |
| 1019 |
return r; |
| 1020 |
|
| 1021 |
r = link_request_routing_policy_rule(link, rule, AF_INET6); |
| 1022 |
if (r < 0) |
| 1023 |
return r; |
| 1024 |
|
| 1025 |
return 0; |
| 1026 |
} |
| 1027 |
|
| 1028 |
int link_request_static_routing_policy_rules(Link *link) { |
| 1029 |
RoutingPolicyRule *rule; |
| 1030 |
int r; |
| 1031 |
|
| 1032 |
assert(link); |
| 1033 |
assert(link->network); |
| 1034 |
|
| 1035 |
link->static_routing_policy_rules_configured = false; |
| 1036 |
|
| 1037 |
HASHMAP_FOREACH(rule, link->network->rules_by_section) { |
| 1038 |
r = link_request_static_routing_policy_rule(link, rule); |
| 1039 |
if (r < 0) |
| 1040 |
return log_link_warning_errno(link, r, "Could not request routing policy rule: %m"); |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (link->static_routing_policy_rule_messages == 0) { |
| 1044 |
link->static_routing_policy_rules_configured = true; |
| 1045 |
link_check_ready(link); |
| 1046 |
} else { |
| 1047 |
log_link_debug(link, "Requesting routing policy rules."); |
| 1048 |
link_set_state(link, LINK_STATE_CONFIGURING); |
| 1049 |
} |
| 1050 |
|
| 1051 |
return 0; |
| 1052 |
} |
| 1053 |
|
| 1054 |
int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) { |
| 1055 |
int r; |
| 1056 |
|
| 1057 |
assert(rtnl); |
| 1058 |
assert(message); |
| 1059 |
|
| 1060 |
if (sd_netlink_message_is_error(message)) { |
| 1061 |
r = sd_netlink_message_get_errno(message); |
| 1062 |
if (r < 0) |
| 1063 |
log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring"); |
| 1064 |
|
| 1065 |
return 0; |
| 1066 |
} |
| 1067 |
|
| 1068 |
uint16_t type; |
| 1069 |
r = sd_netlink_message_get_type(message, &type); |
| 1070 |
if (r < 0) { |
| 1071 |
log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
| 1072 |
return 0; |
| 1073 |
} else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) { |
| 1074 |
log_warning("rtnl: received unexpected message type %u when processing rule, ignoring.", type); |
| 1075 |
return 0; |
| 1076 |
} |
| 1077 |
|
| 1078 |
_cleanup_(routing_policy_rule_unrefp) RoutingPolicyRule *tmp = NULL; |
| 1079 |
r = routing_policy_rule_new(&tmp); |
| 1080 |
if (r < 0) { |
| 1081 |
log_oom(); |
| 1082 |
return 0; |
| 1083 |
} |
| 1084 |
|
| 1085 |
r = sd_rtnl_message_get_family(message, &tmp->family); |
| 1086 |
if (r < 0) { |
| 1087 |
log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m"); |
| 1088 |
return 0; |
| 1089 |
} else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) { |
| 1090 |
log_debug("rtnl: received rule message with invalid family %d, ignoring.", tmp->family); |
| 1091 |
return 0; |
| 1092 |
} |
| 1093 |
|
| 1094 |
r = netlink_message_read_in_addr_union(message, FRA_SRC, tmp->family, &tmp->from.address); |
| 1095 |
if (r < 0 && r != -ENODATA) { |
| 1096 |
log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m"); |
| 1097 |
return 0; |
| 1098 |
} else if (r >= 0) { |
| 1099 |
r = sd_rtnl_message_routing_policy_rule_get_src_prefixlen(message, &tmp->from.prefixlen); |
| 1100 |
if (r < 0) { |
| 1101 |
log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m"); |
| 1102 |
return 0; |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
r = netlink_message_read_in_addr_union(message, FRA_DST, tmp->family, &tmp->to.address); |
| 1107 |
if (r < 0 && r != -ENODATA) { |
| 1108 |
log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m"); |
| 1109 |
return 0; |
| 1110 |
} else if (r >= 0) { |
| 1111 |
r = sd_rtnl_message_routing_policy_rule_get_dst_prefixlen(message, &tmp->to.prefixlen); |
| 1112 |
if (r < 0) { |
| 1113 |
log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m"); |
| 1114 |
return 0; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
r = sd_netlink_message_read_u32(message, FRA_FWMARK, &tmp->fwmark); |
| 1119 |
if (r < 0 && r != -ENODATA) { |
| 1120 |
log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m"); |
| 1121 |
return 0; |
| 1122 |
} |
| 1123 |
|
| 1124 |
r = sd_netlink_message_read_u32(message, FRA_FWMASK, &tmp->fwmask); |
| 1125 |
if (r < 0 && r != -ENODATA) { |
| 1126 |
log_warning_errno(r, "rtnl: could not get FRA_FWMASK attribute, ignoring: %m"); |
| 1127 |
return 0; |
| 1128 |
} |
| 1129 |
|
| 1130 |
r = sd_netlink_message_read_u32(message, FRA_PRIORITY, &tmp->priority); |
| 1131 |
if (r < 0 && r != -ENODATA) { |
| 1132 |
log_warning_errno(r, "rtnl: could not get FRA_PRIORITY attribute, ignoring: %m"); |
| 1133 |
return 0; |
| 1134 |
} |
| 1135 |
/* The kernel does not send priority if priority is zero. So, the flag below must be always set |
| 1136 |
* even if the message does not contain FRA_PRIORITY. */ |
| 1137 |
tmp->priority_set = true; |
| 1138 |
|
| 1139 |
r = sd_netlink_message_read_u32(message, FRA_TABLE, &tmp->table); |
| 1140 |
if (r < 0 && r != -ENODATA) { |
| 1141 |
log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m"); |
| 1142 |
return 0; |
| 1143 |
} |
| 1144 |
|
| 1145 |
r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tmp->tos); |
| 1146 |
if (r < 0 && r != -ENODATA) { |
| 1147 |
log_warning_errno(r, "rtnl: could not get FIB rule TOS, ignoring: %m"); |
| 1148 |
return 0; |
| 1149 |
} |
| 1150 |
|
| 1151 |
r = sd_rtnl_message_routing_policy_rule_get_action(message, &tmp->action); |
| 1152 |
if (r < 0 && r != -ENODATA) { |
| 1153 |
log_warning_errno(r, "rtnl: could not get FIB rule action, ignoring: %m"); |
| 1154 |
return 0; |
| 1155 |
} |
| 1156 |
|
| 1157 |
r = sd_netlink_message_read_string_strdup(message, FRA_IIFNAME, &tmp->iif); |
| 1158 |
if (r < 0 && r != -ENODATA) { |
| 1159 |
log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m"); |
| 1160 |
return 0; |
| 1161 |
} |
| 1162 |
|
| 1163 |
r = sd_netlink_message_read_string_strdup(message, FRA_OIFNAME, &tmp->oif); |
| 1164 |
if (r < 0 && r != -ENODATA) { |
| 1165 |
log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m"); |
| 1166 |
return 0; |
| 1167 |
} |
| 1168 |
|
| 1169 |
r = sd_netlink_message_read_u8(message, FRA_IP_PROTO, &tmp->ipproto); |
| 1170 |
if (r < 0 && r != -ENODATA) { |
| 1171 |
log_warning_errno(r, "rtnl: could not get FRA_IP_PROTO attribute, ignoring: %m"); |
| 1172 |
return 0; |
| 1173 |
} |
| 1174 |
|
| 1175 |
uint8_t l3mdev = 0; |
| 1176 |
r = sd_netlink_message_read_u8(message, FRA_L3MDEV, &l3mdev); |
| 1177 |
if (r < 0 && r != -ENODATA) { |
| 1178 |
log_warning_errno(r, "rtnl: could not get FRA_L3MDEV attribute, ignoring: %m"); |
| 1179 |
return 0; |
| 1180 |
} |
| 1181 |
tmp->l3mdev = l3mdev != 0; |
| 1182 |
|
| 1183 |
r = sd_netlink_message_read(message, FRA_SPORT_RANGE, sizeof(tmp->sport), &tmp->sport); |
| 1184 |
if (r < 0 && r != -ENODATA) { |
| 1185 |
log_warning_errno(r, "rtnl: could not get FRA_SPORT_RANGE attribute, ignoring: %m"); |
| 1186 |
return 0; |
| 1187 |
} |
| 1188 |
|
| 1189 |
r = sd_netlink_message_read(message, FRA_DPORT_RANGE, sizeof(tmp->dport), &tmp->dport); |
| 1190 |
if (r < 0 && r != -ENODATA) { |
| 1191 |
log_warning_errno(r, "rtnl: could not get FRA_DPORT_RANGE attribute, ignoring: %m"); |
| 1192 |
return 0; |
| 1193 |
} |
| 1194 |
|
| 1195 |
r = sd_netlink_message_read(message, FRA_UID_RANGE, sizeof(tmp->uid_range), &tmp->uid_range); |
| 1196 |
if (r < 0 && r != -ENODATA) { |
| 1197 |
log_warning_errno(r, "rtnl: could not get FRA_UID_RANGE attribute, ignoring: %m"); |
| 1198 |
return 0; |
| 1199 |
} |
| 1200 |
|
| 1201 |
uint32_t suppress_prefixlen; |
| 1202 |
r = sd_netlink_message_read_u32(message, FRA_SUPPRESS_PREFIXLEN, &suppress_prefixlen); |
| 1203 |
if (r < 0 && r != -ENODATA) { |
| 1204 |
log_warning_errno(r, "rtnl: could not get FRA_SUPPRESS_PREFIXLEN attribute, ignoring: %m"); |
| 1205 |
return 0; |
| 1206 |
} |
| 1207 |
if (r >= 0) |
| 1208 |
tmp->suppress_prefixlen = (int32_t) suppress_prefixlen; |
| 1209 |
|
| 1210 |
uint32_t suppress_ifgroup; |
| 1211 |
r = sd_netlink_message_read_u32(message, FRA_SUPPRESS_IFGROUP, &suppress_ifgroup); |
| 1212 |
if (r < 0 && r != -ENODATA) { |
| 1213 |
log_warning_errno(r, "rtnl: could not get FRA_SUPPRESS_IFGROUP attribute, ignoring: %m"); |
| 1214 |
return 0; |
| 1215 |
} |
| 1216 |
if (r >= 0) |
| 1217 |
tmp->suppress_ifgroup = (int32_t) suppress_ifgroup; |
| 1218 |
|
| 1219 |
r = sd_netlink_message_read_u64(message, FRA_TUN_ID, &tmp->tunnel_id); |
| 1220 |
if (r < 0 && r != -ENODATA) { |
| 1221 |
log_warning_errno(r, "rtnl: could not get FRA_TUN_ID attribute, ignoring: %m"); |
| 1222 |
return 0; |
| 1223 |
} |
| 1224 |
if (r >= 0) |
| 1225 |
tmp->tunnel_id = be64toh(tmp->tunnel_id); |
| 1226 |
|
| 1227 |
r = sd_netlink_message_read_u32(message, FRA_FLOW, &tmp->realms); |
| 1228 |
if (r < 0 && r != -ENODATA) { |
| 1229 |
log_warning_errno(r, "rtnl: could not get FRA_FLOW attribute, ignoring: %m"); |
| 1230 |
return 0; |
| 1231 |
} |
| 1232 |
|
| 1233 |
/* The kernel always sets the FRA_PROTOCOL attribute, and it is necessary for comparing rules. |
| 1234 |
* Hence, -ENODATA here is critical. */ |
| 1235 |
r = sd_netlink_message_read_u8(message, FRA_PROTOCOL, &tmp->protocol); |
| 1236 |
if (r < 0) { |
| 1237 |
log_warning_errno(r, "rtnl: could not get FRA_PROTOCOL attribute, ignoring: %m"); |
| 1238 |
return 0; |
| 1239 |
} |
| 1240 |
|
| 1241 |
RoutingPolicyRule *rule = NULL; |
| 1242 |
(void) routing_policy_rule_get(m, tmp, tmp->family, &rule); |
| 1243 |
|
| 1244 |
if (type == RTM_DELRULE) { |
| 1245 |
if (rule) |
| 1246 |
routing_policy_rule_forget(m, rule, "Forgetting removed"); |
| 1247 |
else |
| 1248 |
log_routing_policy_rule_debug(tmp, "Kernel removed unknown", NULL, m); |
| 1249 |
return 0; |
| 1250 |
} |
| 1251 |
|
| 1252 |
Request *req = NULL; |
| 1253 |
(void) routing_policy_rule_get_request(m, tmp, tmp->family, &req); |
| 1254 |
|
| 1255 |
bool is_new = false; |
| 1256 |
if (!rule) { |
| 1257 |
if (!req && !m->manage_foreign_rules) { |
| 1258 |
routing_policy_rule_enter_configured(tmp); |
| 1259 |
log_routing_policy_rule_debug(tmp, "Ignoring received", NULL, m); |
| 1260 |
return 0; |
| 1261 |
} |
| 1262 |
|
| 1263 |
/* If we did not know the rule, then save it. */ |
| 1264 |
r = routing_policy_rule_attach(m, tmp); |
| 1265 |
if (r < 0) { |
| 1266 |
log_warning_errno(r, "Failed to save received routing policy rule, ignoring: %m"); |
| 1267 |
return 0; |
| 1268 |
} |
| 1269 |
|
| 1270 |
rule = tmp; |
| 1271 |
is_new = true; |
| 1272 |
} |
| 1273 |
|
| 1274 |
/* Also update information that cannot be obtained through netlink notification. */ |
| 1275 |
if (req && req->waiting_reply) { |
| 1276 |
RoutingPolicyRule *req_rule = ASSERT_PTR(req->userdata); |
| 1277 |
|
| 1278 |
rule->source = req_rule->source; |
| 1279 |
} |
| 1280 |
|
| 1281 |
/* Then, update miscellaneous info from netlink notification. */ |
| 1282 |
r = sd_rtnl_message_routing_policy_rule_get_flags(message, &rule->flags); |
| 1283 |
if (r < 0) |
| 1284 |
log_debug_errno(r, "rtnl: received rule message without valid flag, ignoring: %m"); |
| 1285 |
|
| 1286 |
r = sd_netlink_message_read_u32(message, FRA_GOTO, &rule->priority_goto); |
| 1287 |
if (r < 0 && r != -ENODATA) |
| 1288 |
log_debug_errno(r, "rtnl: could not get FRA_GOTO attribute, ignoring: %m"); |
| 1289 |
|
| 1290 |
routing_policy_rule_enter_configured(rule); |
| 1291 |
if (req) |
| 1292 |
routing_policy_rule_enter_configured(req->userdata); |
| 1293 |
|
| 1294 |
log_routing_policy_rule_debug(rule, is_new ? "Remembering" : "Received remembered", NULL, m); |
| 1295 |
return 1; |
| 1296 |
} |
| 1297 |
|
| 1298 |
static int parse_fwmark_fwmask(const char *s, uint32_t *ret_fwmark, uint32_t *ret_fwmask) { |
| 1299 |
_cleanup_free_ char *fwmark_str = NULL; |
| 1300 |
uint32_t fwmark, fwmask = 0; |
| 1301 |
const char *slash; |
| 1302 |
int r; |
| 1303 |
|
| 1304 |
assert(s); |
| 1305 |
assert(ret_fwmark); |
| 1306 |
assert(ret_fwmask); |
| 1307 |
|
| 1308 |
slash = strchr(s, '/'); |
| 1309 |
if (slash) { |
| 1310 |
fwmark_str = strndup(s, slash - s); |
| 1311 |
if (!fwmark_str) |
| 1312 |
return -ENOMEM; |
| 1313 |
} |
| 1314 |
|
| 1315 |
r = safe_atou32(fwmark_str ?: s, &fwmark); |
| 1316 |
if (r < 0) |
| 1317 |
return r; |
| 1318 |
|
| 1319 |
if (slash) { |
| 1320 |
r = safe_atou32(slash + 1, &fwmask); |
| 1321 |
if (r < 0) |
| 1322 |
return r; |
| 1323 |
} else if (fwmark > 0) |
| 1324 |
fwmask = UINT32_MAX; |
| 1325 |
|
| 1326 |
*ret_fwmark = fwmark; |
| 1327 |
*ret_fwmask = fwmask; |
| 1328 |
|
| 1329 |
return 0; |
| 1330 |
} |
| 1331 |
|
| 1332 |
static int config_parse_routing_policy_rule_priority( |
| 1333 |
const char *unit, |
| 1334 |
const char *filename, |
| 1335 |
unsigned line, |
| 1336 |
const char *section, |
| 1337 |
unsigned section_line, |
| 1338 |
const char *lvalue, |
| 1339 |
int ltype, |
| 1340 |
const char *rvalue, |
| 1341 |
void *data, |
| 1342 |
void *userdata) { |
| 1343 |
|
| 1344 |
RoutingPolicyRule *rule = ASSERT_PTR(userdata); |
| 1345 |
int r; |
| 1346 |
|
| 1347 |
if (isempty(rvalue)) { |
| 1348 |
rule->priority = 0; |
| 1349 |
rule->priority_set = false; |
| 1350 |
return 1; |
| 1351 |
} |
| 1352 |
|
| 1353 |
r = safe_atou32(rvalue, &rule->priority); |
| 1354 |
if (r < 0) { |
| 1355 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse RPDB rule priority, ignoring: %s", rvalue); |
| 1356 |
return 0; |
| 1357 |
} |
| 1358 |
|
| 1359 |
rule->priority_set = true; |
| 1360 |
return 1; |
| 1361 |
} |
| 1362 |
|
| 1363 |
static int config_parse_routing_policy_rule_goto( |
| 1364 |
const char *unit, |
| 1365 |
const char *filename, |
| 1366 |
unsigned line, |
| 1367 |
const char *section, |
| 1368 |
unsigned section_line, |
| 1369 |
const char *lvalue, |
| 1370 |
int ltype, |
| 1371 |
const char *rvalue, |
| 1372 |
void *data, |
| 1373 |
void *userdata) { |
| 1374 |
|
| 1375 |
RoutingPolicyRule *rule = ASSERT_PTR(userdata); |
| 1376 |
uint32_t priority; |
| 1377 |
int r; |
| 1378 |
|
| 1379 |
assert(lvalue); |
| 1380 |
assert(rvalue); |
| 1381 |
|
| 1382 |
r = safe_atou32(rvalue, &priority); |
| 1383 |
if (r < 0) { |
| 1384 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=%s, ignoring assignment: %m", lvalue, rvalue); |
| 1385 |
return 0; |
| 1386 |
} |
| 1387 |
if (priority <= 0) { |
| 1388 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid goto target priority, ignoring assignment."); |
| 1389 |
return 0; |
| 1390 |
} |
| 1391 |
|
| 1392 |
rule->action = FR_ACT_GOTO; |
| 1393 |
rule->priority_goto = priority; |
| 1394 |
return 1; |
| 1395 |
} |
| 1396 |
|
| 1397 |
static int config_parse_routing_policy_rule_table( |
| 1398 |
const char *unit, |
| 1399 |
const char *filename, |
| 1400 |
unsigned line, |
| 1401 |
const char *section, |
| 1402 |
unsigned section_line, |
| 1403 |
const char *lvalue, |
| 1404 |
int ltype, |
| 1405 |
const char *rvalue, |
| 1406 |
void *data, |
| 1407 |
void *userdata) { |
| 1408 |
|
| 1409 |
RoutingPolicyRule *rule = ASSERT_PTR(userdata); |
| 1410 |
Manager *manager = ASSERT_PTR(ASSERT_PTR(rule->network)->manager); |
| 1411 |
uint32_t *table = ASSERT_PTR(data); |
| 1412 |
int r; |
| 1413 |
|
| 1414 |
assert(rvalue); |
| 1415 |
|
| 1416 |
r = manager_get_route_table_from_string(manager, rvalue, table); |
| 1417 |
if (r < 0) { |
| 1418 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1419 |
"Could not parse RPDB rule route table \"%s\", ignoring assignment: %m", rvalue); |
| 1420 |
return 0; |
| 1421 |
} |
| 1422 |
|
| 1423 |
return 1; |
| 1424 |
} |
| 1425 |
|
| 1426 |
static int config_parse_routing_policy_rule_fwmark( |
| 1427 |
const char *unit, |
| 1428 |
const char *filename, |
| 1429 |
unsigned line, |
| 1430 |
const char *section, |
| 1431 |
unsigned section_line, |
| 1432 |
const char *lvalue, |
| 1433 |
int ltype, |
| 1434 |
const char *rvalue, |
| 1435 |
void *data, |
| 1436 |
void *userdata) { |
| 1437 |
|
| 1438 |
RoutingPolicyRule *rule = ASSERT_PTR(userdata); |
| 1439 |
int r; |
| 1440 |
|
| 1441 |
assert(rvalue); |
| 1442 |
|
| 1443 |
r = parse_fwmark_fwmask(rvalue, &rule->fwmark, &rule->fwmask); |
| 1444 |
if (r < 0) { |
| 1445 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue); |
| 1446 |
return 0; |
| 1447 |
} |
| 1448 |
|
| 1449 |
return 1; |
| 1450 |
} |
| 1451 |
|
| 1452 |
static int config_parse_routing_policy_rule_port_range( |
| 1453 |
const char *unit, |
| 1454 |
const char *filename, |
| 1455 |
unsigned line, |
| 1456 |
const char *section, |
| 1457 |
unsigned section_line, |
| 1458 |
const char *lvalue, |
| 1459 |
int ltype, |
| 1460 |
const char *rvalue, |
| 1461 |
void *data, |
| 1462 |
void *userdata) { |
| 1463 |
|
| 1464 |
struct fib_rule_port_range *p = ASSERT_PTR(data); |
| 1465 |
int r; |
| 1466 |
|
| 1467 |
assert(rvalue); |
| 1468 |
|
| 1469 |
r = parse_ip_port_range(rvalue, &p->start, &p->end, /* allow_zero= */ false); |
| 1470 |
if (r < 0) { |
| 1471 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse routing policy rule port range '%s'", rvalue); |
| 1472 |
return 0; |
| 1473 |
} |
| 1474 |
|
| 1475 |
return 1; |
| 1476 |
} |
| 1477 |
|
| 1478 |
static int config_parse_routing_policy_rule_uid_range( |
| 1479 |
const char *unit, |
| 1480 |
const char *filename, |
| 1481 |
unsigned line, |
| 1482 |
const char *section, |
| 1483 |
unsigned section_line, |
| 1484 |
const char *lvalue, |
| 1485 |
int ltype, |
| 1486 |
const char *rvalue, |
| 1487 |
void *data, |
| 1488 |
void *userdata) { |
| 1489 |
|
| 1490 |
struct fib_rule_uid_range *p = ASSERT_PTR(data); |
| 1491 |
int r; |
| 1492 |
|
| 1493 |
assert(rvalue); |
| 1494 |
|
| 1495 |
if (get_user_creds(rvalue, /* flags= */ 0, NULL, &p->start, NULL, NULL, NULL) >= 0) { |
| 1496 |
p->end = p->start; |
| 1497 |
return 1; |
| 1498 |
} |
| 1499 |
|
| 1500 |
r = parse_uid_range(rvalue, &p->start, &p->end); |
| 1501 |
if (r < 0) { |
| 1502 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1503 |
"Invalid uid or uid range '%s', ignoring: %m", rvalue); |
| 1504 |
return 0; |
| 1505 |
} |
| 1506 |
|
| 1507 |
return 1; |
| 1508 |
} |
| 1509 |
|
| 1510 |
static int config_parse_routing_policy_rule_suppress( |
| 1511 |
const char *unit, |
| 1512 |
const char *filename, |
| 1513 |
unsigned line, |
| 1514 |
const char *section, |
| 1515 |
unsigned section_line, |
| 1516 |
const char *lvalue, |
| 1517 |
int ltype, |
| 1518 |
const char *rvalue, |
| 1519 |
void *data, |
| 1520 |
void *userdata) { |
| 1521 |
|
| 1522 |
int32_t val, *p = ASSERT_PTR(data); |
| 1523 |
int r; |
| 1524 |
|
| 1525 |
assert(lvalue); |
| 1526 |
|
| 1527 |
if (isempty(rvalue)) { |
| 1528 |
*p = -1; |
| 1529 |
return 1; |
| 1530 |
} |
| 1531 |
|
| 1532 |
r = safe_atoi32(rvalue, &val); |
| 1533 |
if (r < 0) { |
| 1534 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1535 |
"Failed to parse %s=%s, ignoring assignment: %m", lvalue, rvalue); |
| 1536 |
return 0; |
| 1537 |
} |
| 1538 |
if (val < 0 || val > ltype) { |
| 1539 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1540 |
"Invalid value specified to %s=, ignoring assignment: %s", lvalue, rvalue); |
| 1541 |
return 0; |
| 1542 |
} |
| 1543 |
|
| 1544 |
*p = val; |
| 1545 |
return 1; |
| 1546 |
} |
| 1547 |
|
| 1548 |
static int config_parse_routing_policy_rule_action( |
| 1549 |
const char *unit, |
| 1550 |
const char *filename, |
| 1551 |
unsigned line, |
| 1552 |
const char *section, |
| 1553 |
unsigned section_line, |
| 1554 |
const char *lvalue, |
| 1555 |
int ltype, |
| 1556 |
const char *rvalue, |
| 1557 |
void *data, |
| 1558 |
void *userdata) { |
| 1559 |
|
| 1560 |
uint8_t *p = ASSERT_PTR(data); |
| 1561 |
int r; |
| 1562 |
|
| 1563 |
assert(rvalue); |
| 1564 |
|
| 1565 |
r = fr_act_type_from_string(rvalue); |
| 1566 |
if (r < 0) { |
| 1567 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1568 |
"Could not parse FIB rule action \"%s\", ignoring assignment: %m", rvalue); |
| 1569 |
return 0; |
| 1570 |
} |
| 1571 |
|
| 1572 |
*p = (uint8_t) r; |
| 1573 |
return 1; |
| 1574 |
} |
| 1575 |
|
| 1576 |
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT( |
| 1577 |
config_parse_routing_policy_rule_family, |
| 1578 |
routing_policy_rule_address_family, |
| 1579 |
AddressFamily, |
| 1580 |
ADDRESS_FAMILY_NO); |
| 1581 |
|
| 1582 |
int config_parse_routing_policy_rule( |
| 1583 |
const char *unit, |
| 1584 |
const char *filename, |
| 1585 |
unsigned line, |
| 1586 |
const char *section, |
| 1587 |
unsigned section_line, |
| 1588 |
const char *lvalue, |
| 1589 |
int ltype, |
| 1590 |
const char *rvalue, |
| 1591 |
void *data, |
| 1592 |
void *userdata) { |
| 1593 |
|
| 1594 |
static const ConfigSectionParser table[_ROUTING_POLICY_RULE_CONF_PARSER_MAX] = { |
| 1595 |
[ROUTING_POLICY_RULE_IIF] = { .parser = config_parse_ifname, .ltype = 0, .offset = offsetof(RoutingPolicyRule, iif), }, |
| 1596 |
[ROUTING_POLICY_RULE_OIF] = { .parser = config_parse_ifname, .ltype = 0, .offset = offsetof(RoutingPolicyRule, oif), }, |
| 1597 |
[ROUTING_POLICY_RULE_FAMILY] = { .parser = config_parse_routing_policy_rule_family, .ltype = 0, .offset = offsetof(RoutingPolicyRule, address_family), }, |
| 1598 |
[ROUTING_POLICY_RULE_FWMARK] = { .parser = config_parse_routing_policy_rule_fwmark, .ltype = 0, .offset = 0, }, |
| 1599 |
[ROUTING_POLICY_RULE_GOTO] = { .parser = config_parse_routing_policy_rule_goto, .ltype = 0, .offset = 0, }, |
| 1600 |
[ROUTING_POLICY_RULE_INVERT] = { .parser = config_parse_uint32_flag, .ltype = FIB_RULE_INVERT, .offset = offsetof(RoutingPolicyRule, flags), }, |
| 1601 |
[ROUTING_POLICY_RULE_IP_PROTOCOL] = { .parser = config_parse_ip_protocol, .ltype = 0, .offset = offsetof(RoutingPolicyRule, ipproto), }, |
| 1602 |
[ROUTING_POLICY_RULE_L3MDEV] = { .parser = config_parse_bool, .ltype = 0, .offset = offsetof(RoutingPolicyRule, l3mdev), }, |
| 1603 |
[ROUTING_POLICY_RULE_SPORT] = { .parser = config_parse_routing_policy_rule_port_range, .ltype = 0, .offset = offsetof(RoutingPolicyRule, sport), }, |
| 1604 |
[ROUTING_POLICY_RULE_DPORT] = { .parser = config_parse_routing_policy_rule_port_range, .ltype = 0, .offset = offsetof(RoutingPolicyRule, dport), }, |
| 1605 |
[ROUTING_POLICY_RULE_FROM] = { .parser = config_parse_in_addr_prefix, .ltype = 0, .offset = offsetof(RoutingPolicyRule, from), }, |
| 1606 |
[ROUTING_POLICY_RULE_TO] = { .parser = config_parse_in_addr_prefix, .ltype = 0, .offset = offsetof(RoutingPolicyRule, to), }, |
| 1607 |
[ROUTING_POLICY_RULE_PRIORITY] = { .parser = config_parse_routing_policy_rule_priority, .ltype = 0, .offset = 0, }, |
| 1608 |
[ROUTING_POLICY_RULE_SUPPRESS_IFGROUP] = { .parser = config_parse_routing_policy_rule_suppress, .ltype = INT32_MAX, .offset = offsetof(RoutingPolicyRule, suppress_ifgroup), }, |
| 1609 |
[ROUTING_POLICY_RULE_SUPPRESS_PREFIXLEN] = { .parser = config_parse_routing_policy_rule_suppress, .ltype = 128, .offset = offsetof(RoutingPolicyRule, suppress_prefixlen), }, |
| 1610 |
[ROUTING_POLICY_RULE_TABLE] = { .parser = config_parse_routing_policy_rule_table, .ltype = 0, .offset = offsetof(RoutingPolicyRule, table), }, |
| 1611 |
[ROUTING_POLICY_RULE_TOS] = { .parser = config_parse_uint8, .ltype = 0, .offset = offsetof(RoutingPolicyRule, tos), }, |
| 1612 |
[ROUTING_POLICY_RULE_ACTION] = { .parser = config_parse_routing_policy_rule_action, .ltype = 0, .offset = offsetof(RoutingPolicyRule, action), }, |
| 1613 |
[ROUTING_POLICY_RULE_UID_RANGE] = { .parser = config_parse_routing_policy_rule_uid_range, .ltype = 0, .offset = offsetof(RoutingPolicyRule, uid_range), }, |
| 1614 |
}; |
| 1615 |
|
| 1616 |
_cleanup_(routing_policy_rule_unref_or_set_invalidp) RoutingPolicyRule *rule = NULL; |
| 1617 |
Network *network = ASSERT_PTR(userdata); |
| 1618 |
int r; |
| 1619 |
|
| 1620 |
assert(filename); |
| 1621 |
|
| 1622 |
r = routing_policy_rule_new_static(network, filename, section_line, &rule); |
| 1623 |
if (r < 0) |
| 1624 |
return log_oom(); |
| 1625 |
|
| 1626 |
r = config_section_parse(table, ELEMENTSOF(table), |
| 1627 |
unit, filename, line, section, section_line, lvalue, ltype, rvalue, rule); |
| 1628 |
if (r <= 0) /* 0 means non-critical error, but the section will be ignored. */ |
| 1629 |
return r; |
| 1630 |
|
| 1631 |
TAKE_PTR(rule); |
| 1632 |
return 0; |
| 1633 |
} |
| 1634 |
|
| 1635 |
#define log_rule_section(rule, fmt, ...) \ |
| 1636 |
({ \ |
| 1637 |
const RoutingPolicyRule *_rule = (rule); \ |
| 1638 |
log_section_warning_errno( \ |
| 1639 |
_rule ? _rule->section : NULL, \ |
| 1640 |
SYNTHETIC_ERRNO(EINVAL), \ |
| 1641 |
fmt " Ignoring [RoutingPolicyRule] section.", \ |
| 1642 |
##__VA_ARGS__); \ |
| 1643 |
}) |
| 1644 |
|
| 1645 |
static int routing_policy_rule_section_verify(RoutingPolicyRule *rule) { |
| 1646 |
assert(rule); |
| 1647 |
|
| 1648 |
if (section_is_invalid(rule->section)) |
| 1649 |
return -EINVAL; |
| 1650 |
|
| 1651 |
rule->family = rule->from.family; |
| 1652 |
if (rule->family == AF_UNSPEC) |
| 1653 |
rule->family = rule->to.family; |
| 1654 |
else if (rule->to.family != AF_UNSPEC && rule->to.family != rule->family) |
| 1655 |
return log_rule_section(rule, "From= and To= settings for routing policy rule contradict each other."); |
| 1656 |
|
| 1657 |
if ((rule->family == AF_INET && FLAGS_SET(rule->address_family, ADDRESS_FAMILY_IPV6)) || |
| 1658 |
(rule->family == AF_INET6 && FLAGS_SET(rule->address_family, ADDRESS_FAMILY_IPV4))) |
| 1659 |
return log_rule_section(rule, "Address family specified by Family= conflicts with To= and/or From=."); |
| 1660 |
|
| 1661 |
if (rule->family == AF_UNSPEC) { |
| 1662 |
if (IN_SET(rule->address_family, ADDRESS_FAMILY_IPV4, ADDRESS_FAMILY_NO)) |
| 1663 |
rule->family = AF_INET; |
| 1664 |
else if (rule->address_family == ADDRESS_FAMILY_IPV6) |
| 1665 |
rule->family = AF_INET6; |
| 1666 |
/* rule->family can be AF_UNSPEC only when Family=both. */ |
| 1667 |
} |
| 1668 |
|
| 1669 |
assert(IN_SET(rule->family, AF_INET, AF_INET6) || rule->address_family == ADDRESS_FAMILY_YES); |
| 1670 |
|
| 1671 |
if (rule->l3mdev) |
| 1672 |
rule->table = RT_TABLE_UNSPEC; |
| 1673 |
|
| 1674 |
if (rule->action == FR_ACT_GOTO) { |
| 1675 |
if (rule->priority_goto <= 0) |
| 1676 |
return log_rule_section(rule, "Type=goto is specified but the target priority GoTo= is unspecified."); |
| 1677 |
|
| 1678 |
if (rule->priority_set && rule->priority >= rule->priority_goto) |
| 1679 |
return log_rule_section(rule, "Goto target priority %"PRIu32" must be larger than the priority of this rule %"PRIu32".", |
| 1680 |
rule->priority_goto, rule->priority); |
| 1681 |
} |
| 1682 |
|
| 1683 |
return 0; |
| 1684 |
} |
| 1685 |
|
| 1686 |
void network_drop_invalid_routing_policy_rules(Network *network) { |
| 1687 |
RoutingPolicyRule *rule; |
| 1688 |
|
| 1689 |
assert(network); |
| 1690 |
|
| 1691 |
HASHMAP_FOREACH(rule, network->rules_by_section) |
| 1692 |
if (routing_policy_rule_section_verify(rule) < 0) |
| 1693 |
routing_policy_rule_detach(rule); |
| 1694 |
} |