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