5 <title>JSDoc: Source: network.js
</title>
7 <script src=
"scripts/prettify/prettify.js"> </script>
8 <script src=
"scripts/prettify/lang-css.js"> </script>
10 <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
12 <link type=
"text/css" rel=
"stylesheet" href=
"styles/prettify-tomorrow.css">
13 <link type=
"text/css" rel=
"stylesheet" href=
"styles/jsdoc-default.css">
20 <h1 class=
"page-title">Source: network.js
</h1>
29 <pre class=
"prettyprint source linenums"><code>'use strict';
35 CONNECT_FAILED: _('Connection attempt failed'),
36 INVALID_ADDRESS: _('IP address in invalid'),
37 INVALID_GATEWAY: _('Gateway address is invalid'),
38 INVALID_LOCAL_ADDRESS: _('Local IP address is invalid'),
39 MISSING_ADDRESS: _('IP address is missing'),
40 MISSING_PEER_ADDRESS: _('Peer address is missing'),
41 NO_DEVICE: _('Network device is not present'),
42 NO_IFACE: _('Unable to determine device name'),
43 NO_IFNAME: _('Unable to determine device name'),
44 NO_WAN_ADDRESS: _('Unable to determine external IP address'),
45 NO_WAN_LINK: _('Unable to determine upstream interface'),
46 PEER_RESOLVE_FAIL: _('Unable to resolve peer host name'),
47 PIN_FAILED: _('PIN code rejected')
50 var iface_patterns_ignore = [
66 var iface_patterns_wireless = [
73 var iface_patterns_virtual = [ ];
75 var callLuciNetworkDevices = rpc.declare({
77 method: 'getNetworkDevices',
81 var callLuciWirelessDevices = rpc.declare({
83 method: 'getWirelessDevices',
87 var callLuciBoardJSON = rpc.declare({
89 method: 'getBoardJSON'
92 var callLuciHostHints = rpc.declare({
94 method: 'getHostHints',
98 var callIwinfoAssoclist = rpc.declare({
101 params: [ 'device', 'mac' ],
102 expect: { results: [] }
105 var callIwinfoScan = rpc.declare({
108 params: [ 'device' ],
110 expect: { results: [] }
113 var callNetworkInterfaceDump = rpc.declare({
114 object: 'network.interface',
116 expect: { 'interface': [] }
119 var callNetworkProtoHandlers = rpc.declare({
121 method: 'get_proto_handlers',
130 function getProtocolHandlers(cache) {
131 return callNetworkProtoHandlers().then(function(protos) {
132 /* Register
"none" protocol */
133 if (!protos.hasOwnProperty('none'))
134 Object.assign(protos, { none: { no_device: false } });
136 /* Hack: emulate relayd protocol */
137 if (!protos.hasOwnProperty('relay'))
138 Object.assign(protos, { relay: { no_device: true } });
140 Object.assign(_protospecs, protos);
142 return Promise.all(Object.keys(protos).map(function(p) {
143 return Promise.resolve(L.require('protocol.%s'.format(p))).catch(function(err) {
144 if (L.isObject(err)
&& err.name != 'NetworkError')
147 })).then(function() {
150 }).catch(function() {
155 function getWifiStateBySid(sid) {
156 var s = uci.get('wireless', sid);
158 if (s != null
&& s['.type'] == 'wifi-iface') {
159 for (var radioname in _state.radios) {
160 for (var i =
0; i
< _state.radios[radioname].interfaces.length; i++) {
161 var netstate = _state.radios[radioname].interfaces[i];
163 if (typeof(netstate.section) != 'string')
166 var s2 = uci.get('wireless', netstate.section);
168 if (s2 != null
&& s['.type'] == s2['.type']
&& s['.name'] == s2['.name']) {
169 if (s2['.anonymous'] == false
&& netstate.section.charAt(
0) == '@')
172 return [ radioname, _state.radios[radioname], netstate ];
181 function getWifiStateByIfname(ifname) {
182 for (var radioname in _state.radios) {
183 for (var i =
0; i
< _state.radios[radioname].interfaces.length; i++) {
184 var netstate = _state.radios[radioname].interfaces[i];
186 if (typeof(netstate.ifname) != 'string')
189 if (netstate.ifname == ifname)
190 return [ radioname, _state.radios[radioname], netstate ];
197 function isWifiIfname(ifname) {
198 for (var i =
0; i
< iface_patterns_wireless.length; i++)
199 if (iface_patterns_wireless[i].test(ifname))
205 function getWifiSidByNetid(netid) {
206 var m = /^(\w+)\.network(\d+)$/.exec(netid);
208 var sections = uci.sections('wireless', 'wifi-iface');
209 for (var i =
0, n =
0; i
< sections.length; i++) {
210 if (sections[i].device != m[
1])
214 return sections[i]['.name'];
221 function getWifiSidByIfname(ifname) {
222 var sid = getWifiSidByNetid(ifname);
227 var res = getWifiStateByIfname(ifname);
229 if (res != null
&& L.isObject(res[
2])
&& typeof(res[
2].section) == 'string')
230 return res[
2].section;
235 function getWifiNetidBySid(sid) {
236 var s = uci.get('wireless', sid);
237 if (s != null
&& s['.type'] == 'wifi-iface') {
238 var radioname = s.device;
239 if (typeof(s.device) == 'string') {
240 var i =
0, netid = null, sections = uci.sections('wireless', 'wifi-iface');
241 for (var i =
0, n =
0; i
< sections.length; i++) {
242 if (sections[i].device != s.device)
247 if (sections[i]['.name'] != s['.name'])
250 return [ '%s.network%d'.format(s.device, n), s.device ];
259 function getWifiNetidByNetname(name) {
260 var sections = uci.sections('wireless', 'wifi-iface');
261 for (var i =
0; i
< sections.length; i++) {
262 if (typeof(sections[i].network) != 'string')
265 var nets = sections[i].network.split(/\s+/);
266 for (var j =
0; j
< nets.length; j++) {
270 return getWifiNetidBySid(sections[i]['.name']);
277 function isVirtualIfname(ifname) {
278 for (var i =
0; i
< iface_patterns_virtual.length; i++)
279 if (iface_patterns_virtual[i].test(ifname))
285 function isIgnoredIfname(ifname) {
286 for (var i =
0; i
< iface_patterns_ignore.length; i++)
287 if (iface_patterns_ignore[i].test(ifname))
293 function appendValue(config, section, option, value) {
294 var values = uci.get(config, section, option),
295 isArray = Array.isArray(values),
298 if (isArray == false)
299 values = L.toArray(values);
301 if (values.indexOf(value) == -
1) {
306 uci.set(config, section, option, isArray ? values : values.join(' '));
311 function removeValue(config, section, option, value) {
312 var values = uci.get(config, section, option),
313 isArray = Array.isArray(values),
316 if (isArray == false)
317 values = L.toArray(values);
319 for (var i = values.length -
1; i
>=
0; i--) {
320 if (values[i] == value) {
326 if (values.length
> 0)
327 uci.set(config, section, option, isArray ? values : values.join(' '));
329 uci.unset(config, section, option);
334 function prefixToMask(bits, v6) {
335 var w = v6 ?
128 :
32,
341 for (var i =
0; i
< w /
16; i++) {
342 var b = Math.min(
16, bits);
343 m.push((
0xffff << (
16 - b))
& 0xffff);
348 return String.prototype.format.apply('%x:%x:%x:%x:%x:%x:%x:%x', m).replace(/:
0(?::
0)+$/, '::');
350 return '%d.%d.%d.%d'.format(m[
0]
>>> 8, m[
0]
& 0xff, m[
1]
>>> 8, m[
1]
& 0xff);
353 function maskToPrefix(mask, v6) {
354 var m = v6 ? validation.parseIPv6(mask) : validation.parseIPv4(mask);
361 for (var i =
0, z = false; i
< m.length; i++) {
364 while (!z
&& (m[i]
& (v6 ?
0x8000 :
0x80))) {
365 m[i] = (m[i]
<< 1)
& (v6 ?
0xffff :
0xff);
376 function initNetworkState(refresh) {
377 if (_state == null || refresh) {
378 _init = _init || Promise.all([
379 L.resolveDefault(callNetworkInterfaceDump(), []),
380 L.resolveDefault(callLuciBoardJSON(), {}),
381 L.resolveDefault(callLuciNetworkDevices(), {}),
382 L.resolveDefault(callLuciWirelessDevices(), {}),
383 L.resolveDefault(callLuciHostHints(), {}),
384 getProtocolHandlers(),
385 uci.load(['network', 'wireless', 'luci'])
386 ]).then(function(data) {
387 var netifd_ifaces = data[
0],
388 board_json = data[
1],
392 isTunnel: {}, isBridge: {}, isSwitch: {}, isWifi: {},
393 ifaces: netifd_ifaces, radios: data[
3], hosts: data[
4],
394 netdevs: {}, bridges: {}, switches: {}
397 for (var name in luci_devs) {
398 var dev = luci_devs[name];
400 if (isVirtualIfname(name))
401 s.isTunnel[name] = true;
403 if (!s.isTunnel[name]
&& isIgnoredIfname(name))
406 s.netdevs[name] = s.netdevs[name] || {
420 if (Array.isArray(dev.ipaddrs))
421 for (var i =
0; i
< dev.ipaddrs.length; i++)
422 s.netdevs[name].ipaddrs.push(dev.ipaddrs[i].address + '/' + dev.ipaddrs[i].netmask);
424 if (Array.isArray(dev.ip6addrs))
425 for (var i =
0; i
< dev.ip6addrs.length; i++)
426 s.netdevs[name].ip6addrs.push(dev.ip6addrs[i].address + '/' + dev.ip6addrs[i].netmask);
429 for (var name in luci_devs) {
430 var dev = luci_devs[name];
442 for (var i =
0; dev.ports
&& i
< dev.ports.length; i++) {
443 var subdev = s.netdevs[dev.ports[i]];
448 b.ifnames.push(subdev);
453 s.isBridge[name] = true;
456 if (L.isObject(board_json.switch)) {
457 for (var switchname in board_json.switch) {
458 var layout = board_json.switch[switchname],
465 if (L.isObject(layout)
&& Array.isArray(layout.ports)) {
466 for (var i =
0, port; (port = layout.ports[i]) != null; i++) {
467 if (typeof(port) == 'object'
&& typeof(port.num) == 'number'
&&
468 (typeof(port.role) == 'string' || typeof(port.device) == 'string')) {
471 role: port.role || 'cpu',
472 index: (port.index != null) ? port.index : port.num
475 if (port.device != null) {
476 spec.device = port.device;
477 spec.tagged = spec.need_tag;
478 netdevs[port.num] = port.device;
483 if (port.role != null)
484 nports[port.role] = (nports[port.role] ||
0) +
1;
488 ports.sort(function(a, b) {
489 if (a.role != b.role)
490 return (a.role
< b.role) ? -
1 :
1;
492 return (a.index - b.index);
495 for (var i =
0, port; (port = ports[i]) != null; i++) {
496 if (port.role != role) {
502 port.label = 'CPU (%s)'.format(port.device);
503 else if (nports[role]
> 1)
504 port.label = '%s %d'.format(role.toUpperCase(), pnum++);
506 port.label = role.toUpperCase();
512 s.switches[switchname] = {
520 if (L.isObject(board_json.dsl)
&& L.isObject(board_json.dsl.modem)) {
521 s.hasDSLModem = board_json.dsl.modem;
530 return (_state != null ? Promise.resolve(_state) : _init);
533 function ifnameOf(obj) {
534 if (obj instanceof Protocol)
535 return obj.getIfname();
536 else if (obj instanceof Device)
537 return obj.getName();
538 else if (obj instanceof WifiDevice)
539 return obj.getName();
540 else if (obj instanceof WifiNetwork)
541 return obj.getIfname();
542 else if (typeof(obj) == 'string')
543 return obj.replace(/:.+$/, '');
548 function networkSort(a, b) {
549 return a.getName()
> b.getName();
552 function deviceSort(a, b) {
553 var typeWeigth = { wifi:
2, alias:
3 },
554 weightA = typeWeigth[a.getType()] ||
1,
555 weightB = typeWeigth[b.getType()] ||
1;
557 if (weightA != weightB)
558 return weightA - weightB;
560 return a.getName()
> b.getName();
563 function formatWifiEncryption(enc) {
564 if (!L.isObject(enc))
570 var ciphers = Array.isArray(enc.ciphers)
571 ? enc.ciphers.map(function(c) { return c.toUpperCase() }) : [ 'NONE' ];
573 if (Array.isArray(enc.wep)) {
574 var has_open = false,
577 for (var i =
0; i
< enc.wep.length; i++)
578 if (enc.wep[i] == 'open')
580 else if (enc.wep[i] == 'shared')
583 if (has_open
&& has_shared)
584 return 'WEP Open/Shared (%s)'.format(ciphers.join(', '));
586 return 'WEP Open System (%s)'.format(ciphers.join(', '));
588 return 'WEP Shared Auth (%s)'.format(ciphers.join(', '));
593 if (Array.isArray(enc.wpa)) {
595 suites = Array.isArray(enc.authentication)
596 ? enc.authentication.map(function(a) { return a.toUpperCase() }) : [ 'NONE' ];
598 for (var i =
0; i
< enc.wpa.length; i++)
599 switch (enc.wpa[i]) {
601 versions.push('WPA');
605 versions.push('WPA%d'.format(enc.wpa[i]));
609 if (versions.length
> 1)
610 return 'mixed %s %s (%s)'.format(versions.join('/'), suites.join(', '), ciphers.join(', '));
612 return '%s %s (%s)'.format(versions[
0], suites.join(', '), ciphers.join(', '));
618 function enumerateNetworks() {
619 var uciInterfaces = uci.sections('network', 'interface'),
622 for (var i =
0; i
< uciInterfaces.length; i++)
623 networks[uciInterfaces[i]['.name']] = this.instantiateNetwork(uciInterfaces[i]['.name']);
625 for (var i =
0; i
< _state.ifaces.length; i++)
626 if (networks[_state.ifaces[i].interface] == null)
627 networks[_state.ifaces[i].interface] =
628 this.instantiateNetwork(_state.ifaces[i].interface, _state.ifaces[i].proto);
632 for (var network in networks)
633 if (networks.hasOwnProperty(network))
634 rv.push(networks[network]);
636 rv.sort(networkSort);
642 var Hosts, Network, Protocol, Device, WifiDevice, WifiNetwork;
650 * The `LuCI.Network` class combines data from multiple `ubus` apis to
651 * provide an abstraction of the current network configuration state.
653 * It provides methods to enumerate interfaces and devices, to query
654 * current configuration details and to manipulate settings.
656 Network = L.Class.extend(/** @lends LuCI.Network.prototype */ {
658 * Converts the given prefix size in bits to a netmask.
662 * @param {number} bits
663 * The prefix size in bits.
665 * @param {boolean} [v6=false]
666 * Whether to convert the bits value into an IPv4 netmask (`false`) or
667 * an IPv6 netmask (`true`).
669 * @returns {null|string}
670 * Returns a string containing the netmask corresponding to the bit count
671 * or `null` when the given amount of bits exceeds the maximum possible
672 * value of `
32` for IPv4 or `
128` for IPv6.
674 prefixToMask: prefixToMask,
677 * Converts the given netmask to a prefix size in bits.
681 * @param {string} netmask
682 * The netmask to convert into a bit count.
684 * @param {boolean} [v6=false]
685 * Whether to parse the given netmask as IPv4 (`false`) or IPv6 (`true`)
688 * @returns {null|number}
689 * Returns the number of prefix bits contained in the netmask or `null`
690 * if the given netmask value was invalid.
692 maskToPrefix: maskToPrefix,
695 * An encryption entry describes active wireless encryption settings
696 * such as the used key management protocols, active ciphers and
699 * @typedef {Object
<string, boolean|Array
<number|string
>>} LuCI.Network.WifiEncryption
700 * @memberof LuCI.Network
702 * @property {boolean} enabled
703 * Specifies whether any kind of encryption, such as `WEP` or `WPA` is
704 * enabled. If set to `false`, then no encryption is active and the
705 * corresponding network is open.
707 * @property {string[]} [wep]
708 * When the `wep` property exists, the network uses WEP encryption.
709 * In this case, the property is set to an array of active WEP modes
710 * which might be either `open`, `shared` or both.
712 * @property {number[]} [wpa]
713 * When the `wpa` property exists, the network uses WPA security.
714 * In this case, the property is set to an array containing the WPA
715 * protocol versions used, e.g. `[
1,
2 ]` for WPA/WPA2 mixed mode or
716 * `[
3 ]` for WPA3-SAE.
718 * @property {string[]} [authentication]
719 * The `authentication` property only applies to WPA encryption and
720 * is defined when the `wpa` property is set as well. It points to
721 * an array of active authentication suites used by the network, e.g.
722 * `[
"psk" ]` for a WPA(
2)-PSK network or `[
"psk",
"sae" ]` for
723 * mixed WPA2-PSK/WPA3-SAE encryption.
725 * @property {string[]} [ciphers]
726 * If either WEP or WPA encryption is active, then the `ciphers`
727 * property will be set to an array describing the active encryption
728 * ciphers used by the network, e.g. `[
"tkip",
"ccmp" ]` for a
729 * WPA/WPA2-PSK mixed network or `[
"wep-40",
"wep-104" ]` for an
734 * Converts a given {@link LuCI.Network.WifiEncryption encryption entry}
735 * into a human readable string such as `mixed WPA/WPA2 PSK (TKIP, CCMP)`
736 * or `WPA3 SAE (CCMP)`.
740 * @param {LuCI.Network.WifiEncryption} encryption
741 * The wireless encryption entry to convert.
743 * @returns {null|string}
744 * Returns the description string for the given encryption entry or
745 * `null` if the given entry was invalid.
747 formatWifiEncryption: formatWifiEncryption,
750 * Flushes the local network state cache and fetches updated information
751 * from the remote `ubus` apis.
753 * @returns {Promise
<Object
>}
754 * Returns a promise resolving to the internal network state object.
756 flushCache: function() {
757 initNetworkState(true);
762 * Instantiates the given {@link LuCI.Network.Protocol Protocol} backend,
763 * optionally using the given network name.
765 * @param {string} protoname
766 * The protocol backend to use, e.g. `static` or `dhcp`.
768 * @param {string} [netname=__dummy__]
769 * The network name to use for the instantiated protocol. This should be
770 * usually set to one of the interfaces described in /etc/config/network
771 * but it is allowed to omit it, e.g. to query protocol capabilities
772 * without the need for an existing interface.
774 * @returns {null|LuCI.Network.Protocol}
775 * Returns the instantiated protocol backend class or `null` if the given
776 * protocol isn't known.
778 getProtocol: function(protoname, netname) {
779 var v = _protocols[protoname];
781 return new v(netname || '__dummy__');
787 * Obtains instances of all known {@link LuCI.Network.Protocol Protocol}
790 * @returns {Array
<LuCI.Network.Protocol
>}
791 * Returns an array of protocol class instances.
793 getProtocols: function() {
796 for (var protoname in _protocols)
797 rv.push(new _protocols[protoname]('__dummy__'));
803 * Registers a new {@link LuCI.Network.Protocol Protocol} subclass
804 * with the given methods and returns the resulting subclass value.
806 * This functions internally calls
807 * {@link LuCI.Class.extend Class.extend()} on the `Network.Protocol`
810 * @param {string} protoname
811 * The name of the new protocol to register.
813 * @param {Object
<string, *
>} methods
814 * The member methods and values of the new `Protocol` subclass to
815 * be passed to {@link LuCI.Class.extend Class.extend()}.
817 * @returns {LuCI.Network.Protocol}
818 * Returns the new `Protocol` subclass.
820 registerProtocol: function(protoname, methods) {
821 var spec = L.isObject(_protospecs) ? _protospecs[protoname] : null;
822 var proto = Protocol.extend(Object.assign({
823 getI18n: function() {
827 isFloating: function() {
831 isVirtual: function() {
832 return (L.isObject(spec)
&& spec.no_device == true);
835 renderFormOptions: function(section) {
839 __init__: function(name) {
843 getProtocol: function() {
848 _protocols[protoname] = proto;
854 * Registers a new regular expression pattern to recognize
855 * virtual interfaces.
857 * @param {RegExp} pat
858 * A `RegExp` instance to match a virtual interface name
859 * such as `
6in4-wan` or `tun0`.
861 registerPatternVirtual: function(pat) {
862 iface_patterns_virtual.push(pat);
866 * Registers a new human readable translation string for a `Protocol`
869 * @param {string} code
870 * The `ubus` protocol error code to register a translation for, e.g.
873 * @param {string} message
874 * The message to use as translation for the given protocol error code.
877 * Returns `true` if the error code description has been added or `false`
878 * if either the arguments were invalid or if there already was a
879 * description for the given code.
881 registerErrorCode: function(code, message) {
882 if (typeof(code) == 'string'
&&
883 typeof(message) == 'string'
&&
884 !proto_errors.hasOwnProperty(code)) {
885 proto_errors[code] = message;
893 * Adds a new network of the given name and update it with the given
896 * If a network with the given name already exist but is empty, then
897 * this function will update its option, otherwise it will do nothing.
899 * @param {string} name
900 * The name of the network to add. Must be in the format `[a-zA-Z0-
9_]+`.
902 * @param {Object
<string, string|string[]
>} [options]
903 * An object of uci option values to set on the new network or to
904 * update in an existing, empty network.
906 * @returns {Promise
<null|LuCI.Network.Protocol
>}
907 * Returns a promise resolving to the `Protocol` subclass instance
908 * describing the added network or resolving to `null` if the name
909 * was invalid or if a non-empty network of the given name already
912 addNetwork: function(name, options) {
913 return this.getNetwork(name).then(L.bind(function(existingNetwork) {
914 if (name != null
&& /^[a-zA-Z0-
9_]+$/.test(name)
&& existingNetwork == null) {
915 var sid = uci.add('network', 'interface', name);
918 if (L.isObject(options))
919 for (var key in options)
920 if (options.hasOwnProperty(key))
921 uci.set('network', sid, key, options[key]);
923 return this.instantiateNetwork(sid);
926 else if (existingNetwork != null
&& existingNetwork.isEmpty()) {
927 if (L.isObject(options))
928 for (var key in options)
929 if (options.hasOwnProperty(key))
930 existingNetwork.set(key, options[key]);
932 return existingNetwork;
938 * Get a {@link LuCI.Network.Protocol Protocol} instance describing
939 * the network with the given name.
941 * @param {string} name
942 * The logical interface name of the network get, e.g. `lan` or `wan`.
944 * @returns {Promise
<null|LuCI.Network.Protocol
>}
945 * Returns a promise resolving to a
946 * {@link LuCI.Network.Protocol Protocol} subclass instance describing
947 * the network or `null` if the network did not exist.
949 getNetwork: function(name) {
950 return initNetworkState().then(L.bind(function() {
951 var section = (name != null) ? uci.get('network', name) : null;
953 if (section != null
&& section['.type'] == 'interface') {
954 return this.instantiateNetwork(name);
956 else if (name != null) {
957 for (var i =
0; i
< _state.ifaces.length; i++)
958 if (_state.ifaces[i].interface == name)
959 return this.instantiateNetwork(name, _state.ifaces[i].proto);
967 * Gets an array containing all known networks.
969 * @returns {Promise
<Array
<LuCI.Network.Protocol
>>}
970 * Returns a promise resolving to a name-sorted array of
971 * {@link LuCI.Network.Protocol Protocol} subclass instances
972 * describing all known networks.
974 getNetworks: function() {
975 return initNetworkState().then(L.bind(enumerateNetworks, this));
979 * Deletes the given network and its references from the network and
980 * firewall configuration.
982 * @param {string} name
983 * The name of the network to delete.
985 * @returns {Promise
<boolean
>}
986 * Returns a promise resolving to either `true` if the network and
987 * references to it were successfully deleted from the configuration or
988 * `false` if the given network could not be found.
990 deleteNetwork: function(name) {
991 var requireFirewall = Promise.resolve(L.require('firewall')).catch(function() {});
993 return Promise.all([ requireFirewall, initNetworkState() ]).then(function() {
994 var uciInterface = uci.get('network', name);
996 if (uciInterface != null
&& uciInterface['.type'] == 'interface') {
997 uci.remove('network', name);
999 uci.sections('luci', 'ifstate', function(s) {
1000 if (s.interface == name)
1001 uci.remove('luci', s['.name']);
1004 uci.sections('network', 'alias', function(s) {
1005 if (s.interface == name)
1006 uci.remove('network', s['.name']);
1009 uci.sections('network', 'route', function(s) {
1010 if (s.interface == name)
1011 uci.remove('network', s['.name']);
1014 uci.sections('network', 'route6', function(s) {
1015 if (s.interface == name)
1016 uci.remove('network', s['.name']);
1019 uci.sections('wireless', 'wifi-iface', function(s) {
1020 var networks = L.toArray(s.network).filter(function(network) { return network != name });
1022 if (networks.length
> 0)
1023 uci.set('wireless', s['.name'], 'network', networks.join(' '));
1025 uci.unset('wireless', s['.name'], 'network');
1029 return L.firewall.deleteNetwork(name).then(function() { return true });
1039 * Rename the given network and its references to a new name.
1041 * @param {string} oldName
1042 * The current name of the network.
1044 * @param {string} newName
1045 * The name to rename the network to, must be in the format
1048 * @returns {Promise
<boolean
>}
1049 * Returns a promise resolving to either `true` if the network was
1050 * successfully renamed or `false` if the new name was invalid, if
1051 * a network with the new name already exists or if the network to
1052 * rename could not be found.
1054 renameNetwork: function(oldName, newName) {
1055 return initNetworkState().then(function() {
1056 if (newName == null || !/^[a-zA-Z0-
9_]+$/.test(newName) || uci.get('network', newName) != null)
1059 var oldNetwork = uci.get('network', oldName);
1061 if (oldNetwork == null || oldNetwork['.type'] != 'interface')
1064 var sid = uci.add('network', 'interface', newName);
1066 for (var key in oldNetwork)
1067 if (oldNetwork.hasOwnProperty(key)
&& key.charAt(
0) != '.')
1068 uci.set('network', sid, key, oldNetwork[key]);
1070 uci.sections('luci', 'ifstate', function(s) {
1071 if (s.interface == oldName)
1072 uci.set('luci', s['.name'], 'interface', newName);
1075 uci.sections('network', 'alias', function(s) {
1076 if (s.interface == oldName)
1077 uci.set('network', s['.name'], 'interface', newName);
1080 uci.sections('network', 'route', function(s) {
1081 if (s.interface == oldName)
1082 uci.set('network', s['.name'], 'interface', newName);
1085 uci.sections('network', 'route6', function(s) {
1086 if (s.interface == oldName)
1087 uci.set('network', s['.name'], 'interface', newName);
1090 uci.sections('wireless', 'wifi-iface', function(s) {
1091 var networks = L.toArray(s.network).map(function(network) { return (network == oldName ? newName : network) });
1093 if (networks.length
> 0)
1094 uci.set('wireless', s['.name'], 'network', networks.join(' '));
1097 uci.remove('network', oldName);
1104 * Get a {@link LuCI.Network.Device Device} instance describing the
1105 * given network device.
1107 * @param {string} name
1108 * The name of the network device to get, e.g. `eth0` or `br-lan`.
1110 * @returns {Promise
<null|LuCI.Network.Device
>}
1111 * Returns a promise resolving to the `Device` instance describing
1112 * the network device or `null` if the given device name could not
1115 getDevice: function(name) {
1116 return initNetworkState().then(L.bind(function() {
1120 if (_state.netdevs.hasOwnProperty(name) || isWifiIfname(name))
1121 return this.instantiateDevice(name);
1123 var netid = getWifiNetidBySid(name);
1125 return this.instantiateDevice(netid[
0]);
1132 * Get a sorted list of all found network devices.
1134 * @returns {Promise
<Array
<LuCI.Network.Device
>>}
1135 * Returns a promise resolving to a sorted array of `Device` class
1136 * instances describing the network devices found on the system.
1138 getDevices: function() {
1139 return initNetworkState().then(L.bind(function() {
1142 /* find simple devices */
1143 var uciInterfaces = uci.sections('network', 'interface');
1144 for (var i =
0; i
< uciInterfaces.length; i++) {
1145 var ifnames = L.toArray(uciInterfaces[i].ifname);
1147 for (var j =
0; j
< ifnames.length; j++) {
1148 if (ifnames[j].charAt(
0) == '@')
1151 if (isIgnoredIfname(ifnames[j]) || isVirtualIfname(ifnames[j]) || isWifiIfname(ifnames[j]))
1154 devices[ifnames[j]] = this.instantiateDevice(ifnames[j]);
1158 for (var ifname in _state.netdevs) {
1159 if (devices.hasOwnProperty(ifname))
1162 if (isIgnoredIfname(ifname) || isVirtualIfname(ifname) || isWifiIfname(ifname))
1165 devices[ifname] = this.instantiateDevice(ifname);
1168 /* find VLAN devices */
1169 var uciSwitchVLANs = uci.sections('network', 'switch_vlan');
1170 for (var i =
0; i
< uciSwitchVLANs.length; i++) {
1171 if (typeof(uciSwitchVLANs[i].ports) != 'string' ||
1172 typeof(uciSwitchVLANs[i].device) != 'string' ||
1173 !_state.switches.hasOwnProperty(uciSwitchVLANs[i].device))
1176 var ports = uciSwitchVLANs[i].ports.split(/\s+/);
1177 for (var j =
0; j
< ports.length; j++) {
1178 var m = ports[j].match(/^(\d+)([tu]?)$/);
1182 var netdev = _state.switches[uciSwitchVLANs[i].device].netdevs[m[
1]];
1186 if (!devices.hasOwnProperty(netdev))
1187 devices[netdev] = this.instantiateDevice(netdev);
1189 _state.isSwitch[netdev] = true;
1194 var vid = uciSwitchVLANs[i].vid || uciSwitchVLANs[i].vlan;
1195 vid = (vid != null ? +vid : null);
1197 if (vid == null || vid
< 0 || vid
> 4095)
1200 var vlandev = '%s.%d'.format(netdev, vid);
1202 if (!devices.hasOwnProperty(vlandev))
1203 devices[vlandev] = this.instantiateDevice(vlandev);
1205 _state.isSwitch[vlandev] = true;
1209 /* find wireless interfaces */
1210 var uciWifiIfaces = uci.sections('wireless', 'wifi-iface'),
1213 for (var i =
0; i
< uciWifiIfaces.length; i++) {
1214 if (typeof(uciWifiIfaces[i].device) != 'string')
1217 networkCount[uciWifiIfaces[i].device] = (networkCount[uciWifiIfaces[i].device] ||
0) +
1;
1219 var netid = '%s.network%d'.format(uciWifiIfaces[i].device, networkCount[uciWifiIfaces[i].device]);
1221 devices[netid] = this.instantiateDevice(netid);
1226 for (var netdev in devices)
1227 if (devices.hasOwnProperty(netdev))
1228 rv.push(devices[netdev]);
1230 rv.sort(deviceSort);
1237 * Test if a given network device name is in the list of patterns for
1238 * device names to ignore.
1240 * Ignored device names are usually Linux network devices which are
1241 * spawned implicitly by kernel modules such as `tunl0` or `hwsim0`
1242 * and which are unsuitable for use in network configuration.
1244 * @param {string} name
1245 * The device name to test.
1247 * @returns {boolean}
1248 * Returns `true` if the given name is in the ignore pattern list,
1249 * else returns `false`.
1251 isIgnoredDevice: function(name) {
1252 return isIgnoredIfname(name);
1256 * Get a {@link LuCI.Network.WifiDevice WifiDevice} instance describing
1257 * the given wireless radio.
1259 * @param {string} devname
1260 * The configuration name of the wireless radio to lookup, e.g. `radio0`
1261 * for the first mac80211 phy on the system.
1263 * @returns {Promise
<null|LuCI.Network.WifiDevice
>}
1264 * Returns a promise resolving to the `WifiDevice` instance describing
1265 * the underlying radio device or `null` if the wireless radio could not
1268 getWifiDevice: function(devname) {
1269 return initNetworkState().then(L.bind(function() {
1270 var existingDevice = uci.get('wireless', devname);
1272 if (existingDevice == null || existingDevice['.type'] != 'wifi-device')
1275 return this.instantiateWifiDevice(devname, _state.radios[devname] || {});
1280 * Obtain a list of all configured radio devices.
1282 * @returns {Promise
<Array
<LuCI.Network.WifiDevice
>>}
1283 * Returns a promise resolving to an array of `WifiDevice` instances
1284 * describing the wireless radios configured in the system.
1285 * The order of the array corresponds to the order of the radios in
1286 * the configuration.
1288 getWifiDevices: function() {
1289 return initNetworkState().then(L.bind(function() {
1290 var uciWifiDevices = uci.sections('wireless', 'wifi-device'),
1293 for (var i =
0; i
< uciWifiDevices.length; i++) {
1294 var devname = uciWifiDevices[i]['.name'];
1295 rv.push(this.instantiateWifiDevice(devname, _state.radios[devname] || {}));
1303 * Get a {@link LuCI.Network.WifiNetwork WifiNetwork} instance describing
1304 * the given wireless network.
1306 * @param {string} netname
1307 * The name of the wireless network to lookup. This may be either an uci
1308 * configuration section ID, a network ID in the form `radio#.network#`
1309 * or a Linux network device name like `wlan0` which is resolved to the
1310 * corresponding configuration section through `ubus` runtime information.
1312 * @returns {Promise
<null|LuCI.Network.WifiNetwork
>}
1313 * Returns a promise resolving to the `WifiNetwork` instance describing
1314 * the wireless network or `null` if the corresponding network could not
1317 getWifiNetwork: function(netname) {
1318 var sid, res, netid, radioname, radiostate, netstate;
1320 return initNetworkState().then(L.bind(function() {
1321 sid = getWifiSidByNetid(netname);
1324 res = getWifiStateBySid(sid);
1326 radioname = res ? res[
0] : null;
1327 radiostate = res ? res[
1] : null;
1328 netstate = res ? res[
2] : null;
1331 res = getWifiStateByIfname(netname);
1335 radiostate = res[
1];
1337 sid = netstate.section;
1338 netid = L.toArray(getWifiNetidBySid(sid))[
0];
1341 res = getWifiStateBySid(netname);
1345 radiostate = res[
1];
1348 netid = L.toArray(getWifiNetidBySid(sid))[
0];
1351 res = getWifiNetidBySid(netname);
1362 return this.instantiateWifiNetwork(sid || netname, radioname, radiostate, netid, netstate);
1367 * Adds a new wireless network to the configuration and sets its options
1368 * to the provided values.
1370 * @param {Object
<string, string|string[]
>} options
1371 * The options to set for the newly added wireless network. This object
1372 * must at least contain a `device` property which is set to the radio
1373 * name the new network belongs to.
1375 * @returns {Promise
<null|LuCI.Network.WifiNetwork
>}
1376 * Returns a promise resolving to a `WifiNetwork` instance describing
1377 * the newly added wireless network or `null` if the given options
1378 * were invalid or if the associated radio device could not be found.
1380 addWifiNetwork: function(options) {
1381 return initNetworkState().then(L.bind(function() {
1382 if (options == null ||
1383 typeof(options) != 'object' ||
1384 typeof(options.device) != 'string')
1387 var existingDevice = uci.get('wireless', options.device);
1388 if (existingDevice == null || existingDevice['.type'] != 'wifi-device')
1391 /* XXX: need to add a named section (wifinet#) here */
1392 var sid = uci.add('wireless', 'wifi-iface');
1393 for (var key in options)
1394 if (options.hasOwnProperty(key))
1395 uci.set('wireless', sid, key, options[key]);
1397 var radioname = existingDevice['.name'],
1398 netid = getWifiNetidBySid(sid) || [];
1400 return this.instantiateWifiNetwork(sid, radioname, _state.radios[radioname], netid[
0], null);
1405 * Deletes the given wireless network from the configuration.
1407 * @param {string} netname
1408 * The name of the network to remove. This may be either a
1409 * network ID in the form `radio#.network#` or a Linux network device
1410 * name like `wlan0` which is resolved to the corresponding configuration
1411 * section through `ubus` runtime information.
1413 * @returns {Promise
<boolean
>}
1414 * Returns a promise resolving to `true` if the wireless network has been
1415 * successfully deleted from the configuration or `false` if it could not
1418 deleteWifiNetwork: function(netname) {
1419 return initNetworkState().then(L.bind(function() {
1420 var sid = getWifiSidByIfname(netname);
1425 uci.remove('wireless', sid);
1431 getStatusByRoute: function(addr, mask) {
1432 return initNetworkState().then(L.bind(function() {
1435 for (var i =
0; i
< _state.ifaces.length; i++) {
1436 if (!Array.isArray(_state.ifaces[i].route))
1439 for (var j =
0; j
< _state.ifaces[i].route.length; j++) {
1440 if (typeof(_state.ifaces[i].route[j]) != 'object' ||
1441 typeof(_state.ifaces[i].route[j].target) != 'string' ||
1442 typeof(_state.ifaces[i].route[j].mask) != 'number')
1445 if (_state.ifaces[i].route[j].table)
1448 if (_state.ifaces[i].route[j].target != addr ||
1449 _state.ifaces[i].route[j].mask != mask)
1452 rv.push(_state.ifaces[i]);
1461 getStatusByAddress: function(addr) {
1462 return initNetworkState().then(L.bind(function() {
1465 for (var i =
0; i
< _state.ifaces.length; i++) {
1466 if (Array.isArray(_state.ifaces[i]['ipv4-address']))
1467 for (var j =
0; j
< _state.ifaces[i]['ipv4-address'].length; j++)
1468 if (typeof(_state.ifaces[i]['ipv4-address'][j]) == 'object'
&&
1469 _state.ifaces[i]['ipv4-address'][j].address == addr)
1470 return _state.ifaces[i];
1472 if (Array.isArray(_state.ifaces[i]['ipv6-address']))
1473 for (var j =
0; j
< _state.ifaces[i]['ipv6-address'].length; j++)
1474 if (typeof(_state.ifaces[i]['ipv6-address'][j]) == 'object'
&&
1475 _state.ifaces[i]['ipv6-address'][j].address == addr)
1476 return _state.ifaces[i];
1478 if (Array.isArray(_state.ifaces[i]['ipv6-prefix-assignment']))
1479 for (var j =
0; j
< _state.ifaces[i]['ipv6-prefix-assignment'].length; j++)
1480 if (typeof(_state.ifaces[i]['ipv6-prefix-assignment'][j]) == 'object'
&&
1481 typeof(_state.ifaces[i]['ipv6-prefix-assignment'][j]['local-address']) == 'object'
&&
1482 _state.ifaces[i]['ipv6-prefix-assignment'][j]['local-address'].address == addr)
1483 return _state.ifaces[i];
1491 * Get IPv4 wan networks.
1493 * This function looks up all networks having a default `
0.0.0.0/
0` route
1494 * and returns them as array.
1496 * @returns {Promise
<Array
<LuCI.Network.Protocol
>>}
1497 * Returns a promise resolving to an array of `Protocol` subclass
1498 * instances describing the found default route interfaces.
1500 getWANNetworks: function() {
1501 return this.getStatusByRoute('
0.0.0.0',
0).then(L.bind(function(statuses) {
1504 for (var i =
0; i
< statuses.length; i++)
1505 rv.push(this.instantiateNetwork(statuses[i].interface, statuses[i].proto));
1512 * Get IPv6 wan networks.
1514 * This function looks up all networks having a default `::/
0` route
1515 * and returns them as array.
1517 * @returns {Promise
<Array
<LuCI.Network.Protocol
>>}
1518 * Returns a promise resolving to an array of `Protocol` subclass
1519 * instances describing the found IPv6 default route interfaces.
1521 getWAN6Networks: function() {
1522 return this.getStatusByRoute('::',
0).then(L.bind(function(statuses) {
1525 for (var i =
0; i
< statuses.length; i++)
1526 rv.push(this.instantiateNetwork(statuses[i].interface, statuses[i].proto));
1533 * Describes an swconfig switch topology by specifying the CPU
1534 * connections and external port labels of a switch.
1536 * @typedef {Object
<string, Object|Array
>} SwitchTopology
1537 * @memberof LuCI.Network
1539 * @property {Object
<number, string
>} netdevs
1540 * The `netdevs` property points to an object describing the CPU port
1541 * connections of the switch. The numeric key of the enclosed object is
1542 * the port number, the value contains the Linux network device name the
1543 * port is hardwired to.
1545 * @property {Array
<Object
<string, boolean|number|string
>>} ports
1546 * The `ports` property points to an array describing the populated
1547 * ports of the switch in the external label order. Each array item is
1548 * an object containg the following keys:
1549 * - `num` - the internal switch port number
1550 * - `label` - the label of the port, e.g. `LAN
1` or `CPU (eth0)`
1551 * - `device` - the connected Linux network device name (CPU ports only)
1552 * - `tagged` - a boolean indicating whether the port must be tagged to
1553 * function (CPU ports only)
1557 * Returns the topologies of all swconfig switches found on the system.
1559 * @returns {Promise
<Object
<string, LuCI.Network.SwitchTopology
>>}
1560 * Returns a promise resolving to an object containing the topologies
1561 * of each switch. The object keys correspond to the name of the switches
1562 * such as `switch0`, the values are
1563 * {@link LuCI.Network.SwitchTopology SwitchTopology} objects describing
1566 getSwitchTopologies: function() {
1567 return initNetworkState().then(function() {
1568 return _state.switches;
1573 instantiateNetwork: function(name, proto) {
1577 proto = (proto == null ? uci.get('network', name, 'proto') : proto);
1579 var protoClass = _protocols[proto] || Protocol;
1580 return new protoClass(name);
1584 instantiateDevice: function(name, network, extend) {
1586 return new (Device.extend(extend))(name, network);
1588 return new Device(name, network);
1592 instantiateWifiDevice: function(radioname, radiostate) {
1593 return new WifiDevice(radioname, radiostate);
1597 instantiateWifiNetwork: function(sid, radioname, radiostate, netid, netstate) {
1598 return new WifiNetwork(sid, radioname, radiostate, netid, netstate);
1602 * Obtains the the network device name of the given object.
1604 * @param {LuCI.Network.Protocol|LuCI.Network.Device|LuCI.Network.WifiDevice|LuCI.Network.WifiNetwork|string} obj
1605 * The object to get the device name from.
1607 * @returns {null|string}
1608 * Returns a string containing the device name or `null` if the given
1609 * object could not be converted to a name.
1611 getIfnameOf: function(obj) {
1612 return ifnameOf(obj);
1616 * Queries the internal DSL modem type from board information.
1618 * @returns {Promise
<null|string
>}
1619 * Returns a promise resolving to the type of the internal modem
1620 * (e.g. `vdsl`) or to `null` if no internal modem is present.
1622 getDSLModemType: function() {
1623 return initNetworkState().then(function() {
1624 return _state.hasDSLModem ? _state.hasDSLModem.type : null;
1629 * Queries aggregated information about known hosts.
1631 * This function aggregates information from various sources such as
1632 * DHCP lease databases, ARP and IPv6 neighbour entries, wireless
1633 * association list etc. and returns a {@link LuCI.Network.Hosts Hosts}
1634 * class instance describing the found hosts.
1636 * @returns {Promise
<LuCI.Network.Hosts
>}
1637 * Returns a `Hosts` instance describing host known on the system.
1639 getHostHints: function() {
1640 return initNetworkState().then(function() {
1641 return new Hosts(_state.hosts);
1648 * @memberof LuCI.Network
1652 * The `LuCI.Network.Hosts` class encapsulates host information aggregated
1653 * from multiple sources and provides convenience functions to access the
1654 * host information by different criteria.
1656 Hosts = L.Class.extend(/** @lends LuCI.Network.Hosts.prototype */ {
1657 __init__: function(hosts) {
1662 * Lookup the hostname associated with the given MAC address.
1664 * @param {string} mac
1665 * The MAC address to lookup.
1667 * @returns {null|string}
1668 * Returns the hostname associated with the given MAC or `null` if
1669 * no matching host could be found or if no hostname is known for
1670 * the corresponding host.
1672 getHostnameByMACAddr: function(mac) {
1673 return this.hosts[mac] ? this.hosts[mac].name : null;
1677 * Lookup the IPv4 address associated with the given MAC address.
1679 * @param {string} mac
1680 * The MAC address to lookup.
1682 * @returns {null|string}
1683 * Returns the IPv4 address associated with the given MAC or `null` if
1684 * no matching host could be found or if no IPv4 address is known for
1685 * the corresponding host.
1687 getIPAddrByMACAddr: function(mac) {
1688 return this.hosts[mac] ? this.hosts[mac].ipv4 : null;
1692 * Lookup the IPv6 address associated with the given MAC address.
1694 * @param {string} mac
1695 * The MAC address to lookup.
1697 * @returns {null|string}
1698 * Returns the IPv6 address associated with the given MAC or `null` if
1699 * no matching host could be found or if no IPv6 address is known for
1700 * the corresponding host.
1702 getIP6AddrByMACAddr: function(mac) {
1703 return this.hosts[mac] ? this.hosts[mac].ipv6 : null;
1707 * Lookup the hostname associated with the given IPv4 address.
1709 * @param {string} ipaddr
1710 * The IPv4 address to lookup.
1712 * @returns {null|string}
1713 * Returns the hostname associated with the given IPv4 or `null` if
1714 * no matching host could be found or if no hostname is known for
1715 * the corresponding host.
1717 getHostnameByIPAddr: function(ipaddr) {
1718 for (var mac in this.hosts)
1719 if (this.hosts[mac].ipv4 == ipaddr
&& this.hosts[mac].name != null)
1720 return this.hosts[mac].name;
1725 * Lookup the MAC address associated with the given IPv4 address.
1727 * @param {string} ipaddr
1728 * The IPv4 address to lookup.
1730 * @returns {null|string}
1731 * Returns the MAC address associated with the given IPv4 or `null` if
1732 * no matching host could be found or if no MAC address is known for
1733 * the corresponding host.
1735 getMACAddrByIPAddr: function(ipaddr) {
1736 for (var mac in this.hosts)
1737 if (this.hosts[mac].ipv4 == ipaddr)
1743 * Lookup the hostname associated with the given IPv6 address.
1745 * @param {string} ipaddr
1746 * The IPv6 address to lookup.
1748 * @returns {null|string}
1749 * Returns the hostname associated with the given IPv6 or `null` if
1750 * no matching host could be found or if no hostname is known for
1751 * the corresponding host.
1753 getHostnameByIP6Addr: function(ip6addr) {
1754 for (var mac in this.hosts)
1755 if (this.hosts[mac].ipv6 == ip6addr
&& this.hosts[mac].name != null)
1756 return this.hosts[mac].name;
1761 * Lookup the MAC address associated with the given IPv6 address.
1763 * @param {string} ipaddr
1764 * The IPv6 address to lookup.
1766 * @returns {null|string}
1767 * Returns the MAC address associated with the given IPv6 or `null` if
1768 * no matching host could be found or if no MAC address is known for
1769 * the corresponding host.
1771 getMACAddrByIP6Addr: function(ip6addr) {
1772 for (var mac in this.hosts)
1773 if (this.hosts[mac].ipv6 == ip6addr)
1779 * Return an array of (MAC address, name hint) tuples sorted by
1782 * @param {boolean} [preferIp6=false]
1783 * Whether to prefer IPv6 addresses (`true`) or IPv4 addresses (`false`)
1784 * as name hint when no hostname is known for a specific MAC address.
1786 * @returns {Array
<Array
<string
>>}
1787 * Returns an array of arrays containing a name hint for each found
1788 * MAC address on the system. The array is sorted ascending by MAC.
1790 * Each item of the resulting array is a two element array with the
1791 * MAC being the first element and the name hint being the second
1792 * element. The name hint is either the hostname, an IPv4 or an IPv6
1793 * address related to the MAC address.
1795 * If no hostname but both IPv4 and IPv6 addresses are known, the
1796 * `preferIP6` flag specifies whether the IPv6 or the IPv4 address
1799 getMACHints: function(preferIp6) {
1801 for (var mac in this.hosts) {
1802 var hint = this.hosts[mac].name ||
1803 this.hosts[mac][preferIp6 ? 'ipv6' : 'ipv4'] ||
1804 this.hosts[mac][preferIp6 ? 'ipv4' : 'ipv6'];
1806 rv.push([mac, hint]);
1808 return rv.sort(function(a, b) { return a[
0]
> b[
0] });
1814 * @memberof LuCI.Network
1818 * The `Network.Protocol` class serves as base for protocol specific
1819 * subclasses which describe logical UCI networks defined by `config
1820 * interface` sections in `/etc/config/network`.
1822 Protocol = L.Class.extend(/** @lends LuCI.Network.Protocol.prototype */ {
1823 __init__: function(name) {
1827 _get: function(opt) {
1828 var val = uci.get('network', this.sid, opt);
1830 if (Array.isArray(val))
1831 return val.join(' ');
1836 _ubus: function(field) {
1837 for (var i =
0; i
< _state.ifaces.length; i++) {
1838 if (_state.ifaces[i].interface != this.sid)
1841 return (field != null ? _state.ifaces[i][field] : _state.ifaces[i]);
1846 * Read the given UCI option value of this network.
1848 * @param {string} opt
1849 * The UCI option name to read.
1851 * @returns {null|string|string[]}
1852 * Returns the UCI option value or `null` if the requested option is
1855 get: function(opt) {
1856 return uci.get('network', this.sid, opt);
1860 * Set the given UCI option of this network to the given value.
1862 * @param {string} opt
1863 * The name of the UCI option to set.
1865 * @param {null|string|string[]} val
1866 * The value to set or `null` to remove the given option from the
1869 set: function(opt, val) {
1870 return uci.set('network', this.sid, opt, val);
1874 * Get the associared Linux network device of this network.
1876 * @returns {null|string}
1877 * Returns the name of the associated network device or `null` if
1878 * it could not be determined.
1880 getIfname: function() {
1883 if (this.isFloating())
1884 ifname = this._ubus('l3_device');
1886 ifname = this._ubus('device') || this._ubus('l3_device');
1891 var res = getWifiNetidByNetname(this.sid);
1892 return (res != null ? res[
0] : null);
1896 * Get the name of this network protocol class.
1898 * This function will be overwritten by subclasses created by
1899 * {@link LuCI.Network#registerProtocol Network.registerProtocol()}.
1903 * Returns the name of the network protocol implementation, e.g.
1904 * `static` or `dhcp`.
1906 getProtocol: function() {
1911 * Return a human readable description for the protcol, such as
1912 * `Static address` or `DHCP client`.
1914 * This function should be overwritten by subclasses.
1918 * Returns the description string.
1920 getI18n: function() {
1921 switch (this.getProtocol()) {
1922 case 'none': return _('Unmanaged');
1923 case 'static': return _('Static address');
1924 case 'dhcp': return _('DHCP client');
1925 default: return _('Unknown');
1930 * Get the type of the underlying interface.
1932 * This function actually is a convenience wrapper around
1933 * `proto.get(
"type")` and is mainly used by other `LuCI.Network` code
1934 * to check whether the interface is declared as bridge in UCI.
1936 * @returns {null|string}
1937 * Returns the value of the `type` option of the associated logical
1938 * interface or `null` if no `type` option is set.
1940 getType: function() {
1941 return this._get('type');
1945 * Get the name of the associated logical interface.
1948 * Returns the logical interface name, such as `lan` or `wan`.
1950 getName: function() {
1955 * Get the uptime of the logical interface.
1958 * Returns the uptime of the associated interface in seconds.
1960 getUptime: function() {
1961 return this._ubus('uptime') ||
0;
1965 * Get the logical interface expiry time in seconds.
1967 * For protocols that have a concept of a lease, such as DHCP or
1968 * DHCPv6, this function returns the remaining time in seconds
1969 * until the lease expires.
1972 * Returns the amount of seconds until the lease expires or `-
1`
1973 * if it isn't applicable to the associated protocol.
1975 getExpiry: function() {
1976 var u = this._ubus('uptime'),
1977 d = this._ubus('data');
1979 if (typeof(u) == 'number'
&& d != null
&&
1980 typeof(d) == 'object'
&& typeof(d.leasetime) == 'number') {
1981 var r = d.leasetime - (u % d.leasetime);
1982 return (r
> 0 ? r :
0);
1989 * Get the metric value of the logical interface.
1992 * Returns the current metric value used for device and network
1993 * routes spawned by the associated logical interface.
1995 getMetric: function() {
1996 return this._ubus('metric') ||
0;
2000 * Get the requested firewall zone name of the logical interface.
2002 * Some protocol implementations request a specific firewall zone
2003 * to trigger inclusion of their resulting network devices into the
2004 * firewall rule set.
2006 * @returns {null|string}
2007 * Returns the requested firewall zone name as published in the
2008 * `ubus` runtime information or `null` if the remote protocol
2009 * handler didn't request a zone.
2011 getZoneName: function() {
2012 var d = this._ubus('data');
2014 if (L.isObject(d)
&& typeof(d.zone) == 'string')
2021 * Query the first (primary) IPv4 address of the logical interface.
2023 * @returns {null|string}
2024 * Returns the primary IPv4 address registered by the protocol handler
2025 * or `null` if no IPv4 addresses were set.
2027 getIPAddr: function() {
2028 var addrs = this._ubus('ipv4-address');
2029 return ((Array.isArray(addrs)
&& addrs.length) ? addrs[
0].address : null);
2033 * Query all IPv4 addresses of the logical interface.
2035 * @returns {string[]}
2036 * Returns an array of IPv4 addresses in CIDR notation which have been
2037 * registered by the protocol handler. The order of the resulting array
2038 * follows the order of the addresses in `ubus` runtime information.
2040 getIPAddrs: function() {
2041 var addrs = this._ubus('ipv4-address'),
2044 if (Array.isArray(addrs))
2045 for (var i =
0; i
< addrs.length; i++)
2046 rv.push('%s/%d'.format(addrs[i].address, addrs[i].mask));
2052 * Query the first (primary) IPv4 netmask of the logical interface.
2054 * @returns {null|string}
2055 * Returns the netmask of the primary IPv4 address registered by the
2056 * protocol handler or `null` if no IPv4 addresses were set.
2058 getNetmask: function() {
2059 var addrs = this._ubus('ipv4-address');
2060 if (Array.isArray(addrs)
&& addrs.length)
2061 return prefixToMask(addrs[
0].mask, false);
2065 * Query the gateway (nexthop) of the default route associated with
2066 * this logical interface.
2069 * Returns a string containing the IPv4 nexthop address of the associated
2070 * default route or `null` if no default route was found.
2072 getGatewayAddr: function() {
2073 var routes = this._ubus('route');
2075 if (Array.isArray(routes))
2076 for (var i =
0; i
< routes.length; i++)
2077 if (typeof(routes[i]) == 'object'
&&
2078 routes[i].target == '
0.0.0.0'
&&
2079 routes[i].mask ==
0)
2080 return routes[i].nexthop;
2086 * Query the IPv4 DNS servers associated with the logical interface.
2088 * @returns {string[]}
2089 * Returns an array of IPv4 DNS servers registered by the remote
2092 getDNSAddrs: function() {
2093 var addrs = this._ubus('dns-server'),
2096 if (Array.isArray(addrs))
2097 for (var i =
0; i
< addrs.length; i++)
2098 if (!/:/.test(addrs[i]))
2105 * Query the first (primary) IPv6 address of the logical interface.
2107 * @returns {null|string}
2108 * Returns the primary IPv6 address registered by the protocol handler
2109 * in CIDR notation or `null` if no IPv6 addresses were set.
2111 getIP6Addr: function() {
2112 var addrs = this._ubus('ipv6-address');
2114 if (Array.isArray(addrs)
&& L.isObject(addrs[
0]))
2115 return '%s/%d'.format(addrs[
0].address, addrs[
0].mask);
2117 addrs = this._ubus('ipv6-prefix-assignment');
2119 if (Array.isArray(addrs)
&& L.isObject(addrs[
0])
&& L.isObject(addrs[
0]['local-address']))
2120 return '%s/%d'.format(addrs[
0]['local-address'].address, addrs[
0]['local-address'].mask);
2126 * Query all IPv6 addresses of the logical interface.
2128 * @returns {string[]}
2129 * Returns an array of IPv6 addresses in CIDR notation which have been
2130 * registered by the protocol handler. The order of the resulting array
2131 * follows the order of the addresses in `ubus` runtime information.
2133 getIP6Addrs: function() {
2134 var addrs = this._ubus('ipv6-address'),
2137 if (Array.isArray(addrs))
2138 for (var i =
0; i
< addrs.length; i++)
2139 if (L.isObject(addrs[i]))
2140 rv.push('%s/%d'.format(addrs[i].address, addrs[i].mask));
2142 addrs = this._ubus('ipv6-prefix-assignment');
2144 if (Array.isArray(addrs))
2145 for (var i =
0; i
< addrs.length; i++)
2146 if (L.isObject(addrs[i])
&& L.isObject(addrs[i]['local-address']))
2147 rv.push('%s/%d'.format(addrs[i]['local-address'].address, addrs[i]['local-address'].mask));
2153 * Query the gateway (nexthop) of the IPv6 default route associated with
2154 * this logical interface.
2157 * Returns a string containing the IPv6 nexthop address of the associated
2158 * default route or `null` if no default route was found.
2160 getGateway6Addr: function() {
2161 var routes = this._ubus('route');
2163 if (Array.isArray(routes))
2164 for (var i =
0; i
< routes.length; i++)
2165 if (typeof(routes[i]) == 'object'
&&
2166 routes[i].target == '::'
&&
2167 routes[i].mask ==
0)
2168 return routes[i].nexthop;
2174 * Query the IPv6 DNS servers associated with the logical interface.
2176 * @returns {string[]}
2177 * Returns an array of IPv6 DNS servers registered by the remote
2180 getDNS6Addrs: function() {
2181 var addrs = this._ubus('dns-server'),
2184 if (Array.isArray(addrs))
2185 for (var i =
0; i
< addrs.length; i++)
2186 if (/:/.test(addrs[i]))
2193 * Query the routed IPv6 prefix associated with the logical interface.
2195 * @returns {null|string}
2196 * Returns the routed IPv6 prefix registered by the remote protocol
2197 * handler or `null` if no prefix is present.
2199 getIP6Prefix: function() {
2200 var prefixes = this._ubus('ipv6-prefix');
2202 if (Array.isArray(prefixes)
&& L.isObject(prefixes[
0]))
2203 return '%s/%d'.format(prefixes[
0].address, prefixes[
0].mask);
2209 * Query interface error messages published in `ubus` runtime state.
2211 * Interface errors are emitted by remote protocol handlers if the setup
2212 * of the underlying logical interface failed, e.g. due to bad
2213 * configuration or network connectivity issues.
2215 * This function will translate the found error codes to human readable
2216 * messages using the descriptions registered by
2217 * {@link LuCI.Network#registerErrorCode Network.registerErrorCode()}
2218 * and fall back to `
"Unknown error (%s)"` where `%s` is replaced by the
2219 * error code in case no translation can be found.
2221 * @returns {string[]}
2222 * Returns an array of translated interface error messages.
2224 getErrors: function() {
2225 var errors = this._ubus('errors'),
2228 if (Array.isArray(errors)) {
2229 for (var i =
0; i
< errors.length; i++) {
2230 if (!L.isObject(errors[i]) || typeof(errors[i].code) != 'string')
2234 rv.push(proto_errors[errors[i].code] || _('Unknown error (%s)').format(errors[i].code));
2242 * Checks whether the underlying logical interface is declared as bridge.
2244 * @returns {boolean}
2245 * Returns `true` when the interface is declared with `option type bridge`
2246 * and when the associated protocol implementation is not marked virtual
2247 * or `false` when the logical interface is no bridge.
2249 isBridge: function() {
2250 return (!this.isVirtual()
&& this.getType() == 'bridge');
2254 * Get the name of the opkg package providing the protocol functionality.
2256 * This function should be overwritten by protocol specific subclasses.
2261 * Returns the name of the opkg package required for the protocol to
2262 * function, e.g. `odhcp6c` for the `dhcpv6` prototocol.
2264 getOpkgPackage: function() {
2269 * Checks whether the protocol functionality is installed.
2271 * This function exists for compatibility with old code, it always
2277 * @returns {boolean}
2278 * Returns `true` if the protocol support is installed, else `false`.
2280 isInstalled: function() {
2285 * Checks whether this protocol is
"virtual".
2287 * A
"virtual" protocol is a protocol which spawns its own interfaces
2288 * on demand instead of using existing physical interfaces.
2290 * Examples for virtual protocols are `
6in4` which `gre` spawn tunnel
2291 * network device on startup, examples for non-virtual protcols are
2292 * `dhcp` or `static` which apply IP configuration to existing interfaces.
2294 * This function should be overwritten by subclasses.
2296 * @returns {boolean}
2297 * Returns a boolean indicating whether the underlying protocol spawns
2298 * dynamic interfaces (`true`) or not (`false`).
2300 isVirtual: function() {
2305 * Checks whether this protocol is
"floating".
2307 * A
"floating" protocol is a protocol which spawns its own interfaces
2308 * on demand, like a virtual one but which relies on an existinf lower
2309 * level interface to initiate the connection.
2311 * An example for such a protocol is
"pppoe".
2313 * This function exists for backwards compatibility with older code
2314 * but should not be used anymore.
2317 * @returns {boolean}
2318 * Returns a boolean indicating whether this protocol is floating (`true`)
2321 isFloating: function() {
2326 * Checks whether this logical interface is dynamic.
2328 * A dynamic interface is an interface which has been created at runtime,
2329 * e.g. as sub-interface of another interface, but which is not backed by
2330 * any user configuration. Such dynamic interfaces cannot be edited but
2331 * only brought down or restarted.
2333 * @returns {boolean}
2334 * Returns a boolean indicating whether this interface is dynamic (`true`)
2337 isDynamic: function() {
2338 return (this._ubus('dynamic') == true);
2342 * Checks whether this interface is an alias interface.
2344 * Alias interfaces are interfaces layering on top of another interface
2345 * and are denoted by a special `@interfacename` notation in the
2346 * underlying `ifname` option.
2348 * @returns {null|string}
2349 * Returns the name of the parent interface if this logical interface
2350 * is an alias or `null` if it is not an alias interface.
2352 isAlias: function() {
2353 var ifnames = L.toArray(uci.get('network', this.sid, 'ifname')),
2356 for (var i =
0; i
< ifnames.length; i++)
2357 if (ifnames[i].charAt(
0) == '@')
2358 parent = ifnames[i].substr(
1);
2359 else if (parent != null)
2366 * Checks whether this logical interface is
"empty", meaning that ut
2367 * has no network devices attached.
2369 * @returns {boolean}
2370 * Returns `true` if this logical interface is empty, else `false`.
2372 isEmpty: function() {
2373 if (this.isFloating())
2377 ifname = this._get('ifname');
2379 if (ifname != null
&& ifname.match(/\S+/))
2382 if (empty == true
&& getWifiNetidBySid(this.sid) != null)
2389 * Checks whether this logical interface is configured and running.
2391 * @returns {boolean}
2392 * Returns `true` when the interface is active or `false` when it is not.
2395 return (this._ubus('up') == true);
2399 * Add the given network device to the logical interface.
2401 * @param {LuCI.Network.Protocol|LuCI.Network.Device|LuCI.Network.WifiDevice|LuCI.Network.WifiNetwork|string} device
2402 * The object or device name to add to the logical interface. In case the
2403 * given argument is not a string, it is resolved though the
2404 * {@link LuCI.Network#getIfnameOf Network.getIfnameOf()} function.
2406 * @returns {boolean}
2407 * Returns `true` if the device name has been added or `false` if any
2408 * argument was invalid, if the device was already part of the logical
2409 * interface or if the logical interface is virtual.
2411 addDevice: function(ifname) {
2412 ifname = ifnameOf(ifname);
2414 if (ifname == null || this.isFloating())
2417 var wif = getWifiSidByIfname(ifname);
2420 return appendValue('wireless', wif, 'network', this.sid);
2422 return appendValue('network', this.sid, 'ifname', ifname);
2426 * Remove the given network device from the logical interface.
2428 * @param {LuCI.Network.Protocol|LuCI.Network.Device|LuCI.Network.WifiDevice|LuCI.Network.WifiNetwork|string} device
2429 * The object or device name to remove from the logical interface. In case
2430 * the given argument is not a string, it is resolved though the
2431 * {@link LuCI.Network#getIfnameOf Network.getIfnameOf()} function.
2433 * @returns {boolean}
2434 * Returns `true` if the device name has been added or `false` if any
2435 * argument was invalid, if the device was already part of the logical
2436 * interface or if the logical interface is virtual.
2438 deleteDevice: function(ifname) {
2441 ifname = ifnameOf(ifname);
2443 if (ifname == null || this.isFloating())
2446 var wif = getWifiSidByIfname(ifname);
2449 rv = removeValue('wireless', wif, 'network', this.sid);
2451 if (removeValue('network', this.sid, 'ifname', ifname))
2458 * Returns the Linux network device associated with this logical
2461 * @returns {LuCI.Network.Device}
2462 * Returns a `Network.Device` class instance representing the
2463 * expected Linux network device according to the configuration.
2465 getDevice: function() {
2466 if (this.isVirtual()) {
2467 var ifname = '%s-%s'.format(this.getProtocol(), this.sid);
2468 _state.isTunnel[this.getProtocol() + '-' + this.sid] = true;
2469 return L.network.instantiateDevice(ifname, this);
2471 else if (this.isBridge()) {
2472 var ifname = 'br-%s'.format(this.sid);
2473 _state.isBridge[ifname] = true;
2474 return new Device(ifname, this);
2477 var ifnames = L.toArray(uci.get('network', this.sid, 'ifname'));
2479 for (var i =
0; i
< ifnames.length; i++) {
2480 var m = ifnames[i].match(/^([^:/]+)/);
2481 return ((m
&& m[
1]) ? L.network.instantiateDevice(m[
1], this) : null);
2484 ifname = getWifiNetidByNetname(this.sid);
2486 return (ifname != null ? L.network.instantiateDevice(ifname[
0], this) : null);
2491 * Returns the layer
2 linux network device currently associated
2492 * with this logical interface.
2494 * @returns {LuCI.Network.Device}
2495 * Returns a `Network.Device` class instance representing the Linux
2496 * network device currently associated with the logical interface.
2498 getL2Device: function() {
2499 var ifname = this._ubus('device');
2500 return (ifname != null ? L.network.instantiateDevice(ifname, this) : null);
2504 * Returns the layer
3 linux network device currently associated
2505 * with this logical interface.
2507 * @returns {LuCI.Network.Device}
2508 * Returns a `Network.Device` class instance representing the Linux
2509 * network device currently associated with the logical interface.
2511 getL3Device: function() {
2512 var ifname = this._ubus('l3_device');
2513 return (ifname != null ? L.network.instantiateDevice(ifname, this) : null);
2517 * Returns a list of network sub-devices associated with this logical
2520 * @returns {null|Array
<LuCI.Network.Device
>}
2521 * Returns an array of of `Network.Device` class instances representing
2522 * the sub-devices attached to this logical interface or `null` if the
2523 * logical interface does not support sub-devices, e.g. because it is
2524 * virtual and not a bridge.
2526 getDevices: function() {
2529 if (!this.isBridge()
&& !(this.isVirtual()
&& !this.isFloating()))
2532 var ifnames = L.toArray(uci.get('network', this.sid, 'ifname'));
2534 for (var i =
0; i
< ifnames.length; i++) {
2535 if (ifnames[i].charAt(
0) == '@')
2538 var m = ifnames[i].match(/^([^:/]+)/);
2540 rv.push(L.network.instantiateDevice(m[
1], this));
2543 var uciWifiIfaces = uci.sections('wireless', 'wifi-iface');
2545 for (var i =
0; i
< uciWifiIfaces.length; i++) {
2546 if (typeof(uciWifiIfaces[i].device) != 'string')
2549 var networks = L.toArray(uciWifiIfaces[i].network);
2551 for (var j =
0; j
< networks.length; j++) {
2552 if (networks[j] != this.sid)
2555 var netid = getWifiNetidBySid(uciWifiIfaces[i]['.name']);
2558 rv.push(L.network.instantiateDevice(netid[
0], this));
2562 rv.sort(deviceSort);
2568 * Checks whether this logical interface contains the given device
2571 * @param {LuCI.Network.Protocol|LuCI.Network.Device|LuCI.Network.WifiDevice|LuCI.Network.WifiNetwork|string} device
2572 * The object or device name to check. In case the given argument is not
2573 * a string, it is resolved though the
2574 * {@link LuCI.Network#getIfnameOf Network.getIfnameOf()} function.
2576 * @returns {boolean}
2577 * Returns `true` when this logical interface contains the given network
2578 * device or `false` if not.
2580 containsDevice: function(ifname) {
2581 ifname = ifnameOf(ifname);
2585 else if (this.isVirtual()
&& '%s-%s'.format(this.getProtocol(), this.sid) == ifname)
2587 else if (this.isBridge()
&& 'br-%s'.format(this.sid) == ifname)
2590 var ifnames = L.toArray(uci.get('network', this.sid, 'ifname'));
2592 for (var i =
0; i
< ifnames.length; i++) {
2593 var m = ifnames[i].match(/^([^:/]+)/);
2594 if (m != null
&& m[
1] == ifname)
2598 var wif = getWifiSidByIfname(ifname);
2601 var networks = L.toArray(uci.get('wireless', wif, 'network'));
2603 for (var i =
0; i
< networks.length; i++)
2604 if (networks[i] == this.sid)
2614 * @memberof LuCI.Network
2618 * A `Network.Device` class instance represents an underlying Linux network
2619 * device and allows querying device details such as packet statistics or MTU.
2621 Device = L.Class.extend(/** @lends LuCI.Network.Device.prototype */ {
2622 __init__: function(ifname, network) {
2623 var wif = getWifiSidByIfname(ifname);
2626 var res = getWifiStateBySid(wif) || [],
2627 netid = getWifiNetidBySid(wif) || [];
2629 this.wif = new WifiNetwork(wif, res[
0], res[
1], netid[
0], res[
2], { ifname: ifname });
2630 this.ifname = this.wif.getIfname();
2633 this.ifname = this.ifname || ifname;
2634 this.dev = _state.netdevs[this.ifname];
2635 this.network = network;
2638 _devstate: function(/* ... */) {
2641 for (var i =
0; i
< arguments.length; i++)
2643 rv = rv[arguments[i]];
2651 * Get the name of the network device.
2654 * Returns the name of the device, e.g. `eth0` or `wlan0`.
2656 getName: function() {
2657 return (this.wif != null ? this.wif.getIfname() : this.ifname);
2661 * Get the MAC address of the device.
2663 * @returns {null|string}
2664 * Returns the MAC address of the device or `null` if not applicable,
2665 * e.g. for non-ethernet tunnel devices.
2667 getMAC: function() {
2668 var mac = this._devstate('macaddr');
2669 return mac ? mac.toUpperCase() : null;
2673 * Get the MTU of the device.
2676 * Returns the MTU of the device.
2678 getMTU: function() {
2679 return this._devstate('mtu');
2683 * Get the IPv4 addresses configured on the device.
2685 * @returns {string[]}
2686 * Returns an array of IPv4 address strings.
2688 getIPAddrs: function() {
2689 var addrs = this._devstate('ipaddrs');
2690 return (Array.isArray(addrs) ? addrs : []);
2694 * Get the IPv6 addresses configured on the device.
2696 * @returns {string[]}
2697 * Returns an array of IPv6 address strings.
2699 getIP6Addrs: function() {
2700 var addrs = this._devstate('ip6addrs');
2701 return (Array.isArray(addrs) ? addrs : []);
2705 * Get the type of the device..
2708 * Returns a string describing the type of the network device:
2709 * - `alias` if it is an abstract alias device (`@` notation)
2710 * - `wifi` if it is a wireless interface (e.g. `wlan0`)
2711 * - `bridge` if it is a bridge device (e.g. `br-lan`)
2712 * - `tunnel` if it is a tun or tap device (e.g. `tun0`)
2713 * - `vlan` if it is a vlan device (e.g. `eth0.1`)
2714 * - `switch` if it is a switch device (e.g.`eth1` connected to switch0)
2715 * - `ethernet` for all other device types
2717 getType: function() {
2718 if (this.ifname != null
&& this.ifname.charAt(
0) == '@')
2720 else if (this.wif != null || isWifiIfname(this.ifname))
2722 else if (_state.isBridge[this.ifname])
2724 else if (_state.isTunnel[this.ifname])
2726 else if (this.ifname.indexOf('.')
> -
1)
2728 else if (_state.isSwitch[this.ifname])
2735 * Get a short description string for the device.
2738 * Returns the device name for non-wifi devices or a string containing
2739 * the operation mode and SSID for wifi devices.
2741 getShortName: function() {
2742 if (this.wif != null)
2743 return this.wif.getShortName();
2749 * Get a long description string for the device.
2752 * Returns a string containing the type description and device name
2753 * for non-wifi devices or operation mode and ssid for wifi ones.
2755 getI18n: function() {
2756 if (this.wif != null) {
2757 return '%s: %s
"%s"'.format(
2758 _('Wireless Network'),
2759 this.wif.getActiveMode(),
2760 this.wif.getActiveSSID() || this.wif.getActiveBSSID() || this.wif.getID() || '?');
2763 return '%s:
"%s"'.format(this.getTypeI18n(), this.getName());
2767 * Get a string describing the device type.
2770 * Returns a string describing the type, e.g.
"Wireless Adapter" or
2773 getTypeI18n: function() {
2774 switch (this.getType()) {
2776 return _('Alias Interface');
2779 return _('Wireless Adapter');
2785 return _('Ethernet Switch');
2788 return (_state.isSwitch[this.ifname] ? _('Switch VLAN') : _('Software VLAN'));
2791 return _('Tunnel Interface');
2794 return _('Ethernet Adapter');
2799 * Get the associated bridge ports of the device.
2801 * @returns {null|Array
<LuCI.Network.Device
>}
2802 * Returns an array of `Network.Device` instances representing the ports
2803 * (slave interfaces) of the bridge or `null` when this device isn't
2806 getPorts: function() {
2807 var br = _state.bridges[this.ifname],
2810 if (br == null || !Array.isArray(br.ifnames))
2813 for (var i =
0; i
< br.ifnames.length; i++)
2814 rv.push(L.network.instantiateDevice(br.ifnames[i].name));
2816 rv.sort(deviceSort);
2824 * @returns {null|string}
2825 * Returns the ID of this network bridge or `null` if this network
2826 * device is not a Linux bridge.
2828 getBridgeID: function() {
2829 var br = _state.bridges[this.ifname];
2830 return (br != null ? br.id : null);
2834 * Get the bridge STP setting
2836 * @returns {boolean}
2837 * Returns `true` when this device is a Linux bridge and has `stp`
2838 * enabled, else `false`.
2840 getBridgeSTP: function() {
2841 var br = _state.bridges[this.ifname];
2842 return (br != null ? !!br.stp : false);
2846 * Checks whether this device is up.
2848 * @returns {boolean}
2849 * Returns `true` when the associated device is running pr `false`
2850 * when it is down or absent.
2853 var up = this._devstate('flags', 'up');
2856 up = (this.getType() == 'alias');
2862 * Checks whether this device is a Linux bridge.
2864 * @returns {boolean}
2865 * Returns `true` when the network device is present and a Linux bridge,
2868 isBridge: function() {
2869 return (this.getType() == 'bridge');
2873 * Checks whether this device is part of a Linux bridge.
2875 * @returns {boolean}
2876 * Returns `true` when this network device is part of a bridge,
2879 isBridgePort: function() {
2880 return (this._devstate('bridge') != null);
2884 * Get the amount of transmitted bytes.
2887 * Returns the amount of bytes transmitted by the network device.
2889 getTXBytes: function() {
2890 var stat = this._devstate('stats');
2891 return (stat != null ? stat.tx_bytes ||
0 :
0);
2895 * Get the amount of received bytes.
2898 * Returns the amount of bytes received by the network device.
2900 getRXBytes: function() {
2901 var stat = this._devstate('stats');
2902 return (stat != null ? stat.rx_bytes ||
0 :
0);
2906 * Get the amount of transmitted packets.
2909 * Returns the amount of packets transmitted by the network device.
2911 getTXPackets: function() {
2912 var stat = this._devstate('stats');
2913 return (stat != null ? stat.tx_packets ||
0 :
0);
2917 * Get the amount of received packets.
2920 * Returns the amount of packets received by the network device.
2922 getRXPackets: function() {
2923 var stat = this._devstate('stats');
2924 return (stat != null ? stat.rx_packets ||
0 :
0);
2928 * Get the primary logical interface this device is assigned to.
2930 * @returns {null|LuCI.Network.Protocol}
2931 * Returns a `Network.Protocol` instance representing the logical
2932 * interface this device is attached to or `null` if it is not
2933 * assigned to any logical interface.
2935 getNetwork: function() {
2936 return this.getNetworks()[
0];
2940 * Get the logical interfaces this device is assigned to.
2942 * @returns {Array
<LuCI.Network.Protocol
>}
2943 * Returns an array of `Network.Protocol` instances representing the
2944 * logical interfaces this device is assigned to.
2946 getNetworks: function() {
2947 if (this.networks == null) {
2950 var networks = enumerateNetworks.apply(L.network);
2952 for (var i =
0; i
< networks.length; i++)
2953 if (networks[i].containsDevice(this.ifname) || networks[i].getIfname() == this.ifname)
2954 this.networks.push(networks[i]);
2956 this.networks.sort(networkSort);
2959 return this.networks;
2963 * Get the related wireless network this device is related to.
2965 * @returns {null|LuCI.Network.WifiNetwork}
2966 * Returns a `Network.WifiNetwork` instance representing the wireless
2967 * network corresponding to this network device or `null` if this device
2968 * is not a wireless device.
2970 getWifiNetwork: function() {
2971 return (this.wif != null ? this.wif : null);
2977 * @memberof LuCI.Network
2981 * A `Network.WifiDevice` class instance represents a wireless radio device
2982 * present on the system and provides wireless capability information as
2983 * well as methods for enumerating related wireless networks.
2985 WifiDevice = L.Class.extend(/** @lends LuCI.Network.WifiDevice.prototype */ {
2986 __init__: function(name, radiostate) {
2987 var uciWifiDevice = uci.get('wireless', name);
2989 if (uciWifiDevice != null
&&
2990 uciWifiDevice['.type'] == 'wifi-device'
&&
2991 uciWifiDevice['.name'] != null) {
2992 this.sid = uciWifiDevice['.name'];
2995 this.sid = this.sid || name;
3003 ubus: function(/* ... */) {
3004 var v = this._ubusdata;
3006 for (var i =
0; i
< arguments.length; i++)
3008 v = v[arguments[i]];
3016 * Read the given UCI option value of this wireless device.
3018 * @param {string} opt
3019 * The UCI option name to read.
3021 * @returns {null|string|string[]}
3022 * Returns the UCI option value or `null` if the requested option is
3025 get: function(opt) {
3026 return uci.get('wireless', this.sid, opt);
3030 * Set the given UCI option of this network to the given value.
3032 * @param {string} opt
3033 * The name of the UCI option to set.
3035 * @param {null|string|string[]} val
3036 * The value to set or `null` to remove the given option from the
3039 set: function(opt, value) {
3040 return uci.set('wireless', this.sid, opt, value);
3044 * Checks whether this wireless radio is disabled.
3046 * @returns {boolean}
3047 * Returns `true` when the wireless radio is marked as disabled in `ubus`
3048 * runtime state or when the `disabled` option is set in the corresponding
3049 * UCI configuration.
3051 isDisabled: function() {
3052 return this.ubus('dev', 'disabled') || this.get('disabled') == '
1';
3056 * Get the configuration name of this wireless radio.
3059 * Returns the UCI section name (e.g. `radio0`) of the corresponding
3060 * radio configuration which also serves as unique logical identifier
3061 * for the wireless phy.
3063 getName: function() {
3068 * Gets a list of supported hwmodes.
3070 * The hwmode values describe the frequency band and wireless standard
3071 * versions supported by the wireless phy.
3073 * @returns {string[]}
3074 * Returns an array of valid hwmode values for this radio. Currently
3075 * known mode values are:
3076 * - `a` - Legacy
802.11a mode,
5 GHz, up to
54 Mbit/s
3077 * - `b` - Legacy
802.11b mode,
2.4 GHz, up to
11 Mbit/s
3078 * - `g` - Legacy
802.11g mode,
2.4 GHz, up to
54 Mbit/s
3079 * - `n` - IEEE
802.11n mode,
2.4 or
5 GHz, up to
600 Mbit/s
3080 * - `ac` - IEEE
802.11ac mode,
5 GHz, up to
6770 Mbit/s
3082 getHWModes: function() {
3083 var hwmodes = this.ubus('dev', 'iwinfo', 'hwmodes');
3084 return Array.isArray(hwmodes) ? hwmodes : [ 'b', 'g' ];
3088 * Gets a list of supported htmodes.
3090 * The htmode values describe the wide-frequency options supported by
3093 * @returns {string[]}
3094 * Returns an array of valid htmode values for this radio. Currently
3095 * known mode values are:
3096 * - `HT20` - applicable to IEEE
802.11n,
20 MHz wide channels
3097 * - `HT40` - applicable to IEEE
802.11n,
40 MHz wide channels
3098 * - `VHT20` - applicable to IEEE
802.11ac,
20 MHz wide channels
3099 * - `VHT40` - applicable to IEEE
802.11ac,
40 MHz wide channels
3100 * - `VHT80` - applicable to IEEE
802.11ac,
80 MHz wide channels
3101 * - `VHT160` - applicable to IEEE
802.11ac,
160 MHz wide channels
3103 getHTModes: function() {
3104 var htmodes = this.ubus('dev', 'iwinfo', 'htmodes');
3105 return (Array.isArray(htmodes)
&& htmodes.length) ? htmodes : null;
3109 * Get a string describing the wireless radio hardware.
3112 * Returns the description string.
3114 getI18n: function() {
3115 var hw = this.ubus('dev', 'iwinfo', 'hardware'),
3116 type = L.isObject(hw) ? hw.name : null;
3118 if (this.ubus('dev', 'iwinfo', 'type') == 'wl')
3121 var hwmodes = this.getHWModes(),
3124 hwmodes.sort(function(a, b) {
3125 return (a.length != b.length ? a.length
> b.length : a
> b);
3128 modestr = hwmodes.join('');
3130 return '%s
802.11%s Wireless Controller (%s)'.format(type || 'Generic', modestr, this.getName());
3134 * A wireless scan result object describes a neighbouring wireless
3135 * network found in the vincinity.
3137 * @typedef {Object
<string, number|string|LuCI.Network.WifiEncryption
>} WifiScanResult
3138 * @memberof LuCI.Network
3140 * @property {string} ssid
3141 * The SSID / Mesh ID of the network.
3143 * @property {string} bssid
3144 * The BSSID if the network.
3146 * @property {string} mode
3147 * The operation mode of the network (`Master`, `Ad-Hoc`, `Mesh Point`).
3149 * @property {number} channel
3150 * The wireless channel of the network.
3152 * @property {number} signal
3153 * The received signal strength of the network in dBm.
3155 * @property {number} quality
3156 * The numeric quality level of the signal, can be used in conjunction
3157 * with `quality_max` to calculate a quality percentage.
3159 * @property {number} quality_max
3160 * The maximum possible quality level of the signal, can be used in
3161 * conjunction with `quality` to calculate a quality percentage.
3163 * @property {LuCI.Network.WifiEncryption} encryption
3164 * The encryption used by the wireless network.
3168 * Trigger a wireless scan on this radio device and obtain a list of
3171 * @returns {Promise
<Array
<LuCI.Network.WifiScanResult
>>}
3172 * Returns a promise resolving to an array of scan result objects
3173 * describing the networks found in the vincinity.
3175 getScanList: function() {
3176 return callIwinfoScan(this.sid);
3180 * Check whether the wireless radio is marked as up in the `ubus`
3183 * @returns {boolean}
3184 * Returns `true` when the radio device is up, else `false`.
3187 if (L.isObject(_state.radios[this.sid]))
3188 return (_state.radios[this.sid].up == true);
3194 * Get the wifi network of the given name belonging to this radio device
3196 * @param {string} network
3197 * The name of the wireless network to lookup. This may be either an uci
3198 * configuration section ID, a network ID in the form `radio#.network#`
3199 * or a Linux network device name like `wlan0` which is resolved to the
3200 * corresponding configuration section through `ubus` runtime information.
3202 * @returns {Promise
<LuCI.Network.WifiNetwork
>}
3203 * Returns a promise resolving to a `Network.WifiNetwork` instance
3204 * representing the wireless network and rejecting with `null` if
3205 * the given network could not be found or is not associated with
3206 * this radio device.
3208 getWifiNetwork: function(network) {
3209 return L.network.getWifiNetwork(network).then(L.bind(function(networkInstance) {
3210 var uciWifiIface = (networkInstance.sid ? uci.get('wireless', networkInstance.sid) : null);
3212 if (uciWifiIface == null || uciWifiIface['.type'] != 'wifi-iface' || uciWifiIface.device != this.sid)
3213 return Promise.reject();
3215 return networkInstance;
3220 * Get all wireless networks associated with this wireless radio device.
3222 * @returns {Promise
<Array
<LuCI.Network.WifiNetwork
>>}
3223 * Returns a promise resolving to an array of `Network.WifiNetwork`
3224 * instances respresenting the wireless networks associated with this
3227 getWifiNetworks: function() {
3228 var uciWifiIfaces = uci.sections('wireless', 'wifi-iface'),
3231 for (var i =
0; i
< uciWifiIfaces.length; i++)
3232 if (uciWifiIfaces[i].device == this.sid)
3233 tasks.push(L.network.getWifiNetwork(uciWifiIfaces[i]['.name']));
3235 return Promise.all(tasks);
3239 * Adds a new wireless network associated with this radio device to the
3240 * configuration and sets its options to the provided values.
3242 * @param {Object
<string, string|string[]
>} [options]
3243 * The options to set for the newly added wireless network.
3245 * @returns {Promise
<null|LuCI.Network.WifiNetwork
>}
3246 * Returns a promise resolving to a `WifiNetwork` instance describing
3247 * the newly added wireless network or `null` if the given options
3250 addWifiNetwork: function(options) {
3251 if (!L.isObject(options))
3254 options.device = this.sid;
3256 return L.network.addWifiNetwork(options);
3260 * Deletes the wireless network with the given name associated with this
3263 * @param {string} network
3264 * The name of the wireless network to lookup. This may be either an uci
3265 * configuration section ID, a network ID in the form `radio#.network#`
3266 * or a Linux network device name like `wlan0` which is resolved to the
3267 * corresponding configuration section through `ubus` runtime information.
3269 * @returns {Promise
<boolean
>}
3270 * Returns a promise resolving to `true` when the wireless network was
3271 * successfully deleted from the configuration or `false` when the given
3272 * network could not be found or if the found network was not associated
3273 * with this wireless radio device.
3275 deleteWifiNetwork: function(network) {
3278 if (network instanceof WifiNetwork) {
3282 var uciWifiIface = uci.get('wireless', network);
3284 if (uciWifiIface == null || uciWifiIface['.type'] != 'wifi-iface')
3285 sid = getWifiSidByIfname(network);
3288 if (sid == null || uci.get('wireless', sid, 'device') != this.sid)
3289 return Promise.resolve(false);
3291 uci.delete('wireless', network);
3293 return Promise.resolve(true);
3299 * @memberof LuCI.Network
3303 * A `Network.WifiNetwork` instance represents a wireless network (vif)
3304 * configured on top of a radio device and provides functions for querying
3305 * the runtime state of the network. Most radio devices support multiple
3306 * such networks in parallel.
3308 WifiNetwork = L.Class.extend(/** @lends LuCI.Network.WifiNetwork.prototype */ {
3309 __init__: function(sid, radioname, radiostate, netid, netstate) {
3319 ubus: function(/* ... */) {
3320 var v = this._ubusdata;
3322 for (var i =
0; i
< arguments.length; i++)
3324 v = v[arguments[i]];
3332 * Read the given UCI option value of this wireless network.
3334 * @param {string} opt
3335 * The UCI option name to read.
3337 * @returns {null|string|string[]}
3338 * Returns the UCI option value or `null` if the requested option is
3341 get: function(opt) {
3342 return uci.get('wireless', this.sid, opt);
3346 * Set the given UCI option of this network to the given value.
3348 * @param {string} opt
3349 * The name of the UCI option to set.
3351 * @param {null|string|string[]} val
3352 * The value to set or `null` to remove the given option from the
3355 set: function(opt, value) {
3356 return uci.set('wireless', this.sid, opt, value);
3360 * Checks whether this wireless network is disabled.
3362 * @returns {boolean}
3363 * Returns `true` when the wireless radio is marked as disabled in `ubus`
3364 * runtime state or when the `disabled` option is set in the corresponding
3365 * UCI configuration.
3367 isDisabled: function() {
3368 return this.ubus('dev', 'disabled') || this.get('disabled') == '
1';
3372 * Get the configured operation mode of the wireless network.
3375 * Returns the configured operation mode. Possible values are:
3376 * - `ap` - Master (Access Point) mode
3377 * - `sta` - Station (client) mode
3378 * - `adhoc` - Ad-Hoc (IBSS) mode
3379 * - `mesh` - Mesh (IEEE
802.11s) mode
3380 * - `monitor` - Monitor mode
3382 getMode: function() {
3383 return this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3387 * Get the configured SSID of the wireless network.
3389 * @returns {null|string}
3390 * Returns the configured SSID value or `null` when this network is
3393 getSSID: function() {
3394 if (this.getMode() == 'mesh')
3397 return this.ubus('net', 'config', 'ssid') || this.get('ssid');
3401 * Get the configured Mesh ID of the wireless network.
3403 * @returns {null|string}
3404 * Returns the configured mesh ID value or `null` when this network
3405 * is not in mesh mode.
3407 getMeshID: function() {
3408 if (this.getMode() != 'mesh')
3411 return this.ubus('net', 'config', 'mesh_id') || this.get('mesh_id');
3415 * Get the configured BSSID of the wireless network.
3417 * @returns {null|string}
3418 * Returns the BSSID value or `null` if none has been specified.
3420 getBSSID: function() {
3421 return this.ubus('net', 'config', 'bssid') || this.get('bssid');
3425 * Get the names of the logical interfaces this wireless network is
3428 * @returns {string[]}
3429 * Returns an array of logical interface names.
3431 getNetworkNames: function() {
3432 return L.toArray(this.ubus('net', 'config', 'network') || this.get('network'));
3436 * Get the internal network ID of this wireless network.
3438 * The network ID is a LuCI specific identifer in the form
3439 * `radio#.network#` to identify wireless networks by their corresponding
3440 * radio and network index numbers.
3443 * Returns the LuCI specific network ID.
3450 * Get the configuration ID of this wireless network.
3453 * Returns the corresponding UCI section ID of the network.
3455 getName: function() {
3460 * Get the Linux network device name.
3462 * @returns {null|string}
3463 * Returns the current Linux network device name as resolved from
3464 * `ubus` runtime information or `null` if this network has no
3465 * associated network device, e.g. when not configured or up.
3467 getIfname: function() {
3468 var ifname = this.ubus('net', 'ifname') || this.ubus('net', 'iwinfo', 'ifname');
3470 if (ifname == null || ifname.match(/^(wifi|radio)\d/))
3471 ifname = this.netid;
3477 * Get the name of the corresponding wifi radio device.
3479 * @returns {null|string}
3480 * Returns the name of the radio device this network is configured on
3481 * or `null` if it cannot be determined.
3483 getWifiDeviceName: function() {
3484 return this.ubus('radio') || this.get('device');
3488 * Get the corresponding wifi radio device.
3490 * @returns {null|LuCI.Network.WifiDevice}
3491 * Returns a `Network.WifiDevice` instance representing the corresponding
3492 * wifi radio device or `null` if the related radio device could not be
3495 getWifiDevice: function() {
3496 var radioname = this.getWifiDeviceName();
3498 if (radioname == null)
3499 return Promise.reject();
3501 return L.network.getWifiDevice(radioname);
3505 * Check whether the radio network is up.
3507 * This function actually queries the up state of the related radio
3508 * device and assumes this network to be up as well when the parent
3509 * radio is up. This is due to the fact that OpenWrt does not control
3510 * virtual interfaces individually but within one common hostapd
3513 * @returns {boolean}
3514 * Returns `true` when the network is up, else `false`.
3517 var device = this.getDevice();
3522 return device.isUp();
3526 * Query the current operation mode from runtime information.
3529 * Returns the human readable mode name as reported by `ubus` runtime
3530 * state. Possible returned values are:
3542 getActiveMode: function() {
3543 var mode = this.ubus('net', 'iwinfo', 'mode') || this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3546 case 'ap': return 'Master';
3547 case 'sta': return 'Client';
3548 case 'adhoc': return 'Ad-Hoc';
3549 case 'mesh': return 'Mesh';
3550 case 'monitor': return 'Monitor';
3551 default: return mode;
3556 * Query the current operation mode from runtime information as
3557 * translated string.
3560 * Returns the translated, human readable mode name as reported by
3561 *`ubus` runtime state.
3563 getActiveModeI18n: function() {
3564 var mode = this.getActiveMode();
3567 case 'Master': return _('Master');
3568 case 'Client': return _('Client');
3569 case 'Ad-Hoc': return _('Ad-Hoc');
3570 case 'Mash': return _('Mesh');
3571 case 'Monitor': return _('Monitor');
3572 default: return mode;
3577 * Query the current SSID from runtime information.
3580 * Returns the current SSID or Mesh ID as reported by `ubus` runtime
3583 getActiveSSID: function() {
3584 return this.ubus('net', 'iwinfo', 'ssid') || this.ubus('net', 'config', 'ssid') || this.get('ssid');
3588 * Query the current BSSID from runtime information.
3591 * Returns the current BSSID or Mesh ID as reported by `ubus` runtime
3594 getActiveBSSID: function() {
3595 return this.ubus('net', 'iwinfo', 'bssid') || this.ubus('net', 'config', 'bssid') || this.get('bssid');
3599 * Query the current encryption settings from runtime information.
3602 * Returns a string describing the current encryption or `-` if the the
3603 * encryption state could not be found in `ubus` runtime information.
3605 getActiveEncryption: function() {
3606 return formatWifiEncryption(this.ubus('net', 'iwinfo', 'encryption')) || '-';
3610 * A wireless peer entry describes the properties of a remote wireless
3611 * peer associated with a local network.
3613 * @typedef {Object
<string, boolean|number|string|LuCI.Network.WifiRateEntry
>} WifiPeerEntry
3614 * @memberof LuCI.Network
3616 * @property {string} mac
3617 * The MAC address (BSSID).
3619 * @property {number} signal
3620 * The received signal strength.
3622 * @property {number} [signal_avg]
3623 * The average signal strength if supported by the driver.
3625 * @property {number} [noise]
3626 * The current noise floor of the radio. May be `
0` or absent if not
3627 * supported by the driver.
3629 * @property {number} inactive
3630 * The amount of milliseconds the peer has been inactive, e.g. due
3633 * @property {number} connected_time
3634 * The amount of milliseconds the peer is associated to this network.
3636 * @property {number} [thr]
3637 * The estimated throughput of the peer, May be `
0` or absent if not
3638 * supported by the driver.
3640 * @property {boolean} authorized
3641 * Specifies whether the peer is authorized to associate to this network.
3643 * @property {boolean} authenticated
3644 * Specifies whether the peer completed authentication to this network.
3646 * @property {string} preamble
3647 * The preamble mode used by the peer. May be `long` or `short`.
3649 * @property {boolean} wme
3650 * Specifies whether the peer supports WME/WMM capabilities.
3652 * @property {boolean} mfp
3653 * Specifies whether management frame protection is active.
3655 * @property {boolean} tdls
3656 * Specifies whether TDLS is active.
3658 * @property {number} [mesh llid]
3659 * The mesh LLID, may be `
0` or absent if not applicable or supported
3662 * @property {number} [mesh plid]
3663 * The mesh PLID, may be `
0` or absent if not applicable or supported
3666 * @property {string} [mesh plink]
3667 * The mesh peer link state description, may be an empty string (`''`)
3668 * or absent if not applicable or supported by the driver.
3670 * The following states are known:
3680 * @property {number} [mesh local PS]
3681 * The local powersafe mode for the peer link, may be an empty
3682 * string (`''`) or absent if not applicable or supported by
3685 * The following modes are known:
3686 * - `ACTIVE` (no power save)
3691 * @property {number} [mesh peer PS]
3692 * The remote powersafe mode for the peer link, may be an empty
3693 * string (`''`) or absent if not applicable or supported by
3696 * The following modes are known:
3697 * - `ACTIVE` (no power save)
3702 * @property {number} [mesh non-peer PS]
3703 * The powersafe mode for all non-peer neigbours, may be an empty
3704 * string (`''`) or absent if not applicable or supported by the driver.
3706 * The following modes are known:
3707 * - `ACTIVE` (no power save)
3712 * @property {LuCI.Network.WifiRateEntry} rx
3713 * Describes the receiving wireless rate from the peer.
3715 * @property {LuCI.Network.WifiRateEntry} tx
3716 * Describes the transmitting wireless rate to the peer.
3720 * A wireless rate entry describes the properties of a wireless
3721 * transmission rate to or from a peer.
3723 * @typedef {Object
<string, boolean|number
>} WifiRateEntry
3724 * @memberof LuCI.Network
3726 * @property {number} [drop_misc]
3727 * The amount of received misc. packages that have been dropped, e.g.
3728 * due to corruption or missing authentication. Only applicable to
3731 * @property {number} packets
3732 * The amount of packets that have been received or sent.
3734 * @property {number} bytes
3735 * The amount of bytes that have been received or sent.
3737 * @property {number} [failed]
3738 * The amount of failed tranmission attempts. Only applicable to
3741 * @property {number} [retries]
3742 * The amount of retried transmissions. Only applicable to transmit
3745 * @property {boolean} is_ht
3746 * Specifies whether this rate is an HT (IEEE
802.11n) rate.
3748 * @property {boolean} is_vht
3749 * Specifies whether this rate is an VHT (IEEE
802.11ac) rate.
3751 * @property {number} mhz
3752 * The channel width in MHz used for the transmission.
3754 * @property {number} rate
3755 * The bitrate in bit/s of the transmission.
3757 * @property {number} [mcs]
3758 * The MCS index of the used transmission rate. Only applicable to
3761 * @property {number} [
40mhz]
3762 * Specifies whether the tranmission rate used
40MHz wide channel.
3763 * Only applicable to HT or VHT rates.
3765 * Note: this option exists for backwards compatibility only and its
3766 * use is discouraged. The `mhz` field should be used instead to
3767 * determine the channel width.
3769 * @property {boolean} [short_gi]
3770 * Specifies whether a short guard interval is used for the transmission.
3771 * Only applicable to HT or VHT rates.
3773 * @property {number} [nss]
3774 * Specifies the number of spatial streams used by the transmission.
3775 * Only applicable to VHT rates.
3779 * Fetch the list of associated peers.
3781 * @returns {Promise
<Array
<LuCI.Network.WifiPeerEntry
>>}
3782 * Returns a promise resolving to an array of wireless peers associated
3783 * with this network.
3785 getAssocList: function() {
3786 return callIwinfoAssoclist(this.getIfname());
3790 * Query the current operating frequency of the wireless network.
3792 * @returns {null|string}
3793 * Returns the current operating frequency of the network from `ubus`
3794 * runtime information in GHz or `null` if the information is not
3797 getFrequency: function() {
3798 var freq = this.ubus('net', 'iwinfo', 'frequency');
3800 if (freq != null
&& freq
> 0)
3801 return '%
.03f'.format(freq /
1000);
3807 * Query the current average bitrate of all peers associated to this
3810 * @returns {null|number}
3811 * Returns the average bit rate among all peers associated to the network
3812 * as reported by `ubus` runtime information or `null` if the information
3815 getBitRate: function() {
3816 var rate = this.ubus('net', 'iwinfo', 'bitrate');
3818 if (rate != null
&& rate
> 0)
3819 return (rate /
1000);
3825 * Query the current wireless channel.
3827 * @returns {null|number}
3828 * Returns the wireless channel as reported by `ubus` runtime information
3829 * or `null` if it cannot be determined.
3831 getChannel: function() {
3832 return this.ubus('net', 'iwinfo', 'channel') || this.ubus('dev', 'config', 'channel') || this.get('channel');
3836 * Query the current wireless signal.
3838 * @returns {null|number}
3839 * Returns the wireless signal in dBm as reported by `ubus` runtime
3840 * information or `null` if it cannot be determined.
3842 getSignal: function() {
3843 return this.ubus('net', 'iwinfo', 'signal') ||
0;
3847 * Query the current radio noise floor.
3850 * Returns the radio noise floor in dBm as reported by `ubus` runtime
3851 * information or `
0` if it cannot be determined.
3853 getNoise: function() {
3854 return this.ubus('net', 'iwinfo', 'noise') ||
0;
3858 * Query the current country code.
3861 * Returns the wireless country code as reported by `ubus` runtime
3862 * information or `
00` if it cannot be determined.
3864 getCountryCode: function() {
3865 return this.ubus('net', 'iwinfo', 'country') || this.ubus('dev', 'config', 'country') || '
00';
3869 * Query the current radio TX power.
3871 * @returns {null|number}
3872 * Returns the wireless network transmit power in dBm as reported by
3873 * `ubus` runtime information or `null` if it cannot be determined.
3875 getTXPower: function() {
3876 return this.ubus('net', 'iwinfo', 'txpower');
3880 * Query the radio TX power offset.
3882 * Some wireless radios have a fixed power offset, e.g. due to the
3883 * use of external amplifiers.
3886 * Returns the wireless network transmit power offset in dBm as reported
3887 * by `ubus` runtime information or `
0` if there is no offset, or if it
3888 * cannot be determined.
3890 getTXPowerOffset: function() {
3891 return this.ubus('net', 'iwinfo', 'txpower_offset') ||
0;
3895 * Calculate the current signal.
3899 * Returns the calculated signal level, which is the difference between
3900 * noise and signal (SNR), divided by
5.
3902 getSignalLevel: function(signal, noise) {
3903 if (this.getActiveBSSID() == '
00:
00:
00:
00:
00:
00')
3906 signal = signal || this.getSignal();
3907 noise = noise || this.getNoise();
3909 if (signal
< 0 && noise
< 0) {
3910 var snr = -
1 * (noise - signal);
3911 return Math.floor(snr /
5);
3918 * Calculate the current signal quality percentage.
3921 * Returns the calculated signal quality in percent. The value is
3922 * calculated from the `quality` and `quality_max` indicators reported
3923 * by `ubus` runtime state.
3925 getSignalPercent: function() {
3926 var qc = this.ubus('net', 'iwinfo', 'quality') ||
0,
3927 qm = this.ubus('net', 'iwinfo', 'quality_max') ||
0;
3929 if (qc
> 0 && qm
> 0)
3930 return Math.floor((
100 / qm) * qc);
3936 * Get a short description string for this wireless network.
3939 * Returns a string describing this network, consisting of the
3940 * active operation mode, followed by either the SSID, BSSID or
3941 * internal network ID, depending on which information is available.
3943 getShortName: function() {
3944 return '%s
"%s"'.format(
3945 this.getActiveModeI18n(),
3946 this.getActiveSSID() || this.getActiveBSSID() || this.getID());
3950 * Get a description string for this wireless network.
3953 * Returns a string describing this network, consisting of the
3954 * term `Wireless Network`, followed by the active operation mode,
3955 * the SSID, BSSID or internal network ID and the Linux network device
3956 * name, depending on which information is available.
3958 getI18n: function() {
3959 return '%s: %s
"%s" (%s)'.format(
3960 _('Wireless Network'),
3961 this.getActiveModeI18n(),
3962 this.getActiveSSID() || this.getActiveBSSID() || this.getID(),
3967 * Get the primary logical interface this wireless network is attached to.
3969 * @returns {null|LuCI.Network.Protocol}
3970 * Returns a `Network.Protocol` instance representing the logical
3971 * interface or `null` if this network is not attached to any logical
3974 getNetwork: function() {
3975 return this.getNetworks()[
0];
3979 * Get the logical interfaces this wireless network is attached to.
3981 * @returns {Array
<LuCI.Network.Protocol
>}
3982 * Returns an array of `Network.Protocol` instances representing the
3983 * logical interfaces this wireless network is attached to.
3985 getNetworks: function() {
3986 var networkNames = this.getNetworkNames(),
3989 for (var i =
0; i
< networkNames.length; i++) {
3990 var uciInterface = uci.get('network', networkNames[i]);
3992 if (uciInterface == null || uciInterface['.type'] != 'interface')
3995 networks.push(L.network.instantiateNetwork(networkNames[i]));
3998 networks.sort(networkSort);
4004 * Get the associated Linux network device.
4006 * @returns {LuCI.Network.Device}
4007 * Returns a `Network.Device` instance representing the Linux network
4008 * device associted with this wireless network.
4010 getDevice: function() {
4011 return L.network.instantiateDevice(this.getIfname());
4026 <h2><a href=
"index.html">Home
</a></h2><h3>Classes
</h3><ul><li><a href=
"LuCI.html">LuCI
</a></li><li><a href=
"LuCI.Class.html">Class
</a></li><li><a href=
"LuCI.dom.html">dom
</a></li><li><a href=
"LuCI.fs.html">fs
</a></li><li><a href=
"LuCI.Headers.html">Headers
</a></li><li><a href=
"LuCI.Network.html">Network
</a></li><li><a href=
"LuCI.Network.Device.html">Device
</a></li><li><a href=
"LuCI.Network.Hosts.html">Hosts
</a></li><li><a href=
"LuCI.Network.Protocol.html">Protocol
</a></li><li><a href=
"LuCI.Network.WifiDevice.html">WifiDevice
</a></li><li><a href=
"LuCI.Network.WifiNetwork.html">WifiNetwork
</a></li><li><a href=
"LuCI.Poll.html">Poll
</a></li><li><a href=
"LuCI.Request.html">Request
</a></li><li><a href=
"LuCI.Request.poll.html">poll
</a></li><li><a href=
"LuCI.Response.html">Response
</a></li><li><a href=
"LuCI.rpc.html">rpc
</a></li><li><a href=
"LuCI.uci.html">uci
</a></li><li><a href=
"LuCI.view.html">view
</a></li><li><a href=
"LuCI.XHR.html">XHR
</a></li></ul>
4032 Documentation generated by
<a href=
"https://github.com/jsdoc/jsdoc">JSDoc
3.6.3</a> on Tue Nov
05 2019 09:
33:
05 GMT+
0100 (Central European Standard Time)
4035 <script> prettyPrint();
</script>
4036 <script src=
"scripts/linenumber.js"> </script>