diff mbox series

netifd: add support for dhcp fallback to static ip

Message ID 20240714193915.12764-1-roman@advem.lv
State New
Headers show
Series netifd: add support for dhcp fallback to static ip | expand

Commit Message

Roman Yeryomin July 14, 2024, 7:39 p.m. UTC
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@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(-)

Comments

Roman Yeryomin July 24, 2024, 8:15 p.m. UTC | #1
On 2024-07-14 22:39, Roman Yeryomin wrote:
> 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.
> 

Hi Felix, I hope I got your points on this implementation in the right 
way.
Would be nice to hear what is your word on this!

Regards,
Roman
diff mbox series

Patch

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 \