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