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>"))
172 bssid.datatype = "macaddr"
175 luci.model.uci.cursor():foreach("wireless", "wifi-device",
177 table.insert(devs, section[".name"])
181 device = s:option(DummyValue, "device", translate("Device"))
183 s.defaults.device = devs[1]
186 mode = s:option(ListValue, "mode", translate("Mode"))
187 mode.override_values = true
188 mode:value("ap", translate("Provide (Access Point)"))
189 mode:value("adhoc", translate("Independent (Ad-Hoc)"))
190 mode:value("sta", translate("Join (Client)"))
192 function mode.write(self, section, value)
193 if value == "sta" then
194 local oldif = m.uci:get("network", "wan", "ifname")
195 if oldif and oldif ~= " " then
196 m.uci:set("network", "wan", "_ifname", oldif)
198 m.uci:set("network", "wan", "ifname", " ")
200 self.map:set(section, "network", "wan")
202 if m.uci:get("network", "wan", "_ifname") then
203 m.uci:set("network", "wan", "ifname", m.uci:get("network", "wan", "_ifname"))
205 self.map:set(section, "network", "lan")
208 return ListValue.write(self, section, value)
211 encr = s:option(ListValue, "encryption", translate("Encryption"))
212 encr.override_values = true
213 encr:value("none", "No Encryption")
214 encr:value("wep", "WEP")
216 if hwtype == "mac80211" then
217 local supplicant = fs.access("/usr/sbin/wpa_supplicant")
218 local hostapd = fs.access("/usr/sbin/hostapd")
220 if hostapd and supplicant then
221 encr:value("psk", "WPA-PSK")
222 encr:value("psk2", "WPA2-PSK")
223 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode")
224 encr:value("wpa", "WPA-Radius", {mode="ap"}, {mode="sta"})
225 encr:value("wpa2", "WPA2-Radius", {mode="ap"}, {mode="sta"})
226 elseif hostapd and not supplicant then
227 encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="adhoc"})
228 encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="adhoc"})
229 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="ap"}, {mode="adhoc"})
230 encr:value("wpa", "WPA-Radius", {mode="ap"})
231 encr:value("wpa2", "WPA2-Radius", {mode="ap"})
232 encr.description = translate(
233 "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
234 "and ad-hoc mode) to be installed."
236 elseif not hostapd and supplicant then
237 encr:value("psk", "WPA-PSK", {mode="sta"})
238 encr:value("psk2", "WPA2-PSK", {mode="sta"})
239 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="sta"})
240 encr:value("wpa", "WPA-EAP", {mode="sta"})
241 encr:value("wpa2", "WPA2-EAP", {mode="sta"})
242 encr.description = translate(
243 "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
244 "and ad-hoc mode) to be installed."
247 encr.description = translate(
248 "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
249 "and ad-hoc mode) to be installed."
252 elseif hwtype == "broadcom" then
253 encr:value("psk", "WPA-PSK")
254 encr:value("psk2", "WPA2-PSK")
255 encr:value("psk+psk2", "WPA-PSK/WPA2-PSK Mixed Mode")
258 key = s:option(Value, "key", translate("Key"))
259 key:depends("encryption", "wep")
260 key:depends("encryption", "psk")
261 key:depends("encryption", "psk2")
262 key:depends("encryption", "psk+psk2")
263 key:depends("encryption", "psk-mixed")
264 key:depends({mode="ap", encryption="wpa"})
265 key:depends({mode="ap", encryption="wpa2"})
269 server = s:option(Value, "server", translate("Radius-Server"))
270 server:depends({mode="ap", encryption="wpa"})
271 server:depends({mode="ap", encryption="wpa2"})
272 server.rmempty = true
274 port = s:option(Value, "port", translate("Radius-Port"))
275 port:depends({mode="ap", encryption="wpa"})
276 port:depends({mode="ap", encryption="wpa2"})
280 if hwtype == "mac80211" then
281 nasid = s:option(Value, "nasid", translate("NAS ID"))
282 nasid:depends({mode="ap", encryption="wpa"})
283 nasid:depends({mode="ap", encryption="wpa2"})
286 eaptype = s:option(ListValue, "eap_type", translate("EAP-Method"))
288 eaptype:value("TTLS")
289 eaptype:value("PEAP")
290 eaptype:depends({mode="sta", encryption="wpa"})
291 eaptype:depends({mode="sta", encryption="wpa2"})
293 cacert = s:option(FileUpload, "ca_cert", translate("Path to CA-Certificate"))
294 cacert:depends({mode="sta", encryption="wpa"})
295 cacert:depends({mode="sta", encryption="wpa2"})
297 privkey = s:option(FileUpload, "priv_key", translate("Path to Private Key"))
298 privkey:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
299 privkey:depends({mode="sta", eap_type="TLS", encryption="wpa"})
301 privkeypwd = s:option(Value, "priv_key_pwd", translate("Password of Private Key"))
302 privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
303 privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa"})
306 auth = s:option(Value, "auth", translate("Authentication"))
310 auth:value("MSCHAPV2")
311 auth:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
312 auth:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
313 auth:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
314 auth:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
317 identity = s:option(Value, "identity", translate("Identity"))
318 identity:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
319 identity:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
320 identity:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
321 identity:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
323 password = s:option(Value, "password", translate("Password"))
324 password:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
325 password:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
326 password:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
327 password:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
331 if hwtype == "broadcom" then
332 iso = s:option(Flag, "isolate", translate("AP-Isolation"), translate("Prevents Client to Client communication"))
334 iso:depends("mode", "ap")
336 hide = s:option(Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
338 hide:depends("mode", "ap")
341 if hwtype == "mac80211" then
342 bssid:depends({mode="adhoc"})
345 if hwtype == "broadcom" then
346 bssid:depends({mode="wds"})
347 bssid:depends({mode="adhoc"})