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()
217 uptime = net:uptime(),
218 gwaddr = net:gwaddr(),
219 dnsaddrs = net:dnsaddrs()
221 for _, info in ipairs(nixio.getifaddrs()) do
222 local name = info.name:match("[^:]+")
224 if info.family == "packet" then
225 data.flags = info.flags
226 data.stats = info.data
227 data.macaddr = info.addr
229 elseif info.family == "inet" then
230 data.ipaddrs = data.ipaddrs or { }
231 data.ipaddrs[#data.ipaddrs+1] = {
233 broadaddr = info.broadaddr,
234 dstaddr = info.dstaddr,
235 netmask = info.netmask,
238 elseif info.family == "inet6" then
239 data.ip6addrs = data.ip6addrs or { }
240 data.ip6addrs[#data.ip6addrs+1] = {
242 netmask = info.netmask,
256 luci.http.prepare_content("application/json")
257 luci.http.write_json(rv)
261 luci.http.status(404, "No such device")
264 function iface_reconnect()
265 local path = luci.dispatcher.context.requestpath
266 local iface = path[#path]
267 local netmd = require "luci.model.network".init()
269 local net = netmd:get_network(iface)
272 for _, ifn in ipairs(net:get_interfaces()) do
273 local wnet = ifn:get_wifinet()
275 local wdev = wnet:get_device()
278 "env -i /sbin/wifi up %q >/dev/null 2>/dev/null"
282 luci.http.status(200, "Reconnected")
288 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
289 luci.http.status(200, "Reconnected")
293 luci.http.status(404, "No such interface")
296 function iface_shutdown()
297 local path = luci.dispatcher.context.requestpath
298 local iface = path[#path]
299 local netmd = require "luci.model.network".init()
301 local net = netmd:get_network(iface)
303 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
304 luci.http.status(200, "Shutdown")
308 luci.http.status(404, "No such interface")
311 function iface_delete()
312 local path = luci.dispatcher.context.requestpath
313 local iface = path[#path]
314 local netmd = require "luci.model.network".init()
316 local net = netmd:del_network(iface)
318 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
319 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
320 netmd:commit("network")
321 netmd:commit("wireless")
325 luci.http.status(404, "No such interface")
328 function wifi_status()
329 local netm = require "luci.model.network".init()
330 local path = luci.dispatcher.context.requestpath
334 for dev in path[#path]:gmatch("[%w%.%-]+") do
335 local j = { id = dev }
336 local wn = netm:get_wifinet(dev)
337 local iw = wn and wn.iwinfo
341 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
342 "quality", "quality_max", "bssid", "country",
343 "encryption", "ifname", "assoclist"
349 j.mode = wn and wn:active_mode() or "?"
350 j.ssid = wn and wn:active_ssid() or "?"
356 luci.http.prepare_content("application/json")
357 luci.http.write_json(rv)
361 luci.http.status(404, "No such device")
364 function lease_status()
365 local s = require "luci.tools.status"
367 luci.http.prepare_content("application/json")
368 luci.http.write_json(s.dhcp_leases())
371 function diag_command(cmd)
372 local path = luci.dispatcher.context.requestpath
373 local addr = path[#path]
375 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
376 luci.http.prepare_content("text/plain")
378 local util = io.popen(cmd % addr)
381 local ln = util:read("*l")
382 if not ln then break end
384 luci.http.write("\n")
393 luci.http.status(500, "Bad address")
397 diag_command("ping -c 5 -W 1 %q 2>&1")
400 function diag_traceroute()
401 diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
404 function diag_nslookup()
405 diag_command("nslookup %q 2>&1")