HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/network/networkd-routing-policy-rule.c
Git blob: ac2dbbfe38a32ce5c46d67c4b6a69d2e8cc64c8f
Lines 1001–1695 of 1,695
TAKE_PTR(tmp);
return 1;
}
static int link_request_static_routing_policy_rule(Link *link, const RoutingPolicyRule *rule) {
int r;
assert(link);
assert(rule);
if (IN_SET(rule->family, AF_INET, AF_INET6))
return link_request_routing_policy_rule(link, rule, rule->family);
assert(rule->address_family == ADDRESS_FAMILY_YES);
r = link_request_routing_policy_rule(link, rule, AF_INET);
if (r < 0)
return r;
r = link_request_routing_policy_rule(link, rule, AF_INET6);
if (r < 0)
return r;
return 0;
}
int link_request_static_routing_policy_rules(Link *link) {
RoutingPolicyRule *rule;
int r;
assert(link);
assert(link->network);
link->static_routing_policy_rules_configured = false;
HASHMAP_FOREACH(rule, link->network->rules_by_section) {
r = link_request_static_routing_policy_rule(link, rule);
if (r < 0)
return log_link_warning_errno(link, r, "Could not request routing policy rule: %m");
}
if (link->static_routing_policy_rule_messages == 0) {
link->static_routing_policy_rules_configured = true;
link_check_ready(link);
} else {
log_link_debug(link, "Requesting routing policy rules.");
link_set_state(link, LINK_STATE_CONFIGURING);
}
return 0;
}
int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
int r;
assert(rtnl);
assert(message);
if (sd_netlink_message_is_error(message)) {
r = sd_netlink_message_get_errno(message);
if (r < 0)
log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring");
return 0;
}
uint16_t type;
r = sd_netlink_message_get_type(message, &type);
if (r < 0) {
log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
return 0;
} else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) {
log_warning("rtnl: received unexpected message type %u when processing rule, ignoring.", type);
return 0;
}
_cleanup_(routing_policy_rule_unrefp) RoutingPolicyRule *tmp = NULL;
r = routing_policy_rule_new(&tmp);
if (r < 0) {
log_oom();
return 0;
}
r = sd_rtnl_message_get_family(message, &tmp->family);
if (r < 0) {
log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m");
return 0;
} else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
log_debug("rtnl: received rule message with invalid family %d, ignoring.", tmp->family);
return 0;
}
r = netlink_message_read_in_addr_union(message, FRA_SRC, tmp->family, &tmp->from.address);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
return 0;
} else if (r >= 0) {
r = sd_rtnl_message_routing_policy_rule_get_src_prefixlen(message, &tmp->from.prefixlen);
if (r < 0) {
log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m");
return 0;
}
}
r = netlink_message_read_in_addr_union(message, FRA_DST, tmp->family, &tmp->to.address);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
return 0;
} else if (r >= 0) {
r = sd_rtnl_message_routing_policy_rule_get_dst_prefixlen(message, &tmp->to.prefixlen);
if (r < 0) {
log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m");
return 0;
}
}
r = sd_netlink_message_read_u32(message, FRA_FWMARK, &tmp->fwmark);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m");
return 0;
}
r = sd_netlink_message_read_u32(message, FRA_FWMASK, &tmp->fwmask);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_FWMASK attribute, ignoring: %m");
return 0;
}
r = sd_netlink_message_read_u32(message, FRA_PRIORITY, &tmp->priority);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_PRIORITY attribute, ignoring: %m");
return 0;
}
/* The kernel does not send priority if priority is zero. So, the flag below must be always set
* even if the message does not contain FRA_PRIORITY. */
tmp->priority_set = true;
r = sd_netlink_message_read_u32(message, FRA_TABLE, &tmp->table);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m");
return 0;
}
r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tmp->tos);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FIB rule TOS, ignoring: %m");
return 0;
}
r = sd_rtnl_message_routing_policy_rule_get_action(message, &tmp->action);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FIB rule action, ignoring: %m");
return 0;
}
r = sd_netlink_message_read_string_strdup(message, FRA_IIFNAME, &tmp->iif);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m");
return 0;
}
r = sd_netlink_message_read_string_strdup(message, FRA_OIFNAME, &tmp->oif);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m");
return 0;
}
r = sd_netlink_message_read_u8(message, FRA_IP_PROTO, &tmp->ipproto);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_IP_PROTO attribute, ignoring: %m");
return 0;
}
uint8_t l3mdev = 0;
r = sd_netlink_message_read_u8(message, FRA_L3MDEV, &l3mdev);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_L3MDEV attribute, ignoring: %m");
return 0;
}
tmp->l3mdev = l3mdev != 0;
r = sd_netlink_message_read(message, FRA_SPORT_RANGE, sizeof(tmp->sport), &tmp->sport);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_SPORT_RANGE attribute, ignoring: %m");
return 0;
}
r = sd_netlink_message_read(message, FRA_DPORT_RANGE, sizeof(tmp->dport), &tmp->dport);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_DPORT_RANGE attribute, ignoring: %m");
return 0;
}
r = sd_netlink_message_read(message, FRA_UID_RANGE, sizeof(tmp->uid_range), &tmp->uid_range);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_UID_RANGE attribute, ignoring: %m");
return 0;
}
uint32_t suppress_prefixlen;
r = sd_netlink_message_read_u32(message, FRA_SUPPRESS_PREFIXLEN, &suppress_prefixlen);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_SUPPRESS_PREFIXLEN attribute, ignoring: %m");
return 0;
}
if (r >= 0)
tmp->suppress_prefixlen = (int32_t) suppress_prefixlen;
uint32_t suppress_ifgroup;
r = sd_netlink_message_read_u32(message, FRA_SUPPRESS_IFGROUP, &suppress_ifgroup);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_SUPPRESS_IFGROUP attribute, ignoring: %m");
return 0;
}
if (r >= 0)
tmp->suppress_ifgroup = (int32_t) suppress_ifgroup;
r = sd_netlink_message_read_u64(message, FRA_TUN_ID, &tmp->tunnel_id);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_TUN_ID attribute, ignoring: %m");
return 0;
}
if (r >= 0)
tmp->tunnel_id = be64toh(tmp->tunnel_id);
r = sd_netlink_message_read_u32(message, FRA_FLOW, &tmp->realms);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_FLOW attribute, ignoring: %m");
return 0;
}
/* The kernel always sets the FRA_PROTOCOL attribute, and it is necessary for comparing rules.
* Hence, -ENODATA here is critical. */
r = sd_netlink_message_read_u8(message, FRA_PROTOCOL, &tmp->protocol);
if (r < 0) {
log_warning_errno(r, "rtnl: could not get FRA_PROTOCOL attribute, ignoring: %m");
return 0;
}
RoutingPolicyRule *rule = NULL;
(void) routing_policy_rule_get(m, tmp, tmp->family, &rule);
if (type == RTM_DELRULE) {
if (rule)
routing_policy_rule_forget(m, rule, "Forgetting removed");
else
log_routing_policy_rule_debug(tmp, "Kernel removed unknown", NULL, m);
return 0;
}
Request *req = NULL;
(void) routing_policy_rule_get_request(m, tmp, tmp->family, &req);
bool is_new = false;
if (!rule) {
if (!req && !m->manage_foreign_rules) {
routing_policy_rule_enter_configured(tmp);
log_routing_policy_rule_debug(tmp, "Ignoring received", NULL, m);
return 0;
}
/* If we did not know the rule, then save it. */
r = routing_policy_rule_attach(m, tmp);
if (r < 0) {
log_warning_errno(r, "Failed to save received routing policy rule, ignoring: %m");
return 0;
}
rule = tmp;
is_new = true;
}
/* Also update information that cannot be obtained through netlink notification. */
if (req && req->waiting_reply) {
RoutingPolicyRule *req_rule = ASSERT_PTR(req->userdata);
rule->source = req_rule->source;
}
/* Then, update miscellaneous info from netlink notification. */
r = sd_rtnl_message_routing_policy_rule_get_flags(message, &rule->flags);
if (r < 0)
log_debug_errno(r, "rtnl: received rule message without valid flag, ignoring: %m");
r = sd_netlink_message_read_u32(message, FRA_GOTO, &rule->priority_goto);
if (r < 0 && r != -ENODATA)
log_debug_errno(r, "rtnl: could not get FRA_GOTO attribute, ignoring: %m");
routing_policy_rule_enter_configured(rule);
if (req)
routing_policy_rule_enter_configured(req->userdata);
log_routing_policy_rule_debug(rule, is_new ? "Remembering" : "Received remembered", NULL, m);
return 1;
}
static int parse_fwmark_fwmask(const char *s, uint32_t *ret_fwmark, uint32_t *ret_fwmask) {
_cleanup_free_ char *fwmark_str = NULL;
uint32_t fwmark, fwmask = 0;
const char *slash;
int r;
assert(s);
assert(ret_fwmark);
assert(ret_fwmask);
slash = strchr(s, '/');
if (slash) {
fwmark_str = strndup(s, slash - s);
if (!fwmark_str)
return -ENOMEM;
}
r = safe_atou32(fwmark_str ?: s, &fwmark);
if (r < 0)
return r;
if (slash) {
r = safe_atou32(slash + 1, &fwmask);
if (r < 0)
return r;
} else if (fwmark > 0)
fwmask = UINT32_MAX;
*ret_fwmark = fwmark;
*ret_fwmask = fwmask;
return 0;
}
static int config_parse_routing_policy_rule_priority(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
RoutingPolicyRule *rule = ASSERT_PTR(userdata);
int r;
if (isempty(rvalue)) {
rule->priority = 0;
rule->priority_set = false;
return 1;
}
r = safe_atou32(rvalue, &rule->priority);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse RPDB rule priority, ignoring: %s", rvalue);
return 0;
}
rule->priority_set = true;
return 1;
}
static int config_parse_routing_policy_rule_goto(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
RoutingPolicyRule *rule = ASSERT_PTR(userdata);
uint32_t priority;
int r;
assert(lvalue);
assert(rvalue);
r = safe_atou32(rvalue, &priority);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=%s, ignoring assignment: %m", lvalue, rvalue);
return 0;
}
if (priority <= 0) {
log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid goto target priority, ignoring assignment.");
return 0;
}
rule->action = FR_ACT_GOTO;
rule->priority_goto = priority;
return 1;
}
static int config_parse_routing_policy_rule_table(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
RoutingPolicyRule *rule = ASSERT_PTR(userdata);
Manager *manager = ASSERT_PTR(ASSERT_PTR(rule->network)->manager);
uint32_t *table = ASSERT_PTR(data);
int r;
assert(rvalue);
r = manager_get_route_table_from_string(manager, rvalue, table);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Could not parse RPDB rule route table \"%s\", ignoring assignment: %m", rvalue);
return 0;
}
return 1;
}
static int config_parse_routing_policy_rule_fwmark(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
RoutingPolicyRule *rule = ASSERT_PTR(userdata);
int r;
assert(rvalue);
r = parse_fwmark_fwmask(rvalue, &rule->fwmark, &rule->fwmask);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse RPDB rule firewall mark or mask, ignoring: %s", rvalue);
return 0;
}
return 1;
}
static int config_parse_routing_policy_rule_port_range(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
struct fib_rule_port_range *p = ASSERT_PTR(data);
int r;
assert(rvalue);
r = parse_ip_port_range(rvalue, &p->start, &p->end, /* allow_zero= */ false);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse routing policy rule port range '%s'", rvalue);
return 0;
}
return 1;
}
static int config_parse_routing_policy_rule_uid_range(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
struct fib_rule_uid_range *p = ASSERT_PTR(data);
int r;
assert(rvalue);
if (get_user_creds(rvalue, /* flags= */ 0, NULL, &p->start, NULL, NULL, NULL) >= 0) {
p->end = p->start;
return 1;
}
r = parse_uid_range(rvalue, &p->start, &p->end);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid uid or uid range '%s', ignoring: %m", rvalue);
return 0;
}
return 1;
}
static int config_parse_routing_policy_rule_suppress(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int32_t val, *p = ASSERT_PTR(data);
int r;
assert(lvalue);
if (isempty(rvalue)) {
*p = -1;
return 1;
}
r = safe_atoi32(rvalue, &val);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse %s=%s, ignoring assignment: %m", lvalue, rvalue);
return 0;
}
if (val < 0 || val > ltype) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Invalid value specified to %s=, ignoring assignment: %s", lvalue, rvalue);
return 0;
}
*p = val;
return 1;
}
static int config_parse_routing_policy_rule_action(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
uint8_t *p = ASSERT_PTR(data);
int r;
assert(rvalue);
r = fr_act_type_from_string(rvalue);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Could not parse FIB rule action \"%s\", ignoring assignment: %m", rvalue);
return 0;
}
*p = (uint8_t) r;
return 1;
}
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(
config_parse_routing_policy_rule_family,
routing_policy_rule_address_family,
AddressFamily,
ADDRESS_FAMILY_NO);
int config_parse_routing_policy_rule(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
static const ConfigSectionParser table[_ROUTING_POLICY_RULE_CONF_PARSER_MAX] = {
[ROUTING_POLICY_RULE_IIF] = { .parser = config_parse_ifname, .ltype = 0, .offset = offsetof(RoutingPolicyRule, iif), },
[ROUTING_POLICY_RULE_OIF] = { .parser = config_parse_ifname, .ltype = 0, .offset = offsetof(RoutingPolicyRule, oif), },
[ROUTING_POLICY_RULE_FAMILY] = { .parser = config_parse_routing_policy_rule_family, .ltype = 0, .offset = offsetof(RoutingPolicyRule, address_family), },
[ROUTING_POLICY_RULE_FWMARK] = { .parser = config_parse_routing_policy_rule_fwmark, .ltype = 0, .offset = 0, },
[ROUTING_POLICY_RULE_GOTO] = { .parser = config_parse_routing_policy_rule_goto, .ltype = 0, .offset = 0, },
[ROUTING_POLICY_RULE_INVERT] = { .parser = config_parse_uint32_flag, .ltype = FIB_RULE_INVERT, .offset = offsetof(RoutingPolicyRule, flags), },
[ROUTING_POLICY_RULE_IP_PROTOCOL] = { .parser = config_parse_ip_protocol, .ltype = 0, .offset = offsetof(RoutingPolicyRule, ipproto), },
[ROUTING_POLICY_RULE_L3MDEV] = { .parser = config_parse_bool, .ltype = 0, .offset = offsetof(RoutingPolicyRule, l3mdev), },
[ROUTING_POLICY_RULE_SPORT] = { .parser = config_parse_routing_policy_rule_port_range, .ltype = 0, .offset = offsetof(RoutingPolicyRule, sport), },
[ROUTING_POLICY_RULE_DPORT] = { .parser = config_parse_routing_policy_rule_port_range, .ltype = 0, .offset = offsetof(RoutingPolicyRule, dport), },
[ROUTING_POLICY_RULE_FROM] = { .parser = config_parse_in_addr_prefix, .ltype = 0, .offset = offsetof(RoutingPolicyRule, from), },
[ROUTING_POLICY_RULE_TO] = { .parser = config_parse_in_addr_prefix, .ltype = 0, .offset = offsetof(RoutingPolicyRule, to), },
[ROUTING_POLICY_RULE_PRIORITY] = { .parser = config_parse_routing_policy_rule_priority, .ltype = 0, .offset = 0, },
[ROUTING_POLICY_RULE_SUPPRESS_IFGROUP] = { .parser = config_parse_routing_policy_rule_suppress, .ltype = INT32_MAX, .offset = offsetof(RoutingPolicyRule, suppress_ifgroup), },
[ROUTING_POLICY_RULE_SUPPRESS_PREFIXLEN] = { .parser = config_parse_routing_policy_rule_suppress, .ltype = 128, .offset = offsetof(RoutingPolicyRule, suppress_prefixlen), },
[ROUTING_POLICY_RULE_TABLE] = { .parser = config_parse_routing_policy_rule_table, .ltype = 0, .offset = offsetof(RoutingPolicyRule, table), },
[ROUTING_POLICY_RULE_TOS] = { .parser = config_parse_uint8, .ltype = 0, .offset = offsetof(RoutingPolicyRule, tos), },
[ROUTING_POLICY_RULE_ACTION] = { .parser = config_parse_routing_policy_rule_action, .ltype = 0, .offset = offsetof(RoutingPolicyRule, action), },
[ROUTING_POLICY_RULE_UID_RANGE] = { .parser = config_parse_routing_policy_rule_uid_range, .ltype = 0, .offset = offsetof(RoutingPolicyRule, uid_range), },
};
_cleanup_(routing_policy_rule_unref_or_set_invalidp) RoutingPolicyRule *rule = NULL;
Network *network = ASSERT_PTR(userdata);
int r;
assert(filename);
r = routing_policy_rule_new_static(network, filename, section_line, &rule);
if (r < 0)
return log_oom();
r = config_section_parse(table, ELEMENTSOF(table),
unit, filename, line, section, section_line, lvalue, ltype, rvalue, rule);
if (r <= 0) /* 0 means non-critical error, but the section will be ignored. */
return r;
TAKE_PTR(rule);
return 0;
}
#define log_rule_section(rule, fmt, ...) \
({ \
const RoutingPolicyRule *_rule = (rule); \
log_section_warning_errno( \
_rule ? _rule->section : NULL, \
SYNTHETIC_ERRNO(EINVAL), \
fmt " Ignoring [RoutingPolicyRule] section.", \
##__VA_ARGS__); \
})
static int routing_policy_rule_section_verify(RoutingPolicyRule *rule) {
assert(rule);
if (section_is_invalid(rule->section))
return -EINVAL;
rule->family = rule->from.family;
if (rule->family == AF_UNSPEC)
rule->family = rule->to.family;
else if (rule->to.family != AF_UNSPEC && rule->to.family != rule->family)
return log_rule_section(rule, "From= and To= settings for routing policy rule contradict each other.");
if ((rule->family == AF_INET && FLAGS_SET(rule->address_family, ADDRESS_FAMILY_IPV6)) ||
(rule->family == AF_INET6 && FLAGS_SET(rule->address_family, ADDRESS_FAMILY_IPV4)))
return log_rule_section(rule, "Address family specified by Family= conflicts with To= and/or From=.");
if (rule->family == AF_UNSPEC) {
if (IN_SET(rule->address_family, ADDRESS_FAMILY_IPV4, ADDRESS_FAMILY_NO))
rule->family = AF_INET;
else if (rule->address_family == ADDRESS_FAMILY_IPV6)
rule->family = AF_INET6;
/* rule->family can be AF_UNSPEC only when Family=both. */
}
assert(IN_SET(rule->family, AF_INET, AF_INET6) || rule->address_family == ADDRESS_FAMILY_YES);
if (rule->l3mdev)
rule->table = RT_TABLE_UNSPEC;
if (rule->action == FR_ACT_GOTO) {
if (rule->priority_goto <= 0)
return log_rule_section(rule, "Type=goto is specified but the target priority GoTo= is unspecified.");
if (rule->priority_set && rule->priority >= rule->priority_goto)
return log_rule_section(rule, "Goto target priority %"PRIu32" must be larger than the priority of this rule %"PRIu32".",
rule->priority_goto, rule->priority);
}
return 0;
}
void network_drop_invalid_routing_policy_rules(Network *network) {
RoutingPolicyRule *rule;
assert(network);
HASHMAP_FOREACH(rule, network->rules_by_section)
if (routing_policy_rule_section_verify(rule) < 0)
routing_policy_rule_detach(rule);
}