From 3951b5a04a113e6cd50b4bcec4cea75b2f5e1bc0 Mon Sep 17 00:00:00 2001 From: hanwckf Date: Thu, 23 Nov 2023 18:50:06 +0800 Subject: [PATCH] mediatek: add mtwifi-cfg for configure mt_wifi --- package/mtk/applications/mtwifi-cfg/Makefile | 53 + .../mtwifi-cfg/files/hotplug/10-mtwifi-detect | 9 + .../mtwifi-cfg/files/l1util/l1dat_parser.lua | 263 ++ .../mtwifi-cfg/files/l1util/l1util | 56 + .../files/luci/mtwifi-cfg-luci.default | 3 + .../mtwifi-cfg/files/luci/wireless-mtk.js | 2251 +++++++++++++++++ .../mtwifi-cfg/files/mtwifi-cfg/inspect.lua | 337 +++ .../mtwifi-cfg/files/mtwifi-cfg/mtwifi_cfg | 538 ++++ .../files/mtwifi-cfg/mtwifi_defs.lua | 219 ++ .../files/mtwifi-cfg/mtwifi_utils.lua | 96 + .../applications/mtwifi-cfg/files/mtwifi.sh | 55 + .../mtwifi-cfg/files/netifd/mtwifi.sh | 140 + 12 files changed, 4020 insertions(+) create mode 100644 package/mtk/applications/mtwifi-cfg/Makefile create mode 100644 package/mtk/applications/mtwifi-cfg/files/hotplug/10-mtwifi-detect create mode 100644 package/mtk/applications/mtwifi-cfg/files/l1util/l1dat_parser.lua create mode 100755 package/mtk/applications/mtwifi-cfg/files/l1util/l1util create mode 100644 package/mtk/applications/mtwifi-cfg/files/luci/mtwifi-cfg-luci.default create mode 100644 package/mtk/applications/mtwifi-cfg/files/luci/wireless-mtk.js create mode 100644 package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/inspect.lua create mode 100755 package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_cfg create mode 100644 package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_defs.lua create mode 100644 package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_utils.lua create mode 100755 package/mtk/applications/mtwifi-cfg/files/mtwifi.sh create mode 100755 package/mtk/applications/mtwifi-cfg/files/netifd/mtwifi.sh diff --git a/package/mtk/applications/mtwifi-cfg/Makefile b/package/mtk/applications/mtwifi-cfg/Makefile new file mode 100644 index 0000000000..8cd8b3b64a --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/Makefile @@ -0,0 +1,53 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=mtwifi-cfg +PKG_VERSION:=1 +PKG_RELEASE:=1 + +include $(INCLUDE_DIR)/package.mk + +define Package/mtwifi-cfg + SECTION:=MTK Properties + CATEGORY:=MTK Properties + SUBMENU:=Applications + TITLE:=mtwifi configure scripts for mt798x + DEPENDS:=+iwinfo +wifi-dats +lua-cjson +datconf-lua @!PACKAGE_luci-app-mtk @!PACKAGE_wifi-profile +endef + +define Package/mtwifi-cfg-luci + SECTION:=MTK Properties + CATEGORY:=MTK Properties + SUBMENU:=Applications + TITLE:=luci files for mtwifi configure scripts + DEPENDS:=+mtwifi-cfg +luci +endef + +define Build/Compile +endef + +define Package/mtwifi-cfg/install + $(INSTALL_DIR) $(1)/sbin $(1)/usr/lib/lua + $(INSTALL_DIR) $(1)/lib/netifd/wireless/ $(1)/lib/wifi/ + + $(INSTALL_DIR) $(1)/etc/hotplug.d/net/ + + $(INSTALL_BIN) ./files/netifd/mtwifi.sh $(1)/lib/netifd/wireless/ + $(INSTALL_BIN) ./files/mtwifi.sh $(1)/lib/wifi/ + $(INSTALL_BIN) ./files/mtwifi-cfg/mtwifi_cfg $(1)/sbin/ + $(INSTALL_BIN) ./files/mtwifi-cfg/inspect.lua $(1)/usr/lib/lua/ + $(INSTALL_BIN) ./files/mtwifi-cfg/mtwifi_defs.lua $(1)/usr/lib/lua/ + $(INSTALL_BIN) ./files/mtwifi-cfg/mtwifi_utils.lua $(1)/usr/lib/lua/ + $(INSTALL_BIN) ./files/l1util/l1util $(1)/sbin/ + $(INSTALL_BIN) ./files/l1util/l1dat_parser.lua $(1)/usr/lib/lua/ + $(INSTALL_DATA) ./files/hotplug/10-mtwifi-detect $(1)/etc/hotplug.d/net/ +endef + +define Package/mtwifi-cfg-luci/install + $(INSTALL_DIR) $(1)/etc/uci-defaults/ $(1)/www/luci-static/resources/view/network/ + + $(INSTALL_DATA) ./files/luci/wireless-mtk.js $(1)/www/luci-static/resources/view/network/wireless-mtk.js + $(INSTALL_DATA) ./files/luci/mtwifi-cfg-luci.default $(1)/etc/uci-defaults/20-mtwifi-cfg-luci.default +endef + +$(eval $(call BuildPackage,mtwifi-cfg)) +$(eval $(call BuildPackage,mtwifi-cfg-luci)) diff --git a/package/mtk/applications/mtwifi-cfg/files/hotplug/10-mtwifi-detect b/package/mtk/applications/mtwifi-cfg/files/hotplug/10-mtwifi-detect new file mode 100644 index 0000000000..3b3ad810ca --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/files/hotplug/10-mtwifi-detect @@ -0,0 +1,9 @@ +#!/bin/sh + +[ "${ACTION}" = "add" ] && [ "${INTERFACE%%[0-9]}" = "ra" ] && { + if [ ! -f /etc/config/wireless ]; then + need_up=1 + fi + /sbin/wifi config + [ "$need_up" = "1" ] && /sbin/wifi up +} diff --git a/package/mtk/applications/mtwifi-cfg/files/l1util/l1dat_parser.lua b/package/mtk/applications/mtwifi-cfg/files/l1util/l1dat_parser.lua new file mode 100644 index 0000000000..a3e740038a --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/files/l1util/l1dat_parser.lua @@ -0,0 +1,263 @@ +#!/usr/bin/env lua + +--[[ + * A lua library to manipulate mtk's wifi driver. used in luci-app-mtk. + * + * Copyright (C) 2016 MTK + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. +]] + +local l1dat_parser = { + L1_DAT_PATH = "/etc/wireless/l1profile.dat", + IF_RINDEX = "ifname_ridx", + DEV_RINDEX = "devname_ridx", + MAX_NUM_APCLI = 1, + MAX_NUM_WDS = 4, + MAX_NUM_MESH = 1, + MAX_NUM_EXTIF = 16, + MAX_NUM_DBDC_BAND = 2, +} + +local l1cfg_options = { + ext_ifname="", + apcli_ifname="apcli", + wds_ifname="wds", + mesh_ifname="mesh" +} + +function l1dat_parser.__trim(s) + if s then return (s:gsub("^%s*(.-)%s*$", "%1")) end +end + +function l1dat_parser.__cfg2list(str) + -- delimeter == ";" + local i = 1 + local list = {} + for k in string.gmatch(str, "([^;]+)") do + list[i] = k + i = i + 1 + end + return list +end + +function l1dat_parser.token_get(str, n, v) + -- n starts from 1 + -- v is the backup in case token n is nil + if not str then return v end + local tmp = l1dat_parser.__cfg2list(str) + return tmp[tonumber(n)] or v +end + +function l1dat_parser.add_default_value(l1cfg) + for k, v in ipairs(l1cfg) do + + for opt, default in pairs(l1cfg_options) do + if ( opt == "ext_ifname" ) then + v[opt] = v[opt] or v["main_ifname"].."_" + else + v[opt] = v[opt] or default..k.."_" + end + end + end + + return l1cfg +end + +function l1dat_parser.get_value_by_idx(devidx, mainidx, subidx, key) + --print("Enter l1dat_parser.get_value_by_idx("..devidx..","..mainidx..", "..subidx..", "..key..")
") + if not devidx or not mainidx or not key then return end + + local devs = l1dat_parser.load_l1_profile(l1dat_parser.L1_DAT_PATH) + if not devs then return end + + local dev_ridx = l1dat_parser.DEV_RINDEX + local sidx = subidx or 1 + local devname1 = devidx.."_"..mainidx + local devname2 = devidx.."_"..mainidx.."_"..sidx + + --print("devnam1=", devname1, "devname2=", devname2, "
") + return devs[dev_ridx][devname2] and devs[dev_ridx][devname2][key] + or devs[dev_ridx][devname1] and devs[dev_ridx][devname1][key] +end + +function l1dat_parser.l1_ifname_to_datpath(ifname) + if not ifname then return end + + local devs = l1dat_parser.load_l1_profile(l1dat_parser.L1_DAT_PATH) + if not devs then return end + + local ridx = l1dat_parser.IF_RINDEX + return devs[ridx][ifname] and devs[ridx][ifname].profile_path +end + +-- input: L1 profile path. +-- output A table, devs, contains +-- 1. devs[%d] = table of each INDEX# in the L1 profile +-- 2. devs.ifname_ridx[ifname] +-- = table of each ifname and point to relevant contain in dev[$d] +-- 3. devs.devname_ridx[devname] similar to devs.ifnameridx, but use devname. +-- devname = INDEX#_value.mainidx(.subidx) +-- Using *_ridx do not need to handle name=k1;k2 case of DBDC card. +function l1dat_parser.load_l1_profile(path) + local devs = setmetatable({}, {__index= + function(tbl, key) + --local util = require("luci.util") + --print("metatable function:", util.serialize_data(tbl), key) + --print("-----------------------------------------------") + if ( string.match(key, "^%d+")) then + tbl[key] = {} + return tbl[key] + end + end + }) + local nixio = require("nixio") + local chipset_num = {} + local dir = io.popen("ls /etc/wireless/") + if not dir then return end + local fd = io.open(path, "r") + if not fd then return end + + -- convert l1 profile into lua table + for line in fd:lines() do + line = l1dat_parser.__trim(line) + if string.byte(line) ~= string.byte("#") then + local i = string.find(line, "=") + if i then + local k, v, k1, k2 + k = l1dat_parser.__trim( string.sub(line, 1, i-1) ) + v = l1dat_parser.__trim( string.sub(line, i+1) ) + k1, k2 = string.match(k, "INDEX(%d+)_(.+)") + if k1 then + k1 = tonumber(k1) + 1 + if devs[k1][k2] then + nixio.syslog("warning", "skip repeated key"..line) + end + devs[k1][k2] = v or "" + else + k1 = string.match(k, "INDEX(%d+)") + k1 = tonumber(k1) + 1 + devs[k1]["INDEX"] = v + + chipset_num[v] = (not chipset_num[v] and 1) or chipset_num[v] + 1 + devs[k1]["mainidx"] = chipset_num[v] + end + --else + -- nixio.syslog("warning", "skip line without '=' "..line) + end + --else + -- nixio.syslog("warning", "skip comment line "..line) + end + end + + l1dat_parser.add_default_value(devs) + --local util = require("luci.util") + --local seen2 = {} + -- print("Before setup ridx", util.serialize_data(devs, seen2)) + + -- Force to setup reverse indice for quick search. + -- Benifit: + -- 1. O(1) search with ifname, devname + -- 2. Seperate DBDC name=k1;k2 format in the L1 profile into each + -- ifname, devname. + local dbdc_if = {} + local ridx = l1dat_parser.IF_RINDEX + local dridx = l1dat_parser.DEV_RINDEX + local band_num = l1dat_parser.MAX_NUM_DBDC_BAND + local k, v, dev, i , j, last + local devname + devs[ridx] = {} + devs[dridx] = {} + for _, dev in ipairs(devs) do + dbdc_if[band_num] = l1dat_parser.token_get(dev.main_ifname, band_num, nil) + if dbdc_if[band_num] then + for i = 1, band_num - 1 do + dbdc_if[i] = l1dat_parser.token_get(dev.main_ifname, i, nil) + end + for i = 1, band_num do + devs[ridx][dbdc_if[i]] = {} + devs[ridx][dbdc_if[i]]["subidx"] = i + + for k, v in pairs(dev) do + if k == "INDEX" or k == "EEPROM_offset" or k == "EEPROM_size" + or k == "mainidx" then + devs[ridx][dbdc_if[i]][k] = v + else + devs[ridx][dbdc_if[i]][k] = l1dat_parser.token_get(v, i, "") + end + end + devname = dev.INDEX.."_"..dev.mainidx.."_"..devs[ridx][dbdc_if[i]]["subidx"] + devs[dridx][devname] = devs[ridx][dbdc_if[i]] + end + + local apcli_if, wds_if, ext_if, mesh_if = {}, {}, {}, {} + + for i = 1, band_num do + ext_if[i] = l1dat_parser.token_get(dev.ext_ifname, i, nil) + apcli_if[i] = l1dat_parser.token_get(dev.apcli_ifname, i, nil) + wds_if[i] = l1dat_parser.token_get(dev.wds_ifname, i, nil) + mesh_if[i] = l1dat_parser.token_get(dev.mesh_ifname, i, nil) + end + + for i = 1, l1dat_parser.MAX_NUM_EXTIF - 1 do -- ifname idx is from 0 + for j = 1, band_num do + devs[ridx][ext_if[j]..i] = devs[ridx][dbdc_if[j]] + end + end + + for i = 0, l1dat_parser.MAX_NUM_APCLI - 1 do + for j = 1, band_num do + devs[ridx][apcli_if[j]..i] = devs[ridx][dbdc_if[j]] + end + end + + for i = 0, l1dat_parser.MAX_NUM_WDS - 1 do + for j = 1, band_num do + devs[ridx][wds_if[j]..i] = devs[ridx][dbdc_if[j]] + end + end + + for i = 0, l1dat_parser.MAX_NUM_MESH - 1 do + for j = 1, band_num do + if mesh_if[j] then + devs[ridx][mesh_if[j]..i] = devs[ridx][dbdc_if[j]] + end + end + end + + else + devs[ridx][dev.main_ifname] = dev + + devname = dev.INDEX.."_"..dev.mainidx + devs[dridx][devname] = dev + + for i = 1, l1dat_parser.MAX_NUM_EXTIF - 1 do -- ifname idx is from 0 + devs[ridx][dev.ext_ifname..i] = dev + end + + for i = 0, l1dat_parser.MAX_NUM_APCLI - 1 do -- ifname idx is from 0 + devs[ridx][dev.apcli_ifname..i] = dev + end + + for i = 0, l1dat_parser.MAX_NUM_WDS - 1 do -- ifname idx is from 0 + devs[ridx][dev.wds_ifname..i] = dev + end + + for i = 0, l1dat_parser.MAX_NUM_MESH - 1 do -- ifname idx is from 0 + devs[ridx][dev.mesh_ifname..i] = dev + end + end + end + + fd:close() + return devs +end + +return l1dat_parser diff --git a/package/mtk/applications/mtwifi-cfg/files/l1util/l1util b/package/mtk/applications/mtwifi-cfg/files/l1util/l1util new file mode 100755 index 0000000000..285357682f --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/files/l1util/l1util @@ -0,0 +1,56 @@ +#!/usr/bin/lua +--[[ + * + * Copyright (C) 2023 hanwckf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * The Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. +]] + +local l1parser = require("l1dat_parser") +local l1dat = l1parser.load_l1_profile(l1parser.L1_DAT_PATH) + +if not l1dat then + return +end + +function show_devs() + local ret = "" + for k, _ in pairs(l1dat.devname_ridx) do + if #ret > 0 then + ret = ret .. " " .. k + else + ret = k + end + end + print(ret) +end + +function get_dev_prop(dev, prop) + if l1dat.devname_ridx[dev] then + print(l1dat.devname_ridx[dev][prop]) + end +end + +local action = { + ["list"] = function() + show_devs() + end, + + ["get"] = function(dev, prop) + get_dev_prop(dev, prop) + end +} + +if #arg == 1 then + action[arg[1]]() +elseif #arg == 3 then + action[arg[1]](arg[2], arg[3]) +end diff --git a/package/mtk/applications/mtwifi-cfg/files/luci/mtwifi-cfg-luci.default b/package/mtk/applications/mtwifi-cfg/files/luci/mtwifi-cfg-luci.default new file mode 100644 index 0000000000..c8447fa794 --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/files/luci/mtwifi-cfg-luci.default @@ -0,0 +1,3 @@ +#!/bin/sh + +mv /www/luci-static/resources/view/network/wireless-mtk.js /www/luci-static/resources/view/network/wireless.js diff --git a/package/mtk/applications/mtwifi-cfg/files/luci/wireless-mtk.js b/package/mtk/applications/mtwifi-cfg/files/luci/wireless-mtk.js new file mode 100644 index 0000000000..7d0b427480 --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/files/luci/wireless-mtk.js @@ -0,0 +1,2251 @@ +'use strict'; +'require view'; +'require dom'; +'require poll'; +'require fs'; +'require ui'; +'require rpc'; +'require uci'; +'require form'; +'require network'; +'require firewall'; +'require tools.widgets as widgets'; + +var isReadonlyView = !L.hasViewPermission(); + +function count_changes(section_id) { + var changes = ui.changes.changes, n = 0; + + if (!L.isObject(changes)) + return n; + + if (Array.isArray(changes.wireless)) + for (var i = 0; i < changes.wireless.length; i++) + n += (changes.wireless[i][1] == section_id); + + return n; +} + +function render_radio_badge(radioDev) { + return E('span', { 'class': 'ifacebadge' }, [ + E('img', { 'src': L.resource('icons/wifi%s.png').format(radioDev.isUp() ? '' : '_disabled') }), + ' ', + radioDev.getName() + ]); +} + +function render_signal_badge(signalPercent, signalValue, noiseValue, wrap, mode) { + var icon, title, value; + + if (signalPercent < 0) + icon = L.resource('icons/signal-none.png'); + else if (signalPercent == 0) + icon = L.resource('icons/signal-0.png'); + else if (signalPercent < 25) + icon = L.resource('icons/signal-0-25.png'); + else if (signalPercent < 50) + icon = L.resource('icons/signal-25-50.png'); + else if (signalPercent < 75) + icon = L.resource('icons/signal-50-75.png'); + else + icon = L.resource('icons/signal-75-100.png'); + + if (signalValue != null && signalValue != 0) { + if (noiseValue != null && noiseValue != 0) { + value = '%d/%d\xa0%s'.format(signalValue, noiseValue, _('dBm')); + title = '%s: %d %s / %s: %d %s / %s %d'.format( + _('Signal'), signalValue, _('dBm'), + _('Noise'), noiseValue, _('dBm'), + _('SNR'), signalValue - noiseValue); + } + else { + value = '%d\xa0%s'.format(signalValue, _('dBm')); + title = '%s: %d %s'.format(_('Signal'), signalValue, _('dBm')); + } + } + else if (signalPercent > -1) { + switch (mode) { + case 'ap': + title = _('No client associated'); + break; + + case 'sta': + case 'adhoc': + case 'mesh': + title = _('Not associated'); + break; + + default: + title = _('No RX signal'); + } + + if (noiseValue != null && noiseValue != 0) { + value = '---/%d\x0a%s'.format(noiseValue, _('dBm')); + title = '%s / %s: %d %s'.format(title, _('Noise'), noiseValue, _('dBm')); + } + else { + value = '---\xa0%s'.format(_('dBm')); + } + } + else { + value = E('em', {}, E('small', {}, [ _('disabled') ])); + title = _('Interface is disabled'); + } + + return E('div', { + 'class': wrap ? 'center' : 'ifacebadge', + 'title': title, + 'data-signal': signalValue, + 'data-noise': noiseValue + }, [ + E('img', { 'src': icon }), + E('span', {}, [ + wrap ? E('br') : ' ', + value + ]) + ]); +} + +function render_network_badge(radioNet) { + return render_signal_badge( + radioNet.isUp() ? radioNet.getSignalPercent() : -1, + radioNet.getSignal(), radioNet.getNoise(), false, radioNet.getMode()); +} + +function render_radio_status(radioDev, wifiNets) { + var name = radioDev.getI18n().replace(/ Wireless Controller .+$/, ''), + node = E('div', [ E('big', {}, E('strong', {}, name)), E('div') ]), + channel, frequency, bitrate; + + for (var i = 0; i < wifiNets.length; i++) { + channel = channel || wifiNets[i].getChannel(); + frequency = frequency || wifiNets[i].getFrequency(); + bitrate = bitrate || wifiNets[i].getBitRate(); + } + + if (radioDev.isUp()) + L.itemlist(node.lastElementChild, [ + _('Channel'), '%s (%s %s)'.format(channel || '?', frequency || '?', _('GHz')), + _('Bitrate'), '%s %s'.format(bitrate || '?', _('Mbit/s')) + ], ' | '); + else + node.lastElementChild.appendChild(E('em', _('Device is not active'))); + + return node; +} + +function render_network_status(radioNet) { + var mode = radioNet.getActiveMode(), + bssid = radioNet.getActiveBSSID(), + channel = radioNet.getChannel(), + disabled = (radioNet.get('disabled') == '1' || uci.get('wireless', radioNet.getWifiDeviceName(), 'disabled') == '1'), + is_assoc = (bssid && bssid != '00:00:00:00:00:00' && channel && mode != 'Unknown' && !disabled), + is_mesh = (radioNet.getMode() == 'mesh'), + changecount = count_changes(radioNet.getName()), + status_text = null; + + if (changecount) + status_text = E('a', { + href: '#', + click: L.bind(ui.changes.displayChanges, ui.changes) + }, _('Interface has %d pending changes').format(changecount)); + else if (!is_assoc) + status_text = E('em', disabled ? _('Wireless is disabled') : _('Wireless is not associated')); + + return L.itemlist(E('div'), [ + is_mesh ? _('Mesh ID') : _('SSID'), (is_mesh ? radioNet.getMeshID() : radioNet.getSSID()) || '?', + _('Mode'), mode, + _('BSSID'), (!changecount && is_assoc) ? bssid : null, + _('Encryption'), (!changecount && is_assoc) ? radioNet.getActiveEncryption() || _('None') : null, + null, status_text + ], [ ' | ', E('br') ]); +} + +function render_modal_status(node, radioNet) { + var mode = radioNet.getActiveMode(), + noise = radioNet.getNoise(), + bssid = radioNet.getActiveBSSID(), + channel = radioNet.getChannel(), + disabled = (radioNet.get('disabled') == '1'), + is_assoc = (bssid && bssid != '00:00:00:00:00:00' && channel && mode != 'Unknown' && !disabled); + + if (node == null) + node = E('span', { 'class': 'ifacebadge large', 'data-network': radioNet.getName() }, [ E('small'), E('span') ]); + + dom.content(node.firstElementChild, render_signal_badge( + disabled ? -1 : radioNet.getSignalPercent(), + radioNet.getSignal(), noise, true, radioNet.getMode())); + + L.itemlist(node.lastElementChild, [ + _('Mode'), mode, + _('SSID'), radioNet.getSSID() || '?', + _('BSSID'), is_assoc ? bssid : null, + _('Encryption'), is_assoc ? radioNet.getActiveEncryption() || _('None') : null, + _('Channel'), is_assoc ? '%d (%.3f %s)'.format(radioNet.getChannel(), radioNet.getFrequency() || 0, _('GHz')) : null, + _('Tx-Power'), is_assoc ? '%d %s'.format(radioNet.getTXPower(), _('dBm')) : null, + _('Signal'), is_assoc ? '%d %s'.format(radioNet.getSignal(), _('dBm')) : null, + _('Noise'), (is_assoc && noise != null) ? '%d %s'.format(noise, _('dBm')) : null, + _('Bitrate'), is_assoc ? '%.1f %s'.format(radioNet.getBitRate() || 0, _('Mbit/s')) : null, + _('Country'), is_assoc ? radioNet.getCountryCode() : null + ], [ ' | ', E('br'), E('br'), E('br'), E('br'), E('br'), ' | ', E('br'), ' | ' ]); + + if (!is_assoc) + dom.append(node.lastElementChild, E('em', disabled ? _('Wireless is disabled') : _('Wireless is not associated'))); + + return node; +} + +function format_wifirate(rate) { + var s = '%.1f\xa0%s, %d\xa0%s'.format(rate.rate / 1000, _('Mbit/s'), rate.mhz, _('MHz')), + ht = rate.ht, vht = rate.vht, + mhz = rate.mhz, nss = rate.nss, + mcs = rate.mcs, sgi = rate.short_gi, + he = rate.he, he_gi = rate.he_gi, + he_dcm = rate.he_dcm; + + if (ht || vht) { + if (vht) s += ', VHT-MCS\xa0%d'.format(mcs); + if (nss) s += ', VHT-NSS\xa0%d'.format(nss); + if (ht) s += ', MCS\xa0%s'.format(mcs); + if (sgi) s += ', ' + _('Short GI').replace(/ /g, '\xa0'); + } + + if (he) { + s += ', HE-MCS\xa0%d'.format(mcs); + if (nss) s += ', HE-NSS\xa0%d'.format(nss); + if (he_gi) s += ', HE-GI\xa0%d'.format(he_gi); + if (he_dcm) s += ', HE-DCM\xa0%d'.format(he_dcm); + } + + return s; +} + +function radio_restart(id, ev) { + var row = document.querySelector('.cbi-section-table-row[data-sid="%s"]'.format(id)), + dsc = row.querySelector('[data-name="_stat"] > div'), + btn = row.querySelector('.cbi-section-actions button'); + + btn.blur(); + btn.classList.add('spinning'); + btn.disabled = true; + + dsc.setAttribute('restart', ''); + dom.content(dsc, E('em', _('Device is restarting…'))); +} + +function network_updown(id, map, ev) { + var radio = uci.get('wireless', id, 'device'), + disabled = (uci.get('wireless', id, 'disabled') == '1') || + (uci.get('wireless', radio, 'disabled') == '1'); + + if (disabled) { + uci.unset('wireless', id, 'disabled'); + uci.unset('wireless', radio, 'disabled'); + } + else { + uci.set('wireless', id, 'disabled', '1'); + + var all_networks_disabled = true, + wifi_ifaces = uci.sections('wireless', 'wifi-iface'); + + for (var i = 0; i < wifi_ifaces.length; i++) { + if (wifi_ifaces[i].device == radio && wifi_ifaces[i].disabled != '1') { + all_networks_disabled = false; + break; + } + } + + if (all_networks_disabled) + uci.set('wireless', radio, 'disabled', '1'); + } + + return map.save().then(function() { + ui.changes.apply() + }); +} + +function next_free_sid(offset) { + var sid = 'wifinet' + offset; + + while (uci.get('wireless', sid)) + sid = 'wifinet' + (++offset); + + return sid; +} + +function add_dependency_permutations(o, deps) { + var res = null; + + for (var key in deps) { + if (!deps.hasOwnProperty(key) || !Array.isArray(deps[key])) + continue; + + var list = deps[key], + tmp = []; + + for (var j = 0; j < list.length; j++) { + for (var k = 0; k < (res ? res.length : 1); k++) { + var item = (res ? Object.assign({}, res[k]) : {}); + item[key] = list[j]; + tmp.push(item); + } + } + + res = tmp; + } + + for (var i = 0; i < (res ? res.length : 0); i++) + o.depends(res[i]); +} + +var CBIWifiFrequencyValue = form.Value.extend({ + callFrequencyList: rpc.declare({ + object: 'iwinfo', + method: 'freqlist', + params: [ 'device' ], + expect: { results: [] } + }), + + load: function(section_id) { + return Promise.all([ + network.getWifiDevice(section_id), + this.callFrequencyList(section_id) + ]).then(L.bind(function(data) { + this.channels = { + '2g': L.hasSystemFeature('hostapd', 'acs') ? [ 'auto', 'auto', true ] : [], + '5g': L.hasSystemFeature('hostapd', 'acs') ? [ 'auto', 'auto', true ] : [], + '6g': [], + '60g': [] + }; + + for (var i = 0; i < data[1].length; i++) { + var band; + + if (data[1][i].mhz >= 2412 && data[1][i].mhz <= 2484) + band = '2g'; + else if (data[1][i].mhz >= 5160 && data[1][i].mhz <= 5885) + band = '5g'; + else if (data[1][i].mhz >= 5925 && data[1][i].mhz <= 7125) + band = '6g'; + else if (data[1][i].mhz >= 58320 && data[1][i].mhz <= 69120) + band = '60g'; + else + continue; + + this.channels[band].push( + data[1][i].channel, + '%d (%d Mhz)'.format(data[1][i].channel, data[1][i].mhz), + !data[1][i].restricted + ); + } + + var hwmodelist = L.toArray(data[0] ? data[0].getHWModes() : null) + .reduce(function(o, v) { o[v] = true; return o }, {}); + + this.modes = [ + '', 'Legacy', true, + 'n', 'N', hwmodelist.n, + 'ac', 'AC', hwmodelist.ac, + 'ax', 'AX', hwmodelist.ax + ]; + + var htmodelist = L.toArray(data[0] ? data[0].getHTModes() : null) + .reduce(function(o, v) { o[v] = true; return o }, {}); + + this.htmodes = { + '': [ '', '-', true ], + 'n': [ + 'HT20', '20 MHz', htmodelist.HT20, + 'HT40', '40 MHz', htmodelist.HT40 + ], + 'ac': [ + 'VHT20', '20 MHz', htmodelist.VHT20, + 'VHT40', '40 MHz', htmodelist.VHT40, + 'VHT80', '80 MHz', htmodelist.VHT80, + 'VHT160', '160 MHz', htmodelist.VHT160 + ], + 'ax': [ + 'HE20', '20 MHz', htmodelist.HE20, + 'HE40', '40 MHz', htmodelist.HE40, + 'HE80', '80 MHz', htmodelist.HE80, + 'HE160', '160 MHz', htmodelist.HE160 + ] + }; + + this.bands = { + '': [ + '2g', '2.4 GHz', this.channels['2g'].length > 3, + '5g', '5 GHz', this.channels['5g'].length > 3, + '60g', '60 GHz', this.channels['60g'].length > 0 + ], + 'n': [ + '2g', '2.4 GHz', this.channels['2g'].length > 3, + '5g', '5 GHz', this.channels['5g'].length > 3 + ], + 'ac': [ + '5g', '5 GHz', true + ], + 'ax': [ + '2g', '2.4 GHz', this.channels['2g'].length > 3, + '5g', '5 GHz', this.channels['5g'].length > 3 + ] + }; + }, this)); + }, + + setValues: function(sel, vals) { + if (sel.vals) + sel.vals.selected = sel.selectedIndex; + + while (sel.options[0]) + sel.remove(0); + + for (var i = 0; vals && i < vals.length; i += 3) + if (vals[i+2]) + sel.add(E('option', { value: vals[i+0] }, [ vals[i+1] ])); + + if (vals && !isNaN(vals.selected)) + sel.selectedIndex = vals.selected; + + sel.parentNode.style.display = (sel.options.length <= 1) ? 'none' : ''; + sel.vals = vals; + }, + + toggleWifiMode: function(elem) { + this.toggleWifiHTMode(elem); + this.toggleWifiBand(elem); + }, + + toggleWifiHTMode: function(elem) { + var mode = elem.querySelector('.mode'); + var bwdt = elem.querySelector('.htmode'); + + this.setValues(bwdt, this.htmodes[mode.value]); + }, + + toggleWifiBand: function(elem) { + var mode = elem.querySelector('.mode'); + var band = elem.querySelector('.band'); + + this.setValues(band, this.bands[mode.value]); + this.toggleWifiChannel(elem); + + this.map.checkDepends(); + }, + + toggleWifiChannel: function(elem) { + var band = elem.querySelector('.band'); + var chan = elem.querySelector('.channel'); + + this.setValues(chan, this.channels[band.value]); + }, + + setInitialValues: function(section_id, elem) { + var mode = elem.querySelector('.mode'), + band = elem.querySelector('.band'), + chan = elem.querySelector('.channel'), + bwdt = elem.querySelector('.htmode'), + htval = uci.get('wireless', section_id, 'htmode'), + hwval = uci.get('wireless', section_id, 'hwmode'), + chval = uci.get('wireless', section_id, 'channel'), + bandval = uci.get('wireless', section_id, 'band'); + + this.setValues(mode, this.modes); + + if (/HE20|HE40|HE80|HE160/.test(htval)) + mode.value = 'ax'; + else if (/VHT20|VHT40|VHT80|VHT160/.test(htval)) + mode.value = 'ac'; + else if (/HT20|HT40/.test(htval)) + mode.value = 'n'; + else + mode.value = ''; + + this.toggleWifiMode(elem); + + if (hwval != null) { + this.useBandOption = false; + + if (/a/.test(hwval)) + band.value = '5g'; + else + band.value = '2g'; + } + else { + this.useBandOption = true; + + band.value = bandval; + } + + this.toggleWifiBand(elem); + + bwdt.value = htval; + chan.value = chval || chan.options[0].value; + + return elem; + }, + + renderWidget: function(section_id, option_index, cfgvalue) { + var elem = E('div'); + + dom.content(elem, [ + E('label', { 'style': 'float:left; margin-right:3px' }, [ + _('Mode'), E('br'), + E('select', { + 'class': 'mode', + 'style': 'width:auto', + 'change': L.bind(this.toggleWifiMode, this, elem), + 'disabled': (this.disabled != null) ? this.disabled : this.map.readonly + }) + ]), + E('label', { 'style': 'float:left; margin-right:3px' }, [ + _('Band'), E('br'), + E('select', { + 'class': 'band', + 'style': 'width:auto', + 'change': L.bind(this.toggleWifiBand, this, elem), + 'disabled': (this.disabled != null) ? this.disabled : this.map.readonly + }) + ]), + E('label', { 'style': 'float:left; margin-right:3px' }, [ + _('Channel'), E('br'), + E('select', { + 'class': 'channel', + 'style': 'width:auto', + 'change': L.bind(this.map.checkDepends, this.map), + 'disabled': (this.disabled != null) ? this.disabled : this.map.readonly + }) + ]), + E('label', { 'style': 'float:left; margin-right:3px' }, [ + _('Width'), E('br'), + E('select', { + 'class': 'htmode', + 'style': 'width:auto', + 'change': L.bind(this.map.checkDepends, this.map), + 'disabled': (this.disabled != null) ? this.disabled : this.map.readonly + }) + ]), + E('br', { 'style': 'clear:left' }) + ]); + + return this.setInitialValues(section_id, elem); + }, + + cfgvalue: function(section_id) { + return [ + uci.get('wireless', section_id, 'htmode'), + uci.get('wireless', section_id, 'hwmode') || uci.get('wireless', section_id, 'band'), + uci.get('wireless', section_id, 'channel') + ]; + }, + + formvalue: function(section_id) { + var node = this.map.findElement('data-field', this.cbid(section_id)); + + return [ + node.querySelector('.htmode').value, + node.querySelector('.band').value, + node.querySelector('.channel').value + ]; + }, + + write: function(section_id, value) { + uci.set('wireless', section_id, 'htmode', value[0] || null); + + if (this.useBandOption) + uci.set('wireless', section_id, 'band', value[1]); + else + uci.set('wireless', section_id, 'hwmode', (value[1] == '2g') ? '11g' : '11a'); + + uci.set('wireless', section_id, 'channel', value[2]); + } +}); + +var CBIWifiTxPowerValue = form.ListValue.extend({ + callTxPowerList: rpc.declare({ + object: 'iwinfo', + method: 'txpowerlist', + params: [ 'device' ], + expect: { results: [] } + }), + + load: function(section_id) { + return this.callTxPowerList(section_id).then(L.bind(function(pwrlist) { + this.powerval = this.wifiNetwork ? this.wifiNetwork.getTXPower() : null; + this.poweroff = this.wifiNetwork ? this.wifiNetwork.getTXPowerOffset() : null; + + this.value('', _('driver default')); + + for (var i = 0; i < pwrlist.length; i++) + this.value(pwrlist[i].dbm, '%d dBm (%d mW)'.format(pwrlist[i].dbm, pwrlist[i].mw)); + + return form.ListValue.prototype.load.apply(this, [section_id]); + }, this)); + }, + + renderWidget: function(section_id, option_index, cfgvalue) { + var widget = form.ListValue.prototype.renderWidget.apply(this, [section_id, option_index, cfgvalue]); + widget.firstElementChild.style.width = 'auto'; + + dom.append(widget, E('span', [ + ' - ', _('Current power'), ': ', + E('span', [ this.powerval != null ? '%d dBm'.format(this.powerval) : E('em', _('unknown')) ]), + this.poweroff ? ' + %d dB offset = %s dBm'.format(this.poweroff, this.powerval != null ? this.powerval + this.poweroff : '?') : '' + ])); + + return widget; + } +}); + +var CBIWifiCountryValue = form.Value.extend({ + callCountryList: rpc.declare({ + object: 'iwinfo', + method: 'countrylist', + params: [ 'device' ], + expect: { results: [] } + }), + + load: function(section_id) { + return this.callCountryList(section_id).then(L.bind(function(countrylist) { + if (Array.isArray(countrylist) && countrylist.length > 0) { + this.value('', _('driver default')); + + for (var i = 0; i < countrylist.length; i++) + this.value(countrylist[i].iso3166, '%s - %s'.format(countrylist[i].iso3166, countrylist[i].country)); + } + + return form.Value.prototype.load.apply(this, [section_id]); + }, this)); + }, + + validate: function(section_id, formvalue) { + if (formvalue != null && formvalue != '' && !/^[A-Z0-9][A-Z0-9]$/.test(formvalue)) + return _('Use ISO/IEC 3166 alpha2 country codes.'); + + return true; + }, + + renderWidget: function(section_id, option_index, cfgvalue) { + var typeClass = (this.keylist && this.keylist.length) ? form.ListValue : form.Value; + return typeClass.prototype.renderWidget.apply(this, [section_id, option_index, cfgvalue]); + } +}); + +return view.extend({ + poll_status: function(map, data) { + var rows = map.querySelectorAll('.cbi-section-table-row[data-sid]'); + + for (var i = 0; i < rows.length; i++) { + var section_id = rows[i].getAttribute('data-sid'), + radioDev = data[1].filter(function(d) { return d.getName() == section_id })[0], + radioNet = data[2].filter(function(n) { return n.getName() == section_id })[0], + badge = rows[i].querySelector('[data-name="_badge"] > div'), + stat = rows[i].querySelector('[data-name="_stat"]'), + btns = rows[i].querySelectorAll('.cbi-section-actions button'), + busy = btns[0].classList.contains('spinning') || btns[1].classList.contains('spinning') || btns[2].classList.contains('spinning'); + + if (radioDev) { + dom.content(badge, render_radio_badge(radioDev)); + dom.content(stat, render_radio_status(radioDev, data[2].filter(function(n) { return n.getWifiDeviceName() == radioDev.getName() }))); + } + else { + dom.content(badge, render_network_badge(radioNet)); + dom.content(stat, render_network_status(radioNet)); + } + + if (stat.hasAttribute('restart')) + dom.content(stat, E('em', _('Device is restarting…'))); + + btns[0].disabled = isReadonlyView || busy; + btns[1].disabled = (isReadonlyView && radioDev) || busy; + btns[2].disabled = isReadonlyView || busy; + } + + var table = document.querySelector('#wifi_assoclist_table'), + hosts = data[0], + trows = []; + + for (var i = 0; i < data[3].length; i++) { + var bss = data[3][i], + name = hosts.getHostnameByMACAddr(bss.mac), + ipv4 = hosts.getIPAddrByMACAddr(bss.mac), + ipv6 = hosts.getIP6AddrByMACAddr(bss.mac); + + var hint; + + if (name && ipv4 && ipv6) + hint = '%s (%s, %s)'.format(name, ipv4, ipv6); + else if (name && (ipv4 || ipv6)) + hint = '%s (%s)'.format(name, ipv4 || ipv6); + else + hint = name || ipv4 || ipv6 || '?'; + + var row = [ + E('span', { + 'class': 'ifacebadge', + 'data-ifname': bss.network.getIfname(), + 'data-ssid': bss.network.getSSID() + }, [ + E('img', { + 'src': L.resource('icons/wifi%s.png').format(bss.network.isUp() ? '' : '_disabled'), + 'title': bss.radio.getI18n() + }), + E('span', [ + ' %s '.format(bss.network.getShortName()), + E('small', '(%s)'.format(bss.network.getIfname())) + ]) + ]), + bss.mac, + hint, + render_signal_badge(Math.min((bss.signal + 110) / 70 * 100, 100), bss.signal, bss.noise), + E('span', {}, [ + E('span', format_wifirate(bss.rx)), + E('br'), + E('span', format_wifirate(bss.tx)) + ]) + ]; + + if (bss.network.isClientDisconnectSupported()) { + if (table.firstElementChild.childNodes.length < 6) + table.firstElementChild.appendChild(E('th', { 'class': 'th cbi-section-actions'})); + + row.push(E('button', { + 'class': 'cbi-button cbi-button-remove', + 'click': L.bind(function(net, mac, ev) { + dom.parent(ev.currentTarget, '.tr').style.opacity = 0.5; + ev.currentTarget.classList.add('spinning'); + ev.currentTarget.disabled = true; + ev.currentTarget.blur(); + + net.disconnectClient(mac, true, 5, 60000); + }, this, bss.network, bss.mac), + 'disabled': isReadonlyView || null + }, [ _('Disconnect') ])); + } + else { + row.push('-'); + } + + trows.push(row); + } + + cbi_update_table(table, trows, E('em', _('No information available'))); + + var stat = document.querySelector('.cbi-modal [data-name="_wifistat_modal"] .ifacebadge.large'); + + if (stat) + render_modal_status(stat, data[2].filter(function(n) { return n.getName() == stat.getAttribute('data-network') })[0]); + + return network.flushCache(); + }, + + load: function() { + return Promise.all([ + uci.changes(), + uci.load('wireless') + ]); + }, + + checkAnonymousSections: function() { + var wifiIfaces = uci.sections('wireless', 'wifi-iface'); + + for (var i = 0; i < wifiIfaces.length; i++) + if (wifiIfaces[i]['.anonymous']) + return true; + + return false; + }, + + callUciRename: rpc.declare({ + object: 'uci', + method: 'rename', + params: [ 'config', 'section', 'name' ] + }), + + render: function() { + if (this.checkAnonymousSections()) + return this.renderMigration(); + else + return this.renderOverview(); + }, + + handleMigration: function(ev) { + var wifiIfaces = uci.sections('wireless', 'wifi-iface'), + id_offset = 0, + tasks = []; + + for (var i = 0; i < wifiIfaces.length; i++) { + if (!wifiIfaces[i]['.anonymous']) + continue; + + var new_name = next_free_sid(id_offset); + + tasks.push(this.callUciRename('wireless', wifiIfaces[i]['.name'], new_name)); + id_offset = +new_name.substring(7) + 1; + } + + return Promise.all(tasks) + .then(L.bind(ui.changes.init, ui.changes)) + .then(L.bind(ui.changes.apply, ui.changes)); + }, + + renderMigration: function() { + ui.showModal(_('Wireless configuration migration'), [ + E('p', _('The existing wireless configuration needs to be changed for LuCI to function properly.')), + E('p', _('Upon pressing "Continue", anonymous "wifi-iface" sections will be assigned with a name in the form wifinet# and the network will be restarted to apply the updated configuration.')), + E('div', { 'class': 'right' }, + E('button', { + 'class': 'btn cbi-button-action important', + 'click': ui.createHandlerFn(this, 'handleMigration') + }, _('Continue'))) + ]); + }, + + renderOverview: function() { + var m, s, o; + + m = new form.Map('wireless'); + m.chain('network'); + m.chain('firewall'); + + s = m.section(form.GridSection, 'wifi-device', _('Wireless Overview')); + s.anonymous = true; + s.addremove = false; + + s.load = function() { + return network.getWifiDevices().then(L.bind(function(radios) { + this.radios = radios.sort(function(a, b) { + return a.getName() > b.getName(); + }); + + var tasks = []; + + for (var i = 0; i < radios.length; i++) + tasks.push(radios[i].getWifiNetworks()); + + return Promise.all(tasks); + }, this)).then(L.bind(function(data) { + this.wifis = []; + + for (var i = 0; i < data.length; i++) + this.wifis.push.apply(this.wifis, data[i]); + }, this)); + }; + + s.cfgsections = function() { + var rv = []; + + for (var i = 0; i < this.radios.length; i++) { + rv.push(this.radios[i].getName()); + + for (var j = 0; j < this.wifis.length; j++) + if (this.wifis[j].getWifiDeviceName() == this.radios[i].getName()) + rv.push(this.wifis[j].getName()); + } + + return rv; + }; + + s.modaltitle = function(section_id) { + var radioNet = this.wifis.filter(function(w) { return w.getName() == section_id})[0]; + return radioNet ? radioNet.getI18n() : _('Edit wireless network'); + }; + + s.lookupRadioOrNetwork = function(section_id) { + var radioDev = this.radios.filter(function(r) { return r.getName() == section_id })[0]; + if (radioDev) + return radioDev; + + var radioNet = this.wifis.filter(function(w) { return w.getName() == section_id })[0]; + if (radioNet) + return radioNet; + + return null; + }; + + s.renderRowActions = function(section_id) { + var inst = this.lookupRadioOrNetwork(section_id), btns; + + if (inst.getWifiNetworks) { + btns = [ + E('button', { + 'class': 'cbi-button cbi-button-neutral', + 'title': _('Restart radio interface'), + 'click': ui.createHandlerFn(this, radio_restart, section_id) + }, _('Restart')), + E('button', { + 'class': 'cbi-button cbi-button-action important', + 'title': _('Find and join network'), + 'click': ui.createHandlerFn(this, 'handleScan', inst) + }, _('Scan')), + E('button', { + 'class': 'cbi-button cbi-button-add', + 'title': _('Provide new network'), + 'click': ui.createHandlerFn(this, 'handleAdd', inst) + }, _('Add')) + ]; + } + else { + var isDisabled = (inst.get('disabled') == '1' || + uci.get('wireless', inst.getWifiDeviceName(), 'disabled') == '1'); + + btns = [ + E('button', { + 'class': 'cbi-button cbi-button-neutral enable-disable', + 'title': isDisabled ? _('Enable this network') : _('Disable this network'), + 'click': ui.createHandlerFn(this, network_updown, section_id, this.map) + }, isDisabled ? _('Enable') : _('Disable')), + E('button', { + 'class': 'cbi-button cbi-button-action important', + 'title': _('Edit this network'), + 'click': ui.createHandlerFn(this, 'renderMoreOptionsModal', section_id) + }, _('Edit')), + E('button', { + 'class': 'cbi-button cbi-button-negative remove', + 'title': _('Delete this network'), + 'click': ui.createHandlerFn(this, 'handleRemove', section_id) + }, _('Remove')) + ]; + } + + return E('td', { 'class': 'td middle cbi-section-actions' }, E('div', btns)); + }; + + s.addModalOptions = function(s) { + return network.getWifiNetwork(s.section).then(function(radioNet) { + var hwtype = uci.get('wireless', radioNet.getWifiDeviceName(), 'type'); + var o, ss; + + o = s.option(form.SectionValue, '_device', form.NamedSection, radioNet.getWifiDeviceName(), 'wifi-device', _('Device Configuration')); + o.modalonly = true; + + ss = o.subsection; + ss.tab('general', _('General Setup')); + ss.tab('advanced', _('Advanced Settings')); + + var isDisabled = (radioNet.get('disabled') == '1' || + uci.get('wireless', radioNet.getWifiDeviceName(), 'disabled') == 1); + + o = ss.taboption('general', form.DummyValue, '_wifistat_modal', _('Status')); + o.cfgvalue = L.bind(function(radioNet) { + return render_modal_status(null, radioNet); + }, this, radioNet); + o.write = function() {}; + + o = ss.taboption('general', form.Button, '_toggle', isDisabled ? _('Wireless network is disabled') : _('Wireless network is enabled')); + o.inputstyle = isDisabled ? 'apply' : 'reset'; + o.inputtitle = isDisabled ? _('Enable') : _('Disable'); + o.onclick = ui.createHandlerFn(s, network_updown, s.section, s.map); + + o = ss.taboption('general', CBIWifiFrequencyValue, '_freq', '
' + _('Operating frequency')); + o.ucisection = s.section; + + if (hwtype == 'mac80211') { + o = ss.taboption('general', form.Flag, 'legacy_rates', _('Allow legacy 802.11b rates'), _('Legacy or badly behaving devices may require legacy 802.11b rates to interoperate. Airtime efficiency may be significantly reduced where these are used. It is recommended to not allow 802.11b rates where possible.')); + o.depends({'_freq': '2g', '!contains': true}); + + o = ss.taboption('general', CBIWifiTxPowerValue, 'txpower', _('Maximum transmit power'), _('Specifies the maximum transmit power the wireless radio may use. Depending on regulatory requirements and wireless usage, the actual transmit power may be reduced by the driver.')); + o.wifiNetwork = radioNet; + + o = ss.taboption('advanced', CBIWifiCountryValue, 'country', _('Country Code')); + o.wifiNetwork = radioNet; + + o = ss.taboption('advanced', form.ListValue, 'cell_density', _('Coverage cell density'), _('Configures data rates based on the coverage cell density. Normal configures basic rates to 6, 12, 24 Mbps if legacy 802.11b rates are not used else to 5.5, 11 Mbps. High configures basic rates to 12, 24 Mbps if legacy 802.11b rates are not used else to the 11 Mbps rate. Very High configures 24 Mbps as the basic rate. Supported rates lower than the minimum basic rate are not offered.')); + o.value('0', _('Disabled')); + o.value('1', _('Normal')); + o.value('2', _('High')); + o.value('3', _('Very High')); + + o = ss.taboption('advanced', form.Flag, 'mu_beamformer', _('MU-MIMO')); + o.default = o.disabled; + + o = ss.taboption('advanced', form.Value, 'distance', _('Distance Optimization'), _('Distance to farthest network member in meters.')); + o.datatype = 'or(range(0,114750),"auto")'; + o.placeholder = 'auto'; + + o = ss.taboption('advanced', form.Value, 'frag', _('Fragmentation Threshold')); + o.datatype = 'min(256)'; + o.placeholder = _('off'); + + o = ss.taboption('advanced', form.Value, 'rts', _('RTS/CTS Threshold')); + o.datatype = 'uinteger'; + o.placeholder = _('off'); + + o = ss.taboption('advanced', form.Flag, 'noscan', _('Force 40MHz mode'), _('Always use 40MHz channels even if the secondary channel overlaps. Using this option does not comply with IEEE 802.11n-2009!')); + o.rmempty = true; + + o = ss.taboption('advanced', form.Flag, 'vendor_vht', _('Enable 256-QAM'), _('802.11n 2.4Ghz Only')); + o.default = o.disabled; + + o = ss.taboption('advanced', form.Value, 'beacon_int', _('Beacon Interval')); + o.datatype = 'range(15,65535)'; + o.placeholder = 100; + o.rmempty = true; + } + + + o = s.option(form.SectionValue, '_device', form.NamedSection, radioNet.getName(), 'wifi-iface', _('Interface Configuration')); + o.modalonly = true; + + ss = o.subsection; + ss.tab('general', _('General Setup')); + ss.tab('encryption', _('Wireless Security')); + ss.tab('macfilter', _('MAC-Filter')); + ss.tab('advanced', _('Advanced Settings')); + + o = ss.taboption('general', form.ListValue, 'mode', _('Mode')); + o.value('ap', _('Access Point')); + o.value('sta', _('Client')); + o.value('adhoc', _('Ad-Hoc')); + + o = ss.taboption('general', form.Value, 'mesh_id', _('Mesh Id')); + o.depends('mode', 'mesh'); + + o = ss.taboption('advanced', form.Flag, 'mesh_fwding', _('Forward mesh peer traffic')); + o.rmempty = false; + o.default = '1'; + o.depends('mode', 'mesh'); + + o = ss.taboption('advanced', form.Value, 'mesh_rssi_threshold', _('RSSI threshold for joining'), _('0 = not using RSSI threshold, 1 = do not change driver default')); + o.rmempty = false; + o.default = '0'; + o.datatype = 'range(-255,1)'; + o.depends('mode', 'mesh'); + + o = ss.taboption('general', form.Value, 'ssid', _('ESSID')); + o.datatype = 'maxlength(32)'; + o.depends('mode', 'ap'); + o.depends('mode', 'sta'); + o.depends('mode', 'adhoc'); + o.depends('mode', 'ahdemo'); + o.depends('mode', 'monitor'); + o.depends('mode', 'ap-wds'); + o.depends('mode', 'sta-wds'); + o.depends('mode', 'wds'); + + o = ss.taboption('general', form.Value, 'bssid', _('BSSID')); + o.datatype = 'macaddr'; + + o = ss.taboption('general', widgets.NetworkSelect, 'network', _('Network'), _('Choose the network(s) you want to attach to this wireless interface or fill out the custom field to define a new network.')); + o.rmempty = true; + o.multiple = true; + o.novirtual = true; + o.write = function(section_id, value) { + return network.getDevice(section_id).then(L.bind(function(dev) { + var old_networks = dev.getNetworks().reduce(function(o, v) { o[v.getName()] = v; return o }, {}), + new_networks = {}, + values = L.toArray(value), + tasks = []; + + for (var i = 0; i < values.length; i++) { + new_networks[values[i]] = true; + + if (old_networks[values[i]]) + continue; + + tasks.push(network.getNetwork(values[i]).then(L.bind(function(name, net) { + return net || network.addNetwork(name, { proto: 'none' }); + }, this, values[i])).then(L.bind(function(dev, net) { + if (net) { + if (!net.isEmpty()) { + var target_dev = net.getDevice(); + + /* Resolve parent interface of vlan */ + while (target_dev && target_dev.getType() == 'vlan') + target_dev = target_dev.getParent(); + + if (!target_dev || target_dev.getType() != 'bridge') + net.set('type', 'bridge'); + } + + net.addDevice(dev); + } + }, this, dev))); + } + + for (var name in old_networks) + if (!new_networks[name]) + tasks.push(network.getNetwork(name).then(L.bind(function(dev, net) { + if (net) + net.deleteDevice(dev); + }, this, dev))); + + return Promise.all(tasks); + }, this)); + }; + + if (hwtype == 'mac80211') { + var mode = ss.children[0], + bssid = ss.children[5], + encr; + + mode.value('mesh', '802.11s'); + mode.value('ahdemo', _('Pseudo Ad-Hoc (ahdemo)')); + mode.value('monitor', _('Monitor')); + + bssid.depends('mode', 'adhoc'); + bssid.depends('mode', 'sta'); + bssid.depends('mode', 'sta-wds'); + + o = ss.taboption('macfilter', form.ListValue, 'macfilter', _('MAC Address Filter')); + o.depends('mode', 'ap'); + o.depends('mode', 'ap-wds'); + o.value('', _('disable')); + o.value('allow', _('Allow listed only')); + o.value('deny', _('Allow all except listed')); + + o = ss.taboption('macfilter', form.DynamicList, 'maclist', _('MAC-List')); + o.datatype = 'macaddr'; + o.retain = true; + o.depends('macfilter', 'allow'); + o.depends('macfilter', 'deny'); + o.load = function(section_id) { + return network.getHostHints().then(L.bind(function(hints) { + hints.getMACHints().map(L.bind(function(hint) { + this.value(hint[0], hint[1] ? '%s (%s)'.format(hint[0], hint[1]) : hint[0]); + }, this)); + + return form.DynamicList.prototype.load.apply(this, [section_id]); + }, this)); + }; + + mode.value('ap-wds', '%s (%s)'.format(_('Access Point'), _('WDS'))); + mode.value('sta-wds', '%s (%s)'.format(_('Client'), _('WDS'))); + + mode.write = function(section_id, value) { + switch (value) { + case 'ap-wds': + uci.set('wireless', section_id, 'mode', 'ap'); + uci.set('wireless', section_id, 'wds', '1'); + break; + + case 'sta-wds': + uci.set('wireless', section_id, 'mode', 'sta'); + uci.set('wireless', section_id, 'wds', '1'); + break; + + default: + uci.set('wireless', section_id, 'mode', value); + uci.unset('wireless', section_id, 'wds'); + break; + } + }; + + mode.cfgvalue = function(section_id) { + var mode = uci.get('wireless', section_id, 'mode'), + wds = uci.get('wireless', section_id, 'wds'); + + if (mode == 'ap' && wds) + return 'ap-wds'; + else if (mode == 'sta' && wds) + return 'sta-wds'; + + return mode; + }; + + o = ss.taboption('general', form.Flag, 'hidden', _('Hide ESSID'), _('Where the ESSID is hidden, clients may fail to roam and airtime efficiency may be significantly reduced.')); + o.depends('mode', 'ap'); + o.depends('mode', 'ap-wds'); + + o = ss.taboption('general', form.Flag, 'wmm', _('WMM Mode'), _('Where Wi-Fi Multimedia (WMM) Mode QoS is disabled, clients may be limited to 802.11a/802.11g rates.')); + o.depends('mode', 'ap'); + o.depends('mode', 'ap-wds'); + o.default = o.enabled; + + /* https://w1.fi/cgit/hostap/commit/?id=34f7c699a6bcb5c45f82ceb6743354ad79296078 */ + /* multicast_to_unicast https://github.com/openwrt/openwrt/commit/7babb978ad9d7fc29acb1ff86afb1eb343af303a */ + o = ss.taboption('advanced', form.Flag, 'multicast_to_unicast', _('Multi To Unicast'), _('ARP, IPv4 and IPv6 (even 802.1Q) with multicast destination MACs are unicast to the STA MAC address. Note: This is not Directed Multicast Service (DMS) in 802.11v. Note: might break receiver STA multicast expectations.')); + o.rmempty = true; + + o = ss.taboption('advanced', form.Flag, 'isolate', _('Isolate Clients'), _('Prevents client-to-client communication')); + o.depends('mode', 'ap'); + o.depends('mode', 'ap-wds'); + + o = ss.taboption('advanced', form.Value, 'ifname', _('Interface name'), _('Override default interface name')); + o.optional = true; + o.placeholder = radioNet.getIfname(); + if (/^radio\d+\.network/.test(o.placeholder)) + o.placeholder = ''; + + o = ss.taboption('advanced', form.Flag, 'short_preamble', _('Short Preamble')); + o.default = o.enabled; + + o = ss.taboption('advanced', form.Value, 'dtim_period', _('DTIM Interval'), _('Delivery Traffic Indication Message Interval')); + o.optional = true; + o.placeholder = 2; + o.datatype = 'range(1,255)'; + + o = ss.taboption('advanced', form.Value, 'wpa_group_rekey', _('Time interval for rekeying GTK'), _('sec')); + o.optional = true; + o.placeholder = 600; + o.datatype = 'uinteger'; + + o = ss.taboption('advanced', form.Flag , 'skip_inactivity_poll', _('Disable Inactivity Polling')); + o.optional = true; + o.datatype = 'uinteger'; + + o = ss.taboption('advanced', form.Value, 'max_inactivity', _('Station inactivity limit'), _('sec')); + o.optional = true; + o.placeholder = 300; + o.datatype = 'uinteger'; + + o = ss.taboption('advanced', form.Value, 'max_listen_interval', _('Maximum allowed Listen Interval')); + o.optional = true; + o.placeholder = 65535; + o.datatype = 'uinteger'; + + o = ss.taboption('advanced', form.Flag, 'disassoc_low_ack', _('Disassociate On Low Acknowledgement'), _('Allow AP mode to disconnect STAs based on low ACK condition')); + o.default = o.enabled; + } + + + encr = o = ss.taboption('encryption', form.ListValue, 'encryption', _('Encryption')); + o.depends('mode', 'ap'); + o.depends('mode', 'sta'); + o.depends('mode', 'adhoc'); + o.depends('mode', 'ahdemo'); + o.depends('mode', 'ap-wds'); + o.depends('mode', 'sta-wds'); + o.depends('mode', 'mesh'); + + o.cfgvalue = function(section_id) { + var v = String(uci.get('wireless', section_id, 'encryption')); + if (v == 'wep') + return 'wep-open'; + else if (v.match(/\+/)) + return v.replace(/\+.+$/, ''); + return v; + }; + + o.write = function(section_id, value) { + var e = this.section.children.filter(function(o) { return o.option == 'encryption' })[0].formvalue(section_id), + co = this.section.children.filter(function(o) { return o.option == 'cipher' })[0], c = co.formvalue(section_id); + + if (value == 'wpa' || value == 'wpa2' || value == 'wpa3' || value == 'wpa3-mixed') + uci.unset('wireless', section_id, 'key'); + + if (co.isActive(section_id) && e && (c == 'tkip' || c == 'ccmp' || c == 'tkip+ccmp')) + e += '+' + c; + + uci.set('wireless', section_id, 'encryption', e); + }; + + o = ss.taboption('encryption', form.ListValue, 'cipher', _('Cipher')); + o.depends('encryption', 'wpa'); + o.depends('encryption', 'wpa2'); + o.depends('encryption', 'wpa3'); + o.depends('encryption', 'wpa3-mixed'); + o.depends('encryption', 'psk'); + o.depends('encryption', 'psk2'); + o.depends('encryption', 'wpa-mixed'); + o.depends('encryption', 'psk-mixed'); + o.value('auto', _('auto')); + o.value('ccmp', _('Force CCMP (AES)')); + o.value('tkip', _('Force TKIP')); + o.value('tkip+ccmp', _('Force TKIP and CCMP (AES)')); + o.write = ss.children.filter(function(o) { return o.option == 'encryption' })[0].write; + + o.cfgvalue = function(section_id) { + var v = String(uci.get('wireless', section_id, 'encryption')); + if (v.match(/\+/)) { + v = v.replace(/^[^+]+\+/, ''); + if (v == 'aes') + v = 'ccmp'; + else if (v == 'tkip+aes' || v == 'aes+tkip' || v == 'ccmp+tkip') + v = 'tkip+ccmp'; + } + return v; + }; + + + var crypto_modes = []; + + if (hwtype == 'mac80211') { + var has_supplicant = L.hasSystemFeature('wpasupplicant'), + has_hostapd = L.hasSystemFeature('hostapd'); + + // Probe EAP support + var has_ap_eap = L.hasSystemFeature('hostapd', 'eap'), + has_sta_eap = L.hasSystemFeature('wpasupplicant', 'eap'); + + // Probe SAE support + var has_ap_sae = L.hasSystemFeature('hostapd', 'sae'), + has_sta_sae = L.hasSystemFeature('wpasupplicant', 'sae'); + + // Probe OWE support + var has_ap_owe = L.hasSystemFeature('hostapd', 'owe'), + has_sta_owe = L.hasSystemFeature('wpasupplicant', 'owe'); + + // Probe Suite-B support + var has_ap_eap192 = L.hasSystemFeature('hostapd', 'suiteb192'), + has_sta_eap192 = L.hasSystemFeature('wpasupplicant', 'suiteb192'); + + // Probe WEP support + var has_ap_wep = L.hasSystemFeature('hostapd', 'wep'), + has_sta_wep = L.hasSystemFeature('wpasupplicant', 'wep'); + + if (has_hostapd || has_supplicant) { + crypto_modes.push(['psk2', 'WPA2-PSK', 35]); + crypto_modes.push(['psk-mixed', 'WPA-PSK/WPA2-PSK Mixed Mode', 22]); + crypto_modes.push(['psk', 'WPA-PSK', 12]); + } + else { + encr.description = _('WPA-Encryption requires wpa_supplicant (for client mode) or hostapd (for AP and ad-hoc mode) to be installed.'); + } + + if (has_ap_sae || has_sta_sae) { + crypto_modes.push(['sae', 'WPA3-SAE', 31]); + crypto_modes.push(['sae-mixed', 'WPA2-PSK/WPA3-SAE Mixed Mode', 30]); + } + + if (has_ap_wep || has_sta_wep) { + crypto_modes.push(['wep-open', _('WEP Open System'), 11]); + crypto_modes.push(['wep-shared', _('WEP Shared Key'), 10]); + } + + if (has_ap_eap || has_sta_eap) { + if (has_ap_eap192 || has_sta_eap192) { + crypto_modes.push(['wpa3', 'WPA3-EAP', 33]); + crypto_modes.push(['wpa3-mixed', 'WPA2-EAP/WPA3-EAP Mixed Mode', 32]); + } + + crypto_modes.push(['wpa2', 'WPA2-EAP', 34]); + crypto_modes.push(['wpa', 'WPA-EAP', 20]); + } + + if (has_ap_owe || has_sta_owe) { + crypto_modes.push(['owe', 'OWE', 1]); + } + + encr.crypto_support = { + 'ap': { + 'wep-open': has_ap_wep || _('Requires hostapd with WEP support'), + 'wep-shared': has_ap_wep || _('Requires hostapd with WEP support'), + 'psk': has_hostapd || _('Requires hostapd'), + 'psk2': has_hostapd || _('Requires hostapd'), + 'psk-mixed': has_hostapd || _('Requires hostapd'), + 'sae': has_ap_sae || _('Requires hostapd with SAE support'), + 'sae-mixed': has_ap_sae || _('Requires hostapd with SAE support'), + 'wpa': has_ap_eap || _('Requires hostapd with EAP support'), + 'wpa2': has_ap_eap || _('Requires hostapd with EAP support'), + 'wpa3': has_ap_eap192 || _('Requires hostapd with EAP Suite-B support'), + 'wpa3-mixed': has_ap_eap192 || _('Requires hostapd with EAP Suite-B support'), + 'owe': has_ap_owe || _('Requires hostapd with OWE support') + }, + 'sta': { + 'wep-open': has_sta_wep || _('Requires wpa-supplicant with WEP support'), + 'wep-shared': has_sta_wep || _('Requires wpa-supplicant with WEP support'), + 'psk': has_supplicant || _('Requires wpa-supplicant'), + 'psk2': has_supplicant || _('Requires wpa-supplicant'), + 'psk-mixed': has_supplicant || _('Requires wpa-supplicant'), + 'sae': has_sta_sae || _('Requires wpa-supplicant with SAE support'), + 'sae-mixed': has_sta_sae || _('Requires wpa-supplicant with SAE support'), + 'wpa': has_sta_eap || _('Requires wpa-supplicant with EAP support'), + 'wpa2': has_sta_eap || _('Requires wpa-supplicant with EAP support'), + 'wpa3': has_sta_eap192 || _('Requires wpa-supplicant with EAP Suite-B support'), + 'wpa3-mixed': has_sta_eap192 || _('Requires wpa-supplicant with EAP Suite-B support'), + 'owe': has_sta_owe || _('Requires wpa-supplicant with OWE support') + }, + 'adhoc': { + 'wep-open': true, + 'wep-shared': true, + 'psk': has_supplicant || _('Requires wpa-supplicant'), + 'psk2': has_supplicant || _('Requires wpa-supplicant'), + 'psk-mixed': has_supplicant || _('Requires wpa-supplicant'), + }, + 'mesh': { + 'sae': has_sta_sae || _('Requires wpa-supplicant with SAE support') + }, + 'ahdemo': { + 'wep-open': true, + 'wep-shared': true + }, + 'wds': { + 'wep-open': true, + 'wep-shared': true + } + }; + + encr.crypto_support['ap-wds'] = encr.crypto_support['ap']; + encr.crypto_support['sta-wds'] = encr.crypto_support['sta']; + + encr.validate = function(section_id, value) { + var modeopt = this.section.children.filter(function(o) { return o.option == 'mode' })[0], + modeval = modeopt.formvalue(section_id), + modetitle = modeopt.vallist[modeopt.keylist.indexOf(modeval)], + enctitle = this.vallist[this.keylist.indexOf(value)]; + + if (value == 'none') + return true; + + if (!L.isObject(this.crypto_support[modeval]) || !this.crypto_support[modeval].hasOwnProperty(value)) + return _('The selected %s mode is incompatible with %s encryption').format(modetitle, enctitle); + + return this.crypto_support[modeval][value]; + }; + } + else if (hwtype == 'broadcom') { + crypto_modes.push(['psk2', 'WPA2-PSK', 33]); + crypto_modes.push(['psk+psk2', 'WPA-PSK/WPA2-PSK Mixed Mode', 22]); + crypto_modes.push(['psk', 'WPA-PSK', 12]); + crypto_modes.push(['wep-open', _('WEP Open System'), 11]); + crypto_modes.push(['wep-shared', _('WEP Shared Key'), 10]); + } + + crypto_modes.push(['none', _('No Encryption'), 0]); + + crypto_modes.sort(function(a, b) { return b[2] - a[2] }); + + for (var i = 0; i < crypto_modes.length; i++) { + var security_level = (crypto_modes[i][2] >= 30) ? _('strong security') + : (crypto_modes[i][2] >= 20) ? _('medium security') + : (crypto_modes[i][2] >= 10) ? _('weak security') : _('open network'); + + encr.value(crypto_modes[i][0], '%s (%s)'.format(crypto_modes[i][1], security_level)); + } + + + o = ss.taboption('encryption', form.Value, 'auth_server', _('Radius-Authentication-Server')); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + o.rmempty = true; + o.datatype = 'host(0)'; + + o = ss.taboption('encryption', form.Value, 'auth_port', _('Radius-Authentication-Port'), _('Default %d').format(1812)); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + o.rmempty = true; + o.datatype = 'port'; + + o = ss.taboption('encryption', form.Value, 'auth_secret', _('Radius-Authentication-Secret')); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + o.rmempty = true; + o.password = true; + + o = ss.taboption('encryption', form.Value, 'acct_server', _('Radius-Accounting-Server')); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + o.rmempty = true; + o.datatype = 'host(0)'; + + o = ss.taboption('encryption', form.Value, 'acct_port', _('Radius-Accounting-Port'), _('Default %d').format(1813)); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + o.rmempty = true; + o.datatype = 'port'; + + o = ss.taboption('encryption', form.Value, 'acct_secret', _('Radius-Accounting-Secret')); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + o.rmempty = true; + o.password = true; + + o = ss.taboption('encryption', form.Value, 'dae_client', _('DAE-Client')); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + o.rmempty = true; + o.datatype = 'host(0)'; + + o = ss.taboption('encryption', form.Value, 'dae_port', _('DAE-Port'), _('Default %d').format(3799)); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + o.rmempty = true; + o.datatype = 'port'; + + o = ss.taboption('encryption', form.Value, 'dae_secret', _('DAE-Secret')); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + o.rmempty = true; + o.password = true; + + + o = ss.taboption('encryption', form.Value, '_wpa_key', _('Key')); + o.depends('encryption', 'psk'); + o.depends('encryption', 'psk2'); + o.depends('encryption', 'psk+psk2'); + o.depends('encryption', 'psk-mixed'); + o.depends('encryption', 'sae'); + o.depends('encryption', 'sae-mixed'); + o.datatype = 'wpakey'; + o.rmempty = true; + o.password = true; + + o.cfgvalue = function(section_id) { + var key = uci.get('wireless', section_id, 'key'); + return /^[1234]$/.test(key) ? null : key; + }; + + o.write = function(section_id, value) { + uci.set('wireless', section_id, 'key', value); + uci.unset('wireless', section_id, 'key1'); + uci.unset('wireless', section_id, 'key2'); + uci.unset('wireless', section_id, 'key3'); + uci.unset('wireless', section_id, 'key4'); + }; + + + o = ss.taboption('encryption', form.ListValue, '_wep_key', _('Used Key Slot')); + o.depends('encryption', 'wep-open'); + o.depends('encryption', 'wep-shared'); + o.value('1', _('Key #%d').format(1)); + o.value('2', _('Key #%d').format(2)); + o.value('3', _('Key #%d').format(3)); + o.value('4', _('Key #%d').format(4)); + + o.cfgvalue = function(section_id) { + var slot = +uci.get('wireless', section_id, 'key'); + return (slot >= 1 && slot <= 4) ? String(slot) : ''; + }; + + o.write = function(section_id, value) { + uci.set('wireless', section_id, 'key', value); + }; + + for (var slot = 1; slot <= 4; slot++) { + o = ss.taboption('encryption', form.Value, 'key%d'.format(slot), _('Key #%d').format(slot)); + o.depends('encryption', 'wep-open'); + o.depends('encryption', 'wep-shared'); + o.datatype = 'wepkey'; + o.rmempty = true; + o.password = true; + + o.write = function(section_id, value) { + if (value != null && (value.length == 5 || value.length == 13)) + value = 's:%s'.format(value); + uci.set('wireless', section_id, this.option, value); + }; + } + + + if (hwtype == 'mac80211') { + + // Probe 802.11k support + o = ss.taboption('encryption', form.Flag, 'ieee80211k', _('802.11k'), _('Enables The 802.11k standard provides information to discover the best available access point')); + o.depends({ mode: 'ap', encryption: 'wpa' }); + o.depends({ mode: 'ap', encryption: 'wpa2' }); + o.depends({ mode: 'ap-wds', encryption: 'wpa' }); + o.depends({ mode: 'ap-wds', encryption: 'wpa2' }); + o.depends({ mode: 'ap', encryption: 'psk' }); + o.depends({ mode: 'ap', encryption: 'psk2' }); + o.depends({ mode: 'ap', encryption: 'psk-mixed' }); + o.depends({ mode: 'ap-wds', encryption: 'psk' }); + o.depends({ mode: 'ap-wds', encryption: 'psk2' }); + o.depends({ mode: 'ap-wds', encryption: 'psk-mixed' }); + o.depends({ mode: 'ap', encryption: 'sae' }); + o.depends({ mode: 'ap', encryption: 'sae-mixed' }); + o.depends({ mode: 'ap-wds', encryption: 'sae' }); + o.depends({ mode: 'ap-wds', encryption: 'sae-mixed' }); + o.rmempty = true; + + o = ss.taboption('encryption', form.Flag, 'rrm_neighbor_report', _('Enable neighbor report via radio measurements')); + o.default = o.enabled; + o.depends({ ieee80211k: '1' }); + o.rmempty = true; + + o = ss.taboption('encryption', form.Flag, 'rrm_beacon_report', _('Enable beacon report via radio measurements')); + o.default = o.enabled; + o.depends({ ieee80211k: '1' }); + o.rmempty = true; + // End of 802.11k options + + // Probe 802.11v support + o = ss.taboption('encryption', form.Flag, 'ieee80211v', _('802.11v'), _('Enables 802.11v allows client devices to exchange information about the network topology,tating overall improvement of the wireless network.')); + o.depends({ mode: 'ap', encryption: 'wpa' }); + o.depends({ mode: 'ap', encryption: 'wpa2' }); + o.depends({ mode: 'ap-wds', encryption: 'wpa' }); + o.depends({ mode: 'ap-wds', encryption: 'wpa2' }); + o.depends({ mode: 'ap', encryption: 'psk' }); + o.depends({ mode: 'ap', encryption: 'psk2' }); + o.depends({ mode: 'ap', encryption: 'psk-mixed' }); + o.depends({ mode: 'ap-wds', encryption: 'psk' }); + o.depends({ mode: 'ap-wds', encryption: 'psk2' }); + o.depends({ mode: 'ap-wds', encryption: 'psk-mixed' }); + o.depends({ mode: 'ap', encryption: 'sae' }); + o.depends({ mode: 'ap', encryption: 'sae-mixed' }); + o.depends({ mode: 'ap-wds', encryption: 'sae' }); + o.depends({ mode: 'ap-wds', encryption: 'sae-mixed' }); + o.rmempty = true; + + o = ss.taboption('encryption', form.Flag, 'wnm_sleep_mode', _('extended sleep mode for stations')); + o.default = o.enabled; + o.depends({ ieee80211v: '1' }); + o.rmempty = true; + + o = ss.taboption('encryption', form.Flag, 'bss_transition', _('BSS Transition Management')); + o.default = o.enabled; + o.depends({ ieee80211v: '1' }); + o.rmempty = true; + + o = ss.taboption('encryption', form.ListValue, 'time_advertisement', _('Time advertisement"')); + o.depends({ ieee80211v: '1' }); + o.value('0', _('disabled')); + o.value('2', _('UTC time at which the TSF timer is 0')); + o.rmempty = true; + + o = ss.taboption('encryption', form.Value, 'time_zone', _('Local time zone as specified in 8.3 of IEEE Std 1003.1-2004')); + o.depends({ time_advertisement: '2' }); + o.placeholder = 'UTC8'; + o.rmempty = true; + // End of 802.11v options + + // Probe 802.11r support (and EAP support as a proxy for Openwrt) + var has_80211r = L.hasSystemFeature('hostapd', '11r') || L.hasSystemFeature('hostapd', 'eap'); + + o = ss.taboption('encryption', form.Flag, 'ieee80211r', _('802.11r Fast Transition'), _('Enables fast roaming among access points that belong to the same Mobility Domain')); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + if (has_80211r) + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['psk', 'psk2', 'psk-mixed', 'sae', 'sae-mixed'] }); + o.rmempty = true; + + o = ss.taboption('encryption', form.Value, 'nasid', _('NAS ID'), _('Used for two different purposes: RADIUS NAS ID and 802.11r R0KH-ID. Not needed with normal WPA(2)-PSK.')); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + o.depends({ ieee80211r: '1' }); + o.rmempty = true; + + o = ss.taboption('encryption', form.Value, 'mobility_domain', _('Mobility Domain'), _('4-character hexadecimal ID')); + o.depends({ ieee80211r: '1' }); + o.placeholder = '4f57'; + o.datatype = 'and(hexstring,length(4))'; + o.rmempty = true; + + o = ss.taboption('encryption', form.Value, 'reassociation_deadline', _('Reassociation Deadline'), _('time units (TUs / 1.024 ms) [1000-65535]')); + o.depends({ ieee80211r: '1' }); + o.placeholder = '1000'; + o.datatype = 'range(1000,65535)'; + o.rmempty = true; + + o = ss.taboption('encryption', form.ListValue, 'ft_over_ds', _('FT protocol')); + o.depends({ ieee80211r: '1' }); + o.value('0', _('FT over the Air')); + o.value('1', _('FT over DS')); + o.rmempty = true; + + o = ss.taboption('encryption', form.Flag, 'ft_psk_generate_local', _('Generate PMK locally'), _('When using a PSK, the PMK can be automatically generated. When enabled, the R0/R1 key options below are not applied. Disable this to use the R0 and R1 key options.')); + o.depends({ ieee80211r: '1' }); + o.default = o.enabled; + o.rmempty = false; + + o = ss.taboption('encryption', form.Value, 'r0_key_lifetime', _('R0 Key Lifetime'), _('minutes')); + o.depends({ ieee80211r: '1' }); + o.placeholder = '10000'; + o.datatype = 'uinteger'; + o.rmempty = true; + + o = ss.taboption('encryption', form.Value, 'r1_key_holder', _('R1 Key Holder'), _('6-octet identifier as a hex string - no colons')); + o.depends({ ieee80211r: '1' }); + o.placeholder = '00004f577274'; + o.datatype = 'and(hexstring,length(12))'; + o.rmempty = true; + + o = ss.taboption('encryption', form.Flag, 'pmk_r1_push', _('PMK R1 Push')); + o.depends({ ieee80211r: '1' }); + o.placeholder = '0'; + o.rmempty = true; + + o = ss.taboption('encryption', form.DynamicList, 'r0kh', _('External R0 Key Holder List'), _('List of R0KHs in the same Mobility Domain.
Format: MAC-address,NAS-Identifier,128-bit key as hex string.
This list is used to map R0KH-ID (NAS Identifier) to a destination MAC address when requesting PMK-R1 key from the R0KH that the STA used during the Initial Mobility Domain Association.')); + o.depends({ ieee80211r: '1' }); + o.rmempty = true; + + o = ss.taboption('encryption', form.DynamicList, 'r1kh', _('External R1 Key Holder List'), _ ('List of R1KHs in the same Mobility Domain.
Format: MAC-address,R1KH-ID as 6 octets with colons,128-bit key as hex string.
This list is used to map R1KH-ID to a destination MAC address when sending PMK-R1 key from the R0KH. This is also the list of authorized R1KHs in the MD that can request PMK-R1 keys.')); + o.depends({ ieee80211r: '1' }); + o.rmempty = true; + // End of 802.11r options + + o = ss.taboption('encryption', form.ListValue, 'eap_type', _('EAP-Method')); + o.value('tls', 'TLS'); + o.value('ttls', 'TTLS'); + o.value('peap', 'PEAP'); + o.value('fast', 'FAST'); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + + o = ss.taboption('encryption', form.Flag, 'ca_cert_usesystem', _('Use system certificates'), _("Validate server certificate using built-in system CA bundle,
requires the \"ca-bundle\" package")); + o.enabled = '1'; + o.disabled = '0'; + o.default = o.disabled; + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + o.validate = function(section_id, value) { + if (value == '1' && !L.hasSystemFeature('cabundle')) { + return _("This option cannot be used because the ca-bundle package is not installed."); + } + return true; + }; + + o = ss.taboption('encryption', form.FileUpload, 'ca_cert', _('Path to CA-Certificate')); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], ca_cert_usesystem: ['0'] }); + + o = ss.taboption('encryption', form.Value, 'subject_match', _('Certificate constraint (Subject)'), _("Certificate constraint substring - e.g. /CN=wifi.mycompany.com
See `logread -f` during handshake for actual values")); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + + o = ss.taboption('encryption', form.DynamicList, 'altsubject_match', _('Certificate constraint (SAN)'), _("Certificate constraint(s) via Subject Alternate Name values
(supported attributes: EMAIL, DNS, URI) - e.g. DNS:wifi.mycompany.com")); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + + o = ss.taboption('encryption', form.DynamicList, 'domain_match', _('Certificate constraint (Domain)'), _("Certificate constraint(s) against DNS SAN values (if available)
or Subject CN (exact match)")); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + + o = ss.taboption('encryption', form.DynamicList, 'domain_suffix_match', _('Certificate constraint (Wildcard)'), _("Certificate constraint(s) against DNS SAN values (if available)
or Subject CN (suffix match)")); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + + o = ss.taboption('encryption', form.FileUpload, 'client_cert', _('Path to Client-Certificate')); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], eap_type: ['tls'] }); + + o = ss.taboption('encryption', form.FileUpload, 'priv_key', _('Path to Private Key')); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], eap_type: ['tls'] }); + + o = ss.taboption('encryption', form.Value, 'priv_key_pwd', _('Password of Private Key')); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], eap_type: ['tls'] }); + o.password = true; + + o = ss.taboption('encryption', form.ListValue, 'auth', _('Authentication')); + o.value('PAP', 'PAP'); + o.value('CHAP', 'CHAP'); + o.value('MSCHAP', 'MSCHAP'); + o.value('MSCHAPV2', 'MSCHAPv2'); + o.value('EAP-GTC', 'EAP-GTC'); + o.value('EAP-MD5', 'EAP-MD5'); + o.value('EAP-MSCHAPV2', 'EAP-MSCHAPv2'); + o.value('EAP-TLS', 'EAP-TLS'); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], eap_type: ['fast', 'peap', 'ttls'] }); + + o.validate = function(section_id, value) { + var eo = this.section.children.filter(function(o) { return o.option == 'eap_type' })[0], + ev = eo.formvalue(section_id); + + if (ev != 'ttls' && (value == 'PAP' || value == 'CHAP' || value == 'MSCHAP' || value == 'MSCHAPV2')) + return _('This authentication type is not applicable to the selected EAP method.'); + + return true; + }; + + o = ss.taboption('encryption', form.Flag, 'ca_cert2_usesystem', _('Use system certificates for inner-tunnel'), _("Validate server certificate using built-in system CA bundle,
requires the \"ca-bundle\" package")); + o.enabled = '1'; + o.disabled = '0'; + o.default = o.disabled; + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], auth: ['EAP-TLS'] }); + o.validate = function(section_id, value) { + if (value == '1' && !L.hasSystemFeature('cabundle')) { + return _("This option cannot be used because the ca-bundle package is not installed."); + } + return true; + }; + + o = ss.taboption('encryption', form.FileUpload, 'ca_cert2', _('Path to inner CA-Certificate')); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], auth: ['EAP-TLS'], ca_cert2_usesystem: ['0'] }); + + o = ss.taboption('encryption', form.Value, 'subject_match2', _('Inner certificate constraint (Subject)'), _("Certificate constraint substring - e.g. /CN=wifi.mycompany.com
See `logread -f` during handshake for actual values")); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], auth: ['EAP-TLS'] }); + + o = ss.taboption('encryption', form.DynamicList, 'altsubject_match2', _('Inner certificate constraint (SAN)'), _("Certificate constraint(s) via Subject Alternate Name values
(supported attributes: EMAIL, DNS, URI) - e.g. DNS:wifi.mycompany.com")); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], auth: ['EAP-TLS'] }); + + o = ss.taboption('encryption', form.DynamicList, 'domain_match2', _('Inner certificate constraint (Domain)'), _("Certificate constraint(s) against DNS SAN values (if available)
or Subject CN (exact match)")); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], auth: ['EAP-TLS'] }); + + o = ss.taboption('encryption', form.DynamicList, 'domain_suffix_match2', _('Inner certificate constraint (Wildcard)'), _("Certificate constraint(s) against DNS SAN values (if available)
or Subject CN (suffix match)")); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], auth: ['EAP-TLS'] }); + + o = ss.taboption('encryption', form.FileUpload, 'client_cert2', _('Path to inner Client-Certificate')); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], auth: ['EAP-TLS'] }); + + o = ss.taboption('encryption', form.FileUpload, 'priv_key2', _('Path to inner Private Key')); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], auth: ['EAP-TLS'] }); + + o = ss.taboption('encryption', form.Value, 'priv_key2_pwd', _('Password of inner Private Key')); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], auth: ['EAP-TLS'] }); + o.password = true; + + o = ss.taboption('encryption', form.Value, 'identity', _('Identity')); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], eap_type: ['fast', 'peap', 'tls', 'ttls'] }); + + o = ss.taboption('encryption', form.Value, 'anonymous_identity', _('Anonymous Identity')); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], eap_type: ['fast', 'peap', 'tls', 'ttls'] }); + + o = ss.taboption('encryption', form.Value, 'password', _('Password')); + add_dependency_permutations(o, { mode: ['sta', 'sta-wds'], encryption: ['wpa', 'wpa2', 'wpa3', 'wpa3-mixed'], eap_type: ['fast', 'peap', 'ttls'] }); + o.password = true; + + + if (hwtype == 'mac80211') { + // ieee802.11w options + o = ss.taboption('encryption', form.ListValue, 'ieee80211w', _('802.11w Management Frame Protection'), _("Note: Some wireless drivers do not fully support 802.11w. E.g. mwlwifi may have problems")); + o.value('0', _('Disabled')); + o.value('1', _('Optional')); + o.value('2', _('Required')); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds', 'sta', 'sta-wds'], encryption: ['owe', 'psk2', 'psk-mixed', 'sae', 'sae-mixed', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + + o.defaults = { + '2': [{ encryption: 'sae' }, { encryption: 'owe' }, { encryption: 'wpa3' }, { encryption: 'wpa3-mixed' }], + '1': [{ encryption: 'sae-mixed'}], + '0': [] + }; + + o.write = function(section_id, value) { + if (value != this.default) + return form.ListValue.prototype.write.call(this, section_id, value); + else + return form.ListValue.prototype.remove.call(this, section_id); + }; + + o = ss.taboption('encryption', form.Value, 'ieee80211w_max_timeout', _('802.11w maximum timeout'), _('802.11w Association SA Query maximum timeout')); + o.depends('ieee80211w', '1'); + o.depends('ieee80211w', '2'); + o.datatype = 'uinteger'; + o.placeholder = '1000'; + o.rmempty = true; + + o = ss.taboption('encryption', form.Value, 'ieee80211w_retry_timeout', _('802.11w retry timeout'), _('802.11w Association SA Query retry timeout')); + o.depends('ieee80211w', '1'); + o.depends('ieee80211w', '2'); + o.datatype = 'uinteger'; + o.placeholder = '201'; + o.rmempty = true; + + o = ss.taboption('encryption', form.Flag, 'wpa_disable_eapol_key_retries', _('Enable key reinstallation (KRACK) countermeasures'), _('Complicates key reinstallation attacks on the client side by disabling retransmission of EAPOL-Key frames that are used to install keys. This workaround might cause interoperability issues and reduced robustness of key negotiation especially in environments with heavy traffic load.')); + add_dependency_permutations(o, { mode: ['ap', 'ap-wds'], encryption: ['psk2', 'psk-mixed', 'sae', 'sae-mixed', 'wpa2', 'wpa3', 'wpa3-mixed'] }); + + if (L.hasSystemFeature('hostapd', 'wps') && L.hasSystemFeature('wpasupplicant')) { + o = ss.taboption('encryption', form.Flag, 'wps_pushbutton', _('Enable WPS pushbutton, requires WPA(2)-PSK/WPA3-SAE')) + o.enabled = '1'; + o.disabled = '0'; + o.default = o.disabled; + o.depends('encryption', 'psk'); + o.depends('encryption', 'psk2'); + o.depends('encryption', 'psk-mixed'); + o.depends('encryption', 'sae'); + o.depends('encryption', 'sae-mixed'); + } + } + } + }); + }; + + s.handleRemove = function(section_id, ev) { + document.querySelector('.cbi-section-table-row[data-sid="%s"]'.format(section_id)).style.opacity = 0.5; + return form.TypedSection.prototype.handleRemove.apply(this, [section_id, ev]); + }; + + s.handleScan = function(radioDev, ev) { + var table = E('table', { 'class': 'table' }, [ + E('tr', { 'class': 'tr table-titles' }, [ + E('th', { 'class': 'th col-2 middle center' }, _('Signal')), + E('th', { 'class': 'th col-4 middle left' }, _('SSID')), + E('th', { 'class': 'th col-2 middle center hide-xs' }, _('Channel')), + E('th', { 'class': 'th col-2 middle left hide-xs' }, _('Mode')), + E('th', { 'class': 'th col-3 middle left hide-xs' }, _('BSSID')), + E('th', { 'class': 'th col-3 middle left' }, _('Encryption')), + E('th', { 'class': 'th cbi-section-actions right' }, ' '), + ]) + ]); + + var stop = E('button', { + 'class': 'btn', + 'click': L.bind(this.handleScanStartStop, this), + 'style': 'display:none', + 'data-state': 'stop' + }, _('Stop refresh')); + + cbi_update_table(table, [], E('em', { class: 'spinning' }, _('Starting wireless scan...'))); + + var md = ui.showModal(_('Join Network: Wireless Scan'), [ + table, + E('div', { 'class': 'right' }, [ + stop, + ' ', + E('button', { + 'class': 'btn', + 'click': L.bind(this.handleScanAbort, this) + }, _('Dismiss')) + ]) + ]); + + md.style.maxWidth = '90%'; + md.style.maxHeight = 'none'; + + this.pollFn = L.bind(this.handleScanRefresh, this, radioDev, {}, table, stop); + + poll.add(this.pollFn); + poll.start(); + }; + + s.handleScanRefresh = function(radioDev, scanCache, table, stop) { + return radioDev.getScanList().then(L.bind(function(results) { + var rows = []; + + for (var i = 0; i < results.length; i++) + scanCache[results[i].bssid] = results[i]; + + for (var k in scanCache) + if (scanCache[k].stale) + results.push(scanCache[k]); + + results.sort(function(a, b) { + var diff = (b.quality - a.quality) || (a.channel - b.channel); + + if (diff) + return diff; + + if (a.ssid < b.ssid) + return -1; + else if (a.ssid > b.ssid) + return 1; + + if (a.bssid < b.bssid) + return -1; + else if (a.bssid > b.bssid) + return 1; + }); + + for (var i = 0; i < results.length; i++) { + var res = results[i], + qv = res.quality || 0, + qm = res.quality_max || 0, + q = (qv > 0 && qm > 0) ? Math.floor((100 / qm) * qv) : 0, + s = res.stale ? 'opacity:0.5' : ''; + + rows.push([ + E('span', { 'style': s }, render_signal_badge(q, res.signal, res.noise)), + E('span', { 'style': s }, (res.ssid != null) ? '%h'.format(res.ssid) : E('em', _('hidden'))), + E('span', { 'style': s }, '%d'.format(res.channel)), + E('span', { 'style': s }, '%h'.format(res.mode)), + E('span', { 'style': s }, '%h'.format(res.bssid)), + E('span', { 'style': s }, '%h'.format(network.formatWifiEncryption(res.encryption))), + E('div', { 'class': 'right' }, E('button', { + 'class': 'cbi-button cbi-button-action important', + 'click': ui.createHandlerFn(this, 'handleJoin', radioDev, res) + }, _('Join Network'))) + ]); + + res.stale = true; + } + + cbi_update_table(table, rows); + + stop.disabled = false; + stop.style.display = ''; + stop.classList.remove('spinning'); + }, this)); + }; + + s.handleScanStartStop = function(ev) { + var btn = ev.currentTarget; + + if (btn.getAttribute('data-state') == 'stop') { + poll.remove(this.pollFn); + btn.firstChild.data = _('Start refresh'); + btn.setAttribute('data-state', 'start'); + } + else { + poll.add(this.pollFn); + btn.firstChild.data = _('Stop refresh'); + btn.setAttribute('data-state', 'stop'); + btn.classList.add('spinning'); + btn.disabled = true; + } + }; + + s.handleScanAbort = function(ev) { + var md = dom.parent(ev.target, 'div[aria-modal="true"]'); + if (md) { + md.style.maxWidth = ''; + md.style.maxHeight = ''; + } + + ui.hideModal(); + poll.remove(this.pollFn); + + this.pollFn = null; + }; + + s.handleJoinConfirm = function(radioDev, bss, form, ev) { + var nameopt = L.toArray(form.lookupOption('name', '_new_'))[0], + passopt = L.toArray(form.lookupOption('password', '_new_'))[0], + ssidopt = L.toArray(form.lookupOption('ssid', '_new_'))[0], + bssidopt = L.toArray(form.lookupOption('bssid', '_new_'))[0], + zoneopt = L.toArray(form.lookupOption('zone', '_new_'))[0], + replopt = L.toArray(form.lookupOption('replace', '_new_'))[0], + nameval = (nameopt && nameopt.isValid('_new_')) ? nameopt.formvalue('_new_') : null, + passval = (passopt && passopt.isValid('_new_')) ? passopt.formvalue('_new_') : null, + ssidval = (ssidopt && ssidopt.isValid('_new_')) ? ssidopt.formvalue('_new_') : null, + bssidval = (bssidopt && bssidopt.isValid('_new_')) ? bssidopt.formvalue('_new_') : null, + zoneval = zoneopt ? zoneopt.formvalue('_new_') : null, + enc = L.isObject(bss.encryption) ? bss.encryption : null, + is_wep = (enc && Array.isArray(enc.wep)), + is_psk = (enc && Array.isArray(enc.wpa) && L.toArray(enc.authentication).filter(function(a) { return a == 'psk' }).length > 0), + is_sae = (enc && Array.isArray(enc.wpa) && L.toArray(enc.authentication).filter(function(a) { return a == 'sae' }).length > 0); + + if (nameval == null || (passopt && passval == null)) + return; + + var section_id = null; + + return this.map.save(function() { + var wifi_sections = uci.sections('wireless', 'wifi-iface'); + + if (replopt.formvalue('_new_') == '1') { + for (var i = 0; i < wifi_sections.length; i++) + if (wifi_sections[i].device == radioDev.getName()) + uci.remove('wireless', wifi_sections[i]['.name']); + } + + if (uci.get('wireless', radioDev.getName(), 'disabled') == '1') { + for (var i = 0; i < wifi_sections.length; i++) + if (wifi_sections[i].device == radioDev.getName()) + uci.set('wireless', wifi_sections[i]['.name'], 'disabled', '1'); + + uci.unset('wireless', radioDev.getName(), 'disabled'); + } + + section_id = next_free_sid(wifi_sections.length); + + uci.add('wireless', 'wifi-iface', section_id); + uci.set('wireless', section_id, 'device', radioDev.getName()); + uci.set('wireless', section_id, 'mode', (bss.mode == 'Ad-Hoc') ? 'adhoc' : 'sta'); + uci.set('wireless', section_id, 'network', nameval); + + if (bss.ssid != null) { + uci.set('wireless', section_id, 'ssid', bss.ssid); + + if (bssidval == '1') + uci.set('wireless', section_id, 'bssid', bss.bssid); + } + else if (bss.bssid != null) { + uci.set('wireless', section_id, 'bssid', bss.bssid); + } + + if (ssidval != null) + uci.set('wireless', section_id, 'ssid', ssidval); + + if (is_sae) { + uci.set('wireless', section_id, 'encryption', 'sae'); + uci.set('wireless', section_id, 'key', passval); + } + else if (is_psk) { + for (var i = enc.wpa.length - 1; i >= 0; i--) { + if (enc.wpa[i] == 2) { + uci.set('wireless', section_id, 'encryption', 'psk2'); + break; + } + else if (enc.wpa[i] == 1) { + uci.set('wireless', section_id, 'encryption', 'psk'); + break; + } + } + + uci.set('wireless', section_id, 'key', passval); + } + else if (is_wep) { + uci.set('wireless', section_id, 'encryption', 'wep-open'); + uci.set('wireless', section_id, 'key', '1'); + uci.set('wireless', section_id, 'key1', passval); + } + else { + uci.set('wireless', section_id, 'encryption', 'none'); + } + + return network.addNetwork(nameval, { proto: 'dhcp' }).then(function(net) { + firewall.deleteNetwork(net.getName()); + + var zonePromise = zoneval + ? firewall.getZone(zoneval).then(function(zone) { return zone || firewall.addZone(zoneval) }) + : Promise.resolve(); + + return zonePromise.then(function(zone) { + if (zone) + zone.addNetwork(net.getName()); + }); + }); + }).then(L.bind(function() { + return this.renderMoreOptionsModal(section_id); + }, this)); + }; + + s.handleJoin = function(radioDev, bss, ev) { + poll.remove(this.pollFn); + + var m2 = new form.Map('wireless'), + s2 = m2.section(form.NamedSection, '_new_'), + enc = L.isObject(bss.encryption) ? bss.encryption : null, + is_wep = (enc && Array.isArray(enc.wep)), + is_psk = (enc && Array.isArray(enc.wpa) && L.toArray(enc.authentication).filter(function(a) { return a == 'psk' || a == 'sae' })), + replace, passphrase, name, bssid, zone; + + var nameUsed = function(name) { + var s = uci.get('network', name); + if (s != null && s['.type'] != 'interface') + return true; + + var net = (s != null) ? network.instantiateNetwork(name) : null; + return (net != null && !net.isEmpty()); + }; + + s2.render = function() { + return Promise.all([ + {}, + this.renderUCISection('_new_') + ]).then(this.renderContents.bind(this)); + }; + + if (bss.ssid == null) { + name = s2.option(form.Value, 'ssid', _('Network SSID'), _('The correct SSID must be manually specified when joining a hidden wireless network')); + name.rmempty = false; + }; + + replace = s2.option(form.Flag, 'replace', _('Replace wireless configuration'), _('Check this option to delete the existing networks from this radio.')); + + name = s2.option(form.Value, 'name', _('Name of the new network'), _('The allowed characters are: A-Z, a-z, 0-9 and _')); + name.datatype = 'uciname'; + name.default = 'wwan'; + name.rmempty = false; + name.validate = function(section_id, value) { + if (nameUsed(value)) + return _('The network name is already used'); + + return true; + }; + + for (var i = 2; nameUsed(name.default); i++) + name.default = 'wwan%d'.format(i); + + if (is_wep || is_psk) { + passphrase = s2.option(form.Value, 'password', is_wep ? _('WEP passphrase') : _('WPA passphrase'), _('Specify the secret encryption key here.')); + passphrase.datatype = is_wep ? 'wepkey' : 'wpakey'; + passphrase.password = true; + passphrase.rmempty = false; + } + + if (bss.ssid != null) { + bssid = s2.option(form.Flag, 'bssid', _('Lock to BSSID'), _('Instead of joining any network with a matching SSID, only connect to the BSSID %h.').format(bss.bssid)); + bssid.default = '0'; + } + + zone = s2.option(widgets.ZoneSelect, 'zone', _('Create / Assign firewall-zone'), _('Choose the firewall zone you want to assign to this interface. Select unspecified to remove the interface from the associated zone or fill out the custom field to define a new zone and attach the interface to it.')); + zone.default = 'wan'; + + return m2.render().then(L.bind(function(nodes) { + ui.showModal(_('Joining Network: %q').replace(/%q/, '"%h"'.format(bss.ssid)), [ + nodes, + E('div', { 'class': 'right' }, [ + E('button', { + 'class': 'btn', + 'click': ui.hideModal + }, _('Cancel')), ' ', + E('button', { + 'class': 'cbi-button cbi-button-positive important', + 'click': ui.createHandlerFn(this, 'handleJoinConfirm', radioDev, bss, m2) + }, _('Submit')) + ]) + ], 'cbi-modal').querySelector('[id="%s"] input[class][type]'.format((passphrase || name).cbid('_new_'))).focus(); + }, this)); + }; + + s.handleAdd = function(radioDev, ev) { + var section_id = next_free_sid(uci.sections('wireless', 'wifi-iface').length); + + uci.unset('wireless', radioDev.getName(), 'disabled'); + + uci.add('wireless', 'wifi-iface', section_id); + uci.set('wireless', section_id, 'device', radioDev.getName()); + uci.set('wireless', section_id, 'mode', 'ap'); + uci.set('wireless', section_id, 'ssid', 'OpenWrt'); + uci.set('wireless', section_id, 'encryption', 'none'); + + this.addedSection = section_id; + return this.renderMoreOptionsModal(section_id); + }; + + o = s.option(form.DummyValue, '_badge'); + o.modalonly = false; + o.textvalue = function(section_id) { + var inst = this.section.lookupRadioOrNetwork(section_id), + node = E('div', { 'class': 'center' }); + + if (inst.getWifiNetworks) + node.appendChild(render_radio_badge(inst)); + else + node.appendChild(render_network_badge(inst)); + + return node; + }; + + o = s.option(form.DummyValue, '_stat'); + o.modalonly = false; + o.textvalue = function(section_id) { + var inst = this.section.lookupRadioOrNetwork(section_id); + + if (inst.getWifiNetworks) + return render_radio_status(inst, this.section.wifis.filter(function(e) { + return (e.getWifiDeviceName() == inst.getName()); + })); + else + return render_network_status(inst); + }; + + return m.render().then(L.bind(function(m, nodes) { + poll.add(L.bind(function() { + var section_ids = m.children[0].cfgsections(), + tasks = [ network.getHostHints(), network.getWifiDevices() ]; + + for (var i = 0; i < section_ids.length; i++) { + var row = nodes.querySelector('.cbi-section-table-row[data-sid="%s"]'.format(section_ids[i])), + dsc = row.querySelector('[data-name="_stat"] > div'), + btns = row.querySelectorAll('.cbi-section-actions button'); + + if (dsc.getAttribute('restart') == '') { + dsc.setAttribute('restart', '1'); + tasks.push(fs.exec('/sbin/wifi', ['up', section_ids[i]]).catch(function(e) { + ui.addNotification(null, E('p', e.message)); + })); + } + else if (dsc.getAttribute('restart') == '1') { + dsc.removeAttribute('restart'); + btns[0].classList.remove('spinning'); + btns[0].disabled = false; + } + } + + return Promise.all(tasks) + .then(L.bind(function(hosts_radios) { + var tasks = []; + + for (var i = 0; i < hosts_radios[1].length; i++) + tasks.push(hosts_radios[1][i].getWifiNetworks()); + + return Promise.all(tasks).then(function(data) { + hosts_radios[2] = []; + + for (var i = 0; i < data.length; i++) + hosts_radios[2].push.apply(hosts_radios[2], data[i]); + + return hosts_radios; + }); + }, network)) + .then(L.bind(function(hosts_radios_wifis) { + var tasks = []; + + for (var i = 0; i < hosts_radios_wifis[2].length; i++) + tasks.push(hosts_radios_wifis[2][i].getAssocList()); + + return Promise.all(tasks).then(function(data) { + hosts_radios_wifis[3] = []; + + for (var i = 0; i < data.length; i++) { + var wifiNetwork = hosts_radios_wifis[2][i], + radioDev = hosts_radios_wifis[1].filter(function(d) { return d.getName() == wifiNetwork.getWifiDeviceName() })[0]; + + for (var j = 0; j < data[i].length; j++) + hosts_radios_wifis[3].push(Object.assign({ radio: radioDev, network: wifiNetwork }, data[i][j])); + } + + return hosts_radios_wifis; + }); + }, network)) + .then(L.bind(this.poll_status, this, nodes)); + }, this), 5); + + var table = E('table', { 'class': 'table assoclist', 'id': 'wifi_assoclist_table' }, [ + E('tr', { 'class': 'tr table-titles' }, [ + E('th', { 'class': 'th nowrap' }, _('Network')), + E('th', { 'class': 'th hide-xs' }, _('MAC address')), + E('th', { 'class': 'th' }, _('Host')), + E('th', { 'class': 'th' }, _('Signal / Noise')), + E('th', { 'class': 'th' }, _('RX Rate / TX Rate')) + ]) + ]); + + cbi_update_table(table, [], E('em', { 'class': 'spinning' }, _('Collecting data...'))) + + return E([ nodes, E('h3', _('Associated Stations')), table ]); + }, this, m)); + } +}); diff --git a/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/inspect.lua b/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/inspect.lua new file mode 100644 index 0000000000..d809de09b0 --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/inspect.lua @@ -0,0 +1,337 @@ +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table +local inspect = {Options = {}, } + + + + + + + + + + + + + + + + + +inspect._VERSION = 'inspect.lua 3.1.0' +inspect._URL = 'http://github.com/kikito/inspect.lua' +inspect._DESCRIPTION = 'human-readable representations of tables' +inspect._LICENSE = [[ + MIT LICENSE + + Copyright (c) 2022 Enrique García Cota + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +]] +inspect.KEY = setmetatable({}, { __tostring = function() return 'inspect.KEY' end }) +inspect.METATABLE = setmetatable({}, { __tostring = function() return 'inspect.METATABLE' end }) + +local tostring = tostring +local rep = string.rep +local match = string.match +local char = string.char +local gsub = string.gsub +local fmt = string.format + +local function rawpairs(t) + return next, t, nil +end + + + +local function smartQuote(str) + if match(str, '"') and not match(str, "'") then + return "'" .. str .. "'" + end + return '"' .. gsub(str, '"', '\\"') .. '"' +end + + +local shortControlCharEscapes = { + ["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n", + ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v", ["\127"] = "\\127", +} +local longControlCharEscapes = { ["\127"] = "\127" } +for i = 0, 31 do + local ch = char(i) + if not shortControlCharEscapes[ch] then + shortControlCharEscapes[ch] = "\\" .. i + longControlCharEscapes[ch] = fmt("\\%03d", i) + end +end + +local function escape(str) + return (gsub(gsub(gsub(str, "\\", "\\\\"), + "(%c)%f[0-9]", longControlCharEscapes), + "%c", shortControlCharEscapes)) +end + +local function isIdentifier(str) + return type(str) == "string" and not not str:match("^[_%a][_%a%d]*$") +end + +local flr = math.floor +local function isSequenceKey(k, sequenceLength) + return type(k) == "number" and + flr(k) == k and + 1 <= (k) and + k <= sequenceLength +end + +local defaultTypeOrders = { + ['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4, + ['function'] = 5, ['userdata'] = 6, ['thread'] = 7, +} + +local function sortKeys(a, b) + local ta, tb = type(a), type(b) + + + if ta == tb and (ta == 'string' or ta == 'number') then + return (a) < (b) + end + + local dta = defaultTypeOrders[ta] or 100 + local dtb = defaultTypeOrders[tb] or 100 + + + return dta == dtb and ta < tb or dta < dtb +end + +local function getKeys(t) + + local seqLen = 1 + while rawget(t, seqLen) ~= nil do + seqLen = seqLen + 1 + end + seqLen = seqLen - 1 + + local keys, keysLen = {}, 0 + for k in rawpairs(t) do + if not isSequenceKey(k, seqLen) then + keysLen = keysLen + 1 + keys[keysLen] = k + end + end + table.sort(keys, sortKeys) + return keys, keysLen, seqLen +end + +local function countCycles(x, cycles) + if type(x) == "table" then + if cycles[x] then + cycles[x] = cycles[x] + 1 + else + cycles[x] = 1 + for k, v in rawpairs(x) do + countCycles(k, cycles) + countCycles(v, cycles) + end + countCycles(getmetatable(x), cycles) + end + end +end + +local function makePath(path, a, b) + local newPath = {} + local len = #path + for i = 1, len do newPath[i] = path[i] end + + newPath[len + 1] = a + newPath[len + 2] = b + + return newPath +end + + +local function processRecursive(process, + item, + path, + visited) + if item == nil then return nil end + if visited[item] then return visited[item] end + + local processed = process(item, path) + if type(processed) == "table" then + local processedCopy = {} + visited[item] = processedCopy + local processedKey + + for k, v in rawpairs(processed) do + processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited) + if processedKey ~= nil then + processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited) + end + end + + local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited) + if type(mt) ~= 'table' then mt = nil end + setmetatable(processedCopy, mt) + processed = processedCopy + end + return processed +end + +local function puts(buf, str) + buf.n = buf.n + 1 + buf[buf.n] = str +end + + + +local Inspector = {} + + + + + + + + + + +local Inspector_mt = { __index = Inspector } + +local function tabify(inspector) + puts(inspector.buf, inspector.newline .. rep(inspector.indent, inspector.level)) +end + +function Inspector:getId(v) + local id = self.ids[v] + local ids = self.ids + if not id then + local tv = type(v) + id = (ids[tv] or 0) + 1 + ids[v], ids[tv] = id, id + end + return tostring(id) +end + +function Inspector:putValue(v) + local buf = self.buf + local tv = type(v) + if tv == 'string' then + puts(buf, smartQuote(escape(v))) + elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or + tv == 'cdata' or tv == 'ctype' then + puts(buf, tostring(v)) + elseif tv == 'table' and not self.ids[v] then + local t = v + + if t == inspect.KEY or t == inspect.METATABLE then + puts(buf, tostring(t)) + elseif self.level >= self.depth then + puts(buf, '{...}') + else + if self.cycles[t] > 1 then puts(buf, fmt('<%d>', self:getId(t))) end + + local keys, keysLen, seqLen = getKeys(t) + + puts(buf, '{') + self.level = self.level + 1 + + for i = 1, seqLen + keysLen do + if i > 1 then puts(buf, ',') end + if i <= seqLen then + puts(buf, ' ') + self:putValue(t[i]) + else + local k = keys[i - seqLen] + tabify(self) + if isIdentifier(k) then + puts(buf, k) + else + puts(buf, "[") + self:putValue(k) + puts(buf, "]") + end + puts(buf, ' = ') + self:putValue(t[k]) + end + end + + local mt = getmetatable(t) + if type(mt) == 'table' then + if seqLen + keysLen > 0 then puts(buf, ',') end + tabify(self) + puts(buf, ' = ') + self:putValue(mt) + end + + self.level = self.level - 1 + + if keysLen > 0 or type(mt) == 'table' then + tabify(self) + elseif seqLen > 0 then + puts(buf, ' ') + end + + puts(buf, '}') + end + + else + puts(buf, fmt('<%s %d>', tv, self:getId(v))) + end +end + + + + +function inspect.inspect(root, options) + options = options or {} + + local depth = options.depth or (math.huge) + local newline = options.newline or '\n' + local indent = options.indent or ' ' + local process = options.process + + if process then + root = processRecursive(process, root, {}, {}) + end + + local cycles = {} + countCycles(root, cycles) + + local inspector = setmetatable({ + buf = { n = 0 }, + ids = {}, + cycles = cycles, + depth = depth, + level = 0, + newline = newline, + indent = indent, + }, Inspector_mt) + + inspector:putValue(root) + + return table.concat(inspector.buf) +end + +setmetatable(inspect, { + __call = function(_, root, options) + return inspect.inspect(root, options) + end, +}) + +return inspect \ No newline at end of file diff --git a/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_cfg b/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_cfg new file mode 100755 index 0000000000..fe89ff72b3 --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_cfg @@ -0,0 +1,538 @@ +#!/usr/bin/lua +--[[ + * + * Copyright (C) 2023 hanwckf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * The Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. +]] + +local cjson = require "cjson" +local inspect = require "inspect" +local nixio = require "nixio" +local l1parser = require "l1dat_parser" +local datconf = require "datconf" +local utils = require "mtwifi_utils" +local defs = require "mtwifi_defs" + +local l1dat = l1parser.load_l1_profile(l1parser.L1_DAT_PATH) + +function string:split(sep) + local sep, fields = sep or ":", {} + local pattern = string.format("([^%s]+)", sep) + self:gsub(pattern, function(c) fields[#fields+1] = c end) + return fields +end + +function load_profile(path) + local cfgs = {} + + cfgobj = datconf.openfile(path) + if cfgobj then + cfgs = cfgobj:getall() + cfgobj:close() + end + + return cfgs +end + +function save_profile(cfgs, path) + if not cfgs then + return + end + + local datobj = datconf.openfile(path) + datobj:merge(cfgs) + datobj:close(true) + + os.execute("sync") +end + +function diff_cfg(cfg1, cfg2) + local diff = {} + + for k,v in pairs(cfg1) do + if tostring(cfg2[k]) ~= tostring(cfg1[k]) then + diff[k] = {cfg1[k] or "", cfg2[k] or ""} + end + end + + for k,v in pairs(cfg2) do + if tostring(cfg2[k]) ~= tostring(cfg1[k]) then + diff[k] = {cfg1[k] or "", cfg2[k] or ""} + end + end + + return diff +end + +function vif_status(ifname) + local flags = tonumber(utils.read_pipe("cat /sys/class/net/"..ifname.."/flags 2>/dev/null")) or 0 + if flags%2 == 1 then + return "up" + end + return "down" +end + +function is_dbdc_inited(ifname) + local mac = utils.trim(utils.read_pipe("cat /sys/class/net/"..ifname.."/address 2>/dev/null")) + if mac and mac ~= "00:00:00:00:00:00" then + return true + end + return false +end + +function ifup(ifname) + if ifname and #ifname > 0 then + os.execute("ifconfig "..ifname.." up") + end +end + +function ifdown(ifname) + if ifname and #ifname > 0 then + os.execute("ifconfig "..ifname.." down") + end +end + +function cfg2dat(cfg) + if type(cfg) == type(true) then + if cfg then return 1 else return 0 end + elseif type(cfg) == type(0) or type(cfg) == type("") then + return cfg + end +end + +function set_dat(dats, apidx, dat, cfg) + if cfg ~= nil then + dats[dat] = utils.token_set(dats[dat], apidx, cfg2dat(cfg)) + end +end + +function set_idx_dat(dats, apidx, dat, cfg) + if cfg ~= nil then + dats[dat..tostring(apidx)] = cfg + end +end + +function mtwifi_up(devname, cfg, restore_vifs, is_dbdc) + nixio.syslog("info", "mtwifi-cfg: start "..devname) + local dev = l1dat.devname_ridx[devname] + local dbdc_main_ifname = defs.dbdc_init_ifname + + if not dev then return end + + if is_dbdc then + if not is_dbdc_inited(dbdc_main_ifname) then + nixio.syslog("info", "mtwifi-cfg: dbdc card init...") + ifup(dbdc_main_ifname) + utils.sleep(1) + ifdown(dbdc_main_ifname) + end + + if restore_vifs and devname ~= cfg.device then + for _,vif in ipairs(restore_vifs) do + nixio.syslog("info", "mtwifi-cfg: restore vif: "..vif) + ifup(vif) + end + return + end + end + + -- start vifs + for _, v in pairs(cfg.interfaces) do + local mode = v.config.mode + if mode and (mode == "ap" or mode == "sta") + and (not v.config.disabled) and v.mtwifi_ifname then + local vif = v.mtwifi_ifname + nixio.syslog("info", "mtwifi-cfg: up vif: "..vif) + ifup(vif) + end + end +end + +function mtwifi_down(devname, cfg) + nixio.syslog("info", "mtwifi-cfg: stop "..devname) + local dev = l1dat.devname_ridx[devname] + local reset_vifs = {} + + if not dev then return end + + for _,vif in ipairs(string.split(utils.read_pipe("ls /sys/class/net"), "\n")) do + if (vif_status(vif) == "up" and (vif == dev.main_ifname + or string.match(vif, utils.esc(dev.ext_ifname).."[0-9]+") + or string.match(vif, utils.esc(dev.apcli_ifname).."[0-9]+") + or string.match(vif, utils.esc(dev.wds_ifname).."[0-9]+") + or string.match(vif, utils.esc(dev.mesh_ifname).."[0-9]+"))) + then + nixio.syslog("info", "mtwifi-cfg: down vif: "..vif) + ifdown(vif) + if cfg and devname ~= cfg.device then + reset_vifs[#reset_vifs+1] = vif + end + end + end + return reset_vifs +end + +function mtwifi_reinstall() + nixio.syslog("info", "mtwifi-cfg: unload mtwifi module...") + + --os.execute("rmmod mt_whnat") + --os.execute("rmmod mtfwd") + os.execute("rmmod mtk_warp_proxy") + os.execute("rmmod mtk_warp") + --os.execute("rmmod mt7915_mt_wifi") + os.execute("rmmod mt_wifi") + + utils.sleep(2) + + nixio.syslog("info", "mtwifi-cfg: reload mtwifi module...") + + os.execute("modprobe mt_wifi") + --os.execute("modprobe mt7915_mt_wifi") + os.execute("modprobe mtk_warp") + os.execute("modprobe mtk_warp_proxy") + --os.execute("modprobe mtfwd") + --os.execute("modprobe mt_whnat") +end + +function mtwifi_cfg_setup(argv) + local cfg = cjson.decode(argv) + + utils.log2file("input = " .. inspect(cfg)) + + local devname = cfg.device + local dev = l1dat.devname_ridx[devname] + + if not dev then return end + + local profile = dev.profile_path + + local dats = load_profile(profile) + if not dats then + nixio.syslog("err", "mtwifi-cfg: profile ".. profile .. "open failed") + return + end + local dats_orig = load_profile(profile) + + local bssid_num = 0 + local vif_num = 0 + for k,v in pairs(cfg.interfaces) do + vif_num = vif_num + 1 + if v.config.mode == "ap" then + bssid_num = bssid_num + 1 + end + end + + if vif_num == 0 then + nixio.syslog("err", "mtwifi-cfg: not valid vif found!") + return + elseif vif_num > defs.max_mbssid then + nixio.syslog("err", "mtwifi-cfg: too many vifs!") + return + end + + if bssid_num > 0 then + dats.BssidNum = bssid_num + else + dats.BssidNum = 1 + end + + -- setup apcli + dats.ApCliEnable = 0 + dats.ApCliSsid = "" + dats.ApCliBssid = "" + dats.ApCliAuthMode = "" + dats.ApCliEncrypType = "" + dats.ApCliWPAPSK = "" + + for _,v in pairs(cfg.interfaces) do + if v.config.mode == "sta" then + if not v.config.disabled then + dats.ApCliEnable = 1 + end + dats.ApCliSsid = v.config.ssid + dats.ApCliBssid = v.config.bssid or "" + dats.ApCliAuthMode = defs.enc2dat[v.config.encryption][1] + dats.ApCliEncrypType = defs.enc2dat[v.config.encryption][2] + dats.ApCliWPAPSK = v.config.key or "" + break + end + end + + -- setup dev cfgs + if cfg.config.twt then + dats.TWTSupport = 1 + else + dats.TWTSupport = 0 + end + + if type(cfg.config.country) == type("") and #cfg.config.country == 2 then + dats.CountryCode = cfg.config.country + if cfg.config.band == "2g" then + dats.CountryRegion = defs.countryRegions[cfg.config.country][1] + elseif cfg.config.band == "5g" then + dats.CountryRegionABand = defs.countryRegions[cfg.config.country][2] + end + end + + if cfg.config.channel == "auto" then + dats.AutoChannelSelect = 3 + dats.Channel = 0 + else + dats.AutoChannelSelect = 0 + dats.Channel = cfg.config.channel + end + + if cfg.config.htmode == "HT20" or cfg.config.htmode == "VHT20" or cfg.config.htmode == "HE20" then + dats.HT_BW = 0 + dats.VHT_BW = 0 + elseif cfg.config.htmode == "HT40" or cfg.config.htmode == "VHT40" or cfg.config.htmode == "HE40" then + dats.HT_BW = 1 + dats.VHT_BW = 0 + if cfg.config.noscan ~= nil and cfg.config.noscan == "1" then + dats.HT_BSSCoexistence = 0 + else + dats.HT_BSSCoexistence = 1 + end + elseif cfg.config.htmode == "VHT80" or cfg.config.htmode == "HE80" then + dats.HT_BW = 1 + dats.VHT_BW = 1 + elseif cfg.config.htmode == "VHT160" or cfg.config.htmode == "HE160" then + dats.HT_BW = 1 + dats.VHT_BW = 2 + end + + if cfg.config.txpower and cfg.config.txpower < 100 then + dats.PERCENTAGEenable = 1 + dats.TxPower = cfg.config.txpower + else + dats.PERCENTAGEenable = 0 + dats.TxPower = 100 + end + + if cfg.config.mu_beamformer then + dats.ETxBfEnCond = 1 + if dats.ApCliEnable == 1 then + dats.MUTxRxEnable = 3 + else + dats.MUTxRxEnable = 1 + end + dats.ITxBfEn = 0 + else + dats.ETxBfEnCond = 0 + dats.MUTxRxEnable = 0 + dats.ITxBfEn = 0 + end + + local WirelessMode + if cfg.config.band == "2g" then + if string.sub(cfg.config.htmode,1,2) == "HE" then + WirelessMode = 16 -- PHY_11AX_24G + else + WirelessMode = 9 -- PHY_11BGN_MIXED + end + elseif cfg.config.band == "5g" then + if string.sub(cfg.config.htmode,1,2) == "HE" then + WirelessMode = 17 -- PHY_11AX_5G + else + WirelessMode = 15 -- PHY_11VHT_N_MIXED + end + end + + -- reset vif cfgs to default + for k,v in pairs(defs.vif_cfgs) do + dats[k] = "" + for i = 1, bssid_num do + dats[k] = utils.token_set(dats[k], i, v) + end + end + + for k,v in pairs(defs.vif_cfgs_idx) do + for i = 1, defs.max_mbssid do + dats[k..tostring(i)] = "" + end + for i = 1, bssid_num do + dats[k..tostring(i)] = v + end + end + + for k,v in pairs(defs.vif_acl) do + for i = 0, defs.max_mbssid-1 do + dats[k..tostring(i)] = "" + end + for i = 0, bssid_num-1 do + dats[k..tostring(i)] = v + end + end + + -- setup vif cfgs + local apidx = 1 + for idx = 0,vif_num-1 do + v = cfg.interfaces[tostring(idx)] + if v.config.mode == "ap" then + set_idx_dat(dats, apidx, "SSID", v.config.ssid) + set_idx_dat(dats, apidx, "WPAPSK", v.config.key or "") + set_dat(dats, apidx, "NoForwarding", v.config.isolate) + set_dat(dats, apidx, "HideSSID", v.config.hidden) + set_dat(dats, apidx, "WmmCapable", v.config.wmm) + set_dat(dats, apidx, "RRMEnable", v.config.ieee80211k) + set_dat(dats, apidx, "RekeyInterval", v.config.wpa_group_rekey) + set_dat(dats, apidx, "MuMimoDlEnable",v.config.mumimo_dl) + set_dat(dats, apidx, "MuMimoUlEnable",v.config.mumimo_ul) + set_dat(dats, apidx, "MuOfdmaDlEnable",v.config.ofdma_dl) + set_dat(dats, apidx, "MuOfdmaUlEnable",v.config.ofdma_ul) + set_dat(dats, apidx, "HT_AMSDU",v.config.amsdu) + set_dat(dats, apidx, "HT_AutoBA",v.config.autoba) + set_dat(dats, apidx, "APSDCapable",v.config.uapsd) + set_dat(dats, apidx, "RTSThreshold", cfg.config.rts) + set_dat(dats, apidx, "FragThreshold",cfg.config.frag) + set_dat(dats, apidx, "WirelessMode", WirelessMode) + + if v.config.macfilter then + if v.config.macfilter == "allow" then + set_idx_dat(dats, apidx-1, "AccessPolicy", 1) + elseif v.config.macfilter == "deny" then + set_idx_dat(dats, apidx-1, "AccessPolicy", 2) + end + end + + if v.config.maclist then + local maclist = "" + for _, v in ipairs(v.config.maclist) do + if #maclist > 0 then + maclist = v .. ";" .. maclist + else + maclist = v + end + end + set_idx_dat(dats, apidx-1, "AccessControlList", maclist) + end + + local authmode = defs.enc2dat[v.config.encryption][1] + set_dat(dats, apidx, "AuthMode", authmode) + set_dat(dats, apidx, "EncrypType", defs.enc2dat[v.config.encryption][2]) + + if authmode == "OWE" or authmode == "WPA3PSK" then + set_dat(dats, apidx, "PMFMFPC", 1) + set_dat(dats, apidx, "PMFMFPR", 1) + set_dat(dats, apidx, "PMFSHA256", 0) + elseif authmode == "WPA2PSKWPA3PSK" then + set_dat(dats, apidx, "PMFMFPC", 1) + set_dat(dats, apidx, "PMFMFPR", 0) + set_dat(dats, apidx, "PMFSHA256", 0) + end + + if not (authmode == "OPEN" or authmode == "OWE") then + set_dat(dats, apidx, "RekeyMethod", "TIME") + end + + apidx = apidx + 1 + end + end + + local reinstall_wifidrv = false + local cfg_diff = diff_cfg(dats_orig, dats) + utils.log2file("diff = " .. inspect(cfg_diff)) + + save_profile(dats, profile) + + for _,v in pairs(defs.reinstall_cfgs) do + if cfg_diff[v] ~= nil then + if utils.exists("/sys/module/mt_wifi") == false then + nixio.syslog("err", "mtwifi-cfg: mtwifi module is build-in, please reboot the device!") + return + else + reinstall_wifidrv = true + end + end + end + + if string.find(profile, "dbdc") then + if reinstall_wifidrv then + for k, _ in pairs(l1dat.devname_ridx) do + mtwifi_down(k) + end + + mtwifi_reinstall() + + for k, _ in pairs(l1dat.devname_ridx) do + if k == devname then + mtwifi_up(k, cfg, nil, true) + else + os.execute("/sbin/wifi up " .. k) + end + end + else + local restore_vifs = {} + local restart_other_dbdc_dev = false + if next(cfg_diff) ~= nil then + restart_other_dbdc_dev = true + end + + if restart_other_dbdc_dev then + for k, _ in pairs(l1dat.devname_ridx) do + local ret = mtwifi_down(k, cfg) + for _, v in ipairs(ret) do + table.insert(restore_vifs, v) + end + end + + if #restore_vifs > 0 then + nixio.syslog("info", "mtwifi-cfg: dbdc restore_vifs: "..inspect.inspect(restore_vifs)) + end + + for k, _ in pairs(l1dat.devname_ridx) do + mtwifi_up(k, cfg, restore_vifs, true) + end + else + mtwifi_down(devname) + mtwifi_up(devname, cfg, nil, true) + end + end + else + mtwifi_down(devname) + + if reinstall_wifidrv then + mtwifi_reinstall() + end + + mtwifi_up(devname, cfg) + end +end + +function mtwifi_cfg_down(devname) + if devname then + mtwifi_down(devname) + else + for k, _ in pairs(l1dat.devname_ridx) do + mtwifi_down(k) + end + end +end + +local action = { + ["down"] = function(devname) + mtwifi_cfg_down(devname) + end, + + ["setup"] = function() + local argv = io.read() + if #argv > 0 then + mtwifi_cfg_setup(argv) + end + end +} + +if #arg == 1 then + action[arg[1]]() +elseif #arg == 2 then + action[arg[1]](arg[2]) +end diff --git a/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_defs.lua b/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_defs.lua new file mode 100644 index 0000000000..e79af165cc --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_defs.lua @@ -0,0 +1,219 @@ +#!/usr/bin/lua +--[[ + * + * Copyright (C) 2023 hanwckf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * The Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. +]] + +local mtwifi_defs = {} + +mtwifi_defs.dbdc_init_ifname = "ra0" +mtwifi_defs.max_mbssid = 16 + +mtwifi_defs.vif_cfgs = { + -- dat cfg = default val + ["AuthMode"] = "OPEN", + ["EncrypType"] = "NONE", + ["PMFMFPC"] = "0", + ["PMFMFPR"] = "0", + ["PMFSHA256"] = "0", + ["RekeyInterval"] = "3600", + ["DefaultKeyID"] = "1", + ["IEEE8021X"] = "0", + ["Key1Type"] = "0", + ["Key2Type"] = "0", + ["Key3Type"] = "0", + ["Key4Type"] = "0", + ["PMKCachePeriod"] = "10", + ["PreAuth"] = "0", + ["RADIUS_Port"] = "1812", + ["RADIUS_Server"] = "0", + ["RekeyMethod"] = "DISABLE", + ["session_timeout_interval"] = "0", + ["Wapiifname"] = "", + ["HideSSID"] = "0", + ["WirelessMode"] = "", + ["NoForwarding"] = "0", + ["APSDCapable"] = "1", + ["WmmCapable"] = "1", + ["FragThreshold"] = "2346", + ["RTSThreshold"] = "2347", + ["HT_AMSDU"] = "1", + ["HT_AutoBA"] = "1", + ["HT_GI"] = "1", + ["HT_LDPC"] = "1", + ["HT_OpMode"] = "0", + ["HT_PROTECT"] = "1", + ["HT_STBC"] = "1", + ["IgmpSnEnable"] = "0", + ["RRMEnable"] = "1", + ["VHT_BW_SIGNAL"] = "0", + ["VHT_LDPC"] = "1", + ["VHT_SGI"] = "1", + ["VHT_STBC"] = "1", + ["MuMimoDlEnable"] = "1", + ["MuMimoUlEnable"] = "1", + ["MuOfdmaDlEnable"] = "1", + ["MuOfdmaUlEnable"] = "1", + ["DLSCapable"] = "0", + ["WdsEnable"] = "0", + ["WscConfMode"] = "0", +} + +mtwifi_defs.vif_cfgs_idx = { + ["WPAPSK"] = "12345678", + ["SSID"] = "", + ["RADIUS_Key"] = "", +} + +mtwifi_defs.vif_acl = { + ["AccessPolicy"] = "0", + ["AccessControlList"] = "", +} + +mtwifi_defs.reinstall_cfgs = { + "BssidNum", "WHNAT", "E2pAccessMode", + "HT_RxStream", "HT_TxStream", "WdsEnable" +} + +mtwifi_defs.enc2dat = { + -- enc = AuthMode, EncrypType + ["none"] = {"OPEN", "NONE"}, + ["sae"] = {"WPA3PSK", "AES"}, + ["sae-mixed"] = {"WPA2PSKWPA3PSK", "AES"}, + ["psk2+tkip+ccmp"] = {"WPA2PSK", "TKIPAES"}, + ["psk2+tkip+aes"] = {"WPA2PSK", "TKIPAES"}, + ["psk2+tkip"] = {"WPA2PSK", "TKIP"}, + ["psk2+ccmp"] = {"WPA2PSK", "AES"}, + ["psk2+aes"] = {"WPA2PSK", "AES"}, + ["psk2"] = {"WPA2PSK", "AES"}, + ["psk+tkip+ccmp"] = {"WPAPSK", "AES"}, + ["psk+tkip+aes"] = {"WPAPSK", "AES"}, + ["psk+tkip"] = {"WPAPSK", "TKIP"}, + ["psk+ccmp"] = {"WPAPSK", "AES"}, + ["psk+aes"] = {"WPAPSK", "AES"}, + ["psk"] = {"WPAPSK", "AES"}, + ["psk-mixed+tkip+ccmp"] = {"WPAPSKWPA2PSK", "TKIPAES"}, + ["psk-mixed+tkip+aes"] = {"WPAPSKWPA2PSK", "TKIPAES"}, + ["psk-mixed+tkip"] = {"WPAPSKWPA2PSK", "TKIP"}, + ["psk-mixed+ccmp"] = {"WPAPSKWPA2PSK", "AES"}, + ["psk-mixed+aes"] = {"WPAPSKWPA2PSK", "AES"}, + ["psk-mixed"] = {"WPAPSKWPA2PSK", "AES"}, + ["owe"] = {"OWE", "AES"}, +} + +mtwifi_defs.countryRegions = { + -- CountryCode = 2g region, 5g region + ["DB"] = { 5, 13 }, + ["AE"] = { 1, 0 }, + ["AL"] = { 1, 0 }, + ["AR"] = { 1, 3 }, + ["AT"] = { 1, 1 }, + ["AM"] = { 1, 2 }, + ["AU"] = { 1, 0 }, + ["AZ"] = { 1, 2 }, + ["BE"] = { 1, 1 }, + ["BH"] = { 1, 0 }, + ["BY"] = { 1, 0 }, + ["BO"] = { 1, 4 }, + ["BR"] = { 1, 1 }, + ["BN"] = { 1, 4 }, + ["BG"] = { 1, 1 }, + ["BZ"] = { 1, 4 }, + ["CA"] = { 0, 0 }, + ["CH"] = { 1, 1 }, + ["CL"] = { 1, 0 }, + ["CN"] = { 1, 0 }, + ["CO"] = { 0, 0 }, + ["CR"] = { 1, 0 }, + ["CY"] = { 1, 1 }, + ["CZ"] = { 1, 2 }, + ["DE"] = { 1, 1 }, + ["DK"] = { 1, 1 }, + ["DO"] = { 0, 0 }, + ["DZ"] = { 1, 0 }, + ["EC"] = { 1, 0 }, + ["EG"] = { 1, 2 }, + ["EE"] = { 1, 1 }, + ["ES"] = { 1, 1 }, + ["FI"] = { 1, 1 }, + ["FR"] = { 1, 2 }, + ["GE"] = { 1, 2 }, + ["GB"] = { 1, 1 }, + ["GR"] = { 1, 1 }, + ["GT"] = { 0, 0 }, + ["HN"] = { 1, 0 }, + ["HK"] = { 1, 0 }, + ["HU"] = { 1, 1 }, + ["HR"] = { 1, 2 }, + ["IS"] = { 1, 1 }, + ["IN"] = { 1, 0 }, + ["ID"] = { 1, 4 }, + ["IR"] = { 1, 4 }, + ["IE"] = { 1, 1 }, + ["IL"] = { 1, 0 }, + ["IT"] = { 1, 1 }, + ["JP"] = { 1, 9 }, + ["JO"] = { 1, 0 }, + ["KP"] = { 1, 5 }, + ["KR"] = { 1, 5 }, + ["KW"] = { 1, 0 }, + ["KZ"] = { 1, 0 }, + ["LB"] = { 1, 0 }, + ["LI"] = { 1, 1 }, + ["LT"] = { 1, 1 }, + ["LU"] = { 1, 1 }, + ["LV"] = { 1, 1 }, + ["MA"] = { 1, 0 }, + ["MC"] = { 1, 2 }, + ["MO"] = { 1, 0 }, + ["MK"] = { 1, 0 }, + ["MX"] = { 0, 0 }, + ["MY"] = { 1, 0 }, + ["NL"] = { 1, 1 }, + ["NO"] = { 0, 0 }, + ["NZ"] = { 1, 0 }, + ["OM"] = { 1, 0 }, + ["PA"] = { 0, 0 }, + ["PE"] = { 1, 4 }, + ["PH"] = { 1, 4 }, + ["PL"] = { 1, 1 }, + ["PK"] = { 1, 0 }, + ["PT"] = { 1, 1 }, + ["PR"] = { 0, 0 }, + ["QA"] = { 1, 0 }, + ["RO"] = { 1, 0 }, + ["RU"] = { 1, 0 }, + ["SA"] = { 1, 0 }, + ["SG"] = { 1, 0 }, + ["SK"] = { 1, 1 }, + ["SI"] = { 1, 1 }, + ["SV"] = { 1, 0 }, + ["SE"] = { 1, 1 }, + ["SY"] = { 1, 0 }, + ["TH"] = { 1, 0 }, + ["TN"] = { 1, 2 }, + ["TR"] = { 1, 2 }, + ["TT"] = { 1, 2 }, + ["TW"] = { 0, 3 }, + ["UA"] = { 1, 0 }, + ["US"] = { 0, 0 }, + ["UY"] = { 1, 5 }, + ["UZ"] = { 0, 1 }, + ["VE"] = { 1, 5 }, + ["VN"] = { 1, 0 }, + ["YE"] = { 1, 0 }, + ["ZA"] = { 1, 1 }, + ["ZW"] = { 1, 0 }, +} + +return mtwifi_defs diff --git a/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_utils.lua b/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_utils.lua new file mode 100644 index 0000000000..4a224eaea0 --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/files/mtwifi-cfg/mtwifi_utils.lua @@ -0,0 +1,96 @@ +#!/usr/bin/lua + +local mtwifi_utils = {} + +local mtwifi_logfile = "/tmp/mtwifi.log" + +function mtwifi_utils.esc(x) + return (x:gsub('%%', '%%%%') + :gsub('^%^', '%%^') + :gsub('%$$', '%%$') + :gsub('%(', '%%(') + :gsub('%)', '%%)') + :gsub('%.', '%%.') + :gsub('%[', '%%[') + :gsub('%]', '%%]') + :gsub('%*', '%%*') + :gsub('%+', '%%+') + :gsub('%-', '%%-') + :gsub('%?', '%%?')) +end + +function mtwifi_utils.trim(s) + if s then return (s:gsub("^%s*(.-)%s*$", "%1")) end +end + +function mtwifi_utils.exists(path) + local fp = io.open(path, "rb") + if fp then fp:close() end + return fp ~= nil +end + +function mtwifi_utils.read_pipe(pipe) + local retry_count = 10 + local fp, txt, err + repeat + fp = io.popen(pipe) + txt, err = fp:read("*a") + fp:close() + retry_count = retry_count - 1 + until err == nil or retry_count == 0 + return txt +end + +function mtwifi_utils.__cfg2list(str) + -- delimeter == ";" + local i = 1 + local list = {} + for k in string.gmatch(str, "([^;]+)") do + list[i] = k + i = i + 1 + end + return list +end + +function mtwifi_utils.token_set(str, n, v) + -- n start from 1 + -- delimeter == ";" + if not str then return end + if v == nil then return end + local tmp = mtwifi_utils.__cfg2list(str) + if type(v) ~= type("") and type(v) ~= type(0) then + nixio.syslog("err", "invalid value type in token_set, "..type(v)) + return + end + if #tmp < tonumber(n) then + for i=#tmp, tonumber(n) do + if not tmp[i] then + tmp[i] = v -- pad holes with v ! + end + end + else + tmp[n] = v + end + return table.concat(tmp, ";"):gsub("^;*(.-);*$", "%1"):gsub(";+",";") +end + +function mtwifi_utils.token_get(str, n, v) + -- n starts from 1 + -- v is the backup in case token n is nil + if not str then return v end + local tmp = mtwifi_utils.__cfg2list(str) + return tmp[tonumber(n)] or v +end + +function mtwifi_utils.sleep(s) + local ntime = os.clock() + s + repeat until os.clock() > ntime +end + +function mtwifi_utils.log2file(str) + logfile = io.open(mtwifi_logfile, "a") + logfile:write(os.date("%H:%M:%S", os.time()) .. " " .. str .. "\n") + logfile:close() +end + +return mtwifi_utils diff --git a/package/mtk/applications/mtwifi-cfg/files/mtwifi.sh b/package/mtk/applications/mtwifi-cfg/files/mtwifi.sh new file mode 100755 index 0000000000..29e33bb5ee --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/files/mtwifi.sh @@ -0,0 +1,55 @@ +#!/bin/sh +# +# Copyright (C) 2023, hanwckf +# + +append DRIVERS "mtwifi" + +detect_mtwifi() { + local idx ifname + local band hwmode htmode htbsscoex ssid + if [ -d "/sys/module/mt_wifi" ]; then + dev_list="$(l1util list)" + for dev in $dev_list; do + config_get type ${dev} type + [ "$type" = "mtwifi" ] || { + ifname="$(l1util get ${dev} main_ifname)" + idx="$(l1util get ${dev} subidx)" + if [ $idx -eq 1 ]; then + band="2g" + hwmode="11g" + htmode="HE40" + htbsscoex="1" + ssid="ImmortalWrt-2.4G" + else + band="5g" + hwmode="11a" + htmode="HE160" + htbsscoex="0" + ssid="ImmortalWrt-5G" + fi + uci -q batch <<-EOF + set wireless.${dev}=wifi-device + set wireless.${dev}.type=mtwifi + set wireless.${dev}.phy=${ifname} + set wireless.${dev}.hwmode=${hwmode} + set wireless.${dev}.band=${band} + set wireless.${dev}.channel=auto + set wireless.${dev}.txpower=100 + set wireless.${dev}.htmode=${htmode} + set wireless.${dev}.country=CN + set wireless.${dev}.mu_beamformer=1 + set wireless.${dev}.noscan=${htbsscoex} + + set wireless.default_${dev}=wifi-iface + set wireless.default_${dev}.device=${dev} + set wireless.default_${dev}.network=lan + set wireless.default_${dev}.mode=ap + set wireless.default_${dev}.ssid=${ssid} + set wireless.default_${dev}.encryption=none +EOF + uci -q commit wireless + } + done + fi +} diff --git a/package/mtk/applications/mtwifi-cfg/files/netifd/mtwifi.sh b/package/mtk/applications/mtwifi-cfg/files/netifd/mtwifi.sh new file mode 100755 index 0000000000..1c4c7ec71c --- /dev/null +++ b/package/mtk/applications/mtwifi-cfg/files/netifd/mtwifi.sh @@ -0,0 +1,140 @@ +#!/bin/sh +# +# Copyright (c) 2023, hanwckf +# + +. /lib/netifd/netifd-wireless.sh + +init_wireless_driver "$@" + +LOCK_FILE="/tmp/mtwifi.lock" + +MTWIFI_MAX_AP_IDX=15 +MTWIFI_MAX_APCLI_IDX=0 +MTWIFI_CFG_IFNAME_KEY="mtwifi_ifname" + +drv_mtwifi_init_device_config() { + config_add_int txpower frag rts + config_add_boolean mu_beamformer twt + config_add_string country +} + +drv_mtwifi_init_iface_config() { + config_add_string 'ssid:string' macfilter bssid + config_add_boolean wmm hidden isolate ieee80211k + config_add_int wpa_group_rekey + config_add_array 'maclist:list(macaddr)' + config_add_boolean mumimo_dl mumimo_ul ofdma_dl ofdma_ul amsdu autoba uapsd +} + +drv_mtwifi_cleanup() { + return +} + +mtwifi_vif_ap_config() { + local name="$1" + local ifname="" + local disabled="" + + json_select config + json_get_var disabled disabled + json_select .. + + [ "$disabled" = "1" ] && return + + json_get_var ifname $MTWIFI_CFG_IFNAME_KEY + + if [ -n "$ifname" ]; then + logger -t "netifd-mtwifi" "add $ifname to vifidx $name" + wireless_add_vif "$name" "$ifname" + fi +} + +mtwifi_vif_sta_config() { + local name="$1" + local ifname="" + local disabled="" + + json_select config + json_get_var disabled disabled + json_select .. + + [ "$disabled" = "1" ] && return + + json_get_var ifname $MTWIFI_CFG_IFNAME_KEY + + if [ -n "$ifname" ]; then + logger -t "netifd-mtwifi" "add $ifname to vifidx $name" + + # setup apcli autoconnect + iwpriv "$ifname" set ApCliAutoConnect=3 + + wireless_add_vif "$name" "$ifname" + fi +} + +mtwifi_vif_ap_set_data() { + local ifname="" + + if [ ! $AP_IDX -gt $MTWIFI_MAX_AP_IDX ]; then + ifname="${MTWIFI_AP_IF_PREFIX}${AP_IDX}" + AP_IDX=$((AP_IDX+1)) + fi + + json_add_string "$MTWIFI_CFG_IFNAME_KEY" "$ifname" +} + +mtwifi_vif_sta_set_data() { + local ifname="" + + if [ ! $APCLI_IDX -gt $MTWIFI_MAX_APCLI_IDX ]; then + ifname="${MTWIFI_APCLI_IF_PREFIX}${APCLI_IDX}" + APCLI_IDX=$((APCLI_IDX+1)) + fi + + json_add_string "$MTWIFI_CFG_IFNAME_KEY" "$ifname" +} + +drv_mtwifi_setup() { + ubus -t 120 wait_for network.interface.lan + + local dev="$1" + + json_add_string device "$dev" + + lock $LOCK_FILE + + logger -t "netifd-mtwifi" "up: $dev" + + MTWIFI_AP_IF_PREFIX="$(l1util get $dev ext_ifname)" + MTWIFI_APCLI_IF_PREFIX="$(l1util get $dev apcli_ifname)" + + AP_IDX=0 + for_each_interface ap mtwifi_vif_ap_set_data + + APCLI_IDX=0 + for_each_interface sta mtwifi_vif_sta_set_data + + json_dump | /sbin/mtwifi_cfg setup + + for_each_interface ap mtwifi_vif_ap_config + for_each_interface sta mtwifi_vif_sta_config + + wireless_set_up + + lock -u $LOCK_FILE +} + +drv_mtwifi_teardown() { + local dev="$1" + + lock $LOCK_FILE + + logger -t "netifd-mtwifi" "down: $dev" + + /sbin/mtwifi_cfg down "$dev" + + lock -u $LOCK_FILE +} + +add_driver mtwifi