luci-0.9: merge r5782
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / wifi.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 local wa = require "luci.tools.webadmin"
16 local fs = require "nixio.fs"
17
18 arg[1] = arg[1] or ""
19
20 m = Map("wireless", translate("networks"), translate("a_w_networks1"))
21
22 s = m:section(NamedSection, arg[1], "wifi-device", translate("device") .. " " .. arg[1])
23 s.addremove = false
24
25 back = s:option(DummyValue, "_overview", translate("overview"))
26 back.value = ""
27 back.titleref = luci.dispatcher.build_url("admin", "network", "wireless")
28
29
30 en = s:option(Flag, "disabled", translate("enable"))
31 en.enabled = "0"
32 en.disabled = "1"
33 en.rmempty = false
34
35 function en.cfgvalue(self, section)
36         return Flag.cfgvalue(self, section) or "0"
37 end
38
39 s:option(DummyValue, "type", translate("type"))
40 local hwtype = m:get(arg[1], "type")
41 -- NanoFoo
42 local nsantenna = m:get(arg[1], "antenna")
43
44 ch = s:option(Value, "channel", translate("a_w_channel"))
45 ch:value("auto", translate("wifi_auto"))
46 for c, f in luci.util.kspairs(luci.sys.wifi.channels()) do
47         ch:value(c, "%i (%.3f GHz)" %{ c, f })
48 end
49
50
51 ------------------- MAC80211 Device ------------------
52
53 if hwtype == "mac80211" then
54         s:option(Value, "txpower", translate("a_w_txpwr"), "dBm").rmempty = true
55
56         mode = s:option(ListValue, "hwmode", translate("mode"))
57         mode:value("", translate("auto"))
58         mode:value("11b", "802.11b")
59         mode:value("11g", "802.11g")
60         mode:value("11a", "802.11a")
61         mode:value("11ng", "802.11g+n")
62         mode:value("11na", "802.11a+n")
63
64         htmode = s:option(ListValue, "htmode", translate("wifi_htmode", "HT mode"))
65         htmode:depends("hwmode", "11na")
66         htmode:depends("hwmode", "11ng")
67         htmode:value("HT20", "20MHz")
68         htmode:value("HT40-", translate("wifi_ht40m", "40MHz - 2nd channel below"))
69         htmode:value("HT40+", translate("wifi_ht40p", "40MHz - 2nd channel above"))
70         
71         --htcapab = s:option(DynamicList, "ht_capab", translate("wifi_htcapab", "HT capabilities"))
72         --htcapab:depends("hwmode", "11na")
73         --htcapab:depends("hwmode", "11ng")
74
75         s:option(Value, "country", translate("wifi_country", "Country Code"),
76                 translate("wifi_country_iso3166", "Use ISO/IEC 3166 alpha2 country codes.")).optional = true
77
78         s:option(Value, "distance", translate("wifi_distance", "Distance Optimization"),
79                 translate("wifi_distance_desc", "Distance to farthest network member in meters.")).optional = true
80 end
81
82
83 ------------------- Madwifi Device ------------------
84
85 if hwtype == "atheros" then
86         s:option(Value, "txpower", translate("a_w_txpwr"), "dBm").rmempty = true
87
88         mode = s:option(ListValue, "hwmode", translate("mode"))
89         mode:value("", translate("wifi_auto"))
90         mode:value("11b", "802.11b")
91         mode:value("11g", "802.11g")
92         mode:value("11a", "802.11a")
93         mode:value("11bg", "802.11b+g")
94         mode:value("11gst", "802.11g + Turbo")
95         mode:value("11ast", "802.11a + Turbo")
96         mode:value("fh", translate("wifi_fh"))
97
98         s:option(Flag, "diversity", translate("wifi_diversity")).rmempty = false
99
100         if not nsantenna then
101                 s:option(Value, "txantenna", translate("wifi_txantenna")).optional = true
102                 s:option(Value, "rxantenna", translate("wifi_rxantenna")).optional = true
103         else -- NanoFoo
104                 local ant = s:option(ListValue, "antenna", translate("wifi_txantenna"))
105                 ant:value("auto")
106                 ant:value("vertical")
107                 ant:value("horizontal")
108                 ant:value("external")
109         end
110         s:option(Value, "distance", translate("wifi_distance"),
111                 translate("wifi_distance_desc")).optional = true
112         s:option(Value, "regdomain", translate("wifi_regdomain")).optional = true
113         s:option(Value, "country", translate("wifi_country")).optional = true
114         s:option(Flag, "outdoor", translate("wifi_outdoor")).optional = true
115
116         --s:option(Flag, "nosbeacon", translate("wifi_nosbeacon")).optional = true
117 end
118
119
120
121 ------------------- Broadcom Device ------------------
122
123 if hwtype == "broadcom" then
124         s:option(Value, "txpower", translate("a_w_txpwr"), "dBm").rmempty = true
125
126         mp = s:option(ListValue, "macfilter", translate("wifi_macpolicy"))
127         mp.optional = true
128         mp:value("")
129         mp:value("allow", translate("wifi_whitelist"))
130         mp:value("deny", translate("wifi_blacklist"))
131         ml = s:option(DynamicList, "maclist", translate("wifi_maclist"))
132         ml:depends({macfilter="allow"})
133         ml:depends({macfilter="deny"})
134
135         s:option(Value, "txantenna", translate("wifi_txantenna")).optional = true
136         s:option(Value, "rxantenna", translate("wifi_rxantenna")).optional = true
137
138         s:option(Flag, "frameburst", translate("wifi_bursting")).optional = true
139
140         s:option(Value, "distance", translate("wifi_distance")).optional = true
141         --s:option(Value, "slottime", translate("wifi_slottime")).optional = true
142
143         s:option(Value, "country", translate("wifi_country")).optional = true
144         s:option(Value, "maxassoc", translate("wifi_maxassoc")).optional = true
145 end
146
147
148 --------------------- HostAP Device ---------------------
149
150 if hwtype == "prism2" then
151         s:option(Value, "txpower", translate("a_w_txpwr"), "att units").rmempty = true
152
153         s:option(Flag, "diversity", translate("wifi_diversity")).rmempty = false
154
155         s:option(Value, "txantenna", translate("wifi_txantenna")).optional = true
156         s:option(Value, "rxantenna", translate("wifi_rxantenna")).optional = true
157 end
158
159
160 ----------------------- Interface -----------------------
161
162 s = m:section(TypedSection, "wifi-iface", translate("interfaces"))
163 s.addremove = true
164 s.anonymous = true
165 s:depends("device", arg[1])
166 s.defaults.device = arg[1]
167
168 s:option(Value, "ssid", translate("wifi_essid"))
169
170 network = s:option(Value, "network", translate("network"), translate("a_w_network1"))
171 network.rmempty = true
172 network:value("")
173 network.combobox_manual = translate("a_w_netmanual")
174 wa.cbi_add_networks(network)
175
176 function network.write(self, section, value)
177         if not m.uci:get("network", value) then
178                 -- avoid "value not defined in enum" because network is not known yet
179                 s.override_scheme = true
180
181                 m:chain("network")
182                 m.uci:set("network", value, "interface")
183                 Value.write(self, section, value)
184         else
185                 if m.uci:get("network", value) == "interface" then
186                         Value.write(self, section, value)
187                 end
188         end
189 end
190
191
192 mode = s:option(ListValue, "mode", translate("mode"))
193 mode.override_values = true
194 mode:value("ap", translate("a_w_ap"))
195 mode:value("adhoc", translate("a_w_adhoc"))
196 mode:value("sta", translate("a_w_client"))
197
198 bssid = s:option(Value, "bssid", translate("wifi_bssid"))
199
200
201 -------------------- MAC80211 Interface ----------------------
202
203 if hwtype == "mac80211" then
204         if fs.access("/usr/sbin/iw") then
205                 mode:value("mesh", "802.11s")
206         end
207
208         mode:value("monitor", translate("a_w_monitor"))
209         bssid:depends({mode="adhoc"})
210
211         mode:value("ap-wds", "%s (WDS)" % translate("a_w_ap"))
212         mode:value("sta-wds", "%s (WDS)" % translate("a_w_client"))             
213
214         function mode.write(self, section, value)
215                 if value == "ap-wds" then
216                         ListValue.write(self, section, "ap")
217                         m.uci:set("wireless", section, "wds", 1)
218                 elseif value == "sta-wds" then
219                         ListValue.write(self, section, "sta")
220                         m.uci:set("wireless", section, "wds", 1)
221                 else
222                         ListValue.write(self, section, value)
223                         m.uci:delete("wireless", section, "wds")
224                 end
225         end
226
227         function mode.cfgvalue(self, section)
228                 local mode = ListValue.cfgvalue(self, section)
229                 local wds  = m.uci:get("wireless", section, "wds") == "1"
230
231                 if mode == "ap" and wds then
232                         return "ap-wds"
233                 elseif mode == "sta" and wds then
234                         return "sta-wds"
235                 else
236                         return mode
237                 end
238         end
239                 
240         hidden = s:option(Flag, "hidden", translate("wifi_hidden"))
241         hidden:depends({mode="ap"})
242         hidden:depends({mode="ap-wds"})
243         hidden.optional = true
244
245         s:option(Value, "frag", translate("wifi_frag")).optional = true
246         s:option(Value, "rts", translate("wifi_rts")).optional = true
247 end
248
249
250
251 -------------------- Madwifi Interface ----------------------
252
253 if hwtype == "atheros" then
254         mode:value("ahdemo", translate("a_w_ahdemo"))
255         mode:value("monitor", translate("a_w_monitor"))
256
257         bssid:depends({mode="adhoc"})
258         bssid:depends({mode="ahdemo"})
259
260         mode:value("ap-wds", "%s (WDS)" % translate("a_w_ap"))
261         mode:value("sta-wds", "%s (WDS)" % translate("a_w_client"))             
262
263         function mode.write(self, section, value)
264                 if value == "ap-wds" then
265                         ListValue.write(self, section, "ap")
266                         m.uci:set("wireless", section, "wds", 1)
267                 elseif value == "sta-wds" then
268                         ListValue.write(self, section, "sta")
269                         m.uci:set("wireless", section, "wds", 1)
270                 else
271                         ListValue.write(self, section, value)
272                         m.uci:delete("wireless", section, "wds")
273                 end
274         end
275
276         function mode.cfgvalue(self, section)
277                 local mode = ListValue.cfgvalue(self, section)
278                 local wds  = m.uci:get("wireless", section, "wds") == "1"
279
280                 if mode == "ap" and wds then
281                         return "ap-wds"
282                 elseif mode == "sta" and wds then
283                         return "sta-wds"
284                 else
285                         return mode
286                 end
287         end
288
289         wdssep = s:option(Flag, "wdssep", translate("wifi_wdssep"))
290         wdssep:depends({mode="ap-wds"})
291         wdssep.optional = true
292
293         s:option(Flag, "doth", "802.11h").optional = true
294
295         hidden = s:option(Flag, "hidden", translate("wifi_hidden"))
296         hidden:depends({mode="ap"})
297         hidden:depends({mode="adhoc"})
298         hidden:depends({mode="wds"})
299         hidden:depends({mode="ap-wds"})
300         hidden.optional = true
301
302         isolate = s:option(Flag, "isolate", translate("wifi_isolate"),
303          translate("wifi_isolate_desc"))
304         isolate:depends({mode="ap"})
305         isolate.optional = true
306         s:option(Flag, "bgscan", translate("wifi_bgscan")).optional = true
307
308         mp = s:option(ListValue, "macpolicy", translate("wifi_macpolicy"))
309         mp.optional = true
310         mp:value("")
311         mp:value("allow", translate("wifi_whitelist"))
312         mp:value("deny", translate("wifi_blacklist"))
313         ml = s:option(DynamicList, "maclist", translate("wifi_maclist"))
314         ml:depends({macpolicy="allow"})
315         ml:depends({macpolicy="deny"})
316
317         s:option(Value, "rate", translate("wifi_rate")).optional = true
318         s:option(Value, "mcast_rate", translate("wifi_mcast_rate")).optional = true
319         s:option(Value, "frag", translate("wifi_frag")).optional = true
320         s:option(Value, "rts", translate("wifi_rts")).optional = true
321         s:option(Value, "minrate", translate("wifi_minrate")).optional = true
322         s:option(Value, "maxrate", translate("wifi_maxrate")).optional = true
323         s:option(Flag, "compression", translate("wifi_compression")).optional = true
324
325         s:option(Flag, "bursting", translate("wifi_bursting")).optional = true
326         s:option(Flag, "turbo", translate("wifi_turbo")).optional = true
327         s:option(Flag, "ff", translate("wifi_ff")).optional = true
328
329         s:option(Flag, "wmm", translate("wifi_wmm")).optional = true
330         s:option(Flag, "xr", translate("wifi_xr")).optional = true
331         s:option(Flag, "ar", translate("wifi_ar")).optional = true
332
333         local swm = s:option(Flag, "sw_merge", translate("wifi_nosbeacon"))
334         swm:depends({mode="adhoc"})
335         swm.optional = true
336
337         local nos = s:option(Flag, "nosbeacon", translate("wifi_nosbeacon"))
338         nos:depends({mode="sta"})
339         nos.optional = true
340
341         local probereq = s:option(Flag, "probereq", translate("wifi_noprobereq"))
342         probereq.optional = true
343         probereq.enabled  = "0"
344         probereq.disabled = "1"
345 end
346
347
348 -------------------- Broadcom Interface ----------------------
349
350 if hwtype == "broadcom" then
351         mode:value("wds", translate("a_w_wds"))
352         mode:value("monitor", translate("a_w_monitor"))
353
354         hidden = s:option(Flag, "hidden", translate("wifi_hidden"))
355         hidden:depends({mode="ap"})
356         hidden:depends({mode="adhoc"})
357         hidden:depends({mode="wds"})
358         hidden.optional = true
359
360         isolate = s:option(Flag, "isolate", translate("wifi_isolate"),
361          translate("wifi_isolate_desc"))
362         isolate:depends({mode="ap"})
363         isolate.optional = true
364
365         bssid:depends({mode="wds"})
366         bssid:depends({mode="adhoc"})
367 end
368
369
370 ----------------------- HostAP Interface ---------------------
371
372 if hwtype == "prism2" then
373         mode:value("wds", translate("a_w_wds"))
374         mode:value("monitor", translate("a_w_monitor"))
375
376         hidden = s:option(Flag, "hidden", translate("wifi_hidden"))
377         hidden:depends({mode="ap"})
378         hidden:depends({mode="adhoc"})
379         hidden:depends({mode="wds"})
380         hidden.optional = true
381
382         bssid:depends({mode="sta"})
383
384         mp = s:option(ListValue, "macpolicy", translate("wifi_macpolicy"))
385         mp.optional = true
386         mp:value("")
387         mp:value("allow", translate("wifi_whitelist"))
388         mp:value("deny", translate("wifi_blacklist"))
389         ml = s:option(DynamicList, "maclist", translate("wifi_maclist"))
390         ml:depends({macpolicy="allow"})
391         ml:depends({macpolicy="deny"})
392
393         s:option(Value, "rate", translate("wifi_rate")).optional = true
394         s:option(Value, "frag", translate("wifi_frag")).optional = true
395         s:option(Value, "rts", translate("wifi_rts")).optional = true
396 end
397
398
399 ------------------- WiFI-Encryption -------------------
400
401 encr = s:option(ListValue, "encryption", translate("encryption"))
402 encr.override_values = true
403 encr:depends({mode="ap"})
404 encr:depends({mode="sta"})
405 encr:depends({mode="adhoc"})
406 encr:depends({mode="ahdemo"})
407 encr:depends({mode="ap-wds"})
408 encr:depends({mode="sta-wds"})
409 encr:depends({mode="wds"})
410 encr:depends({mode="mesh"})
411
412 encr:value("none", "No Encryption")
413 encr:value("wep", "WEP")
414
415 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
416         local supplicant = fs.access("/usr/sbin/wpa_supplicant")
417         local hostapd = fs.access("/usr/sbin/hostapd")
418
419         if hostapd and supplicant then
420                 encr:value("psk", "WPA-PSK")
421                 encr:value("psk2", "WPA2-PSK")
422                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode")
423                 encr:value("wpa", "WPA-EAP", {mode="ap"}, {mode="sta"})
424                 encr:value("wpa2", "WPA2-EAP", {mode="ap"}, {mode="sta"})
425         elseif hostapd and not supplicant then
426                 encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="adhoc"}, {mode="ahdemo"})
427                 encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="adhoc"}, {mode="ahdemo"})
428                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="ap"}, {mode="adhoc"}, {mode="ahdemo"})
429                 encr:value("wpa", "WPA-EAP", {mode="ap"})
430                 encr:value("wpa2", "WPA2-EAP", {mode="ap"})
431                 encr.description = translate("wifi_wpareq")
432         elseif not hostapd and supplicant then
433                 encr:value("psk", "WPA-PSK", {mode="sta"})
434                 encr:value("psk2", "WPA2-PSK", {mode="sta"})
435                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="sta"})
436                 encr:value("wpa", "WPA-EAP", {mode="sta"})
437                 encr:value("wpa2", "WPA2-EAP", {mode="sta"})
438                 encr.description = translate("wifi_wpareq")
439         else
440                 encr.description = translate("wifi_wpareq")
441         end
442 elseif hwtype == "broadcom" then
443         encr:value("psk", "WPA-PSK")
444         encr:value("psk2", "WPA2-PSK")
445         encr:value("psk+psk2", "WPA-PSK/WPA2-PSK Mixed Mode")
446 end
447
448 encr:depends("mode", "ap")
449 encr:depends("mode", "sta")
450 encr:depends("mode", "wds")
451
452 server = s:option(Value, "server", translate("a_w_radiussrv"))
453 server:depends({mode="ap", encryption="wpa"})
454 server:depends({mode="ap", encryption="wpa2"})
455 server.rmempty = true
456
457 port = s:option(Value, "port", translate("a_w_radiusport"))
458 port:depends({mode="ap", encryption="wpa"})
459 port:depends({mode="ap", encryption="wpa2"})
460 port.rmempty = true
461
462 key = s:option(Value, "key", translate("key"))
463 key:depends("encryption", "wep")
464 key:depends("encryption", "psk")
465 key:depends("encryption", "psk2")
466 key:depends("encryption", "psk+psk2")
467 key:depends("encryption", "psk-mixed")
468 key:depends({mode="ap", encryption="wpa"})
469 key:depends({mode="ap", encryption="wpa2"})
470 key.rmempty = true
471 key.password = true
472
473 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
474         nasid = s:option(Value, "nasid", translate("a_w_nasid"))
475         nasid:depends({mode="ap", encryption="wpa"})
476         nasid:depends({mode="ap", encryption="wpa2"})
477         nasid.rmempty = true
478
479         eaptype = s:option(ListValue, "eap_type", translate("a_w_eaptype"))
480         eaptype:value("TLS")
481         eaptype:value("TTLS")
482         eaptype:value("PEAP")
483         eaptype:depends({mode="sta", encryption="wpa"})
484         eaptype:depends({mode="sta", encryption="wpa2"})
485
486         cacert = s:option(FileUpload, "ca_cert", translate("a_w_cacert"))
487         cacert:depends({mode="sta", encryption="wpa"})
488         cacert:depends({mode="sta", encryption="wpa2"})
489
490         privkey = s:option(FileUpload, "priv_key", translate("a_w_tlsprivkey"))
491         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
492         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa"})
493
494         privkeypwd = s:option(Value, "priv_key_pwd", translate("a_w_tlsprivkeypwd"))
495         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
496         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa"})
497
498
499         auth = s:option(Value, "auth", translate("a_w_peapauth"))
500         auth:value("PAP")
501         auth:value("CHAP")
502         auth:value("MSCHAP")
503         auth:value("MSCHAPV2")
504         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
505         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
506         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
507         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
508
509
510         identity = s:option(Value, "identity", translate("a_w_peapidentity"))
511         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
512         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
513         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
514         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
515
516         password = s:option(Value, "password", translate("a_w_peappassword"))
517         password:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
518         password:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
519         password:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
520         password:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
521 end
522
523
524 return m