[RFC] netifd: add support for dhcp fallback to static ip

Roman Yeryomin roman at advem.lv
Sat Jul 13 17:34:57 PDT 2024


It is pretty common use case for a network device to be configured
as DHCP client but having some fallback static IP address where it
would be reachable for, e.g., configuration.

This reacts on udhcpc events and sets/removes ipaddr which is
configured on an iface from network config.
This means that for an interface with proto set to dhcp but with
fallback static IP disabled (ipaddr parameter is not set in config)
cost of this feature is one uci call.
If static fallback is enabled (ipaddr is present) then the cost is
3 uci calls and one ip call for each deconfig/renew/bound udhcpc
event.
It is not nothing but to me it seem like it's a very small overhead
for such a convenience and I think it should be OpenWrt default at
least for targets with one ethernet port.

Signed-off-by: Roman Yeryomin <roman at advem.lv>
---
 package/network/config/netifd/Makefile        |  1 -
 .../files/etc/udhcpc.user.d/fallbackip.sh     | 23 +++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 package/network/config/netifd/files/etc/udhcpc.user.d/fallbackip.sh

diff --git a/package/network/config/netifd/Makefile b/package/network/config/netifd/Makefile
index d80c2eeed6..8ef391d913 100644
--- a/package/network/config/netifd/Makefile
+++ b/package/network/config/netifd/Makefile
@@ -42,7 +42,6 @@ define Package/netifd/install
 	$(INSTALL_DIR) $(1)/sbin
 	$(INSTALL_BIN) $(PKG_BUILD_DIR)/netifd $(1)/sbin/
 	$(CP) ./files/* $(1)/
-	$(INSTALL_DIR) $(1)/etc/udhcpc.user.d/
 	$(CP) \
 		$(PKG_BUILD_DIR)/scripts/utils.sh \
 		$(PKG_BUILD_DIR)/scripts/netifd-proto.sh \
diff --git a/package/network/config/netifd/files/etc/udhcpc.user.d/fallbackip.sh b/package/network/config/netifd/files/etc/udhcpc.user.d/fallbackip.sh
new file mode 100644
index 0000000000..79984e9a2f
--- /dev/null
+++ b/package/network/config/netifd/files/etc/udhcpc.user.d/fallbackip.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+[ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1
+
+ipaddr=$(uci get network.$INTERFACE.ipaddr)
+[ -z "$ipaddr" ] && exit 0
+
+ifname=$(uci get network.$INTERFACE.ifname)
+[ -z "$ifname" ] && exit 0
+
+netmask=$(uci get network.$INTERFACE.netmask)
+[ -z "$netmask" ] && netmask=24
+
+case "$1" in
+	deconfig)
+		ip ad add $ipaddr/$netmask dev $ifname
+	;;
+	renew|bound)
+		ip ad del $ipaddr dev $ifname
+	;;
+esac
+
+exit 0
-- 
2.43.0




More information about the openwrt-devel mailing list