modules/admin-full, modules/admin-mini: prevent addremove in system page
[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("a_w_devices1"))
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("iwscan_encr"))
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("iwscan"), translate("iwscan1"))
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("iwscan_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("iwscan_encr"))
127
128 t2:option(DummyValue, "Signal level", translate("iwscan_signal"))
129
130 t2:option(DummyValue, "Noise level", translate("iwscan_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("a_w_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("m_n_local"))
174 s.anonymous = true
175 s.addremove = false
176
177 s:option(Value, "ssid", translate("a_w_netid"))
178
179 bssid = s:option(Value, "bssid", translate("wifi_bssid"))
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("m_w_ap"))
196 mode:value("adhoc", translate("m_w_adhoc"))
197 mode:value("sta", translate("m_w_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("wifi_wpareq")
240         elseif not hostapd and supplicant then
241                 encr:value("psk", "WPA-PSK", {mode="sta"})
242                 encr:value("psk2", "WPA2-PSK", {mode="sta"})
243                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="sta"})
244                 encr:value("wpa", "WPA-EAP", {mode="sta"})
245                 encr:value("wpa2", "WPA2-EAP", {mode="sta"})
246                 encr.description = translate("wifi_wpareq")
247         else
248                 encr.description = translate("wifi_wpareq")
249         end
250 elseif hwtype == "broadcom" then
251         encr:value("psk", "WPA-PSK")
252         encr:value("psk2", "WPA2-PSK")
253         encr:value("psk+psk2", "WPA-PSK/WPA2-PSK Mixed Mode")
254 end
255
256 key = s:option(Value, "key", translate("key"))
257 key:depends("encryption", "wep")
258 key:depends("encryption", "psk")
259 key:depends("encryption", "psk2")
260 key:depends("encryption", "psk+psk2")
261 key:depends("encryption", "mixed")
262 key:depends({mode="ap", encryption="wpa"})
263 key:depends({mode="ap", encryption="wpa2"})
264 key.rmempty = true
265 key.password = true
266
267 server = s:option(Value, "server", translate("a_w_radiussrv"))
268 server:depends({mode="ap", encryption="wpa"})
269 server:depends({mode="ap", encryption="wpa2"})
270 server.rmempty = true
271
272 port = s:option(Value, "port", translate("a_w_radiusport"))
273 port:depends({mode="ap", encryption="wpa"})
274 port:depends({mode="ap", encryption="wpa2"})
275 port.rmempty = true
276
277
278 if hwtype == "atheros" or hwtype == "mac80211" then
279         nasid = s:option(Value, "nasid", translate("a_w_nasid"))
280         nasid:depends({mode="ap", encryption="wpa"})
281         nasid:depends({mode="ap", encryption="wpa2"})
282         nasid.rmempty = true
283
284         eaptype = s:option(ListValue, "eap_type", translate("a_w_eaptype"))
285         eaptype:value("TLS")
286         eaptype:value("TTLS")
287         eaptype:value("PEAP")
288         eaptype:depends({mode="sta", encryption="wpa"})
289         eaptype:depends({mode="sta", encryption="wpa2"})
290
291         cacert = s:option(FileUpload, "ca_cert", translate("a_w_cacert"))
292         cacert:depends({mode="sta", encryption="wpa"})
293         cacert:depends({mode="sta", encryption="wpa2"})
294
295         privkey = s:option(FileUpload, "priv_key", translate("a_w_tlsprivkey"))
296         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
297         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa"})
298
299         privkeypwd = s:option(Value, "priv_key_pwd", translate("a_w_tlsprivkeypwd"))
300         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
301         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa"})
302
303
304         auth = s:option(Value, "auth", translate("a_w_peapauth"))
305         auth:value("PAP")
306         auth:value("CHAP")
307         auth:value("MSCHAP")
308         auth:value("MSCHAPV2")
309         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
310         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
311         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
312         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
313
314
315         identity = s:option(Value, "identity", translate("a_w_peapidentity"))
316         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
317         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
318         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
319         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
320
321         password = s:option(Value, "password", translate("a_w_peappassword"))
322         password:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
323         password:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
324         password:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
325         password:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
326 end
327
328
329 if hwtype == "atheros" or hwtype == "broadcom" then
330         iso = s:option(Flag, "isolate", translate("a_w_apisolation"), translate("a_w_apisolation1"))
331         iso.rmempty = true
332         iso:depends("mode", "ap")
333
334         hide = s:option(Flag, "hidden", translate("a_w_hideessid"))
335         hide.rmempty = true
336         hide:depends("mode", "ap")
337 end
338
339 if hwtype == "mac80211" or hwtype == "atheros" then
340         bssid:depends({mode="adhoc"})
341 end
342
343 if hwtype == "broadcom" then
344         bssid:depends({mode="wds"})
345         bssid:depends({mode="adhoc"})
346 end
347
348
349 return m