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 i18n = luci.i18n.translate
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 = i18n("Network")
40 page = node("admin", "network", "vlan")
41 page.target = cbi("admin_network/vlan")
42 page.title = i18n("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")), i18n("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 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
68 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
71 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
74 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
77 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
80 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
83 uci:foreach("network", "interface",
85 local ifc = section[".name"]
86 if ifc ~= "loopback" then
87 entry({"admin", "network", "network", ifc},
94 if nixio.fs.access("/etc/config/dhcp") then
95 page = node("admin", "network", "dhcpleases")
96 page.target = cbi("admin_network/dhcpleases")
97 page.title = i18n("DHCP Leases")
100 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
103 page = node("admin", "network", "hosts")
104 page.target = cbi("admin_network/hosts")
105 page.title = i18n("Hostnames")
109 page = node("admin", "network", "routes")
110 page.target = cbi("admin_network/routes")
111 page.title = i18n("Static Routes")
117 local function param(x)
118 return luci.http.formvalue(x)
121 local function ptable(x)
123 return x and (type(x) ~= "table" and { x } or x) or {}
126 local dev = param("device")
127 local ssid = param("join")
130 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
133 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
135 local cbi = require "luci.cbi"
136 local tpl = require "luci.template"
137 local map = luci.cbi.load("admin_network/wifi_add")[1]
139 if map:parse() ~= cbi.FORM_DONE then
146 luci.template.render("admin_network/wifi_join")
151 local dev = luci.http.formvalue("device")
152 local ntm = require "luci.model.network".init()
154 dev = dev and ntm:get_wifidev(dev)
157 local net = dev:add_wifinet({
164 luci.http.redirect(net:adminlink())
168 function wifi_delete(network)
169 local ntm = require "luci.model.network".init()
171 ntm:del_wifinet(network)
174 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
177 function iface_status()
178 local path = luci.dispatcher.context.requestpath
179 local x = luci.model.uci.cursor_state()
183 for iface in path[#path]:gmatch("[%w%.%-]+") do
185 if x:get("network", iface, "type") == "bridge" then
188 dev = x:get("network", iface, "device") or ""
191 if #dev == 0 or dev:match("^%d") or dev:match("%W") then
192 dev = x:get("network", iface, "ifname") or ""
193 dev = dev:match("%S+")
197 local data = { id = iface }
198 for _, info in ipairs(nixio.getifaddrs()) do
199 local name = info.name:match("[^:]+")
201 if info.family == "packet" then
202 data.flags = info.flags
203 data.stats = info.data
204 data.macaddr = info.addr
206 elseif info.family == "inet" then
207 data.ipaddrs = data.ipaddrs or { }
208 data.ipaddrs[#data.ipaddrs+1] = {
210 broadaddr = info.broadaddr,
211 dstaddr = info.dstaddr,
212 netmask = info.netmask,
215 elseif info.family == "inet6" then
216 data.ip6addrs = data.ip6addrs or { }
217 data.ip6addrs[#data.ip6addrs+1] = {
219 netmask = info.netmask,
232 luci.http.prepare_content("application/json")
233 luci.http.write_json(rv)
237 luci.http.status(404, "No such device")
240 function iface_reconnect()
241 local path = luci.dispatcher.context.requestpath
242 local iface = path[#path]
243 local netmd = require "luci.model.network".init()
245 local net = netmd:get_network(iface)
248 for _, ifn in ipairs(net:get_interfaces()) do
249 local wnet = ifn:get_wifinet()
251 local wdev = wnet:get_device()
254 "env -i /sbin/wifi up %q >/dev/null 2>/dev/null"
258 luci.http.status(200, "Reconnected")
264 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
265 luci.http.status(200, "Reconnected")
269 luci.http.status(404, "No such interface")
272 function iface_shutdown()
273 local path = luci.dispatcher.context.requestpath
274 local iface = path[#path]
275 local netmd = require "luci.model.network".init()
277 local net = netmd:get_network(iface)
279 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
280 luci.http.status(200, "Shutdown")
284 luci.http.status(404, "No such interface")
287 function iface_delete()
288 local path = luci.dispatcher.context.requestpath
289 local iface = path[#path]
290 local netmd = require "luci.model.network".init()
292 local net = netmd:del_network(iface)
294 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
295 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
296 netmd:commit("network")
297 netmd:commit("wireless")
301 luci.http.status(404, "No such interface")
304 function wifi_status()
305 local path = luci.dispatcher.context.requestpath
306 local arp = luci.sys.net.arptable()
310 for dev in path[#path]:gmatch("[%w%.%-]+") do
311 local j = { id = dev }
312 local iw = luci.sys.wifi.getiwinfo(dev)
316 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
317 "quality", "quality_max", "mode", "ssid", "bssid", "country",
318 "encryption", "ifname", "assoclist"
327 luci.http.prepare_content("application/json")
328 luci.http.write_json(rv)
332 luci.http.status(404, "No such device")
335 function lease_status()
337 local leasefile = "/var/dhcp.leases"
339 local uci = require "luci.model.uci".cursor()
340 local nfs = require "nixio.fs"
342 uci:foreach("dhcp", "dnsmasq",
344 if s.leasefile and nfs.access(s.leasefile) then
345 leasefile = s.leasefile
350 local fd = io.open(leasefile, "r")
353 local ln = fd:read("*l")
357 local ts, mac, ip, name = ln:match("^(%d+) (%S+) (%S+) (%S+)")
358 if ts and mac and ip and name then
360 expires = os.difftime(tonumber(ts) or 0, os.time()),
363 hostname = (name ~= "*") and name
371 luci.http.prepare_content("application/json")
372 luci.http.write_json(rv)