1
0

network.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. ip6prefix = net:ip6prefix(),
  192. name = device:shortname(),
  193. type = device:type(),
  194. ifname = device:name(),
  195. macaddr = device:mac(),
  196. is_up = device:is_up(),
  197. rx_bytes = device:rx_bytes(),
  198. tx_bytes = device:tx_bytes(),
  199. rx_packets = device:rx_packets(),
  200. tx_packets = device:tx_packets(),
  201. subdevices = { }
  202. }
  203. for _, device in ipairs(net:get_interfaces() or {}) do
  204. data.subdevices[#data.subdevices+1] = {
  205. name = device:shortname(),
  206. type = device:type(),
  207. ifname = device:name(),
  208. macaddr = device:mac(),
  209. macaddr = device:mac(),
  210. is_up = device:is_up(),
  211. rx_bytes = device:rx_bytes(),
  212. tx_bytes = device:tx_bytes(),
  213. rx_packets = device:rx_packets(),
  214. tx_packets = device:tx_packets(),
  215. }
  216. end
  217. rv[#rv+1] = data
  218. else
  219. rv[#rv+1] = {
  220. id = iface,
  221. name = iface,
  222. type = "ethernet"
  223. }
  224. end
  225. end
  226. if #rv > 0 then
  227. luci.http.prepare_content("application/json")
  228. luci.http.write_json(rv)
  229. return
  230. end
  231. luci.http.status(404, "No such device")
  232. end
  233. function iface_reconnect(iface)
  234. local netmd = require "luci.model.network".init()
  235. local net = netmd:get_network(iface)
  236. if net then
  237. luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
  238. luci.http.status(200, "Reconnected")
  239. return
  240. end
  241. luci.http.status(404, "No such interface")
  242. end
  243. function iface_shutdown(iface)
  244. local netmd = require "luci.model.network".init()
  245. local net = netmd:get_network(iface)
  246. if net then
  247. luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
  248. luci.http.status(200, "Shutdown")
  249. return
  250. end
  251. luci.http.status(404, "No such interface")
  252. end
  253. function iface_delete(iface)
  254. local netmd = require "luci.model.network".init()
  255. local net = netmd:del_network(iface)
  256. if net then
  257. luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
  258. luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
  259. netmd:commit("network")
  260. netmd:commit("wireless")
  261. return
  262. end
  263. luci.http.status(404, "No such interface")
  264. end
  265. function wifi_status(devs)
  266. local s = require "luci.tools.status"
  267. local rv = { }
  268. local dev
  269. for dev in devs:gmatch("[%w%.%-]+") do
  270. rv[#rv+1] = s.wifi_network(dev)
  271. end
  272. if #rv > 0 then
  273. luci.http.prepare_content("application/json")
  274. luci.http.write_json(rv)
  275. return
  276. end
  277. luci.http.status(404, "No such device")
  278. end
  279. local function wifi_reconnect_shutdown(shutdown, wnet)
  280. local netmd = require "luci.model.network".init()
  281. local net = netmd:get_wifinet(wnet)
  282. local dev = net:get_device()
  283. if dev and net then
  284. dev:set("disabled", nil)
  285. net:set("disabled", shutdown and 1 or nil)
  286. netmd:commit("wireless")
  287. luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>/dev/null")
  288. luci.http.status(200, shutdown and "Shutdown" or "Reconnected")
  289. return
  290. end
  291. luci.http.status(404, "No such radio")
  292. end
  293. function wifi_reconnect(wnet)
  294. wifi_reconnect_shutdown(false, wnet)
  295. end
  296. function wifi_shutdown(wnet)
  297. wifi_reconnect_shutdown(true, wnet)
  298. end
  299. function lease_status()
  300. local s = require "luci.tools.status"
  301. luci.http.prepare_content("application/json")
  302. luci.http.write('[')
  303. luci.http.write_json(s.dhcp_leases())
  304. luci.http.write(',')
  305. luci.http.write_json(s.dhcp6_leases())
  306. luci.http.write(']')
  307. end
  308. function switch_status(switches)
  309. local s = require "luci.tools.status"
  310. luci.http.prepare_content("application/json")
  311. luci.http.write_json(s.switch_status(switches))
  312. end
  313. function diag_command(cmd, addr)
  314. if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
  315. luci.http.prepare_content("text/plain")
  316. local util = io.popen(cmd % addr)
  317. if util then
  318. while true do
  319. local ln = util:read("*l")
  320. if not ln then break end
  321. luci.http.write(ln)
  322. luci.http.write("\n")
  323. end
  324. util:close()
  325. end
  326. return
  327. end
  328. luci.http.status(500, "Bad address")
  329. end
  330. function diag_ping(addr)
  331. diag_command("ping -c 5 -W 1 %q 2>&1", addr)
  332. end
  333. function diag_traceroute(addr)
  334. diag_command("traceroute -q 1 -w 1 -n %q 2>&1", addr)
  335. end
  336. function diag_nslookup(addr)
  337. diag_command("nslookup %q 2>&1", addr)
  338. end
  339. function diag_ping6(addr)
  340. diag_command("ping6 -c 5 %q 2>&1", addr)
  341. end
  342. function diag_traceroute6(addr)
  343. diag_command("traceroute6 -q 1 -w 2 -n %q 2>&1", addr)
  344. end