contrib/package: /etc/functions.sh => /lib/functions.sh
[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 get meshwizard.netconfig.$net\_ip4addr)
11 [ -z "$ipaddr" ] && msg_missing_value meshwizard $net\_ip4addr
12
13 netmask=$(uci -q get meshwizard.netconfig.$net\_netmask)
14 [ -z "$netmask" ] && netmask="$interface_netmask"
15 [ -z "$netmask" ] && netmask="255.255.0.0"
16
17 uci set network.$netrenamed="interface"
18 set_defaults "interface_" network.$netrenamed
19
20 uci batch << EOF
21         set network.$netrenamed.proto="static"
22         set network.$netrenamed.ipaddr="$ipaddr"
23         set network.$netrenamed.netmask="$netmask"
24 EOF
25
26 # Setup IPv6 for the interface
27 local ip6addr
28 if [ "$profile_ipv6" = 1 ]; then
29         if [ "$profile_ipv6_config" = "auto-ipv6-dhcpv6" ]; then
30                 ip6addr="$($dir/helpers/gen_auto-ipv6-dhcpv6-ip.sh $netrenamed)"
31                 uci set network.$netrenamed.ip6addr="${ip6addr}/112"
32         fi
33 fi
34
35 uci_commitverbose "Setup interface $netrenamed" network
36
37 # setup dhcp alias/interface
38
39 net_dhcp=$(uci -q get meshwizard.netconfig.${net}_dhcp)
40 if [ "$net_dhcp" == 1 ]; then
41
42         # Load meshwizard_settings
43         dhcprange="$(uci -q get meshwizard.netconfig.${net}_dhcprange)"
44         interface_ip="$(uci -q get meshwizard.netconfig.${net}_ip4addr)"
45         vap=$(uci -q get meshwizard.netconfig.${net}_vap)
46
47         # Rename config
48         handle_dhcpalias() {
49                         config_get interface "$1" interface
50                         if [ "$interface" == "$netrenamed" ]; then
51                                 if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
52                                         section_rename network $1 ${netrenamed}dhcp
53                                 fi
54                         fi
55         }
56         config_load network
57         config_foreach handle_dhcpalias alias
58
59         # Get IP/netmask and start-ip for $net dhcp
60         # If no dhcprange is given in /etc/config/meshwizard we autogenerate one
61
62         if [ -z "$dhcprange" ]; then
63                 dhcprange="$($dir/helpers/gen_dhcp_ip.sh $interface_ip)/24"
64                 uci set meshwizard.netconfig.${net}_dhcprange="$dhcprange"
65         fi
66         eval $(sh $dir/helpers/ipcalc-cidr.sh $dhcprange 1 0)
67
68         # setup wifi-dhcp interface or alias
69
70         # Setup alias for $net
71
72         if [ "$vap" == 1 ]; then
73                 uci set network.${netrenamed}dhcp=interface
74         else
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         uci_commitverbose  "Setup interface for ${netrenamed}dhcp" network
85
86 fi