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