summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2024-08-28 07:01:36 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2024-09-06 03:37:23 +0200
commit6cf41d4603da48064e9349d62ab0ae04b8622a64 (patch)
treeff164d4b97697ac7521c77c795678c2214e94a94 /src
parentfirewall-util: several cleanups for config_parse_nft_set() (diff)
downloadsystemd-6cf41d4603da48064e9349d62ab0ae04b8622a64.tar.xz
systemd-6cf41d4603da48064e9349d62ab0ae04b8622a64.zip
network/address: introduce generic config parser for [Address] section
Then, use generic conf parsers defined in conf-parser.[ch].
Diffstat (limited to 'src')
-rw-r--r--src/network/networkd-address.c159
-rw-r--r--src/network/networkd-address.h18
-rw-r--r--src/network/networkd-network-gperf.gperf16
3 files changed, 41 insertions, 152 deletions
diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index 7377c22407..c2342c641a 100644
--- a/src/network/networkd-address.c
+++ b/src/network/networkd-address.c
@@ -2280,53 +2280,6 @@ int config_parse_lifetime(
return 0;
}
-int config_parse_address_flags(
- 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) {
-
- Network *network = userdata;
- _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
- int r;
-
- assert(filename);
- assert(section);
- assert(lvalue);
- assert(rvalue);
- assert(data);
-
- r = address_new_static(network, filename, section_line, &n);
- if (r == -ENOMEM)
- return log_oom();
- if (r < 0) {
- log_syntax(unit, LOG_WARNING, filename, line, r,
- "Failed to allocate new address, ignoring assignment: %m");
- return 0;
- }
-
- r = parse_boolean(rvalue);
- if (r < 0) {
- log_syntax(unit, LOG_WARNING, filename, line, r,
- "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
- return 0;
- }
-
- if (streq(lvalue, "AddPrefixRoute"))
- r = !r;
-
- SET_FLAG(n->flags, ltype, r);
-
- TAKE_PTR(n);
- return 0;
-}
-
int config_parse_address_scope(
const char *unit,
const char *filename,
@@ -2371,48 +2324,6 @@ int config_parse_address_scope(
return 0;
}
-int config_parse_address_route_metric(
- 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) {
-
- Network *network = userdata;
- _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
- int r;
-
- assert(filename);
- assert(section);
- assert(lvalue);
- assert(rvalue);
- assert(data);
-
- r = address_new_static(network, filename, section_line, &n);
- if (r == -ENOMEM)
- return log_oom();
- if (r < 0) {
- log_syntax(unit, LOG_WARNING, filename, line, r,
- "Failed to allocate new address, ignoring assignment: %m");
- return 0;
- }
-
- r = safe_atou32(rvalue, &n->route_metric);
- if (r < 0) {
- log_syntax(unit, LOG_WARNING, filename, line, r,
- "Could not parse %s=, ignoring assignment: %s", lvalue, rvalue);
- return 0;
- }
-
- TAKE_PTR(n);
- return 0;
-}
-
int config_parse_duplicate_address_detection(
const char *unit,
const char *filename,
@@ -2467,7 +2378,7 @@ int config_parse_duplicate_address_detection(
return 0;
}
-int config_parse_address_netlabel(
+int config_parse_address_section(
const char *unit,
const char *filename,
unsigned line,
@@ -2479,18 +2390,24 @@ int config_parse_address_netlabel(
void *data,
void *userdata) {
- Network *network = userdata;
- _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
+ static const ConfigSectionParser table[_ADDRESS_CONF_PARSER_MAX] = {
+ [ADDRESS_HOME_ADDRESS] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_HOMEADDRESS, .offset = offsetof(Address, flags), },
+ [ADDRESS_MANAGE_TEMPORARY_ADDRESS] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_MANAGETEMPADDR, .offset = offsetof(Address, flags), },
+ [ADDRESS_PREFIX_ROUTE] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_NOPREFIXROUTE, .offset = offsetof(Address, flags), },
+ [ADDRESS_ADD_PREFIX_ROUTE] = { .parser = config_parse_uint32_invert_flag, .ltype = IFA_F_NOPREFIXROUTE, .offset = offsetof(Address, flags), },
+ [ADDRESS_AUTO_JOIN] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_MCAUTOJOIN, .offset = offsetof(Address, flags), },
+ [ADDRESS_ROUTE_METRIC] = { .parser = config_parse_uint32, .ltype = 0, .offset = offsetof(Address, route_metric), },
+ [ADDRESS_NET_LABEL] = { .parser = config_parse_string, .ltype = CONFIG_PARSE_STRING_SAFE, .offset = offsetof(Address, netlabel), },
+ [ADDRESS_NFT_SET] = { .parser = config_parse_nft_set, .ltype = NFT_SET_PARSE_NETWORK, .offset = offsetof(Address, nft_set_context), },
+ };
+
+ _cleanup_(address_unref_or_set_invalidp) Address *address = NULL;
+ Network *network = ASSERT_PTR(userdata);
int r;
assert(filename);
- assert(section);
- assert(lvalue);
- assert(rvalue);
- assert(data);
- assert(network);
- r = address_new_static(network, filename, section_line, &n);
+ r = address_new_static(network, filename, section_line, &address);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
@@ -2499,12 +2416,12 @@ int config_parse_address_netlabel(
return 0;
}
- r = config_parse_string(unit, filename, line, section, section_line,
- lvalue, CONFIG_PARSE_STRING_SAFE, rvalue, &n->netlabel, network);
- if (r < 0)
+ r = config_section_parse(table, ELEMENTSOF(table),
+ unit, filename, line, section, section_line, lvalue, ltype, rvalue, address);
+ if (r <= 0)
return r;
- TAKE_PTR(n);
+ TAKE_PTR(address);
return 0;
}
@@ -2655,41 +2572,3 @@ int network_drop_invalid_addresses(Network *network) {
return 0;
}
-
-int config_parse_address_ip_nft_set(
- 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) {
-
- Network *network = userdata;
- _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
- int r;
-
- assert(filename);
- assert(lvalue);
- assert(rvalue);
- assert(network);
-
- r = address_new_static(network, filename, section_line, &n);
- if (r == -ENOMEM)
- return log_oom();
- if (r < 0) {
- log_syntax(unit, LOG_WARNING, filename, line, r,
- "Failed to allocate a new address, ignoring assignment: %m");
- return 0;
- }
-
- r = config_parse_nft_set(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &n->nft_set_context, network);
- if (r < 0)
- return r;
-
- TAKE_PTR(n);
- return 0;
-}
diff --git a/src/network/networkd-address.h b/src/network/networkd-address.h
index fcad445e17..0c4a4d166e 100644
--- a/src/network/networkd-address.h
+++ b/src/network/networkd-address.h
@@ -159,13 +159,23 @@ DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(Address, address);
void link_mark_addresses(Link *link, NetworkConfigSource source);
+typedef enum AddressConfParserType {
+ ADDRESS_HOME_ADDRESS,
+ ADDRESS_MANAGE_TEMPORARY_ADDRESS,
+ ADDRESS_PREFIX_ROUTE,
+ ADDRESS_ADD_PREFIX_ROUTE,
+ ADDRESS_AUTO_JOIN,
+ ADDRESS_ROUTE_METRIC,
+ ADDRESS_NET_LABEL,
+ ADDRESS_NFT_SET,
+ _ADDRESS_CONF_PARSER_MAX,
+ _ADDRESS_CONF_PARSER_INVALID = -EINVAL,
+} AddressConfParserType;
+
+CONFIG_PARSER_PROTOTYPE(config_parse_address_section);
CONFIG_PARSER_PROTOTYPE(config_parse_address);
CONFIG_PARSER_PROTOTYPE(config_parse_broadcast);
CONFIG_PARSER_PROTOTYPE(config_parse_label);
CONFIG_PARSER_PROTOTYPE(config_parse_lifetime);
-CONFIG_PARSER_PROTOTYPE(config_parse_address_flags);
CONFIG_PARSER_PROTOTYPE(config_parse_address_scope);
-CONFIG_PARSER_PROTOTYPE(config_parse_address_route_metric);
CONFIG_PARSER_PROTOTYPE(config_parse_duplicate_address_detection);
-CONFIG_PARSER_PROTOTYPE(config_parse_address_netlabel);
-CONFIG_PARSER_PROTOTYPE(config_parse_address_ip_nft_set);
diff --git a/src/network/networkd-network-gperf.gperf b/src/network/networkd-network-gperf.gperf
index e39c617500..d6a610e958 100644
--- a/src/network/networkd-network-gperf.gperf
+++ b/src/network/networkd-network-gperf.gperf
@@ -161,16 +161,16 @@ Address.Peer, config_parse_address,
Address.Broadcast, config_parse_broadcast, 0, 0
Address.Label, config_parse_label, 0, 0
Address.PreferredLifetime, config_parse_lifetime, 0, 0
-Address.HomeAddress, config_parse_address_flags, IFA_F_HOMEADDRESS, 0
-Address.ManageTemporaryAddress, config_parse_address_flags, IFA_F_MANAGETEMPADDR, 0
-Address.PrefixRoute, config_parse_address_flags, IFA_F_NOPREFIXROUTE, 0 /* deprecated */
-Address.AddPrefixRoute, config_parse_address_flags, IFA_F_NOPREFIXROUTE, 0
-Address.AutoJoin, config_parse_address_flags, IFA_F_MCAUTOJOIN, 0
+Address.HomeAddress, config_parse_address_section, ADDRESS_HOME_ADDRESS, 0
+Address.ManageTemporaryAddress, config_parse_address_section, ADDRESS_MANAGE_TEMPORARY_ADDRESS, 0
+Address.PrefixRoute, config_parse_address_section, ADDRESS_PREFIX_ROUTE, 0 /* deprecated */
+Address.AddPrefixRoute, config_parse_address_section, ADDRESS_ADD_PREFIX_ROUTE, 0
+Address.AutoJoin, config_parse_address_section, ADDRESS_AUTO_JOIN, 0
Address.DuplicateAddressDetection, config_parse_duplicate_address_detection, 0, 0
Address.Scope, config_parse_address_scope, 0, 0
-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
+Address.RouteMetric, config_parse_address_section, ADDRESS_ROUTE_METRIC, 0
+Address.NetLabel, config_parse_address_section, ADDRESS_NET_LABEL, 0
+Address.NFTSet, config_parse_address_section, ADDRESS_NFT_SET, 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