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.
5 module("luci.controller.admin.network", package.seeall)
8 local uci = require("luci.model.uci").cursor()
11 page = node("admin", "network")
12 page.target = firstchild()
13 page.title = _("Network")
18 local has_switch = false
20 uci:foreach("network", "switch",
27 page = node("admin", "network", "vlan")
28 page.target = cbi("admin_network/vlan")
29 page.title = _("Switch")
32 page = entry({"admin", "network", "switch_status"}, call("switch_status"), nil)
37 local has_wifi = false
39 uci:foreach("wireless", "wifi-device",
46 page = entry({"admin", "network", "wireless_assoclist"}, call("wifi_assoclist"), nil)
49 page = entry({"admin", "network", "wireless_join"}, post("wifi_join"), nil)
52 page = entry({"admin", "network", "wireless_add"}, post("wifi_add"), nil)
55 page = entry({"admin", "network", "wireless_delete"}, post("wifi_delete"), nil)
58 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil)
61 page = entry({"admin", "network", "wireless_reconnect"}, post("wifi_reconnect"), nil)
64 page = entry({"admin", "network", "wireless_shutdown"}, post("wifi_shutdown"), nil)
67 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wireless"), 15)
73 local net = require "luci.model.network".init(uci)
74 for _, wdev in ipairs(net:get_wifidevs()) do
76 for _, wnet in ipairs(wdev:get_wifinets()) do
78 {"admin", "network", "wireless", wnet:id()},
79 alias("admin", "network", "wireless"),
80 wdev:name() .. ": " .. wnet:shortname()
88 page = entry({"admin", "network", "iface_add"}, form("admin_network/iface_add"), nil)
91 page = entry({"admin", "network", "iface_delete"}, post("iface_delete"), nil)
94 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
97 page = entry({"admin", "network", "iface_reconnect"}, post("iface_reconnect"), nil)
100 page = entry({"admin", "network", "iface_shutdown"}, post("iface_shutdown"), nil)
103 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)
108 uci:foreach("network", "interface",
110 local ifc = section[".name"]
111 if ifc ~= "loopback" then
112 entry({"admin", "network", "network", ifc},
119 if nixio.fs.access("/etc/config/dhcp") then
120 page = node("admin", "network", "dhcp")
121 page.target = cbi("admin_network/dhcp")
122 page.title = _("DHCP and DNS")
125 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
128 page = node("admin", "network", "hosts")
129 page.target = cbi("admin_network/hosts")
130 page.title = _("Hostnames")
134 page = node("admin", "network", "routes")
135 page.target = cbi("admin_network/routes")
136 page.title = _("Static Routes")
139 page = node("admin", "network", "diagnostics")
140 page.target = template("admin_network/diagnostics")
141 page.title = _("Diagnostics")
144 page = entry({"admin", "network", "diag_ping"}, post("diag_ping"), nil)
147 page = entry({"admin", "network", "diag_nslookup"}, post("diag_nslookup"), nil)
150 page = entry({"admin", "network", "diag_traceroute"}, post("diag_traceroute"), nil)
153 page = entry({"admin", "network", "diag_ping6"}, post("diag_ping6"), nil)
156 page = entry({"admin", "network", "diag_traceroute6"}, post("diag_traceroute6"), nil)
162 local tpl = require "luci.template"
163 local http = require "luci.http"
164 local dev = http.formvalue("device")
165 local ssid = http.formvalue("join")
168 local cancel = (http.formvalue("cancel") or http.formvalue("cbi.cancel"))
170 local cbi = require "luci.cbi"
171 local map = luci.cbi.load("admin_network/wifi_add")[1]
173 if map:parse() ~= cbi.FORM_DONE then
183 tpl.render("admin_network/wifi_join")
187 local dev = luci.http.formvalue("device")
188 local ntm = require "luci.model.network".init()
190 dev = dev and ntm:get_wifidev(dev)
193 local net = dev:add_wifinet({
200 luci.http.redirect(net:adminlink())
204 function wifi_delete(network)
205 local ntm = require "luci.model.network".init()
206 local wnet = ntm:get_wifinet(network)
208 local dev = wnet:get_device()
209 local nets = wnet:get_networks()
211 ntm:del_wifinet(network)
212 ntm:commit("wireless")
214 for _, net in ipairs(nets) do
215 if net:is_empty() then
216 ntm:del_network(net:name())
217 ntm:commit("network")
220 luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>/dev/null")
224 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
227 function iface_status(ifaces)
228 local netm = require "luci.model.network".init()
232 for iface in ifaces:gmatch("[%w%.%-_]+") do
233 local net = netm:get_network(iface)
234 local device = net and net:get_interface()
239 uptime = net:uptime(),
240 gwaddr = net:gwaddr(),
241 ipaddrs = net:ipaddrs(),
242 ip6addrs = net:ip6addrs(),
243 dnsaddrs = net:dnsaddrs(),
244 ip6prefix = net:ip6prefix(),
245 name = device:shortname(),
246 type = device:type(),
247 ifname = device:name(),
248 macaddr = device:mac(),
249 is_up = device:is_up(),
250 rx_bytes = device:rx_bytes(),
251 tx_bytes = device:tx_bytes(),
252 rx_packets = device:rx_packets(),
253 tx_packets = device:tx_packets(),
258 for _, device in ipairs(net:get_interfaces() or {}) do
259 data.subdevices[#data.subdevices+1] = {
260 name = device:shortname(),
261 type = device:type(),
262 ifname = device:name(),
263 macaddr = device:mac(),
264 is_up = device:is_up(),
265 rx_bytes = device:rx_bytes(),
266 tx_bytes = device:tx_bytes(),
267 rx_packets = device:rx_packets(),
268 tx_packets = device:tx_packets(),
283 luci.http.prepare_content("application/json")
284 luci.http.write_json(rv)
288 luci.http.status(404, "No such device")
291 function iface_reconnect(iface)
292 local netmd = require "luci.model.network".init()
293 local net = netmd:get_network(iface)
295 luci.sys.call("env -i /sbin/ifup %s >/dev/null 2>/dev/null"
296 % luci.util.shellquote(iface))
297 luci.http.status(200, "Reconnected")
301 luci.http.status(404, "No such interface")
304 function iface_shutdown(iface)
305 local netmd = require "luci.model.network".init()
306 local net = netmd:get_network(iface)
308 luci.sys.call("env -i /sbin/ifdown %s >/dev/null 2>/dev/null"
309 % luci.util.shellquote(iface))
310 luci.http.status(200, "Shutdown")
314 luci.http.status(404, "No such interface")
317 function iface_delete(iface)
318 local netmd = require "luci.model.network".init()
319 local net = netmd:del_network(iface)
321 luci.sys.call("env -i /sbin/ifdown %s >/dev/null 2>/dev/null"
322 % luci.util.shellquote(iface))
323 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
324 netmd:commit("network")
325 netmd:commit("wireless")
329 luci.http.status(404, "No such interface")
332 function wifi_status(devs)
333 local s = require "luci.tools.status"
336 if type(devs) == "string" then
338 for dev in devs:gmatch("[%w%.%-]+") do
339 rv[#rv+1] = s.wifi_network(dev)
344 luci.http.prepare_content("application/json")
345 luci.http.write_json(rv)
349 luci.http.status(404, "No such device")
352 local function wifi_reconnect_shutdown(shutdown, wnet)
353 local netmd = require "luci.model.network".init()
354 local net = netmd:get_wifinet(wnet)
355 local dev = net:get_device()
357 dev:set("disabled", nil)
358 net:set("disabled", shutdown and 1 or nil)
359 netmd:commit("wireless")
361 luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>/dev/null")
362 luci.http.status(200, shutdown and "Shutdown" or "Reconnected")
367 luci.http.status(404, "No such radio")
370 function wifi_reconnect(wnet)
371 wifi_reconnect_shutdown(false, wnet)
374 function wifi_shutdown(wnet)
375 wifi_reconnect_shutdown(true, wnet)
378 function wifi_assoclist()
379 local s = require "luci.tools.status"
381 luci.http.prepare_content("application/json")
382 luci.http.write_json(s.wifi_assoclist())
385 function lease_status()
386 local s = require "luci.tools.status"
388 luci.http.prepare_content("application/json")
390 luci.http.write_json(s.dhcp_leases())
392 luci.http.write_json(s.dhcp6_leases())
396 function switch_status(switches)
397 local s = require "luci.tools.status"
399 luci.http.prepare_content("application/json")
400 luci.http.write_json(s.switch_status(switches))
403 function diag_command(cmd, addr)
404 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
405 luci.http.prepare_content("text/plain")
407 local util = io.popen(cmd % luci.util.shellquote(addr))
410 local ln = util:read("*l")
411 if not ln then break end
413 luci.http.write("\n")
422 luci.http.status(500, "Bad address")
425 function diag_ping(addr)
426 diag_command("ping -c 5 -W 1 %s 2>&1", addr)
429 function diag_traceroute(addr)
430 diag_command("traceroute -q 1 -w 1 -n %s 2>&1", addr)
433 function diag_nslookup(addr)
434 diag_command("nslookup %s 2>&1", addr)
437 function diag_ping6(addr)
438 diag_command("ping6 -c 5 %s 2>&1", addr)
441 function diag_traceroute6(addr)
442 diag_command("traceroute6 -q 1 -w 2 -n %s 2>&1", addr)