wifi.lua 11 KB

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