73efc35b03ec7915a9c59c2ca930bd4919ddc4cf
[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
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 module("luci.controller.admin.network", package.seeall)
16
17 function index()
18         require("luci.i18n")
19         local uci = require("luci.model.uci").cursor()
20         local net = require "luci.model.network".init(uci)
21         local i18n = luci.i18n.translate
22         local has_wifi = nixio.fs.stat("/etc/config/wireless")
23         local has_switch = false
24
25         uci:foreach("network", "switch",
26                 function(s)
27                         has_switch = true
28                         return false
29                 end
30         )
31
32         local page
33
34         page = node("admin", "network")
35         page.target = alias("admin", "network", "network")
36         page.title  = i18n("Network")
37         page.order  = 50
38         page.index  = true
39
40         if has_switch then
41                 page  = node("admin", "network", "vlan")
42                 page.target = cbi("admin_network/vlan")
43                 page.title  = i18n("Switch")
44                 page.order  = 20
45         end
46
47         if has_wifi and has_wifi.size > 0 then
48                 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("Wifi"), 15)
49                 page.leaf = true
50                 page.subindex = true
51
52                 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
53                 page.leaf = true
54
55                 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
56                 page.leaf = true
57
58                 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
59                 page.leaf = true
60
61                 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
62                 page.leaf = true
63
64                 local wdev
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.netid},
70                                         alias("admin", "network", "wireless"),
71                                         wdev:name() .. ": " .. wnet:shortname()
72                                 )
73                         end
74                 end
75         end
76
77         page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
78         page.leaf   = true
79         page.subindex = true
80
81         page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
82         page.leaf = true
83
84         page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
85         page.leaf = true
86
87         page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
88         page.leaf = true
89
90         page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
91         page.leaf = true
92
93         page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
94         page.leaf = true
95
96         uci:foreach("network", "interface",
97                 function (section)
98                         local ifc = section[".name"]
99                         if ifc ~= "loopback" then
100                                 entry({"admin", "network", "network", ifc},
101                                  true,
102                                  ifc:upper())
103                         end
104                 end
105         )
106
107         if nixio.fs.access("/etc/config/dhcp") then
108                 page = node("admin", "network", "dhcpleases")
109                 page.target = cbi("admin_network/dhcpleases")
110                 page.title  = i18n("DHCP Leases")
111                 page.order  = 30
112
113                 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
114                 page.leaf = true
115
116                 page = node("admin", "network", "hosts")
117                 page.target = cbi("admin_network/hosts")
118                 page.title  = i18n("Hostnames")
119                 page.order  = 40
120         end
121
122         page  = node("admin", "network", "routes")
123         page.target = cbi("admin_network/routes")
124         page.title  = i18n("Static Routes")
125         page.order  = 50
126
127         page = node("admin", "network", "diagnostics")
128         page.target = template("admin_network/diagnostics")
129         page.title  = i18n("Diagnostics")
130         page.order  = 60
131
132         page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
133         page.leaf = true
134
135         page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
136         page.leaf = true
137
138         page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
139         page.leaf = true
140 end
141
142 function wifi_join()
143         local function param(x)
144                 return luci.http.formvalue(x)
145         end
146
147         local function ptable(x)
148                 x = param(x)
149                 return x and (type(x) ~= "table" and { x } or x) or {}
150         end
151
152         local dev  = param("device")
153         local ssid = param("join")
154
155         if dev and ssid then
156                 local cancel  = (param("cancel") or param("cbi.cancel")) and true or false
157
158                 if cancel then
159                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
160                 else
161                         local cbi = require "luci.cbi"
162                         local tpl = require "luci.template"
163                         local map = luci.cbi.load("admin_network/wifi_add")[1]
164
165                         if map:parse() ~= cbi.FORM_DONE then
166                                 tpl.render("header")
167                                 map:render()
168                                 tpl.render("footer")
169                         end
170                 end
171         else
172                 luci.template.render("admin_network/wifi_join")
173         end
174 end
175
176 function wifi_add()
177         local dev = luci.http.formvalue("device")
178         local ntm = require "luci.model.network".init()
179
180         dev = dev and ntm:get_wifidev(dev)
181
182         if dev then
183                 local net = dev:add_wifinet({
184                         mode       = "ap",
185                         ssid       = "OpenWrt",
186                         encryption = "none"
187                 })
188
189                 ntm:save("wireless")
190                 luci.http.redirect(net:adminlink())
191         end
192 end
193
194 function wifi_delete(network)
195         local ntm = require "luci.model.network".init()
196
197         ntm:del_wifinet(network)
198         ntm:save("wireless")
199
200         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
201 end
202
203 function iface_status()
204         local path = luci.dispatcher.context.requestpath
205         local netm = require "luci.model.network".init()
206         local rv   = { }
207
208         local iface
209         for iface in path[#path]:gmatch("[%w%.%-]+") do
210                 local net = netm:get_network(iface)
211                 if net then
212                         local info
213                         local dev  = net:ifname()
214                         local data = { id = iface, uptime = net:uptime() }
215                         for _, info in ipairs(nixio.getifaddrs()) do
216                                 local name = info.name:match("[^:]+")
217                                 if name == dev then
218                                         if info.family == "packet" then
219                                                 data.flags   = info.flags
220                                                 data.stats   = info.data
221                                                 data.macaddr = info.addr
222                                                 data.ifname  = name
223                                         elseif info.family == "inet" then
224                                                 data.ipaddrs = data.ipaddrs or { }
225                                                 data.ipaddrs[#data.ipaddrs+1] = {
226                                                         addr      = info.addr,
227                                                         broadaddr = info.broadaddr,
228                                                         dstaddr   = info.dstaddr,
229                                                         netmask   = info.netmask,
230                                                         prefix    = info.prefix
231                                                 }
232                                         elseif info.family == "inet6" then
233                                                 data.ip6addrs = data.ip6addrs or { }
234                                                 data.ip6addrs[#data.ip6addrs+1] = {
235                                                         addr    = info.addr,
236                                                         netmask = info.netmask,
237                                                         prefix  = info.prefix
238                                                 }
239                                         end
240                                 end
241                         end
242
243                         if next(data) then
244                                 rv[#rv+1] = data
245                         end
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()
259         local path  = luci.dispatcher.context.requestpath
260         local iface = path[#path]
261         local netmd = require "luci.model.network".init()
262
263         local net = netmd:get_network(iface)
264         if net then
265                 local ifn
266                 for _, ifn in ipairs(net:get_interfaces()) do
267                         local wnet = ifn:get_wifinet()
268                         if wnet then
269                                 local wdev = wnet:get_device()
270                                 if wdev then
271                                         luci.sys.call(
272                                                 "env -i /sbin/wifi up %q >/dev/null 2>/dev/null"
273                                                         % wdev:name()
274                                         )
275
276                                         luci.http.status(200, "Reconnected")
277                                         return
278                                 end
279                         end
280                 end
281
282                 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
283                 luci.http.status(200, "Reconnected")
284                 return
285         end
286
287         luci.http.status(404, "No such interface")
288 end
289
290 function iface_shutdown()
291         local path  = luci.dispatcher.context.requestpath
292         local iface = path[#path]
293         local netmd = require "luci.model.network".init()
294
295         local net = netmd:get_network(iface)
296         if net then
297                 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
298                 luci.http.status(200, "Shutdown")
299                 return
300         end
301
302         luci.http.status(404, "No such interface")
303 end
304
305 function iface_delete()
306         local path  = luci.dispatcher.context.requestpath
307         local iface = path[#path]
308         local netmd = require "luci.model.network".init()
309
310         local net = netmd:del_network(iface)
311         if net then
312                 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
313                 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
314                 netmd:commit("network")
315                 netmd:commit("wireless")
316                 return
317         end
318
319         luci.http.status(404, "No such interface")
320 end
321
322 function wifi_status()
323         local netm  = require "luci.model.network".init()
324         local path = luci.dispatcher.context.requestpath
325         local arp  = luci.sys.net.arptable()
326         local rv   = { }
327
328         local dev
329         for dev in path[#path]:gmatch("[%w%.%-]+") do
330                 local j = { id = dev }
331                 local wn = netm:get_wifinet(dev)
332                 local iw = wn and wn.iwinfo
333                 if iw then
334                         local f
335                         for _, f in ipairs({
336                                 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
337                                 "quality", "quality_max", "bssid", "country",
338                                 "encryption", "ifname", "assoclist"
339                         }) do
340                                 j[f] = iw[f]
341                         end
342                 end
343
344                 j.mode = wn and wn:active_mode() or "?"
345                 j.ssid = wn and wn:active_ssid() or "?"
346
347                 rv[#rv+1] = j
348         end
349
350         if #rv > 0 then
351                 luci.http.prepare_content("application/json")
352                 luci.http.write_json(rv)
353                 return
354         end
355
356         luci.http.status(404, "No such device")
357 end
358
359 function lease_status()
360         local rv = { }
361         local leasefile = "/var/dhcp.leases"
362
363         local uci = require "luci.model.uci".cursor()
364         local nfs = require "nixio.fs"
365
366         uci:foreach("dhcp", "dnsmasq",
367                 function(s)
368                         if s.leasefile and nfs.access(s.leasefile) then
369                                 leasefile = s.leasefile
370                                 return false
371                         end
372                 end)
373
374         local fd = io.open(leasefile, "r")
375         if fd then
376                 while true do
377                         local ln = fd:read("*l")
378                         if not ln then
379                                 break
380                         else
381                                 local ts, mac, ip, name = ln:match("^(%d+) (%S+) (%S+) (%S+)")
382                                 if ts and mac and ip and name then
383                                         rv[#rv+1] = {
384                                                 expires  = os.difftime(tonumber(ts) or 0, os.time()),
385                                                 macaddr  = mac,
386                                                 ipaddr   = ip,
387                                                 hostname = (name ~= "*") and name
388                                         }
389                                 end
390                         end
391                 end
392                 fd:close()
393         end
394
395         luci.http.prepare_content("application/json")
396         luci.http.write_json(rv)
397 end
398
399 function diag_command(cmd)
400         local path = luci.dispatcher.context.requestpath
401         local addr = path[#path]
402
403         if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
404                 luci.http.prepare_content("text/plain")
405
406                 local util = io.popen(cmd % addr)
407                 if util then
408                         while true do
409                                 local ln = util:read("*l")
410                                 if not ln then break end
411                                 luci.http.write(ln)
412                                 luci.http.write("\n")
413                         end
414
415                         util:close()
416                 end
417
418                 return
419         end
420
421         luci.http.status(500, "Bad address")
422 end
423
424 function diag_ping()
425         diag_command("ping -c 5 -W 1 %q 2>&1")
426 end
427
428 function diag_traceroute()
429         diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
430 end
431
432 function diag_nslookup()
433         diag_command("nslookup %q 2>&1")
434 end