NIU: Minor fixes
[project/luci.git] / modules / niu / luasrc / model / cbi / niu / wireless / ap1.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2009 Steven Barth <steven@midlink.org>
5 Copyright 2009 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 local iface = "ap"
17 local ap = true
18
19
20 local fs = require "nixio.fs"
21 local sys = require "luci.sys"
22 local cursor = require "luci.model.uci".inst
23 local state = require "luci.model.uci".inst_state
24 cursor:unload("wireless")
25
26 local device = cursor:get("wireless", iface, "device")
27 local hwtype = cursor:get("wireless", device, "type")
28
29 local nsantenna = cursor:get("wireless", device, "antenna")
30
31 local iw = nil
32 local tx_powers = {}
33 local chan = sys.wifi.channels()
34
35 state:foreach("wireless", "wifi-iface",
36         function(s)
37                 if s.device == device and not iw then
38                         iw = sys.wifi.getiwinfo(s.ifname or s.device)
39                         chan = sys.wifi.channels(s.ifname or s.device)
40                         tx_powers = iw.txpwrlist or { }
41                 end
42         end)
43         
44 local m
45
46
47 if ap then
48 m = Map("wireless", translate("Configure Access Point"))
49 end
50
51 --- Device Settings ---
52 s = m:section(NamedSection, device, "wifi-device", "Device Configuration")
53 s.addremove = false
54
55 s:tab("general", translate("General Settings"))
56
57 ch = s:taboption("general", Value, "channel", translate("Channel"))
58 ch:value("auto", translate("automatic"))
59 for _, f in ipairs(chan) do
60         ch:value(f.channel, "%i (%.3f GHz)" %{ f.channel, f.mhz / 1000 })
61 end
62
63
64
65 s:tab("expert", translate("Expert Settings"))
66 if hwtype == "mac80211" then
67         local macaddr = cursor:get("wireless", device, "macaddr") or "!"
68         local hwmode = cursor:get("wireless", device, "hwmode")
69         local modes = {}
70         local phy
71         local allowed = {}
72         for entry in fs.glob("/sys/class/ieee80211/*") do
73                 if (fs.readfile(entry .. "/macaddress") or ""):find(macaddr) == 1 then
74                         phy = entry:sub(22)
75                 end
76         end
77         if phy then
78                 local iwp = io.popen("iw phy " .. phy .. " info")
79                 local iwin = iwp:read("*a")
80                 
81                 if iwp then
82                         iwp:close()
83                         local htcap = iwin:match("HT capabilities:%s*0x([0-9a-fA-F]+)")
84                         allowed.n = (htcap and tonumber(htcap, 16) or 0) > 0
85                         allowed.g = iwin:find("2412 MHz")
86                         allowed.a = iwin:find("5180 MHz")
87                 end
88         end
89         
90         if next(allowed) then
91                 mode = s:taboption("expert", ListValue, "hwmode", translate("Communication Protocol"))
92                 if allowed.n and allowed.g then
93                         mode:value("11ng", "802.11n (2.4 GHz)")
94                 end
95                 if allowed.n and allowed.a then
96                         mode:value("11na", "802.11n (5 GHz)")
97                 end
98                 if allowed.a then
99                         mode:value("11a", "802.11a (5 GHz)")
100                 end
101                 if allowed.g then
102                         mode:value("11g", "802.11g (2.4 GHz)")
103                         mode:value("11bg", "802.11b+g (2.4 GHz)")
104                         mode:value("11b", "802.11b (2.4 GHz)")
105                 end
106         end
107
108         tp = s:taboption("expert",
109                 (tx_powers and #tx_powers > 0) and ListValue or Value,
110                 "txpower", translate("Transmission Power"), "dBm")
111
112         tp.rmempty = true
113         tp:value("", translate("automatic"))
114         for _, p in ipairs(iw and iw.txpwrlist or {}) do
115                 tp:value(p.dbm, "%i dBm (%i mW)" %{ p.dbm, p.mw })
116         end
117 elseif hwtype == "atheros" then
118         tp = s:taboption("expert",
119                 (#tx_powers > 0) and ListValue or Value,
120                 "txpower", translate("Transmission Power"), "dBm")
121
122         tp.rmempty = true
123         tp:value("", translate("automatic"))
124         for _, p in ipairs(iw.txpwrlist) do
125                 tp:value(p.dbm, "%i dBm (%i mW)" %{ p.dbm, p.mw })
126         end
127
128         mode = s:taboption("expert", ListValue, "hwmode", translate("Communication Protocol"))
129         mode:value("", translate("automatic"))
130         mode:value("11g", "802.11g")
131         mode:value("11b", "802.11b")
132         mode:value("11bg", "802.11b+g")
133         mode:value("11a", "802.11a")
134         mode:value("11gst", "802.11g + Turbo")
135         mode:value("11ast", "802.11a + Turbo")
136         
137         if nsantenna then -- NanoFoo
138                 local ant = s:taboption("expert", ListValue, "antenna", translate("Transmitter Antenna"))
139                 ant:value("auto")
140                 ant:value("vertical")
141                 ant:value("horizontal")
142                 ant:value("external")
143                 ant.default = "auto"
144         end
145 elseif hwtype == "broadcom" then
146         tp = s:taboption("expert",
147                 (#tx_powers > 0) and ListValue or Value,
148                 "txpower", translate("Transmit Power"), "dBm")
149
150         tp.rmempty = true
151         tp:value("", translate("automatic"))
152         for _, p in ipairs(iw.txpwrlist) do
153                 tp:value(p.dbm, "%i dBm (%i mW)" %{ p.dbm, p.mw })
154         end     
155
156         mp = s:taboption("expert", ListValue, "macfilter", translate("MAC-Address Filter"))
157         mp:value("", translate("disable"))
158         mp:value("allow", translate("Allow listed only"))
159         mp:value("deny", translate("Allow all except listed"))
160         ml = s:taboption("expert", DynamicList, "maclist", translate("MAC-List"))
161         ml:depends({macfilter="allow"})
162         ml:depends({macfilter="deny"})
163
164         s:taboption("expert", Flag, "frameburst", translate("Allow Burst Transmissions"))
165 elseif hwtype == "prism2" then
166         s:taboption("expert", Value, "txpower", translate("Transmission Power"), "att units").rmempty = true
167 end
168
169
170
171
172 s = m:section(NamedSection, iface, "wifi-iface", translate("Interface Details"))
173 s.addremove = false
174
175 s:tab("general", translate("General Settings"))
176 s:tab("expert", translate("Expert Settings"))
177
178 s:taboption("general", Value, "ssid", translate("Network Name (<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>)"))
179
180 mode = s:taboption("expert", ListValue, "mode", translate("Operating Mode"))
181 mode.override_values = true
182 mode:value("ap", translate("Access Point"))
183
184 encr = s:taboption("expert", ListValue, "encryption", translate("Encryption"))
185
186
187 if hwtype == "mac80211" then
188         s:taboption("expert", Flag, "wds", translate("Enable Bridging and Repeating (WDS)"))
189         s:taboption("expert", Flag, "powersave", translate("Enable Powersaving"))
190 elseif hwtype == "atheros" then
191         -- mode:value("wds", translate("Static WDS"))
192         
193         mp = s:taboption("expert", ListValue, "macpolicy", translate("MAC-Address Filter"))
194         mp:value("", translate("disable"))
195         mp:value("deny", translate("Allow listed only"))
196         mp:value("allow", translate("Allow all except listed"))
197         ml = s:taboption("expert", DynamicList, "maclist", translate("MAC-List"))
198         ml:depends({macpolicy="allow"})
199         ml:depends({macpolicy="deny"})
200
201         s:taboption("expert", Flag, "wds", translate("Enable Bridging and Repeating (WDS)"))
202                 
203         if ap then                              
204                 hidden = s:taboption("expert", Flag, "hidden", translate("Hide Access Point"))
205                 hidden:depends({mode="ap"})
206                 hidden:depends({mode="ap-wds"})
207                 
208                 isolate = s:taboption("expert", Flag, "isolate", translate("Prevent communication between clients"))
209                 isolate:depends({mode="ap"})
210         end
211         
212         s:taboption("expert", Flag, "bursting", translate("Allow Burst Transmissions"))
213 elseif hwtype == "broadcom" then
214         if ap then
215                 hidden = s:taboption("expert", Flag, "hidden", translate("Hide Access Point"))
216                 hidden:depends({mode="ap"})
217                 hidden:depends({mode="wds"})
218         
219                 isolate = s:taboption("expert", Flag, "isolate", translate("Prevent communication between clients"))
220                 isolate:depends({mode="ap"})
221         end
222 elseif hwtype == "prism2" then
223         mp = s:taboption("expert", ListValue, "macpolicy", translate("MAC-Address Filter"))
224         mp:value("", translate("disable"))
225         mp:value("deny", translate("Allow listed only"))
226         mp:value("allow", translate("Allow all except listed"))
227         
228         ml = s:taboption("expert", DynamicList, "maclist", translate("MAC-List"))
229         ml:depends({macpolicy="allow"})
230         ml:depends({macpolicy="deny"})
231         
232         if ap then
233                 hidden = s:taboption("expert", Flag, "hidden", translate("Hide Access Point"))
234                 hidden:depends({mode="ap"})
235                 hidden:depends({mode="wds"})
236         end
237 end
238
239 -- Encryption --
240
241 encr.default = "wep" -- Early default
242 encr.override_values = true
243 encr.override_depends = true
244 encr:value("none", "No Encryption")
245 encr:value("wep", "WEP", {mode="ap"})
246
247 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
248         local hostapd = fs.access("/usr/sbin/hostapd") or os.getenv("LUCI_SYSROOT")
249
250         if hostapd then
251                 --s:taboption("expert", Flag, "_alloweap", "Allow EAP / 802.11i authentication")
252                 
253                 encr:value("psk", "WPA", {mode="ap"})
254                 encr:value("wpa", "WPA-EAP", {mode="ap"})
255                 encr:value("psk-mixed", "WPA + WPA2", {mode="ap"})
256                 encr:value("psk2", "WPA2", {mode="ap"})
257                 encr:value("wpa2", "WPA2-EAP (802.11i)", {mode="ap"})
258                 encr.default = "psk-mixed"
259         end
260 elseif hwtype == "broadcom" then
261         encr:value("psk", "WPA")
262         encr:value("psk+psk2", "WPA + WPA2")
263         encr:value("psk2", "WPA2")
264         encr.default = "psk+psk2"
265 end
266
267 server = s:taboption("general", Value, "server", translate("Radius-Server"))
268 server:depends({mode="ap", encryption="wpa"})
269 server:depends({mode="ap", encryption="wpa2"})
270 server.rmempty = true
271
272 port = s:taboption("general", Value, "port", translate("Radius-Port"))
273 port:depends({mode="ap", encryption="wpa"})
274 port:depends({mode="ap", encryption="wpa2"})
275 port.rmempty = true
276
277 key = s:taboption("general", Value, "key", translate("Password"))
278 key:depends("encryption", "wep")
279 key:depends("encryption", "psk")
280 key:depends("encryption", "psk2")
281 key:depends("encryption", "psk+psk2")
282 key:depends("encryption", "psk-mixed")
283 key:depends({mode="ap", encryption="wpa"})
284 key:depends({mode="ap", encryption="wpa2"})
285 key.rmempty = true
286 key.password = true
287
288 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
289         nasid = s:taboption("general", Value, "nasid", translate("NAS ID"))
290         nasid:depends({mode="ap", encryption="wpa"})
291         nasid:depends({mode="ap", encryption="wpa2"})
292         nasid.rmempty = true
293 end
294 return m