762412e03429a288f19a9590485d1a0daba5426e
[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         luci.util.update(t2.data, sys.wifi.iwscan(ifname))
106 end
107
108 t2._render = t2.render
109 t2.render = function() end
110
111 t2:option(DummyValue, "Quality", translate("Link"))
112 essid = t2:option(DummyValue, "ESSID", "ESSID")
113 function essid.cfgvalue(self, section)
114         return self.map:get(section, "ESSID")
115 end
116
117 t2:option(DummyValue, "Address", "BSSID")
118 t2:option(DummyValue, "Mode", translate("Mode"))
119 chan = t2:option(DummyValue, "channel", translate("Channel"))
120 function chan.cfgvalue(self, section)
121         return self.map:get(section, "Channel")
122             or self.map:get(section, "Frequency")
123             or "-"
124 end
125
126 t2:option(DummyValue, "Encryption key", translate("<abbr title=\"Encrypted\">Encr.</abbr>"))
127
128 t2:option(DummyValue, "Signal level", translate("Signal"))
129
130 t2:option(DummyValue, "Noise level", translate("Noise"))
131
132
133
134 if #wifidevs < 1 then
135         return m
136 end
137
138 -- Config Section --
139
140 s = m:section(NamedSection, wifidevs[1], "wifi-device", translate("Devices"))
141 s.addremove = false
142
143 en = s:option(Flag, "disabled", translate("enable"))
144 en.rmempty = false
145 en.enabled = "0"
146 en.disabled = "1"
147
148 function en.cfgvalue(self, section)
149         return Flag.cfgvalue(self, section) or "0"
150 end
151
152
153 local hwtype = m:get(wifidevs[1], "type")
154
155 if hwtype == "atheros" then
156         mode = s:option(ListValue, "hwmode", translate("Mode"))
157         mode.override_values = true
158         mode:value("", "auto")
159         mode:value("11b", "802.11b")
160         mode:value("11g", "802.11g")
161         mode:value("11a", "802.11a")
162         mode:value("11bg", "802.11b+g")
163         mode.rmempty = true
164 end
165
166
167 ch = s:option(Value, "channel", translate("Channel"))
168 for i=1, 14 do
169         ch:value(i, i .. " (2.4 GHz)")
170 end
171
172
173 s = m:section(TypedSection, "wifi-iface", translate("Local Network"))
174 s.anonymous = true
175 s.addremove = false
176
177 s:option(Value, "ssid", translate("Network Name (<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>)"))
178
179 bssid = s:option(Value, "bssid", translate("<abbr title=\"Basic Service Set Identifier\">BSSID</abbr>"))
180
181 local devs = {}
182 luci.model.uci.cursor():foreach("wireless", "wifi-device",
183         function (section)
184                 table.insert(devs, section[".name"])
185         end)
186
187 if #devs > 1 then
188         device = s:option(DummyValue, "device", translate("Device"))
189 else
190         s.defaults.device = devs[1]
191 end
192
193 mode = s:option(ListValue, "mode", translate("Mode"))
194 mode.override_values = true
195 mode:value("ap", translate("Provide (Access Point)"))
196 mode:value("adhoc", translate("Independent (Ad-Hoc)"))
197 mode:value("sta", translate("Join (Client)"))
198
199 function mode.write(self, section, value)
200         if value == "sta" then
201                 local oldif = m.uci:get("network", "wan", "ifname")
202                 if oldif and oldif ~= " " then
203                         m.uci:set("network", "wan", "_ifname", oldif)
204                 end
205                 m.uci:set("network", "wan", "ifname", " ")
206
207                 self.map:set(section, "network", "wan")
208         else
209                 if m.uci:get("network", "wan", "_ifname") then
210                         m.uci:set("network", "wan", "ifname", m.uci:get("network", "wan", "_ifname"))
211                 end
212                 self.map:set(section, "network", "lan")
213         end
214
215         return ListValue.write(self, section, value)
216 end
217
218 encr = s:option(ListValue, "encryption", translate("Encryption"))
219 encr.override_values = true
220 encr:value("none", "No Encryption")
221 encr:value("wep", "WEP")
222
223 if hwtype == "atheros" or hwtype == "mac80211" then
224         local supplicant = fs.access("/usr/sbin/wpa_supplicant")
225         local hostapd    = fs.access("/usr/sbin/hostapd")
226
227         if hostapd and supplicant then
228                 encr:value("psk", "WPA-PSK")
229                 encr:value("psk2", "WPA2-PSK")
230                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode")
231                 encr:value("wpa", "WPA-Radius", {mode="ap"}, {mode="sta"})
232                 encr:value("wpa2", "WPA2-Radius", {mode="ap"}, {mode="sta"})
233         elseif hostapd and not supplicant then
234                 encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="adhoc"})
235                 encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="adhoc"})
236                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="ap"}, {mode="adhoc"})
237                 encr:value("wpa", "WPA-Radius", {mode="ap"})
238                 encr:value("wpa2", "WPA2-Radius", {mode="ap"})
239                 encr.description = translate(
240                         "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
241                         "and ad-hoc mode) to be installed."
242                 )
243         elseif not hostapd and supplicant then
244                 encr:value("psk", "WPA-PSK", {mode="sta"})
245                 encr:value("psk2", "WPA2-PSK", {mode="sta"})
246                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="sta"})
247                 encr:value("wpa", "WPA-EAP", {mode="sta"})
248                 encr:value("wpa2", "WPA2-EAP", {mode="sta"})
249                 encr.description = translate(
250                         "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
251                         "and ad-hoc mode) to be installed."
252                 )               
253         else
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         end
259 elseif hwtype == "broadcom" then
260         encr:value("psk", "WPA-PSK")
261         encr:value("psk2", "WPA2-PSK")
262         encr:value("psk+psk2", "WPA-PSK/WPA2-PSK Mixed Mode")
263 end
264
265 key = s:option(Value, "key", translate("Key"))
266 key:depends("encryption", "wep")
267 key:depends("encryption", "psk")
268 key:depends("encryption", "psk2")
269 key:depends("encryption", "psk+psk2")
270 key:depends("encryption", "psk-mixed")
271 key:depends({mode="ap", encryption="wpa"})
272 key:depends({mode="ap", encryption="wpa2"})
273 key.rmempty = true
274 key.password = true
275
276 server = s:option(Value, "server", translate("Radius-Server"))
277 server:depends({mode="ap", encryption="wpa"})
278 server:depends({mode="ap", encryption="wpa2"})
279 server.rmempty = true
280
281 port = s:option(Value, "port", translate("Radius-Port"))
282 port:depends({mode="ap", encryption="wpa"})
283 port:depends({mode="ap", encryption="wpa2"})
284 port.rmempty = true
285
286
287 if hwtype == "atheros" or hwtype == "mac80211" then
288         nasid = s:option(Value, "nasid", translate("NAS ID"))
289         nasid:depends({mode="ap", encryption="wpa"})
290         nasid:depends({mode="ap", encryption="wpa2"})
291         nasid.rmempty = true
292
293         eaptype = s:option(ListValue, "eap_type", translate("EAP-Method"))
294         eaptype:value("TLS")
295         eaptype:value("TTLS")
296         eaptype:value("PEAP")
297         eaptype:depends({mode="sta", encryption="wpa"})
298         eaptype:depends({mode="sta", encryption="wpa2"})
299
300         cacert = s:option(FileUpload, "ca_cert", translate("Path to CA-Certificate"))
301         cacert:depends({mode="sta", encryption="wpa"})
302         cacert:depends({mode="sta", encryption="wpa2"})
303
304         privkey = s:option(FileUpload, "priv_key", translate("Path to Private Key"))
305         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
306         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa"})
307
308         privkeypwd = s:option(Value, "priv_key_pwd", translate("Password of Private Key"))
309         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
310         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa"})
311
312
313         auth = s:option(Value, "auth", translate("Authentication"))
314         auth:value("PAP")
315         auth:value("CHAP")
316         auth:value("MSCHAP")
317         auth:value("MSCHAPV2")
318         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
319         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
320         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
321         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
322
323
324         identity = s:option(Value, "identity", translate("Identity"))
325         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
326         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
327         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
328         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
329
330         password = s:option(Value, "password", translate("Password"))
331         password:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
332         password:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
333         password:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
334         password:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
335 end
336
337
338 if hwtype == "atheros" or hwtype == "broadcom" then
339         iso = s:option(Flag, "isolate", translate("AP-Isolation"), translate("Prevents Client to Client communication"))
340         iso.rmempty = true
341         iso:depends("mode", "ap")
342
343         hide = s:option(Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
344         hide.rmempty = true
345         hide:depends("mode", "ap")
346 end
347
348 if hwtype == "mac80211" or hwtype == "atheros" then
349         bssid:depends({mode="adhoc"})
350 end
351
352 if hwtype == "broadcom" then
353         bssid:depends({mode="wds"})
354         bssid:depends({mode="adhoc"})
355 end
356
357
358 return m