00e1fe2ff1fb9f676a7cc924633bd1ae2ebdfb36
[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 $Id$
14 ]]--
15
16 module("luci.controller.admin.network", package.seeall)
17
18 function index()
19         local uci = require("luci.model.uci").cursor()
20         local page
21
22         page = node("admin", "network")
23         page.target = firstchild()
24         page.title  = _("Network")
25         page.order  = 50
26         page.index  = true
27
28 --      if page.inreq then
29                 local has_switch = false
30
31                 uci:foreach("network", "switch",
32                         function(s)
33                                 has_switch = true
34                                 return false
35                         end)
36
37                 if has_switch then
38                         page  = node("admin", "network", "vlan")
39                         page.target = cbi("admin_network/vlan")
40                         page.title  = _("Switch")
41                         page.order  = 20
42                 end
43
44
45                 local has_wifi = false
46
47                 uci:foreach("wireless", "wifi-device",
48                         function(s)
49                                 has_wifi = true
50                                 return false
51                         end)
52
53                 if has_wifi then
54                         page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil)
55                         page.leaf = true
56
57                         page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil)
58                         page.leaf = true
59
60                         page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil)
61                         page.leaf = true
62
63                         page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil)
64                         page.leaf = true
65
66                         page = entry({"admin", "network", "wireless_reconnect"}, call("wifi_reconnect"), nil)
67                         page.leaf = true
68
69                         page = entry({"admin", "network", "wireless_shutdown"}, call("wifi_reconnect"), nil)
70                         page.leaf = true
71
72                         page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wifi"), 15)
73                         page.leaf = true
74                         page.subindex = true
75
76                         if page.inreq then
77                                 local wdev
78                                 local net = require "luci.model.network".init(uci)
79                                 for _, wdev in ipairs(net:get_wifidevs()) do
80                                         local wnet
81                                         for _, wnet in ipairs(wdev:get_wifinets()) do
82                                                 entry(
83                                                         {"admin", "network", "wireless", wnet:id()},
84                                                         alias("admin", "network", "wireless"),
85                                                         wdev:name() .. ": " .. wnet:shortname()
86                                                 )
87                                         end
88                                 end
89                         end
90                 end
91
92
93                 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
94                 page.leaf = true
95
96                 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
97                 page.leaf = true
98
99                 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
100                 page.leaf = true
101
102                 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
103                 page.leaf = true
104
105                 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
106                 page.leaf = true
107
108                 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)
109                 page.leaf   = true
110                 page.subindex = true
111
112                 if page.inreq then
113                         uci:foreach("network", "interface",
114                                 function (section)
115                                         local ifc = section[".name"]
116                                         if ifc ~= "loopback" then
117                                                 entry({"admin", "network", "network", ifc},
118                                                 true, ifc:upper())
119                                         end
120                                 end)
121                 end
122
123
124                 if nixio.fs.access("/etc/config/dhcp") then
125                         page = node("admin", "network", "dhcp")
126                         page.target = cbi("admin_network/dhcp")
127                         page.title  = _("DHCP and DNS")
128                         page.order  = 30
129
130                         page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
131                         page.leaf = true
132
133                         page = node("admin", "network", "hosts")
134                         page.target = cbi("admin_network/hosts")
135                         page.title  = _("Hostnames")
136                         page.order  = 40
137                 end
138
139                 page  = node("admin", "network", "routes")
140                 page.target = cbi("admin_network/routes")
141                 page.title  = _("Static Routes")
142                 page.order  = 50
143
144                 page = node("admin", "network", "diagnostics")
145                 page.target = template("admin_network/diagnostics")
146                 page.title  = _("Diagnostics")
147                 page.order  = 60
148
149                 page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
150                 page.leaf = true
151
152                 page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
153                 page.leaf = true
154
155                 page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
156                 page.leaf = true
157 --      end
158 end
159
160 function wifi_join()
161         local function param(x)
162                 return luci.http.formvalue(x)
163         end
164
165         local function ptable(x)
166                 x = param(x)
167                 return x and (type(x) ~= "table" and { x } or x) or {}
168         end
169
170         local dev  = param("device")
171         local ssid = param("join")
172
173         if dev and ssid then
174                 local cancel  = (param("cancel") or param("cbi.cancel")) and true or false
175
176                 if cancel then
177                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
178                 else
179                         local cbi = require "luci.cbi"
180                         local tpl = require "luci.template"
181                         local map = luci.cbi.load("admin_network/wifi_add")[1]
182
183                         if map:parse() ~= cbi.FORM_DONE then
184                                 tpl.render("header")
185                                 map:render()
186                                 tpl.render("footer")
187                         end
188                 end
189         else
190                 luci.template.render("admin_network/wifi_join")
191         end
192 end
193
194 function wifi_add()
195         local dev = luci.http.formvalue("device")
196         local ntm = require "luci.model.network".init()
197
198         dev = dev and ntm:get_wifidev(dev)
199
200         if dev then
201                 local net = dev:add_wifinet({
202                         mode       = "ap",
203                         ssid       = "OpenWrt",
204                         encryption = "none"
205                 })
206
207                 ntm:save("wireless")
208                 luci.http.redirect(net:adminlink())
209         end
210 end
211
212 function wifi_delete(network)
213         local ntm = require "luci.model.network".init()
214         local wnet = ntm:get_wifinet(network)
215         if wnet then
216                 local dev = wnet:get_device()
217                 local nets = wnet:get_networks()
218                 if dev then
219                         luci.sys.call("env -i /sbin/wifi down %q >/dev/null" % dev:name())
220                         ntm:del_wifinet(network)
221                         ntm:commit("wireless")
222                         local _, net
223                         for _, net in ipairs(nets) do
224                                 if net:is_empty() then
225                                         ntm:del_network(net:name())
226                                         ntm:commit("network")
227                                 end
228                         end
229                         luci.sys.call("env -i /sbin/wifi up %q >/dev/null" % dev:name())
230                 end
231         end
232
233         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
234 end
235
236 function iface_status()
237         local path = luci.dispatcher.context.requestpath
238         local netm = require "luci.model.network".init()
239         local rv   = { }
240
241         local iface
242         for iface in path[#path]:gmatch("[%w%.%-_]+") do
243                 local net = netm:get_network(iface)
244                 local device = net and net:get_interface()
245                 if device then
246                         local data = {
247                                 id         = iface,
248                                 proto      = net:proto(),
249                                 uptime     = net:uptime(),
250                                 gwaddr     = net:gwaddr(),
251                                 dnsaddrs   = net:dnsaddrs(),
252                                 name       = device:shortname(),
253                                 type       = device:type(),
254                                 ifname     = device:name(),
255                                 macaddr    = device:mac(),
256                                 is_up      = device:is_up(),
257                                 rx_bytes   = device:rx_bytes(),
258                                 tx_bytes   = device:tx_bytes(),
259                                 rx_packets = device:rx_packets(),
260                                 tx_packets = device:tx_packets(),
261
262                                 ipaddrs    = { },
263                                 ip6addrs   = { },
264                                 subdevices = { }
265                         }
266
267                         local _, a
268                         for _, a in ipairs(device:ipaddrs()) do
269                                 data.ipaddrs[#data.ipaddrs+1] = {
270                                         addr      = a:host():string(),
271                                         netmask   = a:mask():string(),
272                                         prefix    = a:prefix()
273                                 }
274                         end
275                         for _, a in ipairs(device:ip6addrs()) do
276                                 if not a:is6linklocal() then
277                                         data.ip6addrs[#data.ip6addrs+1] = {
278                                                 addr      = a:host():string(),
279                                                 netmask   = a:mask():string(),
280                                                 prefix    = a:prefix()
281                                         }
282                                 end
283                         end
284
285                         for _, device in ipairs(net:get_interfaces() or {}) do
286                                 data.subdevices[#data.subdevices+1] = {
287                                         name       = device:shortname(),
288                                         type       = device:type(),
289                                         ifname     = device:name(),
290                                         macaddr    = device:mac(),
291                                         macaddr    = device:mac(),
292                                         is_up      = device:is_up(),
293                                         rx_bytes   = device:rx_bytes(),
294                                         tx_bytes   = device:tx_bytes(),
295                                         rx_packets = device:rx_packets(),
296                                         tx_packets = device:tx_packets(),
297                                 }
298                         end
299
300                         rv[#rv+1] = data
301                 else
302                         rv[#rv+1] = {
303                                 id   = iface,
304                                 name = iface,
305                                 type = "ethernet"
306                         }
307                 end
308         end
309
310         if #rv > 0 then
311                 luci.http.prepare_content("application/json")
312                 luci.http.write_json(rv)
313                 return
314         end
315
316         luci.http.status(404, "No such device")
317 end
318
319 function iface_reconnect()
320         local path  = luci.dispatcher.context.requestpath
321         local iface = path[#path]
322         local netmd = require "luci.model.network".init()
323
324         local net = netmd:get_network(iface)
325         if net then
326                 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
327                 luci.http.status(200, "Reconnected")
328                 return
329         end
330
331         luci.http.status(404, "No such interface")
332 end
333
334 function iface_shutdown()
335         local path  = luci.dispatcher.context.requestpath
336         local iface = path[#path]
337         local netmd = require "luci.model.network".init()
338
339         local net = netmd:get_network(iface)
340         if net then
341                 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
342                 luci.http.status(200, "Shutdown")
343                 return
344         end
345
346         luci.http.status(404, "No such interface")
347 end
348
349 function iface_delete()
350         local path  = luci.dispatcher.context.requestpath
351         local iface = path[#path]
352         local netmd = require "luci.model.network".init()
353
354         local net = netmd:del_network(iface)
355         if net then
356                 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
357                 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
358                 netmd:commit("network")
359                 netmd:commit("wireless")
360                 return
361         end
362
363         luci.http.status(404, "No such interface")
364 end
365
366 function wifi_status()
367         local path = luci.dispatcher.context.requestpath
368         local s    = require "luci.tools.status"
369         local rv   = { }
370
371         local dev
372         for dev in path[#path]:gmatch("[%w%.%-]+") do
373                 rv[#rv+1] = s.wifi_network(dev)
374         end
375
376         if #rv > 0 then
377                 luci.http.prepare_content("application/json")
378                 luci.http.write_json(rv)
379                 return
380         end
381
382         luci.http.status(404, "No such device")
383 end
384
385 function wifi_reconnect()
386         local path  = luci.dispatcher.context.requestpath
387         local mode  = path[#path-1]
388         local wnet  = path[#path]
389         local netmd = require "luci.model.network".init()
390
391         local net = netmd:get_wifinet(wnet)
392         local dev = net:get_device()
393         if dev and net then
394                 luci.sys.call("env -i /sbin/wifi down >/dev/null 2>/dev/null")
395
396                 dev:set("disabled", nil)
397                 net:set("disabled", (mode == "wireless_shutdown") and 1 or nil)
398                 netmd:commit("wireless")
399
400                 luci.sys.call("env -i /sbin/wifi up >/dev/null 2>/dev/null")
401                 luci.http.status(200, (mode == "wireless_shutdown") and "Shutdown" or "Reconnected")
402
403                 return
404         end
405
406         luci.http.status(404, "No such radio")
407 end
408
409 function lease_status()
410         local s = require "luci.tools.status"
411
412         luci.http.prepare_content("application/json")
413         luci.http.write('[')
414         luci.http.write_json(s.dhcp_leases())
415         luci.http.write(',')
416         luci.http.write_json(s.dhcp6_leases())
417         luci.http.write(']')
418 end
419
420 function diag_command(cmd)
421         local path = luci.dispatcher.context.requestpath
422         local addr = path[#path]
423
424         if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
425                 luci.http.prepare_content("text/plain")
426
427                 local util = io.popen(cmd % addr)
428                 if util then
429                         while true do
430                                 local ln = util:read("*l")
431                                 if not ln then break end
432                                 luci.http.write(ln)
433                                 luci.http.write("\n")
434                         end
435
436                         util:close()
437                 end
438
439                 return
440         end
441
442         luci.http.status(500, "Bad address")
443 end
444
445 function diag_ping()
446         diag_command("ping -c 5 -W 1 %q 2>&1")
447 end
448
449 function diag_traceroute()
450         diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
451 end
452
453 function diag_nslookup()
454         diag_command("nslookup %q 2>&1")
455 end