1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2011-2018 Jo-Philipp Wich <jo@mein.io>
3 -- Licensed to the public under the Apache License 2.0.
5 module("luci.controller.admin.network", package.seeall)
8 local uci = require("luci.model.uci").cursor()
12 local has_switch = false
14 uci:foreach("network", "switch",
21 page = node("admin", "network", "vlan")
22 page.target = cbi("admin_network/vlan")
23 page.title = _("Switch")
26 page = entry({"admin", "network", "switch_status"}, call("switch_status"), nil)
31 local has_wifi = false
33 uci:foreach("wireless", "wifi-device",
40 page = entry({"admin", "network", "wireless_join"}, post("wifi_join"), nil)
43 page = entry({"admin", "network", "wireless_add"}, post("wifi_add"), nil)
46 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil)
49 page = entry({"admin", "network", "wireless_reconnect"}, post("wifi_reconnect"), nil)
52 page = entry({"admin", "network", "wireless_scan_trigger"}, post("wifi_scan_trigger"), nil)
55 page = entry({"admin", "network", "wireless_scan_results"}, call("wifi_scan_results"), nil)
58 page = entry({"admin", "network", "wireless"}, arcombine(cbi("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wireless"), 15)
64 local net = require "luci.model.network".init(uci)
65 for _, wdev in ipairs(net:get_wifidevs()) do
67 for _, wnet in ipairs(wdev:get_wifinets()) do
69 {"admin", "network", "wireless", wnet:id()},
70 alias("admin", "network", "wireless"),
71 wdev:name() .. ": " .. wnet:shortname()
79 page = entry({"admin", "network", "iface_add"}, form("admin_network/iface_add"), nil)
82 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
85 page = entry({"admin", "network", "iface_reconnect"}, post("iface_reconnect"), nil)
88 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)
93 uci:foreach("network", "interface",
95 local ifc = section[".name"]
96 if ifc ~= "loopback" then
97 entry({"admin", "network", "network", ifc},
104 if nixio.fs.access("/etc/config/dhcp") then
105 page = node("admin", "network", "dhcp")
106 page.target = view("network/dhcp")
107 page.title = _("DHCP and DNS")
110 page = node("admin", "network", "hosts")
111 page.target = view("network/hosts")
112 page.title = _("Hostnames")
116 page = node("admin", "network", "routes")
117 page.target = view("network/routes")
118 page.title = _("Static Routes")
121 page = node("admin", "network", "diagnostics")
122 page.target = template("admin_network/diagnostics")
123 page.title = _("Diagnostics")
126 page = entry({"admin", "network", "diag_ping"}, post("diag_ping"), nil)
129 page = entry({"admin", "network", "diag_nslookup"}, post("diag_nslookup"), nil)
132 page = entry({"admin", "network", "diag_traceroute"}, post("diag_traceroute"), nil)
135 page = entry({"admin", "network", "diag_ping6"}, post("diag_ping6"), nil)
138 page = entry({"admin", "network", "diag_traceroute6"}, post("diag_traceroute6"), nil)
144 local tpl = require "luci.template"
145 local http = require "luci.http"
146 local dev = http.formvalue("device")
147 local ssid = http.formvalue("join")
150 local cancel = (http.formvalue("cancel") or http.formvalue("cbi.cancel"))
152 local cbi = require "luci.cbi"
153 local map = luci.cbi.load("admin_network/wifi_add")[1]
155 if map:parse() ~= cbi.FORM_DONE then
165 tpl.render("admin_network/wifi_join")
169 local dev = luci.http.formvalue("device")
170 local ntm = require "luci.model.network".init()
172 dev = dev and ntm:get_wifidev(dev)
175 local net = dev:add_wifinet({
183 luci.http.redirect(net:adminlink())
187 function iface_status(ifaces)
188 local netm = require "luci.model.network".init()
192 for iface in ifaces:gmatch("[%w%.%-_]+") do
193 local net = netm:get_network(iface)
194 local device = net and net:get_interface()
198 desc = net:get_i18n(),
200 uptime = net:uptime(),
201 gwaddr = net:gwaddr(),
202 ipaddrs = net:ipaddrs(),
203 ip6addrs = net:ip6addrs(),
204 dnsaddrs = net:dnsaddrs(),
205 ip6prefix = net:ip6prefix(),
206 errors = net:errors(),
207 name = device:shortname(),
208 type = device:type(),
209 typename = device:get_type_i18n(),
210 ifname = device:name(),
211 macaddr = device:mac(),
212 is_up = net:is_up() and device:is_up(),
213 is_alias = net:is_alias(),
214 is_dynamic = net:is_dynamic(),
215 is_auto = net:is_auto(),
216 rx_bytes = device:rx_bytes(),
217 tx_bytes = device:tx_bytes(),
218 rx_packets = device:rx_packets(),
219 tx_packets = device:tx_packets(),
224 for _, device in ipairs(net:get_interfaces() or {}) do
225 data.subdevices[#data.subdevices+1] = {
226 name = device:shortname(),
227 type = device:type(),
228 typename = device:get_type_i18n(),
229 ifname = device:name(),
230 macaddr = device:mac(),
231 is_up = device:is_up(),
232 rx_bytes = device:rx_bytes(),
233 tx_bytes = device:tx_bytes(),
234 rx_packets = device:rx_packets(),
235 tx_packets = device:tx_packets(),
250 luci.http.prepare_content("application/json")
251 luci.http.write_json(rv)
255 luci.http.status(404, "No such device")
258 function iface_reconnect(iface)
259 local netmd = require "luci.model.network".init()
260 local net = netmd:get_network(iface)
262 luci.sys.call("env -i /sbin/ifup %s >/dev/null 2>/dev/null"
263 % luci.util.shellquote(iface))
264 luci.http.status(200, "Reconnected")
268 luci.http.status(404, "No such interface")
271 function wifi_status(devs)
272 local s = require "luci.tools.status"
275 if type(devs) == "string" then
277 for dev in devs:gmatch("[%w%.%-]+") do
278 rv[#rv+1] = s.wifi_network(dev)
283 luci.http.prepare_content("application/json")
284 luci.http.write_json(rv)
288 luci.http.status(404, "No such device")
291 function wifi_reconnect(radio)
292 local rc = luci.sys.call("env -i /sbin/wifi up %s" % luci.util.shellquote(radio))
295 luci.http.status(200, "Reconnected")
297 luci.http.status(500, "Error")
301 local function _wifi_get_scan_results(cache_key)
302 local results = luci.util.ubus("session", "get", {
303 ubus_rpc_session = luci.model.uci:get_session_id(),
307 if type(results) == "table" and
308 type(results.values) == "table" and
309 type(results.values[cache_key]) == "table"
311 return results.values[cache_key]
317 function wifi_scan_trigger(radio, update)
318 local iw = radio and luci.sys.wifi.getiwinfo(radio)
321 luci.http.status(404, "No such radio device")
325 luci.http.status(204, "Scan scheduled")
327 if nixio.fork() == 0 then
332 local data, bssids = { }, { }
333 local cache_key = "scan_%s" % radio
335 luci.util.ubus("session", "set", {
336 ubus_rpc_session = luci.model.uci:get_session_id(),
337 values = { [cache_key] = nil }
340 for _, bss in ipairs(iw.scanlist or { }) do
342 bssids[bss.bssid] = bss
346 local cached = _wifi_get_scan_results(cache_key)
348 for _, bss in ipairs(cached) do
349 if not bssids[bss.bssid] then
351 data[#data + 1] = bss
357 luci.util.ubus("session", "set", {
358 ubus_rpc_session = luci.model.uci:get_session_id(),
359 values = { [cache_key] = data }
364 function wifi_scan_results(radio)
365 local results = radio and _wifi_get_scan_results("scan_%s" % radio)
368 luci.http.prepare_content("application/json")
369 luci.http.write_json(results)
371 luci.http.status(404, "No wireless scan results")
375 function switch_status(switches)
376 local s = require "luci.tools.status"
378 luci.http.prepare_content("application/json")
379 luci.http.write_json(s.switch_status(switches))
382 function diag_command(cmd, addr)
383 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
384 luci.http.prepare_content("text/plain")
386 local util = io.popen(cmd % luci.util.shellquote(addr))
389 local ln = util:read("*l")
390 if not ln then break end
392 luci.http.write("\n")
401 luci.http.status(500, "Bad address")
404 function diag_ping(addr)
405 diag_command("ping -c 5 -W 1 %s 2>&1", addr)
408 function diag_traceroute(addr)
409 diag_command("traceroute -q 1 -w 1 -n %s 2>&1", addr)
412 function diag_nslookup(addr)
413 diag_command("nslookup %s 2>&1", addr)
416 function diag_ping6(addr)
417 diag_command("ping6 -c 5 %s 2>&1", addr)
420 function diag_traceroute6(addr)
421 diag_command("traceroute6 -q 1 -w 2 -n %s 2>&1", addr)