From 30eab38020522eae93bc7b0530b1ccc2d8aceb16 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 23 Apr 2024 15:29:28 +0900 Subject: [PATCH 1/3] network/route: use our definitions of route preference Including linux/icmpv6.h easily trigger conflicts when another header includes netinet/icmp6.h. Let's drop the dependency and use our definition of the same values. --- src/network/networkd-route.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c index efe378a457..a4c56f1bbd 100644 --- a/src/network/networkd-route.c +++ b/src/network/networkd-route.c @@ -1,6 +1,5 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ -#include #include #include @@ -1810,11 +1809,11 @@ int config_parse_ipv6_route_preference( } if (streq(rvalue, "low")) - route->pref = ICMPV6_ROUTER_PREF_LOW; + route->pref = SD_NDISC_PREFERENCE_LOW; else if (streq(rvalue, "medium")) - route->pref = ICMPV6_ROUTER_PREF_MEDIUM; + route->pref = SD_NDISC_PREFERENCE_MEDIUM; else if (streq(rvalue, "high")) - route->pref = ICMPV6_ROUTER_PREF_HIGH; + route->pref = SD_NDISC_PREFERENCE_HIGH; else { log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown route preference: %s", rvalue); return 0; From 29d9672eb2884b9019f463ef219ca476987fe2f2 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 25 Apr 2024 09:41:26 +0900 Subject: [PATCH 2/3] network/radv: rename pref64Prefix -> Prefix64 Also this made several coding style cleanups. --- src/network/networkd-network.c | 2 +- src/network/networkd-radv.c | 38 +++++++++++++++++----------------- src/network/networkd-radv.h | 10 ++++----- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index 00a1138ab4..8232db06c9 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -795,7 +795,7 @@ static Network *network_free(Network *network) { hashmap_free_with_destructor(network->address_labels_by_section, address_label_free); hashmap_free_with_destructor(network->prefixes_by_section, prefix_free); hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free); - hashmap_free_with_destructor(network->pref64_prefixes_by_section, pref64_prefix_free); + hashmap_free_with_destructor(network->pref64_prefixes_by_section, prefix64_free); hashmap_free_with_destructor(network->rules_by_section, routing_policy_rule_free); hashmap_free_with_destructor(network->dhcp_static_leases_by_section, dhcp_static_lease_free); ordered_hashmap_free_with_destructor(network->sr_iov_by_section, sr_iov_free); diff --git a/src/network/networkd-radv.c b/src/network/networkd-radv.c index 3d6e9a128f..2a62089774 100644 --- a/src/network/networkd-radv.c +++ b/src/network/networkd-radv.c @@ -35,7 +35,7 @@ bool link_radv_enabled(Link *link) { return link->network->router_prefix_delegation; } -Prefix *prefix_free(Prefix *prefix) { +Prefix* prefix_free(Prefix *prefix) { if (!prefix) return NULL; @@ -93,7 +93,7 @@ static int prefix_new_static(Network *network, const char *filename, unsigned se return 0; } -RoutePrefix *route_prefix_free(RoutePrefix *prefix) { +RoutePrefix* route_prefix_free(RoutePrefix *prefix) { if (!prefix) return NULL; @@ -148,7 +148,7 @@ static int route_prefix_new_static(Network *network, const char *filename, unsig return 0; } -pref64Prefix *pref64_prefix_free(pref64Prefix *prefix) { +Prefix64* prefix64_free(Prefix64 *prefix) { if (!prefix) return NULL; @@ -162,11 +162,11 @@ pref64Prefix *pref64_prefix_free(pref64Prefix *prefix) { return mfree(prefix); } -DEFINE_SECTION_CLEANUP_FUNCTIONS(pref64Prefix, pref64_prefix_free); +DEFINE_SECTION_CLEANUP_FUNCTIONS(Prefix64, prefix64_free); -static int pref64_prefix_new_static(Network *network, const char *filename, unsigned section_line, pref64Prefix **ret) { +static int prefix64_new_static(Network *network, const char *filename, unsigned section_line, Prefix64 **ret) { _cleanup_(config_section_freep) ConfigSection *n = NULL; - _cleanup_(pref64_prefix_freep) pref64Prefix *prefix = NULL; + _cleanup_(prefix64_freep) Prefix64 *prefix = NULL; int r; assert(network); @@ -184,11 +184,11 @@ static int pref64_prefix_new_static(Network *network, const char *filename, unsi return 0; } - prefix = new(pref64Prefix, 1); + prefix = new(Prefix64, 1); if (!prefix) return -ENOMEM; - *prefix = (pref64Prefix) { + *prefix = (Prefix64) { .network = network, .section = TAKE_PTR(n), @@ -331,7 +331,7 @@ static int radv_set_route_prefix(Link *link, RoutePrefix *prefix) { return sd_radv_add_route_prefix(link->radv, p); } -static int radv_set_pref64_prefix(Link *link, pref64Prefix *prefix) { +static int radv_set_pref64_prefix(Link *link, Prefix64 *prefix) { _cleanup_(sd_radv_pref64_prefix_unrefp) sd_radv_pref64_prefix *p = NULL; int r; @@ -496,9 +496,6 @@ static int radv_find_uplink(Link *link, Link **ret) { static int radv_configure(Link *link) { Link *uplink = NULL; - RoutePrefix *q; - pref64Prefix *n; - Prefix *p; int r; assert(link); @@ -553,18 +550,21 @@ static int radv_configure(Link *link) { if (r < 0) return r; + Prefix *p; HASHMAP_FOREACH(p, link->network->prefixes_by_section) { r = radv_set_prefix(link, p); if (r < 0 && r != -EEXIST) return r; } + RoutePrefix *q; HASHMAP_FOREACH(q, link->network->route_prefixes_by_section) { r = radv_set_route_prefix(link, q); if (r < 0 && r != -EEXIST) return r; } + Prefix64 *n; HASHMAP_FOREACH(n, link->network->pref64_prefixes_by_section) { r = radv_set_pref64_prefix(link, n); if (r < 0 && r != -EEXIST) @@ -855,7 +855,7 @@ void network_adjust_radv(Network *network) { if (!FLAGS_SET(network->router_prefix_delegation, RADV_PREFIX_DELEGATION_STATIC)) { network->prefixes_by_section = hashmap_free_with_destructor(network->prefixes_by_section, prefix_free); network->route_prefixes_by_section = hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free); - network->pref64_prefixes_by_section = hashmap_free_with_destructor(network->pref64_prefixes_by_section, pref64_prefix_free); + network->pref64_prefixes_by_section = hashmap_free_with_destructor(network->pref64_prefixes_by_section, prefix64_free); } if (!network->router_prefix_delegation) @@ -883,10 +883,10 @@ void network_adjust_radv(Network *network) { if (route_prefix_section_verify(route) < 0) route_prefix_free(route); - pref64Prefix *pref64; + Prefix64 *pref64; HASHMAP_FOREACH(pref64, network->pref64_prefixes_by_section) if (section_is_invalid(pref64->section)) - pref64_prefix_free(pref64); + prefix64_free(pref64); } int config_parse_prefix( @@ -1190,7 +1190,7 @@ int config_parse_pref64_prefix( void *data, void *userdata) { - _cleanup_(pref64_prefix_free_or_set_invalidp) pref64Prefix *p = NULL; + _cleanup_(prefix64_free_or_set_invalidp) Prefix64 *p = NULL; Network *network = ASSERT_PTR(userdata); union in_addr_union a; uint8_t prefixlen; @@ -1201,7 +1201,7 @@ int config_parse_pref64_prefix( assert(lvalue); assert(rvalue); - r = pref64_prefix_new_static(network, filename, section_line, &p); + r = prefix64_new_static(network, filename, section_line, &p); if (r < 0) return log_oom(); @@ -1238,7 +1238,7 @@ int config_parse_pref64_prefix_lifetime( void *data, void *userdata) { - _cleanup_(pref64_prefix_free_or_set_invalidp) pref64Prefix *p = NULL; + _cleanup_(prefix64_free_or_set_invalidp) Prefix64 *p = NULL; Network *network = ASSERT_PTR(userdata); usec_t usec; int r; @@ -1248,7 +1248,7 @@ int config_parse_pref64_prefix_lifetime( assert(lvalue); assert(rvalue); - r = pref64_prefix_new_static(network, filename, section_line, &p); + r = prefix64_new_static(network, filename, section_line, &p); if (r < 0) return log_oom(); diff --git a/src/network/networkd-radv.h b/src/network/networkd-radv.h index c1ec6453ce..4cb747c73c 100644 --- a/src/network/networkd-radv.h +++ b/src/network/networkd-radv.h @@ -50,18 +50,18 @@ typedef struct RoutePrefix { usec_t lifetime; } RoutePrefix; -typedef struct pref64Prefix { +typedef struct Prefix64 { Network *network; ConfigSection *section; struct in6_addr prefix; uint8_t prefixlen; usec_t lifetime; -} pref64Prefix; +} Prefix64; -Prefix *prefix_free(Prefix *prefix); -RoutePrefix *route_prefix_free(RoutePrefix *prefix); -pref64Prefix *pref64_prefix_free(pref64Prefix *prefix); +Prefix* prefix_free(Prefix *prefix); +RoutePrefix* route_prefix_free(RoutePrefix *prefix); +Prefix64* prefix64_free(Prefix64 *prefix); void network_adjust_radv(Network *network); From d11ba681ab581ed9864a9c3ed27b2ddf9256c3aa Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 23 Apr 2024 15:30:22 +0900 Subject: [PATCH 3/3] network/radv: use sd_ndisc_prefix and friends to manage prefixes and so on No functional change, just refactoring. --- src/network/networkd-radv.c | 84 +++++++++++++++++++------------------ src/network/networkd-radv.h | 15 ++----- 2 files changed, 48 insertions(+), 51 deletions(-) diff --git a/src/network/networkd-radv.c b/src/network/networkd-radv.c index 2a62089774..652e7845fc 100644 --- a/src/network/networkd-radv.c +++ b/src/network/networkd-radv.c @@ -80,9 +80,11 @@ static int prefix_new_static(Network *network, const char *filename, unsigned se .network = network, .section = TAKE_PTR(n), - .flags = ND_OPT_PI_FLAG_ONLINK | ND_OPT_PI_FLAG_AUTO, - .preferred_lifetime = RADV_DEFAULT_PREFERRED_LIFETIME_USEC, - .valid_lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC, + .prefix.flags = ND_OPT_PI_FLAG_ONLINK | ND_OPT_PI_FLAG_AUTO, + .prefix.valid_lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC, + .prefix.preferred_lifetime = RADV_DEFAULT_PREFERRED_LIFETIME_USEC, + .prefix.valid_until = USEC_INFINITY, + .prefix.preferred_until = USEC_INFINITY, }; r = hashmap_ensure_put(&network->prefixes_by_section, &config_section_hash_ops, prefix->section, prefix); @@ -137,7 +139,8 @@ static int route_prefix_new_static(Network *network, const char *filename, unsig .network = network, .section = TAKE_PTR(n), - .lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC, + .route.lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC, + .route.valid_until = USEC_INFINITY, }; r = hashmap_ensure_put(&network->route_prefixes_by_section, &config_section_hash_ops, prefix->section, prefix); @@ -192,7 +195,8 @@ static int prefix64_new_static(Network *network, const char *filename, unsigned .network = network, .section = TAKE_PTR(n), - .lifetime = RADV_PREF64_DEFAULT_LIFETIME_USEC, + .prefix64.lifetime = RADV_PREF64_DEFAULT_LIFETIME_USEC, + .prefix64.valid_until = USEC_INFINITY, }; r = hashmap_ensure_put(&network->pref64_prefixes_by_section, &config_section_hash_ops, prefix->section, prefix); @@ -217,11 +221,11 @@ int link_request_radv_addresses(Link *link) { continue; /* radv_generate_addresses() below requires the prefix length <= 64. */ - if (p->prefixlen > 64) + if (p->prefix.prefixlen > 64) continue; _cleanup_hashmap_free_ Hashmap *tokens_by_address = NULL; - r = radv_generate_addresses(link, p->tokens, &p->prefix, p->prefixlen, &tokens_by_address); + r = radv_generate_addresses(link, p->tokens, &p->prefix.address, p->prefix.prefixlen, &tokens_by_address); if (r < 0) return r; @@ -237,7 +241,7 @@ int link_request_radv_addresses(Link *link) { address->source = NETWORK_CONFIG_SOURCE_STATIC; address->family = AF_INET6; address->in_addr.in6 = *a; - address->prefixlen = p->prefixlen; + address->prefixlen = p->prefix.prefixlen; address->route_metric = p->route_metric; address->token = ipv6_token_ref(token); @@ -285,23 +289,23 @@ static int radv_set_prefix(Link *link, Prefix *prefix) { if (r < 0) return r; - r = sd_radv_prefix_set_prefix(p, &prefix->prefix, prefix->prefixlen); + r = sd_radv_prefix_set_prefix(p, &prefix->prefix.address, prefix->prefix.prefixlen); if (r < 0) return r; - r = sd_radv_prefix_set_preferred_lifetime(p, prefix->preferred_lifetime, USEC_INFINITY); + r = sd_radv_prefix_set_preferred_lifetime(p, prefix->prefix.preferred_lifetime, prefix->prefix.preferred_until); if (r < 0) return r; - r = sd_radv_prefix_set_valid_lifetime(p, prefix->valid_lifetime, USEC_INFINITY); + r = sd_radv_prefix_set_valid_lifetime(p, prefix->prefix.valid_lifetime, prefix->prefix.valid_until); if (r < 0) return r; - r = sd_radv_prefix_set_onlink(p, FLAGS_SET(prefix->flags, ND_OPT_PI_FLAG_ONLINK)); + r = sd_radv_prefix_set_onlink(p, FLAGS_SET(prefix->prefix.flags, ND_OPT_PI_FLAG_ONLINK)); if (r < 0) return r; - r = sd_radv_prefix_set_address_autoconfiguration(p, FLAGS_SET(prefix->flags, ND_OPT_PI_FLAG_AUTO)); + r = sd_radv_prefix_set_address_autoconfiguration(p, FLAGS_SET(prefix->prefix.flags, ND_OPT_PI_FLAG_AUTO)); if (r < 0) return r; @@ -320,11 +324,11 @@ static int radv_set_route_prefix(Link *link, RoutePrefix *prefix) { if (r < 0) return r; - r = sd_radv_route_prefix_set_prefix(p, &prefix->prefix, prefix->prefixlen); + r = sd_radv_route_prefix_set_prefix(p, &prefix->route.address, prefix->route.prefixlen); if (r < 0) return r; - r = sd_radv_route_prefix_set_lifetime(p, prefix->lifetime, USEC_INFINITY); + r = sd_radv_route_prefix_set_lifetime(p, prefix->route.lifetime, prefix->route.valid_until); if (r < 0) return r; @@ -343,7 +347,7 @@ static int radv_set_pref64_prefix(Link *link, Prefix64 *prefix) { if (r < 0) return r; - r = sd_radv_pref64_prefix_set_prefix(p, &prefix->prefix, prefix->prefixlen, prefix->lifetime); + r = sd_radv_pref64_prefix_set_prefix(p, &prefix->prefix64.prefix, prefix->prefix64.prefixlen, prefix->prefix64.lifetime); if (r < 0) return r; @@ -781,36 +785,36 @@ static int prefix_section_verify(Prefix *p) { if (section_is_invalid(p->section)) return -EINVAL; - if (in6_addr_is_null(&p->prefix)) + if (in6_addr_is_null(&p->prefix.address)) return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "%s: [IPv6Prefix] section without Prefix= field configured, " "or specified prefix is the null address. " "Ignoring [IPv6Prefix] section from line %u.", p->section->filename, p->section->line); - if (p->prefixlen < 3 || p->prefixlen > 128) + if (p->prefix.prefixlen < 3 || p->prefix.prefixlen > 128) return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "%s: Invalid prefix length %u is specified in [IPv6Prefix] section. " "Valid range is 3…128. Ignoring [IPv6Prefix] section from line %u.", - p->section->filename, p->prefixlen, p->section->line); + p->section->filename, p->prefix.prefixlen, p->section->line); - if (p->prefixlen > 64) { + if (p->prefix.prefixlen > 64) { log_info("%s:%u: Unusual prefix length %u (> 64) is specified in [IPv6Prefix] section from line %s%s.", p->section->filename, p->section->line, - p->prefixlen, + p->prefix.prefixlen, p->assign ? ", refusing to assign an address in " : "", - p->assign ? IN6_ADDR_PREFIX_TO_STRING(&p->prefix, p->prefixlen) : ""); + p->assign ? IN6_ADDR_PREFIX_TO_STRING(&p->prefix.address, p->prefix.prefixlen) : ""); p->assign = false; } - if (p->preferred_lifetime > p->valid_lifetime) + if (p->prefix.preferred_lifetime > p->prefix.valid_lifetime) return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "%s: The preferred lifetime %s is longer than the valid lifetime %s. " "Ignoring [IPv6Prefix] section from line %u.", p->section->filename, - FORMAT_TIMESPAN(p->preferred_lifetime, USEC_PER_SEC), - FORMAT_TIMESPAN(p->valid_lifetime, USEC_PER_SEC), + FORMAT_TIMESPAN(p->prefix.preferred_lifetime, USEC_PER_SEC), + FORMAT_TIMESPAN(p->prefix.valid_lifetime, USEC_PER_SEC), p->section->line); return 0; @@ -820,11 +824,11 @@ static int route_prefix_section_verify(RoutePrefix *p) { if (section_is_invalid(p->section)) return -EINVAL; - if (p->prefixlen > 128) + if (p->route.prefixlen > 128) return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "%s: Invalid prefix length %u is specified in [IPv6RoutePrefix] section. " "Valid range is 0…128. Ignoring [IPv6RoutePrefix] section from line %u.", - p->section->filename, p->prefixlen, p->section->line); + p->section->filename, p->route.prefixlen, p->section->line); return 0; } @@ -915,15 +919,15 @@ int config_parse_prefix( if (r < 0) return log_oom(); - r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->prefixlen); + r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->prefix.prefixlen); if (r < 0) { log_syntax(unit, LOG_WARNING, filename, line, r, "Prefix is invalid, ignoring assignment: %s", rvalue); return 0; } - (void) in6_addr_mask(&a.in6, p->prefixlen); - p->prefix = a.in6; + (void) in6_addr_mask(&a.in6, p->prefix.prefixlen); + p->prefix.address = a.in6; TAKE_PTR(p); return 0; @@ -962,7 +966,7 @@ int config_parse_prefix_boolean( } if (ltype != 0) - SET_FLAG(p->flags, ltype, r); + SET_FLAG(p->prefix.flags, ltype, r); else { assert(streq(lvalue, "Assign")); p->assign = r; @@ -1012,9 +1016,9 @@ int config_parse_prefix_lifetime( } if (streq(lvalue, "PreferredLifetimeSec")) - p->preferred_lifetime = usec; + p->prefix.preferred_lifetime = usec; else if (streq(lvalue, "ValidLifetimeSec")) - p->valid_lifetime = usec; + p->prefix.valid_lifetime = usec; else assert_not_reached(); @@ -1119,15 +1123,15 @@ int config_parse_route_prefix( if (r < 0) return log_oom(); - r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->prefixlen); + r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->route.prefixlen); if (r < 0) { log_syntax(unit, LOG_WARNING, filename, line, r, "Route prefix is invalid, ignoring assignment: %s", rvalue); return 0; } - (void) in6_addr_mask(&a.in6, p->prefixlen); - p->prefix = a.in6; + (void) in6_addr_mask(&a.in6, p->route.prefixlen); + p->route.address = a.in6; TAKE_PTR(p); return 0; @@ -1172,7 +1176,7 @@ int config_parse_route_prefix_lifetime( return 0; } - p->lifetime = usec; + p->route.lifetime = usec; TAKE_PTR(p); return 0; @@ -1219,8 +1223,8 @@ int config_parse_pref64_prefix( } (void) in6_addr_mask(&a.in6, prefixlen); - p->prefix = a.in6; - p->prefixlen = prefixlen; + p->prefix64.prefix = a.in6; + p->prefix64.prefixlen = prefixlen; TAKE_PTR(p); return 0; @@ -1265,7 +1269,7 @@ int config_parse_pref64_prefix_lifetime( return 0; } - p->lifetime = usec; + p->prefix64.lifetime = usec; TAKE_PTR(p); return 0; diff --git a/src/network/networkd-radv.h b/src/network/networkd-radv.h index 4cb747c73c..ccdd656449 100644 --- a/src/network/networkd-radv.h +++ b/src/network/networkd-radv.h @@ -12,6 +12,7 @@ #include "in-addr-util.h" #include "conf-parser.h" +#include "ndisc-option.h" #include "networkd-util.h" typedef struct Link Link; @@ -30,11 +31,7 @@ typedef struct Prefix { Network *network; ConfigSection *section; - uint8_t flags; - uint8_t prefixlen; - struct in6_addr prefix; - usec_t preferred_lifetime; - usec_t valid_lifetime; + sd_ndisc_prefix prefix; bool assign; uint32_t route_metric; @@ -45,18 +42,14 @@ typedef struct RoutePrefix { Network *network; ConfigSection *section; - struct in6_addr prefix; - uint8_t prefixlen; - usec_t lifetime; + sd_ndisc_route route; } RoutePrefix; typedef struct Prefix64 { Network *network; ConfigSection *section; - struct in6_addr prefix; - uint8_t prefixlen; - usec_t lifetime; + sd_ndisc_prefix64 prefix64; } Prefix64; Prefix* prefix_free(Prefix *prefix);