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