1d4c3ba7e196be9b896228c5b794e57c04b90aa0
[project/luci.git] / contrib / package / meshwizard / files / usr / bin / meshwizard / helpers / setup_network.sh
1 # setup entry in /etc/config/network for a interface
2 # Argument $1: network interface
3
4 net="$1"
5 . /etc/functions.sh
6 . $dir/functions.sh
7
8 # Setup a (new) interface section for $net
9
10 ipaddr=$(uci get meshwizard.netconfig.$net\_ip4addr)
11 [ -z "$ipaddr" ] && msg_missing_value meshwizard $net\_ip4addr
12 [ -z "$interface_netmask" ] && interface netmask="255.255.0.0"
13
14 uci batch << EOF
15 set network.$netrenamed="interface"
16 set network.$netrenamed.proto="static"
17 set network.$netrenamed.ipaddr="$ipaddr"
18 EOF
19 set_defaults "interface_" network.$netrenamed
20 uci_commitverbose "Setup interface $netrenamed" network
21
22 # setup dhcp alias/interface
23
24 net_dhcp=$(uci -q get meshwizard.netconfig.${net}_dhcp)
25 if [ "$net_dhcp" == 1 ]; then
26
27         # Load meshwizard_settings
28         dhcprange="$(uci -q get meshwizard.netconfig.${net}_dhcprange)"
29         interface_ip="$(uci -q get meshwizard.netconfig.${net}_ip4addr)"
30         vap=$(uci -q get meshwizard.netconfig.${net}_vap)
31
32         # Rename config
33         handle_dhcpalias() {
34                         config_get interface "$1" interface
35                         if [ "$interface" == "$netrenamed" ]; then
36                                 if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
37                                         section_rename network $1 ${netrenamed}dhcp
38                                 fi
39                         fi
40         }
41         config_load network
42         config_foreach handle_dhcpalias alias
43
44         # Get IP/netmask and start-ip for $net dhcp
45         # If no dhcprange is given in /etc/config/meshwizard we autogenerate one
46
47         if [ -z "$dhcprange" ]; then
48                 dhcprange="$($dir/helpers/gen_dhcp_ip.sh $interface_ip)/24"
49                 uci set meshwizard.netconfig.${net}_dhcprange="$dhcprange"
50         fi
51         eval $(sh $dir/helpers/ipcalc-cidr.sh $dhcprange 1 0)
52
53         # setup wifi-dhcp interface or alias
54
55         # Setup alias for $net
56
57         if [ "$vap" == 1 ]; then
58                 uci set network.${netrenamed}dhcp=interface
59         else
60                 uci set network.${netrenamed}dhcp=alias
61                 uci set network.${netrenamed}dhcp.interface="$netrenamed"
62         fi
63
64         uci batch << EOF
65         set network.${netrenamed}dhcp.proto=static
66         set network.${netrenamed}dhcp.ipaddr="$START"
67         set network.${netrenamed}dhcp.netmask="$NETMASK"
68         uci_commitverbose  "Setup interface for ${netrenamed}dhcp" network
69 EOF
70
71 fi
72