wifi.lua 11 KB

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