1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-09 01:18:19 +03:00

network/address-label: introduce generic conf parser for [IPv6AddressLabel] section

This also
- renames n -> label,
- use log_syntax_parse_error().

No functional change, just refactoring.
This commit is contained in:
Yu Watanabe 2024-08-28 13:57:05 +09:00
parent 152b8a4e71
commit 934cf1c06b
4 changed files with 79 additions and 57 deletions

View File

@ -336,7 +336,7 @@ void manager_drop_invalid_address_labels(Manager *manager) {
drop_invalid_address_labels(manager->address_labels_by_section);
}
int config_parse_address_label_prefix(
static int config_parse_ipv6_address_label_prefix(
const char *unit,
const char *filename,
unsigned line,
@ -348,35 +348,20 @@ int config_parse_address_label_prefix(
void *data,
void *userdata) {
_cleanup_(address_label_free_or_set_invalidp) AddressLabel *n = NULL;
Manager *manager = ltype ? userdata : NULL;
Network *network = ltype ? NULL : userdata;
AddressLabel *label = ASSERT_PTR(userdata);
unsigned char prefixlen;
union in_addr_union a;
int r;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
assert(userdata);
r = address_label_new_static(manager, network, filename, section_line, &n);
if (r < 0)
return log_oom();
if (isempty(rvalue)) {
n->prefix_set = false;
TAKE_PTR(n);
return 0;
label->prefix_set = false;
return 1;
}
r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &prefixlen);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid prefix for address label, ignoring assignment: %s", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
if (in6_addr_is_ipv4_mapped_address(&a.in6) && prefixlen > 96) {
/* See ip6addrlbl_alloc() in net/ipv6/addrlabel.c of kernel. */
log_syntax(unit, LOG_WARNING, filename, line, 0,
@ -385,14 +370,13 @@ int config_parse_address_label_prefix(
return 0;
}
n->prefix = a.in6;
n->prefixlen = prefixlen;
n->prefix_set = true;
TAKE_PTR(n);
return 0;
label->prefix = a.in6;
label->prefixlen = prefixlen;
label->prefix_set = true;
return 1;
}
int config_parse_address_label(
static int config_parse_ipv6_address_label(
const char *unit,
const char *filename,
unsigned line,
@ -404,40 +388,67 @@ int config_parse_address_label(
void *data,
void *userdata) {
_cleanup_(address_label_free_or_set_invalidp) AddressLabel *n = NULL;
Manager *manager = ltype ? userdata : NULL;
Network *network = ltype ? NULL : userdata;
uint32_t k;
uint32_t k, *label = ASSERT_PTR(data);
int r;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
assert(userdata);
r = address_label_new_static(manager, network, filename, section_line, &n);
if (r < 0)
return log_oom();
if (isempty(rvalue)) {
n->label = UINT32_MAX;
TAKE_PTR(n);
return 0;
*label = UINT32_MAX;
return 1;
}
r = safe_atou32(rvalue, &k);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse address label, ignoring: %s", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
if (k == UINT32_MAX) {
log_syntax(unit, LOG_WARNING, filename, line, 0, "Address label is invalid, ignoring: %s", rvalue);
return 0;
}
n->label = k;
TAKE_PTR(n);
*label = k;
return 1;
}
int config_parse_ipv6_address_label_section(
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[_IPV6_ADDRESS_LABEL_CONF_PARSER_MAX] = {
[IPV6_ADDRESS_LABEL] = { .parser = config_parse_ipv6_address_label, .ltype = 0, .offset = offsetof(AddressLabel, label), },
[IPV6_ADDRESS_LABEL_PREFIX] = { .parser = config_parse_ipv6_address_label_prefix, .ltype = 0, .offset = 0, },
};
_cleanup_(address_label_free_or_set_invalidp) AddressLabel *label = NULL;
Manager *manager = NULL;
Network *network = NULL;
int r;
assert(filename);
if (FLAGS_SET(ltype, IPV6_ADDRESS_LABEL_BY_MANAGER))
manager = ASSERT_PTR(userdata);
else
network = ASSERT_PTR(userdata);
ltype &= IPV6_ADDRESS_LABEL_SECTION_MASK;
r = address_label_new_static(manager, network, filename, section_line, &label);
if (r < 0)
return log_oom();
r = config_section_parse(table, ELEMENTSOF(table),
unit, filename, line, section, section_line, lvalue, ltype, rvalue, label);
if (r <= 0)
return r;
TAKE_PTR(label);
return 0;
}

View File

@ -30,5 +30,16 @@ void manager_drop_invalid_address_labels(Manager *manager);
int link_request_static_address_labels(Link *link);
int manager_request_static_address_labels(Manager *manager);
CONFIG_PARSER_PROTOTYPE(config_parse_address_label);
CONFIG_PARSER_PROTOTYPE(config_parse_address_label_prefix);
typedef enum IPv6AddressLabelConfParserType {
IPV6_ADDRESS_LABEL,
IPV6_ADDRESS_LABEL_PREFIX,
_IPV6_ADDRESS_LABEL_CONF_PARSER_MAX,
_IPV6_ADDRESS_LABEL_CONF_PARSER_INVALID = -EINVAL,
IPV6_ADDRESS_LABEL_BY_MANAGER = 1 << 16,
IPV6_ADDRESS_LABEL_SECTION_MASK = IPV6_ADDRESS_LABEL_BY_MANAGER - 1,
} IPv6AddressLabelConfParserType;
assert_cc(IPV6_ADDRESS_LABEL_BY_MANAGER >= _IPV6_ADDRESS_LABEL_CONF_PARSER_MAX);
CONFIG_PARSER_PROTOTYPE(config_parse_ipv6_address_label_section);

View File

@ -34,8 +34,8 @@ Network.IPv6Forwarding, config_parse_tristate,
Network.IPv6PrivacyExtensions, config_parse_ipv6_privacy_extensions, 0, offsetof(Manager, ipv6_privacy_extensions)
Network.UseDomains, config_parse_use_domains, 0, offsetof(Manager, use_domains)
IPv6AcceptRA.UseDomains, config_parse_use_domains, 0, offsetof(Manager, ndisc_use_domains)
IPv6AddressLabel.Prefix, config_parse_address_label_prefix, 1, 0
IPv6AddressLabel.Label, config_parse_address_label, 1, 0
IPv6AddressLabel.Prefix, config_parse_ipv6_address_label_section, IPV6_ADDRESS_LABEL_BY_MANAGER | IPV6_ADDRESS_LABEL_PREFIX, 0
IPv6AddressLabel.Label, config_parse_ipv6_address_label_section, IPV6_ADDRESS_LABEL_BY_MANAGER | IPV6_ADDRESS_LABEL, 0
DHCPv4.UseDomains, config_parse_use_domains, 0, offsetof(Manager, dhcp_use_domains)
DHCPv4.DUIDType, config_parse_duid_type, 0, offsetof(Manager, dhcp_duid)
DHCPv4.DUIDRawData, config_parse_duid_rawdata, 0, offsetof(Manager, dhcp_duid)

View File

@ -171,8 +171,8 @@ Address.Scope, config_parse_address_scope,
Address.RouteMetric, config_parse_address_route_metric, 0, 0
Address.NetLabel, config_parse_address_netlabel, 0, 0
Address.NFTSet, config_parse_address_ip_nft_set, NFT_SET_PARSE_NETWORK, 0
IPv6AddressLabel.Prefix, config_parse_address_label_prefix, 0, 0
IPv6AddressLabel.Label, config_parse_address_label, 0, 0
IPv6AddressLabel.Prefix, config_parse_ipv6_address_label_section, IPV6_ADDRESS_LABEL_PREFIX, 0
IPv6AddressLabel.Label, config_parse_ipv6_address_label_section, IPV6_ADDRESS_LABEL, 0
Neighbor.Address, config_parse_neighbor_address, 0, 0
Neighbor.LinkLayerAddress, config_parse_neighbor_lladdr, 0, 0
Neighbor.MACAddress, config_parse_neighbor_lladdr, 0, 0 /* deprecated */