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