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