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()
22 page = node("admin", "network")
23 page.target = firstchild()
24 page.title = _("Network")
29 local has_switch = false
31 uci:foreach("network", "switch",
38 page = node("admin", "network", "vlan")
39 page.target = cbi("admin_network/vlan")
40 page.title = _("Switch")
45 local has_wifi = false
47 uci:foreach("wireless", "wifi-device",
54 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil)
57 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil)
60 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil)
63 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil)
66 page = entry({"admin", "network", "wireless_reconnect"}, call("wifi_reconnect"), nil)
69 page = entry({"admin", "network", "wireless_shutdown"}, call("wifi_reconnect"), nil)
72 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wifi"), 15)
78 local net = require "luci.model.network".init(uci)
79 for _, wdev in ipairs(net:get_wifidevs()) do
81 for _, wnet in ipairs(wdev:get_wifinets()) do
83 {"admin", "network", "wireless", wnet:id()},
84 alias("admin", "network", "wireless"),
85 wdev:name() .. ": " .. wnet:shortname()
93 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
96 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
99 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
102 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
105 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
108 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)
113 uci:foreach("network", "interface",
115 local ifc = section[".name"]
116 if ifc ~= "loopback" then
117 entry({"admin", "network", "network", ifc},
124 if nixio.fs.access("/etc/config/dhcp") then
125 page = node("admin", "network", "dhcp")
126 page.target = cbi("admin_network/dhcp")
127 page.title = _("DHCP and DNS")
130 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
133 page = node("admin", "network", "hosts")
134 page.target = cbi("admin_network/hosts")
135 page.title = _("Hostnames")
139 page = node("admin", "network", "routes")
140 page.target = cbi("admin_network/routes")
141 page.title = _("Static Routes")
144 page = node("admin", "network", "diagnostics")
145 page.target = template("admin_network/diagnostics")
146 page.title = _("Diagnostics")
149 page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
152 page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
155 page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
161 local function param(x)
162 return luci.http.formvalue(x)
165 local function ptable(x)
167 return x and (type(x) ~= "table" and { x } or x) or {}
170 local dev = param("device")
171 local ssid = param("join")
174 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
177 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
179 local cbi = require "luci.cbi"
180 local tpl = require "luci.template"
181 local map = luci.cbi.load("admin_network/wifi_add")[1]
183 if map:parse() ~= cbi.FORM_DONE then
190 luci.template.render("admin_network/wifi_join")
195 local dev = luci.http.formvalue("device")
196 local ntm = require "luci.model.network".init()
198 dev = dev and ntm:get_wifidev(dev)
201 local net = dev:add_wifinet({
208 luci.http.redirect(net:adminlink())
212 function wifi_delete(network)
213 local ntm = require "luci.model.network".init()
214 local wnet = ntm:get_wifinet(network)
216 local dev = wnet:get_device()
217 local net = wnet:get_network()
219 luci.sys.call("env -i /sbin/wifi down %q >/dev/null" % dev:name())
220 ntm:del_wifinet(network)
221 ntm:commit("wireless")
222 if net and net:is_empty() then
223 ntm:del_network(net:name())
224 ntm:commit("network")
226 luci.sys.call("env -i /sbin/wifi up %q >/dev/null" % dev:name())
230 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
233 function iface_status()
234 local path = luci.dispatcher.context.requestpath
235 local netm = require "luci.model.network".init()
239 for iface in path[#path]:gmatch("[%w%.%-_]+") do
240 local net = netm:get_network(iface)
241 local device = net and net:get_interface()
246 uptime = net:uptime(),
247 gwaddr = net:gwaddr(),
248 dnsaddrs = net:dnsaddrs(),
249 name = device:shortname(),
250 type = device:type(),
251 ifname = device:name(),
252 macaddr = device:mac(),
253 is_up = device:is_up(),
254 rx_bytes = device:rx_bytes(),
255 tx_bytes = device:tx_bytes(),
256 rx_packets = device:rx_packets(),
257 tx_packets = device:tx_packets(),
265 for _, a in ipairs(device:ipaddrs()) do
266 data.ipaddrs[#data.ipaddrs+1] = {
267 addr = a:host():string(),
268 netmask = a:mask():string(),
272 for _, a in ipairs(device:ip6addrs()) do
273 if not a:is6linklocal() then
274 data.ip6addrs[#data.ip6addrs+1] = {
275 addr = a:host():string(),
276 netmask = a:mask():string(),
282 for _, device in ipairs(net:get_interfaces() or {}) do
283 data.subdevices[#data.subdevices+1] = {
284 name = device:shortname(),
285 type = device:type(),
286 ifname = device:name(),
287 macaddr = device:mac(),
288 macaddr = device:mac(),
289 is_up = device:is_up(),
290 rx_bytes = device:rx_bytes(),
291 tx_bytes = device:tx_bytes(),
292 rx_packets = device:rx_packets(),
293 tx_packets = device:tx_packets(),
308 luci.http.prepare_content("application/json")
309 luci.http.write_json(rv)
313 luci.http.status(404, "No such device")
316 function iface_reconnect()
317 local path = luci.dispatcher.context.requestpath
318 local iface = path[#path]
319 local netmd = require "luci.model.network".init()
321 local net = netmd:get_network(iface)
323 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
324 luci.http.status(200, "Reconnected")
328 luci.http.status(404, "No such interface")
331 function iface_shutdown()
332 local path = luci.dispatcher.context.requestpath
333 local iface = path[#path]
334 local netmd = require "luci.model.network".init()
336 local net = netmd:get_network(iface)
338 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
339 luci.http.status(200, "Shutdown")
343 luci.http.status(404, "No such interface")
346 function iface_delete()
347 local path = luci.dispatcher.context.requestpath
348 local iface = path[#path]
349 local netmd = require "luci.model.network".init()
351 local net = netmd:del_network(iface)
353 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
354 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
355 netmd:commit("network")
356 netmd:commit("wireless")
360 luci.http.status(404, "No such interface")
363 function wifi_status()
364 local path = luci.dispatcher.context.requestpath
365 local s = require "luci.tools.status"
369 for dev in path[#path]:gmatch("[%w%.%-]+") do
370 rv[#rv+1] = s.wifi_network(dev)
374 luci.http.prepare_content("application/json")
375 luci.http.write_json(rv)
379 luci.http.status(404, "No such device")
382 function wifi_reconnect()
383 local path = luci.dispatcher.context.requestpath
384 local mode = path[#path-1]
385 local wnet = path[#path]
386 local netmd = require "luci.model.network".init()
388 local net = netmd:get_wifinet(wnet)
389 local dev = net:get_device()
391 luci.sys.call("env -i /sbin/wifi down >/dev/null 2>/dev/null")
393 dev:set("disabled", nil)
394 net:set("disabled", (mode == "wireless_shutdown") and 1 or nil)
395 netmd:commit("wireless")
397 luci.sys.call("env -i /sbin/wifi up >/dev/null 2>/dev/null")
398 luci.http.status(200, (mode == "wireless_shutdown") and "Shutdown" or "Reconnected")
403 luci.http.status(404, "No such radio")
406 function lease_status()
407 local s = require "luci.tools.status"
409 luci.http.prepare_content("application/json")
410 luci.http.write_json(s.dhcp_leases())
413 function diag_command(cmd)
414 local path = luci.dispatcher.context.requestpath
415 local addr = path[#path]
417 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
418 luci.http.prepare_content("text/plain")
420 local util = io.popen(cmd % addr)
423 local ln = util:read("*l")
424 if not ln then break end
426 luci.http.write("\n")
435 luci.http.status(500, "Bad address")
439 diag_command("ping -c 5 -W 1 %q 2>&1")
442 function diag_traceroute()
443 diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
446 function diag_nslookup()
447 diag_command("nslookup %q 2>&1")