modules/admin-full: rework dnsamasq page, get rid of options, add tabs, better help...
[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", "add"}, cbi("admin_network/iface_add"), nil)
69         page.leaf = true
70
71         page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
72         page.leaf = true
73
74         uci:foreach("network", "interface",
75                 function (section)
76                         local ifc = section[".name"]
77                         if ifc ~= "loopback" then
78                                 entry({"admin", "network", "network", ifc},
79                                  true,
80                                  ifc:upper())
81                         end
82                 end
83         )
84
85         if nixio.fs.access("/etc/config/dhcp") then
86                 page  = node("admin", "network", "dhcpleases")
87                 page.target = cbi("admin_network/dhcpleases")
88                 page.title  = i18n("DHCP Leases")
89                 page.order  = 30
90         end
91
92         page  = node("admin", "network", "hosts")
93         page.target = cbi("admin_network/hosts")
94         page.title  = i18n("Hostnames")
95         page.order  = 40
96
97         page  = node("admin", "network", "routes")
98         page.target = cbi("admin_network/routes")
99         page.title  = i18n("Static Routes")
100         page.order  = 50
101
102 end
103
104 function wifi_join()
105         local function param(x)
106                 return luci.http.formvalue(x)
107         end
108
109         local function ptable(x)
110                 x = param(x)
111                 return x and (type(x) ~= "table" and { x } or x) or {}
112         end
113
114         local dev  = param("device")
115         local ssid = param("join")
116
117         if dev and ssid then
118                 local cancel  = (param("cancel") or param("cbi.cancel")) and true or false
119
120                 if cancel then
121                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
122                 else
123                         local cbi = require "luci.cbi"
124                         local tpl = require "luci.template"
125                         local map = luci.cbi.load("admin_network/wifi_add")[1]
126
127                         if map:parse() ~= cbi.FORM_DONE then
128                                 tpl.render("header")
129                                 map:render()
130                                 tpl.render("footer")
131                         end
132                 end
133         else
134                 luci.template.render("admin_network/wifi_join")
135         end
136 end
137
138 function wifi_add()
139         local dev = luci.http.formvalue("device")
140         local ntm = require "luci.model.network".init()
141
142         dev = dev and ntm:get_wifidev(dev)
143
144         if dev then
145                 local net = dev:add_wifinet({
146                         mode       = "ap",
147                         ssid       = "OpenWrt",
148                         encryption = "none"
149                 })
150
151                 ntm:save("wireless")
152                 luci.http.redirect(net:adminlink())
153         end
154 end
155
156 function wifi_delete(network)
157         local ntm = require "luci.model.network".init()
158
159         ntm:del_wifinet(network)
160         ntm:save("wireless")
161
162         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
163 end
164
165 function iface_status()
166         local path = luci.dispatcher.context.requestpath
167         local x    = luci.model.uci.cursor_state()
168         local rv   = { }
169
170         local iface
171         for iface in path[#path]:gmatch("[%w%.%-]+") do
172                 local dev = x:get("network", iface, "device") or ""
173                 if #dev == 0 or dev:match("^%d") or dev:match("%W") then
174                         dev = x:get("network", iface, "ifname") or ""
175                         dev = dev:match("%S+")
176                 end
177
178                 local info
179                 local data = { }
180                 for _, info in ipairs(nixio.getifaddrs()) do
181                         local name = info.name:match("[^:]+")
182                         if name == dev then
183                                 if info.family == "packet" then
184                                         data.flags   = info.flags
185                                         data.stats   = info.data
186                                         data.macaddr = info.addr
187                                         data.ifname  = name
188                                 elseif info.family == "inet" then
189                                         data.ipaddrs = data.ipaddrs or { }
190                                         data.ipaddrs[#data.ipaddrs+1] = {
191                                                 addr      = info.addr,
192                                                 broadaddr = info.broadaddr,
193                                                 dstaddr   = info.dstaddr,
194                                                 netmask   = info.netmask,
195                                                 prefix    = info.prefix
196                                         }
197                                 elseif info.family == "inet6" then
198                                         data.ip6addrs = data.ip6addrs or { }
199                                         data.ip6addrs[#data.ip6addrs+1] = {
200                                                 addr    = info.addr,
201                                                 netmask = info.netmask,
202                                                 prefix  = info.prefix
203                                         }
204                                 end
205                         end
206                 end
207
208                 if next(data) then
209                         rv[#rv+1] = data
210                 end
211         end
212
213         if #rv > 0 then
214                 luci.http.prepare_content("application/json")
215                 luci.http.write_json(rv)
216                 return
217         end
218
219         luci.http.status(404, "No such device")
220 end
221
222 function wifi_status()
223         local path = luci.dispatcher.context.requestpath
224         local rv   = { }
225
226         local dev
227         for dev in path[#path]:gmatch("[%w%.%-]+") do
228                 local iw = luci.sys.wifi.getiwinfo(dev)
229                 if iw then
230                         local f
231                         local j = { }
232                         for _, f in ipairs({
233                                 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
234                                 "quality", "quality_max", "mode", "ssid", "bssid", "country",
235                                 "encryption", "ifname"
236                         }) do
237                                 j[f] = iw[f]
238                         end
239
240                         rv[#rv+1] = j
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