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