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 = cbi("admin_network/dhcp")
107 page.title = _("DHCP and DNS")
110 page = node("admin", "network", "hosts")
111 page.target = cbi("admin_network/hosts")
112 page.title = _("Hostnames")
116 page = node("admin", "network", "routes")
117 page.target = cbi("admin_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 rx_bytes = device:rx_bytes(),
216 tx_bytes = device:tx_bytes(),
217 rx_packets = device:rx_packets(),
218 tx_packets = device:tx_packets(),
223 for _, device in ipairs(net:get_interfaces() or {}) do
224 data.subdevices[#data.subdevices+1] = {
225 name = device:shortname(),
226 type = device:type(),
227 typename = device:get_type_i18n(),
228 ifname = device:name(),
229 macaddr = device:mac(),
230 is_up = device:is_up(),
231 rx_bytes = device:rx_bytes(),
232 tx_bytes = device:tx_bytes(),
233 rx_packets = device:rx_packets(),
234 tx_packets = device:tx_packets(),
249 luci.http.prepare_content("application/json")
250 luci.http.write_json(rv)
254 luci.http.status(404, "No such device")
257 function iface_reconnect(iface)
258 local netmd = require "luci.model.network".init()
259 local net = netmd:get_network(iface)
261 luci.sys.call("env -i /sbin/ifup %s >/dev/null 2>/dev/null"
262 % luci.util.shellquote(iface))
263 luci.http.status(200, "Reconnected")
267 luci.http.status(404, "No such interface")
270 function wifi_status(devs)
271 local s = require "luci.tools.status"
274 if type(devs) == "string" then
276 for dev in devs:gmatch("[%w%.%-]+") do
277 rv[#rv+1] = s.wifi_network(dev)
282 luci.http.prepare_content("application/json")
283 luci.http.write_json(rv)
287 luci.http.status(404, "No such device")
290 function wifi_reconnect(radio)
291 local rc = luci.sys.call("env -i /sbin/wifi up %s" % luci.util.shellquote(radio))
294 luci.http.status(200, "Reconnected")
296 luci.http.status(500, "Error")
300 local function _wifi_get_scan_results(cache_key)
301 local results = luci.util.ubus("session", "get", {
302 ubus_rpc_session = luci.model.uci:get_session_id(),
306 if type(results) == "table" and
307 type(results.values) == "table" and
308 type(results.values[cache_key]) == "table"
310 return results.values[cache_key]
316 function wifi_scan_trigger(radio, update)
317 local iw = radio and luci.sys.wifi.getiwinfo(radio)
320 luci.http.status(404, "No such radio device")
324 luci.http.status(204, "Scan scheduled")
326 if nixio.fork() == 0 then
331 local data, bssids = { }, { }
332 local cache_key = "scan_%s" % radio
334 luci.util.ubus("session", "set", {
335 ubus_rpc_session = luci.model.uci:get_session_id(),
336 values = { [cache_key] = nil }
339 for _, bss in ipairs(iw.scanlist or { }) do
341 bssids[bss.bssid] = bss
345 for _, bss in ipairs(_wifi_get_scan_results(cache_key)) do
346 if not bssids[bss.bssid] then
348 data[#data + 1] = bss
353 luci.util.ubus("session", "set", {
354 ubus_rpc_session = luci.model.uci:get_session_id(),
355 values = { [cache_key] = data }
360 function wifi_scan_results(radio)
361 local results = radio and _wifi_get_scan_results("scan_%s" % radio)
363 if results and #results > 0 then
364 luci.http.prepare_content("application/json")
365 luci.http.write_json(results)
367 luci.http.status(404, "No wireless scan results")
371 function switch_status(switches)
372 local s = require "luci.tools.status"
374 luci.http.prepare_content("application/json")
375 luci.http.write_json(s.switch_status(switches))
378 function diag_command(cmd, addr)
379 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
380 luci.http.prepare_content("text/plain")
382 local util = io.popen(cmd % luci.util.shellquote(addr))
385 local ln = util:read("*l")
386 if not ln then break end
388 luci.http.write("\n")
397 luci.http.status(500, "Bad address")
400 function diag_ping(addr)
401 diag_command("ping -c 5 -W 1 %s 2>&1", addr)
404 function diag_traceroute(addr)
405 diag_command("traceroute -q 1 -w 1 -n %s 2>&1", addr)
408 function diag_nslookup(addr)
409 diag_command("nslookup %s 2>&1", addr)
412 function diag_ping6(addr)
413 diag_command("ping6 -c 5 %s 2>&1", addr)
416 function diag_traceroute6(addr)
417 diag_command("traceroute6 -q 1 -w 2 -n %s 2>&1", addr)