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", "add"}, cbi("admin_network/iface_add"), nil)
71 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
74 uci:foreach("network", "interface",
76 local ifc = section[".name"]
77 if ifc ~= "loopback" then
78 entry({"admin", "network", "network", ifc},
85 if nixio.fs.access("/etc/config/dhcp") then
86 page = node("admin", "network", "dhcpleases")
87 page.target = cbi("admin_network/dhcpleases")
88 page.title = i18n("DHCP Leases")
91 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
95 page = node("admin", "network", "hosts")
96 page.target = cbi("admin_network/hosts")
97 page.title = i18n("Hostnames")
100 page = node("admin", "network", "routes")
101 page.target = cbi("admin_network/routes")
102 page.title = i18n("Static Routes")
108 local function param(x)
109 return luci.http.formvalue(x)
112 local function ptable(x)
114 return x and (type(x) ~= "table" and { x } or x) or {}
117 local dev = param("device")
118 local ssid = param("join")
121 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
124 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
126 local cbi = require "luci.cbi"
127 local tpl = require "luci.template"
128 local map = luci.cbi.load("admin_network/wifi_add")[1]
130 if map:parse() ~= cbi.FORM_DONE then
137 luci.template.render("admin_network/wifi_join")
142 local dev = luci.http.formvalue("device")
143 local ntm = require "luci.model.network".init()
145 dev = dev and ntm:get_wifidev(dev)
148 local net = dev:add_wifinet({
155 luci.http.redirect(net:adminlink())
159 function wifi_delete(network)
160 local ntm = require "luci.model.network".init()
162 ntm:del_wifinet(network)
165 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
168 function iface_status()
169 local path = luci.dispatcher.context.requestpath
170 local x = luci.model.uci.cursor_state()
174 for iface in path[#path]:gmatch("[%w%.%-]+") do
175 local dev = x:get("network", iface, "device") or ""
176 if #dev == 0 or dev:match("^%d") or dev:match("%W") then
177 dev = x:get("network", iface, "ifname") or ""
178 dev = dev:match("%S+")
183 for _, info in ipairs(nixio.getifaddrs()) do
184 local name = info.name:match("[^:]+")
186 if info.family == "packet" then
187 data.flags = info.flags
188 data.stats = info.data
189 data.macaddr = info.addr
191 elseif info.family == "inet" then
192 data.ipaddrs = data.ipaddrs or { }
193 data.ipaddrs[#data.ipaddrs+1] = {
195 broadaddr = info.broadaddr,
196 dstaddr = info.dstaddr,
197 netmask = info.netmask,
200 elseif info.family == "inet6" then
201 data.ip6addrs = data.ip6addrs or { }
202 data.ip6addrs[#data.ip6addrs+1] = {
204 netmask = info.netmask,
217 luci.http.prepare_content("application/json")
218 luci.http.write_json(rv)
222 luci.http.status(404, "No such device")
225 function wifi_status()
226 local path = luci.dispatcher.context.requestpath
230 for dev in path[#path]:gmatch("[%w%.%-]+") do
231 local iw = luci.sys.wifi.getiwinfo(dev)
234 local j = { id = dev }
236 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
237 "quality", "quality_max", "mode", "ssid", "bssid", "country",
238 "encryption", "ifname", "assoclist"
248 luci.http.prepare_content("application/json")
249 luci.http.write_json(rv)
253 luci.http.status(404, "No such device")
256 function lease_status()
258 local leasefile = "/var/dhcp.leases"
260 local uci = require "luci.model.uci".cursor()
261 local nfs = require "nixio.fs"
263 uci:foreach("dhcp", "dnsmasq",
265 if s.leasefile and nfs.access(s.leasefile) then
266 leasefile = s.leasefile
271 local fd = io.open(leasefile, "r")
274 local ln = fd:read("*l")
278 local ts, mac, ip, name = ln:match("^(%d+) (%S+) (%S+) (%S+)")
279 if ts and mac and ip and name then
281 expires = os.difftime(tonumber(ts) or 0, os.time()),
284 hostname = (name ~= "*") and name
292 luci.http.prepare_content("application/json")
293 luci.http.write_json(rv)