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 = firstchild()
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)
54 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil)
57 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil)
60 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil)
63 page = entry({"admin", "network", "wireless_reconnect"}, call("wifi_reconnect"), nil)
66 page = entry({"admin", "network", "wireless_shutdown"}, call("wifi_reconnect"), nil)
70 for _, wdev in ipairs(net:get_wifidevs()) do
72 for _, wnet in ipairs(wdev:get_wifinets()) do
74 {"admin", "network", "wireless", wnet:id()},
75 alias("admin", "network", "wireless"),
76 wdev:name() .. ": " .. wnet:shortname()
82 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)
86 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
89 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
92 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
95 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
98 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
101 uci:foreach("network", "interface",
103 local ifc = section[".name"]
104 if ifc ~= "loopback" then
105 entry({"admin", "network", "network", ifc},
112 if nixio.fs.access("/etc/config/dhcp") then
113 page = node("admin", "network", "dhcp")
114 page.target = cbi("admin_network/dhcp")
115 page.title = _("DHCP and DNS")
118 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
121 page = node("admin", "network", "hosts")
122 page.target = cbi("admin_network/hosts")
123 page.title = _("Hostnames")
127 page = node("admin", "network", "routes")
128 page.target = cbi("admin_network/routes")
129 page.title = _("Static Routes")
132 page = node("admin", "network", "diagnostics")
133 page.target = template("admin_network/diagnostics")
134 page.title = _("Diagnostics")
137 page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
140 page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
143 page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
148 local function param(x)
149 return luci.http.formvalue(x)
152 local function ptable(x)
154 return x and (type(x) ~= "table" and { x } or x) or {}
157 local dev = param("device")
158 local ssid = param("join")
161 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
164 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
166 local cbi = require "luci.cbi"
167 local tpl = require "luci.template"
168 local map = luci.cbi.load("admin_network/wifi_add")[1]
170 if map:parse() ~= cbi.FORM_DONE then
177 luci.template.render("admin_network/wifi_join")
182 local dev = luci.http.formvalue("device")
183 local ntm = require "luci.model.network".init()
185 dev = dev and ntm:get_wifidev(dev)
188 local net = dev:add_wifinet({
195 luci.http.redirect(net:adminlink())
199 function wifi_delete(network)
200 local ntm = require "luci.model.network".init()
201 local net = ntm:get_wifinet(network)
204 local dev = net:get_device()
206 luci.sys.call("env -i /sbin/wifi down %q" % dev:name())
207 ntm:del_wifinet(network)
208 ntm:commit("wireless")
209 luci.sys.call("env -i /sbin/wifi up %q" % dev:name())
213 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
216 function iface_status()
217 local path = luci.dispatcher.context.requestpath
218 local netm = require "luci.model.network".init()
222 for iface in path[#path]:gmatch("[%w%.%-_]+") do
223 local net = netm:get_network(iface)
224 local device = net and net:get_interface()
226 local device = net:get_interface()
230 uptime = net:uptime(),
231 gwaddr = net:gwaddr(),
232 dnsaddrs = net:dnsaddrs(),
233 name = device:shortname(),
234 type = device:type(),
235 ifname = device:name(),
236 macaddr = device:mac(),
237 is_up = device:is_up(),
238 rx_bytes = device:rx_bytes(),
239 tx_bytes = device:tx_bytes(),
240 rx_packets = device:rx_packets(),
241 tx_packets = device:tx_packets(),
249 for _, a in ipairs(device:ipaddrs()) do
250 data.ipaddrs[#data.ipaddrs+1] = {
251 addr = a:host():string(),
252 netmask = a:mask():string(),
256 for _, a in ipairs(device:ip6addrs()) do
257 data.ip6addrs[#data.ip6addrs+1] = {
258 addr = a:host():string(),
259 netmask = a:mask():string(),
264 for _, device in ipairs(net:get_interfaces() or {}) do
265 data.subdevices[#data.subdevices+1] = {
266 name = device:shortname(),
267 type = device:type(),
268 ifname = device:name(),
269 macaddr = device:mac(),
270 macaddr = device:mac(),
271 is_up = device:is_up(),
272 rx_bytes = device:rx_bytes(),
273 tx_bytes = device:tx_bytes(),
274 rx_packets = device:rx_packets(),
275 tx_packets = device:tx_packets(),
290 luci.http.prepare_content("application/json")
291 luci.http.write_json(rv)
295 luci.http.status(404, "No such device")
298 function iface_reconnect()
299 local path = luci.dispatcher.context.requestpath
300 local iface = path[#path]
301 local netmd = require "luci.model.network".init()
303 local net = netmd:get_network(iface)
305 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
306 luci.http.status(200, "Reconnected")
310 luci.http.status(404, "No such interface")
313 function iface_shutdown()
314 local path = luci.dispatcher.context.requestpath
315 local iface = path[#path]
316 local netmd = require "luci.model.network".init()
318 local net = netmd:get_network(iface)
320 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
321 luci.http.status(200, "Shutdown")
325 luci.http.status(404, "No such interface")
328 function iface_delete()
329 local path = luci.dispatcher.context.requestpath
330 local iface = path[#path]
331 local netmd = require "luci.model.network".init()
333 local net = netmd:del_network(iface)
335 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
336 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
337 netmd:commit("network")
338 netmd:commit("wireless")
342 luci.http.status(404, "No such interface")
345 function wifi_status()
346 local path = luci.dispatcher.context.requestpath
347 local s = require "luci.tools.status"
351 for dev in path[#path]:gmatch("[%w%.%-]+") do
352 rv[#rv+1] = s.wifi_network(dev)
356 luci.http.prepare_content("application/json")
357 luci.http.write_json(rv)
361 luci.http.status(404, "No such device")
364 function wifi_reconnect()
365 local path = luci.dispatcher.context.requestpath
366 local mode = path[#path-1]
367 local wnet = path[#path]
368 local netmd = require "luci.model.network".init()
370 local net = netmd:get_wifinet(wnet)
371 local dev = net:get_device()
373 dev:set("disabled", nil)
374 net:set("disabled", (mode == "wireless_shutdown") and 1 or nil)
375 netmd:commit("wireless")
377 luci.sys.call("(env -i /sbin/wifi down; env -i /sbin/wifi up) >/dev/null 2>/dev/null")
378 luci.http.status(200, (mode == "wireless_shutdown") and "Shutdown" or "Reconnected")
383 luci.http.status(404, "No such radio")
386 function lease_status()
387 local s = require "luci.tools.status"
389 luci.http.prepare_content("application/json")
390 luci.http.write_json(s.dhcp_leases())
393 function diag_command(cmd)
394 local path = luci.dispatcher.context.requestpath
395 local addr = path[#path]
397 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
398 luci.http.prepare_content("text/plain")
400 local util = io.popen(cmd % addr)
403 local ln = util:read("*l")
404 if not ln then break end
406 luci.http.write("\n")
415 luci.http.status(500, "Bad address")
419 diag_command("ping -c 5 -W 1 %q 2>&1")
422 function diag_traceroute()
423 diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
426 function diag_nslookup()
427 diag_command("nslookup %q 2>&1")