network: do not remove localhost address
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 31 Jan 2022 10:26:51 +0000 (19:26 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 1 Feb 2022 02:30:37 +0000 (11:30 +0900)
Managing loopback interfaces by networkd is not recommended, but supporeted.
Even such spurious situation, do not drop the localhost addresses.

src/basic/in-addr-util.c
src/basic/in-addr-util.h
src/network/networkd-address.c

index a43a831991253002656628bac09a5d9e23cf4816..660c1f18247c93417b523fac20437d1b0ad85718 100644 (file)
@@ -121,6 +121,19 @@ int in_addr_is_localhost(int family, const union in_addr_union *u) {
         return -EAFNOSUPPORT;
 }
 
+int in_addr_is_localhost_one(int family, const union in_addr_union *u) {
+        assert(u);
+
+        if (family == AF_INET)
+                /* 127.0.0.1 */
+                return be32toh(u->in.s_addr) == UINT32_C(0x7F000001);
+
+        if (family == AF_INET6)
+                return IN6_IS_ADDR_LOOPBACK(&u->in6); /* lgtm [cpp/potentially-dangerous-function] */
+
+        return -EAFNOSUPPORT;
+}
+
 bool in6_addr_is_ipv4_mapped_address(const struct in6_addr *a) {
         return a->s6_addr32[0] == 0 &&
                 a->s6_addr32[1] == 0 &&
index 0178391e5f252cb91da63cfb1166578f2084df81..5de87a9539f472845a988d25a1137be0389494d0 100644 (file)
@@ -49,6 +49,7 @@ bool in6_addr_is_link_local_all_nodes(const struct in6_addr *a);
 
 bool in4_addr_is_localhost(const struct in_addr *a);
 int in_addr_is_localhost(int family, const union in_addr_union *u);
+int in_addr_is_localhost_one(int family, const union in_addr_union *u);
 
 bool in4_addr_is_local_multicast(const struct in_addr *a);
 bool in4_addr_is_non_local(const struct in_addr *a);
index b5091bb5980eef907dd19c19d539afe371d16782..44f77db3be62387b504f4ca86cb34ae6f36e9f12 100644 (file)
@@ -854,6 +854,10 @@ int link_drop_foreign_addresses(Link *link) {
                 if (address->family == AF_INET6 && in6_addr_is_link_local(&address->in_addr.in6))
                         continue;
 
+                /* Do not remove localhost address (127.0.0.1 and ::1) */
+                if (link->flags & IFF_LOOPBACK && in_addr_is_localhost_one(address->family, &address->in_addr) > 0)
+                        continue;
+
                 /* Ignore addresses we configured. */
                 if (address->source != NETWORK_CONFIG_SOURCE_FOREIGN)
                         continue;