luci-0.9: merge r6035
[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         mode = s:option(ListValue, "hwmode", translate("mode"))
127         mode:value("11bg", "802.11b+g")
128         mode:value("11b", "802.11b")
129         mode:value("11g", "802.11g")
130         mode:value("11gst", "802.11g + Turbo")
131
132         mp = s:option(ListValue, "macfilter", translate("wifi_macpolicy"))
133         mp.optional = true
134         mp:value("")
135         mp:value("allow", translate("wifi_whitelist"))
136         mp:value("deny", translate("wifi_blacklist"))
137         ml = s:option(DynamicList, "maclist", translate("wifi_maclist"))
138         ml:depends({macfilter="allow"})
139         ml:depends({macfilter="deny"})
140
141         s:option(Value, "txantenna", translate("wifi_txantenna")).optional = true
142         s:option(Value, "rxantenna", translate("wifi_rxantenna")).optional = true
143
144         s:option(Flag, "frameburst", translate("wifi_bursting")).optional = true
145
146         s:option(Value, "distance", translate("wifi_distance")).optional = true
147         --s:option(Value, "slottime", translate("wifi_slottime")).optional = true
148
149         s:option(Value, "country", translate("wifi_country")).optional = true
150         s:option(Value, "maxassoc", translate("wifi_maxassoc")).optional = true
151 end
152
153
154 --------------------- HostAP Device ---------------------
155
156 if hwtype == "prism2" then
157         s:option(Value, "txpower", translate("a_w_txpwr"), "att units").rmempty = true
158
159         s:option(Flag, "diversity", translate("wifi_diversity")).rmempty = false
160
161         s:option(Value, "txantenna", translate("wifi_txantenna")).optional = true
162         s:option(Value, "rxantenna", translate("wifi_rxantenna")).optional = true
163 end
164
165
166 ----------------------- Interface -----------------------
167
168 s = m:section(TypedSection, "wifi-iface", translate("interfaces"))
169 s.addremove = true
170 s.anonymous = true
171 s:depends("device", arg[1])
172 s.defaults.device = arg[1]
173
174 s:option(Value, "ssid", translate("wifi_essid"))
175
176 network = s:option(Value, "network", translate("network"), translate("a_w_network1"))
177 network.rmempty = true
178 network:value("")
179 network.combobox_manual = translate("a_w_netmanual")
180 wa.cbi_add_networks(network)
181
182 function network.write(self, section, value)
183         if not m.uci:get("network", value) then
184                 -- avoid "value not defined in enum" because network is not known yet
185                 s.override_scheme = true
186
187                 m:chain("network")
188                 m.uci:set("network", value, "interface")
189                 Value.write(self, section, value)
190         else
191                 if m.uci:get("network", value) == "interface" then
192                         Value.write(self, section, value)
193                 end
194         end
195 end
196
197
198 mode = s:option(ListValue, "mode", translate("mode"))
199 mode.override_values = true
200 mode:value("ap", translate("a_w_ap"))
201 mode:value("adhoc", translate("a_w_adhoc"))
202 mode:value("sta", translate("a_w_client"))
203
204 bssid = s:option(Value, "bssid", translate("wifi_bssid"))
205
206
207 -------------------- MAC80211 Interface ----------------------
208
209 if hwtype == "mac80211" then
210         if fs.access("/usr/sbin/iw") then
211                 mode:value("mesh", "802.11s")
212         end
213
214         mode:value("monitor", translate("a_w_monitor"))
215         bssid:depends({mode="adhoc"})
216
217         mode:value("ap-wds", "%s (WDS)" % translate("a_w_ap"))
218         mode:value("sta-wds", "%s (WDS)" % translate("a_w_client"))             
219
220         function mode.write(self, section, value)
221                 if value == "ap-wds" then
222                         ListValue.write(self, section, "ap")
223                         m.uci:set("wireless", section, "wds", 1)
224                 elseif value == "sta-wds" then
225                         ListValue.write(self, section, "sta")
226                         m.uci:set("wireless", section, "wds", 1)
227                 else
228                         ListValue.write(self, section, value)
229                         m.uci:delete("wireless", section, "wds")
230                 end
231         end
232
233         function mode.cfgvalue(self, section)
234                 local mode = ListValue.cfgvalue(self, section)
235                 local wds  = m.uci:get("wireless", section, "wds") == "1"
236
237                 if mode == "ap" and wds then
238                         return "ap-wds"
239                 elseif mode == "sta" and wds then
240                         return "sta-wds"
241                 else
242                         return mode
243                 end
244         end
245                 
246         hidden = s:option(Flag, "hidden", translate("wifi_hidden"))
247         hidden:depends({mode="ap"})
248         hidden:depends({mode="ap-wds"})
249         hidden.optional = true
250
251         s:option(Value, "frag", translate("wifi_frag")).optional = true
252         s:option(Value, "rts", translate("wifi_rts")).optional = true
253 end
254
255
256
257 -------------------- Madwifi Interface ----------------------
258
259 if hwtype == "atheros" then
260         mode:value("ahdemo", translate("a_w_ahdemo"))
261         mode:value("monitor", translate("a_w_monitor"))
262
263         bssid:depends({mode="adhoc"})
264         bssid:depends({mode="ahdemo"})
265
266         mode:value("ap-wds", "%s (WDS)" % translate("a_w_ap"))
267         mode:value("sta-wds", "%s (WDS)" % translate("a_w_client"))             
268
269         function mode.write(self, section, value)
270                 if value == "ap-wds" then
271                         ListValue.write(self, section, "ap")
272                         m.uci:set("wireless", section, "wds", 1)
273                 elseif value == "sta-wds" then
274                         ListValue.write(self, section, "sta")
275                         m.uci:set("wireless", section, "wds", 1)
276                 else
277                         ListValue.write(self, section, value)
278                         m.uci:delete("wireless", section, "wds")
279                 end
280         end
281
282         function mode.cfgvalue(self, section)
283                 local mode = ListValue.cfgvalue(self, section)
284                 local wds  = m.uci:get("wireless", section, "wds") == "1"
285
286                 if mode == "ap" and wds then
287                         return "ap-wds"
288                 elseif mode == "sta" and wds then
289                         return "sta-wds"
290                 else
291                         return mode
292                 end
293         end
294
295         wdssep = s:option(Flag, "wdssep", translate("wifi_wdssep"))
296         wdssep:depends({mode="ap-wds"})
297         wdssep.optional = true
298
299         s:option(Flag, "doth", "802.11h").optional = true
300
301         hidden = s:option(Flag, "hidden", translate("wifi_hidden"))
302         hidden:depends({mode="ap"})
303         hidden:depends({mode="adhoc"})
304         hidden:depends({mode="wds"})
305         hidden:depends({mode="ap-wds"})
306         hidden.optional = true
307
308         isolate = s:option(Flag, "isolate", translate("wifi_isolate"),
309          translate("wifi_isolate_desc"))
310         isolate:depends({mode="ap"})
311         isolate.optional = true
312         s:option(Flag, "bgscan", translate("wifi_bgscan")).optional = true
313
314         mp = s:option(ListValue, "macpolicy", translate("wifi_macpolicy"))
315         mp.optional = true
316         mp:value("")
317         mp:value("allow", translate("wifi_whitelist"))
318         mp:value("deny", translate("wifi_blacklist"))
319         ml = s:option(DynamicList, "maclist", translate("wifi_maclist"))
320         ml:depends({macpolicy="allow"})
321         ml:depends({macpolicy="deny"})
322
323         s:option(Value, "rate", translate("wifi_rate")).optional = true
324         s:option(Value, "mcast_rate", translate("wifi_mcast_rate")).optional = true
325         s:option(Value, "frag", translate("wifi_frag")).optional = true
326         s:option(Value, "rts", translate("wifi_rts")).optional = true
327         s:option(Value, "minrate", translate("wifi_minrate")).optional = true
328         s:option(Value, "maxrate", translate("wifi_maxrate")).optional = true
329         s:option(Flag, "compression", translate("wifi_compression")).optional = true
330
331         s:option(Flag, "bursting", translate("wifi_bursting")).optional = true
332         s:option(Flag, "turbo", translate("wifi_turbo")).optional = true
333         s:option(Flag, "ff", translate("wifi_ff")).optional = true
334
335         s:option(Flag, "wmm", translate("wifi_wmm")).optional = true
336         s:option(Flag, "xr", translate("wifi_xr")).optional = true
337         s:option(Flag, "ar", translate("wifi_ar")).optional = true
338
339         local swm = s:option(Flag, "sw_merge", translate("wifi_nosbeacon"))
340         swm:depends({mode="adhoc"})
341         swm.optional = true
342
343         local nos = s:option(Flag, "nosbeacon", translate("wifi_nosbeacon"))
344         nos:depends({mode="sta"})
345         nos.optional = true
346
347         local probereq = s:option(Flag, "probereq", translate("wifi_noprobereq"))
348         probereq.optional = true
349         probereq.enabled  = "0"
350         probereq.disabled = "1"
351 end
352
353
354 -------------------- Broadcom Interface ----------------------
355
356 if hwtype == "broadcom" then
357         mode:value("wds", translate("a_w_wds"))
358         mode:value("monitor", translate("a_w_monitor"))
359
360         hidden = s:option(Flag, "hidden", translate("wifi_hidden"))
361         hidden:depends({mode="ap"})
362         hidden:depends({mode="adhoc"})
363         hidden:depends({mode="wds"})
364         hidden.optional = true
365
366         isolate = s:option(Flag, "isolate", translate("wifi_isolate"),
367          translate("wifi_isolate_desc"))
368         isolate:depends({mode="ap"})
369         isolate.optional = true
370
371         bssid:depends({mode="wds"})
372         bssid:depends({mode="adhoc"})
373 end
374
375
376 ----------------------- HostAP Interface ---------------------
377
378 if hwtype == "prism2" then
379         mode:value("wds", translate("a_w_wds"))
380         mode:value("monitor", translate("a_w_monitor"))
381
382         hidden = s:option(Flag, "hidden", translate("wifi_hidden"))
383         hidden:depends({mode="ap"})
384         hidden:depends({mode="adhoc"})
385         hidden:depends({mode="wds"})
386         hidden.optional = true
387
388         bssid:depends({mode="sta"})
389
390         mp = s:option(ListValue, "macpolicy", translate("wifi_macpolicy"))
391         mp.optional = true
392         mp:value("")
393         mp:value("allow", translate("wifi_whitelist"))
394         mp:value("deny", translate("wifi_blacklist"))
395         ml = s:option(DynamicList, "maclist", translate("wifi_maclist"))
396         ml:depends({macpolicy="allow"})
397         ml:depends({macpolicy="deny"})
398
399         s:option(Value, "rate", translate("wifi_rate")).optional = true
400         s:option(Value, "frag", translate("wifi_frag")).optional = true
401         s:option(Value, "rts", translate("wifi_rts")).optional = true
402 end
403
404
405 ------------------- WiFI-Encryption -------------------
406
407 encr = s:option(ListValue, "encryption", translate("encryption"))
408 encr.override_values = true
409 encr:depends({mode="ap"})
410 encr:depends({mode="sta"})
411 encr:depends({mode="adhoc"})
412 encr:depends({mode="ahdemo"})
413 encr:depends({mode="ap-wds"})
414 encr:depends({mode="sta-wds"})
415 encr:depends({mode="wds"})
416 encr:depends({mode="mesh"})
417
418 encr:value("none", "No Encryption")
419 encr:value("wep", "WEP")
420
421 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
422         local supplicant = fs.access("/usr/sbin/wpa_supplicant")
423         local hostapd = fs.access("/usr/sbin/hostapd")
424
425         if hostapd and supplicant then
426                 encr:value("psk", "WPA-PSK")
427                 encr:value("psk2", "WPA2-PSK")
428                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode")
429                 encr:value("wpa", "WPA-EAP", {mode="ap"}, {mode="sta"})
430                 encr:value("wpa2", "WPA2-EAP", {mode="ap"}, {mode="sta"})
431         elseif hostapd and not supplicant then
432                 encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="adhoc"}, {mode="ahdemo"})
433                 encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="adhoc"}, {mode="ahdemo"})
434                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="ap"}, {mode="adhoc"}, {mode="ahdemo"})
435                 encr:value("wpa", "WPA-EAP", {mode="ap"})
436                 encr:value("wpa2", "WPA2-EAP", {mode="ap"})
437                 encr.description = translate("wifi_wpareq")
438         elseif not hostapd and supplicant then
439                 encr:value("psk", "WPA-PSK", {mode="sta"})
440                 encr:value("psk2", "WPA2-PSK", {mode="sta"})
441                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="sta"})
442                 encr:value("wpa", "WPA-EAP", {mode="sta"})
443                 encr:value("wpa2", "WPA2-EAP", {mode="sta"})
444                 encr.description = translate("wifi_wpareq")
445         else
446                 encr.description = translate("wifi_wpareq")
447         end
448 elseif hwtype == "broadcom" then
449         encr:value("psk", "WPA-PSK")
450         encr:value("psk2", "WPA2-PSK")
451         encr:value("psk+psk2", "WPA-PSK/WPA2-PSK Mixed Mode")
452 end
453
454 encr:depends("mode", "ap")
455 encr:depends("mode", "sta")
456 encr:depends("mode", "wds")
457
458 server = s:option(Value, "server", translate("a_w_radiussrv"))
459 server:depends({mode="ap", encryption="wpa"})
460 server:depends({mode="ap", encryption="wpa2"})
461 server.rmempty = true
462
463 port = s:option(Value, "port", translate("a_w_radiusport"))
464 port:depends({mode="ap", encryption="wpa"})
465 port:depends({mode="ap", encryption="wpa2"})
466 port.rmempty = true
467
468 key = s:option(Value, "key", translate("key"))
469 key:depends("encryption", "wep")
470 key:depends("encryption", "psk")
471 key:depends("encryption", "psk2")
472 key:depends("encryption", "psk+psk2")
473 key:depends("encryption", "psk-mixed")
474 key:depends({mode="ap", encryption="wpa"})
475 key:depends({mode="ap", encryption="wpa2"})
476 key.rmempty = true
477 key.password = true
478
479 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
480         nasid = s:option(Value, "nasid", translate("a_w_nasid"))
481         nasid:depends({mode="ap", encryption="wpa"})
482         nasid:depends({mode="ap", encryption="wpa2"})
483         nasid.rmempty = true
484
485         eaptype = s:option(ListValue, "eap_type", translate("a_w_eaptype"))
486         eaptype:value("TLS")
487         eaptype:value("TTLS")
488         eaptype:value("PEAP")
489         eaptype:depends({mode="sta", encryption="wpa"})
490         eaptype:depends({mode="sta", encryption="wpa2"})
491
492         cacert = s:option(FileUpload, "ca_cert", translate("a_w_cacert"))
493         cacert:depends({mode="sta", encryption="wpa"})
494         cacert:depends({mode="sta", encryption="wpa2"})
495
496         privkey = s:option(FileUpload, "priv_key", translate("a_w_tlsprivkey"))
497         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
498         privkey:depends({mode="sta", eap_type="TLS", encryption="wpa"})
499
500         privkeypwd = s:option(Value, "priv_key_pwd", translate("a_w_tlsprivkeypwd"))
501         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
502         privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa"})
503
504
505         auth = s:option(Value, "auth", translate("a_w_peapauth"))
506         auth:value("PAP")
507         auth:value("CHAP")
508         auth:value("MSCHAP")
509         auth:value("MSCHAPV2")
510         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
511         auth:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
512         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
513         auth:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
514
515
516         identity = s:option(Value, "identity", translate("a_w_peapidentity"))
517         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
518         identity:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
519         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
520         identity:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
521
522         password = s:option(Value, "password", translate("a_w_peappassword"))
523         password:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
524         password:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
525         password:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
526         password:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
527 end
528
529
530 return m