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_join"}, post("wifi_join"), nil)
49 page = entry({"admin", "network", "wireless_add"}, post("wifi_add"), nil)
52 page = entry({"admin", "network", "wireless_delete"}, post("wifi_delete"), nil)
55 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil)
58 page = entry({"admin", "network", "wireless_reconnect"}, post("wifi_reconnect"), nil)
61 page = entry({"admin", "network", "wireless_shutdown"}, post("wifi_shutdown"), nil)
64 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wifi"), 15)
70 local net = require "luci.model.network".init(uci)
71 for _, wdev in ipairs(net:get_wifidevs()) do
73 for _, wnet in ipairs(wdev:get_wifinets()) do
75 {"admin", "network", "wireless", wnet:id()},
76 alias("admin", "network", "wireless"),
77 wdev:name() .. ": " .. wnet:shortname()
85 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
88 page = entry({"admin", "network", "iface_delete"}, post("iface_delete"), nil)
91 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
94 page = entry({"admin", "network", "iface_reconnect"}, post("iface_reconnect"), nil)
97 page = entry({"admin", "network", "iface_shutdown"}, post("iface_shutdown"), nil)
100 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)
105 uci:foreach("network", "interface",
107 local ifc = section[".name"]
108 if ifc ~= "loopback" then
109 entry({"admin", "network", "network", ifc},
116 if nixio.fs.access("/etc/config/dhcp") then
117 page = node("admin", "network", "dhcp")
118 page.target = cbi("admin_network/dhcp")
119 page.title = _("DHCP and DNS")
122 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
125 page = node("admin", "network", "hosts")
126 page.target = cbi("admin_network/hosts")
127 page.title = _("Hostnames")
131 page = node("admin", "network", "routes")
132 page.target = cbi("admin_network/routes")
133 page.title = _("Static Routes")
136 page = node("admin", "network", "diagnostics")
137 page.target = template("admin_network/diagnostics")
138 page.title = _("Diagnostics")
141 page = entry({"admin", "network", "diag_ping"}, post("diag_ping"), nil)
144 page = entry({"admin", "network", "diag_nslookup"}, post("diag_nslookup"), nil)
147 page = entry({"admin", "network", "diag_traceroute"}, post("diag_traceroute"), nil)
150 page = entry({"admin", "network", "diag_ping6"}, post("diag_ping6"), nil)
153 page = entry({"admin", "network", "diag_traceroute6"}, post("diag_traceroute6"), nil)
159 local tpl = require "luci.template"
160 local http = require "luci.http"
161 local dev = http.formvalue("device")
162 local ssid = http.formvalue("join")
165 local cancel = (http.formvalue("cancel") or http.formvalue("cbi.cancel"))
167 local cbi = require "luci.cbi"
168 local map = luci.cbi.load("admin_network/wifi_add")[1]
170 if map:parse() ~= cbi.FORM_DONE then
180 tpl.render("admin_network/wifi_join")
184 local dev = luci.http.formvalue("device")
185 local ntm = require "luci.model.network".init()
187 dev = dev and ntm:get_wifidev(dev)
190 local net = dev:add_wifinet({
197 luci.http.redirect(net:adminlink())
201 function wifi_delete(network)
202 local ntm = require "luci.model.network".init()
203 local wnet = ntm:get_wifinet(network)
205 local dev = wnet:get_device()
206 local nets = wnet:get_networks()
208 ntm:del_wifinet(network)
209 ntm:commit("wireless")
211 for _, net in ipairs(nets) do
212 if net:is_empty() then
213 ntm:del_network(net:name())
214 ntm:commit("network")
217 luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>/dev/null")
221 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
224 function iface_status(ifaces)
225 local netm = require "luci.model.network".init()
229 for iface in ifaces:gmatch("[%w%.%-_]+") do
230 local net = netm:get_network(iface)
231 local device = net and net:get_interface()
236 uptime = net:uptime(),
237 gwaddr = net:gwaddr(),
238 dnsaddrs = net:dnsaddrs(),
239 name = device:shortname(),
240 type = device:type(),
241 ifname = device:name(),
242 macaddr = device:mac(),
243 is_up = device:is_up(),
244 rx_bytes = device:rx_bytes(),
245 tx_bytes = device:tx_bytes(),
246 rx_packets = device:rx_packets(),
247 tx_packets = device:tx_packets(),
255 for _, a in ipairs(device:ipaddrs()) do
256 data.ipaddrs[#data.ipaddrs+1] = {
257 addr = a:host():string(),
258 netmask = a:mask():string(),
262 for _, a in ipairs(device:ip6addrs()) do
263 if not a:is6linklocal() then
264 data.ip6addrs[#data.ip6addrs+1] = {
265 addr = a:host():string(),
266 netmask = a:mask():string(),
272 for _, device in ipairs(net:get_interfaces() or {}) do
273 data.subdevices[#data.subdevices+1] = {
274 name = device:shortname(),
275 type = device:type(),
276 ifname = device:name(),
277 macaddr = device:mac(),
278 macaddr = device:mac(),
279 is_up = device:is_up(),
280 rx_bytes = device:rx_bytes(),
281 tx_bytes = device:tx_bytes(),
282 rx_packets = device:rx_packets(),
283 tx_packets = device:tx_packets(),
298 luci.http.prepare_content("application/json")
299 luci.http.write_json(rv)
303 luci.http.status(404, "No such device")
306 function iface_reconnect(iface)
307 local netmd = require "luci.model.network".init()
308 local net = netmd:get_network(iface)
310 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
311 luci.http.status(200, "Reconnected")
315 luci.http.status(404, "No such interface")
318 function iface_shutdown(iface)
319 local netmd = require "luci.model.network".init()
320 local net = netmd:get_network(iface)
322 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
323 luci.http.status(200, "Shutdown")
327 luci.http.status(404, "No such interface")
330 function iface_delete(iface)
331 local netmd = require "luci.model.network".init()
332 local net = netmd:del_network(iface)
334 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
335 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
336 netmd:commit("network")
337 netmd:commit("wireless")
341 luci.http.status(404, "No such interface")
344 function wifi_status(devs)
345 local s = require "luci.tools.status"
349 for dev in devs:gmatch("[%w%.%-]+") do
350 rv[#rv+1] = s.wifi_network(dev)
354 luci.http.prepare_content("application/json")
355 luci.http.write_json(rv)
359 luci.http.status(404, "No such device")
362 local function wifi_reconnect_shutdown(shutdown, wnet)
363 local netmd = require "luci.model.network".init()
364 local net = netmd:get_wifinet(wnet)
365 local dev = net:get_device()
367 dev:set("disabled", nil)
368 net:set("disabled", shutdown and 1 or nil)
369 netmd:commit("wireless")
371 luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>/dev/null")
372 luci.http.status(200, shutdown and "Shutdown" or "Reconnected")
377 luci.http.status(404, "No such radio")
380 function wifi_reconnect(wnet)
381 wifi_reconnect_shutdown(false, wnet)
384 function wifi_shutdown(wnet)
385 wifi_reconnect_shutdown(true, wnet)
388 function lease_status()
389 local s = require "luci.tools.status"
391 luci.http.prepare_content("application/json")
393 luci.http.write_json(s.dhcp_leases())
395 luci.http.write_json(s.dhcp6_leases())
399 function switch_status(switches)
400 local s = require "luci.tools.status"
402 luci.http.prepare_content("application/json")
403 luci.http.write_json(s.switch_status(switches))
406 function diag_command(cmd, addr)
407 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
408 luci.http.prepare_content("text/plain")
410 local util = io.popen(cmd % addr)
413 local ln = util:read("*l")
414 if not ln then break end
416 luci.http.write("\n")
425 luci.http.status(500, "Bad address")
428 function diag_ping(addr)
429 diag_command("ping -c 5 -W 1 %q 2>&1", addr)
432 function diag_traceroute(addr)
433 diag_command("traceroute -q 1 -w 1 -n %q 2>&1", addr)
436 function diag_nslookup(addr)
437 diag_command("nslookup %q 2>&1", addr)
440 function diag_ping6(addr)
441 diag_command("ping6 -c 5 %q 2>&1", addr)
444 function diag_traceroute6(addr)
445 diag_command("traceroute6 -q 1 -w 2 -n %q 2>&1", addr)