modules/admin-full: add dhcp and wifi status info
[project/luci.git] / modules / admin-full / luasrc / controller / admin / network.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
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
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 module("luci.controller.admin.network", package.seeall)
16
17 function index()
18         require("luci.i18n")
19         local uci = require("luci.model.uci").cursor()
20         local net = require "luci.model.network".init(uci)
21         local i18n = luci.i18n.translate
22         local has_wifi = nixio.fs.stat("/etc/config/wireless")
23         local has_switch = false
24
25         uci:foreach("network", "switch",
26                 function(s)
27                         has_switch = true
28                         return false
29                 end
30         )
31
32         local page
33
34         page = node("admin", "network")
35         page.target = alias("admin", "network", "network")
36         page.title  = i18n("Network")
37         page.order  = 50
38         page.index  = true
39
40         if has_switch then
41                 page  = node("admin", "network", "vlan")
42                 page.target = cbi("admin_network/vlan")
43                 page.title  = i18n("Switch")
44                 page.order  = 20
45         end
46
47         if has_wifi and has_wifi.size > 0 then
48                 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("Wifi"), 15)
49                 page.leaf = true
50                 page.subindex = true
51
52                 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
53                 page.leaf = true
54
55                 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
56                 page.leaf = true
57
58                 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
59                 page.leaf = true
60
61                 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
62                 page.leaf = true
63
64                 local wdev
65                 for _, wdev in ipairs(net:get_wifidevs()) do
66                         local wnet
67                         for _, wnet in ipairs(wdev:get_wifinets()) do
68                                 entry(
69                                         {"admin", "network", "wireless", wnet.netid},
70                                         alias("admin", "network", "wireless"),
71                                         wdev:name() .. ": " .. wnet:shortname()
72                                 )
73                         end
74                 end
75         end
76
77         page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
78         page.leaf   = true
79         page.subindex = true
80
81         page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
82         page.leaf = true
83
84         page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
85         page.leaf = true
86
87         page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
88         page.leaf = true
89
90         page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
91         page.leaf = true
92
93         page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
94         page.leaf = true
95
96         uci:foreach("network", "interface",
97                 function (section)
98                         local ifc = section[".name"]
99                         if ifc ~= "loopback" then
100                                 entry({"admin", "network", "network", ifc},
101                                  true,
102                                  ifc:upper())
103                         end
104                 end
105         )
106
107         if nixio.fs.access("/etc/config/dhcp") then
108                 page = node("admin", "network", "dhcpleases")
109                 page.target = cbi("admin_network/dhcpleases")
110                 page.title  = i18n("DHCP Leases")
111                 page.order  = 30
112
113                 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
114                 page.leaf = true
115
116                 page = node("admin", "network", "hosts")
117                 page.target = cbi("admin_network/hosts")
118                 page.title  = i18n("Hostnames")
119                 page.order  = 40
120         end
121
122         page  = node("admin", "network", "routes")
123         page.target = cbi("admin_network/routes")
124         page.title  = i18n("Static Routes")
125         page.order  = 50
126
127         page = node("admin", "network", "diagnostics")
128         page.target = template("admin_network/diagnostics")
129         page.title  = i18n("Diagnostics")
130         page.order  = 60
131
132         page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
133         page.leaf = true
134
135         page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
136         page.leaf = true
137
138         page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
139         page.leaf = true
140 end
141
142 function wifi_join()
143         local function param(x)
144                 return luci.http.formvalue(x)
145         end
146
147         local function ptable(x)
148                 x = param(x)
149                 return x and (type(x) ~= "table" and { x } or x) or {}
150         end
151
152         local dev  = param("device")
153         local ssid = param("join")
154
155         if dev and ssid then
156                 local cancel  = (param("cancel") or param("cbi.cancel")) and true or false
157
158                 if cancel then
159                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
160                 else
161                         local cbi = require "luci.cbi"
162                         local tpl = require "luci.template"
163                         local map = luci.cbi.load("admin_network/wifi_add")[1]
164
165                         if map:parse() ~= cbi.FORM_DONE then
166                                 tpl.render("header")
167                                 map:render()
168                                 tpl.render("footer")
169                         end
170                 end
171         else
172                 luci.template.render("admin_network/wifi_join")
173         end
174 end
175
176 function wifi_add()
177         local dev = luci.http.formvalue("device")
178         local ntm = require "luci.model.network".init()
179
180         dev = dev and ntm:get_wifidev(dev)
181
182         if dev then
183                 local net = dev:add_wifinet({
184                         mode       = "ap",
185                         ssid       = "OpenWrt",
186                         encryption = "none"
187                 })
188
189                 ntm:save("wireless")
190                 luci.http.redirect(net:adminlink())
191         end
192 end
193
194 function wifi_delete(network)
195         local ntm = require "luci.model.network".init()
196
197         ntm:del_wifinet(network)
198         ntm:save("wireless")
199
200         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
201 end
202
203 function iface_status()
204         local path = luci.dispatcher.context.requestpath
205         local netm = require "luci.model.network".init()
206         local rv   = { }
207
208         local iface
209         for iface in path[#path]:gmatch("[%w%.%-]+") do
210                 local net = netm:get_network(iface)
211                 if net then
212                         local info
213                         local dev  = net:ifname()
214                         local data = { id = iface, uptime = net:uptime() }
215                         for _, info in ipairs(nixio.getifaddrs()) do
216                                 local name = info.name:match("[^:]+")
217                                 if name == dev then
218                                         if info.family == "packet" then
219                                                 data.flags   = info.flags
220                                                 data.stats   = info.data
221                                                 data.macaddr = info.addr
222                                                 data.ifname  = name
223                                         elseif info.family == "inet" then
224                                                 data.ipaddrs = data.ipaddrs or { }
225                                                 data.ipaddrs[#data.ipaddrs+1] = {
226                                                         addr      = info.addr,
227                                                         broadaddr = info.broadaddr,
228                                                         dstaddr   = info.dstaddr,
229                                                         netmask   = info.netmask,
230                                                         prefix    = info.prefix
231                                                 }
232                                         elseif info.family == "inet6" then
233                                                 data.ip6addrs = data.ip6addrs or { }
234                                                 data.ip6addrs[#data.ip6addrs+1] = {
235                                                         addr    = info.addr,
236                                                         netmask = info.netmask,
237                                                         prefix  = info.prefix
238                                                 }
239                                         end
240                                 end
241                         end
242
243                         if next(data) then
244                                 rv[#rv+1] = data
245                         end
246                 end
247         end
248
249         if #rv > 0 then
250                 luci.http.prepare_content("application/json")
251                 luci.http.write_json(rv)
252                 return
253         end
254
255         luci.http.status(404, "No such device")
256 end
257
258 function iface_reconnect()
259         local path  = luci.dispatcher.context.requestpath
260         local iface = path[#path]
261         local netmd = require "luci.model.network".init()
262
263         local net = netmd:get_network(iface)
264         if net then
265                 local ifn
266                 for _, ifn in ipairs(net:get_interfaces()) do
267                         local wnet = ifn:get_wifinet()
268                         if wnet then
269                                 local wdev = wnet:get_device()
270                                 if wdev then
271                                         luci.sys.call(
272                                                 "env -i /sbin/wifi up %q >/dev/null 2>/dev/null"
273                                                         % wdev:name()
274                                         )
275
276                                         luci.http.status(200, "Reconnected")
277                                         return
278                                 end
279                         end
280                 end
281
282                 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
283
284                 require "luci.fs"
285                 if luci.fs.access("/etc/config/radvd") then
286                         luci.sys.call("/etc/init.d/radvd restart >/dev/null 2>/dev/null")
287                 end
288
289                 luci.http.status(200, "Reconnected")
290                 return
291         end
292
293         luci.http.status(404, "No such interface")
294 end
295
296 function iface_shutdown()
297         local path  = luci.dispatcher.context.requestpath
298         local iface = path[#path]
299         local netmd = require "luci.model.network".init()
300
301         local net = netmd:get_network(iface)
302         if net then
303                 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
304                 luci.http.status(200, "Shutdown")
305                 return
306         end
307
308         luci.http.status(404, "No such interface")
309 end
310
311 function iface_delete()
312         local path  = luci.dispatcher.context.requestpath
313         local iface = path[#path]
314         local netmd = require "luci.model.network".init()
315
316         local net = netmd:del_network(iface)
317         if net then
318                 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
319                 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
320                 netmd:commit("network")
321                 netmd:commit("wireless")
322                 return
323         end
324
325         luci.http.status(404, "No such interface")
326 end
327
328 function wifi_status()
329         local netm  = require "luci.model.network".init()
330         local path = luci.dispatcher.context.requestpath
331         local arp  = luci.sys.net.arptable()
332         local rv   = { }
333
334         local dev
335         for dev in path[#path]:gmatch("[%w%.%-]+") do
336                 local j = { id = dev }
337                 local wn = netm:get_wifinet(dev)
338                 local iw = wn and wn.iwinfo
339                 if iw then
340                         local f
341                         for _, f in ipairs({
342                                 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
343                                 "quality", "quality_max", "bssid", "country",
344                                 "encryption", "ifname", "assoclist"
345                         }) do
346                                 j[f] = iw[f]
347                         end
348                 end
349
350                 j.mode = wn and wn:active_mode() or "?"
351                 j.ssid = wn and wn:active_ssid() or "?"
352
353                 rv[#rv+1] = j
354         end
355
356         if #rv > 0 then
357                 luci.http.prepare_content("application/json")
358                 luci.http.write_json(rv)
359                 return
360         end
361
362         luci.http.status(404, "No such device")
363 end
364
365 function lease_status()
366         local rv = { }
367         local leasefile = "/var/dhcp.leases"
368
369         local uci = require "luci.model.uci".cursor()
370         local nfs = require "nixio.fs"
371
372         uci:foreach("dhcp", "dnsmasq",
373                 function(s)
374                         if s.leasefile and nfs.access(s.leasefile) then
375                                 leasefile = s.leasefile
376                                 return false
377                         end
378                 end)
379
380         local fd = io.open(leasefile, "r")
381         if fd then
382                 while true do
383                         local ln = fd:read("*l")
384                         if not ln then
385                                 break
386                         else
387                                 local ts, mac, ip, name = ln:match("^(%d+) (%S+) (%S+) (%S+)")
388                                 if ts and mac and ip and name then
389                                         rv[#rv+1] = {
390                                                 expires  = os.difftime(tonumber(ts) or 0, os.time()),
391                                                 macaddr  = mac,
392                                                 ipaddr   = ip,
393                                                 hostname = (name ~= "*") and name
394                                         }
395                                 end
396                         end
397                 end
398                 fd:close()
399         end
400
401         luci.http.prepare_content("application/json")
402         luci.http.write_json(rv)
403 end
404
405 function diag_command(cmd)
406         local path = luci.dispatcher.context.requestpath
407         local addr = path[#path]
408
409         if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
410                 luci.http.prepare_content("text/plain")
411
412                 local util = io.popen(cmd % addr)
413                 if util then
414                         while true do
415                                 local ln = util:read("*l")
416                                 if not ln then break end
417                                 luci.http.write(ln)
418                                 luci.http.write("\n")
419                         end
420
421                         util:close()
422                 end
423
424                 return
425         end
426
427         luci.http.status(500, "Bad address")
428 end
429
430 function diag_ping()
431         diag_command("ping -c 5 -W 1 %q 2>&1")
432 end
433
434 function diag_traceroute()
435         diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
436 end
437
438 function diag_nslookup()
439         diag_command("nslookup %q 2>&1")
440 end