modules/admin-mini: use iwinfo to perform scans
[project/luci.git] / modules / admin-mini / luasrc / model / cbi / mini / wifi.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 -- Data init --
17
18 local fs  = require "nixio.fs"
19 local sys = require "luci.sys"
20 local uci = require "luci.model.uci".cursor()
21
22 if not uci:get("network", "wan") then
23         uci:section("network", "interface", "wan", {proto="none", ifname=" "})
24         uci:save("network")
25         uci:commit("network")
26 end
27
28 local wlcursor = luci.model.uci.cursor_state()
29 local wireless = wlcursor:get_all("wireless")
30 local wifidata = sys.wifi.getiwconfig()
31 local wifidevs = {}
32 local ifaces = {}
33
34 for k, v in pairs(wireless) do
35         if v[".type"] == "wifi-iface" then
36                 table.insert(ifaces, v)
37         end
38 end
39
40 wlcursor:foreach("wireless", "wifi-device",
41         function(section)
42                 table.insert(wifidevs, section[".name"])
43         end)
44
45
46 -- Main Map --
47
48 m = Map("wireless", translate("Wifi"), translate("Here you can configure installed wifi devices."))
49 m:chain("network")
50
51
52 -- Status Table --
53 s = m:section(Table, ifaces, translate("Networks"))
54
55 link = s:option(DummyValue, "_link", translate("Link"))
56 function link.cfgvalue(self, section)
57         local ifname = self.map:get(section, "ifname")
58         return wifidata[ifname] and wifidata[ifname]["Link Quality"] or "-"
59 end
60
61 essid = s:option(DummyValue, "ssid", "ESSID")
62
63 bssid = s:option(DummyValue, "_bsiid", "BSSID")
64 function bssid.cfgvalue(self, section)
65         local ifname = self.map:get(section, "ifname")
66         return (wifidata[ifname] and (wifidata[ifname].Cell
67          or wifidata[ifname]["Access Point"])) or "-"
68 end
69
70 channel = s:option(DummyValue, "channel", translate("Channel"))
71 function channel.cfgvalue(self, section)
72         return wireless[self.map:get(section, "device")].channel
73 end
74
75 protocol = s:option(DummyValue, "_mode", translate("Protocol"))
76 function protocol.cfgvalue(self, section)
77         local mode = wireless[self.map:get(section, "device")].mode
78         return mode and "802." .. mode
79 end
80
81 mode = s:option(DummyValue, "mode", translate("Mode"))
82 encryption = s:option(DummyValue, "encryption", translate("<abbr title=\"Encrypted\">Encr.</abbr>"))
83
84 power = s:option(DummyValue, "_power", translate("Power"))
85 function power.cfgvalue(self, section)
86         local ifname = self.map:get(section, "ifname")
87         return wifidata[ifname] and wifidata[ifname]["Tx-Power"] or "-"
88 end
89
90 scan = s:option(Button, "_scan", translate("Scan"))
91 scan.inputstyle = "find"
92
93 function scan.cfgvalue(self, section)
94         return self.map:get(section, "ifname") or false
95 end
96
97 -- WLAN-Scan-Table --
98
99 t2 = m:section(Table, {}, translate("<abbr title=\"Wireless Local Area Network\">WLAN</abbr>-Scan"), translate("Wifi networks in your local environment"))
100
101 function scan.write(self, section)
102         m.autoapply = false
103         t2.render = t2._render
104         local ifname = self.map:get(section, "ifname")
105         local iwinfo = sys.wifi.getiwinfo(ifname)
106         if iwinfo then
107                 local _, cell
108                 for _, cell in ipairs(iwinfo.scanlist) do
109                         t2.data[#t2.data+1] = {
110                                 Quality = "%d/%d" %{ cell.quality, cell.quality_max },
111                                 ESSID   = cell.ssid,
112                                 Address = cell.bssid,
113                                 Mode    = cell.mode,
114                                 ["Encryption key"] = cell.encryption.enabled and "On" or "Off",
115                                 ["Signal level"]   = "%d dBm" % cell.signal,
116                                 ["Noise level"]    = "%d dBm" % iwinfo.noise
117                         }
118                 end
119         end
120 end
121
122 t2._render = t2.render
123 t2.render = function() end
124
125 t2:option(DummyValue, "Quality", translate("Link"))
126 essid = t2:option(DummyValue, "ESSID", "ESSID")
127 function essid.cfgvalue(self, section)
128         return self.map:get(section, "ESSID")
129 end
130
131 t2:option(DummyValue, "Address", "BSSID")
132 t2:option(DummyValue, "Mode", translate("Mode"))
133 chan = t2:option(DummyValue, "channel", translate("Channel"))
134 function chan.cfgvalue(self, section)
135         return self.map:get(section, "Channel")
136             or self.map:get(section, "Frequency")
137             or "-"
138 end
139
140 t2:option(DummyValue, "Encryption key", translate("<abbr title=\"Encrypted\">Encr.</abbr>"))
141
142 t2:option(DummyValue, "Signal level", translate("Signal"))
143
144 t2:option(DummyValue, "Noise level", translate("Noise"))
145
146
147
148 if #wifidevs < 1 then
149         return m
150 end
151
152 -- Config Section --
153
154 s = m:section(NamedSection, wifidevs[1], "wifi-device", translate("Devices"))
155 s.addremove = false
156
157 en = s:option(Flag, "disabled", translate("enable"))
158 en.rmempty = false
159 en.enabled = "0"
160 en.disabled = "1"
161
162 function en.cfgvalue(self, section)
163         return Flag.cfgvalue(self, section) or "0"
164 end
165
166
167 local hwtype = m:get(wifidevs[1], "type")
168
169 if hwtype == "atheros" then
170         mode = s:option(ListValue, "hwmode", translate("Mode"))
171         mode.override_values = true
172         mode:value("", "auto")
173         mode:value("11b", "802.11b")
174         mode:value("11g", "802.11g")
175         mode:value("11a", "802.11a")
176         mode:value("11bg", "802.11b+g")
177         mode.rmempty = true
178 end
179
180
181 ch = s:option(Value, "channel", translate("Channel"))
182 for i=1, 14 do
183         ch:value(i, i .. " (2.4 GHz)")
184 end
185
186
187 s = m:section(TypedSection, "wifi-iface", translate("Local Network"))
188 s.anonymous = true
189 s.addremove = false
190
191 s:option(Value, "ssid", translate("Network Name (<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>)"))
192
193 bssid = s:option(Value, "bssid", translate("<abbr title=\"Basic Service Set Identifier\">BSSID</abbr>"))
194
195 local devs = {}
196 luci.model.uci.cursor():foreach("wireless", "wifi-device",
197         function (section)
198                 table.insert(devs, section[".name"])
199         end)
200
201 if #devs > 1 then
202         device = s:option(DummyValue, "device", translate("Device"))
203 else
204         s.defaults.device = devs[1]
205 end
206
207 mode = s:option(ListValue, "mode", translate("Mode"))
208 mode.override_values = true
209 mode:value("ap", translate("Provide (Access Point)"))
210 mode:value("adhoc", translate("Independent (Ad-Hoc)"))
211 mode:value("sta", translate("Join (Client)"))
212
213 function mode.write(self, section, value)
214         if value == "sta" then
215                 local oldif = m.uci:get("network", "wan", "ifname")
216                 if oldif and oldif ~= " " then
217                         m.uci:set("network", "wan", "_ifname", oldif)
218                 end
219                 m.uci:set("network", "wan", "ifname", " ")
220
221                 self.map:set(section, "network", "wan")
222         else
223                 if m.uci:get("network", "wan", "_ifname") then
224                         m.uci:set("network", "wan", "ifname", m.uci:get("network", "wan", "_ifname"))
225                 end
226                 self.map:set(section, "network", "lan")
227         end
228
229         return ListValue.write(self, section, value)
230 end
231
232 encr = s:option(ListValue, "encryption", translate("Encryption"))
233 encr.override_values = true
234 encr:value("none", "No Encryption")
235 encr:value("wep", "WEP")
236
237 if hwtype == "atheros" or hwtype == "mac80211" then
238         local supplicant = fs.access("/usr/sbin/wpa_supplicant")
239         local hostapd    = fs.access("/usr/sbin/hostapd")
240
241         if hostapd and supplicant then
242                 encr:value("psk", "WPA-PSK")
243                 encr:value("psk2", "WPA2-PSK")
244                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode")
245                 encr:value("wpa", "WPA-Radius", {mode="ap"}, {mode="sta"})
246                 encr:value("wpa2", "WPA2-Radius", {mode="ap"}, {mode="sta"})
247         elseif hostapd and not supplicant then
248                 encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="adhoc"})
249                 encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="adhoc"})
250                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="ap"}, {mode="adhoc"})
251                 encr:value("wpa", "WPA-Radius", {mode="ap"})
252                 encr:value("wpa2", "WPA2-Radius", {mode="ap"})
253                 encr.description = translate(
254                         "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
255                         "and ad-hoc mode) to be installed."
256                 )
257         elseif not hostapd and supplicant then
258                 encr:value("psk", "WPA-PSK", {mode="sta"})
259                 encr:value("psk2", "WPA2-PSK", {mode="sta"})
260                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="sta"})
261                 encr:value("wpa", "WPA-EAP", {mode="sta"})
262                 encr:value("wpa2", "WPA2-EAP", {mode="sta"})
263                 encr.description = translate(
264                         "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
265                         "and ad-hoc mode) to be installed."
266                 )               
267         else
268                 encr.description = translate(
269                         "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
270                         "and ad-hoc mode) to be installed."
271                 )
272         end
273 elseif hwtype == "broadcom" then
274         encr:value("psk", "WPA-PSK")
275         encr:value("psk2", "WPA2-PSK")
276         encr:value("psk+psk2", "WPA-PSK/WPA2-PSK Mixed Mode")
277 end
278
279 key = s:option(Value, "key", translate("Key"))
280 key:depends("encryption", "wep")
281 key:depends("encryption", "psk")
282 key:depends("encryption", "psk2")
283 key:depends("encryption", "psk+psk2")
284 key:depends("encryption", "psk-mixed")
285 key:depends({mode="ap", encryption="wpa"})
286 key:depends({mode="ap", encryption="wpa2"})
287 key.rmempty = true
288 key.password = true
289
290 server = s:option(Value, "server", translate("Radius-Server"))
291 server:depends({mode="ap", encryption="wpa"})
292 server:depends({mode="ap", encryption="wpa2"})
293 server.rmempty = true
294
295 port = s:option(Value, "port", translate("Radius-Port"))
296 port:depends({mode="ap", encryption="wpa"})
297 port:depends({mode="ap", encryption="wpa2"})
298 port.rmempty = true
299
300
301 if hwtype == "atheros" or hwtype == "mac80211" then
302         nasid = s:option(Value, "nasid", translate("NAS ID"))
303         nasid:depends({mode="ap", encryption="wpa"})
304         nasid:depends({mode="ap", encryption="wpa2"})
305         nasid.rmempty = true
306
307         eaptype = s:option(ListValue, "eap_type", translate("EAP-Method"))
308         eaptype:value("TLS")
309         eaptype:value("TTLS")
310         eaptype:value("PEAP")
311         eaptype:depends({mode="sta", encryption="wpa"})
312         eaptype:depends({mode="sta", encryption="wpa2"})
313
314         cacert = s:option(FileUpload, "ca_cert", translate("Path to CA-Certificate"))
315         cacert:depends({mode="sta", encryption="wpa"})
316         cacert:depends({mode="sta", encryption="wpa2"})
317
318         privkey = s:option(FileUpload, "priv_key", translate("Path to Private Key"))
319         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
320         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa"})
321
322         privkeypwd = s:option(Value, "priv_key_pwd", translate("Password of Private Key"))
323         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
324         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa"})
325
326
327         auth = s:option(Value, "auth", translate("Authentication"))
328         auth:value("PAP")
329         auth:value("CHAP")
330         auth:value("MSCHAP")
331         auth:value("MSCHAPV2")
332         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
333         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
334         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
335         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
336
337
338         identity = s:option(Value, "identity", translate("Identity"))
339         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
340         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
341         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
342         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
343
344         password = s:option(Value, "password", translate("Password"))
345         password:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
346         password:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
347         password:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
348         password:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
349 end
350
351
352 if hwtype == "atheros" or hwtype == "broadcom" then
353         iso = s:option(Flag, "isolate", translate("AP-Isolation"), translate("Prevents Client to Client communication"))
354         iso.rmempty = true
355         iso:depends("mode", "ap")
356
357         hide = s:option(Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
358         hide.rmempty = true
359         hide:depends("mode", "ap")
360 end
361
362 if hwtype == "mac80211" or hwtype == "atheros" then
363         bssid:depends({mode="adhoc"})
364 end
365
366 if hwtype == "broadcom" then
367         bssid:depends({mode="wds"})
368         bssid:depends({mode="adhoc"})
369 end
370
371
372 return m