modules/admin-full: decouple enable/disable button state from assoc state
[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 nw = require "luci.model.network"
17 local ut = require "luci.util"
18 local nt = require "luci.sys".net
19 local fs = require "nixio.fs"
20
21 arg[1] = arg[1] or ""
22
23 m = Map("wireless", "",
24         translate("The <em>Device Configuration</em> section covers physical settings of the radio " ..
25                 "hardware such as channel, transmit power or antenna selection which are shared among all " ..
26                 "defined wireless networks (if the radio hardware is multi-SSID capable). Per network settings " ..
27                 "like encryption or operation mode are grouped in the <em>Interface Configuration</em>."))
28
29 m:chain("network")
30 m:chain("firewall")
31
32 local ifsection
33
34 function m.on_commit(map)
35         local wnet = nw:get_wifinet(arg[1])
36         if ifsection and wnet then
37                 ifsection.section = wnet.sid
38                 m.title = luci.util.pcdata(wnet:get_i18n())
39         end
40 end
41
42 nw.init(m.uci)
43
44 local wnet = nw:get_wifinet(arg[1])
45 local wdev = wnet and wnet:get_device()
46
47 -- redirect to overview page if network does not exist anymore (e.g. after a revert)
48 if not wnet or not wdev then
49         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
50         return
51 end
52
53 -- wireless toggle was requested, commit and reload page
54 function m.parse(map)
55         if m:formvalue("cbid.wireless.%s.__toggle" % wdev:name()) then
56                 if wdev:get("disabled") == "1" or wnet:get("disabled") == "1" then
57                         wnet:set("disabled", nil)
58                 else
59                         wnet:set("disabled", "1")
60                 end
61                 wdev:set("disabled", nil)
62
63                 nw:commit("wireless")
64                 luci.sys.call("(env -i /bin/ubus call network reload) >/dev/null 2>/dev/null")
65
66                 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless", arg[1]))
67                 return
68         end
69         Map.parse(map)
70 end
71
72 m.title = luci.util.pcdata(wnet:get_i18n())
73
74
75 local function txpower_list(iw)
76         local list = iw.txpwrlist or { }
77         local off  = tonumber(iw.txpower_offset) or 0
78         local new  = { }
79         local prev = -1
80         local _, val
81         for _, val in ipairs(list) do
82                 local dbm = val.dbm + off
83                 local mw  = math.floor(10 ^ (dbm / 10))
84                 if mw ~= prev then
85                         prev = mw
86                         new[#new+1] = {
87                                 display_dbm = dbm,
88                                 display_mw  = mw,
89                                 driver_dbm  = val.dbm,
90                                 driver_mw   = val.mw
91                         }
92                 end
93         end
94         return new
95 end
96
97 local function txpower_current(pwr, list)
98         pwr = tonumber(pwr)
99         if pwr ~= nil then
100                 local _, item
101                 for _, item in ipairs(list) do
102                         if item.driver_dbm >= pwr then
103                                 return item.driver_dbm
104                         end
105                 end
106         end
107         return (list[#list] and list[#list].driver_dbm) or pwr or 0
108 end
109
110 local iw = luci.sys.wifi.getiwinfo(arg[1])
111 local hw_modes      = iw.hwmodelist or { }
112 local tx_power_list = txpower_list(iw)
113 local tx_power_cur  = txpower_current(wdev:get("txpower"), tx_power_list)
114
115 s = m:section(NamedSection, wdev:name(), "wifi-device", translate("Device Configuration"))
116 s.addremove = false
117
118 s:tab("general", translate("General Setup"))
119 s:tab("macfilter", translate("MAC-Filter"))
120 s:tab("advanced", translate("Advanced Settings"))
121
122 --[[
123 back = s:option(DummyValue, "_overview", translate("Overview"))
124 back.value = ""
125 back.titleref = luci.dispatcher.build_url("admin", "network", "wireless")
126 ]]
127
128 st = s:taboption("general", DummyValue, "__status", translate("Status"))
129 st.template = "admin_network/wifi_status"
130 st.ifname   = arg[1]
131
132 en = s:taboption("general", Button, "__toggle")
133
134 if wdev:get("disabled") == "1" or wnet:get("disabled") == "1" then
135         en.title      = translate("Wireless network is disabled")
136         en.inputtitle = translate("Enable")
137         en.inputstyle = "apply"
138 else
139         en.title      = translate("Wireless network is enabled")
140         en.inputtitle = translate("Disable")
141         en.inputstyle = "reset"
142 end
143
144
145 local hwtype = wdev:get("type")
146
147 -- NanoFoo
148 local nsantenna = wdev:get("antenna")
149
150 -- Check whether there is a client interface on the same radio,
151 -- if yes, lock the channel choice as the station will dicatate the freq
152 local has_sta = nil
153 local _, net
154 for _, net in ipairs(wdev:get_wifinets()) do
155         if net:mode() == "sta" and net:id() ~= wnet:id() then
156                 has_sta = net
157                 break
158         end
159 end
160
161 if has_sta then
162         ch = s:taboption("general", DummyValue, "choice", translate("Channel"))
163         ch.value = translatef("Locked to channel %d used by %s",
164                 has_sta:channel(), has_sta:shortname())
165 else
166         ch = s:taboption("general", Value, "channel", translate("Channel"))
167         ch:value("auto", translate("auto"))
168         for _, f in ipairs(iw and iw.freqlist or { }) do
169                 if not f.restricted then
170                         ch:value(f.channel, "%i (%.3f GHz)" %{ f.channel, f.mhz / 1000 })
171                 end
172         end
173 end
174
175 ------------------- MAC80211 Device ------------------
176
177 if hwtype == "mac80211" then
178         if #tx_power_list > 1 then
179                 tp = s:taboption("general", ListValue,
180                         "txpower", translate("Transmit Power"), "dBm")
181                 tp.rmempty = true
182                 tp.default = tx_power_cur
183                 function tp.cfgvalue(...)
184                         return txpower_current(Value.cfgvalue(...), tx_power_list)
185                 end
186
187                 for _, p in ipairs(tx_power_list) do
188                         tp:value(p.driver_dbm, "%i dBm (%i mW)"
189                                 %{ p.display_dbm, p.display_mw })
190                 end
191         end
192
193         mode = s:taboption("advanced", ListValue, "hwmode", translate("Band"))
194
195         if hw_modes.n then
196                 if hw_modes.g then mode:value("11g", "2.4GHz (802.11g+n)") end
197                 if hw_modes.a then mode:value("11a", "5GHz (802.11a+n)") end
198
199                 htmode = s:taboption("advanced", ListValue, "htmode", translate("HT mode (802.11n)"))
200                 htmode:value("", translate("disabled"))
201                 htmode:value("HT20", "20MHz")
202                 htmode:value("HT40", "40MHz")
203
204                 function mode.cfgvalue(...)
205                         local v = Value.cfgvalue(...)
206                         if v == "11na" then
207                                 return "11a"
208                         elseif v == "11ng" then
209                                 return "11g"
210                         end
211                         return v
212                 end
213
214                 noscan = s:taboption("advanced", Flag, "noscan", translate("Force 40MHz mode"),
215                         translate("Always use 40MHz channels even if the secondary channel overlaps. Using this option does not comply with IEEE 802.11n-2009!"))
216                 noscan:depends("htmode", "HT40")
217                 noscan.default = noscan.disabled
218         else
219                 if hw_modes.g then mode:value("11g", "2.4GHz (802.11g)") end
220                 if hw_modes.a then mode:value("11a", "5GHz (802.11a)") end
221         end
222
223         local cl = iw and iw.countrylist
224         if cl and #cl > 0 then
225                 cc = s:taboption("advanced", ListValue, "country", translate("Country Code"), translate("Use ISO/IEC 3166 alpha2 country codes."))
226                 cc.default = tostring(iw and iw.country or "00")
227                 for _, c in ipairs(cl) do
228                         cc:value(c.alpha2, "%s - %s" %{ c.alpha2, c.name })
229                 end
230         else
231                 s:taboption("advanced", Value, "country", translate("Country Code"), translate("Use ISO/IEC 3166 alpha2 country codes."))
232         end
233
234         s:taboption("advanced", Value, "distance", translate("Distance Optimization"),
235                 translate("Distance to farthest network member in meters."))
236
237         -- external antenna profiles
238         local eal = iw and iw.extant
239         if eal and #eal > 0 then
240                 ea = s:taboption("advanced", ListValue, "extant", translate("Antenna Configuration"))
241                 for _, eap in ipairs(eal) do
242                         ea:value(eap.id, "%s (%s)" %{ eap.name, eap.description })
243                         if eap.selected then
244                                 ea.default = eap.id
245                         end
246                 end
247         end
248
249         s:taboption("advanced", Value, "frag", translate("Fragmentation Threshold"))
250         s:taboption("advanced", Value, "rts", translate("RTS/CTS Threshold"))
251 end
252
253
254 ------------------- Madwifi Device ------------------
255
256 if hwtype == "atheros" then
257         tp = s:taboption("general",
258                 (#tx_power_list > 0) and ListValue or Value,
259                 "txpower", translate("Transmit Power"), "dBm")
260
261         tp.rmempty = true
262         tp.default = tx_power_cur
263
264         function tp.cfgvalue(...)
265                 return txpower_current(Value.cfgvalue(...), tx_power_list)
266         end
267
268         for _, p in ipairs(tx_power_list) do
269                 tp:value(p.driver_dbm, "%i dBm (%i mW)"
270                         %{ p.display_dbm, p.display_mw })
271         end
272
273         mode = s:taboption("advanced", ListValue, "hwmode", translate("Mode"))
274         mode:value("", translate("auto"))
275         if hw_modes.b then mode:value("11b", "802.11b") end
276         if hw_modes.g then mode:value("11g", "802.11g") end
277         if hw_modes.a then mode:value("11a", "802.11a") end
278         if hw_modes.g then mode:value("11bg", "802.11b+g") end
279         if hw_modes.g then mode:value("11gst", "802.11g + Turbo") end
280         if hw_modes.a then mode:value("11ast", "802.11a + Turbo") end
281         mode:value("fh", translate("Frequency Hopping"))
282
283         s:taboption("advanced", Flag, "diversity", translate("Diversity")).rmempty = false
284
285         if not nsantenna then
286                 ant1 = s:taboption("advanced", ListValue, "txantenna", translate("Transmitter Antenna"))
287                 ant1.widget = "radio"
288                 ant1.orientation = "horizontal"
289                 ant1:depends("diversity", "")
290                 ant1:value("0", translate("auto"))
291                 ant1:value("1", translate("Antenna 1"))
292                 ant1:value("2", translate("Antenna 2"))
293
294                 ant2 = s:taboption("advanced", ListValue, "rxantenna", translate("Receiver Antenna"))
295                 ant2.widget = "radio"
296                 ant2.orientation = "horizontal"
297                 ant2:depends("diversity", "")
298                 ant2:value("0", translate("auto"))
299                 ant2:value("1", translate("Antenna 1"))
300                 ant2:value("2", translate("Antenna 2"))
301
302         else -- NanoFoo
303                 local ant = s:taboption("advanced", ListValue, "antenna", translate("Transmitter Antenna"))
304                 ant:value("auto")
305                 ant:value("vertical")
306                 ant:value("horizontal")
307                 ant:value("external")
308         end
309
310         s:taboption("advanced", Value, "distance", translate("Distance Optimization"),
311                 translate("Distance to farthest network member in meters."))
312         s:taboption("advanced", Value, "regdomain", translate("Regulatory Domain"))
313         s:taboption("advanced", Value, "country", translate("Country Code"))
314         s:taboption("advanced", Flag, "outdoor", translate("Outdoor Channels"))
315
316         --s:option(Flag, "nosbeacon", translate("Disable HW-Beacon timer"))
317 end
318
319
320
321 ------------------- Broadcom Device ------------------
322
323 if hwtype == "broadcom" then
324         tp = s:taboption("general",
325                 (#tx_power_list > 0) and ListValue or Value,
326                 "txpower", translate("Transmit Power"), "dBm")
327
328         tp.rmempty = true
329         tp.default = tx_power_cur
330
331         function tp.cfgvalue(...)
332                 return txpower_current(Value.cfgvalue(...), tx_power_list)
333         end
334
335         for _, p in ipairs(tx_power_list) do
336                 tp:value(p.driver_dbm, "%i dBm (%i mW)"
337                         %{ p.display_dbm, p.display_mw })
338         end
339
340         mode = s:taboption("advanced", ListValue, "hwmode", translate("Mode"))
341         mode:value("11bg", "802.11b+g")
342         mode:value("11b", "802.11b")
343         mode:value("11g", "802.11g")
344         mode:value("11gst", "802.11g + Turbo")
345
346         ant1 = s:taboption("advanced", ListValue, "txantenna", translate("Transmitter Antenna"))
347         ant1.widget = "radio"
348         ant1:depends("diversity", "")
349         ant1:value("3", translate("auto"))
350         ant1:value("0", translate("Antenna 1"))
351         ant1:value("1", translate("Antenna 2"))
352
353         ant2 = s:taboption("advanced", ListValue, "rxantenna", translate("Receiver Antenna"))
354         ant2.widget = "radio"
355         ant2:depends("diversity", "")
356         ant2:value("3", translate("auto"))
357         ant2:value("0", translate("Antenna 1"))
358         ant2:value("1", translate("Antenna 2"))
359
360         s:taboption("advanced", Flag, "frameburst", translate("Frame Bursting"))
361
362         s:taboption("advanced", Value, "distance", translate("Distance Optimization"))
363         --s:option(Value, "slottime", translate("Slot time"))
364
365         s:taboption("advanced", Value, "country", translate("Country Code"))
366         s:taboption("advanced", Value, "maxassoc", translate("Connection Limit"))
367 end
368
369
370 --------------------- HostAP Device ---------------------
371
372 if hwtype == "prism2" then
373         s:taboption("advanced", Value, "txpower", translate("Transmit Power"), "att units").rmempty = true
374
375         s:taboption("advanced", Flag, "diversity", translate("Diversity")).rmempty = false
376
377         s:taboption("advanced", Value, "txantenna", translate("Transmitter Antenna"))
378         s:taboption("advanced", Value, "rxantenna", translate("Receiver Antenna"))
379 end
380
381
382 ----------------------- Interface -----------------------
383
384 s = m:section(NamedSection, wnet.sid, "wifi-iface", translate("Interface Configuration"))
385 ifsection = s
386 s.addremove = false
387 s.anonymous = true
388 s.defaults.device = wdev:name()
389
390 s:tab("general", translate("General Setup"))
391 s:tab("encryption", translate("Wireless Security"))
392 s:tab("macfilter", translate("MAC-Filter"))
393 s:tab("advanced", translate("Advanced Settings"))
394
395 s:taboption("general", Value, "ssid", translate("<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
396
397 mode = s:taboption("general", ListValue, "mode", translate("Mode"))
398 mode.override_values = true
399 mode:value("ap", translate("Access Point"))
400 mode:value("sta", translate("Client"))
401 mode:value("adhoc", translate("Ad-Hoc"))
402
403 bssid = s:taboption("general", Value, "bssid", translate("<abbr title=\"Basic Service Set Identifier\">BSSID</abbr>"))
404
405 network = s:taboption("general", Value, "network", translate("Network"),
406         translate("Choose the network(s) you want to attach to this wireless interface or " ..
407                 "fill out the <em>create</em> field to define a new network."))
408
409 network.rmempty = true
410 network.template = "cbi/network_netlist"
411 network.widget = "checkbox"
412 network.novirtual = true
413
414 function network.write(self, section, value)
415         local i = nw:get_interface(section)
416         if i then
417                 if value == '-' then
418                         value = m:formvalue(self:cbid(section) .. ".newnet")
419                         if value and #value > 0 then
420                                 local n = nw:add_network(value, {proto="none"})
421                                 if n then n:add_interface(i) end
422                         else
423                                 local n = i:get_network()
424                                 if n then n:del_interface(i) end
425                         end
426                 else
427                         local v
428                         for _, v in ipairs(i:get_networks()) do
429                                 v:del_interface(i)
430                         end
431                         for v in ut.imatch(value) do
432                                 local n = nw:get_network(v)
433                                 if n then
434                                         if not n:is_empty() then
435                                                 n:set("type", "bridge")
436                                         end
437                                         n:add_interface(i)
438                                 end
439                         end
440                 end
441         end
442 end
443
444 -------------------- MAC80211 Interface ----------------------
445
446 if hwtype == "mac80211" then
447         if fs.access("/usr/sbin/iw") then
448                 mode:value("mesh", "802.11s")
449         end
450
451         mode:value("ahdemo", translate("Pseudo Ad-Hoc (ahdemo)"))
452         mode:value("monitor", translate("Monitor"))
453         bssid:depends({mode="adhoc"})
454         bssid:depends({mode="sta"})
455         bssid:depends({mode="sta-wds"})
456
457         mp = s:taboption("macfilter", ListValue, "macfilter", translate("MAC-Address Filter"))
458         mp:depends({mode="ap"})
459         mp:depends({mode="ap-wds"})
460         mp:value("", translate("disable"))
461         mp:value("allow", translate("Allow listed only"))
462         mp:value("deny", translate("Allow all except listed"))
463
464         ml = s:taboption("macfilter", DynamicList, "maclist", translate("MAC-List"))
465         ml.datatype = "macaddr"
466         ml:depends({macfilter="allow"})
467         ml:depends({macfilter="deny"})
468         nt.mac_hints(function(mac, name) ml:value(mac, "%s (%s)" %{ mac, name }) end)
469
470         mode:value("ap-wds", "%s (%s)" % {translate("Access Point"), translate("WDS")})
471         mode:value("sta-wds", "%s (%s)" % {translate("Client"), translate("WDS")})
472
473         function mode.write(self, section, value)
474                 if value == "ap-wds" then
475                         ListValue.write(self, section, "ap")
476                         m.uci:set("wireless", section, "wds", 1)
477                 elseif value == "sta-wds" then
478                         ListValue.write(self, section, "sta")
479                         m.uci:set("wireless", section, "wds", 1)
480                 else
481                         ListValue.write(self, section, value)
482                         m.uci:delete("wireless", section, "wds")
483                 end
484         end
485
486         function mode.cfgvalue(self, section)
487                 local mode = ListValue.cfgvalue(self, section)
488                 local wds  = m.uci:get("wireless", section, "wds") == "1"
489
490                 if mode == "ap" and wds then
491                         return "ap-wds"
492                 elseif mode == "sta" and wds then
493                         return "sta-wds"
494                 else
495                         return mode
496                 end
497         end
498
499         hidden = s:taboption("general", Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
500         hidden:depends({mode="ap"})
501         hidden:depends({mode="ap-wds"})
502
503         wmm = s:taboption("general", Flag, "wmm", translate("WMM Mode"))
504         wmm:depends({mode="ap"})
505         wmm:depends({mode="ap-wds"})
506         wmm.default = wmm.enabled
507 end
508
509
510
511 -------------------- Madwifi Interface ----------------------
512
513 if hwtype == "atheros" then
514         mode:value("ahdemo", translate("Pseudo Ad-Hoc (ahdemo)"))
515         mode:value("monitor", translate("Monitor"))
516         mode:value("ap-wds", "%s (%s)" % {translate("Access Point"), translate("WDS")})
517         mode:value("sta-wds", "%s (%s)" % {translate("Client"), translate("WDS")})
518         mode:value("wds", translate("Static WDS"))
519
520         function mode.write(self, section, value)
521                 if value == "ap-wds" then
522                         ListValue.write(self, section, "ap")
523                         m.uci:set("wireless", section, "wds", 1)
524                 elseif value == "sta-wds" then
525                         ListValue.write(self, section, "sta")
526                         m.uci:set("wireless", section, "wds", 1)
527                 else
528                         ListValue.write(self, section, value)
529                         m.uci:delete("wireless", section, "wds")
530                 end
531         end
532
533         function mode.cfgvalue(self, section)
534                 local mode = ListValue.cfgvalue(self, section)
535                 local wds  = m.uci:get("wireless", section, "wds") == "1"
536
537                 if mode == "ap" and wds then
538                         return "ap-wds"
539                 elseif mode == "sta" and wds then
540                         return "sta-wds"
541                 else
542                         return mode
543                 end
544         end
545
546         bssid:depends({mode="adhoc"})
547         bssid:depends({mode="ahdemo"})
548         bssid:depends({mode="wds"})
549
550         wdssep = s:taboption("advanced", Flag, "wdssep", translate("Separate WDS"))
551         wdssep:depends({mode="ap-wds"})
552
553         s:taboption("advanced", Flag, "doth", "802.11h")
554         hidden = s:taboption("general", Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
555         hidden:depends({mode="ap"})
556         hidden:depends({mode="adhoc"})
557         hidden:depends({mode="ap-wds"})
558         hidden:depends({mode="sta-wds"})
559         isolate = s:taboption("advanced", Flag, "isolate", translate("Separate Clients"),
560          translate("Prevents client-to-client communication"))
561         isolate:depends({mode="ap"})
562         s:taboption("advanced", Flag, "bgscan", translate("Background Scan"))
563
564         mp = s:taboption("macfilter", ListValue, "macpolicy", translate("MAC-Address Filter"))
565         mp:value("", translate("disable"))
566         mp:value("allow", translate("Allow listed only"))
567         mp:value("deny", translate("Allow all except listed"))
568
569         ml = s:taboption("macfilter", DynamicList, "maclist", translate("MAC-List"))
570         ml.datatype = "macaddr"
571         ml:depends({macpolicy="allow"})
572         ml:depends({macpolicy="deny"})
573         nt.mac_hints(function(mac, name) ml:value(mac, "%s (%s)" %{ mac, name }) end)
574
575         s:taboption("advanced", Value, "rate", translate("Transmission Rate"))
576         s:taboption("advanced", Value, "mcast_rate", translate("Multicast Rate"))
577         s:taboption("advanced", Value, "frag", translate("Fragmentation Threshold"))
578         s:taboption("advanced", Value, "rts", translate("RTS/CTS Threshold"))
579         s:taboption("advanced", Value, "minrate", translate("Minimum Rate"))
580         s:taboption("advanced", Value, "maxrate", translate("Maximum Rate"))
581         s:taboption("advanced", Flag, "compression", translate("Compression"))
582
583         s:taboption("advanced", Flag, "bursting", translate("Frame Bursting"))
584         s:taboption("advanced", Flag, "turbo", translate("Turbo Mode"))
585         s:taboption("advanced", Flag, "ff", translate("Fast Frames"))
586
587         s:taboption("advanced", Flag, "wmm", translate("WMM Mode"))
588         s:taboption("advanced", Flag, "xr", translate("XR Support"))
589         s:taboption("advanced", Flag, "ar", translate("AR Support"))
590
591         local swm = s:taboption("advanced", Flag, "sw_merge", translate("Disable HW-Beacon timer"))
592         swm:depends({mode="adhoc"})
593
594         local nos = s:taboption("advanced", Flag, "nosbeacon", translate("Disable HW-Beacon timer"))
595         nos:depends({mode="sta"})
596         nos:depends({mode="sta-wds"})
597
598         local probereq = s:taboption("advanced", Flag, "probereq", translate("Do not send probe responses"))
599         probereq.enabled  = "0"
600         probereq.disabled = "1"
601 end
602
603
604 -------------------- Broadcom Interface ----------------------
605
606 if hwtype == "broadcom" then
607         mode:value("wds", translate("WDS"))
608         mode:value("monitor", translate("Monitor"))
609
610         hidden = s:taboption("general", Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
611         hidden:depends({mode="ap"})
612         hidden:depends({mode="adhoc"})
613         hidden:depends({mode="wds"})
614
615         isolate = s:taboption("advanced", Flag, "isolate", translate("Separate Clients"),
616          translate("Prevents client-to-client communication"))
617         isolate:depends({mode="ap"})
618
619         s:taboption("advanced", Flag, "doth", "802.11h")
620         s:taboption("advanced", Flag, "wmm", translate("WMM Mode"))
621
622         bssid:depends({mode="wds"})
623         bssid:depends({mode="adhoc"})
624 end
625
626
627 ----------------------- HostAP Interface ---------------------
628
629 if hwtype == "prism2" then
630         mode:value("wds", translate("WDS"))
631         mode:value("monitor", translate("Monitor"))
632
633         hidden = s:taboption("general", Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
634         hidden:depends({mode="ap"})
635         hidden:depends({mode="adhoc"})
636         hidden:depends({mode="wds"})
637
638         bssid:depends({mode="sta"})
639
640         mp = s:taboption("macfilter", ListValue, "macpolicy", translate("MAC-Address Filter"))
641         mp:value("", translate("disable"))
642         mp:value("allow", translate("Allow listed only"))
643         mp:value("deny", translate("Allow all except listed"))
644         ml = s:taboption("macfilter", DynamicList, "maclist", translate("MAC-List"))
645         ml:depends({macpolicy="allow"})
646         ml:depends({macpolicy="deny"})
647         nt.mac_hints(function(mac, name) ml:value(mac, "%s (%s)" %{ mac, name }) end)
648
649         s:taboption("advanced", Value, "rate", translate("Transmission Rate"))
650         s:taboption("advanced", Value, "frag", translate("Fragmentation Threshold"))
651         s:taboption("advanced", Value, "rts", translate("RTS/CTS Threshold"))
652 end
653
654
655 ------------------- WiFI-Encryption -------------------
656
657 encr = s:taboption("encryption", ListValue, "encryption", translate("Encryption"))
658 encr.override_values = true
659 encr.override_depends = true
660 encr:depends({mode="ap"})
661 encr:depends({mode="sta"})
662 encr:depends({mode="adhoc"})
663 encr:depends({mode="ahdemo"})
664 encr:depends({mode="ap-wds"})
665 encr:depends({mode="sta-wds"})
666 encr:depends({mode="mesh"})
667
668 cipher = s:taboption("encryption", ListValue, "cipher", translate("Cipher"))
669 cipher:depends({encryption="wpa"})
670 cipher:depends({encryption="wpa2"})
671 cipher:depends({encryption="psk"})
672 cipher:depends({encryption="psk2"})
673 cipher:depends({encryption="wpa-mixed"})
674 cipher:depends({encryption="psk-mixed"})
675 cipher:value("auto", translate("auto"))
676 cipher:value("ccmp", translate("Force CCMP (AES)"))
677 cipher:value("tkip", translate("Force TKIP"))
678 cipher:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
679
680 function encr.cfgvalue(self, section)
681         local v = tostring(ListValue.cfgvalue(self, section))
682         if v == "wep" then
683                 return "wep-open"
684         elseif v and v:match("%+") then
685                 return (v:gsub("%+.+$", ""))
686         end
687         return v
688 end
689
690 function encr.write(self, section, value)
691         local e = tostring(encr:formvalue(section))
692         local c = tostring(cipher:formvalue(section))
693         if value == "wpa" or value == "wpa2"  then
694                 self.map.uci:delete("wireless", section, "key")
695         end
696         if e and (c == "tkip" or c == "ccmp" or c == "tkip+ccmp") then
697                 e = e .. "+" .. c
698         end
699         self.map:set(section, "encryption", e)
700 end
701
702 function cipher.cfgvalue(self, section)
703         local v = tostring(ListValue.cfgvalue(encr, section))
704         if v and v:match("%+") then
705                 v = v:gsub("^[^%+]+%+", "")
706                 if v == "aes" then v = "ccmp"
707                 elseif v == "tkip+aes" then v = "tkip+ccmp"
708                 elseif v == "aes+tkip" then v = "tkip+ccmp"
709                 elseif v == "ccmp+tkip" then v = "tkip+ccmp"
710                 end
711         end
712         return v
713 end
714
715 function cipher.write(self, section)
716         return encr:write(section)
717 end
718
719
720 encr:value("none", "No Encryption")
721 encr:value("wep-open",   translate("WEP Open System"), {mode="ap"}, {mode="sta"}, {mode="ap-wds"}, {mode="sta-wds"}, {mode="adhoc"}, {mode="ahdemo"}, {mode="wds"})
722 encr:value("wep-shared", translate("WEP Shared Key"),  {mode="ap"}, {mode="sta"}, {mode="ap-wds"}, {mode="sta-wds"}, {mode="adhoc"}, {mode="ahdemo"}, {mode="wds"})
723
724 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
725         local supplicant = fs.access("/usr/sbin/wpa_supplicant")
726         local hostapd = fs.access("/usr/sbin/hostapd")
727
728         -- Probe EAP support
729         local has_ap_eap  = (os.execute("hostapd -veap >/dev/null 2>/dev/null") == 0)
730         local has_sta_eap = (os.execute("wpa_supplicant -veap >/dev/null 2>/dev/null") == 0)
731
732         if hostapd and supplicant then
733                 encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="sta"}, {mode="ap-wds"}, {mode="sta-wds"})
734                 encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="sta"}, {mode="ap-wds"}, {mode="sta-wds"})
735                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="ap"}, {mode="sta"}, {mode="ap-wds"}, {mode="sta-wds"})
736                 if has_ap_eap and has_sta_eap then
737                         encr:value("wpa", "WPA-EAP", {mode="ap"}, {mode="sta"}, {mode="ap-wds"}, {mode="sta-wds"})
738                         encr:value("wpa2", "WPA2-EAP", {mode="ap"}, {mode="sta"}, {mode="ap-wds"}, {mode="sta-wds"})
739                 end
740         elseif hostapd and not supplicant then
741                 encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="ap-wds"})
742                 encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="ap-wds"})
743                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="ap"}, {mode="ap-wds"})
744                 if has_ap_eap then
745                         encr:value("wpa", "WPA-EAP", {mode="ap"}, {mode="ap-wds"})
746                         encr:value("wpa2", "WPA2-EAP", {mode="ap"}, {mode="ap-wds"})
747                 end
748                 encr.description = translate(
749                         "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
750                         "and ad-hoc mode) to be installed."
751                 )
752         elseif not hostapd and supplicant then
753                 encr:value("psk", "WPA-PSK", {mode="sta"}, {mode="sta-wds"})
754                 encr:value("psk2", "WPA2-PSK", {mode="sta"}, {mode="sta-wds"})
755                 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="sta"}, {mode="sta-wds"})
756                 if has_sta_eap then
757                         encr:value("wpa", "WPA-EAP", {mode="sta"}, {mode="sta-wds"})
758                         encr:value("wpa2", "WPA2-EAP", {mode="sta"}, {mode="sta-wds"})
759                 end
760                 encr.description = translate(
761                         "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
762                         "and ad-hoc mode) to be installed."
763                 )
764         else
765                 encr.description = translate(
766                         "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
767                         "and ad-hoc mode) to be installed."
768                 )
769         end
770 elseif hwtype == "broadcom" then
771         encr:value("psk", "WPA-PSK")
772         encr:value("psk2", "WPA2-PSK")
773         encr:value("psk+psk2", "WPA-PSK/WPA2-PSK Mixed Mode")
774 end
775
776 auth_server = s:taboption("encryption", Value, "auth_server", translate("Radius-Authentication-Server"))
777 auth_server:depends({mode="ap", encryption="wpa"})
778 auth_server:depends({mode="ap", encryption="wpa2"})
779 auth_server:depends({mode="ap-wds", encryption="wpa"})
780 auth_server:depends({mode="ap-wds", encryption="wpa2"})
781 auth_server.rmempty = true
782 auth_server.datatype = "host"
783
784 auth_port = s:taboption("encryption", Value, "auth_port", translate("Radius-Authentication-Port"), translatef("Default %d", 1812))
785 auth_port:depends({mode="ap", encryption="wpa"})
786 auth_port:depends({mode="ap", encryption="wpa2"})
787 auth_port:depends({mode="ap-wds", encryption="wpa"})
788 auth_port:depends({mode="ap-wds", encryption="wpa2"})
789 auth_port.rmempty = true
790 auth_port.datatype = "port"
791
792 auth_secret = s:taboption("encryption", Value, "auth_secret", translate("Radius-Authentication-Secret"))
793 auth_secret:depends({mode="ap", encryption="wpa"})
794 auth_secret:depends({mode="ap", encryption="wpa2"})
795 auth_secret:depends({mode="ap-wds", encryption="wpa"})
796 auth_secret:depends({mode="ap-wds", encryption="wpa2"})
797 auth_secret.rmempty = true
798 auth_secret.password = true
799
800 acct_server = s:taboption("encryption", Value, "acct_server", translate("Radius-Accounting-Server"))
801 acct_server:depends({mode="ap", encryption="wpa"})
802 acct_server:depends({mode="ap", encryption="wpa2"})
803 acct_server:depends({mode="ap-wds", encryption="wpa"})
804 acct_server:depends({mode="ap-wds", encryption="wpa2"})
805 acct_server.rmempty = true
806 acct_server.datatype = "host"
807
808 acct_port = s:taboption("encryption", Value, "acct_port", translate("Radius-Accounting-Port"), translatef("Default %d", 1813))
809 acct_port:depends({mode="ap", encryption="wpa"})
810 acct_port:depends({mode="ap", encryption="wpa2"})
811 acct_port:depends({mode="ap-wds", encryption="wpa"})
812 acct_port:depends({mode="ap-wds", encryption="wpa2"})
813 acct_port.rmempty = true
814 acct_port.datatype = "port"
815
816 acct_secret = s:taboption("encryption", Value, "acct_secret", translate("Radius-Accounting-Secret"))
817 acct_secret:depends({mode="ap", encryption="wpa"})
818 acct_secret:depends({mode="ap", encryption="wpa2"})
819 acct_secret:depends({mode="ap-wds", encryption="wpa"})
820 acct_secret:depends({mode="ap-wds", encryption="wpa2"})
821 acct_secret.rmempty = true
822 acct_secret.password = true
823
824 wpakey = s:taboption("encryption", Value, "_wpa_key", translate("Key"))
825 wpakey:depends("encryption", "psk")
826 wpakey:depends("encryption", "psk2")
827 wpakey:depends("encryption", "psk+psk2")
828 wpakey:depends("encryption", "psk-mixed")
829 wpakey.datatype = "wpakey"
830 wpakey.rmempty = true
831 wpakey.password = true
832
833 wpakey.cfgvalue = function(self, section, value)
834         local key = m.uci:get("wireless", section, "key")
835         if key == "1" or key == "2" or key == "3" or key == "4" then
836                 return nil
837         end
838         return key
839 end
840
841 wpakey.write = function(self, section, value)
842         self.map.uci:set("wireless", section, "key", value)
843         self.map.uci:delete("wireless", section, "key1")
844 end
845
846
847 wepslot = s:taboption("encryption", ListValue, "_wep_key", translate("Used Key Slot"))
848 wepslot:depends("encryption", "wep-open")
849 wepslot:depends("encryption", "wep-shared")
850 wepslot:value("1", translatef("Key #%d", 1))
851 wepslot:value("2", translatef("Key #%d", 2))
852 wepslot:value("3", translatef("Key #%d", 3))
853 wepslot:value("4", translatef("Key #%d", 4))
854
855 wepslot.cfgvalue = function(self, section)
856         local slot = tonumber(m.uci:get("wireless", section, "key"))
857         if not slot or slot < 1 or slot > 4 then
858                 return 1
859         end
860         return slot
861 end
862
863 wepslot.write = function(self, section, value)
864         self.map.uci:set("wireless", section, "key", value)
865 end
866
867 local slot
868 for slot=1,4 do
869         wepkey = s:taboption("encryption", Value, "key" .. slot, translatef("Key #%d", slot))
870         wepkey:depends("encryption", "wep-open")
871         wepkey:depends("encryption", "wep-shared")
872         wepkey.datatype = "wepkey"
873         wepkey.rmempty = true
874         wepkey.password = true
875
876         function wepkey.write(self, section, value)
877                 if value and (#value == 5 or #value == 13) then
878                         value = "s:" .. value
879                 end
880                 return Value.write(self, section, value)
881         end
882 end
883
884
885 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
886         nasid = s:taboption("encryption", Value, "nasid", translate("NAS ID"))
887         nasid:depends({mode="ap", encryption="wpa"})
888         nasid:depends({mode="ap", encryption="wpa2"})
889         nasid:depends({mode="ap-wds", encryption="wpa"})
890         nasid:depends({mode="ap-wds", encryption="wpa2"})
891         nasid.rmempty = true
892
893         eaptype = s:taboption("encryption", ListValue, "eap_type", translate("EAP-Method"))
894         eaptype:value("tls",  "TLS")
895         eaptype:value("ttls", "TTLS")
896         eaptype:value("peap", "PEAP")
897         eaptype:depends({mode="sta", encryption="wpa"})
898         eaptype:depends({mode="sta", encryption="wpa2"})
899         eaptype:depends({mode="sta-wds", encryption="wpa"})
900         eaptype:depends({mode="sta-wds", encryption="wpa2"})
901
902         cacert = s:taboption("encryption", FileUpload, "ca_cert", translate("Path to CA-Certificate"))
903         cacert:depends({mode="sta", encryption="wpa"})
904         cacert:depends({mode="sta", encryption="wpa2"})
905         cacert:depends({mode="sta-wds", encryption="wpa"})
906         cacert:depends({mode="sta-wds", encryption="wpa2"})
907
908         clientcert = s:taboption("encryption", FileUpload, "client_cert", translate("Path to Client-Certificate"))
909         clientcert:depends({mode="sta", encryption="wpa"})
910         clientcert:depends({mode="sta", encryption="wpa2"})
911         clientcert:depends({mode="sta-wds", encryption="wpa"})
912         clientcert:depends({mode="sta-wds", encryption="wpa2"})
913
914         privkey = s:taboption("encryption", FileUpload, "priv_key", translate("Path to Private Key"))
915         privkey:depends({mode="sta", eap_type="tls", encryption="wpa2"})
916         privkey:depends({mode="sta", eap_type="tls", encryption="wpa"})
917         privkey:depends({mode="sta-wds", eap_type="tls", encryption="wpa2"})
918         privkey:depends({mode="sta-wds", eap_type="tls", encryption="wpa"})
919
920         privkeypwd = s:taboption("encryption", Value, "priv_key_pwd", translate("Password of Private Key"))
921         privkeypwd:depends({mode="sta", eap_type="tls", encryption="wpa2"})
922         privkeypwd:depends({mode="sta", eap_type="tls", encryption="wpa"})
923         privkeypwd:depends({mode="sta-wds", eap_type="tls", encryption="wpa2"})
924         privkeypwd:depends({mode="sta-wds", eap_type="tls", encryption="wpa"})
925
926
927         auth = s:taboption("encryption", Value, "auth", translate("Authentication"))
928         auth:value("PAP")
929         auth:value("CHAP")
930         auth:value("MSCHAP")
931         auth:value("MSCHAPV2")
932         auth:depends({mode="sta", eap_type="peap", encryption="wpa2"})
933         auth:depends({mode="sta", eap_type="peap", encryption="wpa"})
934         auth:depends({mode="sta", eap_type="ttls", encryption="wpa2"})
935         auth:depends({mode="sta", eap_type="ttls", encryption="wpa"})
936         auth:depends({mode="sta-wds", eap_type="peap", encryption="wpa2"})
937         auth:depends({mode="sta-wds", eap_type="peap", encryption="wpa"})
938         auth:depends({mode="sta-wds", eap_type="ttls", encryption="wpa2"})
939         auth:depends({mode="sta-wds", eap_type="ttls", encryption="wpa"})
940
941
942         identity = s:taboption("encryption", Value, "identity", translate("Identity"))
943         identity:depends({mode="sta", eap_type="peap", encryption="wpa2"})
944         identity:depends({mode="sta", eap_type="peap", encryption="wpa"})
945         identity:depends({mode="sta", eap_type="ttls", encryption="wpa2"})
946         identity:depends({mode="sta", eap_type="ttls", encryption="wpa"})
947         identity:depends({mode="sta-wds", eap_type="peap", encryption="wpa2"})
948         identity:depends({mode="sta-wds", eap_type="peap", encryption="wpa"})
949         identity:depends({mode="sta-wds", eap_type="ttls", encryption="wpa2"})
950         identity:depends({mode="sta-wds", eap_type="ttls", encryption="wpa"})
951
952         password = s:taboption("encryption", Value, "password", translate("Password"))
953         password:depends({mode="sta", eap_type="peap", encryption="wpa2"})
954         password:depends({mode="sta", eap_type="peap", encryption="wpa"})
955         password:depends({mode="sta", eap_type="ttls", encryption="wpa2"})
956         password:depends({mode="sta", eap_type="ttls", encryption="wpa"})
957         password:depends({mode="sta-wds", eap_type="peap", encryption="wpa2"})
958         password:depends({mode="sta-wds", eap_type="peap", encryption="wpa"})
959         password:depends({mode="sta-wds", eap_type="ttls", encryption="wpa2"})
960         password:depends({mode="sta-wds", eap_type="ttls", encryption="wpa"})
961 end
962
963 return m