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
16 module("luci.controller.admin.network", package.seeall)
19 local uci = require("luci.model.uci").cursor()
23 page = node("admin", "network")
24 page.target = firstchild()
25 page.title = _("Network")
30 local has_switch = false
32 uci:foreach("network", "switch",
39 page = node("admin", "network", "vlan")
40 page.target = cbi("admin_network/vlan")
41 page.title = _("Switch")
46 local has_wifi = false
48 uci:foreach("wireless", "wifi-device",
55 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil)
58 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil)
61 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil)
64 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil)
67 page = entry({"admin", "network", "wireless_reconnect"}, call("wifi_reconnect"), nil)
70 page = entry({"admin", "network", "wireless_shutdown"}, call("wifi_reconnect"), nil)
73 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wifi"), 15)
79 local net = require "luci.model.network".init(uci)
80 for _, wdev in ipairs(net:get_wifidevs()) do
82 for _, wnet in ipairs(wdev:get_wifinets()) do
84 {"admin", "network", "wireless", wnet:id()},
85 alias("admin", "network", "wireless"),
86 wdev:name() .. ": " .. wnet:shortname()
94 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
97 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
100 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
103 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
106 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
109 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)
114 uci:foreach("network", "interface",
116 local ifc = section[".name"]
117 if ifc ~= "loopback" then
118 entry({"admin", "network", "network", ifc},
125 if nixio.fs.access("/etc/config/dhcp") then
126 page = node("admin", "network", "dhcp")
127 page.target = cbi("admin_network/dhcp")
128 page.title = _("DHCP and DNS")
131 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
134 page = node("admin", "network", "hosts")
135 page.target = cbi("admin_network/hosts")
136 page.title = _("Hostnames")
140 page = node("admin", "network", "routes")
141 page.target = cbi("admin_network/routes")
142 page.title = _("Static Routes")
145 page = node("admin", "network", "diagnostics")
146 page.target = template("admin_network/diagnostics")
147 page.title = _("Diagnostics")
150 page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
153 page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
156 page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
162 local function param(x)
163 return luci.http.formvalue(x)
166 local function ptable(x)
168 return x and (type(x) ~= "table" and { x } or x) or {}
171 local dev = param("device")
172 local ssid = param("join")
175 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
178 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
180 local cbi = require "luci.cbi"
181 local tpl = require "luci.template"
182 local map = luci.cbi.load("admin_network/wifi_add")[1]
184 if map:parse() ~= cbi.FORM_DONE then
191 luci.template.render("admin_network/wifi_join")
196 local dev = luci.http.formvalue("device")
197 local ntm = require "luci.model.network".init()
199 dev = dev and ntm:get_wifidev(dev)
202 local net = dev:add_wifinet({
209 luci.http.redirect(net:adminlink())
213 function wifi_delete(network)
214 local ntm = require "luci.model.network".init()
215 local wnet = ntm:get_wifinet(network)
217 local dev = wnet:get_device()
218 local net = wnet:get_network()
220 luci.sys.call("env -i /sbin/wifi down %q" % dev:name())
221 ntm:del_wifinet(network)
222 ntm:commit("wireless")
223 if net:is_empty() then
224 ntm:del_network(net:name())
225 ntm:commit("network")
227 luci.sys.call("env -i /sbin/wifi up %q" % dev:name())
231 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
234 function iface_status()
235 local path = luci.dispatcher.context.requestpath
236 local netm = require "luci.model.network".init()
240 for iface in path[#path]:gmatch("[%w%.%-_]+") do
241 local net = netm:get_network(iface)
242 local device = net and net:get_interface()
247 uptime = net:uptime(),
248 gwaddr = net:gwaddr(),
249 dnsaddrs = net:dnsaddrs(),
250 name = device:shortname(),
251 type = device:type(),
252 ifname = device:name(),
253 macaddr = device:mac(),
254 is_up = device:is_up(),
255 rx_bytes = device:rx_bytes(),
256 tx_bytes = device:tx_bytes(),
257 rx_packets = device:rx_packets(),
258 tx_packets = device:tx_packets(),
266 for _, a in ipairs(device:ipaddrs()) do
267 data.ipaddrs[#data.ipaddrs+1] = {
268 addr = a:host():string(),
269 netmask = a:mask():string(),
273 for _, a in ipairs(device:ip6addrs()) do
274 data.ip6addrs[#data.ip6addrs+1] = {
275 addr = a:host():string(),
276 netmask = a:mask():string(),
281 for _, device in ipairs(net:get_interfaces() or {}) do
282 data.subdevices[#data.subdevices+1] = {
283 name = device:shortname(),
284 type = device:type(),
285 ifname = device:name(),
286 macaddr = device:mac(),
287 macaddr = device:mac(),
288 is_up = device:is_up(),
289 rx_bytes = device:rx_bytes(),
290 tx_bytes = device:tx_bytes(),
291 rx_packets = device:rx_packets(),
292 tx_packets = device:tx_packets(),
307 luci.http.prepare_content("application/json")
308 luci.http.write_json(rv)
312 luci.http.status(404, "No such device")
315 function iface_reconnect()
316 local path = luci.dispatcher.context.requestpath
317 local iface = path[#path]
318 local netmd = require "luci.model.network".init()
320 local net = netmd:get_network(iface)
322 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
323 luci.http.status(200, "Reconnected")
327 luci.http.status(404, "No such interface")
330 function iface_shutdown()
331 local path = luci.dispatcher.context.requestpath
332 local iface = path[#path]
333 local netmd = require "luci.model.network".init()
335 local net = netmd:get_network(iface)
337 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
338 luci.http.status(200, "Shutdown")
342 luci.http.status(404, "No such interface")
345 function iface_delete()
346 local path = luci.dispatcher.context.requestpath
347 local iface = path[#path]
348 local netmd = require "luci.model.network".init()
350 local net = netmd:del_network(iface)
352 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
353 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
354 netmd:commit("network")
355 netmd:commit("wireless")
359 luci.http.status(404, "No such interface")
362 function wifi_status()
363 local path = luci.dispatcher.context.requestpath
364 local s = require "luci.tools.status"
368 for dev in path[#path]:gmatch("[%w%.%-]+") do
369 rv[#rv+1] = s.wifi_network(dev)
373 luci.http.prepare_content("application/json")
374 luci.http.write_json(rv)
378 luci.http.status(404, "No such device")
381 function wifi_reconnect()
382 local path = luci.dispatcher.context.requestpath
383 local mode = path[#path-1]
384 local wnet = path[#path]
385 local netmd = require "luci.model.network".init()
387 local net = netmd:get_wifinet(wnet)
388 local dev = net:get_device()
390 luci.sys.call("env -i /sbin/wifi down >/dev/null 2>/dev/null")
392 dev:set("disabled", nil)
393 net:set("disabled", (mode == "wireless_shutdown") and 1 or nil)
394 netmd:commit("wireless")
396 luci.sys.call("env -i /sbin/wifi up >/dev/null 2>/dev/null")
397 luci.http.status(200, (mode == "wireless_shutdown") and "Shutdown" or "Reconnected")
402 luci.http.status(404, "No such radio")
405 function lease_status()
406 local s = require "luci.tools.status"
408 luci.http.prepare_content("application/json")
409 luci.http.write_json(s.dhcp_leases())
412 function diag_command(cmd)
413 local path = luci.dispatcher.context.requestpath
414 local addr = path[#path]
416 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
417 luci.http.prepare_content("text/plain")
419 local util = io.popen(cmd % addr)
422 local ln = util:read("*l")
423 if not ln then break end
425 luci.http.write("\n")
434 luci.http.status(500, "Bad address")
438 diag_command("ping -c 5 -W 1 %q 2>&1")
441 function diag_traceroute()
442 diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
445 function diag_nslookup()
446 diag_command("nslookup %q 2>&1")