1da5eac4643c2922a70e691e5703187df740365f
[project/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 disabled = 1
180 })
181
182 ntm:save("wireless")
183 luci.http.redirect(net:adminlink())
184 end
185 end
186
187 function iface_status(ifaces)
188 local netm = require "luci.model.network".init()
189 local rv = { }
190
191 local iface
192 for iface in ifaces:gmatch("[%w%.%-_]+") do
193 local net = netm:get_network(iface)
194 local device = net and net:get_interface()
195 if device then
196 local data = {
197 id = iface,
198 desc = net:get_i18n(),
199 proto = net:proto(),
200 uptime = net:uptime(),
201 gwaddr = net:gwaddr(),
202 ipaddrs = net:ipaddrs(),
203 ip6addrs = net:ip6addrs(),
204 dnsaddrs = net:dnsaddrs(),
205 ip6prefix = net:ip6prefix(),
206 errors = net:errors(),
207 name = device:shortname(),
208 type = device:type(),
209 typename = device:get_type_i18n(),
210 ifname = device:name(),
211 macaddr = device:mac(),
212 is_up = net:is_up() and device:is_up(),
213 is_alias = net:is_alias(),
214 is_dynamic = net:is_dynamic(),
215 rx_bytes = device:rx_bytes(),
216 tx_bytes = device:tx_bytes(),
217 rx_packets = device:rx_packets(),
218 tx_packets = device:tx_packets(),
219
220 subdevices = { }
221 }
222
223 for _, device in ipairs(net:get_interfaces() or {}) do
224 data.subdevices[#data.subdevices+1] = {
225 name = device:shortname(),
226 type = device:type(),
227 typename = device:get_type_i18n(),
228 ifname = device:name(),
229 macaddr = device:mac(),
230 is_up = device:is_up(),
231 rx_bytes = device:rx_bytes(),
232 tx_bytes = device:tx_bytes(),
233 rx_packets = device:rx_packets(),
234 tx_packets = device:tx_packets(),
235 }
236 end
237
238 rv[#rv+1] = data
239 else
240 rv[#rv+1] = {
241 id = iface,
242 name = iface,
243 type = "ethernet"
244 }
245 end
246 end
247
248 if #rv > 0 then
249 luci.http.prepare_content("application/json")
250 luci.http.write_json(rv)
251 return
252 end
253
254 luci.http.status(404, "No such device")
255 end
256
257 function iface_reconnect(iface)
258 local netmd = require "luci.model.network".init()
259 local net = netmd:get_network(iface)
260 if net then
261 luci.sys.call("env -i /sbin/ifup %s >/dev/null 2>/dev/null"
262 % luci.util.shellquote(iface))
263 luci.http.status(200, "Reconnected")
264 return
265 end
266
267 luci.http.status(404, "No such interface")
268 end
269
270 function wifi_status(devs)
271 local s = require "luci.tools.status"
272 local rv = { }
273
274 if type(devs) == "string" then
275 local dev
276 for dev in devs:gmatch("[%w%.%-]+") do
277 rv[#rv+1] = s.wifi_network(dev)
278 end
279 end
280
281 if #rv > 0 then
282 luci.http.prepare_content("application/json")
283 luci.http.write_json(rv)
284 return
285 end
286
287 luci.http.status(404, "No such device")
288 end
289
290 function wifi_reconnect(radio)
291 local rc = luci.sys.call("env -i /sbin/wifi up %s" % luci.util.shellquote(radio))
292
293 if rc == 0 then
294 luci.http.status(200, "Reconnected")
295 else
296 luci.http.status(500, "Error")
297 end
298 end
299
300 local function _wifi_get_scan_results(cache_key)
301 local results = luci.util.ubus("session", "get", {
302 ubus_rpc_session = luci.model.uci:get_session_id(),
303 keys = { cache_key }
304 })
305
306 if type(results) == "table" and
307 type(results.values) == "table" and
308 type(results.values[cache_key]) == "table"
309 then
310 return results.values[cache_key]
311 end
312
313 return { }
314 end
315
316 function wifi_scan_trigger(radio, update)
317 local iw = radio and luci.sys.wifi.getiwinfo(radio)
318
319 if not iw then
320 luci.http.status(404, "No such radio device")
321 return
322 end
323
324 luci.http.status(204, "Scan scheduled")
325
326 if nixio.fork() == 0 then
327 io.stderr:close()
328 io.stdout:close()
329
330 local _, bss
331 local data, bssids = { }, { }
332 local cache_key = "scan_%s" % radio
333
334 luci.util.ubus("session", "set", {
335 ubus_rpc_session = luci.model.uci:get_session_id(),
336 values = { [cache_key] = nil }
337 })
338
339 for _, bss in ipairs(iw.scanlist or { }) do
340 data[_] = bss
341 bssids[bss.bssid] = bss
342 end
343
344 if update then
345 for _, bss in ipairs(_wifi_get_scan_results(cache_key)) do
346 if not bssids[bss.bssid] then
347 bss.stale = true
348 data[#data + 1] = bss
349 end
350 end
351 end
352
353 luci.util.ubus("session", "set", {
354 ubus_rpc_session = luci.model.uci:get_session_id(),
355 values = { [cache_key] = data }
356 })
357 end
358 end
359
360 function wifi_scan_results(radio)
361 local results = radio and _wifi_get_scan_results("scan_%s" % radio)
362
363 if results and #results > 0 then
364 luci.http.prepare_content("application/json")
365 luci.http.write_json(results)
366 else
367 luci.http.status(404, "No wireless scan results")
368 end
369 end
370
371 function switch_status(switches)
372 local s = require "luci.tools.status"
373
374 luci.http.prepare_content("application/json")
375 luci.http.write_json(s.switch_status(switches))
376 end
377
378 function diag_command(cmd, addr)
379 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
380 luci.http.prepare_content("text/plain")
381
382 local util = io.popen(cmd % luci.util.shellquote(addr))
383 if util then
384 while true do
385 local ln = util:read("*l")
386 if not ln then break end
387 luci.http.write(ln)
388 luci.http.write("\n")
389 end
390
391 util:close()
392 end
393
394 return
395 end
396
397 luci.http.status(500, "Bad address")
398 end
399
400 function diag_ping(addr)
401 diag_command("ping -c 5 -W 1 %s 2>&1", addr)
402 end
403
404 function diag_traceroute(addr)
405 diag_command("traceroute -q 1 -w 1 -n %s 2>&1", addr)
406 end
407
408 function diag_nslookup(addr)
409 diag_command("nslookup %s 2>&1", addr)
410 end
411
412 function diag_ping6(addr)
413 diag_command("ping6 -c 5 %s 2>&1", addr)
414 end
415
416 function diag_traceroute6(addr)
417 diag_command("traceroute6 -q 1 -w 2 -n %s 2>&1", addr)
418 end