Merge pull request #817 from ascob/luci-app-dynapoint
[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 . /lib/functions.sh
6 . $dir/functions.sh
7
8 # Setup a (new) interface section for $net
9
10 ipaddr=$(uci -q get meshwizard.netconfig.$net\_ip4addr)
11 ip6addr=$(uci -q get meshwizard.netconfig.$net\_ip6addr)
12 [ -z "$ipaddr" ] && msg_missing_value meshwizard $net\_ip4addr
13
14 netmask=$(uci -q get meshwizard.netconfig.$net\_netmask)
15 [ -z "$netmask" ] && netmask="$interface_netmask"
16 [ -z "$netmask" ] && netmask="255.255.0.0"
17
18 uci set network.$netrenamed="interface"
19 set_defaults "interface_" network.$netrenamed
20
21 uci batch << EOF
22         set network.$netrenamed.proto="static"
23         set network.$netrenamed.ipaddr="$ipaddr"
24         set network.$netrenamed.netmask="$netmask"
25 EOF
26
27 if [ "$netrenamed" = "lan" ]; then
28         # remove the bridge if the interface is used for olsr
29         # since this script is only run in this case, no need
30         # to check for lan_proto = "olsr" currently.
31         uci -q delete network.lan.type
32 fi
33
34 # Setup IPv6 for the interface
35 if [ "$ipv6_enabled" = 1 ]; then
36         if [ "$ipv6_config" = "auto-ipv6-dhcpv6" ]; then
37                 ip6addr="$($dir/helpers/gen_auto-ipv6-dhcpv6-ip.sh $netrenamed)"
38                 uci set network.$netrenamed.ip6addr="${ip6addr}/112"
39         fi
40         if [ "$ipv6_config" = "static" ] && [ -n "$ip6addr" ]; then
41                 uci set network.$netrenamed.ip6addr="$ip6addr"
42         fi
43 fi
44
45 uci_commitverbose "Setup interface $netrenamed" network
46
47 # setup dhcp alias/interface
48
49 net_dhcp=$(uci -q get meshwizard.netconfig.${net}_dhcp)
50 if [ "$net_dhcp" == 1 ]; then
51
52         # Load meshwizard_settings
53         dhcprange="$(uci -q get meshwizard.netconfig.${net}_dhcprange)"
54         interface_ip="$(uci -q get meshwizard.netconfig.${net}_ip4addr)"
55         vap=$(uci -q get meshwizard.netconfig.${net}_vap)
56
57         # Rename config
58         handle_dhcpalias() {
59                         config_get interface "$1" interface
60                         if [ "$interface" == "$netrenamed" ]; then
61                                 if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
62                                         section_rename network $1 ${netrenamed}dhcp
63                                 fi
64                         fi
65         }
66         config_load network
67         config_foreach handle_dhcpalias interface
68
69         # Get IP/netmask and start-ip for $net dhcp
70         # If no dhcprange is given in /etc/config/meshwizard we autogenerate one
71
72         if [ -z "$dhcprange" ]; then
73                 dhcprange="$($dir/helpers/gen_dhcp_ip.sh $interface_ip)/24"
74                 uci set meshwizard.netconfig.${net}_dhcprange="$dhcprange"
75         fi
76
77         # If we use VAP and also offer dhcp on the adhoc interface then cut the dhcp
78         # range in two halves. one for the adhoc, one for the managed VAP interface
79         ahdhcp_when_vap="$(uci get profile_$community.profile.adhoc_dhcp_when_vap)"
80
81         if [ "$supports_vap" = 1 -a "$vap" = 1 -a "$ahdhcp_when_vap" = 1 ]; then
82                 # VAPs are enabled for this interface, supported and we want to
83                 # also use DHCP on the adhoc interface
84                 network=${dhcprange%%/*}
85                 mask=${dhcprange##*/}
86                 # Divide network size by adding 1 to the netmask
87                 mask=$(($mask + 1))
88                 # Get first ip and netmask for the adhoc dhcp network
89                 eval $(sh $dir/helpers/ipcalc-cidr.sh ${network}/${mask} 1 0)
90                 STARTADHOC=$START
91                 NETMASKADHOC=$NETMASK
92                 # Get first ip and netmask for the managed dhcp network
93                 eval $(sh $dir/helpers/ipcalc-cidr.sh ${NEXTNET}/${mask} 1 0)
94                 STARTVAP=$START
95                 NETMASKVAP=$NETMASK
96                 # Add dhcp interface
97                 uci batch <<- EOF
98                         set network.${netrenamed}dhcp=interface
99                         set network.${netrenamed}dhcp.proto=static
100                         set network.${netrenamed}dhcp.ipaddr="$STARTVAP"
101                         set network.${netrenamed}dhcp.netmask="$NETMASKVAP"
102                 EOF
103                 uci_commitverbose  "Setup interface for ${netrenamed}dhcp" network
104         else
105                 eval $(sh $dir/helpers/ipcalc-cidr.sh $dhcprange 1 0)
106                 STARTADHOC=$START
107                 NETMASKADHOC=$NETMASK
108         fi
109         if [ "$supports_vap" = 1 -a "$vap" = 1 -a "$ahdhcp_when_vap" != 1 ]; then
110                 # vaps are enabled and supported and we do not use DHCP on adhoc
111                 # Add dhcp interface
112                 uci batch <<- EOF
113                         set network.${netrenamed}dhcp=interface
114                         set network.${netrenamed}dhcp.proto=static
115                         set network.${netrenamed}dhcp.ipaddr="$STARTADHOC"
116                         set network.${netrenamed}dhcp.netmask="$NETMASKADHOC"
117                 EOF
118                 uci_commitverbose  "Setup interface for ${netrenamed}dhcp" network
119         fi
120
121
122         # Setup alias for $net adhoc interface 
123         if  [ "$supports_vap" = 0 ] || \
124                 [ "$vap" = 0 ] || \
125                 [ "$supports_vap" = 1 -a "$vap" = 1 -a "$ahdhcp_when_vap" = 1 ] || \
126                 [ "$lan_is_olsr" = "1" ]; then
127                 # setup an alias interface for the main interface to use as a network for clients
128                 # when one of the following conditions is met
129                 # * vaps are not supported
130                 # * or not enabled
131                 # * or they are supported and enabled but we also want to use DHCP on the adhoc interface
132                 # * or this is the lan interface and it is used for olsrd (and dhcp is enabled)
133                 uci batch <<- EOF
134                         set network.${netrenamed}ahdhcp=interface
135                         set network.${netrenamed}ahdhcp.ifname="@${netrenamed}"
136                         set network.${netrenamed}ahdhcp.proto=static
137                         set network.${netrenamed}ahdhcp.ipaddr="$STARTADHOC"
138                         set network.${netrenamed}ahdhcp.netmask="$NETMASKADHOC"
139                 EOF
140                 uci_commitverbose  "Setup interface for ${netrenamed}ahdhcp" network
141         fi
142 fi