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)
20 local uci = require("luci.model.uci").cursor()
21 local net = require "luci.model.network".init(uci)
22 local i18n = luci.i18n.translate
23 local has_wifi = nixio.fs.stat("/etc/config/wireless")
24 local has_switch = false
26 uci:foreach("network", "switch",
35 page = node("admin", "network")
36 page.target = alias("admin", "network", "network")
37 page.title = i18n("Network")
42 page = node("admin", "network", "vlan")
43 page.target = cbi("admin_network/vlan")
44 page.title = i18n("Switch")
48 if has_wifi and has_wifi.size > 0 then
49 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("Wifi"), 15)
53 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
56 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
59 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
62 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
66 for _, wdev in ipairs(net:get_wifidevs()) do
68 for _, wnet in ipairs(wdev:get_wifinets()) do
70 {"admin", "network", "wireless", wnet:id()},
71 alias("admin", "network", "wireless"),
72 wdev:name() .. ": " .. wnet:shortname()
78 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
82 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
85 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
88 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
91 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
94 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
97 uci:foreach("network", "interface",
99 local ifc = section[".name"]
100 if ifc ~= "loopback" then
101 entry({"admin", "network", "network", ifc},
108 if nixio.fs.access("/etc/config/dhcp") then
109 page = node("admin", "network", "dhcpleases")
110 page.target = cbi("admin_network/dhcpleases")
111 page.title = i18n("DHCP Leases")
114 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
117 page = node("admin", "network", "hosts")
118 page.target = cbi("admin_network/hosts")
119 page.title = i18n("Hostnames")
123 page = node("admin", "network", "routes")
124 page.target = cbi("admin_network/routes")
125 page.title = i18n("Static Routes")
128 page = node("admin", "network", "diagnostics")
129 page.target = template("admin_network/diagnostics")
130 page.title = i18n("Diagnostics")
133 page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
136 page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
139 page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
144 local function param(x)
145 return luci.http.formvalue(x)
148 local function ptable(x)
150 return x and (type(x) ~= "table" and { x } or x) or {}
153 local dev = param("device")
154 local ssid = param("join")
157 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
160 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
162 local cbi = require "luci.cbi"
163 local tpl = require "luci.template"
164 local map = luci.cbi.load("admin_network/wifi_add")[1]
166 if map:parse() ~= cbi.FORM_DONE then
173 luci.template.render("admin_network/wifi_join")
178 local dev = luci.http.formvalue("device")
179 local ntm = require "luci.model.network".init()
181 dev = dev and ntm:get_wifidev(dev)
184 local net = dev:add_wifinet({
191 luci.http.redirect(net:adminlink())
195 function wifi_delete(network)
196 local ntm = require "luci.model.network".init()
198 ntm:del_wifinet(network)
201 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
204 function iface_status()
205 local path = luci.dispatcher.context.requestpath
206 local netm = require "luci.model.network".init()
210 for iface in path[#path]:gmatch("[%w%.%-]+") do
211 local net = netm:get_network(iface)
214 local dev = net:ifname()
218 uptime = net:uptime(),
219 gwaddr = net:gwaddr(),
220 dnsaddrs = net:dnsaddrs()
222 for _, info in ipairs(nixio.getifaddrs()) do
223 local name = info.name:match("[^:]+")
225 if info.family == "packet" then
226 data.flags = info.flags
227 data.stats = info.data
228 data.macaddr = info.addr
230 elseif info.family == "inet" then
231 data.ipaddrs = data.ipaddrs or { }
232 data.ipaddrs[#data.ipaddrs+1] = {
234 broadaddr = info.broadaddr,
235 dstaddr = info.dstaddr,
236 netmask = info.netmask,
239 elseif info.family == "inet6" then
240 data.ip6addrs = data.ip6addrs or { }
241 data.ip6addrs[#data.ip6addrs+1] = {
243 netmask = info.netmask,
257 luci.http.prepare_content("application/json")
258 luci.http.write_json(rv)
262 luci.http.status(404, "No such device")
265 function iface_reconnect()
266 local path = luci.dispatcher.context.requestpath
267 local iface = path[#path]
268 local netmd = require "luci.model.network".init()
270 local net = netmd:get_network(iface)
273 for _, ifn in ipairs(net:get_interfaces()) do
274 local wnet = ifn:get_wifinet()
276 local wdev = wnet:get_device()
279 "env -i /sbin/wifi up %q >/dev/null 2>/dev/null"
283 luci.http.status(200, "Reconnected")
289 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
290 luci.http.status(200, "Reconnected")
294 luci.http.status(404, "No such interface")
297 function iface_shutdown()
298 local path = luci.dispatcher.context.requestpath
299 local iface = path[#path]
300 local netmd = require "luci.model.network".init()
302 local net = netmd:get_network(iface)
304 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
305 luci.http.status(200, "Shutdown")
309 luci.http.status(404, "No such interface")
312 function iface_delete()
313 local path = luci.dispatcher.context.requestpath
314 local iface = path[#path]
315 local netmd = require "luci.model.network".init()
317 local net = netmd:del_network(iface)
319 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
320 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
321 netmd:commit("network")
322 netmd:commit("wireless")
326 luci.http.status(404, "No such interface")
329 function wifi_status()
330 local path = luci.dispatcher.context.requestpath
331 local s = require "luci.tools.status"
335 for dev in path[#path]:gmatch("[%w%.%-]+") do
336 rv[#rv+1] = s.wifi_network(dev)
340 luci.http.prepare_content("application/json")
341 luci.http.write_json(rv)
345 luci.http.status(404, "No such device")
348 function lease_status()
349 local s = require "luci.tools.status"
351 luci.http.prepare_content("application/json")
352 luci.http.write_json(s.dhcp_leases())
355 function diag_command(cmd)
356 local path = luci.dispatcher.context.requestpath
357 local addr = path[#path]
359 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
360 luci.http.prepare_content("text/plain")
362 local util = io.popen(cmd % addr)
365 local ln = util:read("*l")
366 if not ln then break end
368 luci.http.write("\n")
377 luci.http.status(500, "Bad address")
381 diag_command("ping -c 5 -W 1 %q 2>&1")
384 function diag_traceroute()
385 diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
388 function diag_nslookup()
389 diag_command("nslookup %q 2>&1")