network.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2011 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"}, call("wifi_join"), nil)
  36. page.leaf = true
  37. page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil)
  38. page.leaf = true
  39. page = entry({"admin", "network", "wireless_delete"}, call("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"}, call("wifi_reconnect"), nil)
  44. page.leaf = true
  45. page = entry({"admin", "network", "wireless_shutdown"}, call("wifi_shutdown"), nil)
  46. page.leaf = true
  47. page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wifi"), 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"}, call("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"}, call("iface_reconnect"), nil)
  72. page.leaf = true
  73. page = entry({"admin", "network", "iface_shutdown"}, call("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"}, call("diag_ping"), nil)
  109. page.leaf = true
  110. page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
  111. page.leaf = true
  112. page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
  113. page.leaf = true
  114. page = entry({"admin", "network", "diag_ping6"}, call("diag_ping6"), nil)
  115. page.leaf = true
  116. page = entry({"admin", "network", "diag_traceroute6"}, call("diag_traceroute6"), nil)
  117. page.leaf = true
  118. -- end
  119. end
  120. function wifi_join()
  121. local function param(x)
  122. return luci.http.formvalue(x)
  123. end
  124. local function ptable(x)
  125. x = param(x)
  126. return x and (type(x) ~= "table" and { x } or x) or {}
  127. end
  128. local dev = param("device")
  129. local ssid = param("join")
  130. if dev and ssid then
  131. local cancel = (param("cancel") or param("cbi.cancel")) and true or false
  132. if cancel then
  133. luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
  134. else
  135. local cbi = require "luci.cbi"
  136. local tpl = require "luci.template"
  137. local map = luci.cbi.load("admin_network/wifi_add")[1]
  138. if map:parse() ~= cbi.FORM_DONE then
  139. tpl.render("header")
  140. map:render()
  141. tpl.render("footer")
  142. end
  143. end
  144. else
  145. luci.template.render("admin_network/wifi_join")
  146. end
  147. end
  148. function wifi_add()
  149. local dev = luci.http.formvalue("device")
  150. local ntm = require "luci.model.network".init()
  151. dev = dev and ntm:get_wifidev(dev)
  152. if dev then
  153. local net = dev:add_wifinet({
  154. mode = "ap",
  155. ssid = "OpenWrt",
  156. encryption = "none"
  157. })
  158. ntm:save("wireless")
  159. luci.http.redirect(net:adminlink())
  160. end
  161. end
  162. function wifi_delete(network)
  163. local ntm = require "luci.model.network".init()
  164. local wnet = ntm:get_wifinet(network)
  165. if wnet then
  166. local dev = wnet:get_device()
  167. local nets = wnet:get_networks()
  168. if dev then
  169. ntm:del_wifinet(network)
  170. ntm:commit("wireless")
  171. local _, net
  172. for _, net in ipairs(nets) do
  173. if net:is_empty() then
  174. ntm:del_network(net:name())
  175. ntm:commit("network")
  176. end
  177. end
  178. luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>/dev/null")
  179. end
  180. end
  181. luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
  182. end
  183. function iface_status(ifaces)
  184. local netm = require "luci.model.network".init()
  185. local rv = { }
  186. local iface
  187. for iface in ifaces:gmatch("[%w%.%-_]+") do
  188. local net = netm:get_network(iface)
  189. local device = net and net:get_interface()
  190. if device then
  191. local data = {
  192. id = iface,
  193. proto = net:proto(),
  194. uptime = net:uptime(),
  195. gwaddr = net:gwaddr(),
  196. dnsaddrs = net:dnsaddrs(),
  197. name = device:shortname(),
  198. type = device:type(),
  199. ifname = device:name(),
  200. macaddr = device:mac(),
  201. is_up = device:is_up(),
  202. rx_bytes = device:rx_bytes(),
  203. tx_bytes = device:tx_bytes(),
  204. rx_packets = device:rx_packets(),
  205. tx_packets = device:tx_packets(),
  206. ipaddrs = { },
  207. ip6addrs = { },
  208. subdevices = { }
  209. }
  210. local _, a
  211. for _, a in ipairs(device:ipaddrs()) do
  212. data.ipaddrs[#data.ipaddrs+1] = {
  213. addr = a:host():string(),
  214. netmask = a:mask():string(),
  215. prefix = a:prefix()
  216. }
  217. end
  218. for _, a in ipairs(device:ip6addrs()) do
  219. if not a:is6linklocal() then
  220. data.ip6addrs[#data.ip6addrs+1] = {
  221. addr = a:host():string(),
  222. netmask = a:mask():string(),
  223. prefix = a:prefix()
  224. }
  225. end
  226. end
  227. for _, device in ipairs(net:get_interfaces() or {}) do
  228. data.subdevices[#data.subdevices+1] = {
  229. name = device:shortname(),
  230. type = device:type(),
  231. ifname = device:name(),
  232. macaddr = device:mac(),
  233. macaddr = device:mac(),
  234. is_up = device:is_up(),
  235. rx_bytes = device:rx_bytes(),
  236. tx_bytes = device:tx_bytes(),
  237. rx_packets = device:rx_packets(),
  238. tx_packets = device:tx_packets(),
  239. }
  240. end
  241. rv[#rv+1] = data
  242. else
  243. rv[#rv+1] = {
  244. id = iface,
  245. name = iface,
  246. type = "ethernet"
  247. }
  248. end
  249. end
  250. if #rv > 0 then
  251. luci.http.prepare_content("application/json")
  252. luci.http.write_json(rv)
  253. return
  254. end
  255. luci.http.status(404, "No such device")
  256. end
  257. function iface_reconnect(iface)
  258. local netmd = require "luci.model.network".init()
  259. local net = netmd:get_network(iface)
  260. if net then
  261. luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
  262. luci.http.status(200, "Reconnected")
  263. return
  264. end
  265. luci.http.status(404, "No such interface")
  266. end
  267. function iface_shutdown(iface)
  268. local netmd = require "luci.model.network".init()
  269. local net = netmd:get_network(iface)
  270. if net then
  271. luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
  272. luci.http.status(200, "Shutdown")
  273. return
  274. end
  275. luci.http.status(404, "No such interface")
  276. end
  277. function iface_delete(iface)
  278. local netmd = require "luci.model.network".init()
  279. local net = netmd:del_network(iface)
  280. if net then
  281. luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
  282. luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
  283. netmd:commit("network")
  284. netmd:commit("wireless")
  285. return
  286. end
  287. luci.http.status(404, "No such interface")
  288. end
  289. function wifi_status(devs)
  290. local s = require "luci.tools.status"
  291. local rv = { }
  292. local dev
  293. for dev in devs:gmatch("[%w%.%-]+") do
  294. rv[#rv+1] = s.wifi_network(dev)
  295. end
  296. if #rv > 0 then
  297. luci.http.prepare_content("application/json")
  298. luci.http.write_json(rv)
  299. return
  300. end
  301. luci.http.status(404, "No such device")
  302. end
  303. local function wifi_reconnect_shutdown(shutdown, wnet)
  304. local netmd = require "luci.model.network".init()
  305. local net = netmd:get_wifinet(wnet)
  306. local dev = net:get_device()
  307. if dev and net then
  308. dev:set("disabled", nil)
  309. net:set("disabled", shutdown and 1 or nil)
  310. netmd:commit("wireless")
  311. luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>/dev/null")
  312. luci.http.status(200, shutdown and "Shutdown" or "Reconnected")
  313. return
  314. end
  315. luci.http.status(404, "No such radio")
  316. end
  317. function wifi_reconnect(wnet)
  318. wifi_reconnect_shutdown(false, wnet)
  319. end
  320. function wifi_shutdown(wnet)
  321. wifi_reconnect_shutdown(true, wnet)
  322. end
  323. function lease_status()
  324. local s = require "luci.tools.status"
  325. luci.http.prepare_content("application/json")
  326. luci.http.write('[')
  327. luci.http.write_json(s.dhcp_leases())
  328. luci.http.write(',')
  329. luci.http.write_json(s.dhcp6_leases())
  330. luci.http.write(']')
  331. end
  332. function switch_status(switches)
  333. local s = require "luci.tools.status"
  334. luci.http.prepare_content("application/json")
  335. luci.http.write_json(s.switch_status(switches))
  336. end
  337. function diag_command(cmd, addr)
  338. if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
  339. luci.http.prepare_content("text/plain")
  340. local util = io.popen(cmd % addr)
  341. if util then
  342. while true do
  343. local ln = util:read("*l")
  344. if not ln then break end
  345. luci.http.write(ln)
  346. luci.http.write("\n")
  347. end
  348. util:close()
  349. end
  350. return
  351. end
  352. luci.http.status(500, "Bad address")
  353. end
  354. function diag_ping(addr)
  355. diag_command("ping -c 5 -W 1 %q 2>&1", addr)
  356. end
  357. function diag_traceroute(addr)
  358. diag_command("traceroute -q 1 -w 1 -n %q 2>&1", addr)
  359. end
  360. function diag_nslookup(addr)
  361. diag_command("nslookup %q 2>&1", addr)
  362. end
  363. function diag_ping6(addr)
  364. diag_command("ping6 -c 5 %q 2>&1", addr)
  365. end
  366. function diag_traceroute6(addr)
  367. diag_command("traceroute6 -q 1 -w 2 -n %q 2>&1", addr)
  368. end