4f03d228c5fe1c13b90ea60c6e665b2bbce9e883
[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 # Delete the network interface section for $net
9 if [ "$cleanup" == 1 ]; then
10         section_cleanup network.$netrenamed
11 fi
12
13 # Setup a (new) interface section for $net
14
15 ipaddr=$(uci get meshwizard.netconfig.$net\_ip4addr)
16 [ -z "$ipaddr" ] && msg_missing_value meshwizard $net\_ip4addr
17
18 [ -z "$interface_netmask" ] && interface netmask="255.255.0.0"
19
20 uci batch << EOF
21 set network.$netrenamed="interface"
22 set network.$netrenamed.proto="static"
23 set network.$netrenamed.ipaddr="$ipaddr"
24 set network.$netrenamed.netmask="$interface_netmask"
25 set network.$netrenamed.dns="$interface_dns"
26 EOF
27
28 echo "    IP address: $ipaddr"
29 echo "    Netmask   : $interface_netmask"
30
31 # setup dhcp alias/interface
32
33 net_dhcp=$(uci -q get meshwizard.netconfig.${net}_dhcp)
34 if [ "$net_dhcp" == 1 ]; then
35
36         # Load meshwizard_settings
37         dhcprange="$(uci -q get meshwizard.netconfig.${net}_dhcprange)"
38         interface_ip="$(uci -q get meshwizard.netconfig.${net}_ip4addr)"
39         vap=$(uci -q get meshwizard.netconfig.${net}_vap)
40
41         # Clean/rename config
42         handle_dhcpalias() {
43                         config_get interface "$1" interface
44                         if [ "$interface" == "$netrenamed" ]; then
45                                 if [ "$cleanup" == 1 ]; then
46                                         section_cleanup network.$1
47                                 else
48                                         if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
49                                                 section_rename network $1 ${netrenamed}dhcp
50                                         fi
51                                 fi
52                         fi
53         }
54         config_load network
55         config_foreach handle_dhcpalias alias
56
57         # Get IP/netmask and start-ip for $net dhcp
58         # If no dhcprange is given in /etc/config/meshwizard we autogenerate one
59
60         if [ -z "$dhcprange" ]; then
61                 dhcprange="$($dir/helpers/gen_dhcp_ip.sh $interface_ip)/24"
62                 uci set meshwizard.netconfig.${net}_dhcprange="$dhcprange"
63         fi
64         eval $(sh $dir/helpers/ipcalc-cidr.sh $dhcprange 1 0)
65
66         # setup wifi-dhcp interface or alias
67
68         # Setup alias for $net
69
70         if [ "$vap" == 1 ]; then
71                 echo "    + Setup interface ${netrenamed}dhcp."
72                 uci set network.${netrenamed}dhcp=interface
73         else
74                 echo "    + Setup alias interface ${netrenamed}dhcp."
75                 uci set network.${netrenamed}dhcp=alias
76                 uci set network.${netrenamed}dhcp.interface="$netrenamed"
77         fi
78
79         uci batch << EOF
80 set network.${netrenamed}dhcp.proto=static
81 set network.${netrenamed}dhcp.ipaddr="$START"
82 set network.${netrenamed}dhcp.netmask="$NETMASK"
83 EOF
84
85         echo "    interface: $net
86     ipaddr: $START
87     netmask: $NETMASK"
88
89 fi
90
91 uci commit