app-statistics: df: Don't separate disk usage types
[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 local ip6addr
36 if [ "$ipv6_enabled" = 1 ]; then
37         if [ "$ipv6_config" = "auto-ipv6-dhcpv6" ]; then
38                 ip6addr="$($dir/helpers/gen_auto-ipv6-dhcpv6-ip.sh $netrenamed)"
39                 uci set network.$netrenamed.ip6addr="${ip6addr}/112"
40         fi
41         if [ "$ipv6_config" = "static" ] && [ -n "$ip6addr" ]; then
42                 uci set network.$netrenamed.ip6addr="$ip6addr"
43         fi
44 fi
45
46 uci_commitverbose "Setup interface $netrenamed" network
47
48 # setup dhcp alias/interface
49
50 net_dhcp=$(uci -q get meshwizard.netconfig.${net}_dhcp)
51 if [ "$net_dhcp" == 1 ]; then
52
53         # Load meshwizard_settings
54         dhcprange="$(uci -q get meshwizard.netconfig.${net}_dhcprange)"
55         interface_ip="$(uci -q get meshwizard.netconfig.${net}_ip4addr)"
56         vap=$(uci -q get meshwizard.netconfig.${net}_vap)
57
58         # Rename config
59         handle_dhcpalias() {
60                         config_get interface "$1" interface
61                         if [ "$interface" == "$netrenamed" ]; then
62                                 if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
63                                         section_rename network $1 ${netrenamed}dhcp
64                                 fi
65                         fi
66         }
67         config_load network
68         config_foreach handle_dhcpalias interface
69
70         # Get IP/netmask and start-ip for $net dhcp
71         # If no dhcprange is given in /etc/config/meshwizard we autogenerate one
72
73         if [ -z "$dhcprange" ]; then
74                 dhcprange="$($dir/helpers/gen_dhcp_ip.sh $interface_ip)/24"
75                 uci set meshwizard.netconfig.${net}_dhcprange="$dhcprange"
76         fi
77
78         # If we use VAP and also offer dhcp on the adhoc interface then cut the dhcp
79         # range in two halves. one for the adhoc, one for the managed VAP interface
80         ahdhcp_when_vap="$(uci get profile_$community.profile.adhoc_dhcp_when_vap)"
81
82         if [ "$supports_vap" = 1 -a "$vap" = 1 -a "$ahdhcp_when_vap" = 1 ]; then
83                 # VAPs are enabled for this interface, supported and we want to
84                 # also use DHCP on the adhoc interface
85                 local network
86                 local mask
87                 network=${dhcprange%%/*}
88                 mask=${dhcprange##*/}
89                 # Divide network size by adding 1 to the netmask
90                 mask=$(($mask + 1))
91                 # Get first ip and netmask for the adhoc dhcp network
92                 eval $(sh $dir/helpers/ipcalc-cidr.sh ${network}/${mask} 1 0)
93                 STARTADHOC=$START
94                 NETMASKADHOC=$NETMASK
95                 # Get first ip and netmask for the managed dhcp network
96                 eval $(sh $dir/helpers/ipcalc-cidr.sh ${NEXTNET}/${mask} 1 0)
97                 STARTVAP=$START
98                 NETMASKVAP=$NETMASK
99                 # Add dhcp interface
100                 uci batch <<- EOF
101                         set network.${netrenamed}dhcp=interface
102                         set network.${netrenamed}dhcp.proto=static
103                         set network.${netrenamed}dhcp.ipaddr="$STARTVAP"
104                         set network.${netrenamed}dhcp.netmask="$NETMASKVAP"
105                 EOF
106                 uci_commitverbose  "Setup interface for ${netrenamed}dhcp" network
107         else
108                 eval $(sh $dir/helpers/ipcalc-cidr.sh $dhcprange 1 0)
109                 STARTADHOC=$START
110                 NETMASKADHOC=$NETMASK
111         fi
112         if [ "$supports_vap" = 1 -a "$vap" = 1 -a "$ahdhcp_when_vap" != 1 ]; then
113                 # vaps are enabled and supported and we do not use DHCP on adhoc
114                 # Add dhcp interface
115                 uci batch <<- EOF
116                         set network.${netrenamed}dhcp=interface
117                         set network.${netrenamed}dhcp.proto=static
118                         set network.${netrenamed}dhcp.ipaddr="$STARTADHOC"
119                         set network.${netrenamed}dhcp.netmask="$NETMASKADHOC"
120                 EOF
121                 uci_commitverbose  "Setup interface for ${netrenamed}dhcp" network
122         fi
123
124
125         # Setup alias for $net adhoc interface 
126         if  [ "$supports_vap" = 0 ] || \
127                 [ "$vap" = 0 ] || \
128                 [ "$supports_vap" = 1 -a "$vap" = 1 -a "$ahdhcp_when_vap" = 1 ] || \
129                 [ "$lan_is_olsr" = "1" ]; then
130                 # setup an alias interface for the main interface to use as a network for clients
131                 # when one of the following conditions is met
132                 # * vaps are not supported
133                 # * or not enabled
134                 # * or they are supported and enabled but we also want to use DHCP on the adhoc interface
135                 # * or this is the lan interface and it is used for olsrd (and dhcp is enabled)
136                 uci batch <<- EOF
137                         set network.${netrenamed}ahdhcp=interface
138                         set network.${netrenamed}ahdhcp.ifname="@${netrenamed}"
139                         set network.${netrenamed}ahdhcp.proto=static
140                         set network.${netrenamed}ahdhcp.ipaddr="$STARTADHOC"
141                         set network.${netrenamed}ahdhcp.netmask="$NETMASKADHOC"
142                 EOF
143                 uci_commitverbose  "Setup interface for ${netrenamed}ahdhcp" network
144         fi
145 fi