2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
15 module("luci.controller.admin.network", package.seeall)
19 local uci = require("luci.model.uci").cursor()
20 local i18n = luci.i18n.translate
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 = i18n("Network")
40 page = node("admin", "network", "vlan")
41 page.target = cbi("admin_network/vlan")
42 page.title = i18n("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")), i18n("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 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
68 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
71 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
74 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
77 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
80 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
83 uci:foreach("network", "interface",
85 local ifc = section[".name"]
86 if ifc ~= "loopback" then
87 entry({"admin", "network", "network", ifc},
94 if nixio.fs.access("/etc/config/dhcp") then
95 page = node("admin", "network", "dhcpleases")
96 page.target = cbi("admin_network/dhcpleases")
97 page.title = i18n("DHCP Leases")
100 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
103 page = node("admin", "network", "hosts")
104 page.target = cbi("admin_network/hosts")
105 page.title = i18n("Hostnames")
109 page = node("admin", "network", "routes")
110 page.target = cbi("admin_network/routes")
111 page.title = i18n("Static Routes")
114 page = node("admin", "network", "diagnostics")
115 page.target = template("admin_network/diagnostics")
116 page.title = i18n("Diagnostics")
119 page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
122 page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
125 page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
130 local function param(x)
131 return luci.http.formvalue(x)
134 local function ptable(x)
136 return x and (type(x) ~= "table" and { x } or x) or {}
139 local dev = param("device")
140 local ssid = param("join")
143 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
146 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
148 local cbi = require "luci.cbi"
149 local tpl = require "luci.template"
150 local map = luci.cbi.load("admin_network/wifi_add")[1]
152 if map:parse() ~= cbi.FORM_DONE then
159 luci.template.render("admin_network/wifi_join")
164 local dev = luci.http.formvalue("device")
165 local ntm = require "luci.model.network".init()
167 dev = dev and ntm:get_wifidev(dev)
170 local net = dev:add_wifinet({
177 luci.http.redirect(net:adminlink())
181 function wifi_delete(network)
182 local ntm = require "luci.model.network".init()
184 ntm:del_wifinet(network)
187 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
190 function iface_status()
191 local path = luci.dispatcher.context.requestpath
192 local netm = require "luci.model.network".init()
196 for iface in path[#path]:gmatch("[%w%.%-]+") do
197 local net = netm:get_network(iface)
200 local dev = net:ifname()
201 local data = { id = iface, uptime = net:uptime() }
202 for _, info in ipairs(nixio.getifaddrs()) do
203 local name = info.name:match("[^:]+")
205 if info.family == "packet" then
206 data.flags = info.flags
207 data.stats = info.data
208 data.macaddr = info.addr
210 elseif info.family == "inet" then
211 data.ipaddrs = data.ipaddrs or { }
212 data.ipaddrs[#data.ipaddrs+1] = {
214 broadaddr = info.broadaddr,
215 dstaddr = info.dstaddr,
216 netmask = info.netmask,
219 elseif info.family == "inet6" then
220 data.ip6addrs = data.ip6addrs or { }
221 data.ip6addrs[#data.ip6addrs+1] = {
223 netmask = info.netmask,
237 luci.http.prepare_content("application/json")
238 luci.http.write_json(rv)
242 luci.http.status(404, "No such device")
245 function iface_reconnect()
246 local path = luci.dispatcher.context.requestpath
247 local iface = path[#path]
248 local netmd = require "luci.model.network".init()
250 local net = netmd:get_network(iface)
253 for _, ifn in ipairs(net:get_interfaces()) do
254 local wnet = ifn:get_wifinet()
256 local wdev = wnet:get_device()
259 "env -i /sbin/wifi up %q >/dev/null 2>/dev/null"
263 luci.http.status(200, "Reconnected")
269 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
270 luci.http.status(200, "Reconnected")
274 luci.http.status(404, "No such interface")
277 function iface_shutdown()
278 local path = luci.dispatcher.context.requestpath
279 local iface = path[#path]
280 local netmd = require "luci.model.network".init()
282 local net = netmd:get_network(iface)
284 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
285 luci.http.status(200, "Shutdown")
289 luci.http.status(404, "No such interface")
292 function iface_delete()
293 local path = luci.dispatcher.context.requestpath
294 local iface = path[#path]
295 local netmd = require "luci.model.network".init()
297 local net = netmd:del_network(iface)
299 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
300 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
301 netmd:commit("network")
302 netmd:commit("wireless")
306 luci.http.status(404, "No such interface")
309 function wifi_status()
310 local path = luci.dispatcher.context.requestpath
311 local arp = luci.sys.net.arptable()
315 for dev in path[#path]:gmatch("[%w%.%-]+") do
316 local j = { id = dev }
317 local iw = luci.sys.wifi.getiwinfo(dev)
321 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
322 "quality", "quality_max", "mode", "ssid", "bssid", "country",
323 "encryption", "ifname", "assoclist"
332 luci.http.prepare_content("application/json")
333 luci.http.write_json(rv)
337 luci.http.status(404, "No such device")
340 function lease_status()
342 local leasefile = "/var/dhcp.leases"
344 local uci = require "luci.model.uci".cursor()
345 local nfs = require "nixio.fs"
347 uci:foreach("dhcp", "dnsmasq",
349 if s.leasefile and nfs.access(s.leasefile) then
350 leasefile = s.leasefile
355 local fd = io.open(leasefile, "r")
358 local ln = fd:read("*l")
362 local ts, mac, ip, name = ln:match("^(%d+) (%S+) (%S+) (%S+)")
363 if ts and mac and ip and name then
365 expires = os.difftime(tonumber(ts) or 0, os.time()),
368 hostname = (name ~= "*") and name
376 luci.http.prepare_content("application/json")
377 luci.http.write_json(rv)
380 function diag_command(cmd)
381 local path = luci.dispatcher.context.requestpath
382 local addr = path[#path]
384 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
385 luci.http.prepare_content("text/plain")
387 local util = io.popen(cmd % addr)
390 local ln = util:read("*l")
391 if not ln then break end
393 luci.http.write("\n")
402 luci.http.status(500, "Bad address")
406 diag_command("ping -c 5 -W 1 %q 2>&1")
409 function diag_traceroute()
410 diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
413 function diag_nslookup()
414 diag_command("nslookup %q 2>&1")