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()
20 local net = require "luci.model.network".init(uci)
21 local has_wifi = nixio.fs.stat("/etc/config/wireless")
22 local has_switch = false
24 uci:foreach("network", "switch",
33 page = node("admin", "network")
34 page.target = alias("admin", "network", "network")
35 page.title = _("Network")
40 page = node("admin", "network", "vlan")
41 page.target = cbi("admin_network/vlan")
42 page.title = _("Switch")
46 if has_wifi and has_wifi.size > 0 then
47 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wifi"), 15)
51 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
54 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
57 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
60 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
64 for _, wdev in ipairs(net:get_wifidevs()) do
66 for _, wnet in ipairs(wdev:get_wifinets()) do
68 {"admin", "network", "wireless", wnet:id()},
69 alias("admin", "network", "wireless"),
70 wdev:name() .. ": " .. wnet:shortname()
76 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)
80 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
83 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
86 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
89 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
92 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
95 uci:foreach("network", "interface",
97 local ifc = section[".name"]
98 if ifc ~= "loopback" then
99 entry({"admin", "network", "network", ifc},
106 if nixio.fs.access("/etc/config/dhcp") then
107 page = node("admin", "network", "dhcp")
108 page.target = cbi("admin_network/dhcp")
109 page.title = _("DHCP and DNS")
112 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
115 page = node("admin", "network", "hosts")
116 page.target = cbi("admin_network/hosts")
117 page.title = _("Hostnames")
121 page = node("admin", "network", "routes")
122 page.target = cbi("admin_network/routes")
123 page.title = _("Static Routes")
126 page = node("admin", "network", "diagnostics")
127 page.target = template("admin_network/diagnostics")
128 page.title = _("Diagnostics")
131 page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
134 page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
137 page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
142 local function param(x)
143 return luci.http.formvalue(x)
146 local function ptable(x)
148 return x and (type(x) ~= "table" and { x } or x) or {}
151 local dev = param("device")
152 local ssid = param("join")
155 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
158 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
160 local cbi = require "luci.cbi"
161 local tpl = require "luci.template"
162 local map = luci.cbi.load("admin_network/wifi_add")[1]
164 if map:parse() ~= cbi.FORM_DONE then
171 luci.template.render("admin_network/wifi_join")
176 local dev = luci.http.formvalue("device")
177 local ntm = require "luci.model.network".init()
179 dev = dev and ntm:get_wifidev(dev)
182 local net = dev:add_wifinet({
189 luci.http.redirect(net:adminlink())
193 function wifi_delete(network)
194 local ntm = require "luci.model.network".init()
196 ntm:del_wifinet(network)
199 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
202 function iface_status()
203 local path = luci.dispatcher.context.requestpath
204 local netm = require "luci.model.network".init()
208 for iface in path[#path]:gmatch("[%w%.%-_]+") do
209 local net = netm:get_network(iface)
210 local device = net and net:get_interface()
212 local device = net:get_interface()
216 uptime = net:uptime(),
217 gwaddr = net:gwaddr(),
218 dnsaddrs = net:dnsaddrs(),
219 name = device:shortname(),
220 type = device:type(),
221 ifname = device:name(),
222 macaddr = device:mac(),
223 is_up = device:is_up(),
224 rx_bytes = device:rx_bytes(),
225 tx_bytes = device:tx_bytes(),
226 rx_packets = device:rx_packets(),
227 tx_packets = device:tx_packets(),
235 for _, a in ipairs(device:ipaddrs()) do
236 data.ipaddrs[#data.ipaddrs+1] = {
237 addr = a:host():string(),
238 netmask = a:mask():string(),
242 for _, a in ipairs(device:ip6addrs()) do
243 data.ip6addrs[#data.ip6addrs+1] = {
244 addr = a:host():string(),
245 netmask = a:mask():string(),
250 for _, device in ipairs(net:get_interfaces() or {}) do
251 data.subdevices[#data.subdevices+1] = {
252 name = device:shortname(),
253 type = device:type(),
254 ifname = device:name(),
255 macaddr = device:mac(),
256 macaddr = device:mac(),
257 is_up = device:is_up(),
258 rx_bytes = device:rx_bytes(),
259 tx_bytes = device:tx_bytes(),
260 rx_packets = device:rx_packets(),
261 tx_packets = device:tx_packets(),
270 luci.http.prepare_content("application/json")
271 luci.http.write_json(rv)
275 luci.http.status(404, "No such device")
278 function iface_reconnect()
279 local path = luci.dispatcher.context.requestpath
280 local iface = path[#path]
281 local netmd = require "luci.model.network".init()
283 local net = netmd:get_network(iface)
286 for _, ifn in ipairs(net:get_interfaces()) do
287 local wnet = ifn:get_wifinet()
289 local wdev = wnet:get_device()
292 "env -i /sbin/wifi up %q >/dev/null 2>/dev/null"
296 luci.http.status(200, "Reconnected")
302 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
303 luci.http.status(200, "Reconnected")
307 luci.http.status(404, "No such interface")
310 function iface_shutdown()
311 local path = luci.dispatcher.context.requestpath
312 local iface = path[#path]
313 local netmd = require "luci.model.network".init()
315 local net = netmd:get_network(iface)
317 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
318 luci.http.status(200, "Shutdown")
322 luci.http.status(404, "No such interface")
325 function iface_delete()
326 local path = luci.dispatcher.context.requestpath
327 local iface = path[#path]
328 local netmd = require "luci.model.network".init()
330 local net = netmd:del_network(iface)
332 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
333 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
334 netmd:commit("network")
335 netmd:commit("wireless")
339 luci.http.status(404, "No such interface")
342 function wifi_status()
343 local path = luci.dispatcher.context.requestpath
344 local s = require "luci.tools.status"
348 for dev in path[#path]:gmatch("[%w%.%-]+") do
349 rv[#rv+1] = s.wifi_network(dev)
353 luci.http.prepare_content("application/json")
354 luci.http.write_json(rv)
358 luci.http.status(404, "No such device")
361 function wifi_reconnect()
362 local path = luci.dispatcher.context.requestpath
363 local mode = path[#path-1]
364 local wnet = path[#path]
365 local netmd = require "luci.model.network".init()
367 local net = netmd:get_wifinet(wnet)
368 local dev = net:get_device()
370 dev:set("disabled", nil)
371 net:set("disabled", (mode == "wireless_shutdown") and 1 or nil)
372 netmd:commit("wireless")
374 luci.sys.call("(env -i /sbin/wifi down; env -i /sbin/wifi up) >/dev/null 2>/dev/null")
375 luci.http.status(200, (mode == "wireless_shutdown") and "Shutdown" or "Reconnected")
380 luci.http.status(404, "No such radio")
383 function lease_status()
384 local s = require "luci.tools.status"
386 luci.http.prepare_content("application/json")
387 luci.http.write_json(s.dhcp_leases())
390 function diag_command(cmd)
391 local path = luci.dispatcher.context.requestpath
392 local addr = path[#path]
394 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
395 luci.http.prepare_content("text/plain")
397 local util = io.popen(cmd % addr)
400 local ln = util:read("*l")
401 if not ln then break end
403 luci.http.write("\n")
412 luci.http.status(500, "Bad address")
416 diag_command("ping -c 5 -W 1 %q 2>&1")
419 function diag_traceroute()
420 diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
423 function diag_nslookup()
424 diag_command("nslookup %q 2>&1")