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 net = require "luci.model.network".init(uci)
21 local i18n = luci.i18n.translate
22 local has_wifi = nixio.fs.stat("/etc/config/wireless")
23 local has_switch = false
25 uci:foreach("network", "switch",
34 page = node("admin", "network")
35 page.target = alias("admin", "network", "network")
36 page.title = i18n("Network")
41 page = node("admin", "network", "vlan")
42 page.target = cbi("admin_network/vlan")
43 page.title = i18n("Switch")
47 if has_wifi and has_wifi.size > 0 then
48 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("Wifi"), 15)
52 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
55 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
58 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
61 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
65 for _, wdev in ipairs(net:get_wifidevs()) do
67 for _, wnet in ipairs(wdev:get_wifinets()) do
69 {"admin", "network", "wireless", wnet.netid},
70 alias("admin", "network", "wireless"),
71 wdev:name() .. ": " .. wnet:shortname()
77 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
81 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
84 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
87 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
90 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
93 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
96 uci:foreach("network", "interface",
98 local ifc = section[".name"]
99 if ifc ~= "loopback" then
100 entry({"admin", "network", "network", ifc},
107 if nixio.fs.access("/etc/config/dhcp") then
108 page = node("admin", "network", "dhcpleases")
109 page.target = cbi("admin_network/dhcpleases")
110 page.title = i18n("DHCP Leases")
113 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
116 page = node("admin", "network", "hosts")
117 page.target = cbi("admin_network/hosts")
118 page.title = i18n("Hostnames")
122 page = node("admin", "network", "routes")
123 page.target = cbi("admin_network/routes")
124 page.title = i18n("Static Routes")
127 page = node("admin", "network", "diagnostics")
128 page.target = template("admin_network/diagnostics")
129 page.title = i18n("Diagnostics")
132 page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
135 page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
138 page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
143 local function param(x)
144 return luci.http.formvalue(x)
147 local function ptable(x)
149 return x and (type(x) ~= "table" and { x } or x) or {}
152 local dev = param("device")
153 local ssid = param("join")
156 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
159 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
161 local cbi = require "luci.cbi"
162 local tpl = require "luci.template"
163 local map = luci.cbi.load("admin_network/wifi_add")[1]
165 if map:parse() ~= cbi.FORM_DONE then
172 luci.template.render("admin_network/wifi_join")
177 local dev = luci.http.formvalue("device")
178 local ntm = require "luci.model.network".init()
180 dev = dev and ntm:get_wifidev(dev)
183 local net = dev:add_wifinet({
190 luci.http.redirect(net:adminlink())
194 function wifi_delete(network)
195 local ntm = require "luci.model.network".init()
197 ntm:del_wifinet(network)
200 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
203 function iface_status()
204 local path = luci.dispatcher.context.requestpath
205 local netm = require "luci.model.network".init()
209 for iface in path[#path]:gmatch("[%w%.%-]+") do
210 local net = netm:get_network(iface)
213 local dev = net:ifname()
214 local data = { id = iface, uptime = net:uptime() }
215 for _, info in ipairs(nixio.getifaddrs()) do
216 local name = info.name:match("[^:]+")
218 if info.family == "packet" then
219 data.flags = info.flags
220 data.stats = info.data
221 data.macaddr = info.addr
223 elseif info.family == "inet" then
224 data.ipaddrs = data.ipaddrs or { }
225 data.ipaddrs[#data.ipaddrs+1] = {
227 broadaddr = info.broadaddr,
228 dstaddr = info.dstaddr,
229 netmask = info.netmask,
232 elseif info.family == "inet6" then
233 data.ip6addrs = data.ip6addrs or { }
234 data.ip6addrs[#data.ip6addrs+1] = {
236 netmask = info.netmask,
250 luci.http.prepare_content("application/json")
251 luci.http.write_json(rv)
255 luci.http.status(404, "No such device")
258 function iface_reconnect()
259 local path = luci.dispatcher.context.requestpath
260 local iface = path[#path]
261 local netmd = require "luci.model.network".init()
263 local net = netmd:get_network(iface)
266 for _, ifn in ipairs(net:get_interfaces()) do
267 local wnet = ifn:get_wifinet()
269 local wdev = wnet:get_device()
272 "env -i /sbin/wifi up %q >/dev/null 2>/dev/null"
276 luci.http.status(200, "Reconnected")
282 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
283 luci.http.status(200, "Reconnected")
287 luci.http.status(404, "No such interface")
290 function iface_shutdown()
291 local path = luci.dispatcher.context.requestpath
292 local iface = path[#path]
293 local netmd = require "luci.model.network".init()
295 local net = netmd:get_network(iface)
297 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
298 luci.http.status(200, "Shutdown")
302 luci.http.status(404, "No such interface")
305 function iface_delete()
306 local path = luci.dispatcher.context.requestpath
307 local iface = path[#path]
308 local netmd = require "luci.model.network".init()
310 local net = netmd:del_network(iface)
312 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
313 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
314 netmd:commit("network")
315 netmd:commit("wireless")
319 luci.http.status(404, "No such interface")
322 function wifi_status()
323 local netm = require "luci.model.network".init()
324 local path = luci.dispatcher.context.requestpath
325 local arp = luci.sys.net.arptable()
329 for dev in path[#path]:gmatch("[%w%.%-]+") do
330 local j = { id = dev }
331 local wn = netm:get_wifinet(dev)
332 local iw = wn and wn.iwinfo
336 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
337 "quality", "quality_max", "bssid", "country",
338 "encryption", "ifname", "assoclist"
344 j.mode = wn and wn:active_mode() or "?"
345 j.ssid = wn and wn:active_ssid() or "?"
351 luci.http.prepare_content("application/json")
352 luci.http.write_json(rv)
356 luci.http.status(404, "No such device")
359 function lease_status()
361 local leasefile = "/var/dhcp.leases"
363 local uci = require "luci.model.uci".cursor()
364 local nfs = require "nixio.fs"
366 uci:foreach("dhcp", "dnsmasq",
368 if s.leasefile and nfs.access(s.leasefile) then
369 leasefile = s.leasefile
374 local fd = io.open(leasefile, "r")
377 local ln = fd:read("*l")
381 local ts, mac, ip, name = ln:match("^(%d+) (%S+) (%S+) (%S+)")
382 if ts and mac and ip and name then
384 expires = os.difftime(tonumber(ts) or 0, os.time()),
387 hostname = (name ~= "*") and name
395 luci.http.prepare_content("application/json")
396 luci.http.write_json(rv)
399 function diag_command(cmd)
400 local path = luci.dispatcher.context.requestpath
401 local addr = path[#path]
403 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
404 luci.http.prepare_content("text/plain")
406 local util = io.popen(cmd % addr)
409 local ln = util:read("*l")
410 if not ln then break end
412 luci.http.write("\n")
421 luci.http.status(500, "Bad address")
425 diag_command("ping -c 5 -W 1 %q 2>&1")
428 function diag_traceroute()
429 diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
432 function diag_nslookup()
433 diag_command("nslookup %q 2>&1")