Globally reduce copyright headers
[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 <xm@leipzig.freifunk.net>
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("Wifi"), 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 if hwtype == "atheros" then
160         mode = s:option(ListValue, "hwmode", translate("Mode"))
161         mode.override_values = true
162         mode:value("", "auto")
163         mode:value("11b", "802.11b")
164         mode:value("11g", "802.11g")
165         mode:value("11a", "802.11a")
166         mode:value("11bg", "802.11b+g")
167         mode.rmempty = true
168 end
169
170
171 ch = s:option(Value, "channel", translate("Channel"))
172 for i=1, 14 do
173         ch:value(i, i .. " (2.4 GHz)")
174 end
175
176
177 s = m:section(TypedSection, "wifi-iface", translate("Local Network"))
178 s.anonymous = true
179 s.addremove = false
180
181 s:option(Value, "ssid", translate("Network Name (<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>)"))
182
183 bssid = s:option(Value, "bssid", translate("<abbr title=\"Basic Service Set Identifier\">BSSID</abbr>"))
184
185 local devs = {}
186 luci.model.uci.cursor():foreach("wireless", "wifi-device",
187         function (section)
188                 table.insert(devs, section[".name"])
189         end)
190
191 if #devs > 1 then
192         device = s:option(DummyValue, "device", translate("Device"))
193 else
194         s.defaults.device = devs[1]
195 end
196
197 mode = s:option(ListValue, "mode", translate("Mode"))
198 mode.override_values = true
199 mode:value("ap", translate("Provide (Access Point)"))
200 mode:value("adhoc", translate("Independent (Ad-Hoc)"))
201 mode:value("sta", translate("Join (Client)"))
202
203 function mode.write(self, section, value)
204         if value == "sta" then
205                 local oldif = m.uci:get("network", "wan", "ifname")
206                 if oldif and oldif ~= " " then
207                         m.uci:set("network", "wan", "_ifname", oldif)
208                 end
209                 m.uci:set("network", "wan", "ifname", " ")
210
211                 self.map:set(section, "network", "wan")
212         else
213                 if m.uci:get("network", "wan", "_ifname") then
214                         m.uci:set("network", "wan", "ifname", m.uci:get("network", "wan", "_ifname"))
215                 end
216                 self.map:set(section, "network", "lan")
217         end
218
219         return ListValue.write(self, section, value)
220 end
221
222 encr = s:option(ListValue, "encryption", translate("Encryption"))
223 encr.override_values = true
224 encr:value("none", "No Encryption")
225 encr:value("wep", "WEP")
226
227 if hwtype == "atheros" or hwtype == "mac80211" then
228         local supplicant = fs.access("/usr/sbin/wpa_supplicant")
229         local hostapd    = fs.access("/usr/sbin/hostapd")
230
231         if hostapd and supplicant then
232                 encr:value("psk", "WPA-PSK")
233                 encr:value("psk2", "WPA2-PSK")
234                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode")
235                 encr:value("wpa", "WPA-Radius", {mode="ap"}, {mode="sta"})
236                 encr:value("wpa2", "WPA2-Radius", {mode="ap"}, {mode="sta"})
237         elseif hostapd and not supplicant then
238                 encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="adhoc"})
239                 encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="adhoc"})
240                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="ap"}, {mode="adhoc"})
241                 encr:value("wpa", "WPA-Radius", {mode="ap"})
242                 encr:value("wpa2", "WPA2-Radius", {mode="ap"})
243                 encr.description = translate(
244                         "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
245                         "and ad-hoc mode) to be installed."
246                 )
247         elseif not hostapd and supplicant then
248                 encr:value("psk", "WPA-PSK", {mode="sta"})
249                 encr:value("psk2", "WPA2-PSK", {mode="sta"})
250                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="sta"})
251                 encr:value("wpa", "WPA-EAP", {mode="sta"})
252                 encr:value("wpa2", "WPA2-EAP", {mode="sta"})
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         else
258                 encr.description = translate(
259                         "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
260                         "and ad-hoc mode) to be installed."
261                 )
262         end
263 elseif hwtype == "broadcom" then
264         encr:value("psk", "WPA-PSK")
265         encr:value("psk2", "WPA2-PSK")
266         encr:value("psk+psk2", "WPA-PSK/WPA2-PSK Mixed Mode")
267 end
268
269 key = s:option(Value, "key", translate("Key"))
270 key:depends("encryption", "wep")
271 key:depends("encryption", "psk")
272 key:depends("encryption", "psk2")
273 key:depends("encryption", "psk+psk2")
274 key:depends("encryption", "psk-mixed")
275 key:depends({mode="ap", encryption="wpa"})
276 key:depends({mode="ap", encryption="wpa2"})
277 key.rmempty = true
278 key.password = true
279
280 server = s:option(Value, "server", translate("Radius-Server"))
281 server:depends({mode="ap", encryption="wpa"})
282 server:depends({mode="ap", encryption="wpa2"})
283 server.rmempty = true
284
285 port = s:option(Value, "port", translate("Radius-Port"))
286 port:depends({mode="ap", encryption="wpa"})
287 port:depends({mode="ap", encryption="wpa2"})
288 port.rmempty = true
289
290
291 if hwtype == "atheros" or hwtype == "mac80211" then
292         nasid = s:option(Value, "nasid", translate("NAS ID"))
293         nasid:depends({mode="ap", encryption="wpa"})
294         nasid:depends({mode="ap", encryption="wpa2"})
295         nasid.rmempty = true
296
297         eaptype = s:option(ListValue, "eap_type", translate("EAP-Method"))
298         eaptype:value("TLS")
299         eaptype:value("TTLS")
300         eaptype:value("PEAP")
301         eaptype:depends({mode="sta", encryption="wpa"})
302         eaptype:depends({mode="sta", encryption="wpa2"})
303
304         cacert = s:option(FileUpload, "ca_cert", translate("Path to CA-Certificate"))
305         cacert:depends({mode="sta", encryption="wpa"})
306         cacert:depends({mode="sta", encryption="wpa2"})
307
308         privkey = s:option(FileUpload, "priv_key", translate("Path to Private Key"))
309         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
310         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa"})
311
312         privkeypwd = s:option(Value, "priv_key_pwd", translate("Password of Private Key"))
313         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
314         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa"})
315
316
317         auth = s:option(Value, "auth", translate("Authentication"))
318         auth:value("PAP")
319         auth:value("CHAP")
320         auth:value("MSCHAP")
321         auth:value("MSCHAPV2")
322         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
323         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
324         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
325         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
326
327
328         identity = s:option(Value, "identity", translate("Identity"))
329         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
330         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
331         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
332         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
333
334         password = s:option(Value, "password", translate("Password"))
335         password:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
336         password:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
337         password:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
338         password:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
339 end
340
341
342 if hwtype == "atheros" or hwtype == "broadcom" then
343         iso = s:option(Flag, "isolate", translate("AP-Isolation"), translate("Prevents Client to Client communication"))
344         iso.rmempty = true
345         iso:depends("mode", "ap")
346
347         hide = s:option(Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
348         hide.rmempty = true
349         hide:depends("mode", "ap")
350 end
351
352 if hwtype == "mac80211" or hwtype == "atheros" then
353         bssid:depends({mode="adhoc"})
354 end
355
356 if hwtype == "broadcom" then
357         bssid:depends({mode="wds"})
358         bssid:depends({mode="adhoc"})
359 end
360
361
362 return m