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