wifi.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. bssid.datatype = "macaddr"
  130. local devs = {}
  131. luci.model.uci.cursor():foreach("wireless", "wifi-device",
  132. function (section)
  133. table.insert(devs, section[".name"])
  134. end)
  135. if #devs > 1 then
  136. device = s:option(DummyValue, "device", translate("Device"))
  137. else
  138. s.defaults.device = devs[1]
  139. end
  140. mode = s:option(ListValue, "mode", translate("Mode"))
  141. mode.override_values = true
  142. mode:value("ap", translate("Provide (Access Point)"))
  143. mode:value("adhoc", translate("Independent (Ad-Hoc)"))
  144. mode:value("sta", translate("Join (Client)"))
  145. function mode.write(self, section, value)
  146. if value == "sta" then
  147. local oldif = m.uci:get("network", "wan", "ifname")
  148. if oldif and oldif ~= " " then
  149. m.uci:set("network", "wan", "_ifname", oldif)
  150. end
  151. m.uci:set("network", "wan", "ifname", " ")
  152. self.map:set(section, "network", "wan")
  153. else
  154. if m.uci:get("network", "wan", "_ifname") then
  155. m.uci:set("network", "wan", "ifname", m.uci:get("network", "wan", "_ifname"))
  156. end
  157. self.map:set(section, "network", "lan")
  158. end
  159. return ListValue.write(self, section, value)
  160. end
  161. encr = s:option(ListValue, "encryption", translate("Encryption"))
  162. encr.override_values = true
  163. encr:value("none", "No Encryption")
  164. encr:value("wep", "WEP")
  165. if hwtype == "mac80211" then
  166. local supplicant = fs.access("/usr/sbin/wpa_supplicant")
  167. local hostapd = fs.access("/usr/sbin/hostapd")
  168. if hostapd and supplicant then
  169. encr:value("psk", "WPA-PSK")
  170. encr:value("psk2", "WPA2-PSK")
  171. encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode")
  172. encr:value("wpa", "WPA-Radius", {mode="ap"}, {mode="sta"})
  173. encr:value("wpa2", "WPA2-Radius", {mode="ap"}, {mode="sta"})
  174. elseif hostapd and not supplicant then
  175. encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="adhoc"})
  176. encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="adhoc"})
  177. encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="ap"}, {mode="adhoc"})
  178. encr:value("wpa", "WPA-Radius", {mode="ap"})
  179. encr:value("wpa2", "WPA2-Radius", {mode="ap"})
  180. encr.description = translate(
  181. "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
  182. "and ad-hoc mode) to be installed."
  183. )
  184. elseif not hostapd and supplicant then
  185. encr:value("psk", "WPA-PSK", {mode="sta"})
  186. encr:value("psk2", "WPA2-PSK", {mode="sta"})
  187. encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="sta"})
  188. encr:value("wpa", "WPA-EAP", {mode="sta"})
  189. encr:value("wpa2", "WPA2-EAP", {mode="sta"})
  190. encr.description = translate(
  191. "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
  192. "and ad-hoc mode) to be installed."
  193. )
  194. else
  195. encr.description = translate(
  196. "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
  197. "and ad-hoc mode) to be installed."
  198. )
  199. end
  200. elseif hwtype == "broadcom" then
  201. encr:value("psk", "WPA-PSK")
  202. encr:value("psk2", "WPA2-PSK")
  203. encr:value("psk+psk2", "WPA-PSK/WPA2-PSK Mixed Mode")
  204. end
  205. key = s:option(Value, "key", translate("Key"))
  206. key:depends("encryption", "wep")
  207. key:depends("encryption", "psk")
  208. key:depends("encryption", "psk2")
  209. key:depends("encryption", "psk+psk2")
  210. key:depends("encryption", "psk-mixed")
  211. key:depends({mode="ap", encryption="wpa"})
  212. key:depends({mode="ap", encryption="wpa2"})
  213. key.rmempty = true
  214. key.password = true
  215. server = s:option(Value, "server", translate("Radius-Server"))
  216. server:depends({mode="ap", encryption="wpa"})
  217. server:depends({mode="ap", encryption="wpa2"})
  218. server.rmempty = true
  219. port = s:option(Value, "port", translate("Radius-Port"))
  220. port:depends({mode="ap", encryption="wpa"})
  221. port:depends({mode="ap", encryption="wpa2"})
  222. port.rmempty = true
  223. if hwtype == "mac80211" then
  224. nasid = s:option(Value, "nasid", translate("NAS ID"))
  225. nasid:depends({mode="ap", encryption="wpa"})
  226. nasid:depends({mode="ap", encryption="wpa2"})
  227. nasid.rmempty = true
  228. eaptype = s:option(ListValue, "eap_type", translate("EAP-Method"))
  229. eaptype:value("TLS")
  230. eaptype:value("TTLS")
  231. eaptype:value("PEAP")
  232. eaptype:depends({mode="sta", encryption="wpa"})
  233. eaptype:depends({mode="sta", encryption="wpa2"})
  234. cacert = s:option(FileUpload, "ca_cert", translate("Path to CA-Certificate"))
  235. cacert:depends({mode="sta", encryption="wpa"})
  236. cacert:depends({mode="sta", encryption="wpa2"})
  237. privkey = s:option(FileUpload, "priv_key", translate("Path to Private Key"))
  238. privkey:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
  239. privkey:depends({mode="sta", eap_type="TLS", encryption="wpa"})
  240. privkeypwd = s:option(Value, "priv_key_pwd", translate("Password of Private Key"))
  241. privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
  242. privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa"})
  243. auth = s:option(Value, "auth", translate("Authentication"))
  244. auth:value("PAP")
  245. auth:value("CHAP")
  246. auth:value("MSCHAP")
  247. auth:value("MSCHAPV2")
  248. auth:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
  249. auth:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
  250. auth:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
  251. auth:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
  252. identity = s:option(Value, "identity", translate("Identity"))
  253. identity:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
  254. identity:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
  255. identity:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
  256. identity:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
  257. password = s:option(Value, "password", translate("Password"))
  258. password:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
  259. password:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
  260. password:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
  261. password:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
  262. end
  263. if hwtype == "broadcom" then
  264. iso = s:option(Flag, "isolate", translate("AP-Isolation"), translate("Prevents Client to Client communication"))
  265. iso.rmempty = true
  266. iso:depends("mode", "ap")
  267. hide = s:option(Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
  268. hide.rmempty = true
  269. hide:depends("mode", "ap")
  270. end
  271. if hwtype == "mac80211" then
  272. bssid:depends({mode="adhoc"})
  273. end
  274. if hwtype == "broadcom" then
  275. bssid:depends({mode="wds"})
  276. bssid:depends({mode="adhoc"})
  277. end
  278. return m