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