2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
15 module("luci.controller.admin.network", package.seeall)
18 local uci = require("luci.model.uci").cursor()
21 page = node("admin", "network")
22 page.target = firstchild()
23 page.title = _("Network")
28 local has_switch = false
30 uci:foreach("network", "switch",
37 page = node("admin", "network", "vlan")
38 page.target = cbi("admin_network/vlan")
39 page.title = _("Switch")
42 page = entry({"admin", "network", "switch_status"}, call("switch_status"), nil)
47 local has_wifi = false
49 uci:foreach("wireless", "wifi-device",
56 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil)
59 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil)
62 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil)
65 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil)
68 page = entry({"admin", "network", "wireless_reconnect"}, call("wifi_reconnect"), nil)
71 page = entry({"admin", "network", "wireless_shutdown"}, call("wifi_shutdown"), nil)
74 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wifi"), 15)
80 local net = require "luci.model.network".init(uci)
81 for _, wdev in ipairs(net:get_wifidevs()) do
83 for _, wnet in ipairs(wdev:get_wifinets()) do
85 {"admin", "network", "wireless", wnet:id()},
86 alias("admin", "network", "wireless"),
87 wdev:name() .. ": " .. wnet:shortname()
95 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
98 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
101 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
104 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
107 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
110 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)
115 uci:foreach("network", "interface",
117 local ifc = section[".name"]
118 if ifc ~= "loopback" then
119 entry({"admin", "network", "network", ifc},
126 if nixio.fs.access("/etc/config/dhcp") then
127 page = node("admin", "network", "dhcp")
128 page.target = cbi("admin_network/dhcp")
129 page.title = _("DHCP and DNS")
132 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
135 page = node("admin", "network", "hosts")
136 page.target = cbi("admin_network/hosts")
137 page.title = _("Hostnames")
141 if nixio.fs.access("/etc/config/6relayd") then
142 page = node("admin", "network", "ipv6")
143 page.target = cbi("admin_network/ipv6")
144 page.title = _("IPv6 RA and DHCPv6")
148 page = node("admin", "network", "routes")
149 page.target = cbi("admin_network/routes")
150 page.title = _("Static Routes")
153 page = node("admin", "network", "diagnostics")
154 page.target = template("admin_network/diagnostics")
155 page.title = _("Diagnostics")
158 page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
161 page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
164 page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
167 page = entry({"admin", "network", "diag_ping6"}, call("diag_ping6"), nil)
170 page = entry({"admin", "network", "diag_traceroute6"}, call("diag_traceroute6"), nil)
176 local function param(x)
177 return luci.http.formvalue(x)
180 local function ptable(x)
182 return x and (type(x) ~= "table" and { x } or x) or {}
185 local dev = param("device")
186 local ssid = param("join")
189 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
192 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
194 local cbi = require "luci.cbi"
195 local tpl = require "luci.template"
196 local map = luci.cbi.load("admin_network/wifi_add")[1]
198 if map:parse() ~= cbi.FORM_DONE then
205 luci.template.render("admin_network/wifi_join")
210 local dev = luci.http.formvalue("device")
211 local ntm = require "luci.model.network".init()
213 dev = dev and ntm:get_wifidev(dev)
216 local net = dev:add_wifinet({
223 luci.http.redirect(net:adminlink())
227 function wifi_delete(network)
228 local ntm = require "luci.model.network".init()
229 local wnet = ntm:get_wifinet(network)
231 local dev = wnet:get_device()
232 local nets = wnet:get_networks()
234 ntm:del_wifinet(network)
235 ntm:commit("wireless")
237 for _, net in ipairs(nets) do
238 if net:is_empty() then
239 ntm:del_network(net:name())
240 ntm:commit("network")
243 luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>/dev/null")
247 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
250 function iface_status(ifaces)
251 local netm = require "luci.model.network".init()
255 for iface in ifaces:gmatch("[%w%.%-_]+") do
256 local net = netm:get_network(iface)
257 local device = net and net:get_interface()
262 uptime = net:uptime(),
263 gwaddr = net:gwaddr(),
264 dnsaddrs = net:dnsaddrs(),
265 name = device:shortname(),
266 type = device:type(),
267 ifname = device:name(),
268 macaddr = device:mac(),
269 is_up = device:is_up(),
270 rx_bytes = device:rx_bytes(),
271 tx_bytes = device:tx_bytes(),
272 rx_packets = device:rx_packets(),
273 tx_packets = device:tx_packets(),
281 for _, a in ipairs(device:ipaddrs()) do
282 data.ipaddrs[#data.ipaddrs+1] = {
283 addr = a:host():string(),
284 netmask = a:mask():string(),
288 for _, a in ipairs(device:ip6addrs()) do
289 if not a:is6linklocal() then
290 data.ip6addrs[#data.ip6addrs+1] = {
291 addr = a:host():string(),
292 netmask = a:mask():string(),
298 for _, device in ipairs(net:get_interfaces() or {}) do
299 data.subdevices[#data.subdevices+1] = {
300 name = device:shortname(),
301 type = device:type(),
302 ifname = device:name(),
303 macaddr = device:mac(),
304 macaddr = device:mac(),
305 is_up = device:is_up(),
306 rx_bytes = device:rx_bytes(),
307 tx_bytes = device:tx_bytes(),
308 rx_packets = device:rx_packets(),
309 tx_packets = device:tx_packets(),
324 luci.http.prepare_content("application/json")
325 luci.http.write_json(rv)
329 luci.http.status(404, "No such device")
332 function iface_reconnect(iface)
333 local netmd = require "luci.model.network".init()
334 local net = netmd:get_network(iface)
336 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
337 luci.http.status(200, "Reconnected")
341 luci.http.status(404, "No such interface")
344 function iface_shutdown(iface)
345 local netmd = require "luci.model.network".init()
346 local net = netmd:get_network(iface)
348 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
349 luci.http.status(200, "Shutdown")
353 luci.http.status(404, "No such interface")
356 function iface_delete(iface)
357 local netmd = require "luci.model.network".init()
358 local net = netmd:del_network(iface)
360 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
361 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
362 netmd:commit("network")
363 netmd:commit("wireless")
367 luci.http.status(404, "No such interface")
370 function wifi_status(devs)
371 local s = require "luci.tools.status"
375 for dev in devs:gmatch("[%w%.%-]+") do
376 rv[#rv+1] = s.wifi_network(dev)
380 luci.http.prepare_content("application/json")
381 luci.http.write_json(rv)
385 luci.http.status(404, "No such device")
388 local function wifi_reconnect_shutdown(shutdown, wnet)
389 local netmd = require "luci.model.network".init()
390 local net = netmd:get_wifinet(wnet)
391 local dev = net:get_device()
393 dev:set("disabled", nil)
394 net:set("disabled", shutdown and 1 or nil)
395 netmd:commit("wireless")
397 luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>/dev/null")
398 luci.http.status(200, shutdown and "Shutdown" or "Reconnected")
403 luci.http.status(404, "No such radio")
406 function wifi_reconnect(wnet)
407 wifi_reconnect_shutdown(false, wnet)
410 function wifi_shutdown(wnet)
411 wifi_reconnect_shutdown(true, wnet)
414 function lease_status()
415 local s = require "luci.tools.status"
417 luci.http.prepare_content("application/json")
419 luci.http.write_json(s.dhcp_leases())
421 luci.http.write_json(s.dhcp6_leases())
425 function switch_status(switches)
426 local s = require "luci.tools.status"
428 luci.http.prepare_content("application/json")
429 luci.http.write_json(s.switch_status(switches))
432 function diag_command(cmd, addr)
433 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
434 luci.http.prepare_content("text/plain")
436 local util = io.popen(cmd % addr)
439 local ln = util:read("*l")
440 if not ln then break end
442 luci.http.write("\n")
451 luci.http.status(500, "Bad address")
454 function diag_ping(addr)
455 diag_command("ping -c 5 -W 1 %q 2>&1", addr)
458 function diag_traceroute(addr)
459 diag_command("traceroute -q 1 -w 1 -n %q 2>&1", addr)
462 function diag_nslookup(addr)
463 diag_command("nslookup %q 2>&1", addr)
466 function diag_ping6(addr)
467 diag_command("ping6 -c 5 %q 2>&1", addr)
470 function diag_traceroute6(addr)
471 diag_command("traceroute6 -q 1 -w 2 -n %q 2>&1", addr)