2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
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
10 http://www.apache.org/licenses/LICENSE-2.0
15 module("luci.controller.admin.network", package.seeall)
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
24 uci:foreach("network", "switch",
33 page = node("admin", "network")
34 page.target = alias("admin", "network", "network")
35 page.title = i18n("Network")
40 page = node("admin", "network", "vlan")
41 page.target = cbi("admin_network/vlan")
42 page.title = i18n("Switch")
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)
51 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
54 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
57 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
60 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
64 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
68 page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
71 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
74 uci:foreach("network", "interface",
76 local ifc = section[".name"]
77 if ifc ~= "loopback" then
78 entry({"admin", "network", "network", ifc},
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")
92 page = node("admin", "network", "hosts")
93 page.target = cbi("admin_network/hosts")
94 page.title = i18n("Hostnames")
97 page = node("admin", "network", "routes")
98 page.target = cbi("admin_network/routes")
99 page.title = i18n("Static Routes")
105 local function param(x)
106 return luci.http.formvalue(x)
109 local function ptable(x)
111 return x and (type(x) ~= "table" and { x } or x) or {}
114 local dev = param("device")
115 local ssid = param("join")
118 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
121 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
123 local cbi = require "luci.cbi"
124 local tpl = require "luci.template"
125 local map = luci.cbi.load("admin_network/wifi_add")[1]
127 if map:parse() ~= cbi.FORM_DONE then
134 luci.template.render("admin_network/wifi_join")
139 local dev = luci.http.formvalue("device")
140 local ntm = require "luci.model.network".init()
142 dev = dev and ntm:get_wifidev(dev)
145 local net = dev:add_wifinet({
152 luci.http.redirect(net:adminlink())
156 function wifi_delete(network)
157 local ntm = require "luci.model.network".init()
159 ntm:del_wifinet(network)
162 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
165 function iface_status()
166 local path = luci.dispatcher.context.requestpath
167 local x = luci.model.uci.cursor_state()
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+")
180 for _, info in ipairs(nixio.getifaddrs()) do
181 local name = info.name:match("[^:]+")
183 if info.family == "packet" then
184 data.flags = info.flags
185 data.stats = info.data
186 data.macaddr = info.addr
188 elseif info.family == "inet" then
189 data.ipaddrs = data.ipaddrs or { }
190 data.ipaddrs[#data.ipaddrs+1] = {
192 broadaddr = info.broadaddr,
193 dstaddr = info.dstaddr,
194 netmask = info.netmask,
197 elseif info.family == "inet6" then
198 data.ip6addrs = data.ip6addrs or { }
199 data.ip6addrs[#data.ip6addrs+1] = {
201 netmask = info.netmask,
214 luci.http.prepare_content("application/json")
215 luci.http.write_json(rv)
219 luci.http.status(404, "No such device")
222 function wifi_status()
223 local path = luci.dispatcher.context.requestpath
227 for dev in path[#path]:gmatch("[%w%.%-]+") do
228 local iw = luci.sys.wifi.getiwinfo(dev)
233 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
234 "quality", "quality_max", "mode", "ssid", "bssid", "country",
235 "encryption", "ifname"
245 luci.http.prepare_content("application/json")
246 luci.http.write_json(rv)
250 luci.http.status(404, "No such device")