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