respect community settings for using VAP in luci-meshwizard
[project/luci.git] / applications / luci-meshwizard / luasrc / model / cbi / freifunk / meshwizard.lua
1 -- wizard rewrite wip
2
3 local uci = require "luci.model.uci".cursor()
4 local sys = require "luci.sys"
5 local util = require "luci.util"
6 local ip = require "luci.ip"
7
8 local community = "profile_" .. (uci:get("freifunk", "community", "name") or "Freifunk")
9 local mesh_network = ip.IPv4(uci:get_first(community, "community", "mesh_network") or "10.0.0.0/8")
10 local community_ipv6 = uci:get_first(community, "community", "ipv6") or 0
11 local community_ipv6mode = uci:get_first(community, "community", "ipv6_config") or "static"
12 local meshkit_ipv6 = uci:get("meshwizard", "ipv6", "enabled") or 0
13 local community_vap = uci:get_first(community, "community", "vap") or 0
14
15 m = Map("meshwizard", translate("Wizard"), translate("This wizard will assist you in setting up your router for Freifunk " ..
16         "or another similar wireless community network."))
17
18 n = m:section(NamedSection, "netconfig", nil, translate("Interfaces"))
19 n.anonymous = true
20
21 -- common functions
22
23 function cbi_configure(device)
24         local configure = n:taboption(device, Flag, device .. "_config", translate("Configure this interface"),
25                 translate("Note: this will setup this interface for mesh operation, i.e. add to zone 'freifunk' and enable olsr."))
26 end
27
28 function cbi_ip4addr(device)
29         local ip4addr = n:taboption(device, Value, device .. "_ip4addr", translate("Mesh IP address"),
30                 translate("This is a unique address in the mesh (e.g. 10.1.1.1) and has to be registered at your local community."))
31                 ip4addr:depends(device .. "_config", 1)
32                 ip4addr.datatype = "ip4addr"
33         function ip4addr.validate(self, value)
34                 local x = ip.IPv4(value)
35                 if mesh_network:contains(x) then
36                         return value
37                 else
38                         return nil, translate("The given IP address is not inside the mesh network range ") ..
39                         "(" .. mesh_network:string() .. ")."
40                 end
41         end
42 end
43
44 function cbi_ip6addr(device)
45         local ip6addr = n:taboption(device, Value, device .. "_ip6addr", translate("Mesh IPv6 address"),
46                 translate("This is a unique IPv6 address in CIDR notation (e.g. 2001:1:2:3::1/64) and has to be registered at your local community."))
47                 ip6addr:depends(device .. "_config", 1)
48                 ip6addr.datatype = "ip6addr"
49 end
50
51
52 function cbi_dhcp(device)
53         local dhcp = n:taboption(device, Flag, device .. "_dhcp", translate("Enable DHCP"),
54                 translate("DHCP will automatically assign ip addresses to clients"))
55         dhcp:depends(device .. "_config", 1)
56         dhcp.rmempty = true
57 end
58
59 function cbi_ra(device)
60         local ra = n:taboption(device, Flag, device .. "_ipv6ra", translate("Enable RA"),
61                 translate("Send router advertisements on this device."))
62         ra:depends(device .. "_config", 1)
63         ra.rmempty = true
64 end
65
66 function cbi_dhcprange(device)
67         local dhcprange = n:taboption(device, Value, device .. "_dhcprange", translate("DHCP IP range"),
68                 translate("The IP range from which clients are assigned ip addresses (e.g. 10.1.2.1/28). " ..
69                 "If this is a range inside your mesh network range, then it will be announced as HNA. Any other range will use NAT. " ..
70                 "If left empty then the defaults from the community profile will be used."))
71         dhcprange:depends(device .. "_dhcp", "1")
72         dhcprange.rmempty = true
73         dhcprange.datatype = "ip4addr"
74 end
75 -- create tabs and config for wireless
76 local nets={}
77 uci:foreach("wireless", "wifi-device", function(section)
78         local device = section[".name"]
79         table.insert(nets, device)
80 end)
81
82 local wired_nets = {}
83 uci:foreach("network", "interface", function(section)
84         local device = section[".name"]
85         if not util.contains(nets, device) and device ~= "loopback" and not device:find("wireless") then
86                 table.insert(nets, device)
87                 table.insert(wired_nets, device)
88         end
89 end)
90
91 for _, net in util.spairs(nets, function(a,b) return (nets[a] < nets[b]) end) do
92         n:tab(net, net)
93 end
94
95 -- create cbi config for wireless
96 uci:foreach("wireless", "wifi-device", function(section)
97         local device = section[".name"]
98         local hwtype = section.type
99         local syscc = section.country or uci:get(community, "wifi_device", "country") or
100                 uci:get("freifunk", "wifi_device", "country")
101
102         cbi_configure(device)
103
104         -- Channel selection
105
106         if hwtype == "atheros" then
107                 local cc = util.trim(sys.exec("grep -i '" .. syscc .. "' /lib/wifi/cc_translate.txt |cut -d ' ' -f 2")) or 0
108                 sys.exec('"echo " .. cc .. " > /proc/sys/dev/" .. device .. "/countrycode"')
109         elseif hwtype == "mac80211" then
110                 sys.exec("iw reg set " .. syscc)
111         elseif hwtype == "broadcom" then
112                 sys.exec ("wlc country " .. syscc)
113         end
114
115         local chan = n:taboption(device, ListValue, device .. "_channel", translate("Channel"),
116                 translate("Your device and neighbouring nodes have to use the same channel."))
117         chan:depends(device .. "_config", 1)
118         chan:value('default')
119
120         local iwinfo = sys.wifi.getiwinfo(device)
121         if iwinfo then
122                 for _, f in ipairs(iwinfo.freqlist) do
123                         if not f.restricted then
124                                 chan:value(f.channel)
125                         end
126                 end
127         end
128         -- IPv4 address
129         cbi_ip4addr(device)
130
131         -- DHCP enable
132         cbi_dhcp(device)
133
134         -- DHCP range
135         cbi_dhcprange(device)
136
137         -- IPv6 addr and RA
138         if community_ipv6 == "1" then
139                 if community_ipv6mode == "static" then
140                         cbi_ip6addr(device)
141                 end
142                 cbi_ra(device)
143         end
144
145         -- Enable VAP
146         local supports_vap = 0
147         if sys.call("/usr/bin/meshwizard/helpers/supports_vap.sh " .. device .. " " .. hwtype) == 0 then
148                 supports_vap = 1
149         end
150         if supports_vap == 1 then
151                 local vap = n:taboption(device, Flag, device .. "_vap", translate("Virtual Access Point (VAP)"),
152                         translate("This will setup a new virtual wireless interface in Access Point mode."))
153                 vap:depends(device .. "_dhcp", "1")
154                 vap.rmempty = true
155                 if community_vap == "1" then
156                         vap.default = "1"
157                 end
158         end
159 end)
160
161 for _, device in pairs(wired_nets) do
162         cbi_configure(device)
163         cbi_ip4addr(device)
164         cbi_dhcp(device)
165         cbi_dhcprange(device)
166         -- IPv6 addr and RA
167         if community_ipv6 == "1" then
168                 if community_ipv6mode == "static" then
169                         cbi_ip6addr(device)
170                 end
171                 cbi_ra(device)
172         end
173 end
174
175 -- General settings
176 g = m:section(TypedSection, "general", translate("General Settings"))
177 g.anonymous = true
178
179 local cleanup = g:option(Flag, "cleanup", translate("Cleanup config"),
180         translate("If this is selected then config is cleaned before setting new config options."))
181 cleanup.default = "1"
182
183 local restrict = g:option(Flag, "local_restrict", translate("Protect LAN"), 
184         translate("Check this to protect your LAN from other nodes or clients") .. " (" .. translate("recommended") .. ").")
185
186 local share = g:option(Flag, "sharenet", translate("Share your internet connection"),
187         translate("Select this to allow others to use your connection to access the internet."))
188         share.rmempty = true
189
190 -- IPv6 config
191 if community_ipv6 == "1" then
192         v6 = m:section(NamedSection, "ipv6", nil, translate("IPv6 Settings"))
193         local enabled = v6:option(Flag, "enabled", translate("Enabled"),
194                 translate("Activate or deactivate IPv6 config globally."))
195         enabled.default = meshkit_ipv6
196         enabled.rmempty = false
197 end
198
199 return m