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

Roman Yeryomin roman at advem.lv
Sun Jul 14 12:39:15 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 adds 'fallbackip' network config option, which will be used
on an interface until device receives an IP from DHCP server.

Signed-off-by: Roman Yeryomin <roman at advem.lv>
---
 .../network/config/netifd/files/lib/netifd/dhcp.script    | 8 +++++++-
 .../network/config/netifd/files/lib/netifd/proto/dhcp.sh  | 7 +++++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/package/network/config/netifd/files/lib/netifd/dhcp.script b/package/network/config/netifd/files/lib/netifd/dhcp.script
index db8deac9e6..da7c30f72c 100755
--- a/package/network/config/netifd/files/lib/netifd/dhcp.script
+++ b/package/network/config/netifd/files/lib/netifd/dhcp.script
@@ -98,7 +98,13 @@ setup_interface () {
 }
 
 deconfig_interface() {
-	proto_init_update "*" 0
+	if [ -z "$FALLBACKIP" ]; then
+		proto_init_update "*" 0
+	else
+		proto_init_update "*" 1
+		proto_add_ipv4_address "$FALLBACKIP" "255.255.255.0"
+	fi
+
 	proto_send_update "$INTERFACE"
 }
 
diff --git a/package/network/config/netifd/files/lib/netifd/proto/dhcp.sh b/package/network/config/netifd/files/lib/netifd/proto/dhcp.sh
index 636b4654ff..020038f0f8 100755
--- a/package/network/config/netifd/files/lib/netifd/proto/dhcp.sh
+++ b/package/network/config/netifd/files/lib/netifd/proto/dhcp.sh
@@ -10,6 +10,7 @@ proto_dhcp_init_config() {
 	renew_handler=1
 
 	proto_config_add_string 'ipaddr:ipaddr'
+	proto_config_add_string 'fallbackip:fallbackip'
 	proto_config_add_string 'hostname:hostname'
 	proto_config_add_string clientid
 	proto_config_add_string vendorid
@@ -35,8 +36,8 @@ proto_dhcp_setup() {
 	local config="$1"
 	local iface="$2"
 
-	local ipaddr hostname clientid vendorid broadcast norelease reqopts defaultreqopts iface6rd sendopts delegate zone6rd zone mtu6rd customroutes classlessroute
-	json_get_vars ipaddr hostname clientid vendorid broadcast norelease reqopts defaultreqopts iface6rd delegate zone6rd zone mtu6rd customroutes classlessroute
+	local ipaddr fallbackip hostname clientid vendorid broadcast norelease reqopts defaultreqopts iface6rd sendopts delegate zone6rd zone mtu6rd customroutes classlessroute
+	json_get_vars ipaddr fallbackip hostname clientid vendorid broadcast norelease reqopts defaultreqopts iface6rd delegate zone6rd zone mtu6rd customroutes classlessroute
 
 	local opt dhcpopts
 	for opt in $reqopts; do
@@ -63,6 +64,8 @@ proto_dhcp_setup() {
 	[ "$classlessroute" = "0" ] || append dhcpopts "-O 121"
 
 	proto_export "INTERFACE=$config"
+	proto_export "FALLBACKIP=$fallbackip"
+
 	proto_run_command "$config" udhcpc \
 		-p /var/run/udhcpc-$iface.pid \
 		-s /lib/netifd/dhcp.script \
-- 
2.43.0




More information about the openwrt-devel mailing list