contrib/meshwizard: Also setup a dhcp server on the adhoc interface if VAP is used...
[project/luci.git] / contrib / package / meshwizard / files / usr / bin / meshwizard / helpers / setup_network.sh
index 493c701..f0ed0e7 100755 (executable)
@@ -1,29 +1,41 @@
 # setup entry in /etc/config/network for a interface
 # Argument $1: network interface
-
 net="$1"
-. /etc/functions.sh
+. /lib/functions.sh
 . $dir/functions.sh
 
-# Delete the network interface section for $net
-if [ "$cleanup" == 1 ]; then
-       section_cleanup network.$netrenamed
-fi
-
 # Setup a (new) interface section for $net
 
-ipaddr=$(uci get meshwizard.netconfig.$net\_ip4addr)
+ipaddr=$(uci -q get meshwizard.netconfig.$net\_ip4addr)
+ip6addr=$(uci -q get meshwizard.netconfig.$net\_ip6addr)
 [ -z "$ipaddr" ] && msg_missing_value meshwizard $net\_ip4addr
-[ -z "$interface_netmask" ] && interface netmask="255.255.0.0"
+
+netmask=$(uci -q get meshwizard.netconfig.$net\_netmask)
+[ -z "$netmask" ] && netmask="$interface_netmask"
+[ -z "$netmask" ] && netmask="255.255.0.0"
+
+uci set network.$netrenamed="interface"
+set_defaults "interface_" network.$netrenamed
 
 uci batch << EOF
-set network.$netrenamed="interface"
-set network.$netrenamed.proto="static"
-set network.$netrenamed.ipaddr="$ipaddr"
-set network.$netrenamed.netmask="$interface_netmask"
-set network.$netrenamed.dns="$interface_dns"
+       set network.$netrenamed.proto="static"
+       set network.$netrenamed.ipaddr="$ipaddr"
+       set network.$netrenamed.netmask="$netmask"
 EOF
 
+# Setup IPv6 for the interface
+local ip6addr
+if [ "$ipv6_enabled" = 1 ]; then
+       if [ "$ipv6_config" = "auto-ipv6-dhcpv6" ]; then
+               ip6addr="$($dir/helpers/gen_auto-ipv6-dhcpv6-ip.sh $netrenamed)"
+               uci set network.$netrenamed.ip6addr="${ip6addr}/112"
+       fi
+       if [ "$ipv6_config" = "static" ] && [ -n "$ip6addr" ]; then
+               uci set network.$netrenamed.ip6addr="$ip6addr"
+       fi
+fi
+
 uci_commitverbose "Setup interface $netrenamed" network
 
 # setup dhcp alias/interface
@@ -36,21 +48,17 @@ if [ "$net_dhcp" == 1 ]; then
        interface_ip="$(uci -q get meshwizard.netconfig.${net}_ip4addr)"
        vap=$(uci -q get meshwizard.netconfig.${net}_vap)
 
-       # Clean/rename config
+       # Rename config
        handle_dhcpalias() {
                        config_get interface "$1" interface
                        if [ "$interface" == "$netrenamed" ]; then
-                               if [ "$cleanup" == 1 ]; then
-                                       section_cleanup network.$1
-                               else
-                                       if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
-                                               section_rename network $1 ${netrenamed}dhcp
-                                       fi
+                               if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
+                                       section_rename network $1 ${netrenamed}dhcp
                                fi
                        fi
        }
        config_load network
-       config_foreach handle_dhcpalias alias
+       config_foreach handle_dhcpalias interface
 
        # Get IP/netmask and start-ip for $net dhcp
        # If no dhcprange is given in /etc/config/meshwizard we autogenerate one
@@ -59,25 +67,48 @@ if [ "$net_dhcp" == 1 ]; then
                dhcprange="$($dir/helpers/gen_dhcp_ip.sh $interface_ip)/24"
                uci set meshwizard.netconfig.${net}_dhcprange="$dhcprange"
        fi
-       eval $(sh $dir/helpers/ipcalc-cidr.sh $dhcprange 1 0)
-
-       # setup wifi-dhcp interface or alias
 
-       # Setup alias for $net
+       # If we use VAP then cut the dhcp range in two halves
+       # one for the adhoc, one for the managed VAP interface
 
        if [ "$vap" == 1 ]; then
-               uci set network.${netrenamed}dhcp=interface
+               local network
+               local mask
+               network=${dhcprange%%/*}
+               mask=${dhcprange##*/}
+               # Divide network size by adding 1 to the netmask
+               mask=$(($mask + 1))
+               # Get first ip and netmask for the adhoc dhcp network
+               eval $(sh $dir/helpers/ipcalc-cidr.sh ${network}/${mask} 1 0)
+               STARTADHOC=$START
+               NETMASKADHOC=$NETMASK
+               # Get first ip and netmask for the managed dhcp network
+               eval $(sh $dir/helpers/ipcalc-cidr.sh ${NEXTNET}/${mask} 1 0)
+               STARTVAP=$START
+               NETMASKVAP=$NETMASK
+               # Add dhcp interface
+               uci batch <<- EOF
+                       set network.${netrenamed}dhcp=interface
+                       set network.${netrenamed}dhcp.proto=static
+                       set network.${netrenamed}dhcp.ipaddr="$STARTVAP"
+                       set network.${netrenamed}dhcp.netmask="$NETMASKVAP"
+               EOF
+               uci_commitverbose  "Setup interface for ${netrenamed}dhcp" network
+
        else
-               uci set network.${netrenamed}dhcp=alias
-               uci set network.${netrenamed}dhcp.interface="$netrenamed"
+               eval $(sh $dir/helpers/ipcalc-cidr.sh $dhcprange 1 0)
+               STARTADHOC=$START
+               NETMASKADHOC=$NETMASK
        fi
 
-       uci batch << EOF
-set network.${netrenamed}dhcp.proto=static
-set network.${netrenamed}dhcp.ipaddr="$START"
-set network.${netrenamed}dhcp.netmask="$NETMASK"
-EOF
+       # Setup alias for $net adhoc interface
 
+       uci batch <<- EOF
+               set network.${netrenamed}ahdhcp=interface
+               set network.${netrenamed}ahdhcp.ifname="@${netrenamed}"
+               set network.${netrenamed}ahdhcp.proto=static
+               set network.${netrenamed}ahdhcp.ipaddr="$STARTADHOC"
+               set network.${netrenamed}ahdhcp.netmask="$NETMASKADHOC"
+       EOF
+       uci_commitverbose  "Setup interface for ${netrenamed}ahdhcp" network
 fi
-
-uci_commitverbose  "Setup interface for ${netrenamed}dhcp" network