Make OpenWRT IP address changable dynamically based on base_instance_num am: 6df3b6e14d

Original change: https://android-review.googlesource.com/c/platform/external/openwrt-prebuilts/+/2369050

Change-Id: I8a03745f3437a3156149fc4b139bc26f4993f469
Signed-off-by: Automerger Merge Worker <[email protected]>
diff --git a/shared/config/network b/shared/config/network
index ab71eb2..1d76e21 100644
--- a/shared/config/network
+++ b/shared/config/network
@@ -33,21 +33,16 @@
 config interface 'wan'
 	option device 'br-lan'
 	option proto 'static'
-	option netmask '255.255.255.0'
-	option ipaddr '192.168.96.2'
+	option netmask '255.255.255.252'
 	option ip6assign '30'
-	option gateway '192.168.96.1'
 	option dns '8.8.8.8'
-	option broadcast '192.168.96.3'
 
 config interface 'wifi0'
-	option proto 'static'
-	option ipaddr '192.168.2.1'
-	option netmask '255.255.255.0'
 	option device 'br-wifi0'
+	option proto 'static'
+	option netmask '255.255.255.192'
 
 config interface 'wifi1'
-	option proto 'static'
-	option ipaddr '192.168.3.1'
-	option netmask '255.255.255.0'
 	option device 'br-wifi1'
+	option proto 'static'
+	option netmask '255.255.255.192'
diff --git a/shared/uci-defaults/0_default_config b/shared/uci-defaults/0_default_config
index 093006f..1f82066 100644
--- a/shared/uci-defaults/0_default_config
+++ b/shared/uci-defaults/0_default_config
@@ -1,4 +1,50 @@
 #!/bin/sh
+
 opkg install /ipks/*
 ip link set eth0 mtu 1460
+
+# Extract instance_num of CF instance. The default value is 1.
+instance_num=1
+bridged_host_network=false
+words=$(cat /proc/cmdline)
+while
+    word=${words%%" "*}
+    if echo "$word" | grep "instance_num"; then
+        instance_num=${word#*"="}
+    fi
+    if echo "$word" | grep "bridged_host_network=true"; then
+        bridged_host_network=true
+    fi
+    next=${words#*" "}
+    [ "$words" != "$next" ]
+do
+    words=$next
+done
+
+# Setup wan based on instance_num. Interface wan will occupy 192.168.96.X.
+rule_name=$(uci add network rule)
+if $bridged_host_network; then
+    uci set network.wan.gateway="192.168.96.1"
+    uci set network.wan.ipaddr="192.168.96.2"
+    uci set network.wan.broadcast="192.168.96.3"
+else
+    d_class_wan_gateway=$(expr $instance_num \* 4 - 3);
+    d_class_wan_ipaddr=$(expr $instance_num \* 4 - 2);
+    d_class_wan_broadcast=$(expr $instance_num \* 4 - 1);
+    uci set network.wan.gateway="192.168.96."$d_class_wan_gateway
+    uci set network.wan.ipaddr="192.168.96."$d_class_wan_ipaddr
+    uci set network.wan.broadcast="192.168.96."$d_class_wan_broadcast
+fi
+
+# Setup wifi0 and wifi1 based on instance_num.
+# Interfaces wifi0 and wifi1 will occupy 192.168.2.X - 192.168.33.X.
+c_class_wifi=$(expr \( $instance_num + 3 \) / 2)
+d_class_wifi0=$(expr \( $instance_num % 2 \* 128 \) + 1)
+d_class_wifi1=$(expr \( $instance_num % 2 \* 128 \) + 65)
+uci set network.wifi0.ipaddr="192.168."$c_class_wifi"."$d_class_wifi0
+uci set network.wifi1.ipaddr="192.168."$c_class_wifi"."$d_class_wifi1
+
+uci commit
+
+# Regarding hostapd issue of OpenWRT 22.03.X versions, reboot it.
 reboot