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