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