From 7653dcc3286067a25be6af47bcf84d3e8136ae39 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 18 Feb 2021 00:36:38 +0900 Subject: [PATCH 01/10] in-addr-util: introduce in_addr_is_set() or friends --- src/basic/in-addr-util.c | 8 +++++++- src/basic/in-addr-util.h | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c index 641c5de7a37..6ee3a6ad63b 100644 --- a/src/basic/in-addr-util.c +++ b/src/basic/in-addr-util.c @@ -24,6 +24,12 @@ bool in4_addr_is_null(const struct in_addr *a) { return a->s_addr == 0; } +bool in6_addr_is_null(const struct in6_addr *a) { + assert(a); + + return IN6_IS_ADDR_UNSPECIFIED(a); +} + int in_addr_is_null(int family, const union in_addr_union *u) { assert(u); @@ -31,7 +37,7 @@ int in_addr_is_null(int family, const union in_addr_union *u) { return in4_addr_is_null(&u->in); if (family == AF_INET6) - return IN6_IS_ADDR_UNSPECIFIED(&u->in6); + return in6_addr_is_null(&u->in6); return -EAFNOSUPPORT; } diff --git a/src/basic/in-addr-util.h b/src/basic/in-addr-util.h index a5ce8258fd9..f7cdf0d9bc0 100644 --- a/src/basic/in-addr-util.h +++ b/src/basic/in-addr-util.h @@ -21,7 +21,24 @@ struct in_addr_data { }; bool in4_addr_is_null(const struct in_addr *a); +static inline bool in4_addr_is_set(const struct in_addr *a) { + return !in4_addr_is_null(a); +} +bool in6_addr_is_null(const struct in6_addr *a); +static inline bool in6_addr_is_set(const struct in6_addr *a) { + return !in6_addr_is_null(a); +} int in_addr_is_null(int family, const union in_addr_union *u); +static inline bool in_addr_is_set(int family, const union in_addr_union *u) { + return in_addr_is_null(family, u) == 0; +} +static inline int in_addr_data_is_null(const struct in_addr_data *a) { + assert(a); + return in_addr_is_null(a->family, &a->address); +} +static inline bool in_addr_data_is_set(const struct in_addr_data *a) { + return in_addr_data_is_null(a); +} int in_addr_is_multicast(int family, const union in_addr_union *u); From 1235befaddae3653d59e3531541ece580e6cfc7f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 18 Feb 2021 01:56:47 +0900 Subject: [PATCH 02/10] in-addr-util: introduce in6_addr_is_link_local() --- src/basic/in-addr-util.c | 8 +++++++- src/basic/in-addr-util.h | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c index 6ee3a6ad63b..06632b846d2 100644 --- a/src/basic/in-addr-util.c +++ b/src/basic/in-addr-util.c @@ -48,6 +48,12 @@ bool in4_addr_is_link_local(const struct in_addr *a) { return (be32toh(a->s_addr) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16); } +bool in6_addr_is_link_local(const struct in6_addr *a) { + assert(a); + + return IN6_IS_ADDR_LINKLOCAL(a); +} + int in_addr_is_link_local(int family, const union in_addr_union *u) { assert(u); @@ -55,7 +61,7 @@ int in_addr_is_link_local(int family, const union in_addr_union *u) { return in4_addr_is_link_local(&u->in); if (family == AF_INET6) - return IN6_IS_ADDR_LINKLOCAL(&u->in6); + return in6_addr_is_link_local(&u->in6); return -EAFNOSUPPORT; } diff --git a/src/basic/in-addr-util.h b/src/basic/in-addr-util.h index f7cdf0d9bc0..7494358d423 100644 --- a/src/basic/in-addr-util.h +++ b/src/basic/in-addr-util.h @@ -43,6 +43,7 @@ static inline bool in_addr_data_is_set(const struct in_addr_data *a) { int in_addr_is_multicast(int family, const union in_addr_union *u); bool in4_addr_is_link_local(const struct in_addr *a); +bool in6_addr_is_link_local(const struct in6_addr *a); int in_addr_is_link_local(int family, const union in_addr_union *u); bool in6_addr_is_link_local_all_nodes(const struct in6_addr *a); From fa550434502bd4a20bc9e8dabb39295f1bb79fde Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 18 Feb 2021 02:17:08 +0900 Subject: [PATCH 03/10] in-addr-util: introduce in6_addr_equal() --- src/basic/in-addr-util.c | 9 ++++++++- src/basic/in-addr-util.h | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c index 06632b846d2..f7a7252f1af 100644 --- a/src/basic/in-addr-util.c +++ b/src/basic/in-addr-util.c @@ -128,6 +128,13 @@ bool in4_addr_equal(const struct in_addr *a, const struct in_addr *b) { return a->s_addr == b->s_addr; } +bool in6_addr_equal(const struct in6_addr *a, const struct in6_addr *b) { + assert(a); + assert(b); + + return IN6_ARE_ADDR_EQUAL(a, b); +} + int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) { assert(a); assert(b); @@ -136,7 +143,7 @@ int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_ return in4_addr_equal(&a->in, &b->in); if (family == AF_INET6) - return IN6_ARE_ADDR_EQUAL(&a->in6, &b->in6); + return in6_addr_equal(&a->in6, &b->in6); return -EAFNOSUPPORT; } diff --git a/src/basic/in-addr-util.h b/src/basic/in-addr-util.h index 7494358d423..519ee53b3a4 100644 --- a/src/basic/in-addr-util.h +++ b/src/basic/in-addr-util.h @@ -54,6 +54,7 @@ bool in4_addr_is_local_multicast(const struct in_addr *a); bool in4_addr_is_non_local(const struct in_addr *a); bool in4_addr_equal(const struct in_addr *a, const struct in_addr *b); +bool in6_addr_equal(const struct in6_addr *a, const struct in6_addr *b); int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b); int in_addr_prefix_intersect(int family, const union in_addr_union *a, unsigned aprefixlen, const union in_addr_union *b, unsigned bprefixlen); int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen); From 275468c033ab4a5da4c6a6d32baa3268b3f46d09 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 18 Feb 2021 02:23:42 +0900 Subject: [PATCH 04/10] network: assign values after all checks are passed --- src/network/networkd-dhcp6.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/network/networkd-dhcp6.c b/src/network/networkd-dhcp6.c index 326c9f3fcf3..eb9fabb2aeb 100644 --- a/src/network/networkd-dhcp6.c +++ b/src/network/networkd-dhcp6.c @@ -1613,6 +1613,8 @@ int config_parse_dhcp6_pd_hint( void *userdata) { Network *network = data; + union in_addr_union u; + unsigned char prefixlen; int r; assert(filename); @@ -1620,18 +1622,22 @@ int config_parse_dhcp6_pd_hint( assert(rvalue); assert(data); - r = in_addr_prefix_from_string(rvalue, AF_INET6, (union in_addr_union *) &network->dhcp6_pd_address, &network->dhcp6_pd_length); + r = in_addr_prefix_from_string(rvalue, AF_INET6, &u, &prefixlen); if (r < 0) { - log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse PrefixDelegationHint=%s, ignoring assignment", rvalue); + log_syntax(unit, LOG_WARNING, filename, line, r, + "Failed to parse %s=%s, ignoring assignment.", lvalue, rvalue); return 0; } - if (network->dhcp6_pd_length < 1 || network->dhcp6_pd_length > 128) { - log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid prefix length='%d', ignoring assignment", network->dhcp6_pd_length); - network->dhcp6_pd_length = 0; + if (prefixlen < 1 || prefixlen > 128) { + log_syntax(unit, LOG_WARNING, filename, line, 0, + "Invalid prefix length in %s=%s, ignoring assignment.", lvalue, rvalue); return 0; } + network->dhcp6_pd_address = u.in6; + network->dhcp6_pd_length = prefixlen; + return 0; } From 94876904bbd7596c58e0a4a60efc0c1e74eb8e98 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 18 Feb 2021 01:29:43 +0900 Subject: [PATCH 05/10] tree-wide: use in_addr_is_set() or friends --- src/libsystemd-network/icmp6-util.c | 18 +++++++++++------- src/libsystemd-network/icmp6-util.h | 4 ++-- src/libsystemd-network/ndisc-router.c | 2 +- src/libsystemd-network/sd-dhcp6-client.c | 4 ++-- src/libsystemd-network/sd-ipv4ll.c | 2 +- src/libsystemd-network/sd-radv.c | 8 +++----- src/network/generator/network-generator.c | 8 ++++---- src/network/netdev/geneve.c | 2 +- src/network/netdev/l2tp-tunnel.c | 8 ++++---- src/network/netdev/tunnel.c | 12 +++++------- src/network/netdev/vxlan.c | 8 ++++---- src/network/networkctl.c | 12 ++++++------ src/network/networkd-address.c | 18 +++++++++--------- src/network/networkd-dhcp-server.c | 2 +- src/network/networkd-dhcp4.c | 4 ++-- src/network/networkd-dhcp6.c | 4 ++-- src/network/networkd-fdb.c | 2 +- src/network/networkd-link.c | 11 +++++------ src/network/networkd-ndisc.c | 11 +++++------ src/network/networkd-nexthop.c | 6 +++--- src/network/networkd-radv.c | 4 ++-- src/network/networkd-route.c | 16 ++++++++-------- src/network/networkd-routing-policy-rule.c | 4 ++-- src/network/test-network.c | 12 ++++++------ src/nspawn/nspawn-expose-ports.c | 4 ++-- src/nss-myhostname/nss-myhostname.c | 2 +- src/nss-resolve/nss-resolve.c | 2 +- src/resolve/resolved-dns-rr.c | 2 +- src/resolve/resolved-dns-scope.c | 4 ++-- src/resolve/resolved-etc-hosts.c | 2 +- src/shared/resolve-util.c | 2 +- 31 files changed, 99 insertions(+), 101 deletions(-) diff --git a/src/libsystemd-network/icmp6-util.c b/src/libsystemd-network/icmp6-util.c index 4af012534ab..d079ad40e99 100644 --- a/src/libsystemd-network/icmp6-util.c +++ b/src/libsystemd-network/icmp6-util.c @@ -145,8 +145,8 @@ int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) { return 0; } -int icmp6_receive(int fd, void *buffer, size_t size, struct in6_addr *dst, - triple_timestamp *timestamp) { +int icmp6_receive(int fd, void *buffer, size_t size, struct in6_addr *ret_dst, + triple_timestamp *ret_timestamp) { CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int)) + /* ttl */ CMSG_SPACE(sizeof(struct timeval))) control; @@ -161,6 +161,8 @@ int icmp6_receive(int fd, void *buffer, size_t size, struct in6_addr *dst, .msg_controllen = sizeof(control), }; struct cmsghdr *cmsg; + struct in6_addr addr = {}; + triple_timestamp t; ssize_t len; iov = IOVEC_MAKE(buffer, size); @@ -175,8 +177,8 @@ int icmp6_receive(int fd, void *buffer, size_t size, struct in6_addr *dst, if (msg.msg_namelen == sizeof(struct sockaddr_in6) && sa.in6.sin6_family == AF_INET6) { - *dst = sa.in6.sin6_addr; - if (in_addr_is_link_local(AF_INET6, (union in_addr_union*) dst) <= 0) + addr = sa.in6.sin6_addr; + if (!in6_addr_is_link_local(&addr)) return -EADDRNOTAVAIL; } else if (msg.msg_namelen > 0) @@ -200,11 +202,13 @@ int icmp6_receive(int fd, void *buffer, size_t size, struct in6_addr *dst, if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMP && cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval))) - triple_timestamp_from_realtime(timestamp, timeval_load((struct timeval*) CMSG_DATA(cmsg))); + triple_timestamp_from_realtime(&t, timeval_load((struct timeval*) CMSG_DATA(cmsg))); } - if (!triple_timestamp_is_set(timestamp)) - triple_timestamp_get(timestamp); + if (!triple_timestamp_is_set(&t)) + triple_timestamp_get(&t); + *ret_dst = addr; + *ret_timestamp = t; return 0; } diff --git a/src/libsystemd-network/icmp6-util.h b/src/libsystemd-network/icmp6-util.h index 50d21b5b593..f7ad26b5e61 100644 --- a/src/libsystemd-network/icmp6-util.h +++ b/src/libsystemd-network/icmp6-util.h @@ -20,5 +20,5 @@ int icmp6_bind_router_solicitation(int ifindex); int icmp6_bind_router_advertisement(int ifindex); int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr); -int icmp6_receive(int fd, void *buffer, size_t size, struct in6_addr *dst, - triple_timestamp *timestamp); +int icmp6_receive(int fd, void *buffer, size_t size, struct in6_addr *ret_dst, + triple_timestamp *ret_timestamp); diff --git a/src/libsystemd-network/ndisc-router.c b/src/libsystemd-network/ndisc-router.c index 3cb71dbdac9..c88293a9239 100644 --- a/src/libsystemd-network/ndisc-router.c +++ b/src/libsystemd-network/ndisc-router.c @@ -56,7 +56,7 @@ _public_ int sd_ndisc_router_get_address(sd_ndisc_router *rt, struct in6_addr *r assert_return(rt, -EINVAL); assert_return(ret_addr, -EINVAL); - if (IN6_IS_ADDR_UNSPECIFIED(&rt->address)) + if (in6_addr_is_null(&rt->address)) return -ENODATA; *ret_addr = rt->address; diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c index f362770c4fe..2184073f19d 100644 --- a/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/libsystemd-network/sd-dhcp6-client.c @@ -171,7 +171,7 @@ int sd_dhcp6_client_set_local_address( assert_return(client, -EINVAL); assert_return(local_address, -EINVAL); - assert_return(in_addr_is_link_local(AF_INET6, (const union in_addr_union *) local_address) > 0, -EINVAL); + assert_return(in6_addr_is_link_local(local_address) > 0, -EINVAL); assert_return(IN_SET(client->state, DHCP6_STATE_STOPPED), -EBUSY); @@ -1693,7 +1693,7 @@ int sd_dhcp6_client_start(sd_dhcp6_client *client) { assert_return(client, -EINVAL); assert_return(client->event, -EINVAL); assert_return(client->ifindex > 0, -EINVAL); - assert_return(in_addr_is_link_local(AF_INET6, (const union in_addr_union *) &client->local_address) > 0, -EINVAL); + assert_return(in6_addr_is_link_local(&client->local_address) > 0, -EINVAL); if (!IN_SET(client->state, DHCP6_STATE_STOPPED)) return -EBUSY; diff --git a/src/libsystemd-network/sd-ipv4ll.c b/src/libsystemd-network/sd-ipv4ll.c index 3af7d89bf07..a83c9b06de5 100644 --- a/src/libsystemd-network/sd-ipv4ll.c +++ b/src/libsystemd-network/sd-ipv4ll.c @@ -186,7 +186,7 @@ int sd_ipv4ll_is_running(sd_ipv4ll *ll) { static bool ipv4ll_address_is_valid(const struct in_addr *address) { assert(address); - if (!in_addr_is_link_local(AF_INET, (const union in_addr_union *) address)) + if (!in4_addr_is_link_local(address)) return false; return !IN_SET(be32toh(address->s_addr) & 0x0000FF00U, 0x0000U, 0xFF00U); diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index 8beb845d79e..b34263568e1 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -167,7 +167,7 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst, uint32_t router_li if (r < 0) return r; - if (dst && !IN6_IS_ADDR_UNSPECIFIED(dst)) + if (dst && in6_addr_is_set(dst)) dst_addr.sin6_addr = *dst; adv.nd_ra_type = ND_ROUTER_ADVERT; @@ -536,7 +536,7 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) { return -EINVAL; /* Refuse prefixes that don't have a prefix set */ - if (IN6_IS_ADDR_UNSPECIFIED(&p->opt.in6_addr)) + if (in6_addr_is_null(&p->opt.in6_addr)) return -ENOEXEC; LIST_FOREACH(prefix, cur, ra->prefixes) { @@ -631,9 +631,7 @@ _public_ sd_radv_prefix *sd_radv_remove_prefix(sd_radv *ra, if (prefixlen != cur->opt.prefixlen) continue; - if (!in_addr_equal(AF_INET6, - (union in_addr_union *)prefix, - (union in_addr_union *)&cur->opt.in6_addr)) + if (!in6_addr_equal(prefix, &cur->opt.in6_addr)) continue; LIST_REMOVE(prefix, ra->prefixes, cur); diff --git a/src/network/generator/network-generator.c b/src/network/generator/network-generator.c index 492bb68e320..ae673ddf5b5 100644 --- a/src/network/generator/network-generator.c +++ b/src/network/generator/network-generator.c @@ -360,7 +360,7 @@ static int network_set_address(Context *context, const char *ifname, int family, union in_addr_union *addr, union in_addr_union *peer) { Network *network; - if (in_addr_is_null(family, addr) != 0) + if (!in_addr_is_set(family, addr)) return 0; network = network_get(context, ifname); @@ -375,7 +375,7 @@ static int network_set_route(Context *context, const char *ifname, int family, u Network *network; int r; - if (in_addr_is_null(family, gateway) != 0) + if (!in_addr_is_set(family, gateway)) return 0; network = network_get(context, ifname); @@ -1000,7 +1000,7 @@ static int address_dump(Address *address, FILE *f) { if (r < 0) return r; - if (in_addr_is_null(address->family, &address->peer) == 0) { + if (in_addr_is_set(address->family, &address->peer)) { r = in_addr_to_string(address->family, &address->peer, &peer); if (r < 0) return r; @@ -1021,7 +1021,7 @@ static int route_dump(Route *route, FILE *f) { _cleanup_free_ char *dest = NULL, *gateway = NULL; int r; - if (in_addr_is_null(route->family, &route->dest) == 0) { + if (in_addr_is_set(route->family, &route->dest)) { r = in_addr_prefix_to_string(route->family, &route->dest, route->prefixlen, &dest); if (r < 0) return r; diff --git a/src/network/netdev/geneve.c b/src/network/netdev/geneve.c index edf92ec93c4..fd0b5119e7f 100644 --- a/src/network/netdev/geneve.c +++ b/src/network/netdev/geneve.c @@ -90,7 +90,7 @@ static int netdev_geneve_create(NetDev *netdev) { return log_netdev_error_errno(netdev, r, "Could not append IFLA_GENEVE_ID attribute: %m"); } - if (in_addr_is_null(v->remote_family, &v->remote) == 0) { + if (in_addr_is_set(v->remote_family, &v->remote)) { if (v->remote_family == AF_INET) r = sd_netlink_message_append_in_addr(m, IFLA_GENEVE_REMOTE, &v->remote.in); else diff --git a/src/network/netdev/l2tp-tunnel.c b/src/network/netdev/l2tp-tunnel.c index a909b157646..32ec0245581 100644 --- a/src/network/netdev/l2tp-tunnel.c +++ b/src/network/netdev/l2tp-tunnel.c @@ -252,7 +252,7 @@ static int l2tp_acquire_local_address_one(L2tpTunnel *t, Address *a, union in_ad if (a->family != t->family) return -EINVAL; - if (in_addr_is_null(a->family, &a->in_addr_peer) <= 0) + if (in_addr_is_set(a->family, &a->in_addr_peer)) return -EINVAL; if (t->local_address_type == NETDEV_L2TP_LOCAL_ADDRESS_STATIC && @@ -275,7 +275,7 @@ static int l2tp_acquire_local_address(L2tpTunnel *t, Link *link, union in_addr_u assert(ret); assert(IN_SET(t->family, AF_INET, AF_INET6)); - if (!in_addr_is_null(t->family, &t->local)) { + if (in_addr_is_set(t->family, &t->local)) { /* local address is explicitly specified. */ *ret = t->local; return 0; @@ -435,7 +435,7 @@ int config_parse_l2tp_tunnel_address( addr_type = l2tp_local_address_type_from_string(rvalue); if (addr_type >= 0) { - if (in_addr_is_null(t->family, &t->remote) != 0) + if (!in_addr_is_set(t->family, &t->remote)) /* If Remote= is not specified yet, then also clear family. */ t->family = AF_UNSPEC; @@ -682,7 +682,7 @@ static int netdev_l2tp_tunnel_verify(NetDev *netdev, const char *filename) { "%s: L2TP tunnel with invalid address family configured. Ignoring", filename); - if (in_addr_is_null(t->family, &t->remote)) + if (!in_addr_is_set(t->family, &t->remote)) return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "%s: L2TP tunnel without a remote address configured. Ignoring", filename); diff --git a/src/network/netdev/tunnel.c b/src/network/netdev/tunnel.c index fdaec488a21..2fab269397f 100644 --- a/src/network/netdev/tunnel.c +++ b/src/network/netdev/tunnel.c @@ -468,7 +468,7 @@ static int netdev_tunnel_verify(NetDev *netdev, const char *filename) { "vti/ipip/sit/gre tunnel without a local/remote IPv4 address configured in %s. Ignoring", filename); if (IN_SET(netdev->kind, NETDEV_KIND_GRETAP, NETDEV_KIND_ERSPAN) && - (t->family != AF_INET || in_addr_is_null(t->family, &t->remote))) + (t->family != AF_INET || !in_addr_is_set(t->family, &t->remote))) return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "gretap/erspan tunnel without a remote IPv4 address configured in %s. Ignoring", filename); @@ -478,7 +478,7 @@ static int netdev_tunnel_verify(NetDev *netdev, const char *filename) { "vti6/ip6tnl/ip6gre tunnel without a local/remote IPv6 address configured in %s. Ignoring", filename); if (netdev->kind == NETDEV_KIND_IP6GRETAP && - (t->family != AF_INET6 || in_addr_is_null(t->family, &t->remote))) + (t->family != AF_INET6 || !in_addr_is_set(t->family, &t->remote))) return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "ip6gretap tunnel without a remote IPv6 address configured in %s. Ignoring", filename); @@ -530,11 +530,9 @@ int config_parse_tunnel_address(const char *unit, *addr = IN_ADDR_NULL; /* As a special case, if both the local and remote addresses are - * unspecified, also clear the address family. - */ - if (t->family != AF_UNSPEC && - in_addr_is_null(t->family, &t->local) != 0 && - in_addr_is_null(t->family, &t->remote) != 0) + * unspecified, also clear the address family. */ + if (!in_addr_is_set(t->family, &t->local) && + !in_addr_is_set(t->family, &t->remote)) t->family = AF_UNSPEC; return 0; } diff --git a/src/network/netdev/vxlan.c b/src/network/netdev/vxlan.c index 6748f67f8f5..52d8b3736cc 100644 --- a/src/network/netdev/vxlan.c +++ b/src/network/netdev/vxlan.c @@ -37,14 +37,14 @@ static int netdev_vxlan_fill_message_create(NetDev *netdev, Link *link, sd_netli return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_ID attribute: %m"); } - if (in_addr_is_null(v->group_family, &v->group) == 0) { + if (in_addr_is_set(v->group_family, &v->group)) { if (v->group_family == AF_INET) r = sd_netlink_message_append_in_addr(m, IFLA_VXLAN_GROUP, &v->group.in); else r = sd_netlink_message_append_in6_addr(m, IFLA_VXLAN_GROUP6, &v->group.in6); if (r < 0) return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_GROUP attribute: %m"); - } else if (in_addr_is_null(v->remote_family, &v->remote) == 0) { + } else if (in_addr_is_set(v->remote_family, &v->remote)) { if (v->remote_family == AF_INET) r = sd_netlink_message_append_in_addr(m, IFLA_VXLAN_GROUP, &v->remote.in); else @@ -53,7 +53,7 @@ static int netdev_vxlan_fill_message_create(NetDev *netdev, Link *link, sd_netli return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_GROUP attribute: %m"); } - if (in_addr_is_null(v->local_family, &v->local) == 0) { + if (in_addr_is_set(v->local_family, &v->local)) { if (v->local_family == AF_INET) r = sd_netlink_message_append_in_addr(m, IFLA_VXLAN_LOCAL, &v->local.in); else @@ -354,7 +354,7 @@ static int netdev_vxlan_verify(NetDev *netdev, const char *filename) { if (!v->dest_port && v->generic_protocol_extension) v->dest_port = 4790; - if (in_addr_is_null(v->group_family, &v->group) == 0 && in_addr_is_null(v->remote_family, &v->remote) == 0) + if (in_addr_is_set(v->group_family, &v->group) && in_addr_is_set(v->remote_family, &v->remote)) return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "%s: VXLAN both 'Group=' and 'Remote=' cannot be specified. Ignoring.", filename); diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 1dd3438abd2..763d5ef37da 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -1787,7 +1787,7 @@ static int link_status_one( if (r < 0) return table_log_add_error(r); } else if (STRPTR_IN_SET(info->netdev_kind, "ipip", "sit", "gre", "gretap", "erspan", "vti")) { - if (!in_addr_is_null(AF_INET, &info->local)) { + if (in_addr_is_set(AF_INET, &info->local)) { r = table_add_many(table, TABLE_EMPTY, TABLE_STRING, "Local:", @@ -1796,7 +1796,7 @@ static int link_status_one( return table_log_add_error(r); } - if (!in_addr_is_null(AF_INET, &info->remote)) { + if (in_addr_is_set(AF_INET, &info->remote)) { r = table_add_many(table, TABLE_EMPTY, TABLE_STRING, "Remote:", @@ -1805,7 +1805,7 @@ static int link_status_one( return table_log_add_error(r); } } else if (STRPTR_IN_SET(info->netdev_kind, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) { - if (!in_addr_is_null(AF_INET6, &info->local)) { + if (in_addr_is_set(AF_INET6, &info->local)) { r = table_add_many(table, TABLE_EMPTY, TABLE_STRING, "Local:", @@ -1814,7 +1814,7 @@ static int link_status_one( return table_log_add_error(r); } - if (!in_addr_is_null(AF_INET6, &info->remote)) { + if (in_addr_is_set(AF_INET6, &info->remote)) { r = table_add_many(table, TABLE_EMPTY, TABLE_STRING, "Remote:", @@ -1830,14 +1830,14 @@ static int link_status_one( if (r < 0) return table_log_add_error(r); - if (info->has_tunnel_ipv4 && !in_addr_is_null(AF_INET, &info->remote)) { + if (info->has_tunnel_ipv4 && in_addr_is_set(AF_INET, &info->remote)) { r = table_add_many(table, TABLE_EMPTY, TABLE_STRING, "Remote:", TABLE_IN_ADDR, &info->remote); if (r < 0) return table_log_add_error(r); - } else if (!in_addr_is_null(AF_INET6, &info->remote)) { + } else if (in_addr_is_set(AF_INET6, &info->remote)) { r = table_add_many(table, TABLE_EMPTY, TABLE_STRING, "Remote:", diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c index d862e45e61c..dd7c28ec6e7 100644 --- a/src/network/networkd-address.c +++ b/src/network/networkd-address.c @@ -130,7 +130,8 @@ Address *address_free(Address *address) { if (n->address == address) free(set_remove(address->link->ndisc_addresses, n)); - if (in_addr_equal(AF_INET6, &address->in_addr, (const union in_addr_union *) &address->link->ipv6ll_address)) + if (address->family == AF_INET6 && + in6_addr_equal(&address->in_addr.in6, &address->link->ipv6ll_address)) memzero(&address->link->ipv6ll_address, sizeof(struct in6_addr)); } @@ -147,7 +148,9 @@ static bool address_may_have_broadcast(const Address *a) { /* A /31 or /32 IPv4 address does not have a broadcast address. * See https://tools.ietf.org/html/rfc3021 */ - return a->family == AF_INET && in4_addr_is_null(&a->in_addr_peer.in) && a->prefixlen <= 30; + return a->family == AF_INET && + in_addr_is_null(AF_INET, &a->in_addr_peer) && + a->prefixlen <= 30; } static uint32_t address_prefix(const Address *a) { @@ -397,7 +400,7 @@ static int address_update(Address *address, const Address *src) { if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) > 0 && - IN6_IS_ADDR_UNSPECIFIED(&address->link->ipv6ll_address) > 0) { + in6_addr_is_null(&address->link->ipv6ll_address)) { r = link_ipv6ll_gained(address->link, &address->in_addr.in6); if (r < 0) @@ -487,7 +490,7 @@ static void log_address_debug(const Address *address, const char *str, const Lin bool has_peer; (void) in_addr_to_string(address->family, &address->in_addr, &addr); - has_peer = in_addr_is_null(address->family, &address->in_addr_peer) == 0; + has_peer = in_addr_is_set(address->family, &address->in_addr_peer); if (has_peer) (void) in_addr_to_string(address->family, &address->in_addr_peer, &peer); @@ -788,10 +791,7 @@ static int address_acquire(Link *link, const Address *original, Address **ret) { assert(ret); /* Something useful was configured? just use it */ - r = in_addr_is_null(original->family, &original->in_addr); - if (r < 0) - return r; - if (r == 0) { + if (in_addr_is_set(original->family, &original->in_addr)) { *ret = NULL; return 0; } @@ -891,7 +891,7 @@ int address_configure( if (r < 0) return log_link_error_errno(link, r, "Could not set scope: %m"); - if (in_addr_is_null(address->family, &address->in_addr_peer) == 0) { + if (in_addr_is_set(address->family, &address->in_addr_peer)) { r = netlink_message_append_in_addr_union(req, IFA_ADDRESS, address->family, &address->in_addr_peer); if (r < 0) return log_link_error_errno(link, r, "Could not append IFA_ADDRESS attribute: %m"); diff --git a/src/network/networkd-dhcp-server.c b/src/network/networkd-dhcp-server.c index 9a9f1eabe53..2c533c6f8dd 100644 --- a/src/network/networkd-dhcp-server.c +++ b/src/network/networkd-dhcp-server.c @@ -47,7 +47,7 @@ static Address* link_find_dhcp_server_address(Link *link) { /* The first statically configured address if there is any */ ORDERED_HASHMAP_FOREACH(address, link->network->addresses_by_section) if (address->family == AF_INET && - !in_addr_is_null(address->family, &address->in_addr)) + in_addr_is_set(address->family, &address->in_addr)) return address; /* If that didn't work, find a suitable address we got from the pool */ diff --git a/src/network/networkd-dhcp4.c b/src/network/networkd-dhcp4.c index 1559e0cfd85..b0c783c774a 100644 --- a/src/network/networkd-dhcp4.c +++ b/src/network/networkd-dhcp4.c @@ -122,7 +122,7 @@ static int route_scope_from_address(const Route *route, const struct in_addr *se assert(self_addr); if (in4_addr_is_localhost(&route->dst.in) || - (!in4_addr_is_null(self_addr) && in4_addr_equal(&route->dst.in, self_addr))) + (in4_addr_is_set(self_addr) && in4_addr_equal(&route->dst.in, self_addr))) return RT_SCOPE_HOST; else if (in4_addr_is_null(&route->gw.in)) return RT_SCOPE_LINK; @@ -835,7 +835,7 @@ static int dhcp4_update_address(Link *link, bool announce) { if (r < 0 && r != -ENODATA) return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m"); - if (r > 0 && !in4_addr_is_null(&router[0])) + if (r > 0 && in4_addr_is_set(&router[0])) log_struct(LOG_INFO, LOG_LINK_INTERFACE(link), LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u via "IPV4_ADDRESS_FMT_STR, diff --git a/src/network/networkd-dhcp6.c b/src/network/networkd-dhcp6.c index eb9fabb2aeb..bb37a0bf715 100644 --- a/src/network/networkd-dhcp6.c +++ b/src/network/networkd-dhcp6.c @@ -379,7 +379,7 @@ static int dhcp6_set_pd_address( address->in_addr = *prefix; - if (!in_addr_is_null(AF_INET6, &link->network->dhcp6_pd_token)) + if (in_addr_is_set(AF_INET6, &link->network->dhcp6_pd_token)) memcpy(address->in_addr.in6.s6_addr + 8, link->network->dhcp6_pd_token.in6.s6_addr + 8, 8); else { r = generate_ipv6_eui_64_address(link, &address->in_addr.in6); @@ -1235,7 +1235,7 @@ int dhcp6_request_address(Link *link, int ir) { assert(link); assert(link->dhcp6_client); assert(link->network); - assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address) > 0); + assert(in6_addr_is_link_local(&link->ipv6ll_address)); r = sd_dhcp6_client_is_running(link->dhcp6_client); if (r < 0) diff --git a/src/network/networkd-fdb.c b/src/network/networkd-fdb.c index 4cd430e8aef..e4e727038f5 100644 --- a/src/network/networkd-fdb.c +++ b/src/network/networkd-fdb.c @@ -142,7 +142,7 @@ static int fdb_entry_configure(Link *link, FdbEntry *fdb_entry) { return log_link_error_errno(link, r, "Could not append NDA_VLAN attribute: %m"); } - if (!in_addr_is_null(fdb_entry->family, &fdb_entry->destination_addr)) { + if (in_addr_is_set(fdb_entry->family, &fdb_entry->destination_addr)) { r = netlink_message_append_in_addr_union(req, NDA_DST, fdb_entry->family, &fdb_entry->destination_addr); if (r < 0) return log_link_error_errno(link, r, "Could not append NDA_DST attribute: %m"); diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index aacfc31c529..b3cf43f4a83 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -766,9 +766,8 @@ void link_check_ready(Link *link) { bool has_ndisc_address = false; NDiscAddress *n; - if (link_ipv6ll_enabled(link) && - in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) - return (void) log_link_debug(link, "%s(): IPv6LL is not configured.", __func__); + if (link_ipv6ll_enabled(link) && !in6_addr_is_set(&link->ipv6ll_address)) + return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__); SET_FOREACH(n, link->ndisc_addresses) if (!n->marked) { @@ -1156,7 +1155,7 @@ static int link_acquire_ipv6_conf(Link *link) { if (link->radv) { assert(link->radv); - assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); + assert(in6_addr_is_link_local(&link->ipv6ll_address)); log_link_debug(link, "Starting IPv6 Router Advertisements"); @@ -1173,7 +1172,7 @@ static int link_acquire_ipv6_conf(Link *link) { DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST, DHCP6_CLIENT_START_MODE_SOLICIT)) { assert(link->dhcp6_client); - assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); + assert(in6_addr_is_link_local(&link->ipv6ll_address)); r = dhcp6_request_address(link, link->network->dhcp6_without_ra == DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST); if (r < 0 && r != -EBUSY) @@ -1223,7 +1222,7 @@ static int link_acquire_conf(Link *link) { if (r < 0) return r; - if (!in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) { + if (in6_addr_is_set(&link->ipv6ll_address)) { r = link_acquire_ipv6_conf(link); if (r < 0) return r; diff --git a/src/network/networkd-ndisc.c b/src/network/networkd-ndisc.c index 4b9485b6b27..4f71396fbb7 100644 --- a/src/network/networkd-ndisc.c +++ b/src/network/networkd-ndisc.c @@ -95,7 +95,7 @@ static int ndisc_address_callback(Address *address) { break; } - if (IN6_IS_ADDR_UNSPECIFIED(&router)) { + if (in6_addr_is_null(&router)) { _cleanup_free_ char *buf = NULL; (void) in_addr_to_string(address->family, &address->in_addr, &buf); @@ -649,12 +649,11 @@ static int ndisc_router_generate_addresses(Link *link, struct in6_addr *address, _cleanup_free_ struct in6_addr *new_address = NULL; if (j->address_generation_type == IPV6_TOKEN_ADDRESS_GENERATION_PREFIXSTABLE - && (IN6_IS_ADDR_UNSPECIFIED(&j->prefix) || IN6_ARE_ADDR_EQUAL(&j->prefix, address))) { + && (in6_addr_is_null(&j->prefix) || IN6_ARE_ADDR_EQUAL(&j->prefix, address))) { /* While this loop uses dad_counter and a retry limit as specified in RFC 7217, the loop - does not actually attempt Duplicate Address Detection; the counter will be incremented - only when the address generation algorithm produces an invalid address, and the loop - may exit with an address which ends up being unusable due to duplication on the link. - */ + * does not actually attempt Duplicate Address Detection; the counter will be incremented + * only when the address generation algorithm produces an invalid address, and the loop + * may exit with an address which ends up being unusable due to duplication on the link. */ for (; j->dad_counter < DAD_CONFLICTS_IDGEN_RETRIES_RFC7217; j->dad_counter++) { r = make_stableprivate_address(link, address, prefixlen, j->dad_counter, &new_address); if (r < 0) diff --git a/src/network/networkd-nexthop.c b/src/network/networkd-nexthop.c index 7a5a6481482..76f6ce6d70c 100644 --- a/src/network/networkd-nexthop.c +++ b/src/network/networkd-nexthop.c @@ -279,7 +279,7 @@ static int nexthop_configure(NextHop *nexthop, Link *link) { if (r < 0) return log_link_error_errno(link, r, "Could not append NHA_OIF attribute: %m"); - if (in_addr_is_null(nexthop->family, &nexthop->gw) == 0) { + if (in_addr_is_set(nexthop->family, &nexthop->gw)) { r = netlink_message_append_in_addr_union(req, NHA_GATEWAY, nexthop->family, &nexthop->gw); if (r < 0) return log_link_error_errno(link, r, "Could not append NHA_GATEWAY attribute: %m"); @@ -591,7 +591,7 @@ int config_parse_nexthop_family( return log_oom(); if (isempty(rvalue) && - in_addr_is_null(n->family, &n->gw) != 0) { + !in_addr_is_set(n->family, &n->gw)) { /* Accept an empty string only when Gateway= is null or not specified. */ n->family = AF_UNSPEC; TAKE_PTR(n); @@ -605,7 +605,7 @@ int config_parse_nexthop_family( return 0; } - if (in_addr_is_null(n->family, &n->gw) == 0 && + if (in_addr_is_set(n->family, &n->gw) && ((a == ADDRESS_FAMILY_IPV4 && n->family == AF_INET6) || (a == ADDRESS_FAMILY_IPV6 && n->family == AF_INET))) { log_syntax(unit, LOG_WARNING, filename, line, 0, diff --git a/src/network/networkd-radv.c b/src/network/networkd-radv.c index 39246498e4c..8d8c21c0d8d 100644 --- a/src/network/networkd-radv.c +++ b/src/network/networkd-radv.c @@ -525,8 +525,8 @@ static int radv_set_dns(Link *link, Link *uplink) { p = dns; for (size_t i = 0; i < link->network->n_router_dns; i++) - if (IN6_IS_ADDR_UNSPECIFIED(&link->network->router_dns[i])) { - if (!IN6_IS_ADDR_UNSPECIFIED(&link->ipv6ll_address)) + if (in6_addr_is_null(&link->network->router_dns[i])) { + if (in6_addr_is_set(&link->ipv6ll_address)) *(p++) = link->ipv6ll_address; } else *(p++) = link->network->router_dns[i]; diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c index fca6c032db7..79ea9f2cdff 100644 --- a/src/network/networkd-route.c +++ b/src/network/networkd-route.c @@ -617,15 +617,15 @@ static void log_route_debug(const Route *route, const char *str, const Link *lin _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL, *table = NULL, *scope = NULL, *proto = NULL; - if (!in_addr_is_null(route->family, &route->dst)) { + 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_null(route->family, &route->src)) + if (in_addr_is_set(route->family, &route->src)) (void) in_addr_to_string(route->family, &route->src, &src); - if (in_addr_is_null(route->gw_family, &route->gw) == 0) + if (in_addr_is_set(route->gw_family, &route->gw)) (void) in_addr_to_string(route->gw_family, &route->gw, &gw); - if (!in_addr_is_null(route->family, &route->prefsrc)) + if (in_addr_is_set(route->family, &route->prefsrc)) (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc); (void) route_scope_to_string_alloc(route->scope, &scope); (void) manager_get_route_table_to_string(m, route->table, &table); @@ -648,7 +648,7 @@ static int route_set_netlink_message(const Route *route, sd_netlink_message *req /* link may be NULL */ - if (in_addr_is_null(route->gw_family, &route->gw) == 0) { + if (in_addr_is_set(route->gw_family, &route->gw)) { if (route->gw_family == route->family) { r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->gw_family, &route->gw); if (r < 0) @@ -685,7 +685,7 @@ static int route_set_netlink_message(const Route *route, sd_netlink_message *req return log_link_error_errno(link, r, "Could not set source prefix length: %m"); } - if (in_addr_is_null(route->family, &route->prefsrc) == 0) { + if (in_addr_is_set(route->family, &route->prefsrc)) { r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc); if (r < 0) return log_link_error_errno(link, r, "Could not append RTA_PREFSRC attribute: %m"); @@ -1260,7 +1260,7 @@ int link_set_routes(Link *link) { if (rt->gateway_from_dhcp_or_ra) continue; - if ((in_addr_is_null(rt->gw_family, &rt->gw) != 0 && ordered_set_isempty(rt->multipath_routes)) != (phase == PHASE_NON_GATEWAY)) + if ((!in_addr_is_set(rt->gw_family, &rt->gw) && ordered_set_isempty(rt->multipath_routes)) != (phase == PHASE_NON_GATEWAY)) continue; r = route_configure(rt, link, route_handler, NULL); @@ -2565,7 +2565,7 @@ static int route_section_verify(Route *route, Network *network) { route->priority = IP6_RT_PRIO_USER; if (ordered_hashmap_isempty(network->addresses_by_section) && - in_addr_is_null(route->gw_family, &route->gw) == 0 && + in_addr_is_set(route->gw_family, &route->gw) && route->gateway_onlink < 0) { log_warning("%s: Gateway= without static address configured. " "Enabling GatewayOnLink= option.", diff --git a/src/network/networkd-routing-policy-rule.c b/src/network/networkd-routing-policy-rule.c index ed966f3c173..3260fa89c75 100644 --- a/src/network/networkd-routing-policy-rule.c +++ b/src/network/networkd-routing-policy-rule.c @@ -420,7 +420,7 @@ static int routing_policy_rule_set_netlink_message(const RoutingPolicyRule *rule /* link may be NULL. */ - if (in_addr_is_null(rule->family, &rule->from) == 0) { + if (in_addr_is_set(rule->family, &rule->from)) { r = netlink_message_append_in_addr_union(m, FRA_SRC, rule->family, &rule->from); if (r < 0) return log_link_error_errno(link, r, "Could not append FRA_SRC attribute: %m"); @@ -430,7 +430,7 @@ static int routing_policy_rule_set_netlink_message(const RoutingPolicyRule *rule return log_link_error_errno(link, r, "Could not set source prefix length: %m"); } - if (in_addr_is_null(rule->family, &rule->to) == 0) { + if (in_addr_is_set(rule->family, &rule->to)) { r = netlink_message_append_in_addr_union(m, FRA_DST, rule->family, &rule->to); if (r < 0) return log_link_error_errno(link, r, "Could not append FRA_DST attribute: %m"); diff --git a/src/network/test-network.c b/src/network/test-network.c index 760e33151b9..25ff3a33a89 100644 --- a/src/network/test-network.c +++ b/src/network/test-network.c @@ -34,15 +34,15 @@ static void test_deserialize_in_addr(void) { assert_se((size = deserialize_in_addrs(&addresses, addresses_string)) >= 0); assert_se(size == 3); - assert_se(in_addr_equal(AF_INET, &a, (union in_addr_union *) &addresses[0])); - assert_se(in_addr_equal(AF_INET, &b, (union in_addr_union *) &addresses[1])); - assert_se(in_addr_equal(AF_INET, &c, (union in_addr_union *) &addresses[2])); + assert_se(in4_addr_equal(&a.in, &addresses[0])); + assert_se(in4_addr_equal(&b.in, &addresses[1])); + assert_se(in4_addr_equal(&c.in, &addresses[2])); assert_se((size = deserialize_in6_addrs(&addresses6, addresses_string)) >= 0); assert_se(size == 3); - assert_se(in_addr_equal(AF_INET6, &d, (union in_addr_union *) &addresses6[0])); - assert_se(in_addr_equal(AF_INET6, &e, (union in_addr_union *) &addresses6[1])); - assert_se(in_addr_equal(AF_INET6, &f, (union in_addr_union *) &addresses6[2])); + assert_se(in6_addr_equal(&d.in6, &addresses6[0])); + assert_se(in6_addr_equal(&e.in6, &addresses6[1])); + assert_se(in6_addr_equal(&f.in6, &addresses6[2])); } static void test_deserialize_dhcp_routes(void) { diff --git a/src/nspawn/nspawn-expose-ports.c b/src/nspawn/nspawn-expose-ports.c index 3bce3241021..9d5051d46d9 100644 --- a/src/nspawn/nspawn-expose-ports.c +++ b/src/nspawn/nspawn-expose-ports.c @@ -92,7 +92,7 @@ int expose_port_flush(FirewallContext **fw_ctx, ExposePort* l, int af, union in_ if (!l) return 0; - if (in_addr_is_null(af, exposed)) + if (!in_addr_is_set(af, exposed)) return 0; log_debug("Lost IP address."); @@ -159,7 +159,7 @@ int expose_port_execute(sd_netlink *rtnl, FirewallContext **fw_ctx, ExposePort * p->host_port, &new_exposed, p->container_port, - in_addr_is_null(af, exposed) ? NULL : exposed); + in_addr_is_set(af, exposed) ? exposed : NULL); if (r < 0) log_warning_errno(r, "Failed to modify %s firewall: %m", af_to_name(af)); } diff --git a/src/nss-myhostname/nss-myhostname.c b/src/nss-myhostname/nss-myhostname.c index ffabc60c709..6e37966da08 100644 --- a/src/nss-myhostname/nss-myhostname.c +++ b/src/nss-myhostname/nss-myhostname.c @@ -134,7 +134,7 @@ enum nss_status _nss_myhostname_gethostbyname4_r( r_tuple->next = r_tuple_prev; r_tuple->name = r_name; r_tuple->family = a->family; - r_tuple->scopeid = a->family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&a->address.in6) ? a->ifindex : 0; + r_tuple->scopeid = a->family == AF_INET6 && in6_addr_is_link_local(&a->address.in6) ? a->ifindex : 0; memcpy(r_tuple->addr, &a->address, 16); idx += ALIGN(sizeof(struct gaih_addrtuple)); diff --git a/src/nss-resolve/nss-resolve.c b/src/nss-resolve/nss-resolve.c index 5fcd39ee0a2..dfc0977c849 100644 --- a/src/nss-resolve/nss-resolve.c +++ b/src/nss-resolve/nss-resolve.c @@ -77,7 +77,7 @@ static uint32_t ifindex_to_scopeid(int family, const void *a, int ifindex) { assert(sizeof(in6) == FAMILY_ADDRESS_SIZE(AF_INET6)); memcpy(&in6, a, sizeof(struct in6_addr)); - return IN6_IS_ADDR_LINKLOCAL(&in6) ? ifindex : 0; + return in6_addr_is_link_local(&in6) ? ifindex : 0; } static int json_dispatch_ifindex(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { diff --git a/src/resolve/resolved-dns-rr.c b/src/resolve/resolved-dns-rr.c index 9e64c6dd311..d844f7c9aa9 100644 --- a/src/resolve/resolved-dns-rr.c +++ b/src/resolve/resolved-dns-rr.c @@ -1730,7 +1730,7 @@ bool dns_resource_record_is_link_local_address(DnsResourceRecord *rr) { return in4_addr_is_link_local(&rr->a.in_addr); if (rr->key->type == DNS_TYPE_AAAA) - return IN6_IS_ADDR_LINKLOCAL(&rr->aaaa.in6_addr); + return in6_addr_is_link_local(&rr->aaaa.in6_addr); return false; } diff --git a/src/resolve/resolved-dns-scope.c b/src/resolve/resolved-dns-scope.c index 4495a93add3..f1dff95a86d 100644 --- a/src/resolve/resolved-dns-scope.c +++ b/src/resolve/resolved-dns-scope.c @@ -920,10 +920,10 @@ void dns_scope_process_query(DnsScope *s, DnsStream *stream, DnsPacket *p) { * the LLMNR multicast addresses. See RFC 4795, * section 2.5. */ - if (p->family == AF_INET && !in_addr_equal(AF_INET, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV4_ADDRESS)) + if (p->family == AF_INET && !in4_addr_equal(&p->destination.in, &LLMNR_MULTICAST_IPV4_ADDRESS)) return; - if (p->family == AF_INET6 && !in_addr_equal(AF_INET6, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV6_ADDRESS)) + if (p->family == AF_INET6 && !in6_addr_equal(&p->destination.in6, &LLMNR_MULTICAST_IPV6_ADDRESS)) return; } diff --git a/src/resolve/resolved-etc-hosts.c b/src/resolve/resolved-etc-hosts.c index 0c35d91a83d..65b815c43c3 100644 --- a/src/resolve/resolved-etc-hosts.c +++ b/src/resolve/resolved-etc-hosts.c @@ -61,7 +61,7 @@ static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) { return 0; } - r = in_addr_is_null(address.family, &address.address); + r = in_addr_data_is_null(&address); if (r < 0) { log_warning_errno(r, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr, address_str); return 0; diff --git a/src/shared/resolve-util.c b/src/shared/resolve-util.c index 1023b62499b..6541d231ed5 100644 --- a/src/shared/resolve-util.c +++ b/src/shared/resolve-util.c @@ -33,7 +33,7 @@ bool dns_server_address_valid(int family, const union in_addr_union *sa) { /* Refuses the 0 IP addresses as well as 127.0.0.53 (which is our own DNS stub) */ - if (in_addr_is_null(family, sa)) + if (!in_addr_is_set(family, sa)) return false; if (family == AF_INET && sa->in.s_addr == htobe32(INADDR_DNS_STUB)) From bb3b08ad983a15fe948e20443376ddbf0cab6ce7 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 18 Feb 2021 02:50:50 +0900 Subject: [PATCH 06/10] resolve: make manager_find_ifindex() or friends return earlier --- src/resolve/resolved-link.c | 6 ++++++ src/resolve/resolved-manager.c | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/resolve/resolved-link.c b/src/resolve/resolved-link.c index d9192d99f65..18dc3d29e9c 100644 --- a/src/resolve/resolved-link.c +++ b/src/resolve/resolved-link.c @@ -697,6 +697,12 @@ LinkAddress *link_find_address(Link *l, int family, const union in_addr_union *i assert(l); + if (!IN_SET(family, AF_INET, AF_INET6)) + return NULL; + + if (!in_addr) + return NULL; + LIST_FOREACH(addresses, a, l->addresses) if (a->family == family && in_addr_equal(family, &a->in_addr, in_addr)) return a; diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index a2a2cf49547..6cc3a5d56f7 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -1153,6 +1153,12 @@ int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_a assert(m); + if (!IN_SET(family, AF_INET, AF_INET6)) + return 0; + + if (!in_addr) + return 0; + a = manager_find_link_address(m, family, in_addr); if (a) return a->link->ifindex; @@ -1248,6 +1254,12 @@ LinkAddress* manager_find_link_address(Manager *m, int family, const union in_ad assert(m); + if (!IN_SET(family, AF_INET, AF_INET6)) + return NULL; + + if (!in_addr) + return NULL; + HASHMAP_FOREACH(l, m->links) { LinkAddress *a; From b1dea5cffa619857c11b33cd3714e19c98c6605a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 18 Feb 2021 02:51:09 +0900 Subject: [PATCH 07/10] resolve: use sockaddr_in_addr() --- src/resolve/resolved-dns-stream.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resolve/resolved-dns-stream.c b/src/resolve/resolved-dns-stream.c index cf5057ddf09..2ab6f6236dc 100644 --- a/src/resolve/resolved-dns-stream.c +++ b/src/resolve/resolved-dns-stream.c @@ -187,7 +187,7 @@ static int dns_stream_identify(DnsStream *s) { /* If we don't know the interface index still, we look for the * first local interface with a matching address. Yuck! */ if (s->ifindex <= 0) - s->ifindex = manager_find_ifindex(s->manager, s->local.sa.sa_family, s->local.sa.sa_family == AF_INET ? (union in_addr_union*) &s->local.in.sin_addr : (union in_addr_union*) &s->local.in6.sin6_addr); + s->ifindex = manager_find_ifindex(s->manager, s->local.sa.sa_family, sockaddr_in_addr(&s->local.sa)); if (s->protocol == DNS_PROTOCOL_LLMNR && s->ifindex > 0) { /* Make sure all packets for this connection are sent on the same interface */ From 5380707abae5633c86b119badee16161ef8edc04 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 18 Feb 2021 03:22:28 +0900 Subject: [PATCH 08/10] network: use in_addr_prefix_to_string() --- src/libsystemd-network/sd-radv.c | 66 +++++++++++----------- src/network/networkd-address-pool.c | 4 +- src/network/networkd-address.c | 10 ++-- src/network/networkd-dhcp6.c | 42 +++++++------- src/network/networkd-link.c | 4 +- src/network/networkd-ndisc.c | 6 +- src/network/networkd-route.c | 14 ++--- src/network/networkd-routing-policy-rule.c | 8 +-- 8 files changed, 73 insertions(+), 81 deletions(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index b34263568e1..eac9b159ce0 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -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; + if (r < 0) + return r; + if (r == 0) + continue; - (void) in_addr_to_string(AF_INET6, - (union in_addr_union*) &p->opt.in6_addr, - &addr_p); + if (dynamic && cur->opt.prefixlen == p->opt.prefixlen) + goto update; - if (dynamic && cur->opt.prefixlen == p->opt.prefixlen) - goto update; + _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)); - (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); - - 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; diff --git a/src/network/networkd-address-pool.c b/src/network/networkd-address-pool.c index c60ece5db8c..79a1c1b847d 100644 --- a/src/network/networkd-address-pool.c +++ b/src/network/networkd-address-pool.c @@ -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; diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c index dd7c28ec6e7..96c9376bede 100644 --- a/src/network/networkd-address.c +++ b/src/network/networkd-address.c @@ -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; } } diff --git a/src/network/networkd-dhcp6.c b/src/network/networkd-dhcp6.c index bb37a0bf715..b36efd811c4 100644 --- a/src/network/networkd-dhcp6.c +++ b/src/network/networkd-dhcp6.c @@ -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); diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index b3cf43f4a83..8f97724ce6b 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -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) diff --git a/src/network/networkd-ndisc.c b/src/network/networkd-ndisc.c index 4f71396fbb7..d9776940a41 100644 --- a/src/network/networkd-ndisc.c +++ b/src/network/networkd-ndisc.c @@ -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; } diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c index 79ea9f2cdff..e21c456b0af 100644 --- a/src/network/networkd-route.c +++ b/src/network/networkd-route.c @@ -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))); } diff --git a/src/network/networkd-routing-policy-rule.c b/src/network/networkd-routing-policy-rule.c index 3260fa89c75..a7fddfd58fc 100644 --- a/src/network/networkd-routing-policy-rule.c +++ b/src/network/networkd-routing-policy-rule.c @@ -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)); } } From 94af46fc66779ddaaff09c4dc704637c3158a3c5 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 18 Feb 2021 03:27:35 +0900 Subject: [PATCH 09/10] network: use temporary buffer for safety --- src/network/networkd-address.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c index 96c9376bede..f55f9fafbe7 100644 --- a/src/network/networkd-address.c +++ b/src/network/networkd-address.c @@ -1457,6 +1457,7 @@ int config_parse_broadcast( Network *network = userdata; _cleanup_(address_free_or_set_invalidp) Address *n = NULL; + union in_addr_union u; int r; assert(filename); @@ -1480,13 +1481,14 @@ int config_parse_broadcast( return 0; } - r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast); + r = in_addr_from_string(AF_INET, rvalue, &u); if (r < 0) { log_syntax(unit, LOG_WARNING, filename, line, r, "Broadcast is invalid, ignoring assignment: %s", rvalue); return 0; } + n->broadcast = u.in; n->family = AF_INET; TAKE_PTR(n); From c633628dafaca7bdbb5995087e56da54fe20abb6 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 18 Feb 2021 03:33:39 +0900 Subject: [PATCH 10/10] tree-wide: constify variables if possible --- src/libsystemd-network/sd-ndisc.c | 2 +- src/libsystemd-network/sd-radv.c | 20 ++++++++++---------- src/network/networkd-ndisc.c | 4 ++-- src/systemctl/systemctl-show.c | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/libsystemd-network/sd-ndisc.c b/src/libsystemd-network/sd-ndisc.c index d02a2a9279a..f704445654e 100644 --- a/src/libsystemd-network/sd-ndisc.c +++ b/src/libsystemd-network/sd-ndisc.c @@ -225,7 +225,7 @@ static int ndisc_recv(sd_event_source *s, int fd, uint32_t revents, void *userda if (r < 0) { switch (r) { case -EADDRNOTAVAIL: - (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &rt->address, &addr); + (void) in_addr_to_string(AF_INET6, (const union in_addr_union*) &rt->address, &addr); log_ndisc("Received RA from non-link-local address %s. Ignoring", addr); break; diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index eac9b159ce0..164b24c6847 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -244,7 +244,7 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat if (r < 0) { switch (r) { case -EADDRNOTAVAIL: - (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &src, &addr); + (void) in_addr_to_string(AF_INET6, (const union in_addr_union*) &src, &addr); log_radv("Received RS from non-link-local address %s. Ignoring", addr); break; @@ -272,7 +272,7 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat return 0; } - (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &src, &addr); + (void) in_addr_to_string(AF_INET6, (const union in_addr_union*) &src, &addr); r = radv_send(ra, &src, ra->lifetime); if (r < 0) @@ -540,15 +540,15 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) { return -ENOEXEC; (void) in_addr_prefix_to_string(AF_INET6, - (union in_addr_union*) &p->opt.in6_addr, + (const 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, - (union in_addr_union*) &cur->opt.in6_addr, + (const union in_addr_union*) &cur->opt.in6_addr, cur->opt.prefixlen, - (union in_addr_union*) &p->opt.in6_addr, + (const union in_addr_union*) &p->opt.in6_addr, p->opt.prefixlen); if (r < 0) return r; @@ -560,7 +560,7 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) { _cleanup_free_ char *addr_cur = NULL; (void) in_addr_prefix_to_string(AF_INET6, - (union in_addr_union*) &cur->opt.in6_addr, + (const 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)); @@ -656,15 +656,15 @@ _public_ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p, int return -EINVAL; (void) in_addr_prefix_to_string(AF_INET6, - (union in_addr_union*) &p->opt.in6_addr, + (const union in_addr_union*) &p->opt.in6_addr, p->opt.prefixlen, &pretty); LIST_FOREACH(prefix, cur, ra->route_prefixes) { r = in_addr_prefix_intersect(AF_INET6, - (union in_addr_union*) &cur->opt.in6_addr, + (const union in_addr_union*) &cur->opt.in6_addr, cur->opt.prefixlen, - (union in_addr_union*) &p->opt.in6_addr, + (const union in_addr_union*) &p->opt.in6_addr, p->opt.prefixlen); if (r < 0) return r; @@ -676,7 +676,7 @@ _public_ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p, int _cleanup_free_ char *addr = NULL; (void) in_addr_prefix_to_string(AF_INET6, - (union in_addr_union*) &cur->opt.in6_addr, + (const 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)); diff --git a/src/network/networkd-ndisc.c b/src/network/networkd-ndisc.c index d9776940a41..7af8e620a08 100644 --- a/src/network/networkd-ndisc.c +++ b/src/network/networkd-ndisc.c @@ -150,7 +150,7 @@ static int ndisc_remove_old_one(Link *link, const struct in6_addr *router, bool if (DEBUG_LOGGING) { _cleanup_free_ char *buf = NULL; - (void) in_addr_to_string(AF_INET6, (union in_addr_union *) router, &buf); + (void) in_addr_to_string(AF_INET6, (const union in_addr_union*) router, &buf); log_link_debug(link, "No SLAAC address obtained from %s is ready. " "The old NDisc information will be removed later.", strna(buf)); @@ -162,7 +162,7 @@ static int ndisc_remove_old_one(Link *link, const struct in6_addr *router, bool if (DEBUG_LOGGING) { _cleanup_free_ char *buf = NULL; - (void) in_addr_to_string(AF_INET6, (union in_addr_union *) router, &buf); + (void) in_addr_to_string(AF_INET6, (const union in_addr_union*) router, &buf); log_link_debug(link, "Removing old NDisc information obtained from %s.", strna(buf)); } diff --git a/src/systemctl/systemctl-show.c b/src/systemctl/systemctl-show.c index 6f3b3566d73..6410a419447 100644 --- a/src/systemctl/systemctl-show.c +++ b/src/systemctl/systemctl-show.c @@ -1474,7 +1474,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8) continue; - if (in_addr_prefix_to_string(family, (union in_addr_union *) ap, prefixlen, &str) < 0) + if (in_addr_prefix_to_string(family, (const union in_addr_union*) ap, prefixlen, &str) < 0) continue; if (!strextend_with_separator(&addresses, " ", str))