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)
212 local dev = net:ifname()
216 uptime = net:uptime(),
217 gwaddr = net:gwaddr(),
218 dnsaddrs = net:dnsaddrs()
220 for _, info in ipairs(nixio.getifaddrs()) do
221 local name = info.name:match("[^:]+")
223 if info.family == "packet" then
224 data.flags = info.flags
225 data.stats = info.data
226 data.macaddr = info.addr
228 elseif info.family == "inet" then
229 data.ipaddrs = data.ipaddrs or { }
230 data.ipaddrs[#data.ipaddrs+1] = {
232 broadaddr = info.broadaddr,
233 dstaddr = info.dstaddr,
234 netmask = info.netmask,
237 elseif info.family == "inet6" then
238 data.ip6addrs = data.ip6addrs or { }
239 data.ip6addrs[#data.ip6addrs+1] = {
241 netmask = info.netmask,
255 luci.http.prepare_content("application/json")
256 luci.http.write_json(rv)
260 luci.http.status(404, "No such device")
263 function iface_reconnect()
264 local path = luci.dispatcher.context.requestpath
265 local iface = path[#path]
266 local netmd = require "luci.model.network".init()
268 local net = netmd:get_network(iface)
271 for _, ifn in ipairs(net:get_interfaces()) do
272 local wnet = ifn:get_wifinet()
274 local wdev = wnet:get_device()
277 "env -i /sbin/wifi up %q >/dev/null 2>/dev/null"
281 luci.http.status(200, "Reconnected")
287 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
288 luci.http.status(200, "Reconnected")
292 luci.http.status(404, "No such interface")
295 function iface_shutdown()
296 local path = luci.dispatcher.context.requestpath
297 local iface = path[#path]
298 local netmd = require "luci.model.network".init()
300 local net = netmd:get_network(iface)
302 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
303 luci.http.status(200, "Shutdown")
307 luci.http.status(404, "No such interface")
310 function iface_delete()
311 local path = luci.dispatcher.context.requestpath
312 local iface = path[#path]
313 local netmd = require "luci.model.network".init()
315 local net = netmd:del_network(iface)
317 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
318 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
319 netmd:commit("network")
320 netmd:commit("wireless")
324 luci.http.status(404, "No such interface")
327 function wifi_status()
328 local path = luci.dispatcher.context.requestpath
329 local s = require "luci.tools.status"
333 for dev in path[#path]:gmatch("[%w%.%-]+") do
334 rv[#rv+1] = s.wifi_network(dev)
338 luci.http.prepare_content("application/json")
339 luci.http.write_json(rv)
343 luci.http.status(404, "No such device")
346 function lease_status()
347 local s = require "luci.tools.status"
349 luci.http.prepare_content("application/json")
350 luci.http.write_json(s.dhcp_leases())
353 function diag_command(cmd)
354 local path = luci.dispatcher.context.requestpath
355 local addr = path[#path]
357 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
358 luci.http.prepare_content("text/plain")
360 local util = io.popen(cmd % addr)
363 local ln = util:read("*l")
364 if not ln then break end
366 luci.http.write("\n")
375 luci.http.status(500, "Bad address")
379 diag_command("ping -c 5 -W 1 %q 2>&1")
382 function diag_traceroute()
383 diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
386 function diag_nslookup()
387 diag_command("nslookup %q 2>&1")