meshwizard: cleanup references to madwifi
[project/luci.git] / applications / luci-app-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 set up this interface for mesh operation, i.e. add it 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 == "mac80211" then
107                 sys.exec("iw reg set " .. syscc)
108         elseif hwtype == "broadcom" then
109                 sys.exec ("wlc country " .. syscc)
110         end
111
112         local chan = n:taboption(device, ListValue, device .. "_channel", translate("Channel"),
113                 translate("Your device and neighbouring nodes have to use the same channel."))
114         chan:depends(device .. "_config", 1)
115         chan:value('default')
116
117         local iwinfo = sys.wifi.getiwinfo(device)
118         if iwinfo and iwinfo.freqlist then
119                 for _, f in ipairs(iwinfo.freqlist) do
120                         if not f.restricted then
121                                 chan:value(f.channel)
122                         end
123                 end
124         end
125         -- IPv4 address
126         cbi_ip4addr(device)
127
128         -- DHCP enable
129         cbi_dhcp(device)
130
131         -- DHCP range
132         cbi_dhcprange(device)
133
134         -- IPv6 addr and RA
135         if community_ipv6 == "1" then
136                 if community_ipv6mode == "static" then
137                         cbi_ip6addr(device)
138                 end
139                 cbi_ra(device)
140         end
141
142         -- Enable VAP
143         local supports_vap = 0
144         if sys.call("/usr/bin/meshwizard/helpers/supports_vap.sh " .. device .. " " .. hwtype) == 0 then
145                 supports_vap = 1
146         end
147         if supports_vap == 1 then
148                 local vap = n:taboption(device, Flag, device .. "_vap", translate("Virtual Access Point (VAP)"),
149                         translate("This will setup a new virtual wireless interface in Access Point mode."))
150                 vap:depends(device .. "_dhcp", "1")
151                 vap.rmempty = true
152                 if community_vap == "1" then
153                         vap.default = "1"
154                 end
155         end
156 end)
157
158 for _, device in pairs(wired_nets) do
159         cbi_configure(device)
160         cbi_ip4addr(device)
161         cbi_dhcp(device)
162         cbi_dhcprange(device)
163         -- IPv6 addr and RA
164         if community_ipv6 == "1" then
165                 if community_ipv6mode == "static" then
166                         cbi_ip6addr(device)
167                 end
168                 cbi_ra(device)
169         end
170 end
171
172 -- General settings
173 g = m:section(TypedSection, "general", translate("General Settings"))
174 g.anonymous = true
175
176 local cleanup = g:option(Flag, "cleanup", translate("Cleanup config"),
177         translate("If this is selected then config is cleaned before setting new config options."))
178 cleanup.default = "1"
179
180 local restrict = g:option(Flag, "local_restrict", translate("Protect LAN"), 
181         translate("Check this to protect your LAN from other nodes or clients") .. " (" .. translate("recommended") .. ").")
182
183 local share = g:option(Flag, "sharenet", translate("Share your internet connection"),
184         translate("Select this to allow others to use your connection to access the internet."))
185         share.rmempty = true
186
187 -- IPv6 config
188 if community_ipv6 == "1" then
189         v6 = m:section(NamedSection, "ipv6", nil, translate("IPv6 Settings"))
190         local enabled = v6:option(Flag, "enabled", translate("Enabled"),
191                 translate("Activate or deactivate IPv6 config globally."))
192         enabled.default = meshkit_ipv6
193         enabled.rmempty = false
194 end
195
196 return m