85dc68fff0128b5c16396a4da85c7569f106ced4
[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_reconnect"), 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 page = node("admin", "network", "routes")
142 page.target = cbi("admin_network/routes")
143 page.title = _("Static Routes")
144 page.order = 50
145
146 page = node("admin", "network", "diagnostics")
147 page.target = template("admin_network/diagnostics")
148 page.title = _("Diagnostics")
149 page.order = 60
150
151 page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
152 page.leaf = true
153
154 page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
155 page.leaf = true
156
157 page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
158 page.leaf = true
159 -- end
160 end
161
162 function wifi_join()
163 local function param(x)
164 return luci.http.formvalue(x)
165 end
166
167 local function ptable(x)
168 x = param(x)
169 return x and (type(x) ~= "table" and { x } or x) or {}
170 end
171
172 local dev = param("device")
173 local ssid = param("join")
174
175 if dev and ssid then
176 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
177
178 if cancel then
179 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
180 else
181 local cbi = require "luci.cbi"
182 local tpl = require "luci.template"
183 local map = luci.cbi.load("admin_network/wifi_add")[1]
184
185 if map:parse() ~= cbi.FORM_DONE then
186 tpl.render("header")
187 map:render()
188 tpl.render("footer")
189 end
190 end
191 else
192 luci.template.render("admin_network/wifi_join")
193 end
194 end
195
196 function wifi_add()
197 local dev = luci.http.formvalue("device")
198 local ntm = require "luci.model.network".init()
199
200 dev = dev and ntm:get_wifidev(dev)
201
202 if dev then
203 local net = dev:add_wifinet({
204 mode = "ap",
205 ssid = "OpenWrt",
206 encryption = "none"
207 })
208
209 ntm:save("wireless")
210 luci.http.redirect(net:adminlink())
211 end
212 end
213
214 function wifi_delete(network)
215 local ntm = require "luci.model.network".init()
216 local wnet = ntm:get_wifinet(network)
217 if wnet then
218 local dev = wnet:get_device()
219 local nets = wnet:get_networks()
220 if dev then
221 luci.sys.call("env -i /sbin/wifi down %q >/dev/null" % dev:name())
222 ntm:del_wifinet(network)
223 ntm:commit("wireless")
224 local _, net
225 for _, net in ipairs(nets) do
226 if net:is_empty() then
227 ntm:del_network(net:name())
228 ntm:commit("network")
229 end
230 end
231 luci.sys.call("env -i /sbin/wifi up %q >/dev/null" % dev:name())
232 end
233 end
234
235 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
236 end
237
238 function iface_status()
239 local path = luci.dispatcher.context.requestpath
240 local netm = require "luci.model.network".init()
241 local rv = { }
242
243 local iface
244 for iface in path[#path]:gmatch("[%w%.%-_]+") do
245 local net = netm:get_network(iface)
246 local device = net and net:get_interface()
247 if device then
248 local data = {
249 id = iface,
250 proto = net:proto(),
251 uptime = net:uptime(),
252 gwaddr = net:gwaddr(),
253 dnsaddrs = net:dnsaddrs(),
254 name = device:shortname(),
255 type = device:type(),
256 ifname = device:name(),
257 macaddr = device:mac(),
258 is_up = device:is_up(),
259 rx_bytes = device:rx_bytes(),
260 tx_bytes = device:tx_bytes(),
261 rx_packets = device:rx_packets(),
262 tx_packets = device:tx_packets(),
263
264 ipaddrs = { },
265 ip6addrs = { },
266 subdevices = { }
267 }
268
269 local _, a
270 for _, a in ipairs(device:ipaddrs()) do
271 data.ipaddrs[#data.ipaddrs+1] = {
272 addr = a:host():string(),
273 netmask = a:mask():string(),
274 prefix = a:prefix()
275 }
276 end
277 for _, a in ipairs(device:ip6addrs()) do
278 if not a:is6linklocal() then
279 data.ip6addrs[#data.ip6addrs+1] = {
280 addr = a:host():string(),
281 netmask = a:mask():string(),
282 prefix = a:prefix()
283 }
284 end
285 end
286
287 for _, device in ipairs(net:get_interfaces() or {}) do
288 data.subdevices[#data.subdevices+1] = {
289 name = device:shortname(),
290 type = device:type(),
291 ifname = device:name(),
292 macaddr = device:mac(),
293 macaddr = device:mac(),
294 is_up = device:is_up(),
295 rx_bytes = device:rx_bytes(),
296 tx_bytes = device:tx_bytes(),
297 rx_packets = device:rx_packets(),
298 tx_packets = device:tx_packets(),
299 }
300 end
301
302 rv[#rv+1] = data
303 else
304 rv[#rv+1] = {
305 id = iface,
306 name = iface,
307 type = "ethernet"
308 }
309 end
310 end
311
312 if #rv > 0 then
313 luci.http.prepare_content("application/json")
314 luci.http.write_json(rv)
315 return
316 end
317
318 luci.http.status(404, "No such device")
319 end
320
321 function iface_reconnect()
322 local path = luci.dispatcher.context.requestpath
323 local iface = path[#path]
324 local netmd = require "luci.model.network".init()
325
326 local net = netmd:get_network(iface)
327 if net then
328 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
329 luci.http.status(200, "Reconnected")
330 return
331 end
332
333 luci.http.status(404, "No such interface")
334 end
335
336 function iface_shutdown()
337 local path = luci.dispatcher.context.requestpath
338 local iface = path[#path]
339 local netmd = require "luci.model.network".init()
340
341 local net = netmd:get_network(iface)
342 if net then
343 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
344 luci.http.status(200, "Shutdown")
345 return
346 end
347
348 luci.http.status(404, "No such interface")
349 end
350
351 function iface_delete()
352 local path = luci.dispatcher.context.requestpath
353 local iface = path[#path]
354 local netmd = require "luci.model.network".init()
355
356 local net = netmd:del_network(iface)
357 if net then
358 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
359 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
360 netmd:commit("network")
361 netmd:commit("wireless")
362 return
363 end
364
365 luci.http.status(404, "No such interface")
366 end
367
368 function wifi_status()
369 local path = luci.dispatcher.context.requestpath
370 local s = require "luci.tools.status"
371 local rv = { }
372
373 local dev
374 for dev in path[#path]:gmatch("[%w%.%-]+") do
375 rv[#rv+1] = s.wifi_network(dev)
376 end
377
378 if #rv > 0 then
379 luci.http.prepare_content("application/json")
380 luci.http.write_json(rv)
381 return
382 end
383
384 luci.http.status(404, "No such device")
385 end
386
387 function wifi_reconnect()
388 local path = luci.dispatcher.context.requestpath
389 local mode = path[#path-1]
390 local wnet = path[#path]
391 local netmd = require "luci.model.network".init()
392
393 local net = netmd:get_wifinet(wnet)
394 local dev = net:get_device()
395 if dev and net then
396 luci.sys.call("env -i /sbin/wifi down >/dev/null 2>/dev/null")
397
398 dev:set("disabled", nil)
399 net:set("disabled", (mode == "wireless_shutdown") and 1 or nil)
400 netmd:commit("wireless")
401
402 luci.sys.call("env -i /sbin/wifi up >/dev/null 2>/dev/null")
403 luci.http.status(200, (mode == "wireless_shutdown") and "Shutdown" or "Reconnected")
404
405 return
406 end
407
408 luci.http.status(404, "No such radio")
409 end
410
411 function lease_status()
412 local s = require "luci.tools.status"
413
414 luci.http.prepare_content("application/json")
415 luci.http.write('[')
416 luci.http.write_json(s.dhcp_leases())
417 luci.http.write(',')
418 luci.http.write_json(s.dhcp6_leases())
419 luci.http.write(']')
420 end
421
422 function switch_status()
423 local path = luci.dispatcher.context.requestpath
424 local s = require "luci.tools.status"
425
426 luci.http.prepare_content("application/json")
427 luci.http.write_json(s.switch_status(path[#path]))
428 end
429
430 function diag_command(cmd)
431 local path = luci.dispatcher.context.requestpath
432 local addr = path[#path]
433
434 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
435 luci.http.prepare_content("text/plain")
436
437 local util = io.popen(cmd % addr)
438 if util then
439 while true do
440 local ln = util:read("*l")
441 if not ln then break end
442 luci.http.write(ln)
443 luci.http.write("\n")
444 end
445
446 util:close()
447 end
448
449 return
450 end
451
452 luci.http.status(500, "Bad address")
453 end
454
455 function diag_ping()
456 diag_command("ping -c 5 -W 1 %q 2>&1")
457 end
458
459 function diag_traceroute()
460 diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
461 end
462
463 function diag_nslookup()
464 diag_command("nslookup %q 2>&1")
465 end