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