build: split into luci and luci-addons packages
[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 local fs = require "nixio.fs"
20 local sys = require "luci.sys"
21 local cursor = require "luci.model.uci".inst
22 local state = require "luci.model.uci".inst_state
23 cursor:unload("wireless")
24
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 = {}
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 = iw and iw.freqlist or { }
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
179
180 local ssid = s:taboption("general", Value, "ssid", translate("Network Name (<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>)"))
181
182 mode = s:taboption("expert", ListValue, "mode", translate("Operating Mode"))
183 mode.override_values = true
184 mode:value("ap", translate("Access Point"))
185
186 encr = s:taboption("expert", ListValue, "encryption", translate("Encryption"))
187
188
189 if hwtype == "mac80211" then
190         mode:value("mesh", translate("Mesh (802.11s)"))
191         local meshid = s:taboption("expert", Value, "mesh_id", translate("Mesh ID"))
192         meshid:depends("mode", "mesh")
193
194         s:taboption("expert", Flag, "wds", translate("Enable Bridging and Repeating (WDS)")):depends("mode", "ap")
195         s:taboption("expert", Flag, "powersave", translate("Enable Powersaving")):depends("mode", "ap")
196 elseif hwtype == "atheros" then
197         -- mode:value("wds", translate("Static WDS"))
198         
199         mp = s:taboption("expert", ListValue, "macpolicy", translate("MAC-Address Filter"))
200         mp:value("", translate("disable"))
201         mp:value("deny", translate("Allow listed only"))
202         mp:value("allow", translate("Allow all except listed"))
203         ml = s:taboption("expert", DynamicList, "maclist", translate("MAC-List"))
204         ml:depends({macpolicy="allow"})
205         ml:depends({macpolicy="deny"})
206
207         s:taboption("expert", Flag, "wds", translate("Enable Bridging and Repeating (WDS)"))
208                 
209         if ap then                              
210                 hidden = s:taboption("expert", Flag, "hidden", translate("Hide Access Point"))
211                 hidden:depends({mode="ap"})
212                 hidden:depends({mode="ap-wds"})
213                 
214                 isolate = s:taboption("expert", Flag, "isolate", translate("Prevent communication between clients"))
215                 isolate:depends({mode="ap"})
216         end
217         
218         s:taboption("expert", Flag, "bursting", translate("Allow Burst Transmissions"))
219 elseif hwtype == "broadcom" then
220         if ap then
221                 hidden = s:taboption("expert", Flag, "hidden", translate("Hide Access Point"))
222                 hidden:depends({mode="ap"})
223                 hidden:depends({mode="wds"})
224         
225                 isolate = s:taboption("expert", Flag, "isolate", translate("Prevent communication between clients"))
226                 isolate:depends({mode="ap"})
227         end
228 elseif hwtype == "prism2" then
229         mp = s:taboption("expert", ListValue, "macpolicy", translate("MAC-Address Filter"))
230         mp:value("", translate("disable"))
231         mp:value("deny", translate("Allow listed only"))
232         mp:value("allow", translate("Allow all except listed"))
233         
234         ml = s:taboption("expert", DynamicList, "maclist", translate("MAC-List"))
235         ml:depends({macpolicy="allow"})
236         ml:depends({macpolicy="deny"})
237         
238         if ap then
239                 hidden = s:taboption("expert", Flag, "hidden", translate("Hide Access Point"))
240                 hidden:depends({mode="ap"})
241                 hidden:depends({mode="wds"})
242         end
243 end
244
245 -- Encryption --
246
247 encr.default = "wep" -- Early default
248 encr.override_values = true
249 encr.override_depends = true
250 encr:value("none", "No Encryption")
251 encr:value("wep", "WEP", {mode="ap"})
252
253 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
254         local hostapd = fs.access("/usr/sbin/hostapd") or os.getenv("LUCI_SYSROOT")
255         local supplicant = fs.access("/usr/sbin/wpa_supplicant") or os.getenv("LUCI_SYSROOT")
256
257         if hostapd and not supplicant then              
258                 encr:value("psk", "WPA", {mode="ap"})
259                 encr:value("wpa", "WPA-EAP", {mode="ap"})
260                 encr:value("psk-mixed", "WPA + WPA2", {mode="ap"})
261                 encr:value("psk2", "WPA2", {mode="ap"})
262                 encr:value("wpa2", "WPA2-EAP (802.11i)", {mode="ap"})
263                 encr.default = "psk-mixed"
264         elseif not hostapd and supplicant then
265                 encr:value("psk", "WPA", {mode="mesh"})
266                 encr:value("psk2", "WPA2", {mode="mesh"})
267                 encr.default = "psk2"
268         elseif hostapd and supplicant then
269                 encr:value("psk", "WPA", {mode="ap"}, {mode="mesh"})
270                 encr:value("wpa", "WPA-EAP", {mode="ap"})
271                 encr:value("psk-mixed", "WPA + WPA2", {mode="ap"})
272                 encr:value("psk2", "WPA2", {mode="ap"}, {mode="mesh"})
273                 encr:value("wpa2", "WPA2-EAP (802.11i)", {mode="ap"})
274                 encr.default = "psk-mixed"              
275         end
276 elseif hwtype == "broadcom" then
277         encr:value("psk", "WPA")
278         encr:value("psk+psk2", "WPA + WPA2")
279         encr:value("psk2", "WPA2")
280         encr.default = "psk+psk2"
281 end
282
283 server = s:taboption("general", Value, "server", translate("Radius-Server"))
284 server:depends({mode="ap", encryption="wpa"})
285 server:depends({mode="ap", encryption="wpa2"})
286 server.rmempty = true
287
288 port = s:taboption("general", Value, "port", translate("Radius-Port"))
289 port:depends({mode="ap", encryption="wpa"})
290 port:depends({mode="ap", encryption="wpa2"})
291 port.rmempty = true
292
293 key = s:taboption("general", Value, "key", translate("Password"))
294 key:depends("encryption", "wep")
295 key:depends("encryption", "psk")
296 key:depends("encryption", "psk2")
297 key:depends("encryption", "psk+psk2")
298 key:depends("encryption", "psk-mixed")
299 key:depends({mode="ap", encryption="wpa"})
300 key:depends({mode="ap", encryption="wpa2"})
301 key.rmempty = true
302 key.password = true
303
304 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
305         nasid = s:taboption("general", Value, "nasid", translate("NAS ID"))
306         nasid:depends({mode="ap", encryption="wpa"})
307         nasid:depends({mode="ap", encryption="wpa2"})
308         nasid.rmempty = true
309 end
310 return m