2 Copyright 2008-2009 Steven Barth <steven@midlink.org>
3 Copyright 2008-2015 Jo-Philipp Wich <jow@openwrt.org>
4 Licensed to the public under the Apache License 2.0.
9 local ip = require "luci.ip"
10 local fs = require "nixio.fs"
11 local utl = require "luci.util"
12 local uci = require "luci.model.uci".cursor()
13 local ntm = require "luci.model.network"
15 local has_iwinfo = pcall(require, "iwinfo")
19 function guess_wifi_hw(dev)
21 local ifname = dev:name()
22 local name, idx = ifname:match("^([a-z]+)(%d+)")
26 local bl = dev.iwinfo.hwmodelist
27 if bl and next(bl) then
28 if bl.a then bands = bands .. "a" end
29 if bl.b then bands = bands .. "b" end
30 if bl.g then bands = bands .. "g" end
31 if bl.n then bands = bands .. "n" end
32 if bl.ac then bands = bands .. "ac" end
35 local hw = dev.iwinfo.hardware_name
37 return "%s 802.11%s" %{ hw, bands }
43 local name = translatef("Broadcom 802.11%s Wireless Controller", bands)
46 local fd = nixio.open("/proc/bus/pci/devices", "r")
49 for ln in fd:linesource() do
50 if ln:match("wl$") then
52 local version = ln:match("^%S+%s+%S%S%S%S([0-9a-f]+)")
54 "Broadcom BCM%04x 802.11 Wireless Controller",
70 elseif name == "ath" or name == "wifi" then
71 return translatef("Atheros 802.11%s Wireless Controller", bands)
74 elseif name == "ra" then
75 return translatef("RaLink 802.11%s Wireless Controller", bands)
78 elseif name == "eth" then
79 return translate("Hermes 802.11b Wireless Controller")
82 elseif name == "wlan" and fs.stat("/proc/net/hostap/" .. ifname, "type") == "dir" then
83 return translate("Prism2/2.5/3 802.11b Wireless Controller")
87 return translatef("Generic 802.11%s Wireless Controller", bands)
91 local devices = ntm:get_wifidevs()
93 ip.neighbors({ family = 4 }, function(n)
94 if n.mac and n.dest then arpcache[n.mac:upper()] = n.dest:string() end
101 for _, dev in ipairs(devices) do
103 for _, net in ipairs(dev:get_wifinets()) do
104 netlist[#netlist+1] = net:id()
105 netdevs[net:id()] = dev:name()
112 <% if not has_iwinfo then %>
113 <div class="errorbox">
114 <strong><%:Package libiwinfo required!%></strong><br />
115 <%_The <em>libiwinfo-lua</em> package is not installed. You must install this component for working wireless configuration!%>
119 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
120 <script type="text/javascript">//<![CDATA[
121 var wifidevs = <%=luci.http.write_json(netdevs)%>;
122 var arptable = <%=luci.http.write_json(arpcache)%>;
124 var is_reconnecting = false;
127 return s.replace(/ /g, ' ');
130 function wifi_shutdown(id, toggle) {
131 var reconnect = (toggle.getAttribute('active') == 'false');
133 if (!reconnect && !confirm(String.format('<%:Really shut down network?\nYou might lose access to this device if you are connected via this interface.%>')))
136 is_reconnecting = true;
138 var s = document.getElementById('iw-rc-status');
141 s.parentNode.style.display = 'block';
142 s.innerHTML = '<%:Waiting for changes to be applied...%>';
145 for (var net in wifidevs)
147 var st = document.getElementById(net + '-iw-status');
149 st.innerHTML = '<em><%:Wireless is restarting...%></em>';
152 (new XHR()).post('<%=url('admin/network')%>/wireless_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, { token: '<%=token%>' },
157 s.innerHTML = reconnect
158 ? '<%:Wireless restarted%>'
159 : '<%:Wireless shut down%>';
161 window.setTimeout(function() {
162 s.parentNode.style.display = 'none';
163 is_reconnecting = false;
170 function wifi_delete(id) {
171 if (!confirm('<%:Really delete this wireless network? The deletion cannot be undone!\nYou might lose access to this device if you are connected via this network.%>'))
174 (new XHR()).post('<%=url('admin/network/wireless_delete')%>/' + id, { token: '<%=token%>' },
176 location.href = '<%=url('admin/network/wireless')%>';
181 XHR.poll(5, '<%=url('admin/network/wireless_status', table.concat(netlist, ","))%>', null,
186 var assoctable = document.getElementById('iw-assoclist');
188 while (assoctable.rows.length > 1)
189 assoctable.rows[1].parentNode.removeChild(assoctable.rows[1]);
194 for( var i = 0; i < st.length; i++ )
197 var is_assoc = (iw.bssid && iw.bssid != '00:00:00:00:00:00' && iw.channel && iw.mode != 'Unknown' && !iw.disabled);
199 var q = is_assoc ? p : -1;
203 icon = "<%=resource%>/icons/signal-none.png";
205 icon = "<%=resource%>/icons/signal-0.png";
207 icon = "<%=resource%>/icons/signal-0-25.png";
209 icon = "<%=resource%>/icons/signal-25-50.png";
211 icon = "<%=resource%>/icons/signal-50-75.png";
213 icon = "<%=resource%>/icons/signal-75-100.png";
215 if (!devup[wifidevs[iw.id]])
216 devup[wifidevs[iw.id]] = is_assoc;
218 var sig = document.getElementById(iw.id + '-iw-signal');
220 sig.innerHTML = String.format(
221 '<img src="%s" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>" /><br />' +
222 '<small>%d%%</small>', icon, iw.signal, iw.noise, p
225 var toggle = document.getElementById(iw.id + '-iw-toggle');
230 toggle.className = 'cbi-button cbi-button-reset';
231 toggle.value = '<%:Disable%>';
232 toggle.title = '<%:Shutdown this network%>';
236 toggle.className = 'cbi-button cbi-button-reload';
237 toggle.value = '<%:Enable%>';
238 toggle.title = '<%:Activate this network%>';
241 toggle.setAttribute('active', !iw.disabled);
244 var info = document.getElementById(iw.id + '-iw-status');
248 info.innerHTML = String.format(
249 '<strong><%:SSID%>:</strong> %h | ' +
250 '<strong><%:Mode%>:</strong> %s<br />' +
251 '<strong><%:BSSID%>:</strong> %s | ' +
252 '<strong><%:Encryption%>:</strong> %s',
253 iw.ssid, iw.mode, iw.bssid,
254 iw.encryption ? iw.encryption : '<%:None%>'
257 info.innerHTML = String.format(
258 '<strong><%:SSID%>:</strong> %h | ' +
259 '<strong><%:Mode%>:</strong> %s<br />' +
261 iw.ssid || '?', iw.mode,
263 ? '<em><%:Wireless is restarting...%></em>'
264 : '<em><%:Wireless is disabled or not associated%></em>'
268 var dev = document.getElementById(wifidevs[iw.id] + '-iw-devinfo');
272 dev.innerHTML = String.format(
273 '<strong><%:Channel%>:</strong> %s (%s <%:GHz%>) | ' +
274 '<strong><%:Bitrate%>:</strong> %s <%:Mbit/s%>',
275 iw.channel ? iw.channel : '?',
276 iw.frequency ? iw.frequency : '?',
277 iw.bitrate ? iw.bitrate : '?'
286 for( var bssid in iw.assoclist )
288 assoclist.push(iw.assoclist[bssid]);
289 assoclist[assoclist.length-1].bssid = bssid;
292 assoclist.sort(function(a, b) { a.bssid < b.bssid });
294 for( var j = 0; j < assoclist.length; j++ )
296 var tr = assoctable.insertRow(-1);
297 tr.className = 'cbi-section-table-row cbi-rowstyle-' + rowstyle;
300 var q = (-1 * (assoclist[j].noise - assoclist[j].signal)) / 5;
302 icon = "<%=resource%>/icons/signal-0.png";
304 icon = "<%=resource%>/icons/signal-0-25.png";
306 icon = "<%=resource%>/icons/signal-25-50.png";
308 icon = "<%=resource%>/icons/signal-50-75.png";
310 icon = "<%=resource%>/icons/signal-75-100.png";
312 tr.insertCell(-1).innerHTML = String.format(
313 '<img src="%s" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>" />',
314 icon, assoclist[j].signal, assoclist[j].noise
317 tr.insertCell(-1).innerHTML = nowrap(String.format('%h', iw.ssid ? iw.ssid : '?'));
318 tr.insertCell(-1).innerHTML = assoclist[j].bssid;
320 tr.insertCell(-1).innerHTML = arptable[assoclist[j].bssid]
321 ? arptable[assoclist[j].bssid] : '?';
323 tr.insertCell(-1).innerHTML = nowrap(String.format('%d <%:dBm%>', assoclist[j].signal));
324 tr.insertCell(-1).innerHTML = nowrap(String.format('%d <%:dBm%>', assoclist[j].noise));
326 tr.insertCell(-1).innerHTML = nowrap((assoclist[j].rx_mcs > -1)
327 ? String.format('%.1f <%:Mbit/s%>, MCS %d, %d<%:MHz%>', assoclist[j].rx_rate / 1000, assoclist[j].rx_mcs, assoclist[j].rx_40mhz ? 40 : 20)
328 : String.format('%.1f <%:Mbit/s%>', assoclist[j].rx_rate / 1000)
331 tr.insertCell(-1).innerHTML = nowrap((assoclist[j].tx_mcs > -1)
332 ? String.format('%.1f <%:Mbit/s%>, MCS %d, %d<%:MHz%>', assoclist[j].tx_rate / 1000, assoclist[j].tx_mcs, assoclist[j].tx_40mhz ? 40 : 20)
333 : String.format('%.1f <%:Mbit/s%>', assoclist[j].tx_rate / 1000)
336 rowstyle = (rowstyle == 1) ? 2 : 1;
341 if (assoctable && assoctable.rows.length == 1)
343 var tr = assoctable.insertRow(-1);
344 tr.className = 'cbi-section-table-row';
346 var td = tr.insertCell(-1);
348 td.innerHTML = '<br /><em><%:No information available%></em>';
351 for (var dev in devup)
353 var img = document.getElementById(dev + '-iw-upstate');
355 img.src = '<%=resource%>/icons/wifi_big' + (devup[dev] ? '' : '_disabled') + '.png';
362 <h2 name="content"><%:Wireless Overview%></h2>
364 <fieldset class="cbi-section" style="display:none">
365 <legend><%:Reconnecting interface%></legend>
366 <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" />
367 <span id="iw-rc-status"><%:Waiting for changes to be applied...%></span>
370 <div class="cbi-map">
372 <% for _, dev in ipairs(devices) do local nets = dev:get_wifinets() %>
373 <!-- device <%=dev:name()%> -->
374 <fieldset class="cbi-section">
375 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
376 <!-- physical device -->
378 <td style="width:34px"><img src="<%=resource%>/icons/wifi_big_disabled.png" style="float:left; margin-right:10px" id="<%=dev:name()%>-iw-upstate" /></td>
379 <td colspan="2" style="text-align:left">
380 <big><strong><%=guess_wifi_hw(dev)%> (<%=dev:name()%>)</strong></big><br />
381 <span id="<%=dev:name()%>-iw-devinfo"></span>
383 <td style="width:310px;text-align:right">
384 <form action="<%=url('admin/network/wireless_join')%>" method="post" class="inline">
385 <input type="hidden" name="device" value="<%=dev:name()%>" />
386 <input type="hidden" name="token" value="<%=token%>" />
387 <input type="submit" class="cbi-button cbi-button-find" style="width:100px" title="<%:Find and join network%>" value="<%:Scan%>" />
389 <form action="<%=url('admin/network/wireless_add')%>" method="post" class="inline">
390 <input type="hidden" name="device" value="<%=dev:name()%>" />
391 <input type="hidden" name="token" value="<%=token%>" />
392 <input type="submit" class="cbi-button cbi-button-add" style="width:100px" title="<%:Provide new network%>" value="<%:Add%>" />
396 <!-- /physical device -->
398 <!-- network list -->
399 <% if #nets > 0 then %>
400 <% for i, net in ipairs(nets) do %>
401 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
403 <td class="cbi-value-field" style="width:16px; padding:3px" id="<%=net:id()%>-iw-signal">
404 <img src="<%=resource%>/icons/signal-none.png" title="<%:Not associated%>" /><br />
407 <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net:id()%>-iw-status">
408 <em><%:Collecting data...%></em>
410 <td class="cbi-value-field" style="width:310px;text-align:right">
411 <input id="<%=net:id()%>-iw-toggle" type="button" class="cbi-button cbi-button-reload" style="width:100px" onclick="wifi_shutdown('<%=net:id()%>', this)" title="<%:Delete this network%>" value="<%:Enable%>" />
412 <input type="button" class="cbi-button cbi-button-edit" style="width:100px" onclick="location.href='<%=net:adminlink()%>'" title="<%:Edit this network%>" value="<%:Edit%>" />
413 <input type="button" class="cbi-button cbi-button-remove" style="width:100px" onclick="wifi_delete('<%=net:ifname()%>')" title="<%:Delete this network%>" value="<%:Remove%>" />
418 <tr class="cbi-section-table-row cbi-rowstyle-2">
420 <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
421 <em><%:No network configured on this device%></em>
425 <!-- /network list -->
428 <!-- /device <%=dev:name()%> -->
432 <h2><%:Associated Stations%></h2>
434 <fieldset class="cbi-section">
435 <table class="cbi-section-table" style="margin:10px" id="iw-assoclist">
436 <tr class="cbi-section-table-titles">
437 <th class="cbi-section-table-cell"></th>
438 <th class="cbi-section-table-cell"><%:SSID%></th>
439 <th class="cbi-section-table-cell"><%:MAC-Address%></th>
440 <th class="cbi-section-table-cell"><%:IPv4-Address%></th>
441 <th class="cbi-section-table-cell"><%:Signal%></th>
442 <th class="cbi-section-table-cell"><%:Noise%></th>
443 <th class="cbi-section-table-cell"><%:RX Rate%></th>
444 <th class="cbi-section-table-cell"><%:TX Rate%></th>
446 <tr class="cbi-section-table-row cbi-rowstyle-2">
447 <td class="cbi-value-field" colspan="8">
448 <em><%:Collecting data...%></em>