network.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2011-2015 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. module("luci.controller.admin.network", package.seeall)
  5. function index()
  6. local uci = require("luci.model.uci").cursor()
  7. local page
  8. page = node("admin", "network")
  9. page.target = firstchild()
  10. page.title = _("Network")
  11. page.order = 50
  12. page.index = true
  13. -- if page.inreq then
  14. local has_switch = false
  15. uci:foreach("network", "switch",
  16. function(s)
  17. has_switch = true
  18. return false
  19. end)
  20. if has_switch then
  21. page = node("admin", "network", "vlan")
  22. page.target = cbi("admin_network/vlan")
  23. page.title = _("Switch")
  24. page.order = 20
  25. page = entry({"admin", "network", "switch_status"}, call("switch_status"), nil)
  26. page.leaf = true
  27. end
  28. local has_wifi = false
  29. uci:foreach("wireless", "wifi-device",
  30. function(s)
  31. has_wifi = true
  32. return false
  33. end)
  34. if has_wifi then
  35. page = entry({"admin", "network", "wireless_join"}, post("wifi_join"), nil)
  36. page.leaf = true
  37. page = entry({"admin", "network", "wireless_add"}, post("wifi_add"), nil)
  38. page.leaf = true
  39. page = entry({"admin", "network", "wireless_delete"}, post("wifi_delete"), nil)
  40. page.leaf = true
  41. page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil)
  42. page.leaf = true
  43. page = entry({"admin", "network", "wireless_reconnect"}, post("wifi_reconnect"), nil)
  44. page.leaf = true
  45. page = entry({"admin", "network", "wireless_shutdown"}, post("wifi_shutdown"), nil)
  46. page.leaf = true
  47. page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wireless"), 15)
  48. page.leaf = true
  49. page.subindex = true
  50. if page.inreq then
  51. local wdev
  52. local net = require "luci.model.network".init(uci)
  53. for _, wdev in ipairs(net:get_wifidevs()) do
  54. local wnet
  55. for _, wnet in ipairs(wdev:get_wifinets()) do
  56. entry(
  57. {"admin", "network", "wireless", wnet:id()},
  58. alias("admin", "network", "wireless"),
  59. wdev:name() .. ": " .. wnet:shortname()
  60. )
  61. end
  62. end
  63. end
  64. end
  65. page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
  66. page.leaf = true
  67. page = entry({"admin", "network", "iface_delete"}, post("iface_delete"), nil)
  68. page.leaf = true
  69. page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
  70. page.leaf = true
  71. page = entry({"admin", "network", "iface_reconnect"}, post("iface_reconnect"), nil)
  72. page.leaf = true
  73. page = entry({"admin", "network", "iface_shutdown"}, post("iface_shutdown"), nil)
  74. page.leaf = true
  75. page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)
  76. page.leaf = true
  77. page.subindex = true
  78. if page.inreq then
  79. uci:foreach("network", "interface",
  80. function (section)
  81. local ifc = section[".name"]
  82. if ifc ~= "loopback" then
  83. entry({"admin", "network", "network", ifc},
  84. true, ifc:upper())
  85. end
  86. end)
  87. end
  88. if nixio.fs.access("/etc/config/dhcp") then
  89. page = node("admin", "network", "dhcp")
  90. page.target = cbi("admin_network/dhcp")
  91. page.title = _("DHCP and DNS")
  92. page.order = 30
  93. page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
  94. page.leaf = true
  95. page = node("admin", "network", "hosts")
  96. page.target = cbi("admin_network/hosts")
  97. page.title = _("Hostnames")
  98. page.order = 40
  99. end
  100. page = node("admin", "network", "routes")
  101. page.target = cbi("admin_network/routes")
  102. page.title = _("Static Routes")
  103. page.order = 50
  104. page = node("admin", "network", "diagnostics")
  105. page.target = template("admin_network/diagnostics")
  106. page.title = _("Diagnostics")
  107. page.order = 60
  108. page = entry({"admin", "network", "diag_ping"}, post("diag_ping"), nil)
  109. page.leaf = true
  110. page = entry({"admin", "network", "diag_nslookup"}, post("diag_nslookup"), nil)
  111. page.leaf = true
  112. page = entry({"admin", "network", "diag_traceroute"}, post("diag_traceroute"), nil)
  113. page.leaf = true
  114. page = entry({"admin", "network", "diag_ping6"}, post("diag_ping6"), nil)
  115. page.leaf = true
  116. page = entry({"admin", "network", "diag_traceroute6"}, post("diag_traceroute6"), nil)
  117. page.leaf = true
  118. -- end
  119. end
  120. function wifi_join()
  121. local tpl = require "luci.template"
  122. local http = require "luci.http"
  123. local dev = http.formvalue("device")
  124. local ssid = http.formvalue("join")
  125. if dev and ssid then
  126. local cancel = (http.formvalue("cancel") or http.formvalue("cbi.cancel"))
  127. if not cancel then
  128. local cbi = require "luci.cbi"
  129. local map = luci.cbi.load("admin_network/wifi_add")[1]
  130. if map:parse() ~= cbi.FORM_DONE then
  131. tpl.render("header")
  132. map:render()
  133. tpl.render("footer")
  134. end
  135. return
  136. end
  137. end
  138. tpl.render("admin_network/wifi_join")
  139. end
  140. function wifi_add()
  141. local dev = luci.http.formvalue("device")
  142. local ntm = require "luci.model.network".init()
  143. dev = dev and ntm:get_wifidev(dev)
  144. if dev then
  145. local net = dev:add_wifinet({
  146. mode = "ap",
  147. ssid = "OpenWrt",
  148. encryption = "none"
  149. })
  150. ntm:save("wireless")
  151. luci.http.redirect(net:adminlink())
  152. end
  153. end
  154. function wifi_delete(network)
  155. local ntm = require "luci.model.network".init()
  156. local wnet = ntm:get_wifinet(network)
  157. if wnet then
  158. local dev = wnet:get_device()
  159. local nets = wnet:get_networks()
  160. if dev then
  161. ntm:del_wifinet(network)
  162. ntm:commit("wireless")
  163. local _, net
  164. for _, net in ipairs(nets) do
  165. if net:is_empty() then
  166. ntm:del_network(net:name())
  167. ntm:commit("network")
  168. end
  169. end
  170. luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>/dev/null")
  171. end
  172. end
  173. luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
  174. end
  175. function iface_status(ifaces)
  176. local netm = require "luci.model.network".init()
  177. local rv = { }
  178. local iface
  179. for iface in ifaces:gmatch("[%w%.%-_]+") do
  180. local net = netm:get_network(iface)
  181. local device = net and net:get_interface()
  182. if device then
  183. local data = {
  184. id = iface,
  185. proto = net:proto(),
  186. uptime = net:uptime(),
  187. gwaddr = net:gwaddr(),
  188. ipaddrs = net:ipaddrs(),
  189. ip6addrs = net:ip6addrs(),
  190. dnsaddrs = net:dnsaddrs(),
  191. name = device:shortname(),
  192. type = device:type(),
  193. ifname = device:name(),
  194. macaddr = device:mac(),
  195. is_up = device:is_up(),
  196. rx_bytes = device:rx_bytes(),
  197. tx_bytes = device:tx_bytes(),
  198. rx_packets = device:rx_packets(),
  199. tx_packets = device:tx_packets(),
  200. subdevices = { }
  201. }
  202. for _, device in ipairs(net:get_interfaces() or {}) do
  203. data.subdevices[#data.subdevices+1] = {
  204. name = device:shortname(),
  205. type = device:type(),
  206. ifname = device:name(),
  207. macaddr = device:mac(),
  208. macaddr = device:mac(),
  209. is_up = device:is_up(),
  210. rx_bytes = device:rx_bytes(),
  211. tx_bytes = device:tx_bytes(),
  212. rx_packets = device:rx_packets(),
  213. tx_packets = device:tx_packets(),
  214. }
  215. end
  216. rv[#rv+1] = data
  217. else
  218. rv[#rv+1] = {
  219. id = iface,
  220. name = iface,
  221. type = "ethernet"
  222. }
  223. end
  224. end
  225. if #rv > 0 then
  226. luci.http.prepare_content("application/json")
  227. luci.http.write_json(rv)
  228. return
  229. end
  230. luci.http.status(404, "No such device")
  231. end
  232. function iface_reconnect(iface)
  233. local netmd = require "luci.model.network".init()
  234. local net = netmd:get_network(iface)
  235. if net then
  236. luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
  237. luci.http.status(200, "Reconnected")
  238. return
  239. end
  240. luci.http.status(404, "No such interface")
  241. end
  242. function iface_shutdown(iface)
  243. local netmd = require "luci.model.network".init()
  244. local net = netmd:get_network(iface)
  245. if net then
  246. luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
  247. luci.http.status(200, "Shutdown")
  248. return
  249. end
  250. luci.http.status(404, "No such interface")
  251. end
  252. function iface_delete(iface)
  253. local netmd = require "luci.model.network".init()
  254. local net = netmd:del_network(iface)
  255. if net then
  256. luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
  257. luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
  258. netmd:commit("network")
  259. netmd:commit("wireless")
  260. return
  261. end
  262. luci.http.status(404, "No such interface")
  263. end
  264. function wifi_status(devs)
  265. local s = require "luci.tools.status"
  266. local rv = { }
  267. local dev
  268. for dev in devs:gmatch("[%w%.%-]+") do
  269. rv[#rv+1] = s.wifi_network(dev)
  270. end
  271. if #rv > 0 then
  272. luci.http.prepare_content("application/json")
  273. luci.http.write_json(rv)
  274. return
  275. end
  276. luci.http.status(404, "No such device")
  277. end
  278. local function wifi_reconnect_shutdown(shutdown, wnet)
  279. local netmd = require "luci.model.network".init()
  280. local net = netmd:get_wifinet(wnet)
  281. local dev = net:get_device()
  282. if dev and net then
  283. dev:set("disabled", nil)
  284. net:set("disabled", shutdown and 1 or nil)
  285. netmd:commit("wireless")
  286. luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>/dev/null")
  287. luci.http.status(200, shutdown and "Shutdown" or "Reconnected")
  288. return
  289. end
  290. luci.http.status(404, "No such radio")
  291. end
  292. function wifi_reconnect(wnet)
  293. wifi_reconnect_shutdown(false, wnet)
  294. end
  295. function wifi_shutdown(wnet)
  296. wifi_reconnect_shutdown(true, wnet)
  297. end
  298. function lease_status()
  299. local s = require "luci.tools.status"
  300. luci.http.prepare_content("application/json")
  301. luci.http.write('[')
  302. luci.http.write_json(s.dhcp_leases())
  303. luci.http.write(',')
  304. luci.http.write_json(s.dhcp6_leases())
  305. luci.http.write(']')
  306. end
  307. function switch_status(switches)
  308. local s = require "luci.tools.status"
  309. luci.http.prepare_content("application/json")
  310. luci.http.write_json(s.switch_status(switches))
  311. end
  312. function diag_command(cmd, addr)
  313. if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
  314. luci.http.prepare_content("text/plain")
  315. local util = io.popen(cmd % addr)
  316. if util then
  317. while true do
  318. local ln = util:read("*l")
  319. if not ln then break end
  320. luci.http.write(ln)
  321. luci.http.write("\n")
  322. end
  323. util:close()
  324. end
  325. return
  326. end
  327. luci.http.status(500, "Bad address")
  328. end
  329. function diag_ping(addr)
  330. diag_command("ping -c 5 -W 1 %q 2>&1", addr)
  331. end
  332. function diag_traceroute(addr)
  333. diag_command("traceroute -q 1 -w 1 -n %q 2>&1", addr)
  334. end
  335. function diag_nslookup(addr)
  336. diag_command("nslookup %q 2>&1", addr)
  337. end
  338. function diag_ping6(addr)
  339. diag_command("ping6 -c 5 %q 2>&1", addr)
  340. end
  341. function diag_traceroute6(addr)
  342. diag_command("traceroute6 -q 1 -w 2 -n %q 2>&1", addr)
  343. end