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 uci
.load(['network', 'wireless', 'luci'])
360 ]).then(function(data
) {
361 var netifd_ifaces
= data
[0],
362 board_json
= data
[1],
366 isTunnel
: {}, isBridge
: {}, isSwitch
: {}, isWifi
: {},
367 ifaces
: netifd_ifaces
, radios
: data
[3], hosts
: data
[4],
368 netdevs
: {}, bridges
: {}, switches
: {}, hostapd
: {}
371 for (var name
in luci_devs
) {
372 var dev
= luci_devs
[name
];
374 if (isVirtualIfname(name
))
375 s
.isTunnel
[name
] = true;
377 if (!s
.isTunnel
[name
] && isIgnoredIfname(name
))
380 s
.netdevs
[name
] = s
.netdevs
[name
] || {
390 wireless
: dev
.wireless
,
395 if (Array
.isArray(dev
.ipaddrs
))
396 for (var i
= 0; i
< dev
.ipaddrs
.length
; i
++)
397 s
.netdevs
[name
].ipaddrs
.push(dev
.ipaddrs
[i
].address
+ '/' + dev
.ipaddrs
[i
].netmask
);
399 if (Array
.isArray(dev
.ip6addrs
))
400 for (var i
= 0; i
< dev
.ip6addrs
.length
; i
++)
401 s
.netdevs
[name
].ip6addrs
.push(dev
.ip6addrs
[i
].address
+ '/' + dev
.ip6addrs
[i
].netmask
);
404 for (var name
in luci_devs
) {
405 var dev
= luci_devs
[name
];
417 for (var i
= 0; dev
.ports
&& i
< dev
.ports
.length
; i
++) {
418 var subdev
= s
.netdevs
[dev
.ports
[i
]];
423 b
.ifnames
.push(subdev
);
428 s
.isBridge
[name
] = true;
431 if (L
.isObject(board_json
.switch)) {
432 for (var switchname
in board_json
.switch) {
433 var layout
= board_json
.switch[switchname
],
440 if (L
.isObject(layout
) && Array
.isArray(layout
.ports
)) {
441 for (var i
= 0, port
; (port
= layout
.ports
[i
]) != null; i
++) {
442 if (typeof(port
) == 'object' && typeof(port
.num
) == 'number' &&
443 (typeof(port
.role
) == 'string' || typeof(port
.device
) == 'string')) {
446 role
: port
.role
|| 'cpu',
447 index
: (port
.index
!= null) ? port
.index
: port
.num
450 if (port
.device
!= null) {
451 spec
.device
= port
.device
;
452 spec
.tagged
= spec
.need_tag
;
453 netdevs
[port
.num
] = port
.device
;
458 if (port
.role
!= null)
459 nports
[port
.role
] = (nports
[port
.role
] || 0) + 1;
463 ports
.sort(function(a
, b
) {
464 if (a
.role
!= b
.role
)
465 return (a
.role
< b
.role
) ? -1 : 1;
467 return (a
.index
- b
.index
);
470 for (var i
= 0, port
; (port
= ports
[i
]) != null; i
++) {
471 if (port
.role
!= role
) {
477 port
.label
= 'CPU (%s)'.format(port
.device
);
478 else if (nports
[role
] > 1)
479 port
.label
= '%s %d'.format(role
.toUpperCase(), pnum
++);
481 port
.label
= role
.toUpperCase();
487 s
.switches
[switchname
] = {
495 if (L
.isObject(board_json
.dsl
) && L
.isObject(board_json
.dsl
.modem
)) {
496 s
.hasDSLModem
= board_json
.dsl
.modem
;
503 if (L
.isObject(s
.radios
))
504 for (var radio
in s
.radios
)
505 if (L
.isObject(s
.radios
[radio
]) && Array
.isArray(s
.radios
[radio
].interfaces
))
506 for (var i
= 0; i
< s
.radios
[radio
].interfaces
.length
; i
++)
507 if (L
.isObject(s
.radios
[radio
].interfaces
[i
]) && s
.radios
[radio
].interfaces
[i
].ifname
)
508 objects
.push('hostapd.%s'.format(s
.radios
[radio
].interfaces
[i
].ifname
));
510 return (objects
.length
? L
.resolveDefault(rpc
.list
.apply(rpc
, objects
), {}) : Promise
.resolve({})).then(function(res
) {
512 var m
= k
.match(/^hostapd\.(.+)$/);
514 s
.hostapd
[m
[1]] = res
[k
];
522 return (_state
!= null ? Promise
.resolve(_state
) : _init
);
525 function ifnameOf(obj
) {
526 if (obj
instanceof Protocol
)
527 return obj
.getIfname();
528 else if (obj
instanceof Device
)
529 return obj
.getName();
530 else if (obj
instanceof WifiDevice
)
531 return obj
.getName();
532 else if (obj
instanceof WifiNetwork
)
533 return obj
.getIfname();
534 else if (typeof(obj
) == 'string')
535 return obj
.replace(/:.+$/, '');
540 function networkSort(a
, b
) {
541 return a
.getName() > b
.getName();
544 function deviceSort(a
, b
) {
545 var typeWeigth
= { wifi
: 2, alias
: 3 },
546 weightA
= typeWeigth
[a
.getType()] || 1,
547 weightB
= typeWeigth
[b
.getType()] || 1;
549 if (weightA
!= weightB
)
550 return weightA
- weightB
;
552 return a
.getName() > b
.getName();
555 function formatWifiEncryption(enc
) {
556 if (!L
.isObject(enc
))
562 var ciphers
= Array
.isArray(enc
.ciphers
)
563 ? enc
.ciphers
.map(function(c
) { return c
.toUpperCase() }) : [ 'NONE' ];
565 if (Array
.isArray(enc
.wep
)) {
566 var has_open
= false,
569 for (var i
= 0; i
< enc
.wep
.length
; i
++)
570 if (enc
.wep
[i
] == 'open')
572 else if (enc
.wep
[i
] == 'shared')
575 if (has_open
&& has_shared
)
576 return 'WEP Open/Shared (%s)'.format(ciphers
.join(', '));
578 return 'WEP Open System (%s)'.format(ciphers
.join(', '));
580 return 'WEP Shared Auth (%s)'.format(ciphers
.join(', '));
585 if (Array
.isArray(enc
.wpa
)) {
587 suites
= Array
.isArray(enc
.authentication
)
588 ? enc
.authentication
.map(function(a
) { return a
.toUpperCase() }) : [ 'NONE' ];
590 for (var i
= 0; i
< enc
.wpa
.length
; i
++)
591 switch (enc
.wpa
[i
]) {
593 versions
.push('WPA');
597 versions
.push('WPA%d'.format(enc
.wpa
[i
]));
601 if (versions
.length
> 1)
602 return 'mixed %s %s (%s)'.format(versions
.join('/'), suites
.join(', '), ciphers
.join(', '));
604 return '%s %s (%s)'.format(versions
[0], suites
.join(', '), ciphers
.join(', '));
610 function enumerateNetworks() {
611 var uciInterfaces
= uci
.sections('network', 'interface'),
614 for (var i
= 0; i
< uciInterfaces
.length
; i
++)
615 networks
[uciInterfaces
[i
]['.name']] = this.instantiateNetwork(uciInterfaces
[i
]['.name']);
617 for (var i
= 0; i
< _state
.ifaces
.length
; i
++)
618 if (networks
[_state
.ifaces
[i
].interface] == null)
619 networks
[_state
.ifaces
[i
].interface] =
620 this.instantiateNetwork(_state
.ifaces
[i
].interface, _state
.ifaces
[i
].proto
);
624 for (var network
in networks
)
625 if (networks
.hasOwnProperty(network
))
626 rv
.push(networks
[network
]);
628 rv
.sort(networkSort
);
634 var Hosts
, Network
, Protocol
, Device
, WifiDevice
, WifiNetwork
;
642 * The `LuCI.network` class combines data from multiple `ubus` apis to
643 * provide an abstraction of the current network configuration state.
645 * It provides methods to enumerate interfaces and devices, to query
646 * current configuration details and to manipulate settings.
648 Network
= baseclass
.extend(/** @lends LuCI.network.prototype */ {
650 * Converts the given prefix size in bits to a netmask.
654 * @param {number} bits
655 * The prefix size in bits.
657 * @param {boolean} [v6=false]
658 * Whether to convert the bits value into an IPv4 netmask (`false`) or
659 * an IPv6 netmask (`true`).
661 * @returns {null|string}
662 * Returns a string containing the netmask corresponding to the bit count
663 * or `null` when the given amount of bits exceeds the maximum possible
664 * value of `32` for IPv4 or `128` for IPv6.
666 prefixToMask
: prefixToMask
,
669 * Converts the given netmask to a prefix size in bits.
673 * @param {string} netmask
674 * The netmask to convert into a bit count.
676 * @param {boolean} [v6=false]
677 * Whether to parse the given netmask as IPv4 (`false`) or IPv6 (`true`)
680 * @returns {null|number}
681 * Returns the number of prefix bits contained in the netmask or `null`
682 * if the given netmask value was invalid.
684 maskToPrefix
: maskToPrefix
,
687 * An encryption entry describes active wireless encryption settings
688 * such as the used key management protocols, active ciphers and
691 * @typedef {Object<string, boolean|Array<number|string>>} LuCI.network.WifiEncryption
692 * @memberof LuCI.network
694 * @property {boolean} enabled
695 * Specifies whether any kind of encryption, such as `WEP` or `WPA` is
696 * enabled. If set to `false`, then no encryption is active and the
697 * corresponding network is open.
699 * @property {string[]} [wep]
700 * When the `wep` property exists, the network uses WEP encryption.
701 * In this case, the property is set to an array of active WEP modes
702 * which might be either `open`, `shared` or both.
704 * @property {number[]} [wpa]
705 * When the `wpa` property exists, the network uses WPA security.
706 * In this case, the property is set to an array containing the WPA
707 * protocol versions used, e.g. `[ 1, 2 ]` for WPA/WPA2 mixed mode or
708 * `[ 3 ]` for WPA3-SAE.
710 * @property {string[]} [authentication]
711 * The `authentication` property only applies to WPA encryption and
712 * is defined when the `wpa` property is set as well. It points to
713 * an array of active authentication suites used by the network, e.g.
714 * `[ "psk" ]` for a WPA(2)-PSK network or `[ "psk", "sae" ]` for
715 * mixed WPA2-PSK/WPA3-SAE encryption.
717 * @property {string[]} [ciphers]
718 * If either WEP or WPA encryption is active, then the `ciphers`
719 * property will be set to an array describing the active encryption
720 * ciphers used by the network, e.g. `[ "tkip", "ccmp" ]` for a
721 * WPA/WPA2-PSK mixed network or `[ "wep-40", "wep-104" ]` for an
726 * Converts a given {@link LuCI.network.WifiEncryption encryption entry}
727 * into a human readable string such as `mixed WPA/WPA2 PSK (TKIP, CCMP)`
728 * or `WPA3 SAE (CCMP)`.
732 * @param {LuCI.network.WifiEncryption} encryption
733 * The wireless encryption entry to convert.
735 * @returns {null|string}
736 * Returns the description string for the given encryption entry or
737 * `null` if the given entry was invalid.
739 formatWifiEncryption
: formatWifiEncryption
,
742 * Flushes the local network state cache and fetches updated information
743 * from the remote `ubus` apis.
745 * @returns {Promise<Object>}
746 * Returns a promise resolving to the internal network state object.
748 flushCache: function() {
749 initNetworkState(true);
754 * Instantiates the given {@link LuCI.network.Protocol Protocol} backend,
755 * optionally using the given network name.
757 * @param {string} protoname
758 * The protocol backend to use, e.g. `static` or `dhcp`.
760 * @param {string} [netname=__dummy__]
761 * The network name to use for the instantiated protocol. This should be
762 * usually set to one of the interfaces described in /etc/config/network
763 * but it is allowed to omit it, e.g. to query protocol capabilities
764 * without the need for an existing interface.
766 * @returns {null|LuCI.network.Protocol}
767 * Returns the instantiated protocol backend class or `null` if the given
768 * protocol isn't known.
770 getProtocol: function(protoname
, netname
) {
771 var v
= _protocols
[protoname
];
773 return new v(netname
|| '__dummy__');
779 * Obtains instances of all known {@link LuCI.network.Protocol Protocol}
782 * @returns {Array<LuCI.network.Protocol>}
783 * Returns an array of protocol class instances.
785 getProtocols: function() {
788 for (var protoname
in _protocols
)
789 rv
.push(new _protocols
[protoname
]('__dummy__'));
795 * Registers a new {@link LuCI.network.Protocol Protocol} subclass
796 * with the given methods and returns the resulting subclass value.
798 * This functions internally calls
799 * {@link LuCI.Class.extend Class.extend()} on the `Network.Protocol`
802 * @param {string} protoname
803 * The name of the new protocol to register.
805 * @param {Object<string, *>} methods
806 * The member methods and values of the new `Protocol` subclass to
807 * be passed to {@link LuCI.Class.extend Class.extend()}.
809 * @returns {LuCI.network.Protocol}
810 * Returns the new `Protocol` subclass.
812 registerProtocol: function(protoname
, methods
) {
813 var spec
= L
.isObject(_protospecs
) ? _protospecs
[protoname
] : null;
814 var proto
= Protocol
.extend(Object
.assign({
815 getI18n: function() {
819 isFloating: function() {
823 isVirtual: function() {
824 return (L
.isObject(spec
) && spec
.no_device
== true);
827 renderFormOptions: function(section
) {
831 __init__: function(name
) {
835 getProtocol: function() {
840 _protocols
[protoname
] = proto
;
846 * Registers a new regular expression pattern to recognize
847 * virtual interfaces.
849 * @param {RegExp} pat
850 * A `RegExp` instance to match a virtual interface name
851 * such as `6in4-wan` or `tun0`.
853 registerPatternVirtual: function(pat
) {
854 iface_patterns_virtual
.push(pat
);
858 * Registers a new human readable translation string for a `Protocol`
861 * @param {string} code
862 * The `ubus` protocol error code to register a translation for, e.g.
865 * @param {string} message
866 * The message to use as translation for the given protocol error code.
869 * Returns `true` if the error code description has been added or `false`
870 * if either the arguments were invalid or if there already was a
871 * description for the given code.
873 registerErrorCode: function(code
, message
) {
874 if (typeof(code
) == 'string' &&
875 typeof(message
) == 'string' &&
876 !proto_errors
.hasOwnProperty(code
)) {
877 proto_errors
[code
] = message
;
885 * Adds a new network of the given name and update it with the given
888 * If a network with the given name already exist but is empty, then
889 * this function will update its option, otherwise it will do nothing.
891 * @param {string} name
892 * The name of the network to add. Must be in the format `[a-zA-Z0-9_]+`.
894 * @param {Object<string, string|string[]>} [options]
895 * An object of uci option values to set on the new network or to
896 * update in an existing, empty network.
898 * @returns {Promise<null|LuCI.network.Protocol>}
899 * Returns a promise resolving to the `Protocol` subclass instance
900 * describing the added network or resolving to `null` if the name
901 * was invalid or if a non-empty network of the given name already
904 addNetwork: function(name
, options
) {
905 return this.getNetwork(name
).then(L
.bind(function(existingNetwork
) {
906 if (name
!= null && /^[a-zA-Z0-9_]+$/.test(name
) && existingNetwork
== null) {
907 var sid
= uci
.add('network', 'interface', name
);
910 if (L
.isObject(options
))
911 for (var key
in options
)
912 if (options
.hasOwnProperty(key
))
913 uci
.set('network', sid
, key
, options
[key
]);
915 return this.instantiateNetwork(sid
);
918 else if (existingNetwork
!= null && existingNetwork
.isEmpty()) {
919 if (L
.isObject(options
))
920 for (var key
in options
)
921 if (options
.hasOwnProperty(key
))
922 existingNetwork
.set(key
, options
[key
]);
924 return existingNetwork
;
930 * Get a {@link LuCI.network.Protocol Protocol} instance describing
931 * the network with the given name.
933 * @param {string} name
934 * The logical interface name of the network get, e.g. `lan` or `wan`.
936 * @returns {Promise<null|LuCI.network.Protocol>}
937 * Returns a promise resolving to a
938 * {@link LuCI.network.Protocol Protocol} subclass instance describing
939 * the network or `null` if the network did not exist.
941 getNetwork: function(name
) {
942 return initNetworkState().then(L
.bind(function() {
943 var section
= (name
!= null) ? uci
.get('network', name
) : null;
945 if (section
!= null && section
['.type'] == 'interface') {
946 return this.instantiateNetwork(name
);
948 else if (name
!= null) {
949 for (var i
= 0; i
< _state
.ifaces
.length
; i
++)
950 if (_state
.ifaces
[i
].interface == name
)
951 return this.instantiateNetwork(name
, _state
.ifaces
[i
].proto
);
959 * Gets an array containing all known networks.
961 * @returns {Promise<Array<LuCI.network.Protocol>>}
962 * Returns a promise resolving to a name-sorted array of
963 * {@link LuCI.network.Protocol Protocol} subclass instances
964 * describing all known networks.
966 getNetworks: function() {
967 return initNetworkState().then(L
.bind(enumerateNetworks
, this));
971 * Deletes the given network and its references from the network and
972 * firewall configuration.
974 * @param {string} name
975 * The name of the network to delete.
977 * @returns {Promise<boolean>}
978 * Returns a promise resolving to either `true` if the network and
979 * references to it were successfully deleted from the configuration or
980 * `false` if the given network could not be found.
982 deleteNetwork: function(name
) {
983 var requireFirewall
= Promise
.resolve(L
.require('firewall')).catch(function() {}),
984 network
= this.instantiateNetwork(name
);
986 return Promise
.all([ requireFirewall
, initNetworkState() ]).then(function(res
) {
987 var uciInterface
= uci
.get('network', name
),
990 if (uciInterface
!= null && uciInterface
['.type'] == 'interface') {
991 return Promise
.resolve(network
? network
.deleteConfiguration() : null).then(function() {
992 uci
.remove('network', name
);
994 uci
.sections('luci', 'ifstate', function(s
) {
995 if (s
.interface == name
)
996 uci
.remove('luci', s
['.name']);
999 uci
.sections('network', 'alias', function(s
) {
1000 if (s
.interface == name
)
1001 uci
.remove('network', s
['.name']);
1004 uci
.sections('network', 'route', function(s
) {
1005 if (s
.interface == name
)
1006 uci
.remove('network', s
['.name']);
1009 uci
.sections('network', 'route6', function(s
) {
1010 if (s
.interface == name
)
1011 uci
.remove('network', s
['.name']);
1014 uci
.sections('wireless', 'wifi-iface', function(s
) {
1015 var networks
= L
.toArray(s
.network
).filter(function(network
) { return network
!= name
});
1017 if (networks
.length
> 0)
1018 uci
.set('wireless', s
['.name'], 'network', networks
.join(' '));
1020 uci
.unset('wireless', s
['.name'], 'network');
1024 return firewall
.deleteNetwork(name
).then(function() { return true });
1027 }).catch(function() {
1037 * Rename the given network and its references to a new name.
1039 * @param {string} oldName
1040 * The current name of the network.
1042 * @param {string} newName
1043 * The name to rename the network to, must be in the format
1046 * @returns {Promise<boolean>}
1047 * Returns a promise resolving to either `true` if the network was
1048 * successfully renamed or `false` if the new name was invalid, if
1049 * a network with the new name already exists or if the network to
1050 * rename could not be found.
1052 renameNetwork: function(oldName
, newName
) {
1053 return initNetworkState().then(function() {
1054 if (newName
== null || !/^[a-zA-Z0-9_]+$/.test(newName
) || uci
.get('network', newName
) != null)
1057 var oldNetwork
= uci
.get('network', oldName
);
1059 if (oldNetwork
== null || oldNetwork
['.type'] != 'interface')
1062 var sid
= uci
.add('network', 'interface', newName
);
1064 for (var key
in oldNetwork
)
1065 if (oldNetwork
.hasOwnProperty(key
) && key
.charAt(0) != '.')
1066 uci
.set('network', sid
, key
, oldNetwork
[key
]);
1068 uci
.sections('luci', 'ifstate', function(s
) {
1069 if (s
.interface == oldName
)
1070 uci
.set('luci', s
['.name'], 'interface', newName
);
1073 uci
.sections('network', 'alias', function(s
) {
1074 if (s
.interface == oldName
)
1075 uci
.set('network', s
['.name'], 'interface', newName
);
1078 uci
.sections('network', 'route', function(s
) {
1079 if (s
.interface == oldName
)
1080 uci
.set('network', s
['.name'], 'interface', newName
);
1083 uci
.sections('network', 'route6', function(s
) {
1084 if (s
.interface == oldName
)
1085 uci
.set('network', s
['.name'], 'interface', newName
);
1088 uci
.sections('wireless', 'wifi-iface', function(s
) {
1089 var networks
= L
.toArray(s
.network
).map(function(network
) { return (network
== oldName
? newName
: network
) });
1091 if (networks
.length
> 0)
1092 uci
.set('wireless', s
['.name'], 'network', networks
.join(' '));
1095 uci
.remove('network', oldName
);
1102 * Get a {@link LuCI.network.Device Device} instance describing the
1103 * given network device.
1105 * @param {string} name
1106 * The name of the network device to get, e.g. `eth0` or `br-lan`.
1108 * @returns {Promise<null|LuCI.network.Device>}
1109 * Returns a promise resolving to the `Device` instance describing
1110 * the network device or `null` if the given device name could not
1113 getDevice: function(name
) {
1114 return initNetworkState().then(L
.bind(function() {
1118 if (_state
.netdevs
.hasOwnProperty(name
) || isWifiIfname(name
))
1119 return this.instantiateDevice(name
);
1121 var netid
= getWifiNetidBySid(name
);
1123 return this.instantiateDevice(netid
[0]);
1130 * Get a sorted list of all found network devices.
1132 * @returns {Promise<Array<LuCI.network.Device>>}
1133 * Returns a promise resolving to a sorted array of `Device` class
1134 * instances describing the network devices found on the system.
1136 getDevices: function() {
1137 return initNetworkState().then(L
.bind(function() {
1140 /* find simple devices */
1141 var uciInterfaces
= uci
.sections('network', 'interface');
1142 for (var i
= 0; i
< uciInterfaces
.length
; i
++) {
1143 var ifnames
= L
.toArray(uciInterfaces
[i
].ifname
);
1145 for (var j
= 0; j
< ifnames
.length
; j
++) {
1146 if (ifnames
[j
].charAt(0) == '@')
1149 if (isIgnoredIfname(ifnames
[j
]) || isVirtualIfname(ifnames
[j
]) || isWifiIfname(ifnames
[j
]))
1152 devices
[ifnames
[j
]] = this.instantiateDevice(ifnames
[j
]);
1156 for (var ifname
in _state
.netdevs
) {
1157 if (devices
.hasOwnProperty(ifname
))
1160 if (isIgnoredIfname(ifname
) || isWifiIfname(ifname
))
1163 if (_state
.netdevs
[ifname
].wireless
)
1166 devices
[ifname
] = this.instantiateDevice(ifname
);
1169 /* find VLAN devices */
1170 var uciSwitchVLANs
= uci
.sections('network', 'switch_vlan');
1171 for (var i
= 0; i
< uciSwitchVLANs
.length
; i
++) {
1172 if (typeof(uciSwitchVLANs
[i
].ports
) != 'string' ||
1173 typeof(uciSwitchVLANs
[i
].device
) != 'string' ||
1174 !_state
.switches
.hasOwnProperty(uciSwitchVLANs
[i
].device
))
1177 var ports
= uciSwitchVLANs
[i
].ports
.split(/\s+/);
1178 for (var j
= 0; j
< ports
.length
; j
++) {
1179 var m
= ports
[j
].match(/^(\d+)([tu]?)$/);
1183 var netdev
= _state
.switches
[uciSwitchVLANs
[i
].device
].netdevs
[m
[1]];
1187 if (!devices
.hasOwnProperty(netdev
))
1188 devices
[netdev
] = this.instantiateDevice(netdev
);
1190 _state
.isSwitch
[netdev
] = true;
1195 var vid
= uciSwitchVLANs
[i
].vid
|| uciSwitchVLANs
[i
].vlan
;
1196 vid
= (vid
!= null ? +vid
: null);
1198 if (vid
== null || vid
< 0 || vid
> 4095)
1201 var vlandev
= '%s.%d'.format(netdev
, vid
);
1203 if (!devices
.hasOwnProperty(vlandev
))
1204 devices
[vlandev
] = this.instantiateDevice(vlandev
);
1206 _state
.isSwitch
[vlandev
] = true;
1210 /* find wireless interfaces */
1211 var uciWifiIfaces
= uci
.sections('wireless', 'wifi-iface'),
1214 for (var i
= 0; i
< uciWifiIfaces
.length
; i
++) {
1215 if (typeof(uciWifiIfaces
[i
].device
) != 'string')
1218 networkCount
[uciWifiIfaces
[i
].device
] = (networkCount
[uciWifiIfaces
[i
].device
] || 0) + 1;
1220 var netid
= '%s.network%d'.format(uciWifiIfaces
[i
].device
, networkCount
[uciWifiIfaces
[i
].device
]);
1222 devices
[netid
] = this.instantiateDevice(netid
);
1227 for (var netdev
in devices
)
1228 if (devices
.hasOwnProperty(netdev
))
1229 rv
.push(devices
[netdev
]);
1231 rv
.sort(deviceSort
);
1238 * Test if a given network device name is in the list of patterns for
1239 * device names to ignore.
1241 * Ignored device names are usually Linux network devices which are
1242 * spawned implicitly by kernel modules such as `tunl0` or `hwsim0`
1243 * and which are unsuitable for use in network configuration.
1245 * @param {string} name
1246 * The device name to test.
1248 * @returns {boolean}
1249 * Returns `true` if the given name is in the ignore pattern list,
1250 * else returns `false`.
1252 isIgnoredDevice: function(name
) {
1253 return isIgnoredIfname(name
);
1257 * Get a {@link LuCI.network.WifiDevice WifiDevice} instance describing
1258 * the given wireless radio.
1260 * @param {string} devname
1261 * The configuration name of the wireless radio to lookup, e.g. `radio0`
1262 * for the first mac80211 phy on the system.
1264 * @returns {Promise<null|LuCI.network.WifiDevice>}
1265 * Returns a promise resolving to the `WifiDevice` instance describing
1266 * the underlying radio device or `null` if the wireless radio could not
1269 getWifiDevice: function(devname
) {
1270 return initNetworkState().then(L
.bind(function() {
1271 var existingDevice
= uci
.get('wireless', devname
);
1273 if (existingDevice
== null || existingDevice
['.type'] != 'wifi-device')
1276 return this.instantiateWifiDevice(devname
, _state
.radios
[devname
] || {});
1281 * Obtain a list of all configured radio devices.
1283 * @returns {Promise<Array<LuCI.network.WifiDevice>>}
1284 * Returns a promise resolving to an array of `WifiDevice` instances
1285 * describing the wireless radios configured in the system.
1286 * The order of the array corresponds to the order of the radios in
1287 * the configuration.
1289 getWifiDevices: function() {
1290 return initNetworkState().then(L
.bind(function() {
1291 var uciWifiDevices
= uci
.sections('wireless', 'wifi-device'),
1294 for (var i
= 0; i
< uciWifiDevices
.length
; i
++) {
1295 var devname
= uciWifiDevices
[i
]['.name'];
1296 rv
.push(this.instantiateWifiDevice(devname
, _state
.radios
[devname
] || {}));
1304 * Get a {@link LuCI.network.WifiNetwork WifiNetwork} instance describing
1305 * the given wireless network.
1307 * @param {string} netname
1308 * The name of the wireless network to lookup. This may be either an uci
1309 * configuration section ID, a network ID in the form `radio#.network#`
1310 * or a Linux network device name like `wlan0` which is resolved to the
1311 * corresponding configuration section through `ubus` runtime information.
1313 * @returns {Promise<null|LuCI.network.WifiNetwork>}
1314 * Returns a promise resolving to the `WifiNetwork` instance describing
1315 * the wireless network or `null` if the corresponding network could not
1318 getWifiNetwork: function(netname
) {
1319 return initNetworkState()
1320 .then(L
.bind(this.lookupWifiNetwork
, this, netname
));
1324 * Get an array of all {@link LuCI.network.WifiNetwork WifiNetwork}
1325 * instances describing the wireless networks present on the system.
1327 * @returns {Promise<Array<LuCI.network.WifiNetwork>>}
1328 * Returns a promise resolving to an array of `WifiNetwork` instances
1329 * describing the wireless networks. The array will be empty if no networks
1332 getWifiNetworks: function() {
1333 return initNetworkState().then(L
.bind(function() {
1334 var wifiIfaces
= uci
.sections('wireless', 'wifi-iface'),
1337 for (var i
= 0; i
< wifiIfaces
.length
; i
++)
1338 rv
.push(this.lookupWifiNetwork(wifiIfaces
[i
]['.name']));
1340 rv
.sort(function(a
, b
) {
1341 return (a
.getID() > b
.getID());
1349 * Adds a new wireless network to the configuration and sets its options
1350 * to the provided values.
1352 * @param {Object<string, string|string[]>} options
1353 * The options to set for the newly added wireless network. This object
1354 * must at least contain a `device` property which is set to the radio
1355 * name the new network belongs to.
1357 * @returns {Promise<null|LuCI.network.WifiNetwork>}
1358 * Returns a promise resolving to a `WifiNetwork` instance describing
1359 * the newly added wireless network or `null` if the given options
1360 * were invalid or if the associated radio device could not be found.
1362 addWifiNetwork: function(options
) {
1363 return initNetworkState().then(L
.bind(function() {
1364 if (options
== null ||
1365 typeof(options
) != 'object' ||
1366 typeof(options
.device
) != 'string')
1369 var existingDevice
= uci
.get('wireless', options
.device
);
1370 if (existingDevice
== null || existingDevice
['.type'] != 'wifi-device')
1373 /* XXX: need to add a named section (wifinet#) here */
1374 var sid
= uci
.add('wireless', 'wifi-iface');
1375 for (var key
in options
)
1376 if (options
.hasOwnProperty(key
))
1377 uci
.set('wireless', sid
, key
, options
[key
]);
1379 var radioname
= existingDevice
['.name'],
1380 netid
= getWifiNetidBySid(sid
) || [];
1382 return this.instantiateWifiNetwork(sid
, radioname
, _state
.radios
[radioname
], netid
[0], null);
1387 * Deletes the given wireless network from the configuration.
1389 * @param {string} netname
1390 * The name of the network to remove. This may be either a
1391 * network ID in the form `radio#.network#` or a Linux network device
1392 * name like `wlan0` which is resolved to the corresponding configuration
1393 * section through `ubus` runtime information.
1395 * @returns {Promise<boolean>}
1396 * Returns a promise resolving to `true` if the wireless network has been
1397 * successfully deleted from the configuration or `false` if it could not
1400 deleteWifiNetwork: function(netname
) {
1401 return initNetworkState().then(L
.bind(function() {
1402 var sid
= getWifiSidByIfname(netname
);
1407 uci
.remove('wireless', sid
);
1413 getStatusByRoute: function(addr
, mask
) {
1414 return initNetworkState().then(L
.bind(function() {
1417 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
1418 if (!Array
.isArray(_state
.ifaces
[i
].route
))
1421 for (var j
= 0; j
< _state
.ifaces
[i
].route
.length
; j
++) {
1422 if (typeof(_state
.ifaces
[i
].route
[j
]) != 'object' ||
1423 typeof(_state
.ifaces
[i
].route
[j
].target
) != 'string' ||
1424 typeof(_state
.ifaces
[i
].route
[j
].mask
) != 'number')
1427 if (_state
.ifaces
[i
].route
[j
].table
)
1430 if (_state
.ifaces
[i
].route
[j
].target
!= addr
||
1431 _state
.ifaces
[i
].route
[j
].mask
!= mask
)
1434 rv
.push(_state
.ifaces
[i
]);
1443 getStatusByAddress: function(addr
) {
1444 return initNetworkState().then(L
.bind(function() {
1447 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
1448 if (Array
.isArray(_state
.ifaces
[i
]['ipv4-address']))
1449 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv4-address'].length
; j
++)
1450 if (typeof(_state
.ifaces
[i
]['ipv4-address'][j
]) == 'object' &&
1451 _state
.ifaces
[i
]['ipv4-address'][j
].address
== addr
)
1452 return _state
.ifaces
[i
];
1454 if (Array
.isArray(_state
.ifaces
[i
]['ipv6-address']))
1455 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv6-address'].length
; j
++)
1456 if (typeof(_state
.ifaces
[i
]['ipv6-address'][j
]) == 'object' &&
1457 _state
.ifaces
[i
]['ipv6-address'][j
].address
== addr
)
1458 return _state
.ifaces
[i
];
1460 if (Array
.isArray(_state
.ifaces
[i
]['ipv6-prefix-assignment']))
1461 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv6-prefix-assignment'].length
; j
++)
1462 if (typeof(_state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]) == 'object' &&
1463 typeof(_state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]['local-address']) == 'object' &&
1464 _state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]['local-address'].address
== addr
)
1465 return _state
.ifaces
[i
];
1473 * Get IPv4 wan networks.
1475 * This function looks up all networks having a default `0.0.0.0/0` route
1476 * and returns them as array.
1478 * @returns {Promise<Array<LuCI.network.Protocol>>}
1479 * Returns a promise resolving to an array of `Protocol` subclass
1480 * instances describing the found default route interfaces.
1482 getWANNetworks: function() {
1483 return this.getStatusByRoute('0.0.0.0', 0).then(L
.bind(function(statuses
) {
1484 var rv
= [], seen
= {};
1486 for (var i
= 0; i
< statuses
.length
; i
++) {
1487 if (!seen
.hasOwnProperty(statuses
[i
].interface)) {
1488 rv
.push(this.instantiateNetwork(statuses
[i
].interface, statuses
[i
].proto
));
1489 seen
[statuses
[i
].interface] = true;
1498 * Get IPv6 wan networks.
1500 * This function looks up all networks having a default `::/0` route
1501 * and returns them as array.
1503 * @returns {Promise<Array<LuCI.network.Protocol>>}
1504 * Returns a promise resolving to an array of `Protocol` subclass
1505 * instances describing the found IPv6 default route interfaces.
1507 getWAN6Networks: function() {
1508 return this.getStatusByRoute('::', 0).then(L
.bind(function(statuses
) {
1509 var rv
= [], seen
= {};
1511 for (var i
= 0; i
< statuses
.length
; i
++) {
1512 if (!seen
.hasOwnProperty(statuses
[i
].interface)) {
1513 rv
.push(this.instantiateNetwork(statuses
[i
].interface, statuses
[i
].proto
));
1514 seen
[statuses
[i
].interface] = true;
1523 * Describes an swconfig switch topology by specifying the CPU
1524 * connections and external port labels of a switch.
1526 * @typedef {Object<string, Object|Array>} SwitchTopology
1527 * @memberof LuCI.network
1529 * @property {Object<number, string>} netdevs
1530 * The `netdevs` property points to an object describing the CPU port
1531 * connections of the switch. The numeric key of the enclosed object is
1532 * the port number, the value contains the Linux network device name the
1533 * port is hardwired to.
1535 * @property {Array<Object<string, boolean|number|string>>} ports
1536 * The `ports` property points to an array describing the populated
1537 * ports of the switch in the external label order. Each array item is
1538 * an object containg the following keys:
1539 * - `num` - the internal switch port number
1540 * - `label` - the label of the port, e.g. `LAN 1` or `CPU (eth0)`
1541 * - `device` - the connected Linux network device name (CPU ports only)
1542 * - `tagged` - a boolean indicating whether the port must be tagged to
1543 * function (CPU ports only)
1547 * Returns the topologies of all swconfig switches found on the system.
1549 * @returns {Promise<Object<string, LuCI.network.SwitchTopology>>}
1550 * Returns a promise resolving to an object containing the topologies
1551 * of each switch. The object keys correspond to the name of the switches
1552 * such as `switch0`, the values are
1553 * {@link LuCI.network.SwitchTopology SwitchTopology} objects describing
1556 getSwitchTopologies: function() {
1557 return initNetworkState().then(function() {
1558 return _state
.switches
;
1563 instantiateNetwork: function(name
, proto
) {
1567 proto
= (proto
== null ? uci
.get('network', name
, 'proto') : proto
);
1569 var protoClass
= _protocols
[proto
] || Protocol
;
1570 return new protoClass(name
);
1574 instantiateDevice: function(name
, network
, extend
) {
1576 return new (Device
.extend(extend
))(name
, network
);
1578 return new Device(name
, network
);
1582 instantiateWifiDevice: function(radioname
, radiostate
) {
1583 return new WifiDevice(radioname
, radiostate
);
1587 instantiateWifiNetwork: function(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
) {
1588 return new WifiNetwork(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
);
1592 lookupWifiNetwork: function(netname
) {
1593 var sid
, res
, netid
, radioname
, radiostate
, netstate
;
1595 sid
= getWifiSidByNetid(netname
);
1598 res
= getWifiStateBySid(sid
);
1600 radioname
= res
? res
[0] : null;
1601 radiostate
= res
? res
[1] : null;
1602 netstate
= res
? res
[2] : null;
1605 res
= getWifiStateByIfname(netname
);
1609 radiostate
= res
[1];
1611 sid
= netstate
.section
;
1612 netid
= L
.toArray(getWifiNetidBySid(sid
))[0];
1615 res
= getWifiStateBySid(netname
);
1619 radiostate
= res
[1];
1622 netid
= L
.toArray(getWifiNetidBySid(sid
))[0];
1625 res
= getWifiNetidBySid(netname
);
1636 return this.instantiateWifiNetwork(sid
|| netname
, radioname
,
1637 radiostate
, netid
, netstate
,
1638 netstate
? _state
.hostapd
[netstate
.ifname
] : null);
1642 * Obtains the the network device name of the given object.
1644 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} obj
1645 * The object to get the device name from.
1647 * @returns {null|string}
1648 * Returns a string containing the device name or `null` if the given
1649 * object could not be converted to a name.
1651 getIfnameOf: function(obj
) {
1652 return ifnameOf(obj
);
1656 * Queries the internal DSL modem type from board information.
1658 * @returns {Promise<null|string>}
1659 * Returns a promise resolving to the type of the internal modem
1660 * (e.g. `vdsl`) or to `null` if no internal modem is present.
1662 getDSLModemType: function() {
1663 return initNetworkState().then(function() {
1664 return _state
.hasDSLModem
? _state
.hasDSLModem
.type
: null;
1669 * Queries aggregated information about known hosts.
1671 * This function aggregates information from various sources such as
1672 * DHCP lease databases, ARP and IPv6 neighbour entries, wireless
1673 * association list etc. and returns a {@link LuCI.network.Hosts Hosts}
1674 * class instance describing the found hosts.
1676 * @returns {Promise<LuCI.network.Hosts>}
1677 * Returns a `Hosts` instance describing host known on the system.
1679 getHostHints: function() {
1680 return initNetworkState().then(function() {
1681 return new Hosts(_state
.hosts
);
1688 * @memberof LuCI.network
1692 * The `LuCI.network.Hosts` class encapsulates host information aggregated
1693 * from multiple sources and provides convenience functions to access the
1694 * host information by different criteria.
1696 Hosts
= baseclass
.extend(/** @lends LuCI.network.Hosts.prototype */ {
1697 __init__: function(hosts
) {
1702 * Lookup the hostname associated with the given MAC address.
1704 * @param {string} mac
1705 * The MAC address to lookup.
1707 * @returns {null|string}
1708 * Returns the hostname associated with the given MAC or `null` if
1709 * no matching host could be found or if no hostname is known for
1710 * the corresponding host.
1712 getHostnameByMACAddr: function(mac
) {
1713 return this.hosts
[mac
] ? this.hosts
[mac
].name
: null;
1717 * Lookup the IPv4 address associated with the given MAC address.
1719 * @param {string} mac
1720 * The MAC address to lookup.
1722 * @returns {null|string}
1723 * Returns the IPv4 address associated with the given MAC or `null` if
1724 * no matching host could be found or if no IPv4 address is known for
1725 * the corresponding host.
1727 getIPAddrByMACAddr: function(mac
) {
1728 return this.hosts
[mac
] ? this.hosts
[mac
].ipv4
: null;
1732 * Lookup the IPv6 address associated with the given MAC address.
1734 * @param {string} mac
1735 * The MAC address to lookup.
1737 * @returns {null|string}
1738 * Returns the IPv6 address associated with the given MAC or `null` if
1739 * no matching host could be found or if no IPv6 address is known for
1740 * the corresponding host.
1742 getIP6AddrByMACAddr: function(mac
) {
1743 return this.hosts
[mac
] ? this.hosts
[mac
].ipv6
: null;
1747 * Lookup the hostname associated with the given IPv4 address.
1749 * @param {string} ipaddr
1750 * The IPv4 address to lookup.
1752 * @returns {null|string}
1753 * Returns the hostname associated with the given IPv4 or `null` if
1754 * no matching host could be found or if no hostname is known for
1755 * the corresponding host.
1757 getHostnameByIPAddr: function(ipaddr
) {
1758 for (var mac
in this.hosts
)
1759 if (this.hosts
[mac
].ipv4
== ipaddr
&& this.hosts
[mac
].name
!= null)
1760 return this.hosts
[mac
].name
;
1765 * Lookup the MAC address associated with the given IPv4 address.
1767 * @param {string} ipaddr
1768 * The IPv4 address to lookup.
1770 * @returns {null|string}
1771 * Returns the MAC address associated with the given IPv4 or `null` if
1772 * no matching host could be found or if no MAC address is known for
1773 * the corresponding host.
1775 getMACAddrByIPAddr: function(ipaddr
) {
1776 for (var mac
in this.hosts
)
1777 if (this.hosts
[mac
].ipv4
== ipaddr
)
1783 * Lookup the hostname associated with the given IPv6 address.
1785 * @param {string} ipaddr
1786 * The IPv6 address to lookup.
1788 * @returns {null|string}
1789 * Returns the hostname associated with the given IPv6 or `null` if
1790 * no matching host could be found or if no hostname is known for
1791 * the corresponding host.
1793 getHostnameByIP6Addr: function(ip6addr
) {
1794 for (var mac
in this.hosts
)
1795 if (this.hosts
[mac
].ipv6
== ip6addr
&& this.hosts
[mac
].name
!= null)
1796 return this.hosts
[mac
].name
;
1801 * Lookup the MAC address associated with the given IPv6 address.
1803 * @param {string} ipaddr
1804 * The IPv6 address to lookup.
1806 * @returns {null|string}
1807 * Returns the MAC address associated with the given IPv6 or `null` if
1808 * no matching host could be found or if no MAC address is known for
1809 * the corresponding host.
1811 getMACAddrByIP6Addr: function(ip6addr
) {
1812 for (var mac
in this.hosts
)
1813 if (this.hosts
[mac
].ipv6
== ip6addr
)
1819 * Return an array of (MAC address, name hint) tuples sorted by
1822 * @param {boolean} [preferIp6=false]
1823 * Whether to prefer IPv6 addresses (`true`) or IPv4 addresses (`false`)
1824 * as name hint when no hostname is known for a specific MAC address.
1826 * @returns {Array<Array<string>>}
1827 * Returns an array of arrays containing a name hint for each found
1828 * MAC address on the system. The array is sorted ascending by MAC.
1830 * Each item of the resulting array is a two element array with the
1831 * MAC being the first element and the name hint being the second
1832 * element. The name hint is either the hostname, an IPv4 or an IPv6
1833 * address related to the MAC address.
1835 * If no hostname but both IPv4 and IPv6 addresses are known, the
1836 * `preferIP6` flag specifies whether the IPv6 or the IPv4 address
1839 getMACHints: function(preferIp6
) {
1841 for (var mac
in this.hosts
) {
1842 var hint
= this.hosts
[mac
].name
||
1843 this.hosts
[mac
][preferIp6
? 'ipv6' : 'ipv4'] ||
1844 this.hosts
[mac
][preferIp6
? 'ipv4' : 'ipv6'];
1846 rv
.push([mac
, hint
]);
1848 return rv
.sort(function(a
, b
) { return a
[0] > b
[0] });
1854 * @memberof LuCI.network
1858 * The `Network.Protocol` class serves as base for protocol specific
1859 * subclasses which describe logical UCI networks defined by `config
1860 * interface` sections in `/etc/config/network`.
1862 Protocol
= baseclass
.extend(/** @lends LuCI.network.Protocol.prototype */ {
1863 __init__: function(name
) {
1867 _get: function(opt
) {
1868 var val
= uci
.get('network', this.sid
, opt
);
1870 if (Array
.isArray(val
))
1871 return val
.join(' ');
1876 _ubus: function(field
) {
1877 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
1878 if (_state
.ifaces
[i
].interface != this.sid
)
1881 return (field
!= null ? _state
.ifaces
[i
][field
] : _state
.ifaces
[i
]);
1886 * Read the given UCI option value of this network.
1888 * @param {string} opt
1889 * The UCI option name to read.
1891 * @returns {null|string|string[]}
1892 * Returns the UCI option value or `null` if the requested option is
1895 get: function(opt
) {
1896 return uci
.get('network', this.sid
, opt
);
1900 * Set the given UCI option of this network to the given value.
1902 * @param {string} opt
1903 * The name of the UCI option to set.
1905 * @param {null|string|string[]} val
1906 * The value to set or `null` to remove the given option from the
1909 set: function(opt
, val
) {
1910 return uci
.set('network', this.sid
, opt
, val
);
1914 * Get the associared Linux network device of this network.
1916 * @returns {null|string}
1917 * Returns the name of the associated network device or `null` if
1918 * it could not be determined.
1920 getIfname: function() {
1923 if (this.isFloating())
1924 ifname
= this._ubus('l3_device');
1926 ifname
= this._ubus('device') || this._ubus('l3_device');
1931 var res
= getWifiNetidByNetname(this.sid
);
1932 return (res
!= null ? res
[0] : null);
1936 * Get the name of this network protocol class.
1938 * This function will be overwritten by subclasses created by
1939 * {@link LuCI.network#registerProtocol Network.registerProtocol()}.
1943 * Returns the name of the network protocol implementation, e.g.
1944 * `static` or `dhcp`.
1946 getProtocol: function() {
1951 * Return a human readable description for the protcol, such as
1952 * `Static address` or `DHCP client`.
1954 * This function should be overwritten by subclasses.
1958 * Returns the description string.
1960 getI18n: function() {
1961 switch (this.getProtocol()) {
1962 case 'none': return _('Unmanaged');
1963 case 'static': return _('Static address');
1964 case 'dhcp': return _('DHCP client');
1965 default: return _('Unknown');
1970 * Get the type of the underlying interface.
1972 * This function actually is a convenience wrapper around
1973 * `proto.get("type")` and is mainly used by other `LuCI.network` code
1974 * to check whether the interface is declared as bridge in UCI.
1976 * @returns {null|string}
1977 * Returns the value of the `type` option of the associated logical
1978 * interface or `null` if no `type` option is set.
1980 getType: function() {
1981 return this._get('type');
1985 * Get the name of the associated logical interface.
1988 * Returns the logical interface name, such as `lan` or `wan`.
1990 getName: function() {
1995 * Get the uptime of the logical interface.
1998 * Returns the uptime of the associated interface in seconds.
2000 getUptime: function() {
2001 return this._ubus('uptime') || 0;
2005 * Get the logical interface expiry time in seconds.
2007 * For protocols that have a concept of a lease, such as DHCP or
2008 * DHCPv6, this function returns the remaining time in seconds
2009 * until the lease expires.
2012 * Returns the amount of seconds until the lease expires or `-1`
2013 * if it isn't applicable to the associated protocol.
2015 getExpiry: function() {
2016 var u
= this._ubus('uptime'),
2017 d
= this._ubus('data');
2019 if (typeof(u
) == 'number' && d
!= null &&
2020 typeof(d
) == 'object' && typeof(d
.leasetime
) == 'number') {
2021 var r
= d
.leasetime
- (u
% d
.leasetime
);
2022 return (r
> 0 ? r
: 0);
2029 * Get the metric value of the logical interface.
2032 * Returns the current metric value used for device and network
2033 * routes spawned by the associated logical interface.
2035 getMetric: function() {
2036 return this._ubus('metric') || 0;
2040 * Get the requested firewall zone name of the logical interface.
2042 * Some protocol implementations request a specific firewall zone
2043 * to trigger inclusion of their resulting network devices into the
2044 * firewall rule set.
2046 * @returns {null|string}
2047 * Returns the requested firewall zone name as published in the
2048 * `ubus` runtime information or `null` if the remote protocol
2049 * handler didn't request a zone.
2051 getZoneName: function() {
2052 var d
= this._ubus('data');
2054 if (L
.isObject(d
) && typeof(d
.zone
) == 'string')
2061 * Query the first (primary) IPv4 address of the logical interface.
2063 * @returns {null|string}
2064 * Returns the primary IPv4 address registered by the protocol handler
2065 * or `null` if no IPv4 addresses were set.
2067 getIPAddr: function() {
2068 var addrs
= this._ubus('ipv4-address');
2069 return ((Array
.isArray(addrs
) && addrs
.length
) ? addrs
[0].address
: null);
2073 * Query all IPv4 addresses of the logical interface.
2075 * @returns {string[]}
2076 * Returns an array of IPv4 addresses in CIDR notation which have been
2077 * registered by the protocol handler. The order of the resulting array
2078 * follows the order of the addresses in `ubus` runtime information.
2080 getIPAddrs: function() {
2081 var addrs
= this._ubus('ipv4-address'),
2084 if (Array
.isArray(addrs
))
2085 for (var i
= 0; i
< addrs
.length
; i
++)
2086 rv
.push('%s/%d'.format(addrs
[i
].address
, addrs
[i
].mask
));
2092 * Query the first (primary) IPv4 netmask of the logical interface.
2094 * @returns {null|string}
2095 * Returns the netmask of the primary IPv4 address registered by the
2096 * protocol handler or `null` if no IPv4 addresses were set.
2098 getNetmask: function() {
2099 var addrs
= this._ubus('ipv4-address');
2100 if (Array
.isArray(addrs
) && addrs
.length
)
2101 return prefixToMask(addrs
[0].mask
, false);
2105 * Query the gateway (nexthop) of the default route associated with
2106 * this logical interface.
2109 * Returns a string containing the IPv4 nexthop address of the associated
2110 * default route or `null` if no default route was found.
2112 getGatewayAddr: function() {
2113 var routes
= this._ubus('route');
2115 if (Array
.isArray(routes
))
2116 for (var i
= 0; i
< routes
.length
; i
++)
2117 if (typeof(routes
[i
]) == 'object' &&
2118 routes
[i
].target
== '0.0.0.0' &&
2119 routes
[i
].mask
== 0)
2120 return routes
[i
].nexthop
;
2126 * Query the IPv4 DNS servers associated with the logical interface.
2128 * @returns {string[]}
2129 * Returns an array of IPv4 DNS servers registered by the remote
2132 getDNSAddrs: function() {
2133 var addrs
= this._ubus('dns-server'),
2136 if (Array
.isArray(addrs
))
2137 for (var i
= 0; i
< addrs
.length
; i
++)
2138 if (!/:/.test(addrs
[i
]))
2145 * Query the first (primary) IPv6 address of the logical interface.
2147 * @returns {null|string}
2148 * Returns the primary IPv6 address registered by the protocol handler
2149 * in CIDR notation or `null` if no IPv6 addresses were set.
2151 getIP6Addr: function() {
2152 var addrs
= this._ubus('ipv6-address');
2154 if (Array
.isArray(addrs
) && L
.isObject(addrs
[0]))
2155 return '%s/%d'.format(addrs
[0].address
, addrs
[0].mask
);
2157 addrs
= this._ubus('ipv6-prefix-assignment');
2159 if (Array
.isArray(addrs
) && L
.isObject(addrs
[0]) && L
.isObject(addrs
[0]['local-address']))
2160 return '%s/%d'.format(addrs
[0]['local-address'].address
, addrs
[0]['local-address'].mask
);
2166 * Query all IPv6 addresses of the logical interface.
2168 * @returns {string[]}
2169 * Returns an array of IPv6 addresses in CIDR notation which have been
2170 * registered by the protocol handler. The order of the resulting array
2171 * follows the order of the addresses in `ubus` runtime information.
2173 getIP6Addrs: function() {
2174 var addrs
= this._ubus('ipv6-address'),
2177 if (Array
.isArray(addrs
))
2178 for (var i
= 0; i
< addrs
.length
; i
++)
2179 if (L
.isObject(addrs
[i
]))
2180 rv
.push('%s/%d'.format(addrs
[i
].address
, addrs
[i
].mask
));
2182 addrs
= this._ubus('ipv6-prefix-assignment');
2184 if (Array
.isArray(addrs
))
2185 for (var i
= 0; i
< addrs
.length
; i
++)
2186 if (L
.isObject(addrs
[i
]) && L
.isObject(addrs
[i
]['local-address']))
2187 rv
.push('%s/%d'.format(addrs
[i
]['local-address'].address
, addrs
[i
]['local-address'].mask
));
2193 * Query the gateway (nexthop) of the IPv6 default route associated with
2194 * this logical interface.
2197 * Returns a string containing the IPv6 nexthop address of the associated
2198 * default route or `null` if no default route was found.
2200 getGateway6Addr: function() {
2201 var routes
= this._ubus('route');
2203 if (Array
.isArray(routes
))
2204 for (var i
= 0; i
< routes
.length
; i
++)
2205 if (typeof(routes
[i
]) == 'object' &&
2206 routes
[i
].target
== '::' &&
2207 routes
[i
].mask
== 0)
2208 return routes
[i
].nexthop
;
2214 * Query the IPv6 DNS servers associated with the logical interface.
2216 * @returns {string[]}
2217 * Returns an array of IPv6 DNS servers registered by the remote
2220 getDNS6Addrs: function() {
2221 var addrs
= this._ubus('dns-server'),
2224 if (Array
.isArray(addrs
))
2225 for (var i
= 0; i
< addrs
.length
; i
++)
2226 if (/:/.test(addrs
[i
]))
2233 * Query the routed IPv6 prefix associated with the logical interface.
2235 * @returns {null|string}
2236 * Returns the routed IPv6 prefix registered by the remote protocol
2237 * handler or `null` if no prefix is present.
2239 getIP6Prefix: function() {
2240 var prefixes
= this._ubus('ipv6-prefix');
2242 if (Array
.isArray(prefixes
) && L
.isObject(prefixes
[0]))
2243 return '%s/%d'.format(prefixes
[0].address
, prefixes
[0].mask
);
2249 * Query interface error messages published in `ubus` runtime state.
2251 * Interface errors are emitted by remote protocol handlers if the setup
2252 * of the underlying logical interface failed, e.g. due to bad
2253 * configuration or network connectivity issues.
2255 * This function will translate the found error codes to human readable
2256 * messages using the descriptions registered by
2257 * {@link LuCI.network#registerErrorCode Network.registerErrorCode()}
2258 * and fall back to `"Unknown error (%s)"` where `%s` is replaced by the
2259 * error code in case no translation can be found.
2261 * @returns {string[]}
2262 * Returns an array of translated interface error messages.
2264 getErrors: function() {
2265 var errors
= this._ubus('errors'),
2268 if (Array
.isArray(errors
)) {
2269 for (var i
= 0; i
< errors
.length
; i
++) {
2270 if (!L
.isObject(errors
[i
]) || typeof(errors
[i
].code
) != 'string')
2274 rv
.push(proto_errors
[errors
[i
].code
] || _('Unknown error (%s)').format(errors
[i
].code
));
2282 * Checks whether the underlying logical interface is declared as bridge.
2284 * @returns {boolean}
2285 * Returns `true` when the interface is declared with `option type bridge`
2286 * and when the associated protocol implementation is not marked virtual
2287 * or `false` when the logical interface is no bridge.
2289 isBridge: function() {
2290 return (!this.isVirtual() && this.getType() == 'bridge');
2294 * Get the name of the opkg package providing the protocol functionality.
2296 * This function should be overwritten by protocol specific subclasses.
2301 * Returns the name of the opkg package required for the protocol to
2302 * function, e.g. `odhcp6c` for the `dhcpv6` prototocol.
2304 getOpkgPackage: function() {
2309 * Check function for the protocol handler if a new interface is createable.
2311 * This function should be overwritten by protocol specific subclasses.
2315 * @param {string} ifname
2316 * The name of the interface to be created.
2318 * @returns {Promise<void>}
2319 * Returns a promise resolving if new interface is createable, else
2320 * rejects with an error message string.
2322 isCreateable: function(ifname
) {
2323 return Promise
.resolve(null);
2327 * Checks whether the protocol functionality is installed.
2329 * This function exists for compatibility with old code, it always
2335 * @returns {boolean}
2336 * Returns `true` if the protocol support is installed, else `false`.
2338 isInstalled: function() {
2343 * Checks whether this protocol is "virtual".
2345 * A "virtual" protocol is a protocol which spawns its own interfaces
2346 * on demand instead of using existing physical interfaces.
2348 * Examples for virtual protocols are `6in4` which `gre` spawn tunnel
2349 * network device on startup, examples for non-virtual protcols are
2350 * `dhcp` or `static` which apply IP configuration to existing interfaces.
2352 * This function should be overwritten by subclasses.
2354 * @returns {boolean}
2355 * Returns a boolean indicating whether the underlying protocol spawns
2356 * dynamic interfaces (`true`) or not (`false`).
2358 isVirtual: function() {
2363 * Checks whether this protocol is "floating".
2365 * A "floating" protocol is a protocol which spawns its own interfaces
2366 * on demand, like a virtual one but which relies on an existinf lower
2367 * level interface to initiate the connection.
2369 * An example for such a protocol is "pppoe".
2371 * This function exists for backwards compatibility with older code
2372 * but should not be used anymore.
2375 * @returns {boolean}
2376 * Returns a boolean indicating whether this protocol is floating (`true`)
2379 isFloating: function() {
2384 * Checks whether this logical interface is dynamic.
2386 * A dynamic interface is an interface which has been created at runtime,
2387 * e.g. as sub-interface of another interface, but which is not backed by
2388 * any user configuration. Such dynamic interfaces cannot be edited but
2389 * only brought down or restarted.
2391 * @returns {boolean}
2392 * Returns a boolean indicating whether this interface is dynamic (`true`)
2395 isDynamic: function() {
2396 return (this._ubus('dynamic') == true);
2400 * Checks whether this interface is an alias interface.
2402 * Alias interfaces are interfaces layering on top of another interface
2403 * and are denoted by a special `@interfacename` notation in the
2404 * underlying `ifname` option.
2406 * @returns {null|string}
2407 * Returns the name of the parent interface if this logical interface
2408 * is an alias or `null` if it is not an alias interface.
2410 isAlias: function() {
2411 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'ifname')),
2414 for (var i
= 0; i
< ifnames
.length
; i
++)
2415 if (ifnames
[i
].charAt(0) == '@')
2416 parent
= ifnames
[i
].substr(1);
2417 else if (parent
!= null)
2424 * Checks whether this logical interface is "empty", meaning that ut
2425 * has no network devices attached.
2427 * @returns {boolean}
2428 * Returns `true` if this logical interface is empty, else `false`.
2430 isEmpty: function() {
2431 if (this.isFloating())
2435 ifname
= this._get('ifname');
2437 if (ifname
!= null && ifname
.match(/\S+/))
2440 if (empty
== true && getWifiNetidBySid(this.sid
) != null)
2447 * Checks whether this logical interface is configured and running.
2449 * @returns {boolean}
2450 * Returns `true` when the interface is active or `false` when it is not.
2453 return (this._ubus('up') == true);
2457 * Add the given network device to the logical interface.
2459 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2460 * The object or device name to add to the logical interface. In case the
2461 * given argument is not a string, it is resolved though the
2462 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2464 * @returns {boolean}
2465 * Returns `true` if the device name has been added or `false` if any
2466 * argument was invalid, if the device was already part of the logical
2467 * interface or if the logical interface is virtual.
2469 addDevice: function(ifname
) {
2470 ifname
= ifnameOf(ifname
);
2472 if (ifname
== null || this.isFloating())
2475 var wif
= getWifiSidByIfname(ifname
);
2478 return appendValue('wireless', wif
, 'network', this.sid
);
2480 return appendValue('network', this.sid
, 'ifname', ifname
);
2484 * Remove the given network device from the logical interface.
2486 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2487 * The object or device name to remove from the logical interface. In case
2488 * the given argument is not a string, it is resolved though the
2489 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2491 * @returns {boolean}
2492 * Returns `true` if the device name has been added or `false` if any
2493 * argument was invalid, if the device was already part of the logical
2494 * interface or if the logical interface is virtual.
2496 deleteDevice: function(ifname
) {
2499 ifname
= ifnameOf(ifname
);
2501 if (ifname
== null || this.isFloating())
2504 var wif
= getWifiSidByIfname(ifname
);
2507 rv
= removeValue('wireless', wif
, 'network', this.sid
);
2509 if (removeValue('network', this.sid
, 'ifname', ifname
))
2516 * Returns the Linux network device associated with this logical
2519 * @returns {LuCI.network.Device}
2520 * Returns a `Network.Device` class instance representing the
2521 * expected Linux network device according to the configuration.
2523 getDevice: function() {
2524 if (this.isVirtual()) {
2525 var ifname
= '%s-%s'.format(this.getProtocol(), this.sid
);
2526 _state
.isTunnel
[this.getProtocol() + '-' + this.sid
] = true;
2527 return Network
.prototype.instantiateDevice(ifname
, this);
2529 else if (this.isBridge()) {
2530 var ifname
= 'br-%s'.format(this.sid
);
2531 _state
.isBridge
[ifname
] = true;
2532 return new Device(ifname
, this);
2535 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'ifname'));
2537 for (var i
= 0; i
< ifnames
.length
; i
++) {
2538 var m
= ifnames
[i
].match(/^([^:/]+)/);
2539 return ((m
&& m
[1]) ? Network
.prototype.instantiateDevice(m
[1], this) : null);
2542 ifname
= getWifiNetidByNetname(this.sid
);
2544 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
[0], this) : null);
2549 * Returns the layer 2 linux network device currently associated
2550 * with this logical interface.
2552 * @returns {LuCI.network.Device}
2553 * Returns a `Network.Device` class instance representing the Linux
2554 * network device currently associated with the logical interface.
2556 getL2Device: function() {
2557 var ifname
= this._ubus('device');
2558 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
, this) : null);
2562 * Returns the layer 3 linux network device currently associated
2563 * with this logical interface.
2565 * @returns {LuCI.network.Device}
2566 * Returns a `Network.Device` class instance representing the Linux
2567 * network device currently associated with the logical interface.
2569 getL3Device: function() {
2570 var ifname
= this._ubus('l3_device');
2571 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
, this) : null);
2575 * Returns a list of network sub-devices associated with this logical
2578 * @returns {null|Array<LuCI.network.Device>}
2579 * Returns an array of of `Network.Device` class instances representing
2580 * the sub-devices attached to this logical interface or `null` if the
2581 * logical interface does not support sub-devices, e.g. because it is
2582 * virtual and not a bridge.
2584 getDevices: function() {
2587 if (!this.isBridge() && !(this.isVirtual() && !this.isFloating()))
2590 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'ifname'));
2592 for (var i
= 0; i
< ifnames
.length
; i
++) {
2593 if (ifnames
[i
].charAt(0) == '@')
2596 var m
= ifnames
[i
].match(/^([^:/]+)/);
2598 rv
.push(Network
.prototype.instantiateDevice(m
[1], this));
2601 var uciWifiIfaces
= uci
.sections('wireless', 'wifi-iface');
2603 for (var i
= 0; i
< uciWifiIfaces
.length
; i
++) {
2604 if (typeof(uciWifiIfaces
[i
].device
) != 'string')
2607 var networks
= L
.toArray(uciWifiIfaces
[i
].network
);
2609 for (var j
= 0; j
< networks
.length
; j
++) {
2610 if (networks
[j
] != this.sid
)
2613 var netid
= getWifiNetidBySid(uciWifiIfaces
[i
]['.name']);
2616 rv
.push(Network
.prototype.instantiateDevice(netid
[0], this));
2620 rv
.sort(deviceSort
);
2626 * Checks whether this logical interface contains the given device
2629 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2630 * The object or device name to check. In case the given argument is not
2631 * a string, it is resolved though the
2632 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2634 * @returns {boolean}
2635 * Returns `true` when this logical interface contains the given network
2636 * device or `false` if not.
2638 containsDevice: function(ifname
) {
2639 ifname
= ifnameOf(ifname
);
2643 else if (this.isVirtual() && '%s-%s'.format(this.getProtocol(), this.sid
) == ifname
)
2645 else if (this.isBridge() && 'br-%s'.format(this.sid
) == ifname
)
2648 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'ifname'));
2650 for (var i
= 0; i
< ifnames
.length
; i
++) {
2651 var m
= ifnames
[i
].match(/^([^:/]+)/);
2652 if (m
!= null && m
[1] == ifname
)
2656 var wif
= getWifiSidByIfname(ifname
);
2659 var networks
= L
.toArray(uci
.get('wireless', wif
, 'network'));
2661 for (var i
= 0; i
< networks
.length
; i
++)
2662 if (networks
[i
] == this.sid
)
2670 * Cleanup related configuration entries.
2672 * This function will be invoked if an interface is about to be removed
2673 * from the configuration and is responsible for performing any required
2674 * cleanup tasks, such as unsetting uci entries in related configurations.
2676 * It should be overwritten by protocol specific subclasses.
2680 * @returns {*|Promise<*>}
2681 * This function may return a promise which is awaited before the rest of
2682 * the configuration is removed. Any non-promise return value and any
2683 * resolved promise value is ignored. If the returned promise is rejected,
2684 * the interface removal will be aborted.
2686 deleteConfiguration: function() {}
2691 * @memberof LuCI.network
2695 * A `Network.Device` class instance represents an underlying Linux network
2696 * device and allows querying device details such as packet statistics or MTU.
2698 Device
= baseclass
.extend(/** @lends LuCI.network.Device.prototype */ {
2699 __init__: function(ifname
, network
) {
2700 var wif
= getWifiSidByIfname(ifname
);
2703 var res
= getWifiStateBySid(wif
) || [],
2704 netid
= getWifiNetidBySid(wif
) || [];
2706 this.wif
= new WifiNetwork(wif
, res
[0], res
[1], netid
[0], res
[2], { ifname
: ifname
});
2707 this.ifname
= this.wif
.getIfname();
2710 this.ifname
= this.ifname
|| ifname
;
2711 this.dev
= _state
.netdevs
[this.ifname
];
2712 this.network
= network
;
2715 _devstate: function(/* ... */) {
2718 for (var i
= 0; i
< arguments
.length
; i
++)
2720 rv
= rv
[arguments
[i
]];
2728 * Get the name of the network device.
2731 * Returns the name of the device, e.g. `eth0` or `wlan0`.
2733 getName: function() {
2734 return (this.wif
!= null ? this.wif
.getIfname() : this.ifname
);
2738 * Get the MAC address of the device.
2740 * @returns {null|string}
2741 * Returns the MAC address of the device or `null` if not applicable,
2742 * e.g. for non-ethernet tunnel devices.
2744 getMAC: function() {
2745 var mac
= this._devstate('macaddr');
2746 return mac
? mac
.toUpperCase() : null;
2750 * Get the MTU of the device.
2753 * Returns the MTU of the device.
2755 getMTU: function() {
2756 return this._devstate('mtu');
2760 * Get the IPv4 addresses configured on the device.
2762 * @returns {string[]}
2763 * Returns an array of IPv4 address strings.
2765 getIPAddrs: function() {
2766 var addrs
= this._devstate('ipaddrs');
2767 return (Array
.isArray(addrs
) ? addrs
: []);
2771 * Get the IPv6 addresses configured on the device.
2773 * @returns {string[]}
2774 * Returns an array of IPv6 address strings.
2776 getIP6Addrs: function() {
2777 var addrs
= this._devstate('ip6addrs');
2778 return (Array
.isArray(addrs
) ? addrs
: []);
2782 * Get the type of the device..
2785 * Returns a string describing the type of the network device:
2786 * - `alias` if it is an abstract alias device (`@` notation)
2787 * - `wifi` if it is a wireless interface (e.g. `wlan0`)
2788 * - `bridge` if it is a bridge device (e.g. `br-lan`)
2789 * - `tunnel` if it is a tun or tap device (e.g. `tun0`)
2790 * - `vlan` if it is a vlan device (e.g. `eth0.1`)
2791 * - `switch` if it is a switch device (e.g.`eth1` connected to switch0)
2792 * - `ethernet` for all other device types
2794 getType: function() {
2795 if (this.ifname
!= null && this.ifname
.charAt(0) == '@')
2797 else if (this.wif
!= null || isWifiIfname(this.ifname
))
2799 else if (_state
.isBridge
[this.ifname
])
2801 else if (_state
.isTunnel
[this.ifname
])
2803 else if (this.ifname
.indexOf('.') > -1)
2805 else if (_state
.isSwitch
[this.ifname
])
2812 * Get a short description string for the device.
2815 * Returns the device name for non-wifi devices or a string containing
2816 * the operation mode and SSID for wifi devices.
2818 getShortName: function() {
2819 if (this.wif
!= null)
2820 return this.wif
.getShortName();
2826 * Get a long description string for the device.
2829 * Returns a string containing the type description and device name
2830 * for non-wifi devices or operation mode and ssid for wifi ones.
2832 getI18n: function() {
2833 if (this.wif
!= null) {
2834 return '%s: %s "%s"'.format(
2835 _('Wireless Network'),
2836 this.wif
.getActiveMode(),
2837 this.wif
.getActiveSSID() || this.wif
.getActiveBSSID() || this.wif
.getID() || '?');
2840 return '%s: "%s"'.format(this.getTypeI18n(), this.getName());
2844 * Get a string describing the device type.
2847 * Returns a string describing the type, e.g. "Wireless Adapter" or
2850 getTypeI18n: function() {
2851 switch (this.getType()) {
2853 return _('Alias Interface');
2856 return _('Wireless Adapter');
2862 return _('Ethernet Switch');
2865 return (_state
.isSwitch
[this.ifname
] ? _('Switch VLAN') : _('Software VLAN'));
2868 return _('Tunnel Interface');
2871 return _('Ethernet Adapter');
2876 * Get the associated bridge ports of the device.
2878 * @returns {null|Array<LuCI.network.Device>}
2879 * Returns an array of `Network.Device` instances representing the ports
2880 * (slave interfaces) of the bridge or `null` when this device isn't
2883 getPorts: function() {
2884 var br
= _state
.bridges
[this.ifname
],
2887 if (br
== null || !Array
.isArray(br
.ifnames
))
2890 for (var i
= 0; i
< br
.ifnames
.length
; i
++)
2891 rv
.push(Network
.prototype.instantiateDevice(br
.ifnames
[i
].name
));
2893 rv
.sort(deviceSort
);
2901 * @returns {null|string}
2902 * Returns the ID of this network bridge or `null` if this network
2903 * device is not a Linux bridge.
2905 getBridgeID: function() {
2906 var br
= _state
.bridges
[this.ifname
];
2907 return (br
!= null ? br
.id
: null);
2911 * Get the bridge STP setting
2913 * @returns {boolean}
2914 * Returns `true` when this device is a Linux bridge and has `stp`
2915 * enabled, else `false`.
2917 getBridgeSTP: function() {
2918 var br
= _state
.bridges
[this.ifname
];
2919 return (br
!= null ? !!br
.stp
: false);
2923 * Checks whether this device is up.
2925 * @returns {boolean}
2926 * Returns `true` when the associated device is running pr `false`
2927 * when it is down or absent.
2930 var up
= this._devstate('flags', 'up');
2933 up
= (this.getType() == 'alias');
2939 * Checks whether this device is a Linux bridge.
2941 * @returns {boolean}
2942 * Returns `true` when the network device is present and a Linux bridge,
2945 isBridge: function() {
2946 return (this.getType() == 'bridge');
2950 * Checks whether this device is part of a Linux bridge.
2952 * @returns {boolean}
2953 * Returns `true` when this network device is part of a bridge,
2956 isBridgePort: function() {
2957 return (this._devstate('bridge') != null);
2961 * Get the amount of transmitted bytes.
2964 * Returns the amount of bytes transmitted by the network device.
2966 getTXBytes: function() {
2967 var stat
= this._devstate('stats');
2968 return (stat
!= null ? stat
.tx_bytes
|| 0 : 0);
2972 * Get the amount of received bytes.
2975 * Returns the amount of bytes received by the network device.
2977 getRXBytes: function() {
2978 var stat
= this._devstate('stats');
2979 return (stat
!= null ? stat
.rx_bytes
|| 0 : 0);
2983 * Get the amount of transmitted packets.
2986 * Returns the amount of packets transmitted by the network device.
2988 getTXPackets: function() {
2989 var stat
= this._devstate('stats');
2990 return (stat
!= null ? stat
.tx_packets
|| 0 : 0);
2994 * Get the amount of received packets.
2997 * Returns the amount of packets received by the network device.
2999 getRXPackets: function() {
3000 var stat
= this._devstate('stats');
3001 return (stat
!= null ? stat
.rx_packets
|| 0 : 0);
3005 * Get the primary logical interface this device is assigned to.
3007 * @returns {null|LuCI.network.Protocol}
3008 * Returns a `Network.Protocol` instance representing the logical
3009 * interface this device is attached to or `null` if it is not
3010 * assigned to any logical interface.
3012 getNetwork: function() {
3013 return this.getNetworks()[0];
3017 * Get the logical interfaces this device is assigned to.
3019 * @returns {Array<LuCI.network.Protocol>}
3020 * Returns an array of `Network.Protocol` instances representing the
3021 * logical interfaces this device is assigned to.
3023 getNetworks: function() {
3024 if (this.networks
== null) {
3027 var networks
= enumerateNetworks
.apply(L
.network
);
3029 for (var i
= 0; i
< networks
.length
; i
++)
3030 if (networks
[i
].containsDevice(this.ifname
) || networks
[i
].getIfname() == this.ifname
)
3031 this.networks
.push(networks
[i
]);
3033 this.networks
.sort(networkSort
);
3036 return this.networks
;
3040 * Get the related wireless network this device is related to.
3042 * @returns {null|LuCI.network.WifiNetwork}
3043 * Returns a `Network.WifiNetwork` instance representing the wireless
3044 * network corresponding to this network device or `null` if this device
3045 * is not a wireless device.
3047 getWifiNetwork: function() {
3048 return (this.wif
!= null ? this.wif
: null);
3054 * @memberof LuCI.network
3058 * A `Network.WifiDevice` class instance represents a wireless radio device
3059 * present on the system and provides wireless capability information as
3060 * well as methods for enumerating related wireless networks.
3062 WifiDevice
= baseclass
.extend(/** @lends LuCI.network.WifiDevice.prototype */ {
3063 __init__: function(name
, radiostate
) {
3064 var uciWifiDevice
= uci
.get('wireless', name
);
3066 if (uciWifiDevice
!= null &&
3067 uciWifiDevice
['.type'] == 'wifi-device' &&
3068 uciWifiDevice
['.name'] != null) {
3069 this.sid
= uciWifiDevice
['.name'];
3072 this.sid
= this.sid
|| name
;
3080 ubus: function(/* ... */) {
3081 var v
= this._ubusdata
;
3083 for (var i
= 0; i
< arguments
.length
; i
++)
3085 v
= v
[arguments
[i
]];
3093 * Read the given UCI option value of this wireless device.
3095 * @param {string} opt
3096 * The UCI option name to read.
3098 * @returns {null|string|string[]}
3099 * Returns the UCI option value or `null` if the requested option is
3102 get: function(opt
) {
3103 return uci
.get('wireless', this.sid
, opt
);
3107 * Set the given UCI option of this network to the given value.
3109 * @param {string} opt
3110 * The name of the UCI option to set.
3112 * @param {null|string|string[]} val
3113 * The value to set or `null` to remove the given option from the
3116 set: function(opt
, value
) {
3117 return uci
.set('wireless', this.sid
, opt
, value
);
3121 * Checks whether this wireless radio is disabled.
3123 * @returns {boolean}
3124 * Returns `true` when the wireless radio is marked as disabled in `ubus`
3125 * runtime state or when the `disabled` option is set in the corresponding
3126 * UCI configuration.
3128 isDisabled: function() {
3129 return this.ubus('dev', 'disabled') || this.get('disabled') == '1';
3133 * Get the configuration name of this wireless radio.
3136 * Returns the UCI section name (e.g. `radio0`) of the corresponding
3137 * radio configuration which also serves as unique logical identifier
3138 * for the wireless phy.
3140 getName: function() {
3145 * Gets a list of supported hwmodes.
3147 * The hwmode values describe the frequency band and wireless standard
3148 * versions supported by the wireless phy.
3150 * @returns {string[]}
3151 * Returns an array of valid hwmode values for this radio. Currently
3152 * known mode values are:
3153 * - `a` - Legacy 802.11a mode, 5 GHz, up to 54 Mbit/s
3154 * - `b` - Legacy 802.11b mode, 2.4 GHz, up to 11 Mbit/s
3155 * - `g` - Legacy 802.11g mode, 2.4 GHz, up to 54 Mbit/s
3156 * - `n` - IEEE 802.11n mode, 2.4 or 5 GHz, up to 600 Mbit/s
3157 * - `ac` - IEEE 802.11ac mode, 5 GHz, up to 6770 Mbit/s
3159 getHWModes: function() {
3160 var hwmodes
= this.ubus('dev', 'iwinfo', 'hwmodes');
3161 return Array
.isArray(hwmodes
) ? hwmodes
: [ 'b', 'g' ];
3165 * Gets a list of supported htmodes.
3167 * The htmode values describe the wide-frequency options supported by
3170 * @returns {string[]}
3171 * Returns an array of valid htmode values for this radio. Currently
3172 * known mode values are:
3173 * - `HT20` - applicable to IEEE 802.11n, 20 MHz wide channels
3174 * - `HT40` - applicable to IEEE 802.11n, 40 MHz wide channels
3175 * - `VHT20` - applicable to IEEE 802.11ac, 20 MHz wide channels
3176 * - `VHT40` - applicable to IEEE 802.11ac, 40 MHz wide channels
3177 * - `VHT80` - applicable to IEEE 802.11ac, 80 MHz wide channels
3178 * - `VHT160` - applicable to IEEE 802.11ac, 160 MHz wide channels
3180 getHTModes: function() {
3181 var htmodes
= this.ubus('dev', 'iwinfo', 'htmodes');
3182 return (Array
.isArray(htmodes
) && htmodes
.length
) ? htmodes
: null;
3186 * Get a string describing the wireless radio hardware.
3189 * Returns the description string.
3191 getI18n: function() {
3192 var hw
= this.ubus('dev', 'iwinfo', 'hardware'),
3193 type
= L
.isObject(hw
) ? hw
.name
: null;
3195 if (this.ubus('dev', 'iwinfo', 'type') == 'wl')
3198 var hwmodes
= this.getHWModes(),
3201 hwmodes
.sort(function(a
, b
) {
3202 return (a
.length
!= b
.length
? a
.length
> b
.length
: a
> b
);
3205 modestr
= hwmodes
.join('');
3207 return '%s 802.11%s Wireless Controller (%s)'.format(type
|| 'Generic', modestr
, this.getName());
3211 * A wireless scan result object describes a neighbouring wireless
3212 * network found in the vincinity.
3214 * @typedef {Object<string, number|string|LuCI.network.WifiEncryption>} WifiScanResult
3215 * @memberof LuCI.network
3217 * @property {string} ssid
3218 * The SSID / Mesh ID of the network.
3220 * @property {string} bssid
3221 * The BSSID if the network.
3223 * @property {string} mode
3224 * The operation mode of the network (`Master`, `Ad-Hoc`, `Mesh Point`).
3226 * @property {number} channel
3227 * The wireless channel of the network.
3229 * @property {number} signal
3230 * The received signal strength of the network in dBm.
3232 * @property {number} quality
3233 * The numeric quality level of the signal, can be used in conjunction
3234 * with `quality_max` to calculate a quality percentage.
3236 * @property {number} quality_max
3237 * The maximum possible quality level of the signal, can be used in
3238 * conjunction with `quality` to calculate a quality percentage.
3240 * @property {LuCI.network.WifiEncryption} encryption
3241 * The encryption used by the wireless network.
3245 * Trigger a wireless scan on this radio device and obtain a list of
3248 * @returns {Promise<Array<LuCI.network.WifiScanResult>>}
3249 * Returns a promise resolving to an array of scan result objects
3250 * describing the networks found in the vincinity.
3252 getScanList: function() {
3253 return callIwinfoScan(this.sid
);
3257 * Check whether the wireless radio is marked as up in the `ubus`
3260 * @returns {boolean}
3261 * Returns `true` when the radio device is up, else `false`.
3264 if (L
.isObject(_state
.radios
[this.sid
]))
3265 return (_state
.radios
[this.sid
].up
== true);
3271 * Get the wifi network of the given name belonging to this radio device
3273 * @param {string} network
3274 * The name of the wireless network to lookup. This may be either an uci
3275 * configuration section ID, a network ID in the form `radio#.network#`
3276 * or a Linux network device name like `wlan0` which is resolved to the
3277 * corresponding configuration section through `ubus` runtime information.
3279 * @returns {Promise<LuCI.network.WifiNetwork>}
3280 * Returns a promise resolving to a `Network.WifiNetwork` instance
3281 * representing the wireless network and rejecting with `null` if
3282 * the given network could not be found or is not associated with
3283 * this radio device.
3285 getWifiNetwork: function(network
) {
3286 return Network
.prototype.getWifiNetwork(network
).then(L
.bind(function(networkInstance
) {
3287 var uciWifiIface
= (networkInstance
.sid
? uci
.get('wireless', networkInstance
.sid
) : null);
3289 if (uciWifiIface
== null || uciWifiIface
['.type'] != 'wifi-iface' || uciWifiIface
.device
!= this.sid
)
3290 return Promise
.reject();
3292 return networkInstance
;
3297 * Get all wireless networks associated with this wireless radio device.
3299 * @returns {Promise<Array<LuCI.network.WifiNetwork>>}
3300 * Returns a promise resolving to an array of `Network.WifiNetwork`
3301 * instances respresenting the wireless networks associated with this
3304 getWifiNetworks: function() {
3305 return Network
.prototype.getWifiNetworks().then(L
.bind(function(networks
) {
3308 for (var i
= 0; i
< networks
.length
; i
++)
3309 if (networks
[i
].getWifiDeviceName() == this.getName())
3310 rv
.push(networks
[i
]);
3317 * Adds a new wireless network associated with this radio device to the
3318 * configuration and sets its options to the provided values.
3320 * @param {Object<string, string|string[]>} [options]
3321 * The options to set for the newly added wireless network.
3323 * @returns {Promise<null|LuCI.network.WifiNetwork>}
3324 * Returns a promise resolving to a `WifiNetwork` instance describing
3325 * the newly added wireless network or `null` if the given options
3328 addWifiNetwork: function(options
) {
3329 if (!L
.isObject(options
))
3332 options
.device
= this.sid
;
3334 return Network
.prototype.addWifiNetwork(options
);
3338 * Deletes the wireless network with the given name associated with this
3341 * @param {string} network
3342 * The name of the wireless network to lookup. This may be either an uci
3343 * configuration section ID, a network ID in the form `radio#.network#`
3344 * or a Linux network device name like `wlan0` which is resolved to the
3345 * corresponding configuration section through `ubus` runtime information.
3347 * @returns {Promise<boolean>}
3348 * Returns a promise resolving to `true` when the wireless network was
3349 * successfully deleted from the configuration or `false` when the given
3350 * network could not be found or if the found network was not associated
3351 * with this wireless radio device.
3353 deleteWifiNetwork: function(network
) {
3356 if (network
instanceof WifiNetwork
) {
3360 var uciWifiIface
= uci
.get('wireless', network
);
3362 if (uciWifiIface
== null || uciWifiIface
['.type'] != 'wifi-iface')
3363 sid
= getWifiSidByIfname(network
);
3366 if (sid
== null || uci
.get('wireless', sid
, 'device') != this.sid
)
3367 return Promise
.resolve(false);
3369 uci
.delete('wireless', network
);
3371 return Promise
.resolve(true);
3377 * @memberof LuCI.network
3381 * A `Network.WifiNetwork` instance represents a wireless network (vif)
3382 * configured on top of a radio device and provides functions for querying
3383 * the runtime state of the network. Most radio devices support multiple
3384 * such networks in parallel.
3386 WifiNetwork
= baseclass
.extend(/** @lends LuCI.network.WifiNetwork.prototype */ {
3387 __init__: function(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
) {
3398 ubus: function(/* ... */) {
3399 var v
= this._ubusdata
;
3401 for (var i
= 0; i
< arguments
.length
; i
++)
3403 v
= v
[arguments
[i
]];
3411 * Read the given UCI option value of this wireless network.
3413 * @param {string} opt
3414 * The UCI option name to read.
3416 * @returns {null|string|string[]}
3417 * Returns the UCI option value or `null` if the requested option is
3420 get: function(opt
) {
3421 return uci
.get('wireless', this.sid
, opt
);
3425 * Set the given UCI option of this network to the given value.
3427 * @param {string} opt
3428 * The name of the UCI option to set.
3430 * @param {null|string|string[]} val
3431 * The value to set or `null` to remove the given option from the
3434 set: function(opt
, value
) {
3435 return uci
.set('wireless', this.sid
, opt
, value
);
3439 * Checks whether this wireless network is disabled.
3441 * @returns {boolean}
3442 * Returns `true` when the wireless radio is marked as disabled in `ubus`
3443 * runtime state or when the `disabled` option is set in the corresponding
3444 * UCI configuration.
3446 isDisabled: function() {
3447 return this.ubus('dev', 'disabled') || this.get('disabled') == '1';
3451 * Get the configured operation mode of the wireless network.
3454 * Returns the configured operation mode. Possible values are:
3455 * - `ap` - Master (Access Point) mode
3456 * - `sta` - Station (client) mode
3457 * - `adhoc` - Ad-Hoc (IBSS) mode
3458 * - `mesh` - Mesh (IEEE 802.11s) mode
3459 * - `monitor` - Monitor mode
3461 getMode: function() {
3462 return this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3466 * Get the configured SSID of the wireless network.
3468 * @returns {null|string}
3469 * Returns the configured SSID value or `null` when this network is
3472 getSSID: function() {
3473 if (this.getMode() == 'mesh')
3476 return this.ubus('net', 'config', 'ssid') || this.get('ssid');
3480 * Get the configured Mesh ID of the wireless network.
3482 * @returns {null|string}
3483 * Returns the configured mesh ID value or `null` when this network
3484 * is not in mesh mode.
3486 getMeshID: function() {
3487 if (this.getMode() != 'mesh')
3490 return this.ubus('net', 'config', 'mesh_id') || this.get('mesh_id');
3494 * Get the configured BSSID of the wireless network.
3496 * @returns {null|string}
3497 * Returns the BSSID value or `null` if none has been specified.
3499 getBSSID: function() {
3500 return this.ubus('net', 'config', 'bssid') || this.get('bssid');
3504 * Get the names of the logical interfaces this wireless network is
3507 * @returns {string[]}
3508 * Returns an array of logical interface names.
3510 getNetworkNames: function() {
3511 return L
.toArray(this.ubus('net', 'config', 'network') || this.get('network'));
3515 * Get the internal network ID of this wireless network.
3517 * The network ID is a LuCI specific identifer in the form
3518 * `radio#.network#` to identify wireless networks by their corresponding
3519 * radio and network index numbers.
3522 * Returns the LuCI specific network ID.
3529 * Get the configuration ID of this wireless network.
3532 * Returns the corresponding UCI section ID of the network.
3534 getName: function() {
3539 * Get the Linux network device name.
3541 * @returns {null|string}
3542 * Returns the current Linux network device name as resolved from
3543 * `ubus` runtime information or `null` if this network has no
3544 * associated network device, e.g. when not configured or up.
3546 getIfname: function() {
3547 var ifname
= this.ubus('net', 'ifname') || this.ubus('net', 'iwinfo', 'ifname');
3549 if (ifname
== null || ifname
.match(/^(wifi|radio)\d/))
3550 ifname
= this.netid
;
3556 * Get the name of the corresponding wifi radio device.
3558 * @returns {null|string}
3559 * Returns the name of the radio device this network is configured on
3560 * or `null` if it cannot be determined.
3562 getWifiDeviceName: function() {
3563 return this.ubus('radio') || this.get('device');
3567 * Get the corresponding wifi radio device.
3569 * @returns {null|LuCI.network.WifiDevice}
3570 * Returns a `Network.WifiDevice` instance representing the corresponding
3571 * wifi radio device or `null` if the related radio device could not be
3574 getWifiDevice: function() {
3575 var radioname
= this.getWifiDeviceName();
3577 if (radioname
== null)
3578 return Promise
.reject();
3580 return Network
.prototype.getWifiDevice(radioname
);
3584 * Check whether the radio network is up.
3586 * This function actually queries the up state of the related radio
3587 * device and assumes this network to be up as well when the parent
3588 * radio is up. This is due to the fact that OpenWrt does not control
3589 * virtual interfaces individually but within one common hostapd
3592 * @returns {boolean}
3593 * Returns `true` when the network is up, else `false`.
3596 var device
= this.getDevice();
3601 return device
.isUp();
3605 * Query the current operation mode from runtime information.
3608 * Returns the human readable mode name as reported by `ubus` runtime
3609 * state. Possible returned values are:
3621 getActiveMode: function() {
3622 var mode
= this.ubus('net', 'iwinfo', 'mode') || this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3625 case 'ap': return 'Master';
3626 case 'sta': return 'Client';
3627 case 'adhoc': return 'Ad-Hoc';
3628 case 'mesh': return 'Mesh';
3629 case 'monitor': return 'Monitor';
3630 default: return mode
;
3635 * Query the current operation mode from runtime information as
3636 * translated string.
3639 * Returns the translated, human readable mode name as reported by
3640 *`ubus` runtime state.
3642 getActiveModeI18n: function() {
3643 var mode
= this.getActiveMode();
3646 case 'Master': return _('Master');
3647 case 'Client': return _('Client');
3648 case 'Ad-Hoc': return _('Ad-Hoc');
3649 case 'Mash': return _('Mesh');
3650 case 'Monitor': return _('Monitor');
3651 default: return mode
;
3656 * Query the current SSID from runtime information.
3659 * Returns the current SSID or Mesh ID as reported by `ubus` runtime
3662 getActiveSSID: function() {
3663 return this.ubus('net', 'iwinfo', 'ssid') || this.ubus('net', 'config', 'ssid') || this.get('ssid');
3667 * Query the current BSSID from runtime information.
3670 * Returns the current BSSID or Mesh ID as reported by `ubus` runtime
3673 getActiveBSSID: function() {
3674 return this.ubus('net', 'iwinfo', 'bssid') || this.ubus('net', 'config', 'bssid') || this.get('bssid');
3678 * Query the current encryption settings from runtime information.
3681 * Returns a string describing the current encryption or `-` if the the
3682 * encryption state could not be found in `ubus` runtime information.
3684 getActiveEncryption: function() {
3685 return formatWifiEncryption(this.ubus('net', 'iwinfo', 'encryption')) || '-';
3689 * A wireless peer entry describes the properties of a remote wireless
3690 * peer associated with a local network.
3692 * @typedef {Object<string, boolean|number|string|LuCI.network.WifiRateEntry>} WifiPeerEntry
3693 * @memberof LuCI.network
3695 * @property {string} mac
3696 * The MAC address (BSSID).
3698 * @property {number} signal
3699 * The received signal strength.
3701 * @property {number} [signal_avg]
3702 * The average signal strength if supported by the driver.
3704 * @property {number} [noise]
3705 * The current noise floor of the radio. May be `0` or absent if not
3706 * supported by the driver.
3708 * @property {number} inactive
3709 * The amount of milliseconds the peer has been inactive, e.g. due
3712 * @property {number} connected_time
3713 * The amount of milliseconds the peer is associated to this network.
3715 * @property {number} [thr]
3716 * The estimated throughput of the peer, May be `0` or absent if not
3717 * supported by the driver.
3719 * @property {boolean} authorized
3720 * Specifies whether the peer is authorized to associate to this network.
3722 * @property {boolean} authenticated
3723 * Specifies whether the peer completed authentication to this network.
3725 * @property {string} preamble
3726 * The preamble mode used by the peer. May be `long` or `short`.
3728 * @property {boolean} wme
3729 * Specifies whether the peer supports WME/WMM capabilities.
3731 * @property {boolean} mfp
3732 * Specifies whether management frame protection is active.
3734 * @property {boolean} tdls
3735 * Specifies whether TDLS is active.
3737 * @property {number} [mesh llid]
3738 * The mesh LLID, may be `0` or absent if not applicable or supported
3741 * @property {number} [mesh plid]
3742 * The mesh PLID, may be `0` or absent if not applicable or supported
3745 * @property {string} [mesh plink]
3746 * The mesh peer link state description, may be an empty string (`''`)
3747 * or absent if not applicable or supported by the driver.
3749 * The following states are known:
3759 * @property {number} [mesh local PS]
3760 * The local powersafe mode for the peer link, may be an empty
3761 * string (`''`) or absent if not applicable or supported by
3764 * The following modes are known:
3765 * - `ACTIVE` (no power save)
3770 * @property {number} [mesh peer PS]
3771 * The remote powersafe mode for the peer link, may be an empty
3772 * string (`''`) or absent if not applicable or supported by
3775 * The following modes are known:
3776 * - `ACTIVE` (no power save)
3781 * @property {number} [mesh non-peer PS]
3782 * The powersafe mode for all non-peer neigbours, may be an empty
3783 * string (`''`) or absent if not applicable or supported by the driver.
3785 * The following modes are known:
3786 * - `ACTIVE` (no power save)
3791 * @property {LuCI.network.WifiRateEntry} rx
3792 * Describes the receiving wireless rate from the peer.
3794 * @property {LuCI.network.WifiRateEntry} tx
3795 * Describes the transmitting wireless rate to the peer.
3799 * A wireless rate entry describes the properties of a wireless
3800 * transmission rate to or from a peer.
3802 * @typedef {Object<string, boolean|number>} WifiRateEntry
3803 * @memberof LuCI.network
3805 * @property {number} [drop_misc]
3806 * The amount of received misc. packages that have been dropped, e.g.
3807 * due to corruption or missing authentication. Only applicable to
3810 * @property {number} packets
3811 * The amount of packets that have been received or sent.
3813 * @property {number} bytes
3814 * The amount of bytes that have been received or sent.
3816 * @property {number} [failed]
3817 * The amount of failed tranmission attempts. Only applicable to
3820 * @property {number} [retries]
3821 * The amount of retried transmissions. Only applicable to transmit
3824 * @property {boolean} is_ht
3825 * Specifies whether this rate is an HT (IEEE 802.11n) rate.
3827 * @property {boolean} is_vht
3828 * Specifies whether this rate is an VHT (IEEE 802.11ac) rate.
3830 * @property {number} mhz
3831 * The channel width in MHz used for the transmission.
3833 * @property {number} rate
3834 * The bitrate in bit/s of the transmission.
3836 * @property {number} [mcs]
3837 * The MCS index of the used transmission rate. Only applicable to
3840 * @property {number} [40mhz]
3841 * Specifies whether the tranmission rate used 40MHz wide channel.
3842 * Only applicable to HT or VHT rates.
3844 * Note: this option exists for backwards compatibility only and its
3845 * use is discouraged. The `mhz` field should be used instead to
3846 * determine the channel width.
3848 * @property {boolean} [short_gi]
3849 * Specifies whether a short guard interval is used for the transmission.
3850 * Only applicable to HT or VHT rates.
3852 * @property {number} [nss]
3853 * Specifies the number of spatial streams used by the transmission.
3854 * Only applicable to VHT rates.
3858 * Fetch the list of associated peers.
3860 * @returns {Promise<Array<LuCI.network.WifiPeerEntry>>}
3861 * Returns a promise resolving to an array of wireless peers associated
3862 * with this network.
3864 getAssocList: function() {
3865 return callIwinfoAssoclist(this.getIfname());
3869 * Query the current operating frequency of the wireless network.
3871 * @returns {null|string}
3872 * Returns the current operating frequency of the network from `ubus`
3873 * runtime information in GHz or `null` if the information is not
3876 getFrequency: function() {
3877 var freq
= this.ubus('net', 'iwinfo', 'frequency');
3879 if (freq
!= null && freq
> 0)
3880 return '%.03f'.format(freq
/ 1000);
3886 * Query the current average bitrate of all peers associated to this
3889 * @returns {null|number}
3890 * Returns the average bit rate among all peers associated to the network
3891 * as reported by `ubus` runtime information or `null` if the information
3894 getBitRate: function() {
3895 var rate
= this.ubus('net', 'iwinfo', 'bitrate');
3897 if (rate
!= null && rate
> 0)
3898 return (rate
/ 1000);
3904 * Query the current wireless channel.
3906 * @returns {null|number}
3907 * Returns the wireless channel as reported by `ubus` runtime information
3908 * or `null` if it cannot be determined.
3910 getChannel: function() {
3911 return this.ubus('net', 'iwinfo', 'channel') || this.ubus('dev', 'config', 'channel') || this.get('channel');
3915 * Query the current wireless signal.
3917 * @returns {null|number}
3918 * Returns the wireless signal in dBm as reported by `ubus` runtime
3919 * information or `null` if it cannot be determined.
3921 getSignal: function() {
3922 return this.ubus('net', 'iwinfo', 'signal') || 0;
3926 * Query the current radio noise floor.
3929 * Returns the radio noise floor in dBm as reported by `ubus` runtime
3930 * information or `0` if it cannot be determined.
3932 getNoise: function() {
3933 return this.ubus('net', 'iwinfo', 'noise') || 0;
3937 * Query the current country code.
3940 * Returns the wireless country code as reported by `ubus` runtime
3941 * information or `00` if it cannot be determined.
3943 getCountryCode: function() {
3944 return this.ubus('net', 'iwinfo', 'country') || this.ubus('dev', 'config', 'country') || '00';
3948 * Query the current radio TX power.
3950 * @returns {null|number}
3951 * Returns the wireless network transmit power in dBm as reported by
3952 * `ubus` runtime information or `null` if it cannot be determined.
3954 getTXPower: function() {
3955 return this.ubus('net', 'iwinfo', 'txpower');
3959 * Query the radio TX power offset.
3961 * Some wireless radios have a fixed power offset, e.g. due to the
3962 * use of external amplifiers.
3965 * Returns the wireless network transmit power offset in dBm as reported
3966 * by `ubus` runtime information or `0` if there is no offset, or if it
3967 * cannot be determined.
3969 getTXPowerOffset: function() {
3970 return this.ubus('net', 'iwinfo', 'txpower_offset') || 0;
3974 * Calculate the current signal.
3978 * Returns the calculated signal level, which is the difference between
3979 * noise and signal (SNR), divided by 5.
3981 getSignalLevel: function(signal
, noise
) {
3982 if (this.getActiveBSSID() == '00:00:00:00:00:00')
3985 signal
= signal
|| this.getSignal();
3986 noise
= noise
|| this.getNoise();
3988 if (signal
< 0 && noise
< 0) {
3989 var snr
= -1 * (noise
- signal
);
3990 return Math
.floor(snr
/ 5);
3997 * Calculate the current signal quality percentage.
4000 * Returns the calculated signal quality in percent. The value is
4001 * calculated from the `quality` and `quality_max` indicators reported
4002 * by `ubus` runtime state.
4004 getSignalPercent: function() {
4005 var qc
= this.ubus('net', 'iwinfo', 'quality') || 0,
4006 qm
= this.ubus('net', 'iwinfo', 'quality_max') || 0;
4008 if (qc
> 0 && qm
> 0)
4009 return Math
.floor((100 / qm
) * qc
);
4015 * Get a short description string for this wireless network.
4018 * Returns a string describing this network, consisting of the
4019 * active operation mode, followed by either the SSID, BSSID or
4020 * internal network ID, depending on which information is available.
4022 getShortName: function() {
4023 return '%s "%s"'.format(
4024 this.getActiveModeI18n(),
4025 this.getActiveSSID() || this.getActiveBSSID() || this.getID());
4029 * Get a description string for this wireless network.
4032 * Returns a string describing this network, consisting of the
4033 * term `Wireless Network`, followed by the active operation mode,
4034 * the SSID, BSSID or internal network ID and the Linux network device
4035 * name, depending on which information is available.
4037 getI18n: function() {
4038 return '%s: %s "%s" (%s)'.format(
4039 _('Wireless Network'),
4040 this.getActiveModeI18n(),
4041 this.getActiveSSID() || this.getActiveBSSID() || this.getID(),
4046 * Get the primary logical interface this wireless network is attached to.
4048 * @returns {null|LuCI.network.Protocol}
4049 * Returns a `Network.Protocol` instance representing the logical
4050 * interface or `null` if this network is not attached to any logical
4053 getNetwork: function() {
4054 return this.getNetworks()[0];
4058 * Get the logical interfaces this wireless network is attached to.
4060 * @returns {Array<LuCI.network.Protocol>}
4061 * Returns an array of `Network.Protocol` instances representing the
4062 * logical interfaces this wireless network is attached to.
4064 getNetworks: function() {
4065 var networkNames
= this.getNetworkNames(),
4068 for (var i
= 0; i
< networkNames
.length
; i
++) {
4069 var uciInterface
= uci
.get('network', networkNames
[i
]);
4071 if (uciInterface
== null || uciInterface
['.type'] != 'interface')
4074 networks
.push(Network
.prototype.instantiateNetwork(networkNames
[i
]));
4077 networks
.sort(networkSort
);
4083 * Get the associated Linux network device.
4085 * @returns {LuCI.network.Device}
4086 * Returns a `Network.Device` instance representing the Linux network
4087 * device associted with this wireless network.
4089 getDevice: function() {
4090 return Network
.prototype.instantiateDevice(this.getIfname());
4094 * Check whether this wifi network supports deauthenticating clients.
4096 * @returns {boolean}
4097 * Returns `true` when this wifi network instance supports forcibly
4098 * deauthenticating clients, otherwise `false`.
4100 isClientDisconnectSupported: function() {
4101 return L
.isObject(this.ubus('hostapd', 'del_client'));
4105 * Forcibly disconnect the given client from the wireless network.
4107 * @param {string} mac
4108 * The MAC address of the client to disconnect.
4110 * @param {boolean} [deauth=false]
4111 * Specifies whether to deauthenticate (`true`) or disassociate (`false`)
4114 * @param {number} [reason=1]
4115 * Specifies the IEEE 802.11 reason code to disassoc/deauth the client
4116 * with. Default is `1` which corresponds to `Unspecified reason`.
4118 * @param {number} [ban_time=0]
4119 * Specifies the amount of milliseconds to ban the client from
4120 * reconnecting. By default, no ban time is set which allows the client
4121 * to reassociate / reauthenticate immediately.
4123 * @returns {Promise<number>}
4124 * Returns a promise resolving to the underlying ubus call result code
4125 * which is typically `0`, even for not existing MAC addresses.
4126 * The promise might reject with an error in case invalid arguments
4129 disconnectClient: function(mac
, deauth
, reason
, ban_time
) {
4130 if (reason
== null || reason
== 0)
4136 return rpc
.declare({
4137 object
: 'hostapd.%s'.format(this.getIfname()),
4138 method
: 'del_client',
4139 params
: [ 'addr', 'deauth', 'reason', 'ban_time' ]
4140 })(mac
, deauth
, reason
, ban_time
);