wifi: add "macaddr" validator to bssid option
[project/luci.git] / modules / luci-mod-admin-mini / luasrc / model / cbi / mini / wifi.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 -- Data init --
6
7 local fs = require "nixio.fs"
8 local sys = require "luci.sys"
9 local uci = require "luci.model.uci".cursor()
10
11 if not uci:get("network", "wan") then
12 uci:section("network", "interface", "wan", {proto="none", ifname=" "})
13 uci:save("network")
14 uci:commit("network")
15 end
16
17 local wlcursor = luci.model.uci.cursor_state()
18 local wireless = wlcursor:get_all("wireless")
19 local wifidevs = {}
20 local ifaces = {}
21
22 for k, v in pairs(wireless) do
23 if v[".type"] == "wifi-iface" then
24 table.insert(ifaces, v)
25 end
26 end
27
28 wlcursor:foreach("wireless", "wifi-device",
29 function(section)
30 table.insert(wifidevs, section[".name"])
31 end)
32
33
34 -- Main Map --
35
36 m = Map("wireless", translate("Wireless"), translate("Here you can configure installed wifi devices."))
37 m:chain("network")
38
39
40 -- Status Table --
41 s = m:section(Table, ifaces, translate("Networks"))
42
43 link = s:option(DummyValue, "_link", translate("Link"))
44 function link.cfgvalue(self, section)
45 local ifname = self.map:get(section, "ifname")
46 local iwinfo = sys.wifi.getiwinfo(ifname)
47 return iwinfo and "%d/%d" %{ iwinfo.quality, iwinfo.quality_max } or "-"
48 end
49
50 essid = s:option(DummyValue, "ssid", "ESSID")
51
52 bssid = s:option(DummyValue, "_bsiid", "BSSID")
53 function bssid.cfgvalue(self, section)
54 local ifname = self.map:get(section, "ifname")
55 local iwinfo = sys.wifi.getiwinfo(ifname)
56 return iwinfo and iwinfo.bssid or "-"
57 end
58
59 channel = s:option(DummyValue, "channel", translate("Channel"))
60 function channel.cfgvalue(self, section)
61 return wireless[self.map:get(section, "device")].channel
62 end
63
64 protocol = s:option(DummyValue, "_mode", translate("Protocol"))
65 function protocol.cfgvalue(self, section)
66 local mode = wireless[self.map:get(section, "device")].mode
67 return mode and "802." .. mode
68 end
69
70 mode = s:option(DummyValue, "mode", translate("Mode"))
71 encryption = s:option(DummyValue, "encryption", translate("<abbr title=\"Encrypted\">Encr.</abbr>"))
72
73 power = s:option(DummyValue, "_power", translate("Power"))
74 function power.cfgvalue(self, section)
75 local ifname = self.map:get(section, "ifname")
76 local iwinfo = sys.wifi.getiwinfo(ifname)
77 return iwinfo and "%d dBm" % iwinfo.txpower or "-"
78 end
79
80 scan = s:option(Button, "_scan", translate("Scan"))
81 scan.inputstyle = "find"
82
83 function scan.cfgvalue(self, section)
84 return self.map:get(section, "ifname") or false
85 end
86
87 -- WLAN-Scan-Table --
88
89 t2 = m:section(Table, {}, translate("<abbr title=\"Wireless Local Area Network\">WLAN</abbr>-Scan"), translate("Wifi networks in your local environment"))
90
91 function scan.write(self, section)
92 m.autoapply = false
93 t2.render = t2._render
94 local ifname = self.map:get(section, "ifname")
95 local iwinfo = sys.wifi.getiwinfo(ifname)
96 if iwinfo then
97 local _, cell
98 for _, cell in ipairs(iwinfo.scanlist) do
99 t2.data[#t2.data+1] = {
100 Quality = "%d/%d" %{ cell.quality, cell.quality_max },
101 ESSID = cell.ssid,
102 Address = cell.bssid,
103 Mode = cell.mode,
104 ["Encryption key"] = cell.encryption.enabled and "On" or "Off",
105 ["Signal level"] = "%d dBm" % cell.signal,
106 ["Noise level"] = "%d dBm" % iwinfo.noise
107 }
108 end
109 end
110 end
111
112 t2._render = t2.render
113 t2.render = function() end
114
115 t2:option(DummyValue, "Quality", translate("Link"))
116 essid = t2:option(DummyValue, "ESSID", "ESSID")
117 function essid.cfgvalue(self, section)
118 return self.map:get(section, "ESSID")
119 end
120
121 t2:option(DummyValue, "Address", "BSSID")
122 t2:option(DummyValue, "Mode", translate("Mode"))
123 chan = t2:option(DummyValue, "channel", translate("Channel"))
124 function chan.cfgvalue(self, section)
125 return self.map:get(section, "Channel")
126 or self.map:get(section, "Frequency")
127 or "-"
128 end
129
130 t2:option(DummyValue, "Encryption key", translate("<abbr title=\"Encrypted\">Encr.</abbr>"))
131
132 t2:option(DummyValue, "Signal level", translate("Signal"))
133
134 t2:option(DummyValue, "Noise level", translate("Noise"))
135
136
137
138 if #wifidevs < 1 then
139 return m
140 end
141
142 -- Config Section --
143
144 s = m:section(NamedSection, wifidevs[1], "wifi-device", translate("Devices"))
145 s.addremove = false
146
147 en = s:option(Flag, "disabled", translate("enable"))
148 en.rmempty = false
149 en.enabled = "0"
150 en.disabled = "1"
151
152 function en.cfgvalue(self, section)
153 return Flag.cfgvalue(self, section) or "0"
154 end
155
156
157 local hwtype = m:get(wifidevs[1], "type")
158
159 ch = s:option(Value, "channel", translate("Channel"))
160 for i=1, 14 do
161 ch:value(i, i .. " (2.4 GHz)")
162 end
163
164
165 s = m:section(TypedSection, "wifi-iface", translate("Local Network"))
166 s.anonymous = true
167 s.addremove = false
168
169 s:option(Value, "ssid", translate("Network Name (<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>)"))
170
171 bssid = s:option(Value, "bssid", translate("<abbr title=\"Basic Service Set Identifier\">BSSID</abbr>"))
172 bssid.datatype = "macaddr"
173
174 local devs = {}
175 luci.model.uci.cursor():foreach("wireless", "wifi-device",
176 function (section)
177 table.insert(devs, section[".name"])
178 end)
179
180 if #devs > 1 then
181 device = s:option(DummyValue, "device", translate("Device"))
182 else
183 s.defaults.device = devs[1]
184 end
185
186 mode = s:option(ListValue, "mode", translate("Mode"))
187 mode.override_values = true
188 mode:value("ap", translate("Provide (Access Point)"))
189 mode:value("adhoc", translate("Independent (Ad-Hoc)"))
190 mode:value("sta", translate("Join (Client)"))
191
192 function mode.write(self, section, value)
193 if value == "sta" then
194 local oldif = m.uci:get("network", "wan", "ifname")
195 if oldif and oldif ~= " " then
196 m.uci:set("network", "wan", "_ifname", oldif)
197 end
198 m.uci:set("network", "wan", "ifname", " ")
199
200 self.map:set(section, "network", "wan")
201 else
202 if m.uci:get("network", "wan", "_ifname") then
203 m.uci:set("network", "wan", "ifname", m.uci:get("network", "wan", "_ifname"))
204 end
205 self.map:set(section, "network", "lan")
206 end
207
208 return ListValue.write(self, section, value)
209 end
210
211 encr = s:option(ListValue, "encryption", translate("Encryption"))
212 encr.override_values = true
213 encr:value("none", "No Encryption")
214 encr:value("wep", "WEP")
215
216 if hwtype == "mac80211" then
217 local supplicant = fs.access("/usr/sbin/wpa_supplicant")
218 local hostapd = fs.access("/usr/sbin/hostapd")
219
220 if hostapd and supplicant then
221 encr:value("psk", "WPA-PSK")
222 encr:value("psk2", "WPA2-PSK")
223 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode")
224 encr:value("wpa", "WPA-Radius", {mode="ap"}, {mode="sta"})
225 encr:value("wpa2", "WPA2-Radius", {mode="ap"}, {mode="sta"})
226 elseif hostapd and not supplicant then
227 encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="adhoc"})
228 encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="adhoc"})
229 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="ap"}, {mode="adhoc"})
230 encr:value("wpa", "WPA-Radius", {mode="ap"})
231 encr:value("wpa2", "WPA2-Radius", {mode="ap"})
232 encr.description = translate(
233 "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
234 "and ad-hoc mode) to be installed."
235 )
236 elseif not hostapd and supplicant then
237 encr:value("psk", "WPA-PSK", {mode="sta"})
238 encr:value("psk2", "WPA2-PSK", {mode="sta"})
239 encr:value("psk-mixed", "WPA-PSK/WPA2-PSK Mixed Mode", {mode="sta"})
240 encr:value("wpa", "WPA-EAP", {mode="sta"})
241 encr:value("wpa2", "WPA2-EAP", {mode="sta"})
242 encr.description = translate(
243 "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
244 "and ad-hoc mode) to be installed."
245 )
246 else
247 encr.description = translate(
248 "WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP " ..
249 "and ad-hoc mode) to be installed."
250 )
251 end
252 elseif hwtype == "broadcom" then
253 encr:value("psk", "WPA-PSK")
254 encr:value("psk2", "WPA2-PSK")
255 encr:value("psk+psk2", "WPA-PSK/WPA2-PSK Mixed Mode")
256 end
257
258 key = s:option(Value, "key", translate("Key"))
259 key:depends("encryption", "wep")
260 key:depends("encryption", "psk")
261 key:depends("encryption", "psk2")
262 key:depends("encryption", "psk+psk2")
263 key:depends("encryption", "psk-mixed")
264 key:depends({mode="ap", encryption="wpa"})
265 key:depends({mode="ap", encryption="wpa2"})
266 key.rmempty = true
267 key.password = true
268
269 server = s:option(Value, "server", translate("Radius-Server"))
270 server:depends({mode="ap", encryption="wpa"})
271 server:depends({mode="ap", encryption="wpa2"})
272 server.rmempty = true
273
274 port = s:option(Value, "port", translate("Radius-Port"))
275 port:depends({mode="ap", encryption="wpa"})
276 port:depends({mode="ap", encryption="wpa2"})
277 port.rmempty = true
278
279
280 if hwtype == "mac80211" then
281 nasid = s:option(Value, "nasid", translate("NAS ID"))
282 nasid:depends({mode="ap", encryption="wpa"})
283 nasid:depends({mode="ap", encryption="wpa2"})
284 nasid.rmempty = true
285
286 eaptype = s:option(ListValue, "eap_type", translate("EAP-Method"))
287 eaptype:value("TLS")
288 eaptype:value("TTLS")
289 eaptype:value("PEAP")
290 eaptype:depends({mode="sta", encryption="wpa"})
291 eaptype:depends({mode="sta", encryption="wpa2"})
292
293 cacert = s:option(FileUpload, "ca_cert", translate("Path to CA-Certificate"))
294 cacert:depends({mode="sta", encryption="wpa"})
295 cacert:depends({mode="sta", encryption="wpa2"})
296
297 privkey = s:option(FileUpload, "priv_key", translate("Path to Private Key"))
298 privkey:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
299 privkey:depends({mode="sta", eap_type="TLS", encryption="wpa"})
300
301 privkeypwd = s:option(Value, "priv_key_pwd", translate("Password of Private Key"))
302 privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa2"})
303 privkeypwd:depends({mode="sta", eap_type="TLS", encryption="wpa"})
304
305
306 auth = s:option(Value, "auth", translate("Authentication"))
307 auth:value("PAP")
308 auth:value("CHAP")
309 auth:value("MSCHAP")
310 auth:value("MSCHAPV2")
311 auth:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
312 auth:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
313 auth:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
314 auth:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
315
316
317 identity = s:option(Value, "identity", translate("Identity"))
318 identity:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
319 identity:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
320 identity:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
321 identity:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
322
323 password = s:option(Value, "password", translate("Password"))
324 password:depends({mode="sta", eap_type="PEAP", encryption="wpa2"})
325 password:depends({mode="sta", eap_type="PEAP", encryption="wpa"})
326 password:depends({mode="sta", eap_type="TTLS", encryption="wpa2"})
327 password:depends({mode="sta", eap_type="TTLS", encryption="wpa"})
328 end
329
330
331 if hwtype == "broadcom" then
332 iso = s:option(Flag, "isolate", translate("AP-Isolation"), translate("Prevents Client to Client communication"))
333 iso.rmempty = true
334 iso:depends("mode", "ap")
335
336 hide = s:option(Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
337 hide.rmempty = true
338 hide:depends("mode", "ap")
339 end
340
341 if hwtype == "mac80211" then
342 bssid:depends({mode="adhoc"})
343 end
344
345 if hwtype == "broadcom" then
346 bssid:depends({mode="wds"})
347 bssid:depends({mode="adhoc"})
348 end
349
350
351 return m