]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: use in_addr_prefix_to_string()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 17 Feb 2021 18:22:28 +0000 (03:22 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 17 Feb 2021 18:48:07 +0000 (03:48 +0900)
src/libsystemd-network/sd-radv.c
src/network/networkd-address-pool.c
src/network/networkd-address.c
src/network/networkd-dhcp6.c
src/network/networkd-link.c
src/network/networkd-ndisc.c
src/network/networkd-route.c
src/network/networkd-routing-policy-rule.c

index b34263568e109fd380dbdde0cd025a1234c5342a..eac9b159ce0f055512570bf8ab0c755ed7643e53 100644 (file)
@@ -539,6 +539,10 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) {
         if (in6_addr_is_null(&p->opt.in6_addr))
                 return -ENOEXEC;
 
+        (void) in_addr_prefix_to_string(AF_INET6,
+                                        (union in_addr_union*) &p->opt.in6_addr,
+                                        p->opt.prefixlen, &addr_p);
+
         LIST_FOREACH(prefix, cur, ra->prefixes) {
 
                 r = in_addr_prefix_intersect(AF_INET6,
@@ -546,25 +550,22 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) {
                                              cur->opt.prefixlen,
                                              (union in_addr_union*) &p->opt.in6_addr,
                                              p->opt.prefixlen);
-                if (r > 0) {
-                        _cleanup_free_ char *addr_cur = NULL;
-
-                        (void) in_addr_to_string(AF_INET6,
-                                                 (union in_addr_union*) &p->opt.in6_addr,
-                                                 &addr_p);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        continue;
 
-                        if (dynamic && cur->opt.prefixlen == p->opt.prefixlen)
-                                goto update;
+                if (dynamic && cur->opt.prefixlen == p->opt.prefixlen)
+                        goto update;
 
-                        (void) in_addr_to_string(AF_INET6,
-                                                 (union in_addr_union*) &cur->opt.in6_addr,
-                                                 &addr_cur);
-                        log_radv("IPv6 prefix %s/%u already configured, ignoring %s/%u",
-                                 addr_cur, cur->opt.prefixlen,
-                                 addr_p, p->opt.prefixlen);
+                _cleanup_free_ char *addr_cur = NULL;
+                (void) in_addr_prefix_to_string(AF_INET6,
+                                                (union in_addr_union*) &cur->opt.in6_addr,
+                                                cur->opt.prefixlen, &addr_cur);
+                log_radv("IPv6 prefix %s already configured, ignoring %s",
+                         strna(addr_cur), strna(addr_p));
 
-                        return -EEXIST;
-                }
+                return -EEXIST;
         }
 
         p = sd_radv_prefix_ref(p);
@@ -573,10 +574,8 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) {
 
         ra->n_prefixes++;
 
-        (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &p->opt.in6_addr, &addr_p);
-
         if (!dynamic) {
-                log_radv("Added prefix %s/%d", addr_p, p->opt.prefixlen);
+                log_radv("Added prefix %s", strna(addr_p));
                 return 0;
         }
 
@@ -609,8 +608,8 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) {
         cur->valid_until = valid_until;
         cur->preferred_until = preferred_until;
 
-        log_radv("Updated prefix %s/%u preferred %s valid %s",
-                 addr_p, p->opt.prefixlen,
+        log_radv("Updated prefix %s preferred %s valid %s",
+                 strna(addr_p),
                  format_timespan(time_string_preferred, FORMAT_TIMESPAN_MAX,
                                  preferred, USEC_PER_SEC),
                  format_timespan(time_string_valid, FORMAT_TIMESPAN_MAX,
@@ -656,12 +655,11 @@ _public_ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p, int
         if (!p)
                 return -EINVAL;
 
-        (void) in_addr_to_string(AF_INET6,
-                                 (union in_addr_union*) &p->opt.in6_addr,
-                                 &pretty);
+        (void) in_addr_prefix_to_string(AF_INET6,
+                                        (union in_addr_union*) &p->opt.in6_addr,
+                                        p->opt.prefixlen, &pretty);
 
         LIST_FOREACH(prefix, cur, ra->route_prefixes) {
-                _cleanup_free_ char *addr = NULL;
 
                 r = in_addr_prefix_intersect(AF_INET6,
                                              (union in_addr_union*) &cur->opt.in6_addr,
@@ -676,12 +674,12 @@ _public_ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p, int
                 if (dynamic && cur->opt.prefixlen == p->opt.prefixlen)
                         goto update;
 
-                (void) in_addr_to_string(AF_INET6,
-                                         (union in_addr_union*) &cur->opt.in6_addr,
-                                         &addr);
-                log_radv("IPv6 route prefix %s/%u already configured, ignoring %s/%u",
-                         strempty(addr), cur->opt.prefixlen,
-                         strempty(pretty), p->opt.prefixlen);
+                _cleanup_free_ char *addr = NULL;
+                (void) in_addr_prefix_to_string(AF_INET6,
+                                                (union in_addr_union*) &cur->opt.in6_addr,
+                                                cur->opt.prefixlen, &addr);
+                log_radv("IPv6 route prefix %s already configured, ignoring %s",
+                         strna(addr), strna(pretty));
 
                 return -EEXIST;
         }
@@ -692,7 +690,7 @@ _public_ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p, int
         ra->n_route_prefixes++;
 
         if (!dynamic) {
-                log_radv("Added prefix %s/%u", strempty(pretty), p->opt.prefixlen);
+                log_radv("Added prefix %s", strna(pretty));
                 return 0;
         }
 
@@ -715,8 +713,8 @@ _public_ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p, int
         if (valid_until == USEC_INFINITY)
                 return -EOVERFLOW;
 
-        log_radv("Updated route prefix %s/%u valid %s",
-                 strempty(pretty), p->opt.prefixlen,
+        log_radv("Updated route prefix %s valid %s",
+                 strna(pretty),
                  format_timespan(time_string_valid, FORMAT_TIMESPAN_MAX, valid, USEC_PER_SEC));
 
         return 0;
index c60ece5db8c155b93bed9b051f7e3c6e502e78a9..79a1c1b847de6bbc7d6f6b59c76326ccd6af782b 100644 (file)
@@ -167,8 +167,8 @@ static int address_pool_acquire_one(AddressPool *p, int family, unsigned prefixl
                         if (DEBUG_LOGGING) {
                                 _cleanup_free_ char *s = NULL;
 
-                                (void) in_addr_to_string(p->family, &u, &s);
-                                log_debug("Found range %s/%u", strna(s), prefixlen);
+                                (void) in_addr_prefix_to_string(p->family, &u, prefixlen, &s);
+                                log_debug("Found range %s", strna(s));
                         }
 
                         *found = u;
index dd7c28ec6e74a0235097500ed71f7697ac92874e..96c9376bedee53aec9b57caf209c53f8e726b257 100644 (file)
@@ -956,8 +956,8 @@ static int static_address_ready_callback(Address *address) {
                 if (!address_is_ready(a)) {
                         _cleanup_free_ char *str = NULL;
 
-                        (void) in_addr_to_string(a->family, &a->in_addr, &str);
-                        log_link_debug(link, "an address %s/%u is not ready", strnull(str), a->prefixlen);
+                        (void) in_addr_prefix_to_string(a->family, &a->in_addr, a->prefixlen, &str);
+                        log_link_debug(link, "an address %s is not ready", strnull(str));
                         return 0;
                 }
 
@@ -1263,9 +1263,9 @@ int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message,
                         if (r < 0) {
                                 _cleanup_free_ char *buf = NULL;
 
-                                (void) in_addr_to_string(tmp->family, &tmp->in_addr, &buf);
-                                log_link_warning_errno(link, r, "Failed to remember foreign address %s/%u, ignoring: %m",
-                                                       strnull(buf), tmp->prefixlen);
+                                (void) in_addr_prefix_to_string(tmp->family, &tmp->in_addr, tmp->prefixlen, &buf);
+                                log_link_warning_errno(link, r, "Failed to remember foreign address %s, ignoring: %m",
+                                                       strnull(buf));
                                 return 0;
                         }
                 }
index bb37a0bf7153e6a6aa7508b43edc80059d8a7feb..b36efd811c4d2007875b3f8558ae7150083ff280 100644 (file)
@@ -785,23 +785,21 @@ static int dhcp6_set_unreachable_route(Link *link, const union in_addr_union *ad
         assert(link);
         assert(addr);
 
-        (void) in_addr_to_string(AF_INET6, addr, &buf);
+        (void) in_addr_prefix_to_string(AF_INET6, addr, prefixlen, &buf);
 
         if (prefixlen > 64) {
-                log_link_debug(link, "PD Prefix length > 64, ignoring prefix %s/%u",
-                               strna(buf), prefixlen);
+                log_link_debug(link, "PD Prefix length > 64, ignoring prefix %s", strna(buf));
                 return 0;
         }
 
         if (prefixlen == 64) {
-                log_link_debug(link, "Not adding a blocking route for DHCPv6 delegated subnet %s/64 since distributed prefix is 64",
+                log_link_debug(link, "Not adding a blocking route for DHCPv6 delegated subnet %s since distributed prefix is 64",
                                strna(buf));
                 return 1;
         }
 
         if (prefixlen < 48)
-                log_link_warning(link, "PD Prefix length < 48, looks unusual %s/%u",
-                                 strna(buf), prefixlen);
+                log_link_warning(link, "PD Prefix length < 48, looks unusual: %s", strna(buf));
 
         r = route_new(&route);
         if (r < 0)
@@ -816,8 +814,8 @@ static int dhcp6_set_unreachable_route(Link *link, const union in_addr_union *ad
 
         r = route_configure(route, link, dhcp6_route_handler, &ret);
         if (r < 0)
-                return log_link_error_errno(link, r, "Failed to set unreachable route for DHCPv6 delegated subnet %s/%u: %m",
-                                            strna(buf), prefixlen);
+                return log_link_error_errno(link, r, "Failed to set unreachable route for DHCPv6 delegated subnet %s: %m",
+                                            strna(buf));
         if (r > 0)
                 link->dhcp6_route_configured = false;
 
@@ -825,8 +823,8 @@ static int dhcp6_set_unreachable_route(Link *link, const union in_addr_union *ad
 
         r = set_ensure_put(&link->dhcp6_routes, &route_hash_ops, ret);
         if (r < 0)
-                return log_link_error_errno(link, r, "Failed to store unreachable route for DHCPv6 delegated subnet %s/%u: %m",
-                                            strna(buf), prefixlen);
+                return log_link_error_errno(link, r, "Failed to store unreachable route for DHCPv6 delegated subnet %s: %m",
+                                            strna(buf));
 
         (void) set_remove(link->dhcp6_routes_old, ret);
 
@@ -885,9 +883,9 @@ static int dhcp6_pd_prefix_acquired(Link *dhcp6_link) {
                         uint64_t n_prefixes = UINT64_C(1) << (64 - pd_prefix_len);
                         _cleanup_free_ char *buf = NULL;
 
-                        (void) in_addr_to_string(AF_INET6, &prefix, &buf);
-                        log_link_debug(dhcp6_link, "Assigning up to %" PRIu64 " prefixes from %s/%u",
-                                       n_prefixes, strna(buf), pd_prefix_len);
+                        (void) in_addr_prefix_to_string(AF_INET6, &prefix, pd_prefix_len, &buf);
+                        log_link_debug(dhcp6_link, "Assigning up to %" PRIu64 " prefixes from %s",
+                                       n_prefixes, strna(buf));
                 }
 
                 dhcp6_pd_prefix_distribute(dhcp6_link,
@@ -962,7 +960,7 @@ static void log_dhcp6_address(Link *link, const Address *address, char **ret) {
         assert(link);
         assert(address);
 
-        (void) in_addr_to_string(address->family, &address->in_addr, &buffer);
+        (void) in_addr_prefix_to_string(address->family, &address->in_addr, address->prefixlen, &buffer);
         if (address->cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME)
                 valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX,
                                             address->cinfo.ifa_valid * USEC_PER_SEC,
@@ -994,18 +992,18 @@ static void log_dhcp6_address(Link *link, const Address *address, char **ret) {
                         break;
                 }
 
-        log_link_warning(link, "DHCPv6 address %s/%u (valid %s%s, preferred %s%s) conflicts the existing address %s/%u%s.",
-                         strnull(buffer), address->prefixlen,
+        log_link_warning(link, "DHCPv6 address %s (valid %s%s, preferred %s%s) conflicts the existing address %s %s.",
+                         strna(buffer),
                          valid_str ? "for " : "forever", strempty(valid_str),
                          preferred_str ? "for " : "forever", strempty(preferred_str),
-                         strnull(buffer), existing->prefixlen,
+                         strna(buffer),
                          by_ndisc ? "assigned by NDISC. Please try to use or update IPv6Token= setting "
                          "to change the address generated by NDISC, or disable UseAutonomousPrefix=" : "");
         goto finalize;
 
 simple_log:
-        log_link_full(link, log_level, "DHCPv6 address %s/%u (valid %s%s, preferred %s%s)",
-                      strnull(buffer), address->prefixlen,
+        log_link_full(link, log_level, "DHCPv6 address %s (valid %s%s, preferred %s%s)",
+                      strna(buffer),
                       valid_str ? "for " : "forever", strempty(valid_str),
                       preferred_str ? "for " : "forever", strempty(preferred_str));
 
@@ -1040,8 +1038,7 @@ static int dhcp6_update_address(
 
         r = address_configure(addr, link, dhcp6_address_handler, &ret);
         if (r < 0)
-                return log_link_error_errno(link, r, "Failed to set DHCPv6 address %s/%u: %m",
-                                            strnull(buffer), addr->prefixlen);
+                return log_link_error_errno(link, r, "Failed to set DHCPv6 address %s: %m", strna(buffer));
         if (r > 0)
                 link->dhcp6_address_configured = false;
 
@@ -1049,8 +1046,7 @@ static int dhcp6_update_address(
 
         r = set_ensure_put(&link->dhcp6_addresses, &address_hash_ops, ret);
         if (r < 0)
-                return log_link_error_errno(link, r, "Failed to store DHCPv6 address %s/%u: %m",
-                                            strnull(buffer), addr->prefixlen);
+                return log_link_error_errno(link, r, "Failed to store DHCPv6 address %s: %m", strna(buffer));
 
         (void) set_remove(link->dhcp6_addresses_old, ret);
 
index b3cf43f4a83dc9c30424a78620eb0288dc058b94..8f97724ce6bfe64dfb4edf016d8e7bf8642f6e7c 100644 (file)
@@ -740,8 +740,8 @@ void link_check_ready(Link *link) {
                 if (!address_is_ready(a)) {
                         _cleanup_free_ char *str = NULL;
 
-                        (void) in_addr_to_string(a->family, &a->in_addr, &str);
-                        return (void) log_link_debug(link, "%s(): an address %s/%d is not ready.", __func__, strnull(str), a->prefixlen);
+                        (void) in_addr_prefix_to_string(a->family, &a->in_addr, a->prefixlen, &str);
+                        return (void) log_link_debug(link, "%s(): an address %s is not ready.", __func__, strna(str));
                 }
 
         if (!link->static_routes_configured)
index 4f71396fbb76bb50ea27f9e82a6244732ef40f5c..d9776940a410714871fac937e3429cf9bc9a2c8b 100644 (file)
@@ -98,9 +98,9 @@ static int ndisc_address_callback(Address *address) {
         if (in6_addr_is_null(&router)) {
                 _cleanup_free_ char *buf = NULL;
 
-                (void) in_addr_to_string(address->family, &address->in_addr, &buf);
-                log_link_debug(address->link, "%s is called for %s/%u, but it is already removed, ignoring.",
-                               __func__, strna(buf), address->prefixlen);
+                (void) in_addr_prefix_to_string(address->family, &address->in_addr, address->prefixlen, &buf);
+                log_link_debug(address->link, "%s is called for %s, but it is already removed, ignoring.",
+                               __func__, strna(buf));
                 return 0;
         }
 
index 79ea9f2cdff3c823e79a747f16fb72b6e868fbe0..e21c456b0afc013944275318ca50f5b994b30b47 100644 (file)
@@ -614,13 +614,11 @@ static void log_route_debug(const Route *route, const char *str, const Link *lin
         /* link may be NULL. */
 
         if (DEBUG_LOGGING) {
-                _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL,
-                        *prefsrc = NULL, *table = NULL, *scope = NULL, *proto = NULL;
+                _cleanup_free_ char *dst = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL,
+                        *table = NULL, *scope = NULL, *proto = NULL;
 
-                if (in_addr_is_set(route->family, &route->dst)) {
-                        (void) in_addr_to_string(route->family, &route->dst, &dst);
-                        (void) asprintf(&dst_prefixlen, "/%u", route->dst_prefixlen);
-                }
+                if (in_addr_is_set(route->family, &route->dst))
+                        (void) in_addr_prefix_to_string(route->family, &route->dst, route->dst_prefixlen, &dst);
                 if (in_addr_is_set(route->family, &route->src))
                         (void) in_addr_to_string(route->family, &route->src, &src);
                 if (in_addr_is_set(route->gw_family, &route->gw))
@@ -632,8 +630,8 @@ static void log_route_debug(const Route *route, const char *str, const Link *lin
                 (void) route_protocol_full_to_string_alloc(route->protocol, &proto);
 
                 log_link_debug(link,
-                               "%s route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
-                               str, strna(dst), strempty(dst_prefixlen), strna(src), strna(gw), strna(prefsrc),
+                               "%s route: dst: %s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
+                               str, strna(dst), strna(src), strna(gw), strna(prefsrc),
                                strna(scope), strna(table), strna(proto),
                                strna(route_type_to_string(route->type)));
         }
index 3260fa89c755eb65aae5ed3dc9706a06479e525d..a7fddfd58fc9246371f5c0846e734feb36495585 100644 (file)
@@ -401,13 +401,13 @@ static void log_routing_policy_rule_debug(const RoutingPolicyRule *rule, int fam
         if (DEBUG_LOGGING) {
                 _cleanup_free_ char *from = NULL, *to = NULL, *table = NULL;
 
-                (void) in_addr_to_string(family, &rule->from, &from);
-                (void) in_addr_to_string(family, &rule->to, &to);
+                (void) in_addr_prefix_to_string(family, &rule->from, rule->from_prefixlen, &from);
+                (void) in_addr_prefix_to_string(family, &rule->to, rule->to_prefixlen, &to);
                 (void) manager_get_route_table_to_string(m, rule->table, &table);
 
                 log_link_debug(link,
-                               "%s routing policy rule: priority: %"PRIu32", %s/%u -> %s/%u, iif: %s, oif: %s, table: %s",
-                               str, rule->priority, strna(from), rule->from_prefixlen, strna(to), rule->to_prefixlen,
+                               "%s routing policy rule: priority: %"PRIu32", %s -> %s, iif: %s, oif: %s, table: %s",
+                               str, rule->priority, strna(from), strna(to),
                                strna(rule->iif), strna(rule->oif), strna(table));
         }
 }