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.
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("Wireless"), 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 ch = s:option(Value, "channel", translate("Channel"))
161 ch:value(i, i .. " (2.4 GHz)")
165 s = m:section(TypedSection, "wifi-iface", translate("Local Network"))
169 s:option(Value, "ssid", translate("Network Name (<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>)"))
171 bssid = s:option(Value, "bssid", translate("<abbr title=\"Basic Service Set Identifier\">BSSID</abbr>"))
174 luci.model.uci.cursor():foreach("wireless", "wifi-device",
176 table.insert(devs, section[".name"])
180 device = s:option(DummyValue, "device", translate("Device"))
182 s.defaults.device = devs[1]
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)"))
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)
197 m.uci:set("network", "wan", "ifname", " ")
199 self.map:set(section, "network", "wan")
201 if m.uci:get("network", "wan", "_ifname") then
202 m.uci:set("network", "wan", "ifname", m.uci:get("network", "wan", "_ifname"))
204 self.map:set(section, "network", "lan")
207 return ListValue.write(self, section, value)
210 encr = s:option(ListValue, "encryption", translate("Encryption"))
211 encr.override_values = true
212 encr:value("none", "No Encryption")
213 encr:value("wep", "WEP")
215 if hwtype == "mac80211" then
216 local supplicant = fs.access("/usr/sbin/wpa_supplicant")
217 local hostapd = fs.access("/usr/sbin/hostapd")
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."
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."
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."
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")
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"})
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
273 port = s:option(Value, "port", translate("Radius-Port"))
274 port:depends({mode="ap", encryption="wpa"})
275 port:depends({mode="ap", encryption="wpa2"})
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"})
285 eaptype = s:option(ListValue, "eap_type", translate("EAP-Method"))
287 eaptype:value("TTLS")
288 eaptype:value("PEAP")
289 eaptype:depends({mode="sta", encryption="wpa"})
290 eaptype:depends({mode="sta", encryption="wpa2"})
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"})
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"})
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"})
305 auth = s:option(Value, "auth", translate("Authentication"))
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"})
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"})
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"})
330 if hwtype == "broadcom" then
331 iso = s:option(Flag, "isolate", translate("AP-Isolation"), translate("Prevents Client to Client communication"))
333 iso:depends("mode", "ap")
335 hide = s:option(Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
337 hide:depends("mode", "ap")
340 if hwtype == "mac80211" then
341 bssid:depends({mode="adhoc"})
344 if hwtype == "broadcom" then
345 bssid:depends({mode="wds"})
346 bssid:depends({mode="adhoc"})