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