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.
7 local fs = require "nixio.fs"
8 local sys = require "luci.sys"
9 local uci = require "luci.model.uci".cursor()
11 if not uci:get("network", "wan") then
12 uci:section("network", "interface", "wan", {proto="none", ifname=" "})
17 local wlcursor = luci.model.uci.cursor_state()
18 local wireless = wlcursor:get_all("wireless")
22 for k, v in pairs(wireless) do
23 if v[".type"] == "wifi-iface" then
24 table.insert(ifaces, v)
28 wlcursor:foreach("wireless", "wifi-device",
30 table.insert(wifidevs, section[".name"])
36 m = Map("wireless", translate("Wifi"), translate("Here you can configure installed wifi devices."))
41 s = m:section(Table, ifaces, translate("Networks"))
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 "-"
50 essid = s:option(DummyValue, "ssid", "ESSID")
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 "-"
59 channel = s:option(DummyValue, "channel", translate("Channel"))
60 function channel.cfgvalue(self, section)
61 return wireless[self.map:get(section, "device")].channel
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
70 mode = s:option(DummyValue, "mode", translate("Mode"))
71 encryption = s:option(DummyValue, "encryption", translate("<abbr title=\"Encrypted\">Encr.</abbr>"))
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 "-"
80 scan = s:option(Button, "_scan", translate("Scan"))
81 scan.inputstyle = "find"
83 function scan.cfgvalue(self, section)
84 return self.map:get(section, "ifname") or false
89 t2 = m:section(Table, {}, translate("<abbr title=\"Wireless Local Area Network\">WLAN</abbr>-Scan"), translate("Wifi networks in your local environment"))
91 function scan.write(self, section)
93 t2.render = t2._render
94 local ifname = self.map:get(section, "ifname")
95 local iwinfo = sys.wifi.getiwinfo(ifname)
98 for _, cell in ipairs(iwinfo.scanlist) do
99 t2.data[#t2.data+1] = {
100 Quality = "%d/%d" %{ cell.quality, cell.quality_max },
102 Address = cell.bssid,
104 ["Encryption key"] = cell.encryption.enabled and "On" or "Off",
105 ["Signal level"] = "%d dBm" % cell.signal,
106 ["Noise level"] = "%d dBm" % iwinfo.noise
112 t2._render = t2.render
113 t2.render = function() end
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")
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")
130 t2:option(DummyValue, "Encryption key", translate("<abbr title=\"Encrypted\">Encr.</abbr>"))
132 t2:option(DummyValue, "Signal level", translate("Signal"))
134 t2:option(DummyValue, "Noise level", translate("Noise"))
138 if #wifidevs < 1 then
144 s = m:section(NamedSection, wifidevs[1], "wifi-device", translate("Devices"))
147 en = s:option(Flag, "disabled", translate("enable"))
152 function en.cfgvalue(self, section)
153 return Flag.cfgvalue(self, section) or "0"
157 local hwtype = m:get(wifidevs[1], "type")
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")
171 ch = s:option(Value, "channel", translate("Channel"))
173 ch:value(i, i .. " (2.4 GHz)")
177 s = m:section(TypedSection, "wifi-iface", translate("Local Network"))
181 s:option(Value, "ssid", translate("Network Name (<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>)"))
183 bssid = s:option(Value, "bssid", translate("<abbr title=\"Basic Service Set Identifier\">BSSID</abbr>"))
186 luci.model.uci.cursor():foreach("wireless", "wifi-device",
188 table.insert(devs, section[".name"])
192 device = s:option(DummyValue, "device", translate("Device"))
194 s.defaults.device = devs[1]
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)"))
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)
209 m.uci:set("network", "wan", "ifname", " ")
211 self.map:set(section, "network", "wan")
213 if m.uci:get("network", "wan", "_ifname") then
214 m.uci:set("network", "wan", "ifname", m.uci:get("network", "wan", "_ifname"))
216 self.map:set(section, "network", "lan")
219 return ListValue.write(self, section, value)
222 encr = s:option(ListValue, "encryption", translate("Encryption"))
223 encr.override_values = true
224 encr:value("none", "No Encryption")
225 encr:value("wep", "WEP")
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")
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."
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."
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."
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")
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"})
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
285 port = s:option(Value, "port", translate("Radius-Port"))
286 port:depends({mode="ap", encryption="wpa"})
287 port:depends({mode="ap", encryption="wpa2"})
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"})
297 eaptype = s:option(ListValue, "eap_type", translate("EAP-Method"))
299 eaptype:value("TTLS")
300 eaptype:value("PEAP")
301 eaptype:depends({mode="sta", encryption="wpa"})
302 eaptype:depends({mode="sta", encryption="wpa2"})
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"})
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"})
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"})
317 auth = s:option(Value, "auth", translate("Authentication"))
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"})
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"})
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"})
342 if hwtype == "atheros" or hwtype == "broadcom" then
343 iso = s:option(Flag, "isolate", translate("AP-Isolation"), translate("Prevents Client to Client communication"))
345 iso:depends("mode", "ap")
347 hide = s:option(Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
349 hide:depends("mode", "ap")
352 if hwtype == "mac80211" or hwtype == "atheros" then
353 bssid:depends({mode="adhoc"})
356 if hwtype == "broadcom" then
357 bssid:depends({mode="wds"})
358 bssid:depends({mode="adhoc"})