9 CONNECT_FAILED
: _('Connection attempt failed'),
10 INVALID_ADDRESS
: _('IP address is invalid'),
11 INVALID_GATEWAY
: _('Gateway address is invalid'),
12 INVALID_LOCAL_ADDRESS
: _('Local IP address is invalid'),
13 MISSING_ADDRESS
: _('IP address is missing'),
14 MISSING_PEER_ADDRESS
: _('Peer address is missing'),
15 NO_DEVICE
: _('Network device is not present'),
16 NO_IFACE
: _('Unable to determine device name'),
17 NO_IFNAME
: _('Unable to determine device name'),
18 NO_WAN_ADDRESS
: _('Unable to determine external IP address'),
19 NO_WAN_LINK
: _('Unable to determine upstream interface'),
20 PEER_RESOLVE_FAIL
: _('Unable to resolve peer host name'),
21 PIN_FAILED
: _('PIN code rejected')
24 var iface_patterns_ignore
= [
40 var iface_patterns_wireless
= [
47 var iface_patterns_virtual
= [ ];
49 var callLuciNetworkDevices
= rpc
.declare({
51 method
: 'getNetworkDevices',
55 var callLuciWirelessDevices
= rpc
.declare({
57 method
: 'getWirelessDevices',
61 var callLuciBoardJSON
= rpc
.declare({
63 method
: 'getBoardJSON'
66 var callLuciHostHints
= rpc
.declare({
68 method
: 'getHostHints',
72 var callIwinfoAssoclist
= rpc
.declare({
75 params
: [ 'device', 'mac' ],
76 expect
: { results
: [] }
79 var callIwinfoScan
= rpc
.declare({
84 expect
: { results
: [] }
87 var callNetworkInterfaceDump
= rpc
.declare({
88 object
: 'network.interface',
90 expect
: { 'interface': [] }
93 var callNetworkProtoHandlers
= rpc
.declare({
95 method
: 'get_proto_handlers',
104 function getProtocolHandlers(cache
) {
105 return callNetworkProtoHandlers().then(function(protos
) {
106 /* Register "none" protocol */
107 if (!protos
.hasOwnProperty('none'))
108 Object
.assign(protos
, { none
: { no_device
: false } });
110 /* Hack: emulate relayd protocol */
111 if (!protos
.hasOwnProperty('relay') && L
.hasSystemFeature('relayd'))
112 Object
.assign(protos
, { relay
: { no_device
: true } });
114 Object
.assign(_protospecs
, protos
);
116 return Promise
.all(Object
.keys(protos
).map(function(p
) {
117 return Promise
.resolve(L
.require('protocol.%s'.format(p
))).catch(function(err
) {
118 if (L
.isObject(err
) && err
.name
!= 'NetworkError')
121 })).then(function() {
124 }).catch(function() {
129 function getWifiStateBySid(sid
) {
130 var s
= uci
.get('wireless', sid
);
132 if (s
!= null && s
['.type'] == 'wifi-iface') {
133 for (var radioname
in _state
.radios
) {
134 for (var i
= 0; i
< _state
.radios
[radioname
].interfaces
.length
; i
++) {
135 var netstate
= _state
.radios
[radioname
].interfaces
[i
];
137 if (typeof(netstate
.section
) != 'string')
140 var s2
= uci
.get('wireless', netstate
.section
);
142 if (s2
!= null && s
['.type'] == s2
['.type'] && s
['.name'] == s2
['.name']) {
143 if (s2
['.anonymous'] == false && netstate
.section
.charAt(0) == '@')
146 return [ radioname
, _state
.radios
[radioname
], netstate
];
155 function getWifiStateByIfname(ifname
) {
156 for (var radioname
in _state
.radios
) {
157 for (var i
= 0; i
< _state
.radios
[radioname
].interfaces
.length
; i
++) {
158 var netstate
= _state
.radios
[radioname
].interfaces
[i
];
160 if (typeof(netstate
.ifname
) != 'string')
163 if (netstate
.ifname
== ifname
)
164 return [ radioname
, _state
.radios
[radioname
], netstate
];
171 function isWifiIfname(ifname
) {
172 for (var i
= 0; i
< iface_patterns_wireless
.length
; i
++)
173 if (iface_patterns_wireless
[i
].test(ifname
))
179 function getWifiSidByNetid(netid
) {
180 var m
= /^(\w+)\.network(\d+)$/.exec(netid
);
182 var sections
= uci
.sections('wireless', 'wifi-iface');
183 for (var i
= 0, n
= 0; i
< sections
.length
; i
++) {
184 if (sections
[i
].device
!= m
[1])
188 return sections
[i
]['.name'];
195 function getWifiSidByIfname(ifname
) {
196 var sid
= getWifiSidByNetid(ifname
);
201 var res
= getWifiStateByIfname(ifname
);
203 if (res
!= null && L
.isObject(res
[2]) && typeof(res
[2].section
) == 'string')
204 return res
[2].section
;
209 function getWifiNetidBySid(sid
) {
210 var s
= uci
.get('wireless', sid
);
211 if (s
!= null && s
['.type'] == 'wifi-iface') {
212 var radioname
= s
.device
;
213 if (typeof(s
.device
) == 'string') {
214 var i
= 0, netid
= null, sections
= uci
.sections('wireless', 'wifi-iface');
215 for (var i
= 0, n
= 0; i
< sections
.length
; i
++) {
216 if (sections
[i
].device
!= s
.device
)
221 if (sections
[i
]['.name'] != s
['.name'])
224 return [ '%s.network%d'.format(s
.device
, n
), s
.device
];
233 function getWifiNetidByNetname(name
) {
234 var sections
= uci
.sections('wireless', 'wifi-iface');
235 for (var i
= 0; i
< sections
.length
; i
++) {
236 if (typeof(sections
[i
].network
) != 'string')
239 var nets
= sections
[i
].network
.split(/\s+/);
240 for (var j
= 0; j
< nets
.length
; j
++) {
244 return getWifiNetidBySid(sections
[i
]['.name']);
251 function isVirtualIfname(ifname
) {
252 for (var i
= 0; i
< iface_patterns_virtual
.length
; i
++)
253 if (iface_patterns_virtual
[i
].test(ifname
))
259 function isIgnoredIfname(ifname
) {
260 for (var i
= 0; i
< iface_patterns_ignore
.length
; i
++)
261 if (iface_patterns_ignore
[i
].test(ifname
))
267 function appendValue(config
, section
, option
, value
) {
268 var values
= uci
.get(config
, section
, option
),
269 isArray
= Array
.isArray(values
),
272 if (isArray
== false)
273 values
= L
.toArray(values
);
275 if (values
.indexOf(value
) == -1) {
280 uci
.set(config
, section
, option
, isArray
? values
: values
.join(' '));
285 function removeValue(config
, section
, option
, value
) {
286 var values
= uci
.get(config
, section
, option
),
287 isArray
= Array
.isArray(values
),
290 if (isArray
== false)
291 values
= L
.toArray(values
);
293 for (var i
= values
.length
- 1; i
>= 0; i
--) {
294 if (values
[i
] == value
) {
300 if (values
.length
> 0)
301 uci
.set(config
, section
, option
, isArray
? values
: values
.join(' '));
303 uci
.unset(config
, section
, option
);
308 function prefixToMask(bits
, v6
) {
309 var w
= v6
? 128 : 32,
315 for (var i
= 0; i
< w
/ 16; i
++) {
316 var b
= Math
.min(16, bits
);
317 m
.push((0xffff << (16 - b
)) & 0xffff);
322 return String
.prototype.format
.apply('%x:%x:%x:%x:%x:%x:%x:%x', m
).replace(/:0(?::0)+$/, '::');
324 return '%d.%d.%d.%d'.format(m
[0] >>> 8, m
[0] & 0xff, m
[1] >>> 8, m
[1] & 0xff);
327 function maskToPrefix(mask
, v6
) {
328 var m
= v6
? validation
.parseIPv6(mask
) : validation
.parseIPv4(mask
);
335 for (var i
= 0, z
= false; i
< m
.length
; i
++) {
338 while (!z
&& (m
[i
] & (v6
? 0x8000 : 0x80))) {
339 m
[i
] = (m
[i
] << 1) & (v6
? 0xffff : 0xff);
350 function initNetworkState(refresh
) {
351 if (_state
== null || refresh
) {
352 _init
= _init
|| Promise
.all([
353 L
.resolveDefault(callNetworkInterfaceDump(), []),
354 L
.resolveDefault(callLuciBoardJSON(), {}),
355 L
.resolveDefault(callLuciNetworkDevices(), {}),
356 L
.resolveDefault(callLuciWirelessDevices(), {}),
357 L
.resolveDefault(callLuciHostHints(), {}),
358 getProtocolHandlers(),
359 L
.resolveDefault(uci
.load('network')),
360 L
.resolveDefault(uci
.load('wireless')),
361 L
.resolveDefault(uci
.load('luci'))
362 ]).then(function(data
) {
363 var netifd_ifaces
= data
[0],
364 board_json
= data
[1],
368 isTunnel
: {}, isBridge
: {}, isSwitch
: {}, isWifi
: {},
369 ifaces
: netifd_ifaces
, radios
: data
[3], hosts
: data
[4],
370 netdevs
: {}, bridges
: {}, switches
: {}, hostapd
: {}
373 for (var name
in luci_devs
) {
374 var dev
= luci_devs
[name
];
376 if (isVirtualIfname(name
))
377 s
.isTunnel
[name
] = true;
379 if (!s
.isTunnel
[name
] && isIgnoredIfname(name
))
382 s
.netdevs
[name
] = s
.netdevs
[name
] || {
390 devtype
: dev
.devtype
,
393 wireless
: dev
.wireless
,
399 if (Array
.isArray(dev
.ipaddrs
))
400 for (var i
= 0; i
< dev
.ipaddrs
.length
; i
++)
401 s
.netdevs
[name
].ipaddrs
.push(dev
.ipaddrs
[i
].address
+ '/' + dev
.ipaddrs
[i
].netmask
);
403 if (Array
.isArray(dev
.ip6addrs
))
404 for (var i
= 0; i
< dev
.ip6addrs
.length
; i
++)
405 s
.netdevs
[name
].ip6addrs
.push(dev
.ip6addrs
[i
].address
+ '/' + dev
.ip6addrs
[i
].netmask
);
408 for (var name
in luci_devs
) {
409 var dev
= luci_devs
[name
];
421 for (var i
= 0; dev
.ports
&& i
< dev
.ports
.length
; i
++) {
422 var subdev
= s
.netdevs
[dev
.ports
[i
]];
427 b
.ifnames
.push(subdev
);
432 s
.isBridge
[name
] = true;
435 for (var name
in luci_devs
) {
436 var dev
= luci_devs
[name
];
438 if (!dev
.parent
|| dev
.devtype
!= 'dsa')
441 s
.isSwitch
[dev
.parent
] = true;
442 s
.isSwitch
[name
] = true;
445 if (L
.isObject(board_json
.switch)) {
446 for (var switchname
in board_json
.switch) {
447 var layout
= board_json
.switch[switchname
],
454 if (L
.isObject(layout
) && Array
.isArray(layout
.ports
)) {
455 for (var i
= 0, port
; (port
= layout
.ports
[i
]) != null; i
++) {
456 if (typeof(port
) == 'object' && typeof(port
.num
) == 'number' &&
457 (typeof(port
.role
) == 'string' || typeof(port
.device
) == 'string')) {
460 role
: port
.role
|| 'cpu',
461 index
: (port
.index
!= null) ? port
.index
: port
.num
464 if (port
.device
!= null) {
465 spec
.device
= port
.device
;
466 spec
.tagged
= spec
.need_tag
;
467 netdevs
[port
.num
] = port
.device
;
472 if (port
.role
!= null)
473 nports
[port
.role
] = (nports
[port
.role
] || 0) + 1;
477 ports
.sort(function(a
, b
) {
478 if (a
.role
!= b
.role
)
479 return (a
.role
< b
.role
) ? -1 : 1;
481 return (a
.index
- b
.index
);
484 for (var i
= 0, port
; (port
= ports
[i
]) != null; i
++) {
485 if (port
.role
!= role
) {
491 port
.label
= 'CPU (%s)'.format(port
.device
);
492 else if (nports
[role
] > 1)
493 port
.label
= '%s %d'.format(role
.toUpperCase(), pnum
++);
495 port
.label
= role
.toUpperCase();
501 s
.switches
[switchname
] = {
509 if (L
.isObject(board_json
.dsl
) && L
.isObject(board_json
.dsl
.modem
)) {
510 s
.hasDSLModem
= board_json
.dsl
.modem
;
517 if (L
.isObject(s
.radios
))
518 for (var radio
in s
.radios
)
519 if (L
.isObject(s
.radios
[radio
]) && Array
.isArray(s
.radios
[radio
].interfaces
))
520 for (var i
= 0; i
< s
.radios
[radio
].interfaces
.length
; i
++)
521 if (L
.isObject(s
.radios
[radio
].interfaces
[i
]) && s
.radios
[radio
].interfaces
[i
].ifname
)
522 objects
.push('hostapd.%s'.format(s
.radios
[radio
].interfaces
[i
].ifname
));
524 return (objects
.length
? L
.resolveDefault(rpc
.list
.apply(rpc
, objects
), {}) : Promise
.resolve({})).then(function(res
) {
526 var m
= k
.match(/^hostapd\.(.+)$/);
528 s
.hostapd
[m
[1]] = res
[k
];
536 return (_state
!= null ? Promise
.resolve(_state
) : _init
);
539 function ifnameOf(obj
) {
540 if (obj
instanceof Protocol
)
541 return obj
.getIfname();
542 else if (obj
instanceof Device
)
543 return obj
.getName();
544 else if (obj
instanceof WifiDevice
)
545 return obj
.getName();
546 else if (obj
instanceof WifiNetwork
)
547 return obj
.getIfname();
548 else if (typeof(obj
) == 'string')
549 return obj
.replace(/:.+$/, '');
554 function networkSort(a
, b
) {
555 return a
.getName() > b
.getName();
558 function deviceSort(a
, b
) {
559 var typeWeigth
= { wifi
: 2, alias
: 3 },
560 weightA
= typeWeigth
[a
.getType()] || 1,
561 weightB
= typeWeigth
[b
.getType()] || 1;
563 if (weightA
!= weightB
)
564 return weightA
- weightB
;
566 return a
.getName() > b
.getName();
569 function formatWifiEncryption(enc
) {
570 if (!L
.isObject(enc
))
576 var ciphers
= Array
.isArray(enc
.ciphers
)
577 ? enc
.ciphers
.map(function(c
) { return c
.toUpperCase() }) : [ 'NONE' ];
579 if (Array
.isArray(enc
.wep
)) {
580 var has_open
= false,
583 for (var i
= 0; i
< enc
.wep
.length
; i
++)
584 if (enc
.wep
[i
] == 'open')
586 else if (enc
.wep
[i
] == 'shared')
589 if (has_open
&& has_shared
)
590 return 'WEP Open/Shared (%s)'.format(ciphers
.join(', '));
592 return 'WEP Open System (%s)'.format(ciphers
.join(', '));
594 return 'WEP Shared Auth (%s)'.format(ciphers
.join(', '));
599 if (Array
.isArray(enc
.wpa
)) {
601 suites
= Array
.isArray(enc
.authentication
)
602 ? enc
.authentication
.map(function(a
) { return a
.toUpperCase() }) : [ 'NONE' ];
604 for (var i
= 0; i
< enc
.wpa
.length
; i
++)
605 switch (enc
.wpa
[i
]) {
607 versions
.push('WPA');
611 versions
.push('WPA%d'.format(enc
.wpa
[i
]));
615 if (versions
.length
> 1)
616 return 'mixed %s %s (%s)'.format(versions
.join('/'), suites
.join(', '), ciphers
.join(', '));
618 return '%s %s (%s)'.format(versions
[0], suites
.join(', '), ciphers
.join(', '));
624 function enumerateNetworks() {
625 var uciInterfaces
= uci
.sections('network', 'interface'),
628 for (var i
= 0; i
< uciInterfaces
.length
; i
++)
629 networks
[uciInterfaces
[i
]['.name']] = this.instantiateNetwork(uciInterfaces
[i
]['.name']);
631 for (var i
= 0; i
< _state
.ifaces
.length
; i
++)
632 if (networks
[_state
.ifaces
[i
].interface] == null)
633 networks
[_state
.ifaces
[i
].interface] =
634 this.instantiateNetwork(_state
.ifaces
[i
].interface, _state
.ifaces
[i
].proto
);
638 for (var network
in networks
)
639 if (networks
.hasOwnProperty(network
))
640 rv
.push(networks
[network
]);
642 rv
.sort(networkSort
);
648 var Hosts
, Network
, Protocol
, Device
, WifiDevice
, WifiNetwork
;
656 * The `LuCI.network` class combines data from multiple `ubus` apis to
657 * provide an abstraction of the current network configuration state.
659 * It provides methods to enumerate interfaces and devices, to query
660 * current configuration details and to manipulate settings.
662 Network
= baseclass
.extend(/** @lends LuCI.network.prototype */ {
664 * Converts the given prefix size in bits to a netmask.
668 * @param {number} bits
669 * The prefix size in bits.
671 * @param {boolean} [v6=false]
672 * Whether to convert the bits value into an IPv4 netmask (`false`) or
673 * an IPv6 netmask (`true`).
675 * @returns {null|string}
676 * Returns a string containing the netmask corresponding to the bit count
677 * or `null` when the given amount of bits exceeds the maximum possible
678 * value of `32` for IPv4 or `128` for IPv6.
680 prefixToMask
: prefixToMask
,
683 * Converts the given netmask to a prefix size in bits.
687 * @param {string} netmask
688 * The netmask to convert into a bit count.
690 * @param {boolean} [v6=false]
691 * Whether to parse the given netmask as IPv4 (`false`) or IPv6 (`true`)
694 * @returns {null|number}
695 * Returns the number of prefix bits contained in the netmask or `null`
696 * if the given netmask value was invalid.
698 maskToPrefix
: maskToPrefix
,
701 * An encryption entry describes active wireless encryption settings
702 * such as the used key management protocols, active ciphers and
705 * @typedef {Object<string, boolean|Array<number|string>>} LuCI.network.WifiEncryption
706 * @memberof LuCI.network
708 * @property {boolean} enabled
709 * Specifies whether any kind of encryption, such as `WEP` or `WPA` is
710 * enabled. If set to `false`, then no encryption is active and the
711 * corresponding network is open.
713 * @property {string[]} [wep]
714 * When the `wep` property exists, the network uses WEP encryption.
715 * In this case, the property is set to an array of active WEP modes
716 * which might be either `open`, `shared` or both.
718 * @property {number[]} [wpa]
719 * When the `wpa` property exists, the network uses WPA security.
720 * In this case, the property is set to an array containing the WPA
721 * protocol versions used, e.g. `[ 1, 2 ]` for WPA/WPA2 mixed mode or
722 * `[ 3 ]` for WPA3-SAE.
724 * @property {string[]} [authentication]
725 * The `authentication` property only applies to WPA encryption and
726 * is defined when the `wpa` property is set as well. It points to
727 * an array of active authentication suites used by the network, e.g.
728 * `[ "psk" ]` for a WPA(2)-PSK network or `[ "psk", "sae" ]` for
729 * mixed WPA2-PSK/WPA3-SAE encryption.
731 * @property {string[]} [ciphers]
732 * If either WEP or WPA encryption is active, then the `ciphers`
733 * property will be set to an array describing the active encryption
734 * ciphers used by the network, e.g. `[ "tkip", "ccmp" ]` for a
735 * WPA/WPA2-PSK mixed network or `[ "wep-40", "wep-104" ]` for an
740 * Converts a given {@link LuCI.network.WifiEncryption encryption entry}
741 * into a human readable string such as `mixed WPA/WPA2 PSK (TKIP, CCMP)`
742 * or `WPA3 SAE (CCMP)`.
746 * @param {LuCI.network.WifiEncryption} encryption
747 * The wireless encryption entry to convert.
749 * @returns {null|string}
750 * Returns the description string for the given encryption entry or
751 * `null` if the given entry was invalid.
753 formatWifiEncryption
: formatWifiEncryption
,
756 * Flushes the local network state cache and fetches updated information
757 * from the remote `ubus` apis.
759 * @returns {Promise<Object>}
760 * Returns a promise resolving to the internal network state object.
762 flushCache: function() {
763 initNetworkState(true);
768 * Instantiates the given {@link LuCI.network.Protocol Protocol} backend,
769 * optionally using the given network name.
771 * @param {string} protoname
772 * The protocol backend to use, e.g. `static` or `dhcp`.
774 * @param {string} [netname=__dummy__]
775 * The network name to use for the instantiated protocol. This should be
776 * usually set to one of the interfaces described in /etc/config/network
777 * but it is allowed to omit it, e.g. to query protocol capabilities
778 * without the need for an existing interface.
780 * @returns {null|LuCI.network.Protocol}
781 * Returns the instantiated protocol backend class or `null` if the given
782 * protocol isn't known.
784 getProtocol: function(protoname
, netname
) {
785 var v
= _protocols
[protoname
];
787 return new v(netname
|| '__dummy__');
793 * Obtains instances of all known {@link LuCI.network.Protocol Protocol}
796 * @returns {Array<LuCI.network.Protocol>}
797 * Returns an array of protocol class instances.
799 getProtocols: function() {
802 for (var protoname
in _protocols
)
803 rv
.push(new _protocols
[protoname
]('__dummy__'));
809 * Registers a new {@link LuCI.network.Protocol Protocol} subclass
810 * with the given methods and returns the resulting subclass value.
812 * This functions internally calls
813 * {@link LuCI.Class.extend Class.extend()} on the `Network.Protocol`
816 * @param {string} protoname
817 * The name of the new protocol to register.
819 * @param {Object<string, *>} methods
820 * The member methods and values of the new `Protocol` subclass to
821 * be passed to {@link LuCI.Class.extend Class.extend()}.
823 * @returns {LuCI.network.Protocol}
824 * Returns the new `Protocol` subclass.
826 registerProtocol: function(protoname
, methods
) {
827 var spec
= L
.isObject(_protospecs
) ? _protospecs
[protoname
] : null;
828 var proto
= Protocol
.extend(Object
.assign({
829 getI18n: function() {
833 isFloating: function() {
837 isVirtual: function() {
838 return (L
.isObject(spec
) && spec
.no_device
== true);
841 renderFormOptions: function(section
) {
845 __init__: function(name
) {
849 getProtocol: function() {
854 _protocols
[protoname
] = proto
;
860 * Registers a new regular expression pattern to recognize
861 * virtual interfaces.
863 * @param {RegExp} pat
864 * A `RegExp` instance to match a virtual interface name
865 * such as `6in4-wan` or `tun0`.
867 registerPatternVirtual: function(pat
) {
868 iface_patterns_virtual
.push(pat
);
872 * Registers a new human readable translation string for a `Protocol`
875 * @param {string} code
876 * The `ubus` protocol error code to register a translation for, e.g.
879 * @param {string} message
880 * The message to use as translation for the given protocol error code.
883 * Returns `true` if the error code description has been added or `false`
884 * if either the arguments were invalid or if there already was a
885 * description for the given code.
887 registerErrorCode: function(code
, message
) {
888 if (typeof(code
) == 'string' &&
889 typeof(message
) == 'string' &&
890 !proto_errors
.hasOwnProperty(code
)) {
891 proto_errors
[code
] = message
;
899 * Adds a new network of the given name and update it with the given
902 * If a network with the given name already exist but is empty, then
903 * this function will update its option, otherwise it will do nothing.
905 * @param {string} name
906 * The name of the network to add. Must be in the format `[a-zA-Z0-9_]+`.
908 * @param {Object<string, string|string[]>} [options]
909 * An object of uci option values to set on the new network or to
910 * update in an existing, empty network.
912 * @returns {Promise<null|LuCI.network.Protocol>}
913 * Returns a promise resolving to the `Protocol` subclass instance
914 * describing the added network or resolving to `null` if the name
915 * was invalid or if a non-empty network of the given name already
918 addNetwork: function(name
, options
) {
919 return this.getNetwork(name
).then(L
.bind(function(existingNetwork
) {
920 if (name
!= null && /^[a-zA-Z0-9_]+$/.test(name
) && existingNetwork
== null) {
921 var sid
= uci
.add('network', 'interface', name
);
924 if (L
.isObject(options
))
925 for (var key
in options
)
926 if (options
.hasOwnProperty(key
))
927 uci
.set('network', sid
, key
, options
[key
]);
929 return this.instantiateNetwork(sid
);
932 else if (existingNetwork
!= null && existingNetwork
.isEmpty()) {
933 if (L
.isObject(options
))
934 for (var key
in options
)
935 if (options
.hasOwnProperty(key
))
936 existingNetwork
.set(key
, options
[key
]);
938 return existingNetwork
;
944 * Get a {@link LuCI.network.Protocol Protocol} instance describing
945 * the network with the given name.
947 * @param {string} name
948 * The logical interface name of the network get, e.g. `lan` or `wan`.
950 * @returns {Promise<null|LuCI.network.Protocol>}
951 * Returns a promise resolving to a
952 * {@link LuCI.network.Protocol Protocol} subclass instance describing
953 * the network or `null` if the network did not exist.
955 getNetwork: function(name
) {
956 return initNetworkState().then(L
.bind(function() {
957 var section
= (name
!= null) ? uci
.get('network', name
) : null;
959 if (section
!= null && section
['.type'] == 'interface') {
960 return this.instantiateNetwork(name
);
962 else if (name
!= null) {
963 for (var i
= 0; i
< _state
.ifaces
.length
; i
++)
964 if (_state
.ifaces
[i
].interface == name
)
965 return this.instantiateNetwork(name
, _state
.ifaces
[i
].proto
);
973 * Gets an array containing all known networks.
975 * @returns {Promise<Array<LuCI.network.Protocol>>}
976 * Returns a promise resolving to a name-sorted array of
977 * {@link LuCI.network.Protocol Protocol} subclass instances
978 * describing all known networks.
980 getNetworks: function() {
981 return initNetworkState().then(L
.bind(enumerateNetworks
, this));
985 * Deletes the given network and its references from the network and
986 * firewall configuration.
988 * @param {string} name
989 * The name of the network to delete.
991 * @returns {Promise<boolean>}
992 * Returns a promise resolving to either `true` if the network and
993 * references to it were successfully deleted from the configuration or
994 * `false` if the given network could not be found.
996 deleteNetwork: function(name
) {
997 var requireFirewall
= Promise
.resolve(L
.require('firewall')).catch(function() {}),
998 network
= this.instantiateNetwork(name
);
1000 return Promise
.all([ requireFirewall
, initNetworkState() ]).then(function(res
) {
1001 var uciInterface
= uci
.get('network', name
),
1004 if (uciInterface
!= null && uciInterface
['.type'] == 'interface') {
1005 return Promise
.resolve(network
? network
.deleteConfiguration() : null).then(function() {
1006 uci
.remove('network', name
);
1008 uci
.sections('luci', 'ifstate', function(s
) {
1009 if (s
.interface == name
)
1010 uci
.remove('luci', s
['.name']);
1013 uci
.sections('network', 'alias', function(s
) {
1014 if (s
.interface == name
)
1015 uci
.remove('network', s
['.name']);
1018 uci
.sections('network', 'route', function(s
) {
1019 if (s
.interface == name
)
1020 uci
.remove('network', s
['.name']);
1023 uci
.sections('network', 'route6', function(s
) {
1024 if (s
.interface == name
)
1025 uci
.remove('network', s
['.name']);
1028 uci
.sections('wireless', 'wifi-iface', function(s
) {
1029 var networks
= L
.toArray(s
.network
).filter(function(network
) { return network
!= name
});
1031 if (networks
.length
> 0)
1032 uci
.set('wireless', s
['.name'], 'network', networks
.join(' '));
1034 uci
.unset('wireless', s
['.name'], 'network');
1038 return firewall
.deleteNetwork(name
).then(function() { return true });
1041 }).catch(function() {
1051 * Rename the given network and its references to a new name.
1053 * @param {string} oldName
1054 * The current name of the network.
1056 * @param {string} newName
1057 * The name to rename the network to, must be in the format
1060 * @returns {Promise<boolean>}
1061 * Returns a promise resolving to either `true` if the network was
1062 * successfully renamed or `false` if the new name was invalid, if
1063 * a network with the new name already exists or if the network to
1064 * rename could not be found.
1066 renameNetwork: function(oldName
, newName
) {
1067 return initNetworkState().then(function() {
1068 if (newName
== null || !/^[a-zA-Z0-9_]+$/.test(newName
) || uci
.get('network', newName
) != null)
1071 var oldNetwork
= uci
.get('network', oldName
);
1073 if (oldNetwork
== null || oldNetwork
['.type'] != 'interface')
1076 var sid
= uci
.add('network', 'interface', newName
);
1078 for (var key
in oldNetwork
)
1079 if (oldNetwork
.hasOwnProperty(key
) && key
.charAt(0) != '.')
1080 uci
.set('network', sid
, key
, oldNetwork
[key
]);
1082 uci
.sections('luci', 'ifstate', function(s
) {
1083 if (s
.interface == oldName
)
1084 uci
.set('luci', s
['.name'], 'interface', newName
);
1087 uci
.sections('network', 'alias', function(s
) {
1088 if (s
.interface == oldName
)
1089 uci
.set('network', s
['.name'], 'interface', newName
);
1092 uci
.sections('network', 'route', function(s
) {
1093 if (s
.interface == oldName
)
1094 uci
.set('network', s
['.name'], 'interface', newName
);
1097 uci
.sections('network', 'route6', function(s
) {
1098 if (s
.interface == oldName
)
1099 uci
.set('network', s
['.name'], 'interface', newName
);
1102 uci
.sections('wireless', 'wifi-iface', function(s
) {
1103 var networks
= L
.toArray(s
.network
).map(function(network
) { return (network
== oldName
? newName
: network
) });
1105 if (networks
.length
> 0)
1106 uci
.set('wireless', s
['.name'], 'network', networks
.join(' '));
1109 uci
.remove('network', oldName
);
1116 * Get a {@link LuCI.network.Device Device} instance describing the
1117 * given network device.
1119 * @param {string} name
1120 * The name of the network device to get, e.g. `eth0` or `br-lan`.
1122 * @returns {Promise<null|LuCI.network.Device>}
1123 * Returns a promise resolving to the `Device` instance describing
1124 * the network device or `null` if the given device name could not
1127 getDevice: function(name
) {
1128 return initNetworkState().then(L
.bind(function() {
1132 if (_state
.netdevs
.hasOwnProperty(name
) || isWifiIfname(name
))
1133 return this.instantiateDevice(name
);
1135 var netid
= getWifiNetidBySid(name
);
1137 return this.instantiateDevice(netid
[0]);
1144 * Get a sorted list of all found network devices.
1146 * @returns {Promise<Array<LuCI.network.Device>>}
1147 * Returns a promise resolving to a sorted array of `Device` class
1148 * instances describing the network devices found on the system.
1150 getDevices: function() {
1151 return initNetworkState().then(L
.bind(function() {
1154 /* find simple devices */
1155 var uciInterfaces
= uci
.sections('network', 'interface');
1156 for (var i
= 0; i
< uciInterfaces
.length
; i
++) {
1157 var ifnames
= L
.toArray(uciInterfaces
[i
].ifname
);
1159 for (var j
= 0; j
< ifnames
.length
; j
++) {
1160 if (ifnames
[j
].charAt(0) == '@')
1163 if (isIgnoredIfname(ifnames
[j
]) || isVirtualIfname(ifnames
[j
]) || isWifiIfname(ifnames
[j
]))
1166 devices
[ifnames
[j
]] = this.instantiateDevice(ifnames
[j
]);
1170 for (var ifname
in _state
.netdevs
) {
1171 if (devices
.hasOwnProperty(ifname
))
1174 if (isIgnoredIfname(ifname
) || isWifiIfname(ifname
))
1177 if (_state
.netdevs
[ifname
].wireless
)
1180 devices
[ifname
] = this.instantiateDevice(ifname
);
1183 /* find VLAN devices */
1184 var uciSwitchVLANs
= uci
.sections('network', 'switch_vlan');
1185 for (var i
= 0; i
< uciSwitchVLANs
.length
; i
++) {
1186 if (typeof(uciSwitchVLANs
[i
].ports
) != 'string' ||
1187 typeof(uciSwitchVLANs
[i
].device
) != 'string' ||
1188 !_state
.switches
.hasOwnProperty(uciSwitchVLANs
[i
].device
))
1191 var ports
= uciSwitchVLANs
[i
].ports
.split(/\s+/);
1192 for (var j
= 0; j
< ports
.length
; j
++) {
1193 var m
= ports
[j
].match(/^(\d+)([tu]?)$/);
1197 var netdev
= _state
.switches
[uciSwitchVLANs
[i
].device
].netdevs
[m
[1]];
1201 if (!devices
.hasOwnProperty(netdev
))
1202 devices
[netdev
] = this.instantiateDevice(netdev
);
1204 _state
.isSwitch
[netdev
] = true;
1209 var vid
= uciSwitchVLANs
[i
].vid
|| uciSwitchVLANs
[i
].vlan
;
1210 vid
= (vid
!= null ? +vid
: null);
1212 if (vid
== null || vid
< 0 || vid
> 4095)
1215 var vlandev
= '%s.%d'.format(netdev
, vid
);
1217 if (!devices
.hasOwnProperty(vlandev
))
1218 devices
[vlandev
] = this.instantiateDevice(vlandev
);
1220 _state
.isSwitch
[vlandev
] = true;
1224 /* find bridge VLAN devices */
1225 var uciBridgeVLANs
= uci
.sections('network', 'bridge-vlan');
1226 for (var i
= 0; i
< uciBridgeVLANs
.length
; i
++) {
1227 var basedev
= uciBridgeVLANs
[i
].device
,
1228 local
= uciBridgeVLANs
[i
].local
,
1229 alias
= uciBridgeVLANs
[i
].alias
,
1230 vid
= +uciBridgeVLANs
[i
].vlan
,
1231 ports
= L
.toArray(uciBridgeVLANs
[i
].ports
);
1236 if (isNaN(vid
) || vid
< 0 || vid
> 4095)
1239 var vlandev
= '%s.%s'.format(basedev
, alias
|| vid
);
1241 _state
.isBridge
[basedev
] = true;
1243 if (!_state
.bridges
.hasOwnProperty(basedev
))
1244 _state
.bridges
[basedev
] = {
1249 if (!devices
.hasOwnProperty(vlandev
))
1250 devices
[vlandev
] = this.instantiateDevice(vlandev
);
1252 ports
.forEach(function(port_name
) {
1253 var m
= port_name
.match(/^([^:]+)(?::[ut*]+)?$/),
1254 p
= m
? m
[1] : null;
1259 if (_state
.bridges
[basedev
].ifnames
.filter(function(sd
) { return sd
.name
== p
}).length
)
1262 _state
.netdevs
[p
] = _state
.netdevs
[p
] || {
1267 devtype
: 'ethernet',
1272 _state
.bridges
[basedev
].ifnames
.push(_state
.netdevs
[p
]);
1273 _state
.netdevs
[p
].bridge
= _state
.bridges
[basedev
];
1277 /* find wireless interfaces */
1278 var uciWifiIfaces
= uci
.sections('wireless', 'wifi-iface'),
1281 for (var i
= 0; i
< uciWifiIfaces
.length
; i
++) {
1282 if (typeof(uciWifiIfaces
[i
].device
) != 'string')
1285 networkCount
[uciWifiIfaces
[i
].device
] = (networkCount
[uciWifiIfaces
[i
].device
] || 0) + 1;
1287 var netid
= '%s.network%d'.format(uciWifiIfaces
[i
].device
, networkCount
[uciWifiIfaces
[i
].device
]);
1289 devices
[netid
] = this.instantiateDevice(netid
);
1292 /* find uci declared devices */
1293 var uciDevices
= uci
.sections('network', 'device');
1295 for (var i
= 0; i
< uciDevices
.length
; i
++) {
1296 var type
= uciDevices
[i
].type
,
1297 name
= uciDevices
[i
].name
;
1299 if (!type
|| !name
|| devices
.hasOwnProperty(name
))
1302 if (type
== 'bridge')
1303 _state
.isBridge
[name
] = true;
1305 devices
[name
] = this.instantiateDevice(name
);
1310 for (var netdev
in devices
)
1311 if (devices
.hasOwnProperty(netdev
))
1312 rv
.push(devices
[netdev
]);
1314 rv
.sort(deviceSort
);
1321 * Test if a given network device name is in the list of patterns for
1322 * device names to ignore.
1324 * Ignored device names are usually Linux network devices which are
1325 * spawned implicitly by kernel modules such as `tunl0` or `hwsim0`
1326 * and which are unsuitable for use in network configuration.
1328 * @param {string} name
1329 * The device name to test.
1331 * @returns {boolean}
1332 * Returns `true` if the given name is in the ignore pattern list,
1333 * else returns `false`.
1335 isIgnoredDevice: function(name
) {
1336 return isIgnoredIfname(name
);
1340 * Get a {@link LuCI.network.WifiDevice WifiDevice} instance describing
1341 * the given wireless radio.
1343 * @param {string} devname
1344 * The configuration name of the wireless radio to lookup, e.g. `radio0`
1345 * for the first mac80211 phy on the system.
1347 * @returns {Promise<null|LuCI.network.WifiDevice>}
1348 * Returns a promise resolving to the `WifiDevice` instance describing
1349 * the underlying radio device or `null` if the wireless radio could not
1352 getWifiDevice: function(devname
) {
1353 return initNetworkState().then(L
.bind(function() {
1354 var existingDevice
= uci
.get('wireless', devname
);
1356 if (existingDevice
== null || existingDevice
['.type'] != 'wifi-device')
1359 return this.instantiateWifiDevice(devname
, _state
.radios
[devname
] || {});
1364 * Obtain a list of all configured radio devices.
1366 * @returns {Promise<Array<LuCI.network.WifiDevice>>}
1367 * Returns a promise resolving to an array of `WifiDevice` instances
1368 * describing the wireless radios configured in the system.
1369 * The order of the array corresponds to the order of the radios in
1370 * the configuration.
1372 getWifiDevices: function() {
1373 return initNetworkState().then(L
.bind(function() {
1374 var uciWifiDevices
= uci
.sections('wireless', 'wifi-device'),
1377 for (var i
= 0; i
< uciWifiDevices
.length
; i
++) {
1378 var devname
= uciWifiDevices
[i
]['.name'];
1379 rv
.push(this.instantiateWifiDevice(devname
, _state
.radios
[devname
] || {}));
1387 * Get a {@link LuCI.network.WifiNetwork WifiNetwork} instance describing
1388 * the given wireless network.
1390 * @param {string} netname
1391 * The name of the wireless network to lookup. This may be either an uci
1392 * configuration section ID, a network ID in the form `radio#.network#`
1393 * or a Linux network device name like `wlan0` which is resolved to the
1394 * corresponding configuration section through `ubus` runtime information.
1396 * @returns {Promise<null|LuCI.network.WifiNetwork>}
1397 * Returns a promise resolving to the `WifiNetwork` instance describing
1398 * the wireless network or `null` if the corresponding network could not
1401 getWifiNetwork: function(netname
) {
1402 return initNetworkState()
1403 .then(L
.bind(this.lookupWifiNetwork
, this, netname
));
1407 * Get an array of all {@link LuCI.network.WifiNetwork WifiNetwork}
1408 * instances describing the wireless networks present on the system.
1410 * @returns {Promise<Array<LuCI.network.WifiNetwork>>}
1411 * Returns a promise resolving to an array of `WifiNetwork` instances
1412 * describing the wireless networks. The array will be empty if no networks
1415 getWifiNetworks: function() {
1416 return initNetworkState().then(L
.bind(function() {
1417 var wifiIfaces
= uci
.sections('wireless', 'wifi-iface'),
1420 for (var i
= 0; i
< wifiIfaces
.length
; i
++)
1421 rv
.push(this.lookupWifiNetwork(wifiIfaces
[i
]['.name']));
1423 rv
.sort(function(a
, b
) {
1424 return (a
.getID() > b
.getID());
1432 * Adds a new wireless network to the configuration and sets its options
1433 * to the provided values.
1435 * @param {Object<string, string|string[]>} options
1436 * The options to set for the newly added wireless network. This object
1437 * must at least contain a `device` property which is set to the radio
1438 * name the new network belongs to.
1440 * @returns {Promise<null|LuCI.network.WifiNetwork>}
1441 * Returns a promise resolving to a `WifiNetwork` instance describing
1442 * the newly added wireless network or `null` if the given options
1443 * were invalid or if the associated radio device could not be found.
1445 addWifiNetwork: function(options
) {
1446 return initNetworkState().then(L
.bind(function() {
1447 if (options
== null ||
1448 typeof(options
) != 'object' ||
1449 typeof(options
.device
) != 'string')
1452 var existingDevice
= uci
.get('wireless', options
.device
);
1453 if (existingDevice
== null || existingDevice
['.type'] != 'wifi-device')
1456 /* XXX: need to add a named section (wifinet#) here */
1457 var sid
= uci
.add('wireless', 'wifi-iface');
1458 for (var key
in options
)
1459 if (options
.hasOwnProperty(key
))
1460 uci
.set('wireless', sid
, key
, options
[key
]);
1462 var radioname
= existingDevice
['.name'],
1463 netid
= getWifiNetidBySid(sid
) || [];
1465 return this.instantiateWifiNetwork(sid
, radioname
, _state
.radios
[radioname
], netid
[0], null);
1470 * Deletes the given wireless network from the configuration.
1472 * @param {string} netname
1473 * The name of the network to remove. This may be either a
1474 * network ID in the form `radio#.network#` or a Linux network device
1475 * name like `wlan0` which is resolved to the corresponding configuration
1476 * section through `ubus` runtime information.
1478 * @returns {Promise<boolean>}
1479 * Returns a promise resolving to `true` if the wireless network has been
1480 * successfully deleted from the configuration or `false` if it could not
1483 deleteWifiNetwork: function(netname
) {
1484 return initNetworkState().then(L
.bind(function() {
1485 var sid
= getWifiSidByIfname(netname
);
1490 uci
.remove('wireless', sid
);
1496 getStatusByRoute: function(addr
, mask
) {
1497 return initNetworkState().then(L
.bind(function() {
1500 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
1501 if (!Array
.isArray(_state
.ifaces
[i
].route
))
1504 for (var j
= 0; j
< _state
.ifaces
[i
].route
.length
; j
++) {
1505 if (typeof(_state
.ifaces
[i
].route
[j
]) != 'object' ||
1506 typeof(_state
.ifaces
[i
].route
[j
].target
) != 'string' ||
1507 typeof(_state
.ifaces
[i
].route
[j
].mask
) != 'number')
1510 if (_state
.ifaces
[i
].route
[j
].table
)
1513 if (_state
.ifaces
[i
].route
[j
].target
!= addr
||
1514 _state
.ifaces
[i
].route
[j
].mask
!= mask
)
1517 rv
.push(_state
.ifaces
[i
]);
1521 rv
.sort(function(a
, b
) {
1522 if (a
.metric
!= b
.metric
)
1523 return (a
.metric
- b
.metric
);
1525 if (a
.interface < b
.interface)
1527 else if (a
.interface > b
.interface)
1538 getStatusByAddress: function(addr
) {
1539 return initNetworkState().then(L
.bind(function() {
1542 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
1543 if (Array
.isArray(_state
.ifaces
[i
]['ipv4-address']))
1544 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv4-address'].length
; j
++)
1545 if (typeof(_state
.ifaces
[i
]['ipv4-address'][j
]) == 'object' &&
1546 _state
.ifaces
[i
]['ipv4-address'][j
].address
== addr
)
1547 return _state
.ifaces
[i
];
1549 if (Array
.isArray(_state
.ifaces
[i
]['ipv6-address']))
1550 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv6-address'].length
; j
++)
1551 if (typeof(_state
.ifaces
[i
]['ipv6-address'][j
]) == 'object' &&
1552 _state
.ifaces
[i
]['ipv6-address'][j
].address
== addr
)
1553 return _state
.ifaces
[i
];
1555 if (Array
.isArray(_state
.ifaces
[i
]['ipv6-prefix-assignment']))
1556 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv6-prefix-assignment'].length
; j
++)
1557 if (typeof(_state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]) == 'object' &&
1558 typeof(_state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]['local-address']) == 'object' &&
1559 _state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]['local-address'].address
== addr
)
1560 return _state
.ifaces
[i
];
1568 * Get IPv4 wan networks.
1570 * This function looks up all networks having a default `0.0.0.0/0` route
1571 * and returns them as array.
1573 * @returns {Promise<Array<LuCI.network.Protocol>>}
1574 * Returns a promise resolving to an array of `Protocol` subclass
1575 * instances describing the found default route interfaces.
1577 getWANNetworks: function() {
1578 return this.getStatusByRoute('0.0.0.0', 0).then(L
.bind(function(statuses
) {
1579 var rv
= [], seen
= {};
1581 for (var i
= 0; i
< statuses
.length
; i
++) {
1582 if (!seen
.hasOwnProperty(statuses
[i
].interface)) {
1583 rv
.push(this.instantiateNetwork(statuses
[i
].interface, statuses
[i
].proto
));
1584 seen
[statuses
[i
].interface] = true;
1593 * Get IPv6 wan networks.
1595 * This function looks up all networks having a default `::/0` route
1596 * and returns them as array.
1598 * @returns {Promise<Array<LuCI.network.Protocol>>}
1599 * Returns a promise resolving to an array of `Protocol` subclass
1600 * instances describing the found IPv6 default route interfaces.
1602 getWAN6Networks: function() {
1603 return this.getStatusByRoute('::', 0).then(L
.bind(function(statuses
) {
1604 var rv
= [], seen
= {};
1606 for (var i
= 0; i
< statuses
.length
; i
++) {
1607 if (!seen
.hasOwnProperty(statuses
[i
].interface)) {
1608 rv
.push(this.instantiateNetwork(statuses
[i
].interface, statuses
[i
].proto
));
1609 seen
[statuses
[i
].interface] = true;
1618 * Describes an swconfig switch topology by specifying the CPU
1619 * connections and external port labels of a switch.
1621 * @typedef {Object<string, Object|Array>} SwitchTopology
1622 * @memberof LuCI.network
1624 * @property {Object<number, string>} netdevs
1625 * The `netdevs` property points to an object describing the CPU port
1626 * connections of the switch. The numeric key of the enclosed object is
1627 * the port number, the value contains the Linux network device name the
1628 * port is hardwired to.
1630 * @property {Array<Object<string, boolean|number|string>>} ports
1631 * The `ports` property points to an array describing the populated
1632 * ports of the switch in the external label order. Each array item is
1633 * an object containg the following keys:
1634 * - `num` - the internal switch port number
1635 * - `label` - the label of the port, e.g. `LAN 1` or `CPU (eth0)`
1636 * - `device` - the connected Linux network device name (CPU ports only)
1637 * - `tagged` - a boolean indicating whether the port must be tagged to
1638 * function (CPU ports only)
1642 * Returns the topologies of all swconfig switches found on the system.
1644 * @returns {Promise<Object<string, LuCI.network.SwitchTopology>>}
1645 * Returns a promise resolving to an object containing the topologies
1646 * of each switch. The object keys correspond to the name of the switches
1647 * such as `switch0`, the values are
1648 * {@link LuCI.network.SwitchTopology SwitchTopology} objects describing
1651 getSwitchTopologies: function() {
1652 return initNetworkState().then(function() {
1653 return _state
.switches
;
1658 instantiateNetwork: function(name
, proto
) {
1662 proto
= (proto
== null ? uci
.get('network', name
, 'proto') : proto
);
1664 var protoClass
= _protocols
[proto
] || Protocol
;
1665 return new protoClass(name
);
1669 instantiateDevice: function(name
, network
, extend
) {
1671 return new (Device
.extend(extend
))(name
, network
);
1673 return new Device(name
, network
);
1677 instantiateWifiDevice: function(radioname
, radiostate
) {
1678 return new WifiDevice(radioname
, radiostate
);
1682 instantiateWifiNetwork: function(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
) {
1683 return new WifiNetwork(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
);
1687 lookupWifiNetwork: function(netname
) {
1688 var sid
, res
, netid
, radioname
, radiostate
, netstate
;
1690 sid
= getWifiSidByNetid(netname
);
1693 res
= getWifiStateBySid(sid
);
1695 radioname
= res
? res
[0] : null;
1696 radiostate
= res
? res
[1] : null;
1697 netstate
= res
? res
[2] : null;
1700 res
= getWifiStateByIfname(netname
);
1704 radiostate
= res
[1];
1706 sid
= netstate
.section
;
1707 netid
= L
.toArray(getWifiNetidBySid(sid
))[0];
1710 res
= getWifiStateBySid(netname
);
1714 radiostate
= res
[1];
1717 netid
= L
.toArray(getWifiNetidBySid(sid
))[0];
1720 res
= getWifiNetidBySid(netname
);
1731 return this.instantiateWifiNetwork(sid
|| netname
, radioname
,
1732 radiostate
, netid
, netstate
,
1733 netstate
? _state
.hostapd
[netstate
.ifname
] : null);
1737 * Obtains the the network device name of the given object.
1739 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} obj
1740 * The object to get the device name from.
1742 * @returns {null|string}
1743 * Returns a string containing the device name or `null` if the given
1744 * object could not be converted to a name.
1746 getIfnameOf: function(obj
) {
1747 return ifnameOf(obj
);
1751 * Queries the internal DSL modem type from board information.
1753 * @returns {Promise<null|string>}
1754 * Returns a promise resolving to the type of the internal modem
1755 * (e.g. `vdsl`) or to `null` if no internal modem is present.
1757 getDSLModemType: function() {
1758 return initNetworkState().then(function() {
1759 return _state
.hasDSLModem
? _state
.hasDSLModem
.type
: null;
1764 * Queries aggregated information about known hosts.
1766 * This function aggregates information from various sources such as
1767 * DHCP lease databases, ARP and IPv6 neighbour entries, wireless
1768 * association list etc. and returns a {@link LuCI.network.Hosts Hosts}
1769 * class instance describing the found hosts.
1771 * @returns {Promise<LuCI.network.Hosts>}
1772 * Returns a `Hosts` instance describing host known on the system.
1774 getHostHints: function() {
1775 return initNetworkState().then(function() {
1776 return new Hosts(_state
.hosts
);
1783 * @memberof LuCI.network
1787 * The `LuCI.network.Hosts` class encapsulates host information aggregated
1788 * from multiple sources and provides convenience functions to access the
1789 * host information by different criteria.
1791 Hosts
= baseclass
.extend(/** @lends LuCI.network.Hosts.prototype */ {
1792 __init__: function(hosts
) {
1797 * Lookup the hostname associated with the given MAC address.
1799 * @param {string} mac
1800 * The MAC address to lookup.
1802 * @returns {null|string}
1803 * Returns the hostname associated with the given MAC or `null` if
1804 * no matching host could be found or if no hostname is known for
1805 * the corresponding host.
1807 getHostnameByMACAddr: function(mac
) {
1808 return this.hosts
[mac
] ? this.hosts
[mac
].name
: null;
1812 * Lookup the IPv4 address associated with the given MAC address.
1814 * @param {string} mac
1815 * The MAC address to lookup.
1817 * @returns {null|string}
1818 * Returns the IPv4 address associated with the given MAC or `null` if
1819 * no matching host could be found or if no IPv4 address is known for
1820 * the corresponding host.
1822 getIPAddrByMACAddr: function(mac
) {
1823 return this.hosts
[mac
] ? this.hosts
[mac
].ipv4
: null;
1827 * Lookup the IPv6 address associated with the given MAC address.
1829 * @param {string} mac
1830 * The MAC address to lookup.
1832 * @returns {null|string}
1833 * Returns the IPv6 address associated with the given MAC or `null` if
1834 * no matching host could be found or if no IPv6 address is known for
1835 * the corresponding host.
1837 getIP6AddrByMACAddr: function(mac
) {
1838 return this.hosts
[mac
] ? this.hosts
[mac
].ipv6
: null;
1842 * Lookup the hostname associated with the given IPv4 address.
1844 * @param {string} ipaddr
1845 * The IPv4 address to lookup.
1847 * @returns {null|string}
1848 * Returns the hostname associated with the given IPv4 or `null` if
1849 * no matching host could be found or if no hostname is known for
1850 * the corresponding host.
1852 getHostnameByIPAddr: function(ipaddr
) {
1853 for (var mac
in this.hosts
)
1854 if (this.hosts
[mac
].ipv4
== ipaddr
&& this.hosts
[mac
].name
!= null)
1855 return this.hosts
[mac
].name
;
1860 * Lookup the MAC address associated with the given IPv4 address.
1862 * @param {string} ipaddr
1863 * The IPv4 address to lookup.
1865 * @returns {null|string}
1866 * Returns the MAC address associated with the given IPv4 or `null` if
1867 * no matching host could be found or if no MAC address is known for
1868 * the corresponding host.
1870 getMACAddrByIPAddr: function(ipaddr
) {
1871 for (var mac
in this.hosts
)
1872 if (this.hosts
[mac
].ipv4
== ipaddr
)
1878 * Lookup the hostname associated with the given IPv6 address.
1880 * @param {string} ipaddr
1881 * The IPv6 address to lookup.
1883 * @returns {null|string}
1884 * Returns the hostname associated with the given IPv6 or `null` if
1885 * no matching host could be found or if no hostname is known for
1886 * the corresponding host.
1888 getHostnameByIP6Addr: function(ip6addr
) {
1889 for (var mac
in this.hosts
)
1890 if (this.hosts
[mac
].ipv6
== ip6addr
&& this.hosts
[mac
].name
!= null)
1891 return this.hosts
[mac
].name
;
1896 * Lookup the MAC address associated with the given IPv6 address.
1898 * @param {string} ipaddr
1899 * The IPv6 address to lookup.
1901 * @returns {null|string}
1902 * Returns the MAC address associated with the given IPv6 or `null` if
1903 * no matching host could be found or if no MAC address is known for
1904 * the corresponding host.
1906 getMACAddrByIP6Addr: function(ip6addr
) {
1907 for (var mac
in this.hosts
)
1908 if (this.hosts
[mac
].ipv6
== ip6addr
)
1914 * Return an array of (MAC address, name hint) tuples sorted by
1917 * @param {boolean} [preferIp6=false]
1918 * Whether to prefer IPv6 addresses (`true`) or IPv4 addresses (`false`)
1919 * as name hint when no hostname is known for a specific MAC address.
1921 * @returns {Array<Array<string>>}
1922 * Returns an array of arrays containing a name hint for each found
1923 * MAC address on the system. The array is sorted ascending by MAC.
1925 * Each item of the resulting array is a two element array with the
1926 * MAC being the first element and the name hint being the second
1927 * element. The name hint is either the hostname, an IPv4 or an IPv6
1928 * address related to the MAC address.
1930 * If no hostname but both IPv4 and IPv6 addresses are known, the
1931 * `preferIP6` flag specifies whether the IPv6 or the IPv4 address
1934 getMACHints: function(preferIp6
) {
1936 for (var mac
in this.hosts
) {
1937 var hint
= this.hosts
[mac
].name
||
1938 this.hosts
[mac
][preferIp6
? 'ipv6' : 'ipv4'] ||
1939 this.hosts
[mac
][preferIp6
? 'ipv4' : 'ipv6'];
1941 rv
.push([mac
, hint
]);
1943 return rv
.sort(function(a
, b
) { return a
[0] > b
[0] });
1949 * @memberof LuCI.network
1953 * The `Network.Protocol` class serves as base for protocol specific
1954 * subclasses which describe logical UCI networks defined by `config
1955 * interface` sections in `/etc/config/network`.
1957 Protocol
= baseclass
.extend(/** @lends LuCI.network.Protocol.prototype */ {
1958 __init__: function(name
) {
1962 _get: function(opt
) {
1963 var val
= uci
.get('network', this.sid
, opt
);
1965 if (Array
.isArray(val
))
1966 return val
.join(' ');
1971 _ubus: function(field
) {
1972 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
1973 if (_state
.ifaces
[i
].interface != this.sid
)
1976 return (field
!= null ? _state
.ifaces
[i
][field
] : _state
.ifaces
[i
]);
1981 * Read the given UCI option value of this network.
1983 * @param {string} opt
1984 * The UCI option name to read.
1986 * @returns {null|string|string[]}
1987 * Returns the UCI option value or `null` if the requested option is
1990 get: function(opt
) {
1991 return uci
.get('network', this.sid
, opt
);
1995 * Set the given UCI option of this network to the given value.
1997 * @param {string} opt
1998 * The name of the UCI option to set.
2000 * @param {null|string|string[]} val
2001 * The value to set or `null` to remove the given option from the
2004 set: function(opt
, val
) {
2005 return uci
.set('network', this.sid
, opt
, val
);
2009 * Get the associared Linux network device of this network.
2011 * @returns {null|string}
2012 * Returns the name of the associated network device or `null` if
2013 * it could not be determined.
2015 getIfname: function() {
2018 if (this.isFloating())
2019 ifname
= this._ubus('l3_device');
2021 ifname
= this._ubus('device') || this._ubus('l3_device');
2026 var res
= getWifiNetidByNetname(this.sid
);
2027 return (res
!= null ? res
[0] : null);
2031 * Get the name of this network protocol class.
2033 * This function will be overwritten by subclasses created by
2034 * {@link LuCI.network#registerProtocol Network.registerProtocol()}.
2038 * Returns the name of the network protocol implementation, e.g.
2039 * `static` or `dhcp`.
2041 getProtocol: function() {
2046 * Return a human readable description for the protcol, such as
2047 * `Static address` or `DHCP client`.
2049 * This function should be overwritten by subclasses.
2053 * Returns the description string.
2055 getI18n: function() {
2056 switch (this.getProtocol()) {
2057 case 'none': return _('Unmanaged');
2058 case 'static': return _('Static address');
2059 case 'dhcp': return _('DHCP client');
2060 default: return _('Unknown');
2065 * Get the type of the underlying interface.
2067 * This function actually is a convenience wrapper around
2068 * `proto.get("type")` and is mainly used by other `LuCI.network` code
2069 * to check whether the interface is declared as bridge in UCI.
2071 * @returns {null|string}
2072 * Returns the value of the `type` option of the associated logical
2073 * interface or `null` if no `type` option is set.
2075 getType: function() {
2076 return this._get('type');
2080 * Get the name of the associated logical interface.
2083 * Returns the logical interface name, such as `lan` or `wan`.
2085 getName: function() {
2090 * Get the uptime of the logical interface.
2093 * Returns the uptime of the associated interface in seconds.
2095 getUptime: function() {
2096 return this._ubus('uptime') || 0;
2100 * Get the logical interface expiry time in seconds.
2102 * For protocols that have a concept of a lease, such as DHCP or
2103 * DHCPv6, this function returns the remaining time in seconds
2104 * until the lease expires.
2107 * Returns the amount of seconds until the lease expires or `-1`
2108 * if it isn't applicable to the associated protocol.
2110 getExpiry: function() {
2111 var u
= this._ubus('uptime'),
2112 d
= this._ubus('data');
2114 if (typeof(u
) == 'number' && d
!= null &&
2115 typeof(d
) == 'object' && typeof(d
.leasetime
) == 'number') {
2116 var r
= d
.leasetime
- (u
% d
.leasetime
);
2117 return (r
> 0 ? r
: 0);
2124 * Get the metric value of the logical interface.
2127 * Returns the current metric value used for device and network
2128 * routes spawned by the associated logical interface.
2130 getMetric: function() {
2131 return this._ubus('metric') || 0;
2135 * Get the requested firewall zone name of the logical interface.
2137 * Some protocol implementations request a specific firewall zone
2138 * to trigger inclusion of their resulting network devices into the
2139 * firewall rule set.
2141 * @returns {null|string}
2142 * Returns the requested firewall zone name as published in the
2143 * `ubus` runtime information or `null` if the remote protocol
2144 * handler didn't request a zone.
2146 getZoneName: function() {
2147 var d
= this._ubus('data');
2149 if (L
.isObject(d
) && typeof(d
.zone
) == 'string')
2156 * Query the first (primary) IPv4 address of the logical interface.
2158 * @returns {null|string}
2159 * Returns the primary IPv4 address registered by the protocol handler
2160 * or `null` if no IPv4 addresses were set.
2162 getIPAddr: function() {
2163 var addrs
= this._ubus('ipv4-address');
2164 return ((Array
.isArray(addrs
) && addrs
.length
) ? addrs
[0].address
: null);
2168 * Query all IPv4 addresses of the logical interface.
2170 * @returns {string[]}
2171 * Returns an array of IPv4 addresses in CIDR notation which have been
2172 * registered by the protocol handler. The order of the resulting array
2173 * follows the order of the addresses in `ubus` runtime information.
2175 getIPAddrs: function() {
2176 var addrs
= this._ubus('ipv4-address'),
2179 if (Array
.isArray(addrs
))
2180 for (var i
= 0; i
< addrs
.length
; i
++)
2181 rv
.push('%s/%d'.format(addrs
[i
].address
, addrs
[i
].mask
));
2187 * Query the first (primary) IPv4 netmask of the logical interface.
2189 * @returns {null|string}
2190 * Returns the netmask of the primary IPv4 address registered by the
2191 * protocol handler or `null` if no IPv4 addresses were set.
2193 getNetmask: function() {
2194 var addrs
= this._ubus('ipv4-address');
2195 if (Array
.isArray(addrs
) && addrs
.length
)
2196 return prefixToMask(addrs
[0].mask
, false);
2200 * Query the gateway (nexthop) of the default route associated with
2201 * this logical interface.
2204 * Returns a string containing the IPv4 nexthop address of the associated
2205 * default route or `null` if no default route was found.
2207 getGatewayAddr: function() {
2208 var routes
= this._ubus('route');
2210 if (Array
.isArray(routes
))
2211 for (var i
= 0; i
< routes
.length
; i
++)
2212 if (typeof(routes
[i
]) == 'object' &&
2213 routes
[i
].target
== '0.0.0.0' &&
2214 routes
[i
].mask
== 0)
2215 return routes
[i
].nexthop
;
2221 * Query the IPv4 DNS servers associated with the logical interface.
2223 * @returns {string[]}
2224 * Returns an array of IPv4 DNS servers registered by the remote
2227 getDNSAddrs: function() {
2228 var addrs
= this._ubus('dns-server'),
2231 if (Array
.isArray(addrs
))
2232 for (var i
= 0; i
< addrs
.length
; i
++)
2233 if (!/:/.test(addrs
[i
]))
2240 * Query the first (primary) IPv6 address of the logical interface.
2242 * @returns {null|string}
2243 * Returns the primary IPv6 address registered by the protocol handler
2244 * in CIDR notation or `null` if no IPv6 addresses were set.
2246 getIP6Addr: function() {
2247 var addrs
= this._ubus('ipv6-address');
2249 if (Array
.isArray(addrs
) && L
.isObject(addrs
[0]))
2250 return '%s/%d'.format(addrs
[0].address
, addrs
[0].mask
);
2252 addrs
= this._ubus('ipv6-prefix-assignment');
2254 if (Array
.isArray(addrs
) && L
.isObject(addrs
[0]) && L
.isObject(addrs
[0]['local-address']))
2255 return '%s/%d'.format(addrs
[0]['local-address'].address
, addrs
[0]['local-address'].mask
);
2261 * Query all IPv6 addresses of the logical interface.
2263 * @returns {string[]}
2264 * Returns an array of IPv6 addresses in CIDR notation which have been
2265 * registered by the protocol handler. The order of the resulting array
2266 * follows the order of the addresses in `ubus` runtime information.
2268 getIP6Addrs: function() {
2269 var addrs
= this._ubus('ipv6-address'),
2272 if (Array
.isArray(addrs
))
2273 for (var i
= 0; i
< addrs
.length
; i
++)
2274 if (L
.isObject(addrs
[i
]))
2275 rv
.push('%s/%d'.format(addrs
[i
].address
, addrs
[i
].mask
));
2277 addrs
= this._ubus('ipv6-prefix-assignment');
2279 if (Array
.isArray(addrs
))
2280 for (var i
= 0; i
< addrs
.length
; i
++)
2281 if (L
.isObject(addrs
[i
]) && L
.isObject(addrs
[i
]['local-address']))
2282 rv
.push('%s/%d'.format(addrs
[i
]['local-address'].address
, addrs
[i
]['local-address'].mask
));
2288 * Query the gateway (nexthop) of the IPv6 default route associated with
2289 * this logical interface.
2292 * Returns a string containing the IPv6 nexthop address of the associated
2293 * default route or `null` if no default route was found.
2295 getGateway6Addr: function() {
2296 var routes
= this._ubus('route');
2298 if (Array
.isArray(routes
))
2299 for (var i
= 0; i
< routes
.length
; i
++)
2300 if (typeof(routes
[i
]) == 'object' &&
2301 routes
[i
].target
== '::' &&
2302 routes
[i
].mask
== 0)
2303 return routes
[i
].nexthop
;
2309 * Query the IPv6 DNS servers associated with the logical interface.
2311 * @returns {string[]}
2312 * Returns an array of IPv6 DNS servers registered by the remote
2315 getDNS6Addrs: function() {
2316 var addrs
= this._ubus('dns-server'),
2319 if (Array
.isArray(addrs
))
2320 for (var i
= 0; i
< addrs
.length
; i
++)
2321 if (/:/.test(addrs
[i
]))
2328 * Query the routed IPv6 prefix associated with the logical interface.
2330 * @returns {null|string}
2331 * Returns the routed IPv6 prefix registered by the remote protocol
2332 * handler or `null` if no prefix is present.
2334 getIP6Prefix: function() {
2335 var prefixes
= this._ubus('ipv6-prefix');
2337 if (Array
.isArray(prefixes
) && L
.isObject(prefixes
[0]))
2338 return '%s/%d'.format(prefixes
[0].address
, prefixes
[0].mask
);
2344 * Query interface error messages published in `ubus` runtime state.
2346 * Interface errors are emitted by remote protocol handlers if the setup
2347 * of the underlying logical interface failed, e.g. due to bad
2348 * configuration or network connectivity issues.
2350 * This function will translate the found error codes to human readable
2351 * messages using the descriptions registered by
2352 * {@link LuCI.network#registerErrorCode Network.registerErrorCode()}
2353 * and fall back to `"Unknown error (%s)"` where `%s` is replaced by the
2354 * error code in case no translation can be found.
2356 * @returns {string[]}
2357 * Returns an array of translated interface error messages.
2359 getErrors: function() {
2360 var errors
= this._ubus('errors'),
2363 if (Array
.isArray(errors
)) {
2364 for (var i
= 0; i
< errors
.length
; i
++) {
2365 if (!L
.isObject(errors
[i
]) || typeof(errors
[i
].code
) != 'string')
2369 rv
.push(proto_errors
[errors
[i
].code
] || _('Unknown error (%s)').format(errors
[i
].code
));
2377 * Checks whether the underlying logical interface is declared as bridge.
2379 * @returns {boolean}
2380 * Returns `true` when the interface is declared with `option type bridge`
2381 * and when the associated protocol implementation is not marked virtual
2382 * or `false` when the logical interface is no bridge.
2384 isBridge: function() {
2385 return (!this.isVirtual() && this.getType() == 'bridge');
2389 * Get the name of the opkg package providing the protocol functionality.
2391 * This function should be overwritten by protocol specific subclasses.
2396 * Returns the name of the opkg package required for the protocol to
2397 * function, e.g. `odhcp6c` for the `dhcpv6` prototocol.
2399 getOpkgPackage: function() {
2404 * Check function for the protocol handler if a new interface is createable.
2406 * This function should be overwritten by protocol specific subclasses.
2410 * @param {string} ifname
2411 * The name of the interface to be created.
2413 * @returns {Promise<void>}
2414 * Returns a promise resolving if new interface is createable, else
2415 * rejects with an error message string.
2417 isCreateable: function(ifname
) {
2418 return Promise
.resolve(null);
2422 * Checks whether the protocol functionality is installed.
2424 * This function exists for compatibility with old code, it always
2430 * @returns {boolean}
2431 * Returns `true` if the protocol support is installed, else `false`.
2433 isInstalled: function() {
2438 * Checks whether this protocol is "virtual".
2440 * A "virtual" protocol is a protocol which spawns its own interfaces
2441 * on demand instead of using existing physical interfaces.
2443 * Examples for virtual protocols are `6in4` which `gre` spawn tunnel
2444 * network device on startup, examples for non-virtual protcols are
2445 * `dhcp` or `static` which apply IP configuration to existing interfaces.
2447 * This function should be overwritten by subclasses.
2449 * @returns {boolean}
2450 * Returns a boolean indicating whether the underlying protocol spawns
2451 * dynamic interfaces (`true`) or not (`false`).
2453 isVirtual: function() {
2458 * Checks whether this protocol is "floating".
2460 * A "floating" protocol is a protocol which spawns its own interfaces
2461 * on demand, like a virtual one but which relies on an existinf lower
2462 * level interface to initiate the connection.
2464 * An example for such a protocol is "pppoe".
2466 * This function exists for backwards compatibility with older code
2467 * but should not be used anymore.
2470 * @returns {boolean}
2471 * Returns a boolean indicating whether this protocol is floating (`true`)
2474 isFloating: function() {
2479 * Checks whether this logical interface is dynamic.
2481 * A dynamic interface is an interface which has been created at runtime,
2482 * e.g. as sub-interface of another interface, but which is not backed by
2483 * any user configuration. Such dynamic interfaces cannot be edited but
2484 * only brought down or restarted.
2486 * @returns {boolean}
2487 * Returns a boolean indicating whether this interface is dynamic (`true`)
2490 isDynamic: function() {
2491 return (this._ubus('dynamic') == true);
2495 * Checks whether this interface is an alias interface.
2497 * Alias interfaces are interfaces layering on top of another interface
2498 * and are denoted by a special `@interfacename` notation in the
2499 * underlying `ifname` option.
2501 * @returns {null|string}
2502 * Returns the name of the parent interface if this logical interface
2503 * is an alias or `null` if it is not an alias interface.
2505 isAlias: function() {
2506 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'ifname')),
2509 for (var i
= 0; i
< ifnames
.length
; i
++)
2510 if (ifnames
[i
].charAt(0) == '@')
2511 parent
= ifnames
[i
].substr(1);
2512 else if (parent
!= null)
2519 * Checks whether this logical interface is "empty", meaning that ut
2520 * has no network devices attached.
2522 * @returns {boolean}
2523 * Returns `true` if this logical interface is empty, else `false`.
2525 isEmpty: function() {
2526 if (this.isFloating())
2530 ifname
= this._get('ifname');
2532 if (ifname
!= null && ifname
.match(/\S+/))
2535 if (empty
== true && getWifiNetidBySid(this.sid
) != null)
2542 * Checks whether this logical interface is configured and running.
2544 * @returns {boolean}
2545 * Returns `true` when the interface is active or `false` when it is not.
2548 return (this._ubus('up') == true);
2552 * Add the given network device to the logical interface.
2554 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2555 * The object or device name to add to the logical interface. In case the
2556 * given argument is not a string, it is resolved though the
2557 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2559 * @returns {boolean}
2560 * Returns `true` if the device name has been added or `false` if any
2561 * argument was invalid, if the device was already part of the logical
2562 * interface or if the logical interface is virtual.
2564 addDevice: function(ifname
) {
2565 ifname
= ifnameOf(ifname
);
2567 if (ifname
== null || this.isFloating())
2570 var wif
= getWifiSidByIfname(ifname
);
2573 return appendValue('wireless', wif
, 'network', this.sid
);
2575 return appendValue('network', this.sid
, 'ifname', ifname
);
2579 * Remove the given network device from the logical interface.
2581 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2582 * The object or device name to remove from the logical interface. In case
2583 * the given argument is not a string, it is resolved though the
2584 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2586 * @returns {boolean}
2587 * Returns `true` if the device name has been added or `false` if any
2588 * argument was invalid, if the device was already part of the logical
2589 * interface or if the logical interface is virtual.
2591 deleteDevice: function(ifname
) {
2594 ifname
= ifnameOf(ifname
);
2596 if (ifname
== null || this.isFloating())
2599 var wif
= getWifiSidByIfname(ifname
);
2602 rv
= removeValue('wireless', wif
, 'network', this.sid
);
2604 if (removeValue('network', this.sid
, 'ifname', ifname
))
2611 * Returns the Linux network device associated with this logical
2614 * @returns {LuCI.network.Device}
2615 * Returns a `Network.Device` class instance representing the
2616 * expected Linux network device according to the configuration.
2618 getDevice: function() {
2619 if (this.isVirtual()) {
2620 var ifname
= '%s-%s'.format(this.getProtocol(), this.sid
);
2621 _state
.isTunnel
[this.getProtocol() + '-' + this.sid
] = true;
2622 return Network
.prototype.instantiateDevice(ifname
, this);
2624 else if (this.isBridge()) {
2625 var ifname
= 'br-%s'.format(this.sid
);
2626 _state
.isBridge
[ifname
] = true;
2627 return new Device(ifname
, this);
2630 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'ifname'));
2632 for (var i
= 0; i
< ifnames
.length
; i
++) {
2633 var m
= ifnames
[i
].match(/^([^:/]+)/);
2634 return ((m
&& m
[1]) ? Network
.prototype.instantiateDevice(m
[1], this) : null);
2637 ifname
= getWifiNetidByNetname(this.sid
);
2639 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
[0], this) : null);
2644 * Returns the layer 2 linux network device currently associated
2645 * with this logical interface.
2647 * @returns {LuCI.network.Device}
2648 * Returns a `Network.Device` class instance representing the Linux
2649 * network device currently associated with the logical interface.
2651 getL2Device: function() {
2652 var ifname
= this._ubus('device');
2653 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
, this) : null);
2657 * Returns the layer 3 linux network device currently associated
2658 * with this logical interface.
2660 * @returns {LuCI.network.Device}
2661 * Returns a `Network.Device` class instance representing the Linux
2662 * network device currently associated with the logical interface.
2664 getL3Device: function() {
2665 var ifname
= this._ubus('l3_device');
2666 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
, this) : null);
2670 * Returns a list of network sub-devices associated with this logical
2673 * @returns {null|Array<LuCI.network.Device>}
2674 * Returns an array of of `Network.Device` class instances representing
2675 * the sub-devices attached to this logical interface or `null` if the
2676 * logical interface does not support sub-devices, e.g. because it is
2677 * virtual and not a bridge.
2679 getDevices: function() {
2682 if (!this.isBridge() && !(this.isVirtual() && !this.isFloating()))
2685 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'ifname'));
2687 for (var i
= 0; i
< ifnames
.length
; i
++) {
2688 if (ifnames
[i
].charAt(0) == '@')
2691 var m
= ifnames
[i
].match(/^([^:/]+)/);
2693 rv
.push(Network
.prototype.instantiateDevice(m
[1], this));
2696 var uciWifiIfaces
= uci
.sections('wireless', 'wifi-iface');
2698 for (var i
= 0; i
< uciWifiIfaces
.length
; i
++) {
2699 if (typeof(uciWifiIfaces
[i
].device
) != 'string')
2702 var networks
= L
.toArray(uciWifiIfaces
[i
].network
);
2704 for (var j
= 0; j
< networks
.length
; j
++) {
2705 if (networks
[j
] != this.sid
)
2708 var netid
= getWifiNetidBySid(uciWifiIfaces
[i
]['.name']);
2711 rv
.push(Network
.prototype.instantiateDevice(netid
[0], this));
2715 rv
.sort(deviceSort
);
2721 * Checks whether this logical interface contains the given device
2724 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2725 * The object or device name to check. In case the given argument is not
2726 * a string, it is resolved though the
2727 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2729 * @returns {boolean}
2730 * Returns `true` when this logical interface contains the given network
2731 * device or `false` if not.
2733 containsDevice: function(ifname
) {
2734 ifname
= ifnameOf(ifname
);
2738 else if (this.isVirtual() && '%s-%s'.format(this.getProtocol(), this.sid
) == ifname
)
2740 else if (this.isBridge() && 'br-%s'.format(this.sid
) == ifname
)
2743 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'ifname'));
2745 for (var i
= 0; i
< ifnames
.length
; i
++) {
2746 var m
= ifnames
[i
].match(/^([^:/]+)/);
2747 if (m
!= null && m
[1] == ifname
)
2751 var wif
= getWifiSidByIfname(ifname
);
2754 var networks
= L
.toArray(uci
.get('wireless', wif
, 'network'));
2756 for (var i
= 0; i
< networks
.length
; i
++)
2757 if (networks
[i
] == this.sid
)
2765 * Cleanup related configuration entries.
2767 * This function will be invoked if an interface is about to be removed
2768 * from the configuration and is responsible for performing any required
2769 * cleanup tasks, such as unsetting uci entries in related configurations.
2771 * It should be overwritten by protocol specific subclasses.
2775 * @returns {*|Promise<*>}
2776 * This function may return a promise which is awaited before the rest of
2777 * the configuration is removed. Any non-promise return value and any
2778 * resolved promise value is ignored. If the returned promise is rejected,
2779 * the interface removal will be aborted.
2781 deleteConfiguration: function() {}
2786 * @memberof LuCI.network
2790 * A `Network.Device` class instance represents an underlying Linux network
2791 * device and allows querying device details such as packet statistics or MTU.
2793 Device
= baseclass
.extend(/** @lends LuCI.network.Device.prototype */ {
2794 __init__: function(ifname
, network
) {
2795 var wif
= getWifiSidByIfname(ifname
);
2798 var res
= getWifiStateBySid(wif
) || [],
2799 netid
= getWifiNetidBySid(wif
) || [];
2801 this.wif
= new WifiNetwork(wif
, res
[0], res
[1], netid
[0], res
[2], { ifname
: ifname
});
2802 this.ifname
= this.wif
.getIfname();
2805 this.ifname
= this.ifname
|| ifname
;
2806 this.dev
= Object
.assign({}, _state
.netdevs
[this.ifname
]);
2807 this.network
= network
;
2810 _devstate: function(/* ... */) {
2813 for (var i
= 0; i
< arguments
.length
; i
++)
2815 rv
= rv
[arguments
[i
]];
2823 * Get the name of the network device.
2826 * Returns the name of the device, e.g. `eth0` or `wlan0`.
2828 getName: function() {
2829 return (this.wif
!= null ? this.wif
.getIfname() : this.ifname
);
2833 * Get the MAC address of the device.
2835 * @returns {null|string}
2836 * Returns the MAC address of the device or `null` if not applicable,
2837 * e.g. for non-ethernet tunnel devices.
2839 getMAC: function() {
2840 var mac
= this._devstate('macaddr');
2841 return mac
? mac
.toUpperCase() : null;
2845 * Get the MTU of the device.
2848 * Returns the MTU of the device.
2850 getMTU: function() {
2851 return this._devstate('mtu');
2855 * Get the IPv4 addresses configured on the device.
2857 * @returns {string[]}
2858 * Returns an array of IPv4 address strings.
2860 getIPAddrs: function() {
2861 var addrs
= this._devstate('ipaddrs');
2862 return (Array
.isArray(addrs
) ? addrs
: []);
2866 * Get the IPv6 addresses configured on the device.
2868 * @returns {string[]}
2869 * Returns an array of IPv6 address strings.
2871 getIP6Addrs: function() {
2872 var addrs
= this._devstate('ip6addrs');
2873 return (Array
.isArray(addrs
) ? addrs
: []);
2877 * Get the type of the device.
2880 * Returns a string describing the type of the network device:
2881 * - `alias` if it is an abstract alias device (`@` notation)
2882 * - `wifi` if it is a wireless interface (e.g. `wlan0`)
2883 * - `bridge` if it is a bridge device (e.g. `br-lan`)
2884 * - `tunnel` if it is a tun or tap device (e.g. `tun0`)
2885 * - `vlan` if it is a vlan device (e.g. `eth0.1`)
2886 * - `switch` if it is a switch device (e.g.`eth1` connected to switch0)
2887 * - `ethernet` for all other device types
2889 getType: function() {
2890 if (this.ifname
!= null && this.ifname
.charAt(0) == '@')
2892 else if (this.dev
.devtype
== 'wlan' || this.wif
!= null || isWifiIfname(this.ifname
))
2894 else if (this.dev
.devtype
== 'bridge' || _state
.isBridge
[this.ifname
])
2896 else if (_state
.isTunnel
[this.ifname
])
2898 else if (this.dev
.devtype
== 'vlan' || this.ifname
.indexOf('.') > -1)
2900 else if (this.dev
.devtype
== 'dsa' || _state
.isSwitch
[this.ifname
])
2907 * Get a short description string for the device.
2910 * Returns the device name for non-wifi devices or a string containing
2911 * the operation mode and SSID for wifi devices.
2913 getShortName: function() {
2914 if (this.wif
!= null)
2915 return this.wif
.getShortName();
2921 * Get a long description string for the device.
2924 * Returns a string containing the type description and device name
2925 * for non-wifi devices or operation mode and ssid for wifi ones.
2927 getI18n: function() {
2928 if (this.wif
!= null) {
2929 return '%s: %s "%s"'.format(
2930 _('Wireless Network'),
2931 this.wif
.getActiveMode(),
2932 this.wif
.getActiveSSID() || this.wif
.getActiveBSSID() || this.wif
.getID() || '?');
2935 return '%s: "%s"'.format(this.getTypeI18n(), this.getName());
2939 * Get a string describing the device type.
2942 * Returns a string describing the type, e.g. "Wireless Adapter" or
2945 getTypeI18n: function() {
2946 switch (this.getType()) {
2948 return _('Alias Interface');
2951 return _('Wireless Adapter');
2957 return (_state
.netdevs
[this.ifname
] && _state
.netdevs
[this.ifname
].devtype
== 'dsa')
2958 ? _('Switch port') : _('Ethernet Switch');
2961 return (_state
.isSwitch
[this.ifname
] ? _('Switch VLAN') : _('Software VLAN'));
2964 return _('Tunnel Interface');
2967 return _('Ethernet Adapter');
2972 * Get the associated bridge ports of the device.
2974 * @returns {null|Array<LuCI.network.Device>}
2975 * Returns an array of `Network.Device` instances representing the ports
2976 * (slave interfaces) of the bridge or `null` when this device isn't
2979 getPorts: function() {
2980 var br
= _state
.bridges
[this.ifname
],
2983 if (br
== null || !Array
.isArray(br
.ifnames
))
2986 for (var i
= 0; i
< br
.ifnames
.length
; i
++)
2987 rv
.push(Network
.prototype.instantiateDevice(br
.ifnames
[i
].name
));
2989 rv
.sort(deviceSort
);
2997 * @returns {null|string}
2998 * Returns the ID of this network bridge or `null` if this network
2999 * device is not a Linux bridge.
3001 getBridgeID: function() {
3002 var br
= _state
.bridges
[this.ifname
];
3003 return (br
!= null ? br
.id
: null);
3007 * Get the bridge STP setting
3009 * @returns {boolean}
3010 * Returns `true` when this device is a Linux bridge and has `stp`
3011 * enabled, else `false`.
3013 getBridgeSTP: function() {
3014 var br
= _state
.bridges
[this.ifname
];
3015 return (br
!= null ? !!br
.stp
: false);
3019 * Checks whether this device is up.
3021 * @returns {boolean}
3022 * Returns `true` when the associated device is running pr `false`
3023 * when it is down or absent.
3026 var up
= this._devstate('flags', 'up');
3029 up
= (this.getType() == 'alias');
3035 * Checks whether this device is a Linux bridge.
3037 * @returns {boolean}
3038 * Returns `true` when the network device is present and a Linux bridge,
3041 isBridge: function() {
3042 return (this.getType() == 'bridge');
3046 * Checks whether this device is part of a Linux bridge.
3048 * @returns {boolean}
3049 * Returns `true` when this network device is part of a bridge,
3052 isBridgePort: function() {
3053 return (this._devstate('bridge') != null);
3057 * Get the amount of transmitted bytes.
3060 * Returns the amount of bytes transmitted by the network device.
3062 getTXBytes: function() {
3063 var stat
= this._devstate('stats');
3064 return (stat
!= null ? stat
.tx_bytes
|| 0 : 0);
3068 * Get the amount of received bytes.
3071 * Returns the amount of bytes received by the network device.
3073 getRXBytes: function() {
3074 var stat
= this._devstate('stats');
3075 return (stat
!= null ? stat
.rx_bytes
|| 0 : 0);
3079 * Get the amount of transmitted packets.
3082 * Returns the amount of packets transmitted by the network device.
3084 getTXPackets: function() {
3085 var stat
= this._devstate('stats');
3086 return (stat
!= null ? stat
.tx_packets
|| 0 : 0);
3090 * Get the amount of received packets.
3093 * Returns the amount of packets received by the network device.
3095 getRXPackets: function() {
3096 var stat
= this._devstate('stats');
3097 return (stat
!= null ? stat
.rx_packets
|| 0 : 0);
3101 * Get the primary logical interface this device is assigned to.
3103 * @returns {null|LuCI.network.Protocol}
3104 * Returns a `Network.Protocol` instance representing the logical
3105 * interface this device is attached to or `null` if it is not
3106 * assigned to any logical interface.
3108 getNetwork: function() {
3109 return this.getNetworks()[0];
3113 * Get the logical interfaces this device is assigned to.
3115 * @returns {Array<LuCI.network.Protocol>}
3116 * Returns an array of `Network.Protocol` instances representing the
3117 * logical interfaces this device is assigned to.
3119 getNetworks: function() {
3120 if (this.networks
== null) {
3123 var networks
= enumerateNetworks
.apply(L
.network
);
3125 for (var i
= 0; i
< networks
.length
; i
++)
3126 if (networks
[i
].containsDevice(this.ifname
) || networks
[i
].getIfname() == this.ifname
)
3127 this.networks
.push(networks
[i
]);
3129 this.networks
.sort(networkSort
);
3132 return this.networks
;
3136 * Get the related wireless network this device is related to.
3138 * @returns {null|LuCI.network.WifiNetwork}
3139 * Returns a `Network.WifiNetwork` instance representing the wireless
3140 * network corresponding to this network device or `null` if this device
3141 * is not a wireless device.
3143 getWifiNetwork: function() {
3144 return (this.wif
!= null ? this.wif
: null);
3148 * Get the logical parent device of this device.
3150 * In case of DSA switch ports, the parent device will be the DSA switch
3151 * device itself, for VLAN devices, the parent refers to the base device
3154 * @returns {null|LuCI.network.Device}
3155 * Returns a `Network.Device` instance representing the parent device or
3156 * `null` when this device has no parent, as it is the case for e.g.
3157 * ordinary ethernet interfaces.
3159 getParent: function() {
3160 return this.dev
.parent
? Network
.prototype.instantiateDevice(this.dev
.parent
) : null;
3166 * @memberof LuCI.network
3170 * A `Network.WifiDevice` class instance represents a wireless radio device
3171 * present on the system and provides wireless capability information as
3172 * well as methods for enumerating related wireless networks.
3174 WifiDevice
= baseclass
.extend(/** @lends LuCI.network.WifiDevice.prototype */ {
3175 __init__: function(name
, radiostate
) {
3176 var uciWifiDevice
= uci
.get('wireless', name
);
3178 if (uciWifiDevice
!= null &&
3179 uciWifiDevice
['.type'] == 'wifi-device' &&
3180 uciWifiDevice
['.name'] != null) {
3181 this.sid
= uciWifiDevice
['.name'];
3184 this.sid
= this.sid
|| name
;
3192 ubus: function(/* ... */) {
3193 var v
= this._ubusdata
;
3195 for (var i
= 0; i
< arguments
.length
; i
++)
3197 v
= v
[arguments
[i
]];
3205 * Read the given UCI option value of this wireless device.
3207 * @param {string} opt
3208 * The UCI option name to read.
3210 * @returns {null|string|string[]}
3211 * Returns the UCI option value or `null` if the requested option is
3214 get: function(opt
) {
3215 return uci
.get('wireless', this.sid
, opt
);
3219 * Set the given UCI option of this network to the given value.
3221 * @param {string} opt
3222 * The name of the UCI option to set.
3224 * @param {null|string|string[]} val
3225 * The value to set or `null` to remove the given option from the
3228 set: function(opt
, value
) {
3229 return uci
.set('wireless', this.sid
, opt
, value
);
3233 * Checks whether this wireless radio is disabled.
3235 * @returns {boolean}
3236 * Returns `true` when the wireless radio is marked as disabled in `ubus`
3237 * runtime state or when the `disabled` option is set in the corresponding
3238 * UCI configuration.
3240 isDisabled: function() {
3241 return this.ubus('dev', 'disabled') || this.get('disabled') == '1';
3245 * Get the configuration name of this wireless radio.
3248 * Returns the UCI section name (e.g. `radio0`) of the corresponding
3249 * radio configuration which also serves as unique logical identifier
3250 * for the wireless phy.
3252 getName: function() {
3257 * Gets a list of supported hwmodes.
3259 * The hwmode values describe the frequency band and wireless standard
3260 * versions supported by the wireless phy.
3262 * @returns {string[]}
3263 * Returns an array of valid hwmode values for this radio. Currently
3264 * known mode values are:
3265 * - `a` - Legacy 802.11a mode, 5 GHz, up to 54 Mbit/s
3266 * - `b` - Legacy 802.11b mode, 2.4 GHz, up to 11 Mbit/s
3267 * - `g` - Legacy 802.11g mode, 2.4 GHz, up to 54 Mbit/s
3268 * - `n` - IEEE 802.11n mode, 2.4 or 5 GHz, up to 600 Mbit/s
3269 * - `ac` - IEEE 802.11ac mode, 5 GHz, up to 6770 Mbit/s
3270 * - `ax` - IEEE 802.11ax mode, 2.4 or 5 GHz
3272 getHWModes: function() {
3273 var hwmodes
= this.ubus('dev', 'iwinfo', 'hwmodes');
3274 return Array
.isArray(hwmodes
) ? hwmodes
: [ 'b', 'g' ];
3278 * Gets a list of supported htmodes.
3280 * The htmode values describe the wide-frequency options supported by
3283 * @returns {string[]}
3284 * Returns an array of valid htmode values for this radio. Currently
3285 * known mode values are:
3286 * - `HT20` - applicable to IEEE 802.11n, 20 MHz wide channels
3287 * - `HT40` - applicable to IEEE 802.11n, 40 MHz wide channels
3288 * - `VHT20` - applicable to IEEE 802.11ac, 20 MHz wide channels
3289 * - `VHT40` - applicable to IEEE 802.11ac, 40 MHz wide channels
3290 * - `VHT80` - applicable to IEEE 802.11ac, 80 MHz wide channels
3291 * - `VHT160` - applicable to IEEE 802.11ac, 160 MHz wide channels
3292 * - `HE20` - applicable to IEEE 802.11ax, 20 MHz wide channels
3293 * - `HE40` - applicable to IEEE 802.11ax, 40 MHz wide channels
3294 * - `HE80` - applicable to IEEE 802.11ax, 80 MHz wide channels
3295 * - `HE160` - applicable to IEEE 802.11ax, 160 MHz wide channels
3297 getHTModes: function() {
3298 var htmodes
= this.ubus('dev', 'iwinfo', 'htmodes');
3299 return (Array
.isArray(htmodes
) && htmodes
.length
) ? htmodes
: null;
3303 * Get a string describing the wireless radio hardware.
3306 * Returns the description string.
3308 getI18n: function() {
3309 var hw
= this.ubus('dev', 'iwinfo', 'hardware'),
3310 type
= L
.isObject(hw
) ? hw
.name
: null;
3312 if (this.ubus('dev', 'iwinfo', 'type') == 'wl')
3315 var hwmodes
= this.getHWModes(),
3318 hwmodes
.sort(function(a
, b
) {
3319 return (a
.length
!= b
.length
? a
.length
> b
.length
: a
> b
);
3322 modestr
= hwmodes
.join('');
3324 return '%s 802.11%s Wireless Controller (%s)'.format(type
|| 'Generic', modestr
, this.getName());
3328 * A wireless scan result object describes a neighbouring wireless
3329 * network found in the vincinity.
3331 * @typedef {Object<string, number|string|LuCI.network.WifiEncryption>} WifiScanResult
3332 * @memberof LuCI.network
3334 * @property {string} ssid
3335 * The SSID / Mesh ID of the network.
3337 * @property {string} bssid
3338 * The BSSID if the network.
3340 * @property {string} mode
3341 * The operation mode of the network (`Master`, `Ad-Hoc`, `Mesh Point`).
3343 * @property {number} channel
3344 * The wireless channel of the network.
3346 * @property {number} signal
3347 * The received signal strength of the network in dBm.
3349 * @property {number} quality
3350 * The numeric quality level of the signal, can be used in conjunction
3351 * with `quality_max` to calculate a quality percentage.
3353 * @property {number} quality_max
3354 * The maximum possible quality level of the signal, can be used in
3355 * conjunction with `quality` to calculate a quality percentage.
3357 * @property {LuCI.network.WifiEncryption} encryption
3358 * The encryption used by the wireless network.
3362 * Trigger a wireless scan on this radio device and obtain a list of
3365 * @returns {Promise<Array<LuCI.network.WifiScanResult>>}
3366 * Returns a promise resolving to an array of scan result objects
3367 * describing the networks found in the vincinity.
3369 getScanList: function() {
3370 return callIwinfoScan(this.sid
);
3374 * Check whether the wireless radio is marked as up in the `ubus`
3377 * @returns {boolean}
3378 * Returns `true` when the radio device is up, else `false`.
3381 if (L
.isObject(_state
.radios
[this.sid
]))
3382 return (_state
.radios
[this.sid
].up
== true);
3388 * Get the wifi network of the given name belonging to this radio device
3390 * @param {string} network
3391 * The name of the wireless network to lookup. This may be either an uci
3392 * configuration section ID, a network ID in the form `radio#.network#`
3393 * or a Linux network device name like `wlan0` which is resolved to the
3394 * corresponding configuration section through `ubus` runtime information.
3396 * @returns {Promise<LuCI.network.WifiNetwork>}
3397 * Returns a promise resolving to a `Network.WifiNetwork` instance
3398 * representing the wireless network and rejecting with `null` if
3399 * the given network could not be found or is not associated with
3400 * this radio device.
3402 getWifiNetwork: function(network
) {
3403 return Network
.prototype.getWifiNetwork(network
).then(L
.bind(function(networkInstance
) {
3404 var uciWifiIface
= (networkInstance
.sid
? uci
.get('wireless', networkInstance
.sid
) : null);
3406 if (uciWifiIface
== null || uciWifiIface
['.type'] != 'wifi-iface' || uciWifiIface
.device
!= this.sid
)
3407 return Promise
.reject();
3409 return networkInstance
;
3414 * Get all wireless networks associated with this wireless radio device.
3416 * @returns {Promise<Array<LuCI.network.WifiNetwork>>}
3417 * Returns a promise resolving to an array of `Network.WifiNetwork`
3418 * instances respresenting the wireless networks associated with this
3421 getWifiNetworks: function() {
3422 return Network
.prototype.getWifiNetworks().then(L
.bind(function(networks
) {
3425 for (var i
= 0; i
< networks
.length
; i
++)
3426 if (networks
[i
].getWifiDeviceName() == this.getName())
3427 rv
.push(networks
[i
]);
3434 * Adds a new wireless network associated with this radio device to the
3435 * configuration and sets its options to the provided values.
3437 * @param {Object<string, string|string[]>} [options]
3438 * The options to set for the newly added wireless network.
3440 * @returns {Promise<null|LuCI.network.WifiNetwork>}
3441 * Returns a promise resolving to a `WifiNetwork` instance describing
3442 * the newly added wireless network or `null` if the given options
3445 addWifiNetwork: function(options
) {
3446 if (!L
.isObject(options
))
3449 options
.device
= this.sid
;
3451 return Network
.prototype.addWifiNetwork(options
);
3455 * Deletes the wireless network with the given name associated with this
3458 * @param {string} network
3459 * The name of the wireless network to lookup. This may be either an uci
3460 * configuration section ID, a network ID in the form `radio#.network#`
3461 * or a Linux network device name like `wlan0` which is resolved to the
3462 * corresponding configuration section through `ubus` runtime information.
3464 * @returns {Promise<boolean>}
3465 * Returns a promise resolving to `true` when the wireless network was
3466 * successfully deleted from the configuration or `false` when the given
3467 * network could not be found or if the found network was not associated
3468 * with this wireless radio device.
3470 deleteWifiNetwork: function(network
) {
3473 if (network
instanceof WifiNetwork
) {
3477 var uciWifiIface
= uci
.get('wireless', network
);
3479 if (uciWifiIface
== null || uciWifiIface
['.type'] != 'wifi-iface')
3480 sid
= getWifiSidByIfname(network
);
3483 if (sid
== null || uci
.get('wireless', sid
, 'device') != this.sid
)
3484 return Promise
.resolve(false);
3486 uci
.delete('wireless', network
);
3488 return Promise
.resolve(true);
3494 * @memberof LuCI.network
3498 * A `Network.WifiNetwork` instance represents a wireless network (vif)
3499 * configured on top of a radio device and provides functions for querying
3500 * the runtime state of the network. Most radio devices support multiple
3501 * such networks in parallel.
3503 WifiNetwork
= baseclass
.extend(/** @lends LuCI.network.WifiNetwork.prototype */ {
3504 __init__: function(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
) {
3515 ubus: function(/* ... */) {
3516 var v
= this._ubusdata
;
3518 for (var i
= 0; i
< arguments
.length
; i
++)
3520 v
= v
[arguments
[i
]];
3528 * Read the given UCI option value of this wireless network.
3530 * @param {string} opt
3531 * The UCI option name to read.
3533 * @returns {null|string|string[]}
3534 * Returns the UCI option value or `null` if the requested option is
3537 get: function(opt
) {
3538 return uci
.get('wireless', this.sid
, opt
);
3542 * Set the given UCI option of this network to the given value.
3544 * @param {string} opt
3545 * The name of the UCI option to set.
3547 * @param {null|string|string[]} val
3548 * The value to set or `null` to remove the given option from the
3551 set: function(opt
, value
) {
3552 return uci
.set('wireless', this.sid
, opt
, value
);
3556 * Checks whether this wireless network is disabled.
3558 * @returns {boolean}
3559 * Returns `true` when the wireless radio is marked as disabled in `ubus`
3560 * runtime state or when the `disabled` option is set in the corresponding
3561 * UCI configuration.
3563 isDisabled: function() {
3564 return this.ubus('dev', 'disabled') || this.get('disabled') == '1';
3568 * Get the configured operation mode of the wireless network.
3571 * Returns the configured operation mode. Possible values are:
3572 * - `ap` - Master (Access Point) mode
3573 * - `sta` - Station (client) mode
3574 * - `adhoc` - Ad-Hoc (IBSS) mode
3575 * - `mesh` - Mesh (IEEE 802.11s) mode
3576 * - `monitor` - Monitor mode
3578 getMode: function() {
3579 return this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3583 * Get the configured SSID of the wireless network.
3585 * @returns {null|string}
3586 * Returns the configured SSID value or `null` when this network is
3589 getSSID: function() {
3590 if (this.getMode() == 'mesh')
3593 return this.ubus('net', 'config', 'ssid') || this.get('ssid');
3597 * Get the configured Mesh ID of the wireless network.
3599 * @returns {null|string}
3600 * Returns the configured mesh ID value or `null` when this network
3601 * is not in mesh mode.
3603 getMeshID: function() {
3604 if (this.getMode() != 'mesh')
3607 return this.ubus('net', 'config', 'mesh_id') || this.get('mesh_id');
3611 * Get the configured BSSID of the wireless network.
3613 * @returns {null|string}
3614 * Returns the BSSID value or `null` if none has been specified.
3616 getBSSID: function() {
3617 return this.ubus('net', 'config', 'bssid') || this.get('bssid');
3621 * Get the names of the logical interfaces this wireless network is
3624 * @returns {string[]}
3625 * Returns an array of logical interface names.
3627 getNetworkNames: function() {
3628 return L
.toArray(this.ubus('net', 'config', 'network') || this.get('network'));
3632 * Get the internal network ID of this wireless network.
3634 * The network ID is a LuCI specific identifer in the form
3635 * `radio#.network#` to identify wireless networks by their corresponding
3636 * radio and network index numbers.
3639 * Returns the LuCI specific network ID.
3646 * Get the configuration ID of this wireless network.
3649 * Returns the corresponding UCI section ID of the network.
3651 getName: function() {
3656 * Get the Linux network device name.
3658 * @returns {null|string}
3659 * Returns the current Linux network device name as resolved from
3660 * `ubus` runtime information or `null` if this network has no
3661 * associated network device, e.g. when not configured or up.
3663 getIfname: function() {
3664 var ifname
= this.ubus('net', 'ifname') || this.ubus('net', 'iwinfo', 'ifname');
3666 if (ifname
== null || ifname
.match(/^(wifi|radio)\d/))
3667 ifname
= this.netid
;
3673 * Get the Linux VLAN network device names.
3675 * @returns {string[]}
3676 * Returns the current Linux VLAN network device name as resolved
3677 * from `ubus` runtime information or empty array if this network
3678 * has no associated VLAN network devices.
3680 getVlanIfnames: function() {
3681 var vlans
= L
.toArray(this.ubus('net', 'vlans')),
3684 for (var i
= 0; i
< vlans
.length
; i
++)
3685 ifnames
.push(vlans
[i
]['ifname']);
3691 * Get the name of the corresponding wifi radio device.
3693 * @returns {null|string}
3694 * Returns the name of the radio device this network is configured on
3695 * or `null` if it cannot be determined.
3697 getWifiDeviceName: function() {
3698 return this.ubus('radio') || this.get('device');
3702 * Get the corresponding wifi radio device.
3704 * @returns {null|LuCI.network.WifiDevice}
3705 * Returns a `Network.WifiDevice` instance representing the corresponding
3706 * wifi radio device or `null` if the related radio device could not be
3709 getWifiDevice: function() {
3710 var radioname
= this.getWifiDeviceName();
3712 if (radioname
== null)
3713 return Promise
.reject();
3715 return Network
.prototype.getWifiDevice(radioname
);
3719 * Check whether the radio network is up.
3721 * This function actually queries the up state of the related radio
3722 * device and assumes this network to be up as well when the parent
3723 * radio is up. This is due to the fact that OpenWrt does not control
3724 * virtual interfaces individually but within one common hostapd
3727 * @returns {boolean}
3728 * Returns `true` when the network is up, else `false`.
3731 var device
= this.getDevice();
3736 return device
.isUp();
3740 * Query the current operation mode from runtime information.
3743 * Returns the human readable mode name as reported by `ubus` runtime
3744 * state. Possible returned values are:
3756 getActiveMode: function() {
3757 var mode
= this.ubus('net', 'iwinfo', 'mode') || this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3760 case 'ap': return 'Master';
3761 case 'sta': return 'Client';
3762 case 'adhoc': return 'Ad-Hoc';
3763 case 'mesh': return 'Mesh';
3764 case 'monitor': return 'Monitor';
3765 default: return mode
;
3770 * Query the current operation mode from runtime information as
3771 * translated string.
3774 * Returns the translated, human readable mode name as reported by
3775 *`ubus` runtime state.
3777 getActiveModeI18n: function() {
3778 var mode
= this.getActiveMode();
3781 case 'Master': return _('Master');
3782 case 'Client': return _('Client');
3783 case 'Ad-Hoc': return _('Ad-Hoc');
3784 case 'Mash': return _('Mesh');
3785 case 'Monitor': return _('Monitor');
3786 default: return mode
;
3791 * Query the current SSID from runtime information.
3794 * Returns the current SSID or Mesh ID as reported by `ubus` runtime
3797 getActiveSSID: function() {
3798 return this.ubus('net', 'iwinfo', 'ssid') || this.ubus('net', 'config', 'ssid') || this.get('ssid');
3802 * Query the current BSSID from runtime information.
3805 * Returns the current BSSID or Mesh ID as reported by `ubus` runtime
3808 getActiveBSSID: function() {
3809 return this.ubus('net', 'iwinfo', 'bssid') || this.ubus('net', 'config', 'bssid') || this.get('bssid');
3813 * Query the current encryption settings from runtime information.
3816 * Returns a string describing the current encryption or `-` if the the
3817 * encryption state could not be found in `ubus` runtime information.
3819 getActiveEncryption: function() {
3820 return formatWifiEncryption(this.ubus('net', 'iwinfo', 'encryption')) || '-';
3824 * A wireless peer entry describes the properties of a remote wireless
3825 * peer associated with a local network.
3827 * @typedef {Object<string, boolean|number|string|LuCI.network.WifiRateEntry>} WifiPeerEntry
3828 * @memberof LuCI.network
3830 * @property {string} mac
3831 * The MAC address (BSSID).
3833 * @property {number} signal
3834 * The received signal strength.
3836 * @property {number} [signal_avg]
3837 * The average signal strength if supported by the driver.
3839 * @property {number} [noise]
3840 * The current noise floor of the radio. May be `0` or absent if not
3841 * supported by the driver.
3843 * @property {number} inactive
3844 * The amount of milliseconds the peer has been inactive, e.g. due
3847 * @property {number} connected_time
3848 * The amount of milliseconds the peer is associated to this network.
3850 * @property {number} [thr]
3851 * The estimated throughput of the peer, May be `0` or absent if not
3852 * supported by the driver.
3854 * @property {boolean} authorized
3855 * Specifies whether the peer is authorized to associate to this network.
3857 * @property {boolean} authenticated
3858 * Specifies whether the peer completed authentication to this network.
3860 * @property {string} preamble
3861 * The preamble mode used by the peer. May be `long` or `short`.
3863 * @property {boolean} wme
3864 * Specifies whether the peer supports WME/WMM capabilities.
3866 * @property {boolean} mfp
3867 * Specifies whether management frame protection is active.
3869 * @property {boolean} tdls
3870 * Specifies whether TDLS is active.
3872 * @property {number} [mesh llid]
3873 * The mesh LLID, may be `0` or absent if not applicable or supported
3876 * @property {number} [mesh plid]
3877 * The mesh PLID, may be `0` or absent if not applicable or supported
3880 * @property {string} [mesh plink]
3881 * The mesh peer link state description, may be an empty string (`''`)
3882 * or absent if not applicable or supported by the driver.
3884 * The following states are known:
3894 * @property {number} [mesh local PS]
3895 * The local powersafe mode for the peer link, may be an empty
3896 * string (`''`) or absent if not applicable or supported by
3899 * The following modes are known:
3900 * - `ACTIVE` (no power save)
3905 * @property {number} [mesh peer PS]
3906 * The remote powersafe mode for the peer link, may be an empty
3907 * string (`''`) or absent if not applicable or supported by
3910 * The following modes are known:
3911 * - `ACTIVE` (no power save)
3916 * @property {number} [mesh non-peer PS]
3917 * The powersafe mode for all non-peer neigbours, may be an empty
3918 * string (`''`) or absent if not applicable or supported by the driver.
3920 * The following modes are known:
3921 * - `ACTIVE` (no power save)
3926 * @property {LuCI.network.WifiRateEntry} rx
3927 * Describes the receiving wireless rate from the peer.
3929 * @property {LuCI.network.WifiRateEntry} tx
3930 * Describes the transmitting wireless rate to the peer.
3934 * A wireless rate entry describes the properties of a wireless
3935 * transmission rate to or from a peer.
3937 * @typedef {Object<string, boolean|number>} WifiRateEntry
3938 * @memberof LuCI.network
3940 * @property {number} [drop_misc]
3941 * The amount of received misc. packages that have been dropped, e.g.
3942 * due to corruption or missing authentication. Only applicable to
3945 * @property {number} packets
3946 * The amount of packets that have been received or sent.
3948 * @property {number} bytes
3949 * The amount of bytes that have been received or sent.
3951 * @property {number} [failed]
3952 * The amount of failed tranmission attempts. Only applicable to
3955 * @property {number} [retries]
3956 * The amount of retried transmissions. Only applicable to transmit
3959 * @property {boolean} is_ht
3960 * Specifies whether this rate is an HT (IEEE 802.11n) rate.
3962 * @property {boolean} is_vht
3963 * Specifies whether this rate is an VHT (IEEE 802.11ac) rate.
3965 * @property {number} mhz
3966 * The channel width in MHz used for the transmission.
3968 * @property {number} rate
3969 * The bitrate in bit/s of the transmission.
3971 * @property {number} [mcs]
3972 * The MCS index of the used transmission rate. Only applicable to
3975 * @property {number} [40mhz]
3976 * Specifies whether the tranmission rate used 40MHz wide channel.
3977 * Only applicable to HT or VHT rates.
3979 * Note: this option exists for backwards compatibility only and its
3980 * use is discouraged. The `mhz` field should be used instead to
3981 * determine the channel width.
3983 * @property {boolean} [short_gi]
3984 * Specifies whether a short guard interval is used for the transmission.
3985 * Only applicable to HT or VHT rates.
3987 * @property {number} [nss]
3988 * Specifies the number of spatial streams used by the transmission.
3989 * Only applicable to VHT rates.
3991 * @property {boolean} [he]
3992 * Specifies whether this rate is an HE (IEEE 802.11ax) rate.
3994 * @property {number} [he_gi]
3995 * Specifies whether the guard interval used for the transmission.
3996 * Only applicable to HE rates.
3998 * @property {number} [he_dcm]
3999 * Specifies whether dual concurrent modulation is used for the transmission.
4000 * Only applicable to HE rates.
4004 * Fetch the list of associated peers.
4006 * @returns {Promise<Array<LuCI.network.WifiPeerEntry>>}
4007 * Returns a promise resolving to an array of wireless peers associated
4008 * with this network.
4010 getAssocList: function() {
4012 var ifnames
= [ this.getIfname() ].concat(this.getVlanIfnames());
4014 for (var i
= 0; i
< ifnames
.length
; i
++)
4015 tasks
.push(callIwinfoAssoclist(ifnames
[i
]));
4017 return Promise
.all(tasks
).then(function(values
) {
4018 return Array
.prototype.concat
.apply([], values
);
4023 * Query the current operating frequency of the wireless network.
4025 * @returns {null|string}
4026 * Returns the current operating frequency of the network from `ubus`
4027 * runtime information in GHz or `null` if the information is not
4030 getFrequency: function() {
4031 var freq
= this.ubus('net', 'iwinfo', 'frequency');
4033 if (freq
!= null && freq
> 0)
4034 return '%.03f'.format(freq
/ 1000);
4040 * Query the current average bitrate of all peers associated to this
4043 * @returns {null|number}
4044 * Returns the average bit rate among all peers associated to the network
4045 * as reported by `ubus` runtime information or `null` if the information
4048 getBitRate: function() {
4049 var rate
= this.ubus('net', 'iwinfo', 'bitrate');
4051 if (rate
!= null && rate
> 0)
4052 return (rate
/ 1000);
4058 * Query the current wireless channel.
4060 * @returns {null|number}
4061 * Returns the wireless channel as reported by `ubus` runtime information
4062 * or `null` if it cannot be determined.
4064 getChannel: function() {
4065 return this.ubus('net', 'iwinfo', 'channel') || this.ubus('dev', 'config', 'channel') || this.get('channel');
4069 * Query the current wireless signal.
4071 * @returns {null|number}
4072 * Returns the wireless signal in dBm as reported by `ubus` runtime
4073 * information or `null` if it cannot be determined.
4075 getSignal: function() {
4076 return this.ubus('net', 'iwinfo', 'signal') || 0;
4080 * Query the current radio noise floor.
4083 * Returns the radio noise floor in dBm as reported by `ubus` runtime
4084 * information or `0` if it cannot be determined.
4086 getNoise: function() {
4087 return this.ubus('net', 'iwinfo', 'noise') || 0;
4091 * Query the current country code.
4094 * Returns the wireless country code as reported by `ubus` runtime
4095 * information or `00` if it cannot be determined.
4097 getCountryCode: function() {
4098 return this.ubus('net', 'iwinfo', 'country') || this.ubus('dev', 'config', 'country') || '00';
4102 * Query the current radio TX power.
4104 * @returns {null|number}
4105 * Returns the wireless network transmit power in dBm as reported by
4106 * `ubus` runtime information or `null` if it cannot be determined.
4108 getTXPower: function() {
4109 return this.ubus('net', 'iwinfo', 'txpower');
4113 * Query the radio TX power offset.
4115 * Some wireless radios have a fixed power offset, e.g. due to the
4116 * use of external amplifiers.
4119 * Returns the wireless network transmit power offset in dBm as reported
4120 * by `ubus` runtime information or `0` if there is no offset, or if it
4121 * cannot be determined.
4123 getTXPowerOffset: function() {
4124 return this.ubus('net', 'iwinfo', 'txpower_offset') || 0;
4128 * Calculate the current signal.
4132 * Returns the calculated signal level, which is the difference between
4133 * noise and signal (SNR), divided by 5.
4135 getSignalLevel: function(signal
, noise
) {
4136 if (this.getActiveBSSID() == '00:00:00:00:00:00')
4139 signal
= signal
|| this.getSignal();
4140 noise
= noise
|| this.getNoise();
4142 if (signal
< 0 && noise
< 0) {
4143 var snr
= -1 * (noise
- signal
);
4144 return Math
.floor(snr
/ 5);
4151 * Calculate the current signal quality percentage.
4154 * Returns the calculated signal quality in percent. The value is
4155 * calculated from the `quality` and `quality_max` indicators reported
4156 * by `ubus` runtime state.
4158 getSignalPercent: function() {
4159 var qc
= this.ubus('net', 'iwinfo', 'quality') || 0,
4160 qm
= this.ubus('net', 'iwinfo', 'quality_max') || 0;
4162 if (qc
> 0 && qm
> 0)
4163 return Math
.floor((100 / qm
) * qc
);
4169 * Get a short description string for this wireless network.
4172 * Returns a string describing this network, consisting of the
4173 * active operation mode, followed by either the SSID, BSSID or
4174 * internal network ID, depending on which information is available.
4176 getShortName: function() {
4177 return '%s "%s"'.format(
4178 this.getActiveModeI18n(),
4179 this.getActiveSSID() || this.getActiveBSSID() || this.getID());
4183 * Get a description string for this wireless network.
4186 * Returns a string describing this network, consisting of the
4187 * term `Wireless Network`, followed by the active operation mode,
4188 * the SSID, BSSID or internal network ID and the Linux network device
4189 * name, depending on which information is available.
4191 getI18n: function() {
4192 return '%s: %s "%s" (%s)'.format(
4193 _('Wireless Network'),
4194 this.getActiveModeI18n(),
4195 this.getActiveSSID() || this.getActiveBSSID() || this.getID(),
4200 * Get the primary logical interface this wireless network is attached to.
4202 * @returns {null|LuCI.network.Protocol}
4203 * Returns a `Network.Protocol` instance representing the logical
4204 * interface or `null` if this network is not attached to any logical
4207 getNetwork: function() {
4208 return this.getNetworks()[0];
4212 * Get the logical interfaces this wireless network is attached to.
4214 * @returns {Array<LuCI.network.Protocol>}
4215 * Returns an array of `Network.Protocol` instances representing the
4216 * logical interfaces this wireless network is attached to.
4218 getNetworks: function() {
4219 var networkNames
= this.getNetworkNames(),
4222 for (var i
= 0; i
< networkNames
.length
; i
++) {
4223 var uciInterface
= uci
.get('network', networkNames
[i
]);
4225 if (uciInterface
== null || uciInterface
['.type'] != 'interface')
4228 networks
.push(Network
.prototype.instantiateNetwork(networkNames
[i
]));
4231 networks
.sort(networkSort
);
4237 * Get the associated Linux network device.
4239 * @returns {LuCI.network.Device}
4240 * Returns a `Network.Device` instance representing the Linux network
4241 * device associted with this wireless network.
4243 getDevice: function() {
4244 return Network
.prototype.instantiateDevice(this.getIfname());
4248 * Check whether this wifi network supports deauthenticating clients.
4250 * @returns {boolean}
4251 * Returns `true` when this wifi network instance supports forcibly
4252 * deauthenticating clients, otherwise `false`.
4254 isClientDisconnectSupported: function() {
4255 return L
.isObject(this.ubus('hostapd', 'del_client'));
4259 * Forcibly disconnect the given client from the wireless network.
4261 * @param {string} mac
4262 * The MAC address of the client to disconnect.
4264 * @param {boolean} [deauth=false]
4265 * Specifies whether to deauthenticate (`true`) or disassociate (`false`)
4268 * @param {number} [reason=1]
4269 * Specifies the IEEE 802.11 reason code to disassoc/deauth the client
4270 * with. Default is `1` which corresponds to `Unspecified reason`.
4272 * @param {number} [ban_time=0]
4273 * Specifies the amount of milliseconds to ban the client from
4274 * reconnecting. By default, no ban time is set which allows the client
4275 * to reassociate / reauthenticate immediately.
4277 * @returns {Promise<number>}
4278 * Returns a promise resolving to the underlying ubus call result code
4279 * which is typically `0`, even for not existing MAC addresses.
4280 * The promise might reject with an error in case invalid arguments
4283 disconnectClient: function(mac
, deauth
, reason
, ban_time
) {
4284 if (reason
== null || reason
== 0)
4290 return rpc
.declare({
4291 object
: 'hostapd.%s'.format(this.getIfname()),
4292 method
: 'del_client',
4293 params
: [ 'addr', 'deauth', 'reason', 'ban_time' ]
4294 })(mac
, deauth
, reason
, ban_time
);