ip addr add command adds a new IP address to an interface. However, when
configuring a static IP address, it results in an error:
    "RTNETLINK answers: File exists"

This causes the ifup command to fail with an exit status of 2, which can
occur if the same IP address or default route is already configured by
other means, such as through a kernel command line in NFS root
environments.

To solve this, the ip addr replace and ip route replace commands are
used instead. These commands either add the IP address or route if they
do not exist, or replace them if they do, without raising an error. This
ensures that ifup does not fail when the IP address or route is already
present.

Signed-off-by: Valeria Petrov <[email protected]>
---
 networking/ifupdown.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/networking/ifupdown.c b/networking/ifupdown.c
index 9c3640be7..9379dda30 100644
--- a/networking/ifupdown.c
+++ b/networking/ifupdown.c
@@ -484,10 +484,10 @@ static int FAST_FUNC static_up6(struct interface_defn_t 
*ifd, execfn *exec)
 {
        int result;
 # if ENABLE_FEATURE_IFUPDOWN_IP
-       result = execute("ip addr add %address%/%netmask% dev %iface%[[ label 
%label%]]", ifd, exec);
+       result = execute("ip addr replace %address%/%netmask% dev %iface%[[ 
label %label%]]", ifd, exec);
        result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] 
%iface% up", ifd, exec);
        /* Reportedly, IPv6 needs "dev %iface%", but IPv4 does not: */
-       result += execute("[[ip route add ::/0 via %gateway% dev %iface%]][[ 
metric %metric%]]", ifd, exec);
+       result += execute("[[ip route replace ::/0 via %gateway% dev 
%iface%]][[ metric %metric%]]", ifd, exec);
 # else
        result = execute("ifconfig %iface%[[ media %media%]][[ hw 
%hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
        result += execute("ifconfig %iface% add %address%/%netmask%", ifd, 
exec);
@@ -574,10 +574,10 @@ static int FAST_FUNC static_up(struct interface_defn_t 
*ifd, execfn *exec)
 {
        int result;
 # if ENABLE_FEATURE_IFUPDOWN_IP
-       result = execute("ip addr add %address%/%bnmask%[[ broadcast 
%broadcast%]] "
+       result = execute("ip addr replace %address%/%bnmask%[[ broadcast 
%broadcast%]] "
                        "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", 
ifd, exec);
        result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] 
%iface% up", ifd, exec);
-       result += execute("[[ip route add default via %gateway% dev %iface%[[ 
metric %metric%]]]]", ifd, exec);
+       result += execute("[[ip route replace default via %gateway% dev 
%iface%[[ metric %metric%]]]]", ifd, exec);
        return ((result == 3) ? 3 : 0);
 # else
        /* ifconfig said to set iface up before it processes hw %hwaddress%,
-- 
2.25.1

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to