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 strcmp(a
, b
) {
113 function getProtocolHandlers(cache
) {
114 return callNetworkProtoHandlers().then(function(protos
) {
115 /* Register "none" protocol */
116 if (!protos
.hasOwnProperty('none'))
117 Object
.assign(protos
, { none
: { no_device
: false } });
119 /* Hack: emulate relayd protocol */
120 if (!protos
.hasOwnProperty('relay') && L
.hasSystemFeature('relayd'))
121 Object
.assign(protos
, { relay
: { no_device
: true } });
123 Object
.assign(_protospecs
, protos
);
125 return Promise
.all(Object
.keys(protos
).map(function(p
) {
126 return Promise
.resolve(L
.require('protocol.%s'.format(p
))).catch(function(err
) {
127 if (L
.isObject(err
) && err
.name
!= 'NetworkError')
130 })).then(function() {
133 }).catch(function() {
138 function getWifiStateBySid(sid
) {
139 var s
= uci
.get('wireless', sid
);
141 if (s
!= null && s
['.type'] == 'wifi-iface') {
142 for (var radioname
in _state
.radios
) {
143 for (var i
= 0; i
< _state
.radios
[radioname
].interfaces
.length
; i
++) {
144 var netstate
= _state
.radios
[radioname
].interfaces
[i
];
146 if (typeof(netstate
.section
) != 'string')
149 var s2
= uci
.get('wireless', netstate
.section
);
151 if (s2
!= null && s
['.type'] == s2
['.type'] && s
['.name'] == s2
['.name']) {
152 if (s2
['.anonymous'] == false && netstate
.section
.charAt(0) == '@')
155 return [ radioname
, _state
.radios
[radioname
], netstate
];
164 function getWifiStateByIfname(ifname
) {
165 for (var radioname
in _state
.radios
) {
166 for (var i
= 0; i
< _state
.radios
[radioname
].interfaces
.length
; i
++) {
167 var netstate
= _state
.radios
[radioname
].interfaces
[i
];
169 if (typeof(netstate
.ifname
) != 'string')
172 if (netstate
.ifname
== ifname
)
173 return [ radioname
, _state
.radios
[radioname
], netstate
];
180 function isWifiIfname(ifname
) {
181 for (var i
= 0; i
< iface_patterns_wireless
.length
; i
++)
182 if (iface_patterns_wireless
[i
].test(ifname
))
188 function getWifiSidByNetid(netid
) {
189 var m
= /^(\w+)\.network(\d+)$/.exec(netid
);
191 var sections
= uci
.sections('wireless', 'wifi-iface');
192 for (var i
= 0, n
= 0; i
< sections
.length
; i
++) {
193 if (sections
[i
].device
!= m
[1])
197 return sections
[i
]['.name'];
204 function getWifiSidByIfname(ifname
) {
205 var sid
= getWifiSidByNetid(ifname
);
210 var res
= getWifiStateByIfname(ifname
);
212 if (res
!= null && L
.isObject(res
[2]) && typeof(res
[2].section
) == 'string')
213 return res
[2].section
;
218 function getWifiNetidBySid(sid
) {
219 var s
= uci
.get('wireless', sid
);
220 if (s
!= null && s
['.type'] == 'wifi-iface') {
221 var radioname
= s
.device
;
222 if (typeof(s
.device
) == 'string') {
223 var i
= 0, netid
= null, sections
= uci
.sections('wireless', 'wifi-iface');
224 for (var i
= 0, n
= 0; i
< sections
.length
; i
++) {
225 if (sections
[i
].device
!= s
.device
)
230 if (sections
[i
]['.name'] != s
['.name'])
233 return [ '%s.network%d'.format(s
.device
, n
), s
.device
];
242 function getWifiNetidByNetname(name
) {
243 var sections
= uci
.sections('wireless', 'wifi-iface');
244 for (var i
= 0; i
< sections
.length
; i
++) {
245 if (typeof(sections
[i
].network
) != 'string')
248 var nets
= sections
[i
].network
.split(/\s+/);
249 for (var j
= 0; j
< nets
.length
; j
++) {
253 return getWifiNetidBySid(sections
[i
]['.name']);
260 function isVirtualIfname(ifname
) {
261 for (var i
= 0; i
< iface_patterns_virtual
.length
; i
++)
262 if (iface_patterns_virtual
[i
].test(ifname
))
268 function isIgnoredIfname(ifname
) {
269 for (var i
= 0; i
< iface_patterns_ignore
.length
; i
++)
270 if (iface_patterns_ignore
[i
].test(ifname
))
276 function appendValue(config
, section
, option
, value
) {
277 var values
= uci
.get(config
, section
, option
),
278 isArray
= Array
.isArray(values
),
281 if (isArray
== false)
282 values
= L
.toArray(values
);
284 if (values
.indexOf(value
) == -1) {
289 uci
.set(config
, section
, option
, isArray
? values
: values
.join(' '));
294 function removeValue(config
, section
, option
, value
) {
295 var values
= uci
.get(config
, section
, option
),
296 isArray
= Array
.isArray(values
),
299 if (isArray
== false)
300 values
= L
.toArray(values
);
302 for (var i
= values
.length
- 1; i
>= 0; i
--) {
303 if (values
[i
] == value
) {
309 if (values
.length
> 0)
310 uci
.set(config
, section
, option
, isArray
? values
: values
.join(' '));
312 uci
.unset(config
, section
, option
);
317 function prefixToMask(bits
, v6
) {
318 var w
= v6
? 128 : 32,
324 for (var i
= 0; i
< w
/ 16; i
++) {
325 var b
= Math
.min(16, bits
);
326 m
.push((0xffff << (16 - b
)) & 0xffff);
331 return String
.prototype.format
.apply('%x:%x:%x:%x:%x:%x:%x:%x', m
).replace(/:0(?::0)+$/, '::');
333 return '%d.%d.%d.%d'.format(m
[0] >>> 8, m
[0] & 0xff, m
[1] >>> 8, m
[1] & 0xff);
336 function maskToPrefix(mask
, v6
) {
337 var m
= v6
? validation
.parseIPv6(mask
) : validation
.parseIPv4(mask
);
344 for (var i
= 0, z
= false; i
< m
.length
; i
++) {
347 while (!z
&& (m
[i
] & (v6
? 0x8000 : 0x80))) {
348 m
[i
] = (m
[i
] << 1) & (v6
? 0xffff : 0xff);
359 function initNetworkState(refresh
) {
360 if (_state
== null || refresh
) {
361 _init
= _init
|| Promise
.all([
362 L
.resolveDefault(callNetworkInterfaceDump(), []),
363 L
.resolveDefault(callLuciBoardJSON(), {}),
364 L
.resolveDefault(callLuciNetworkDevices(), {}),
365 L
.resolveDefault(callLuciWirelessDevices(), {}),
366 L
.resolveDefault(callLuciHostHints(), {}),
367 getProtocolHandlers(),
368 L
.resolveDefault(uci
.load('network')),
369 L
.resolveDefault(uci
.load('wireless')),
370 L
.resolveDefault(uci
.load('luci'))
371 ]).then(function(data
) {
372 var netifd_ifaces
= data
[0],
373 board_json
= data
[1],
377 isTunnel
: {}, isBridge
: {}, isSwitch
: {}, isWifi
: {},
378 ifaces
: netifd_ifaces
, radios
: data
[3], hosts
: data
[4],
379 netdevs
: {}, bridges
: {}, switches
: {}, hostapd
: {}
382 for (var name
in luci_devs
) {
383 var dev
= luci_devs
[name
];
385 if (isVirtualIfname(name
))
386 s
.isTunnel
[name
] = true;
388 if (!s
.isTunnel
[name
] && isIgnoredIfname(name
))
391 s
.netdevs
[name
] = s
.netdevs
[name
] || {
400 devtype
: dev
.devtype
,
403 wireless
: dev
.wireless
,
409 if (Array
.isArray(dev
.ipaddrs
))
410 for (var i
= 0; i
< dev
.ipaddrs
.length
; i
++)
411 s
.netdevs
[name
].ipaddrs
.push(dev
.ipaddrs
[i
].address
+ '/' + dev
.ipaddrs
[i
].netmask
);
413 if (Array
.isArray(dev
.ip6addrs
))
414 for (var i
= 0; i
< dev
.ip6addrs
.length
; i
++)
415 s
.netdevs
[name
].ip6addrs
.push(dev
.ip6addrs
[i
].address
+ '/' + dev
.ip6addrs
[i
].netmask
);
418 for (var name
in luci_devs
) {
419 var dev
= luci_devs
[name
];
431 for (var i
= 0; dev
.ports
&& i
< dev
.ports
.length
; i
++) {
432 var subdev
= s
.netdevs
[dev
.ports
[i
]];
437 b
.ifnames
.push(subdev
);
442 s
.isBridge
[name
] = true;
445 for (var name
in luci_devs
) {
446 var dev
= luci_devs
[name
];
448 if (!dev
.parent
|| dev
.devtype
!= 'dsa')
451 s
.isSwitch
[dev
.parent
] = true;
452 s
.isSwitch
[name
] = true;
455 if (L
.isObject(board_json
.switch)) {
456 for (var switchname
in board_json
.switch) {
457 var layout
= board_json
.switch[switchname
],
464 if (L
.isObject(layout
) && Array
.isArray(layout
.ports
)) {
465 for (var i
= 0, port
; (port
= layout
.ports
[i
]) != null; i
++) {
466 if (typeof(port
) == 'object' && typeof(port
.num
) == 'number' &&
467 (typeof(port
.role
) == 'string' || typeof(port
.device
) == 'string')) {
470 role
: port
.role
|| 'cpu',
471 index
: (port
.index
!= null) ? port
.index
: port
.num
474 if (port
.device
!= null) {
475 spec
.device
= port
.device
;
476 spec
.tagged
= spec
.need_tag
;
477 netdevs
[port
.num
] = port
.device
;
482 if (port
.role
!= null)
483 nports
[port
.role
] = (nports
[port
.role
] || 0) + 1;
487 ports
.sort(function(a
, b
) {
488 if (a
.role
!= b
.role
)
489 return (a
.role
< b
.role
) ? -1 : 1;
491 return (a
.index
- b
.index
);
494 for (var i
= 0, port
; (port
= ports
[i
]) != null; i
++) {
495 if (port
.role
!= role
) {
501 port
.label
= 'CPU (%s)'.format(port
.device
);
502 else if (nports
[role
] > 1)
503 port
.label
= '%s %d'.format(role
.toUpperCase(), pnum
++);
505 port
.label
= role
.toUpperCase();
511 s
.switches
[switchname
] = {
519 if (L
.isObject(board_json
.dsl
) && L
.isObject(board_json
.dsl
.modem
)) {
520 s
.hasDSLModem
= board_json
.dsl
.modem
;
527 if (L
.isObject(s
.radios
))
528 for (var radio
in s
.radios
)
529 if (L
.isObject(s
.radios
[radio
]) && Array
.isArray(s
.radios
[radio
].interfaces
))
530 for (var i
= 0; i
< s
.radios
[radio
].interfaces
.length
; i
++)
531 if (L
.isObject(s
.radios
[radio
].interfaces
[i
]) && s
.radios
[radio
].interfaces
[i
].ifname
)
532 objects
.push('hostapd.%s'.format(s
.radios
[radio
].interfaces
[i
].ifname
));
534 return (objects
.length
? L
.resolveDefault(rpc
.list
.apply(rpc
, objects
), {}) : Promise
.resolve({})).then(function(res
) {
536 var m
= k
.match(/^hostapd\.(.+)$/);
538 s
.hostapd
[m
[1]] = res
[k
];
546 return (_state
!= null ? Promise
.resolve(_state
) : _init
);
549 function ifnameOf(obj
) {
550 if (obj
instanceof Protocol
)
551 return obj
.getIfname();
552 else if (obj
instanceof Device
)
553 return obj
.getName();
554 else if (obj
instanceof WifiDevice
)
555 return obj
.getName();
556 else if (obj
instanceof WifiNetwork
)
557 return obj
.getIfname();
558 else if (typeof(obj
) == 'string')
559 return obj
.replace(/:.+$/, '');
564 function networkSort(a
, b
) {
565 return strcmp(a
.getName(), b
.getName());
568 function deviceSort(a
, b
) {
569 var typeWeigth
= { wifi
: 2, alias
: 3 },
570 weightA
= typeWeigth
[a
.getType()] || 1,
571 weightB
= typeWeigth
[b
.getType()] || 1;
573 if (weightA
!= weightB
)
574 return weightA
- weightB
;
576 return strcmp(a
.getName(), b
.getName());
579 function formatWifiEncryption(enc
) {
580 if (!L
.isObject(enc
))
586 var ciphers
= Array
.isArray(enc
.ciphers
)
587 ? enc
.ciphers
.map(function(c
) { return c
.toUpperCase() }) : [ 'NONE' ];
589 if (Array
.isArray(enc
.wep
)) {
590 var has_open
= false,
593 for (var i
= 0; i
< enc
.wep
.length
; i
++)
594 if (enc
.wep
[i
] == 'open')
596 else if (enc
.wep
[i
] == 'shared')
599 if (has_open
&& has_shared
)
600 return 'WEP Open/Shared (%s)'.format(ciphers
.join(', '));
602 return 'WEP Open System (%s)'.format(ciphers
.join(', '));
604 return 'WEP Shared Auth (%s)'.format(ciphers
.join(', '));
609 if (Array
.isArray(enc
.wpa
)) {
611 suites
= Array
.isArray(enc
.authentication
)
612 ? enc
.authentication
.map(function(a
) { return a
.toUpperCase() }) : [ 'NONE' ];
614 for (var i
= 0; i
< enc
.wpa
.length
; i
++)
615 switch (enc
.wpa
[i
]) {
617 versions
.push('WPA');
621 versions
.push('WPA%d'.format(enc
.wpa
[i
]));
625 if (versions
.length
> 1)
626 return 'mixed %s %s (%s)'.format(versions
.join('/'), suites
.join(', '), ciphers
.join(', '));
628 return '%s %s (%s)'.format(versions
[0], suites
.join(', '), ciphers
.join(', '));
634 function enumerateNetworks() {
635 var uciInterfaces
= uci
.sections('network', 'interface'),
638 for (var i
= 0; i
< uciInterfaces
.length
; i
++)
639 networks
[uciInterfaces
[i
]['.name']] = this.instantiateNetwork(uciInterfaces
[i
]['.name']);
641 for (var i
= 0; i
< _state
.ifaces
.length
; i
++)
642 if (networks
[_state
.ifaces
[i
].interface] == null)
643 networks
[_state
.ifaces
[i
].interface] =
644 this.instantiateNetwork(_state
.ifaces
[i
].interface, _state
.ifaces
[i
].proto
);
648 for (var network
in networks
)
649 if (networks
.hasOwnProperty(network
))
650 rv
.push(networks
[network
]);
652 rv
.sort(networkSort
);
658 var Hosts
, Network
, Protocol
, Device
, WifiDevice
, WifiNetwork
;
666 * The `LuCI.network` class combines data from multiple `ubus` apis to
667 * provide an abstraction of the current network configuration state.
669 * It provides methods to enumerate interfaces and devices, to query
670 * current configuration details and to manipulate settings.
672 Network
= baseclass
.extend(/** @lends LuCI.network.prototype */ {
674 * Converts the given prefix size in bits to a netmask.
678 * @param {number} bits
679 * The prefix size in bits.
681 * @param {boolean} [v6=false]
682 * Whether to convert the bits value into an IPv4 netmask (`false`) or
683 * an IPv6 netmask (`true`).
685 * @returns {null|string}
686 * Returns a string containing the netmask corresponding to the bit count
687 * or `null` when the given amount of bits exceeds the maximum possible
688 * value of `32` for IPv4 or `128` for IPv6.
690 prefixToMask
: prefixToMask
,
693 * Converts the given netmask to a prefix size in bits.
697 * @param {string} netmask
698 * The netmask to convert into a bit count.
700 * @param {boolean} [v6=false]
701 * Whether to parse the given netmask as IPv4 (`false`) or IPv6 (`true`)
704 * @returns {null|number}
705 * Returns the number of prefix bits contained in the netmask or `null`
706 * if the given netmask value was invalid.
708 maskToPrefix
: maskToPrefix
,
711 * An encryption entry describes active wireless encryption settings
712 * such as the used key management protocols, active ciphers and
715 * @typedef {Object<string, boolean|Array<number|string>>} LuCI.network.WifiEncryption
716 * @memberof LuCI.network
718 * @property {boolean} enabled
719 * Specifies whether any kind of encryption, such as `WEP` or `WPA` is
720 * enabled. If set to `false`, then no encryption is active and the
721 * corresponding network is open.
723 * @property {string[]} [wep]
724 * When the `wep` property exists, the network uses WEP encryption.
725 * In this case, the property is set to an array of active WEP modes
726 * which might be either `open`, `shared` or both.
728 * @property {number[]} [wpa]
729 * When the `wpa` property exists, the network uses WPA security.
730 * In this case, the property is set to an array containing the WPA
731 * protocol versions used, e.g. `[ 1, 2 ]` for WPA/WPA2 mixed mode or
732 * `[ 3 ]` for WPA3-SAE.
734 * @property {string[]} [authentication]
735 * The `authentication` property only applies to WPA encryption and
736 * is defined when the `wpa` property is set as well. It points to
737 * an array of active authentication suites used by the network, e.g.
738 * `[ "psk" ]` for a WPA(2)-PSK network or `[ "psk", "sae" ]` for
739 * mixed WPA2-PSK/WPA3-SAE encryption.
741 * @property {string[]} [ciphers]
742 * If either WEP or WPA encryption is active, then the `ciphers`
743 * property will be set to an array describing the active encryption
744 * ciphers used by the network, e.g. `[ "tkip", "ccmp" ]` for a
745 * WPA/WPA2-PSK mixed network or `[ "wep-40", "wep-104" ]` for an
750 * Converts a given {@link LuCI.network.WifiEncryption encryption entry}
751 * into a human readable string such as `mixed WPA/WPA2 PSK (TKIP, CCMP)`
752 * or `WPA3 SAE (CCMP)`.
756 * @param {LuCI.network.WifiEncryption} encryption
757 * The wireless encryption entry to convert.
759 * @returns {null|string}
760 * Returns the description string for the given encryption entry or
761 * `null` if the given entry was invalid.
763 formatWifiEncryption
: formatWifiEncryption
,
766 * Flushes the local network state cache and fetches updated information
767 * from the remote `ubus` apis.
769 * @returns {Promise<Object>}
770 * Returns a promise resolving to the internal network state object.
772 flushCache: function() {
773 initNetworkState(true);
778 * Instantiates the given {@link LuCI.network.Protocol Protocol} backend,
779 * optionally using the given network name.
781 * @param {string} protoname
782 * The protocol backend to use, e.g. `static` or `dhcp`.
784 * @param {string} [netname=__dummy__]
785 * The network name to use for the instantiated protocol. This should be
786 * usually set to one of the interfaces described in /etc/config/network
787 * but it is allowed to omit it, e.g. to query protocol capabilities
788 * without the need for an existing interface.
790 * @returns {null|LuCI.network.Protocol}
791 * Returns the instantiated protocol backend class or `null` if the given
792 * protocol isn't known.
794 getProtocol: function(protoname
, netname
) {
795 var v
= _protocols
[protoname
];
797 return new v(netname
|| '__dummy__');
803 * Obtains instances of all known {@link LuCI.network.Protocol Protocol}
806 * @returns {Array<LuCI.network.Protocol>}
807 * Returns an array of protocol class instances.
809 getProtocols: function() {
812 for (var protoname
in _protocols
)
813 rv
.push(new _protocols
[protoname
]('__dummy__'));
819 * Registers a new {@link LuCI.network.Protocol Protocol} subclass
820 * with the given methods and returns the resulting subclass value.
822 * This functions internally calls
823 * {@link LuCI.Class.extend Class.extend()} on the `Network.Protocol`
826 * @param {string} protoname
827 * The name of the new protocol to register.
829 * @param {Object<string, *>} methods
830 * The member methods and values of the new `Protocol` subclass to
831 * be passed to {@link LuCI.Class.extend Class.extend()}.
833 * @returns {LuCI.network.Protocol}
834 * Returns the new `Protocol` subclass.
836 registerProtocol: function(protoname
, methods
) {
837 var spec
= L
.isObject(_protospecs
) ? _protospecs
[protoname
] : null;
838 var proto
= Protocol
.extend(Object
.assign({
839 getI18n: function() {
843 isFloating: function() {
847 isVirtual: function() {
848 return (L
.isObject(spec
) && spec
.no_device
== true);
851 renderFormOptions: function(section
) {
855 __init__: function(name
) {
859 getProtocol: function() {
864 _protocols
[protoname
] = proto
;
870 * Registers a new regular expression pattern to recognize
871 * virtual interfaces.
873 * @param {RegExp} pat
874 * A `RegExp` instance to match a virtual interface name
875 * such as `6in4-wan` or `tun0`.
877 registerPatternVirtual: function(pat
) {
878 iface_patterns_virtual
.push(pat
);
882 * Registers a new human readable translation string for a `Protocol`
885 * @param {string} code
886 * The `ubus` protocol error code to register a translation for, e.g.
889 * @param {string} message
890 * The message to use as translation for the given protocol error code.
893 * Returns `true` if the error code description has been added or `false`
894 * if either the arguments were invalid or if there already was a
895 * description for the given code.
897 registerErrorCode: function(code
, message
) {
898 if (typeof(code
) == 'string' &&
899 typeof(message
) == 'string' &&
900 !proto_errors
.hasOwnProperty(code
)) {
901 proto_errors
[code
] = message
;
909 * Adds a new network of the given name and update it with the given
912 * If a network with the given name already exist but is empty, then
913 * this function will update its option, otherwise it will do nothing.
915 * @param {string} name
916 * The name of the network to add. Must be in the format `[a-zA-Z0-9_]+`.
918 * @param {Object<string, string|string[]>} [options]
919 * An object of uci option values to set on the new network or to
920 * update in an existing, empty network.
922 * @returns {Promise<null|LuCI.network.Protocol>}
923 * Returns a promise resolving to the `Protocol` subclass instance
924 * describing the added network or resolving to `null` if the name
925 * was invalid or if a non-empty network of the given name already
928 addNetwork: function(name
, options
) {
929 return this.getNetwork(name
).then(L
.bind(function(existingNetwork
) {
930 if (name
!= null && /^[a-zA-Z0-9_]+$/.test(name
) && existingNetwork
== null) {
931 var sid
= uci
.add('network', 'interface', name
);
934 if (L
.isObject(options
))
935 for (var key
in options
)
936 if (options
.hasOwnProperty(key
))
937 uci
.set('network', sid
, key
, options
[key
]);
939 return this.instantiateNetwork(sid
);
942 else if (existingNetwork
!= null && existingNetwork
.isEmpty()) {
943 if (L
.isObject(options
))
944 for (var key
in options
)
945 if (options
.hasOwnProperty(key
))
946 existingNetwork
.set(key
, options
[key
]);
948 return existingNetwork
;
954 * Get a {@link LuCI.network.Protocol Protocol} instance describing
955 * the network with the given name.
957 * @param {string} name
958 * The logical interface name of the network get, e.g. `lan` or `wan`.
960 * @returns {Promise<null|LuCI.network.Protocol>}
961 * Returns a promise resolving to a
962 * {@link LuCI.network.Protocol Protocol} subclass instance describing
963 * the network or `null` if the network did not exist.
965 getNetwork: function(name
) {
966 return initNetworkState().then(L
.bind(function() {
967 var section
= (name
!= null) ? uci
.get('network', name
) : null;
969 if (section
!= null && section
['.type'] == 'interface') {
970 return this.instantiateNetwork(name
);
972 else if (name
!= null) {
973 for (var i
= 0; i
< _state
.ifaces
.length
; i
++)
974 if (_state
.ifaces
[i
].interface == name
)
975 return this.instantiateNetwork(name
, _state
.ifaces
[i
].proto
);
983 * Gets an array containing all known networks.
985 * @returns {Promise<Array<LuCI.network.Protocol>>}
986 * Returns a promise resolving to a name-sorted array of
987 * {@link LuCI.network.Protocol Protocol} subclass instances
988 * describing all known networks.
990 getNetworks: function() {
991 return initNetworkState().then(L
.bind(enumerateNetworks
, this));
995 * Deletes the given network and its references from the network and
996 * firewall configuration.
998 * @param {string} name
999 * The name of the network to delete.
1001 * @returns {Promise<boolean>}
1002 * Returns a promise resolving to either `true` if the network and
1003 * references to it were successfully deleted from the configuration or
1004 * `false` if the given network could not be found.
1006 deleteNetwork: function(name
) {
1007 var requireFirewall
= Promise
.resolve(L
.require('firewall')).catch(function() {}),
1008 loadDHCP
= L
.resolveDefault(uci
.load('dhcp')),
1009 network
= this.instantiateNetwork(name
);
1011 return Promise
.all([ requireFirewall
, loadDHCP
, initNetworkState() ]).then(function(res
) {
1012 var uciInterface
= uci
.get('network', name
),
1015 if (uciInterface
!= null && uciInterface
['.type'] == 'interface') {
1016 return Promise
.resolve(network
? network
.deleteConfiguration() : null).then(function() {
1017 uci
.remove('network', name
);
1019 uci
.sections('luci', 'ifstate', function(s
) {
1020 if (s
.interface == name
)
1021 uci
.remove('luci', s
['.name']);
1024 uci
.sections('network', null, function(s
) {
1025 switch (s
['.type']) {
1029 if (s
.interface == name
)
1030 uci
.remove('network', s
['.name']);
1036 if (s
.in == name
|| s
.out
== name
)
1037 uci
.remove('network', s
['.name']);
1043 uci
.sections('wireless', 'wifi-iface', function(s
) {
1044 var networks
= L
.toArray(s
.network
).filter(function(network
) { return network
!= name
});
1046 if (networks
.length
> 0)
1047 uci
.set('wireless', s
['.name'], 'network', networks
.join(' '));
1049 uci
.unset('wireless', s
['.name'], 'network');
1052 uci
.sections('dhcp', 'dhcp', function(s
) {
1053 if (s
.interface == name
)
1054 uci
.remove('dhcp', s
['.name']);
1058 return firewall
.deleteNetwork(name
).then(function() { return true });
1061 }).catch(function() {
1071 * Rename the given network and its references to a new name.
1073 * @param {string} oldName
1074 * The current name of the network.
1076 * @param {string} newName
1077 * The name to rename the network to, must be in the format
1080 * @returns {Promise<boolean>}
1081 * Returns a promise resolving to either `true` if the network was
1082 * successfully renamed or `false` if the new name was invalid, if
1083 * a network with the new name already exists or if the network to
1084 * rename could not be found.
1086 renameNetwork: function(oldName
, newName
) {
1087 return initNetworkState().then(function() {
1088 if (newName
== null || !/^[a-zA-Z0-9_]+$/.test(newName
) || uci
.get('network', newName
) != null)
1091 var oldNetwork
= uci
.get('network', oldName
);
1093 if (oldNetwork
== null || oldNetwork
['.type'] != 'interface')
1096 var sid
= uci
.add('network', 'interface', newName
);
1098 for (var key
in oldNetwork
)
1099 if (oldNetwork
.hasOwnProperty(key
) && key
.charAt(0) != '.')
1100 uci
.set('network', sid
, key
, oldNetwork
[key
]);
1102 uci
.sections('luci', 'ifstate', function(s
) {
1103 if (s
.interface == oldName
)
1104 uci
.set('luci', s
['.name'], 'interface', newName
);
1107 uci
.sections('network', 'alias', function(s
) {
1108 if (s
.interface == oldName
)
1109 uci
.set('network', s
['.name'], 'interface', newName
);
1112 uci
.sections('network', 'route', function(s
) {
1113 if (s
.interface == oldName
)
1114 uci
.set('network', s
['.name'], 'interface', newName
);
1117 uci
.sections('network', 'route6', function(s
) {
1118 if (s
.interface == oldName
)
1119 uci
.set('network', s
['.name'], 'interface', newName
);
1122 uci
.sections('wireless', 'wifi-iface', function(s
) {
1123 var networks
= L
.toArray(s
.network
).map(function(network
) { return (network
== oldName
? newName
: network
) });
1125 if (networks
.length
> 0)
1126 uci
.set('wireless', s
['.name'], 'network', networks
.join(' '));
1129 uci
.remove('network', oldName
);
1136 * Get a {@link LuCI.network.Device Device} instance describing the
1137 * given network device.
1139 * @param {string} name
1140 * The name of the network device to get, e.g. `eth0` or `br-lan`.
1142 * @returns {Promise<null|LuCI.network.Device>}
1143 * Returns a promise resolving to the `Device` instance describing
1144 * the network device or `null` if the given device name could not
1147 getDevice: function(name
) {
1148 return initNetworkState().then(L
.bind(function() {
1152 if (_state
.netdevs
.hasOwnProperty(name
))
1153 return this.instantiateDevice(name
);
1155 var netid
= getWifiNetidBySid(name
);
1157 return this.instantiateDevice(netid
[0]);
1164 * Get a sorted list of all found network devices.
1166 * @returns {Promise<Array<LuCI.network.Device>>}
1167 * Returns a promise resolving to a sorted array of `Device` class
1168 * instances describing the network devices found on the system.
1170 getDevices: function() {
1171 return initNetworkState().then(L
.bind(function() {
1174 /* find simple devices */
1175 var uciInterfaces
= uci
.sections('network', 'interface');
1176 for (var i
= 0; i
< uciInterfaces
.length
; i
++) {
1177 var ifnames
= L
.toArray(uciInterfaces
[i
].ifname
);
1179 for (var j
= 0; j
< ifnames
.length
; j
++) {
1180 if (ifnames
[j
].charAt(0) == '@')
1183 if (isIgnoredIfname(ifnames
[j
]) || isVirtualIfname(ifnames
[j
]) || isWifiIfname(ifnames
[j
]))
1186 devices
[ifnames
[j
]] = this.instantiateDevice(ifnames
[j
]);
1190 for (var ifname
in _state
.netdevs
) {
1191 if (devices
.hasOwnProperty(ifname
))
1194 if (isIgnoredIfname(ifname
) || isWifiIfname(ifname
))
1197 if (_state
.netdevs
[ifname
].wireless
)
1200 devices
[ifname
] = this.instantiateDevice(ifname
);
1203 /* find VLAN devices */
1204 var uciSwitchVLANs
= uci
.sections('network', 'switch_vlan');
1205 for (var i
= 0; i
< uciSwitchVLANs
.length
; i
++) {
1206 if (typeof(uciSwitchVLANs
[i
].ports
) != 'string' ||
1207 typeof(uciSwitchVLANs
[i
].device
) != 'string' ||
1208 !_state
.switches
.hasOwnProperty(uciSwitchVLANs
[i
].device
))
1211 var ports
= uciSwitchVLANs
[i
].ports
.split(/\s+/);
1212 for (var j
= 0; j
< ports
.length
; j
++) {
1213 var m
= ports
[j
].match(/^(\d+)([tu]?)$/);
1217 var netdev
= _state
.switches
[uciSwitchVLANs
[i
].device
].netdevs
[m
[1]];
1221 if (!devices
.hasOwnProperty(netdev
))
1222 devices
[netdev
] = this.instantiateDevice(netdev
);
1224 _state
.isSwitch
[netdev
] = true;
1229 var vid
= uciSwitchVLANs
[i
].vid
|| uciSwitchVLANs
[i
].vlan
;
1230 vid
= (vid
!= null ? +vid
: null);
1232 if (vid
== null || vid
< 0 || vid
> 4095)
1235 var vlandev
= '%s.%d'.format(netdev
, vid
);
1237 if (!devices
.hasOwnProperty(vlandev
))
1238 devices
[vlandev
] = this.instantiateDevice(vlandev
);
1240 _state
.isSwitch
[vlandev
] = true;
1244 /* find bridge VLAN devices */
1245 var uciBridgeVLANs
= uci
.sections('network', 'bridge-vlan');
1246 for (var i
= 0; i
< uciBridgeVLANs
.length
; i
++) {
1247 var basedev
= uciBridgeVLANs
[i
].device
,
1248 local
= uciBridgeVLANs
[i
].local
,
1249 alias
= uciBridgeVLANs
[i
].alias
,
1250 vid
= +uciBridgeVLANs
[i
].vlan
,
1251 ports
= L
.toArray(uciBridgeVLANs
[i
].ports
);
1256 if (isNaN(vid
) || vid
< 0 || vid
> 4095)
1259 var vlandev
= '%s.%s'.format(basedev
, alias
|| vid
);
1261 _state
.isBridge
[basedev
] = true;
1263 if (!_state
.bridges
.hasOwnProperty(basedev
))
1264 _state
.bridges
[basedev
] = {
1269 if (!devices
.hasOwnProperty(vlandev
))
1270 devices
[vlandev
] = this.instantiateDevice(vlandev
);
1272 ports
.forEach(function(port_name
) {
1273 var m
= port_name
.match(/^([^:]+)(?::[ut*]+)?$/),
1274 p
= m
? m
[1] : null;
1279 if (_state
.bridges
[basedev
].ifnames
.filter(function(sd
) { return sd
.name
== p
}).length
)
1282 _state
.netdevs
[p
] = _state
.netdevs
[p
] || {
1287 devtype
: 'ethernet',
1292 _state
.bridges
[basedev
].ifnames
.push(_state
.netdevs
[p
]);
1293 _state
.netdevs
[p
].bridge
= _state
.bridges
[basedev
];
1297 /* find wireless interfaces */
1298 var uciWifiIfaces
= uci
.sections('wireless', 'wifi-iface'),
1301 for (var i
= 0; i
< uciWifiIfaces
.length
; i
++) {
1302 if (typeof(uciWifiIfaces
[i
].device
) != 'string')
1305 networkCount
[uciWifiIfaces
[i
].device
] = (networkCount
[uciWifiIfaces
[i
].device
] || 0) + 1;
1307 var netid
= '%s.network%d'.format(uciWifiIfaces
[i
].device
, networkCount
[uciWifiIfaces
[i
].device
]);
1309 devices
[netid
] = this.instantiateDevice(netid
);
1312 /* find uci declared devices */
1313 var uciDevices
= uci
.sections('network', 'device');
1315 for (var i
= 0; i
< uciDevices
.length
; i
++) {
1316 var type
= uciDevices
[i
].type
,
1317 name
= uciDevices
[i
].name
;
1319 if (!type
|| !name
|| devices
.hasOwnProperty(name
))
1322 if (type
== 'bridge')
1323 _state
.isBridge
[name
] = true;
1325 devices
[name
] = this.instantiateDevice(name
);
1330 for (var netdev
in devices
)
1331 if (devices
.hasOwnProperty(netdev
))
1332 rv
.push(devices
[netdev
]);
1334 rv
.sort(deviceSort
);
1341 * Test if a given network device name is in the list of patterns for
1342 * device names to ignore.
1344 * Ignored device names are usually Linux network devices which are
1345 * spawned implicitly by kernel modules such as `tunl0` or `hwsim0`
1346 * and which are unsuitable for use in network configuration.
1348 * @param {string} name
1349 * The device name to test.
1351 * @returns {boolean}
1352 * Returns `true` if the given name is in the ignore pattern list,
1353 * else returns `false`.
1355 isIgnoredDevice: function(name
) {
1356 return isIgnoredIfname(name
);
1360 * Get a {@link LuCI.network.WifiDevice WifiDevice} instance describing
1361 * the given wireless radio.
1363 * @param {string} devname
1364 * The configuration name of the wireless radio to lookup, e.g. `radio0`
1365 * for the first mac80211 phy on the system.
1367 * @returns {Promise<null|LuCI.network.WifiDevice>}
1368 * Returns a promise resolving to the `WifiDevice` instance describing
1369 * the underlying radio device or `null` if the wireless radio could not
1372 getWifiDevice: function(devname
) {
1373 return initNetworkState().then(L
.bind(function() {
1374 var existingDevice
= uci
.get('wireless', devname
);
1376 if (existingDevice
== null || existingDevice
['.type'] != 'wifi-device')
1379 return this.instantiateWifiDevice(devname
, _state
.radios
[devname
] || {});
1384 * Obtain a list of all configured radio devices.
1386 * @returns {Promise<Array<LuCI.network.WifiDevice>>}
1387 * Returns a promise resolving to an array of `WifiDevice` instances
1388 * describing the wireless radios configured in the system.
1389 * The order of the array corresponds to the order of the radios in
1390 * the configuration.
1392 getWifiDevices: function() {
1393 return initNetworkState().then(L
.bind(function() {
1394 var uciWifiDevices
= uci
.sections('wireless', 'wifi-device'),
1397 for (var i
= 0; i
< uciWifiDevices
.length
; i
++) {
1398 var devname
= uciWifiDevices
[i
]['.name'];
1399 rv
.push(this.instantiateWifiDevice(devname
, _state
.radios
[devname
] || {}));
1407 * Get a {@link LuCI.network.WifiNetwork WifiNetwork} instance describing
1408 * the given wireless network.
1410 * @param {string} netname
1411 * The name of the wireless network to lookup. This may be either an uci
1412 * configuration section ID, a network ID in the form `radio#.network#`
1413 * or a Linux network device name like `wlan0` which is resolved to the
1414 * corresponding configuration section through `ubus` runtime information.
1416 * @returns {Promise<null|LuCI.network.WifiNetwork>}
1417 * Returns a promise resolving to the `WifiNetwork` instance describing
1418 * the wireless network or `null` if the corresponding network could not
1421 getWifiNetwork: function(netname
) {
1422 return initNetworkState()
1423 .then(L
.bind(this.lookupWifiNetwork
, this, netname
));
1427 * Get an array of all {@link LuCI.network.WifiNetwork WifiNetwork}
1428 * instances describing the wireless networks present on the system.
1430 * @returns {Promise<Array<LuCI.network.WifiNetwork>>}
1431 * Returns a promise resolving to an array of `WifiNetwork` instances
1432 * describing the wireless networks. The array will be empty if no networks
1435 getWifiNetworks: function() {
1436 return initNetworkState().then(L
.bind(function() {
1437 var wifiIfaces
= uci
.sections('wireless', 'wifi-iface'),
1440 for (var i
= 0; i
< wifiIfaces
.length
; i
++)
1441 rv
.push(this.lookupWifiNetwork(wifiIfaces
[i
]['.name']));
1443 rv
.sort(function(a
, b
) {
1444 return strcmp(a
.getID(), b
.getID());
1452 * Adds a new wireless network to the configuration and sets its options
1453 * to the provided values.
1455 * @param {Object<string, string|string[]>} options
1456 * The options to set for the newly added wireless network. This object
1457 * must at least contain a `device` property which is set to the radio
1458 * name the new network belongs to.
1460 * @returns {Promise<null|LuCI.network.WifiNetwork>}
1461 * Returns a promise resolving to a `WifiNetwork` instance describing
1462 * the newly added wireless network or `null` if the given options
1463 * were invalid or if the associated radio device could not be found.
1465 addWifiNetwork: function(options
) {
1466 return initNetworkState().then(L
.bind(function() {
1467 if (options
== null ||
1468 typeof(options
) != 'object' ||
1469 typeof(options
.device
) != 'string')
1472 var existingDevice
= uci
.get('wireless', options
.device
);
1473 if (existingDevice
== null || existingDevice
['.type'] != 'wifi-device')
1476 /* XXX: need to add a named section (wifinet#) here */
1477 var sid
= uci
.add('wireless', 'wifi-iface');
1478 for (var key
in options
)
1479 if (options
.hasOwnProperty(key
))
1480 uci
.set('wireless', sid
, key
, options
[key
]);
1482 var radioname
= existingDevice
['.name'],
1483 netid
= getWifiNetidBySid(sid
) || [];
1485 return this.instantiateWifiNetwork(sid
, radioname
, _state
.radios
[radioname
], netid
[0], null);
1490 * Deletes the given wireless network from the configuration.
1492 * @param {string} netname
1493 * The name of the network to remove. This may be either a
1494 * network ID in the form `radio#.network#` or a Linux network device
1495 * name like `wlan0` which is resolved to the corresponding configuration
1496 * section through `ubus` runtime information.
1498 * @returns {Promise<boolean>}
1499 * Returns a promise resolving to `true` if the wireless network has been
1500 * successfully deleted from the configuration or `false` if it could not
1503 deleteWifiNetwork: function(netname
) {
1504 return initNetworkState().then(L
.bind(function() {
1505 var sid
= getWifiSidByIfname(netname
);
1510 uci
.remove('wireless', sid
);
1516 getStatusByRoute: function(addr
, mask
) {
1517 return initNetworkState().then(L
.bind(function() {
1520 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
1521 if (!Array
.isArray(_state
.ifaces
[i
].route
))
1524 for (var j
= 0; j
< _state
.ifaces
[i
].route
.length
; j
++) {
1525 if (typeof(_state
.ifaces
[i
].route
[j
]) != 'object' ||
1526 typeof(_state
.ifaces
[i
].route
[j
].target
) != 'string' ||
1527 typeof(_state
.ifaces
[i
].route
[j
].mask
) != 'number')
1530 if (_state
.ifaces
[i
].route
[j
].table
)
1533 if (_state
.ifaces
[i
].route
[j
].target
!= addr
||
1534 _state
.ifaces
[i
].route
[j
].mask
!= mask
)
1537 rv
.push(_state
.ifaces
[i
]);
1541 rv
.sort(function(a
, b
) {
1542 if (a
.metric
!= b
.metric
)
1543 return (a
.metric
- b
.metric
);
1545 return strcmp(a
.interface, b
.interface);
1553 getStatusByAddress: function(addr
) {
1554 return initNetworkState().then(L
.bind(function() {
1557 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
1558 if (Array
.isArray(_state
.ifaces
[i
]['ipv4-address']))
1559 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv4-address'].length
; j
++)
1560 if (typeof(_state
.ifaces
[i
]['ipv4-address'][j
]) == 'object' &&
1561 _state
.ifaces
[i
]['ipv4-address'][j
].address
== addr
)
1562 return _state
.ifaces
[i
];
1564 if (Array
.isArray(_state
.ifaces
[i
]['ipv6-address']))
1565 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv6-address'].length
; j
++)
1566 if (typeof(_state
.ifaces
[i
]['ipv6-address'][j
]) == 'object' &&
1567 _state
.ifaces
[i
]['ipv6-address'][j
].address
== addr
)
1568 return _state
.ifaces
[i
];
1570 if (Array
.isArray(_state
.ifaces
[i
]['ipv6-prefix-assignment']))
1571 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv6-prefix-assignment'].length
; j
++)
1572 if (typeof(_state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]) == 'object' &&
1573 typeof(_state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]['local-address']) == 'object' &&
1574 _state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]['local-address'].address
== addr
)
1575 return _state
.ifaces
[i
];
1583 * Get IPv4 wan networks.
1585 * This function looks up all networks having a default `0.0.0.0/0` route
1586 * and returns them as array.
1588 * @returns {Promise<Array<LuCI.network.Protocol>>}
1589 * Returns a promise resolving to an array of `Protocol` subclass
1590 * instances describing the found default route interfaces.
1592 getWANNetworks: function() {
1593 return this.getStatusByRoute('0.0.0.0', 0).then(L
.bind(function(statuses
) {
1594 var rv
= [], seen
= {};
1596 for (var i
= 0; i
< statuses
.length
; i
++) {
1597 if (!seen
.hasOwnProperty(statuses
[i
].interface)) {
1598 rv
.push(this.instantiateNetwork(statuses
[i
].interface, statuses
[i
].proto
));
1599 seen
[statuses
[i
].interface] = true;
1608 * Get IPv6 wan networks.
1610 * This function looks up all networks having a default `::/0` route
1611 * and returns them as array.
1613 * @returns {Promise<Array<LuCI.network.Protocol>>}
1614 * Returns a promise resolving to an array of `Protocol` subclass
1615 * instances describing the found IPv6 default route interfaces.
1617 getWAN6Networks: function() {
1618 return this.getStatusByRoute('::', 0).then(L
.bind(function(statuses
) {
1619 var rv
= [], seen
= {};
1621 for (var i
= 0; i
< statuses
.length
; i
++) {
1622 if (!seen
.hasOwnProperty(statuses
[i
].interface)) {
1623 rv
.push(this.instantiateNetwork(statuses
[i
].interface, statuses
[i
].proto
));
1624 seen
[statuses
[i
].interface] = true;
1633 * Describes an swconfig switch topology by specifying the CPU
1634 * connections and external port labels of a switch.
1636 * @typedef {Object<string, Object|Array>} SwitchTopology
1637 * @memberof LuCI.network
1639 * @property {Object<number, string>} netdevs
1640 * The `netdevs` property points to an object describing the CPU port
1641 * connections of the switch. The numeric key of the enclosed object is
1642 * the port number, the value contains the Linux network device name the
1643 * port is hardwired to.
1645 * @property {Array<Object<string, boolean|number|string>>} ports
1646 * The `ports` property points to an array describing the populated
1647 * ports of the switch in the external label order. Each array item is
1648 * an object containg the following keys:
1649 * - `num` - the internal switch port number
1650 * - `label` - the label of the port, e.g. `LAN 1` or `CPU (eth0)`
1651 * - `device` - the connected Linux network device name (CPU ports only)
1652 * - `tagged` - a boolean indicating whether the port must be tagged to
1653 * function (CPU ports only)
1657 * Returns the topologies of all swconfig switches found on the system.
1659 * @returns {Promise<Object<string, LuCI.network.SwitchTopology>>}
1660 * Returns a promise resolving to an object containing the topologies
1661 * of each switch. The object keys correspond to the name of the switches
1662 * such as `switch0`, the values are
1663 * {@link LuCI.network.SwitchTopology SwitchTopology} objects describing
1666 getSwitchTopologies: function() {
1667 return initNetworkState().then(function() {
1668 return _state
.switches
;
1673 instantiateNetwork: function(name
, proto
) {
1677 proto
= (proto
== null ? uci
.get('network', name
, 'proto') : proto
);
1679 var protoClass
= _protocols
[proto
] || Protocol
;
1680 return new protoClass(name
);
1684 instantiateDevice: function(name
, network
, extend
) {
1686 return new (Device
.extend(extend
))(name
, network
);
1688 return new Device(name
, network
);
1692 instantiateWifiDevice: function(radioname
, radiostate
) {
1693 return new WifiDevice(radioname
, radiostate
);
1697 instantiateWifiNetwork: function(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
) {
1698 return new WifiNetwork(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
);
1702 lookupWifiNetwork: function(netname
) {
1703 var sid
, res
, netid
, radioname
, radiostate
, netstate
;
1705 sid
= getWifiSidByNetid(netname
);
1708 res
= getWifiStateBySid(sid
);
1710 radioname
= res
? res
[0] : null;
1711 radiostate
= res
? res
[1] : null;
1712 netstate
= res
? res
[2] : null;
1715 res
= getWifiStateByIfname(netname
);
1719 radiostate
= res
[1];
1721 sid
= netstate
.section
;
1722 netid
= L
.toArray(getWifiNetidBySid(sid
))[0];
1725 res
= getWifiStateBySid(netname
);
1729 radiostate
= res
[1];
1732 netid
= L
.toArray(getWifiNetidBySid(sid
))[0];
1735 res
= getWifiNetidBySid(netname
);
1746 return this.instantiateWifiNetwork(sid
|| netname
, radioname
,
1747 radiostate
, netid
, netstate
,
1748 netstate
? _state
.hostapd
[netstate
.ifname
] : null);
1752 * Obtains the the network device name of the given object.
1754 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} obj
1755 * The object to get the device name from.
1757 * @returns {null|string}
1758 * Returns a string containing the device name or `null` if the given
1759 * object could not be converted to a name.
1761 getIfnameOf: function(obj
) {
1762 return ifnameOf(obj
);
1766 * Queries the internal DSL modem type from board information.
1768 * @returns {Promise<null|string>}
1769 * Returns a promise resolving to the type of the internal modem
1770 * (e.g. `vdsl`) or to `null` if no internal modem is present.
1772 getDSLModemType: function() {
1773 return initNetworkState().then(function() {
1774 return _state
.hasDSLModem
? _state
.hasDSLModem
.type
: null;
1779 * Queries aggregated information about known hosts.
1781 * This function aggregates information from various sources such as
1782 * DHCP lease databases, ARP and IPv6 neighbour entries, wireless
1783 * association list etc. and returns a {@link LuCI.network.Hosts Hosts}
1784 * class instance describing the found hosts.
1786 * @returns {Promise<LuCI.network.Hosts>}
1787 * Returns a `Hosts` instance describing host known on the system.
1789 getHostHints: function() {
1790 return initNetworkState().then(function() {
1791 return new Hosts(_state
.hosts
);
1798 * @memberof LuCI.network
1802 * The `LuCI.network.Hosts` class encapsulates host information aggregated
1803 * from multiple sources and provides convenience functions to access the
1804 * host information by different criteria.
1806 Hosts
= baseclass
.extend(/** @lends LuCI.network.Hosts.prototype */ {
1807 __init__: function(hosts
) {
1812 * Lookup the hostname associated with the given MAC address.
1814 * @param {string} mac
1815 * The MAC address to lookup.
1817 * @returns {null|string}
1818 * Returns the hostname associated with the given MAC or `null` if
1819 * no matching host could be found or if no hostname is known for
1820 * the corresponding host.
1822 getHostnameByMACAddr: function(mac
) {
1823 return this.hosts
[mac
]
1824 ? (this.hosts
[mac
].name
|| null)
1829 * Lookup the IPv4 address associated with the given MAC address.
1831 * @param {string} mac
1832 * The MAC address to lookup.
1834 * @returns {null|string}
1835 * Returns the IPv4 address associated with the given MAC or `null` if
1836 * no matching host could be found or if no IPv4 address is known for
1837 * the corresponding host.
1839 getIPAddrByMACAddr: function(mac
) {
1840 return this.hosts
[mac
]
1841 ? (L
.toArray(this.hosts
[mac
].ipaddrs
|| this.hosts
[mac
].ipv4
)[0] || null)
1846 * Lookup the IPv6 address associated with the given MAC address.
1848 * @param {string} mac
1849 * The MAC address to lookup.
1851 * @returns {null|string}
1852 * Returns the IPv6 address associated with the given MAC or `null` if
1853 * no matching host could be found or if no IPv6 address is known for
1854 * the corresponding host.
1856 getIP6AddrByMACAddr: function(mac
) {
1857 return this.hosts
[mac
]
1858 ? (L
.toArray(this.hosts
[mac
].ip6addrs
|| this.hosts
[mac
].ipv6
)[0] || null)
1863 * Lookup the hostname associated with the given IPv4 address.
1865 * @param {string} ipaddr
1866 * The IPv4 address to lookup.
1868 * @returns {null|string}
1869 * Returns the hostname associated with the given IPv4 or `null` if
1870 * no matching host could be found or if no hostname is known for
1871 * the corresponding host.
1873 getHostnameByIPAddr: function(ipaddr
) {
1874 for (var mac
in this.hosts
) {
1875 if (this.hosts
[mac
].name
== null)
1878 var addrs
= L
.toArray(this.hosts
[mac
].ipaddrs
|| this.hosts
[mac
].ipv4
);
1880 for (var i
= 0; i
< addrs
.length
; i
++)
1881 if (addrs
[i
] == ipaddr
)
1882 return this.hosts
[mac
].name
;
1889 * Lookup the MAC address associated with the given IPv4 address.
1891 * @param {string} ipaddr
1892 * The IPv4 address to lookup.
1894 * @returns {null|string}
1895 * Returns the MAC address associated with the given IPv4 or `null` if
1896 * no matching host could be found or if no MAC address is known for
1897 * the corresponding host.
1899 getMACAddrByIPAddr: function(ipaddr
) {
1900 for (var mac
in this.hosts
) {
1901 var addrs
= L
.toArray(this.hosts
[mac
].ipaddrs
|| this.hosts
[mac
].ipv4
);
1903 for (var i
= 0; i
< addrs
.length
; i
++)
1904 if (addrs
[i
] == ipaddr
)
1912 * Lookup the hostname associated with the given IPv6 address.
1914 * @param {string} ip6addr
1915 * The IPv6 address to lookup.
1917 * @returns {null|string}
1918 * Returns the hostname associated with the given IPv6 or `null` if
1919 * no matching host could be found or if no hostname is known for
1920 * the corresponding host.
1922 getHostnameByIP6Addr: function(ip6addr
) {
1923 for (var mac
in this.hosts
) {
1924 if (this.hosts
[mac
].name
== null)
1927 var addrs
= L
.toArray(this.hosts
[mac
].ip6addrs
|| this.hosts
[mac
].ipv6
);
1929 for (var i
= 0; i
< addrs
.length
; i
++)
1930 if (addrs
[i
] == ip6addr
)
1931 return this.hosts
[mac
].name
;
1938 * Lookup the MAC address associated with the given IPv6 address.
1940 * @param {string} ip6addr
1941 * The IPv6 address to lookup.
1943 * @returns {null|string}
1944 * Returns the MAC address associated with the given IPv6 or `null` if
1945 * no matching host could be found or if no MAC address is known for
1946 * the corresponding host.
1948 getMACAddrByIP6Addr: function(ip6addr
) {
1949 for (var mac
in this.hosts
) {
1950 var addrs
= L
.toArray(this.hosts
[mac
].ip6addrs
|| this.hosts
[mac
].ipv6
);
1952 for (var i
= 0; i
< addrs
.length
; i
++)
1953 if (addrs
[i
] == ip6addr
)
1961 * Return an array of (MAC address, name hint) tuples sorted by
1964 * @param {boolean} [preferIp6=false]
1965 * Whether to prefer IPv6 addresses (`true`) or IPv4 addresses (`false`)
1966 * as name hint when no hostname is known for a specific MAC address.
1968 * @returns {Array<Array<string>>}
1969 * Returns an array of arrays containing a name hint for each found
1970 * MAC address on the system. The array is sorted ascending by MAC.
1972 * Each item of the resulting array is a two element array with the
1973 * MAC being the first element and the name hint being the second
1974 * element. The name hint is either the hostname, an IPv4 or an IPv6
1975 * address related to the MAC address.
1977 * If no hostname but both IPv4 and IPv6 addresses are known, the
1978 * `preferIP6` flag specifies whether the IPv6 or the IPv4 address
1981 getMACHints: function(preferIp6
) {
1984 for (var mac
in this.hosts
) {
1985 var hint
= this.hosts
[mac
].name
||
1986 L
.toArray(this.hosts
[mac
][preferIp6
? 'ip6addrs' : 'ipaddrs'] || this.hosts
[mac
][preferIp6
? 'ipv6' : 'ipv4'])[0] ||
1987 L
.toArray(this.hosts
[mac
][preferIp6
? 'ipaddrs' : 'ip6addrs'] || this.hosts
[mac
][preferIp6
? 'ipv4' : 'ipv6'])[0];
1989 rv
.push([mac
, hint
]);
1992 return rv
.sort(function(a
, b
) {
1993 return strcmp(a
[0], b
[0]);
2000 * @memberof LuCI.network
2004 * The `Network.Protocol` class serves as base for protocol specific
2005 * subclasses which describe logical UCI networks defined by `config
2006 * interface` sections in `/etc/config/network`.
2008 Protocol
= baseclass
.extend(/** @lends LuCI.network.Protocol.prototype */ {
2009 __init__: function(name
) {
2013 _get: function(opt
) {
2014 var val
= uci
.get('network', this.sid
, opt
);
2016 if (Array
.isArray(val
))
2017 return val
.join(' ');
2022 _ubus: function(field
) {
2023 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
2024 if (_state
.ifaces
[i
].interface != this.sid
)
2027 return (field
!= null ? _state
.ifaces
[i
][field
] : _state
.ifaces
[i
]);
2032 * Read the given UCI option value of this network.
2034 * @param {string} opt
2035 * The UCI option name to read.
2037 * @returns {null|string|string[]}
2038 * Returns the UCI option value or `null` if the requested option is
2041 get: function(opt
) {
2042 return uci
.get('network', this.sid
, opt
);
2046 * Set the given UCI option of this network to the given value.
2048 * @param {string} opt
2049 * The name of the UCI option to set.
2051 * @param {null|string|string[]} val
2052 * The value to set or `null` to remove the given option from the
2055 set: function(opt
, val
) {
2056 return uci
.set('network', this.sid
, opt
, val
);
2060 * Get the associared Linux network device of this network.
2062 * @returns {null|string}
2063 * Returns the name of the associated network device or `null` if
2064 * it could not be determined.
2066 getIfname: function() {
2069 if (this.isFloating())
2070 ifname
= this._ubus('l3_device');
2072 ifname
= this._ubus('device') || this._ubus('l3_device');
2077 var res
= getWifiNetidByNetname(this.sid
);
2078 return (res
!= null ? res
[0] : null);
2082 * Get the name of this network protocol class.
2084 * This function will be overwritten by subclasses created by
2085 * {@link LuCI.network#registerProtocol Network.registerProtocol()}.
2089 * Returns the name of the network protocol implementation, e.g.
2090 * `static` or `dhcp`.
2092 getProtocol: function() {
2097 * Return a human readable description for the protcol, such as
2098 * `Static address` or `DHCP client`.
2100 * This function should be overwritten by subclasses.
2104 * Returns the description string.
2106 getI18n: function() {
2107 switch (this.getProtocol()) {
2108 case 'none': return _('Unmanaged');
2109 case 'static': return _('Static address');
2110 case 'dhcp': return _('DHCP client');
2111 default: return _('Unknown');
2116 * Get the type of the underlying interface.
2118 * This function actually is a convenience wrapper around
2119 * `proto.get("type")` and is mainly used by other `LuCI.network` code
2120 * to check whether the interface is declared as bridge in UCI.
2122 * @returns {null|string}
2123 * Returns the value of the `type` option of the associated logical
2124 * interface or `null` if no `type` option is set.
2126 getType: function() {
2127 return this._get('type');
2131 * Get the name of the associated logical interface.
2134 * Returns the logical interface name, such as `lan` or `wan`.
2136 getName: function() {
2141 * Get the uptime of the logical interface.
2144 * Returns the uptime of the associated interface in seconds.
2146 getUptime: function() {
2147 return this._ubus('uptime') || 0;
2151 * Get the logical interface expiry time in seconds.
2153 * For protocols that have a concept of a lease, such as DHCP or
2154 * DHCPv6, this function returns the remaining time in seconds
2155 * until the lease expires.
2158 * Returns the amount of seconds until the lease expires or `-1`
2159 * if it isn't applicable to the associated protocol.
2161 getExpiry: function() {
2162 var u
= this._ubus('uptime'),
2163 d
= this._ubus('data');
2165 if (typeof(u
) == 'number' && d
!= null &&
2166 typeof(d
) == 'object' && typeof(d
.leasetime
) == 'number') {
2167 var r
= d
.leasetime
- (u
% d
.leasetime
);
2168 return (r
> 0 ? r
: 0);
2175 * Get the metric value of the logical interface.
2178 * Returns the current metric value used for device and network
2179 * routes spawned by the associated logical interface.
2181 getMetric: function() {
2182 return this._ubus('metric') || 0;
2186 * Get the requested firewall zone name of the logical interface.
2188 * Some protocol implementations request a specific firewall zone
2189 * to trigger inclusion of their resulting network devices into the
2190 * firewall rule set.
2192 * @returns {null|string}
2193 * Returns the requested firewall zone name as published in the
2194 * `ubus` runtime information or `null` if the remote protocol
2195 * handler didn't request a zone.
2197 getZoneName: function() {
2198 var d
= this._ubus('data');
2200 if (L
.isObject(d
) && typeof(d
.zone
) == 'string')
2207 * Query the first (primary) IPv4 address of the logical interface.
2209 * @returns {null|string}
2210 * Returns the primary IPv4 address registered by the protocol handler
2211 * or `null` if no IPv4 addresses were set.
2213 getIPAddr: function() {
2214 var addrs
= this._ubus('ipv4-address');
2215 return ((Array
.isArray(addrs
) && addrs
.length
) ? addrs
[0].address
: null);
2219 * Query all IPv4 addresses of the logical interface.
2221 * @returns {string[]}
2222 * Returns an array of IPv4 addresses in CIDR notation which have been
2223 * registered by the protocol handler. The order of the resulting array
2224 * follows the order of the addresses in `ubus` runtime information.
2226 getIPAddrs: function() {
2227 var addrs
= this._ubus('ipv4-address'),
2230 if (Array
.isArray(addrs
))
2231 for (var i
= 0; i
< addrs
.length
; i
++)
2232 rv
.push('%s/%d'.format(addrs
[i
].address
, addrs
[i
].mask
));
2238 * Query the first (primary) IPv4 netmask of the logical interface.
2240 * @returns {null|string}
2241 * Returns the netmask of the primary IPv4 address registered by the
2242 * protocol handler or `null` if no IPv4 addresses were set.
2244 getNetmask: function() {
2245 var addrs
= this._ubus('ipv4-address');
2246 if (Array
.isArray(addrs
) && addrs
.length
)
2247 return prefixToMask(addrs
[0].mask
, false);
2251 * Query the gateway (nexthop) of the default route associated with
2252 * this logical interface.
2255 * Returns a string containing the IPv4 nexthop address of the associated
2256 * default route or `null` if no default route was found.
2258 getGatewayAddr: function() {
2259 var routes
= this._ubus('route');
2261 if (Array
.isArray(routes
))
2262 for (var i
= 0; i
< routes
.length
; i
++)
2263 if (typeof(routes
[i
]) == 'object' &&
2264 routes
[i
].target
== '0.0.0.0' &&
2265 routes
[i
].mask
== 0)
2266 return routes
[i
].nexthop
;
2272 * Query the IPv4 DNS servers associated with the logical interface.
2274 * @returns {string[]}
2275 * Returns an array of IPv4 DNS servers registered by the remote
2278 getDNSAddrs: function() {
2279 var addrs
= this._ubus('dns-server'),
2282 if (Array
.isArray(addrs
))
2283 for (var i
= 0; i
< addrs
.length
; i
++)
2284 if (!/:/.test(addrs
[i
]))
2291 * Query the first (primary) IPv6 address of the logical interface.
2293 * @returns {null|string}
2294 * Returns the primary IPv6 address registered by the protocol handler
2295 * in CIDR notation or `null` if no IPv6 addresses were set.
2297 getIP6Addr: function() {
2298 var addrs
= this._ubus('ipv6-address');
2300 if (Array
.isArray(addrs
) && L
.isObject(addrs
[0]))
2301 return '%s/%d'.format(addrs
[0].address
, addrs
[0].mask
);
2303 addrs
= this._ubus('ipv6-prefix-assignment');
2305 if (Array
.isArray(addrs
) && L
.isObject(addrs
[0]) && L
.isObject(addrs
[0]['local-address']))
2306 return '%s/%d'.format(addrs
[0]['local-address'].address
, addrs
[0]['local-address'].mask
);
2312 * Query all IPv6 addresses of the logical interface.
2314 * @returns {string[]}
2315 * Returns an array of IPv6 addresses in CIDR notation which have been
2316 * registered by the protocol handler. The order of the resulting array
2317 * follows the order of the addresses in `ubus` runtime information.
2319 getIP6Addrs: function() {
2320 var addrs
= this._ubus('ipv6-address'),
2323 if (Array
.isArray(addrs
))
2324 for (var i
= 0; i
< addrs
.length
; i
++)
2325 if (L
.isObject(addrs
[i
]))
2326 rv
.push('%s/%d'.format(addrs
[i
].address
, addrs
[i
].mask
));
2328 addrs
= this._ubus('ipv6-prefix-assignment');
2330 if (Array
.isArray(addrs
))
2331 for (var i
= 0; i
< addrs
.length
; i
++)
2332 if (L
.isObject(addrs
[i
]) && L
.isObject(addrs
[i
]['local-address']))
2333 rv
.push('%s/%d'.format(addrs
[i
]['local-address'].address
, addrs
[i
]['local-address'].mask
));
2339 * Query the gateway (nexthop) of the IPv6 default route associated with
2340 * this logical interface.
2343 * Returns a string containing the IPv6 nexthop address of the associated
2344 * default route or `null` if no default route was found.
2346 getGateway6Addr: function() {
2347 var routes
= this._ubus('route');
2349 if (Array
.isArray(routes
))
2350 for (var i
= 0; i
< routes
.length
; i
++)
2351 if (typeof(routes
[i
]) == 'object' &&
2352 routes
[i
].target
== '::' &&
2353 routes
[i
].mask
== 0)
2354 return routes
[i
].nexthop
;
2360 * Query the IPv6 DNS servers associated with the logical interface.
2362 * @returns {string[]}
2363 * Returns an array of IPv6 DNS servers registered by the remote
2366 getDNS6Addrs: function() {
2367 var addrs
= this._ubus('dns-server'),
2370 if (Array
.isArray(addrs
))
2371 for (var i
= 0; i
< addrs
.length
; i
++)
2372 if (/:/.test(addrs
[i
]))
2379 * Query the routed IPv6 prefix associated with the logical interface.
2381 * @returns {null|string}
2382 * Returns the routed IPv6 prefix registered by the remote protocol
2383 * handler or `null` if no prefix is present.
2385 getIP6Prefix: function() {
2386 var prefixes
= this._ubus('ipv6-prefix');
2388 if (Array
.isArray(prefixes
) && L
.isObject(prefixes
[0]))
2389 return '%s/%d'.format(prefixes
[0].address
, prefixes
[0].mask
);
2395 * Query interface error messages published in `ubus` runtime state.
2397 * Interface errors are emitted by remote protocol handlers if the setup
2398 * of the underlying logical interface failed, e.g. due to bad
2399 * configuration or network connectivity issues.
2401 * This function will translate the found error codes to human readable
2402 * messages using the descriptions registered by
2403 * {@link LuCI.network#registerErrorCode Network.registerErrorCode()}
2404 * and fall back to `"Unknown error (%s)"` where `%s` is replaced by the
2405 * error code in case no translation can be found.
2407 * @returns {string[]}
2408 * Returns an array of translated interface error messages.
2410 getErrors: function() {
2411 var errors
= this._ubus('errors'),
2414 if (Array
.isArray(errors
)) {
2415 for (var i
= 0; i
< errors
.length
; i
++) {
2416 if (!L
.isObject(errors
[i
]) || typeof(errors
[i
].code
) != 'string')
2420 rv
.push(proto_errors
[errors
[i
].code
] || _('Unknown error (%s)').format(errors
[i
].code
));
2428 * Checks whether the underlying logical interface is declared as bridge.
2430 * @returns {boolean}
2431 * Returns `true` when the interface is declared with `option type bridge`
2432 * and when the associated protocol implementation is not marked virtual
2433 * or `false` when the logical interface is no bridge.
2435 isBridge: function() {
2436 return (!this.isVirtual() && this.getType() == 'bridge');
2440 * Get the name of the opkg package providing the protocol functionality.
2442 * This function should be overwritten by protocol specific subclasses.
2447 * Returns the name of the opkg package required for the protocol to
2448 * function, e.g. `odhcp6c` for the `dhcpv6` prototocol.
2450 getOpkgPackage: function() {
2455 * Check function for the protocol handler if a new interface is createable.
2457 * This function should be overwritten by protocol specific subclasses.
2461 * @param {string} ifname
2462 * The name of the interface to be created.
2464 * @returns {Promise<void>}
2465 * Returns a promise resolving if new interface is createable, else
2466 * rejects with an error message string.
2468 isCreateable: function(ifname
) {
2469 return Promise
.resolve(null);
2473 * Checks whether the protocol functionality is installed.
2475 * This function exists for compatibility with old code, it always
2481 * @returns {boolean}
2482 * Returns `true` if the protocol support is installed, else `false`.
2484 isInstalled: function() {
2489 * Checks whether this protocol is "virtual".
2491 * A "virtual" protocol is a protocol which spawns its own interfaces
2492 * on demand instead of using existing physical interfaces.
2494 * Examples for virtual protocols are `6in4` which `gre` spawn tunnel
2495 * network device on startup, examples for non-virtual protcols are
2496 * `dhcp` or `static` which apply IP configuration to existing interfaces.
2498 * This function should be overwritten by subclasses.
2500 * @returns {boolean}
2501 * Returns a boolean indicating whether the underlying protocol spawns
2502 * dynamic interfaces (`true`) or not (`false`).
2504 isVirtual: function() {
2509 * Checks whether this protocol is "floating".
2511 * A "floating" protocol is a protocol which spawns its own interfaces
2512 * on demand, like a virtual one but which relies on an existinf lower
2513 * level interface to initiate the connection.
2515 * An example for such a protocol is "pppoe".
2517 * This function exists for backwards compatibility with older code
2518 * but should not be used anymore.
2521 * @returns {boolean}
2522 * Returns a boolean indicating whether this protocol is floating (`true`)
2525 isFloating: function() {
2530 * Checks whether this logical interface is dynamic.
2532 * A dynamic interface is an interface which has been created at runtime,
2533 * e.g. as sub-interface of another interface, but which is not backed by
2534 * any user configuration. Such dynamic interfaces cannot be edited but
2535 * only brought down or restarted.
2537 * @returns {boolean}
2538 * Returns a boolean indicating whether this interface is dynamic (`true`)
2541 isDynamic: function() {
2542 return (this._ubus('dynamic') == true);
2546 * Checks whether this interface is an alias interface.
2548 * Alias interfaces are interfaces layering on top of another interface
2549 * and are denoted by a special `@interfacename` notation in the
2550 * underlying `device` option.
2552 * @returns {null|string}
2553 * Returns the name of the parent interface if this logical interface
2554 * is an alias or `null` if it is not an alias interface.
2556 isAlias: function() {
2557 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'device')),
2560 for (var i
= 0; i
< ifnames
.length
; i
++)
2561 if (ifnames
[i
].charAt(0) == '@')
2562 parent
= ifnames
[i
].substr(1);
2563 else if (parent
!= null)
2570 * Checks whether this logical interface is "empty", meaning that ut
2571 * has no network devices attached.
2573 * @returns {boolean}
2574 * Returns `true` if this logical interface is empty, else `false`.
2576 isEmpty: function() {
2577 if (this.isFloating())
2581 device
= this._get('device');
2583 if (device
!= null && device
.match(/\S+/))
2586 if (empty
== true && getWifiNetidBySid(this.sid
) != null)
2593 * Checks whether this logical interface is configured and running.
2595 * @returns {boolean}
2596 * Returns `true` when the interface is active or `false` when it is not.
2599 return (this._ubus('up') == true);
2603 * Add the given network device to the logical interface.
2605 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2606 * The object or device name to add to the logical interface. In case the
2607 * given argument is not a string, it is resolved though the
2608 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2610 * @returns {boolean}
2611 * Returns `true` if the device name has been added or `false` if any
2612 * argument was invalid, if the device was already part of the logical
2613 * interface or if the logical interface is virtual.
2615 addDevice: function(device
) {
2616 device
= ifnameOf(device
);
2618 if (device
== null || this.isFloating())
2621 var wif
= getWifiSidByIfname(device
);
2624 return appendValue('wireless', wif
, 'network', this.sid
);
2626 return appendValue('network', this.sid
, 'device', device
);
2630 * Remove the given network device from the logical interface.
2632 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2633 * The object or device name to remove from the logical interface. In case
2634 * the given argument is not a string, it is resolved though the
2635 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2637 * @returns {boolean}
2638 * Returns `true` if the device name has been added or `false` if any
2639 * argument was invalid, if the device was already part of the logical
2640 * interface or if the logical interface is virtual.
2642 deleteDevice: function(device
) {
2645 device
= ifnameOf(device
);
2647 if (device
== null || this.isFloating())
2650 var wif
= getWifiSidByIfname(device
);
2653 rv
= removeValue('wireless', wif
, 'network', this.sid
);
2655 if (removeValue('network', this.sid
, 'device', device
))
2662 * Returns the Linux network device associated with this logical
2665 * @returns {LuCI.network.Device}
2666 * Returns a `Network.Device` class instance representing the
2667 * expected Linux network device according to the configuration.
2669 getDevice: function() {
2670 if (this.isVirtual()) {
2671 var ifname
= '%s-%s'.format(this.getProtocol(), this.sid
);
2672 _state
.isTunnel
[this.getProtocol() + '-' + this.sid
] = true;
2673 return Network
.prototype.instantiateDevice(ifname
, this);
2675 else if (this.isBridge()) {
2676 var ifname
= 'br-%s'.format(this.sid
);
2677 _state
.isBridge
[ifname
] = true;
2678 return new Device(ifname
, this);
2681 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'device'));
2683 for (var i
= 0; i
< ifnames
.length
; i
++) {
2684 var m
= ifnames
[i
].match(/^([^:/]+)/);
2685 return ((m
&& m
[1]) ? Network
.prototype.instantiateDevice(m
[1], this) : null);
2688 ifname
= getWifiNetidByNetname(this.sid
);
2690 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
[0], this) : null);
2695 * Returns the layer 2 linux network device currently associated
2696 * with this logical interface.
2698 * @returns {LuCI.network.Device}
2699 * Returns a `Network.Device` class instance representing the Linux
2700 * network device currently associated with the logical interface.
2702 getL2Device: function() {
2703 var ifname
= this._ubus('device');
2704 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
, this) : null);
2708 * Returns the layer 3 linux network device currently associated
2709 * with this logical interface.
2711 * @returns {LuCI.network.Device}
2712 * Returns a `Network.Device` class instance representing the Linux
2713 * network device currently associated with the logical interface.
2715 getL3Device: function() {
2716 var ifname
= this._ubus('l3_device');
2717 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
, this) : null);
2721 * Returns a list of network sub-devices associated with this logical
2724 * @returns {null|Array<LuCI.network.Device>}
2725 * Returns an array of of `Network.Device` class instances representing
2726 * the sub-devices attached to this logical interface or `null` if the
2727 * logical interface does not support sub-devices, e.g. because it is
2728 * virtual and not a bridge.
2730 getDevices: function() {
2733 if (!this.isBridge() && !(this.isVirtual() && !this.isFloating()))
2736 var device
= uci
.get('network', this.sid
, 'device');
2738 if (device
&& device
.charAt(0) != '@') {
2739 var m
= device
.match(/^([^:/]+)/);
2741 rv
.push(Network
.prototype.instantiateDevice(m
[1], this));
2744 var uciWifiIfaces
= uci
.sections('wireless', 'wifi-iface');
2746 for (var i
= 0; i
< uciWifiIfaces
.length
; i
++) {
2747 if (typeof(uciWifiIfaces
[i
].device
) != 'string')
2750 var networks
= L
.toArray(uciWifiIfaces
[i
].network
);
2752 for (var j
= 0; j
< networks
.length
; j
++) {
2753 if (networks
[j
] != this.sid
)
2756 var netid
= getWifiNetidBySid(uciWifiIfaces
[i
]['.name']);
2759 rv
.push(Network
.prototype.instantiateDevice(netid
[0], this));
2763 rv
.sort(deviceSort
);
2769 * Checks whether this logical interface contains the given device
2772 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2773 * The object or device name to check. In case the given argument is not
2774 * a string, it is resolved though the
2775 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2777 * @returns {boolean}
2778 * Returns `true` when this logical interface contains the given network
2779 * device or `false` if not.
2781 containsDevice: function(device
) {
2782 device
= ifnameOf(device
);
2786 else if (this.isVirtual() && '%s-%s'.format(this.getProtocol(), this.sid
) == device
)
2788 else if (this.isBridge() && 'br-%s'.format(this.sid
) == device
)
2791 var name
= uci
.get('network', this.sid
, 'device');
2793 var m
= name
.match(/^([^:/]+)/);
2794 if (m
!= null && m
[1] == device
)
2798 var wif
= getWifiSidByIfname(device
);
2801 var networks
= L
.toArray(uci
.get('wireless', wif
, 'network'));
2803 for (var i
= 0; i
< networks
.length
; i
++)
2804 if (networks
[i
] == this.sid
)
2812 * Cleanup related configuration entries.
2814 * This function will be invoked if an interface is about to be removed
2815 * from the configuration and is responsible for performing any required
2816 * cleanup tasks, such as unsetting uci entries in related configurations.
2818 * It should be overwritten by protocol specific subclasses.
2822 * @returns {*|Promise<*>}
2823 * This function may return a promise which is awaited before the rest of
2824 * the configuration is removed. Any non-promise return value and any
2825 * resolved promise value is ignored. If the returned promise is rejected,
2826 * the interface removal will be aborted.
2828 deleteConfiguration: function() {}
2833 * @memberof LuCI.network
2837 * A `Network.Device` class instance represents an underlying Linux network
2838 * device and allows querying device details such as packet statistics or MTU.
2840 Device
= baseclass
.extend(/** @lends LuCI.network.Device.prototype */ {
2841 __init__: function(device
, network
) {
2842 var wif
= getWifiSidByIfname(device
);
2845 var res
= getWifiStateBySid(wif
) || [],
2846 netid
= getWifiNetidBySid(wif
) || [];
2848 this.wif
= new WifiNetwork(wif
, res
[0], res
[1], netid
[0], res
[2], { ifname
: device
});
2849 this.device
= this.wif
.getIfname();
2852 this.device
= this.device
|| device
;
2853 this.dev
= Object
.assign({}, _state
.netdevs
[this.device
]);
2854 this.network
= network
;
2857 _devstate: function(/* ... */) {
2860 for (var i
= 0; i
< arguments
.length
; i
++)
2862 rv
= rv
[arguments
[i
]];
2870 * Get the name of the network device.
2873 * Returns the name of the device, e.g. `eth0` or `wlan0`.
2875 getName: function() {
2876 return (this.wif
!= null ? this.wif
.getIfname() : this.device
);
2880 * Get the MAC address of the device.
2882 * @returns {null|string}
2883 * Returns the MAC address of the device or `null` if not applicable,
2884 * e.g. for non-ethernet tunnel devices.
2886 getMAC: function() {
2887 var mac
= this._devstate('macaddr');
2888 return mac
? mac
.toUpperCase() : null;
2892 * Get the MTU of the device.
2895 * Returns the MTU of the device.
2897 getMTU: function() {
2898 return this._devstate('mtu');
2902 * Get the IPv4 addresses configured on the device.
2904 * @returns {string[]}
2905 * Returns an array of IPv4 address strings.
2907 getIPAddrs: function() {
2908 var addrs
= this._devstate('ipaddrs');
2909 return (Array
.isArray(addrs
) ? addrs
: []);
2913 * Get the IPv6 addresses configured on the device.
2915 * @returns {string[]}
2916 * Returns an array of IPv6 address strings.
2918 getIP6Addrs: function() {
2919 var addrs
= this._devstate('ip6addrs');
2920 return (Array
.isArray(addrs
) ? addrs
: []);
2924 * Get the type of the device.
2927 * Returns a string describing the type of the network device:
2928 * - `alias` if it is an abstract alias device (`@` notation)
2929 * - `wifi` if it is a wireless interface (e.g. `wlan0`)
2930 * - `bridge` if it is a bridge device (e.g. `br-lan`)
2931 * - `tunnel` if it is a tun or tap device (e.g. `tun0`)
2932 * - `vlan` if it is a vlan device (e.g. `eth0.1`)
2933 * - `switch` if it is a switch device (e.g.`eth1` connected to switch0)
2934 * - `ethernet` for all other device types
2936 getType: function() {
2937 if (this.device
!= null && this.device
.charAt(0) == '@')
2939 else if (this.dev
.devtype
== 'wlan' || this.wif
!= null || isWifiIfname(this.device
))
2941 else if (this.dev
.devtype
== 'bridge' || _state
.isBridge
[this.device
])
2943 else if (_state
.isTunnel
[this.device
])
2945 else if (this.dev
.devtype
== 'vlan' || this.device
.indexOf('.') > -1)
2947 else if (this.dev
.devtype
== 'dsa' || _state
.isSwitch
[this.device
])
2954 * Get a short description string for the device.
2957 * Returns the device name for non-wifi devices or a string containing
2958 * the operation mode and SSID for wifi devices.
2960 getShortName: function() {
2961 if (this.wif
!= null)
2962 return this.wif
.getShortName();
2968 * Get a long description string for the device.
2971 * Returns a string containing the type description and device name
2972 * for non-wifi devices or operation mode and ssid for wifi ones.
2974 getI18n: function() {
2975 if (this.wif
!= null) {
2976 return '%s: %s "%s"'.format(
2977 _('Wireless Network'),
2978 this.wif
.getActiveMode(),
2979 this.wif
.getActiveSSID() || this.wif
.getActiveBSSID() || this.wif
.getID() || '?');
2982 return '%s: "%s"'.format(this.getTypeI18n(), this.getName());
2986 * Get a string describing the device type.
2989 * Returns a string describing the type, e.g. "Wireless Adapter" or
2992 getTypeI18n: function() {
2993 switch (this.getType()) {
2995 return _('Alias Interface');
2998 return _('Wireless Adapter');
3004 return (_state
.netdevs
[this.device
] && _state
.netdevs
[this.device
].devtype
== 'dsa')
3005 ? _('Switch port') : _('Ethernet Switch');
3008 return (_state
.isSwitch
[this.device
] ? _('Switch VLAN') : _('Software VLAN'));
3011 return _('Tunnel Interface');
3014 return _('Ethernet Adapter');
3019 * Get the associated bridge ports of the device.
3021 * @returns {null|Array<LuCI.network.Device>}
3022 * Returns an array of `Network.Device` instances representing the ports
3023 * (slave interfaces) of the bridge or `null` when this device isn't
3026 getPorts: function() {
3027 var br
= _state
.bridges
[this.device
],
3030 if (br
== null || !Array
.isArray(br
.ifnames
))
3033 for (var i
= 0; i
< br
.ifnames
.length
; i
++)
3034 rv
.push(Network
.prototype.instantiateDevice(br
.ifnames
[i
].name
));
3036 rv
.sort(deviceSort
);
3044 * @returns {null|string}
3045 * Returns the ID of this network bridge or `null` if this network
3046 * device is not a Linux bridge.
3048 getBridgeID: function() {
3049 var br
= _state
.bridges
[this.device
];
3050 return (br
!= null ? br
.id
: null);
3054 * Get the bridge STP setting
3056 * @returns {boolean}
3057 * Returns `true` when this device is a Linux bridge and has `stp`
3058 * enabled, else `false`.
3060 getBridgeSTP: function() {
3061 var br
= _state
.bridges
[this.device
];
3062 return (br
!= null ? !!br
.stp
: false);
3066 * Checks whether this device is up.
3068 * @returns {boolean}
3069 * Returns `true` when the associated device is running pr `false`
3070 * when it is down or absent.
3073 var up
= this._devstate('flags', 'up');
3076 up
= (this.getType() == 'alias');
3082 * Checks whether this device is a Linux bridge.
3084 * @returns {boolean}
3085 * Returns `true` when the network device is present and a Linux bridge,
3088 isBridge: function() {
3089 return (this.getType() == 'bridge');
3093 * Checks whether this device is part of a Linux bridge.
3095 * @returns {boolean}
3096 * Returns `true` when this network device is part of a bridge,
3099 isBridgePort: function() {
3100 return (this._devstate('bridge') != null);
3104 * Get the amount of transmitted bytes.
3107 * Returns the amount of bytes transmitted by the network device.
3109 getTXBytes: function() {
3110 var stat
= this._devstate('stats');
3111 return (stat
!= null ? stat
.tx_bytes
|| 0 : 0);
3115 * Get the amount of received bytes.
3118 * Returns the amount of bytes received by the network device.
3120 getRXBytes: function() {
3121 var stat
= this._devstate('stats');
3122 return (stat
!= null ? stat
.rx_bytes
|| 0 : 0);
3126 * Get the amount of transmitted packets.
3129 * Returns the amount of packets transmitted by the network device.
3131 getTXPackets: function() {
3132 var stat
= this._devstate('stats');
3133 return (stat
!= null ? stat
.tx_packets
|| 0 : 0);
3137 * Get the amount of received packets.
3140 * Returns the amount of packets received by the network device.
3142 getRXPackets: function() {
3143 var stat
= this._devstate('stats');
3144 return (stat
!= null ? stat
.rx_packets
|| 0 : 0);
3148 * Get the carrier state of the network device.
3150 * @returns {boolean}
3151 * Returns true if the device has a carrier, e.g. when a cable is
3152 * inserted into an ethernet port of false if there is none.
3154 getCarrier: function() {
3155 var link
= this._devstate('link');
3156 return (link
!= null ? link
.carrier
|| false : false);
3160 * Get the current link speed of the network device if available.
3162 * @returns {number|null}
3163 * Returns the current speed of the network device in Mbps. If the
3164 * device supports no ethernet speed levels, null is returned.
3165 * If the device supports ethernet speeds but has no carrier, -1 is
3168 getSpeed: function() {
3169 var link
= this._devstate('link');
3170 return (link
!= null ? link
.speed
|| null : null);
3174 * Get the current duplex mode of the network device if available.
3176 * @returns {string|null}
3177 * Returns the current duplex mode of the network device. Returns
3178 * either "full" or "half" if the device supports duplex modes or
3179 * null if the duplex mode is unknown or unsupported.
3181 getDuplex: function() {
3182 var link
= this._devstate('link'),
3183 duplex
= link
? link
.duplex
: null;
3185 return (duplex
!= 'unknown') ? duplex
: null;
3189 * Get the primary logical interface this device is assigned to.
3191 * @returns {null|LuCI.network.Protocol}
3192 * Returns a `Network.Protocol` instance representing the logical
3193 * interface this device is attached to or `null` if it is not
3194 * assigned to any logical interface.
3196 getNetwork: function() {
3197 return this.getNetworks()[0];
3201 * Get the logical interfaces this device is assigned to.
3203 * @returns {Array<LuCI.network.Protocol>}
3204 * Returns an array of `Network.Protocol` instances representing the
3205 * logical interfaces this device is assigned to.
3207 getNetworks: function() {
3208 if (this.networks
== null) {
3211 var networks
= enumerateNetworks
.apply(L
.network
);
3213 for (var i
= 0; i
< networks
.length
; i
++)
3214 if (networks
[i
].containsDevice(this.device
) || networks
[i
].getIfname() == this.device
)
3215 this.networks
.push(networks
[i
]);
3217 this.networks
.sort(networkSort
);
3220 return this.networks
;
3224 * Get the related wireless network this device is related to.
3226 * @returns {null|LuCI.network.WifiNetwork}
3227 * Returns a `Network.WifiNetwork` instance representing the wireless
3228 * network corresponding to this network device or `null` if this device
3229 * is not a wireless device.
3231 getWifiNetwork: function() {
3232 return (this.wif
!= null ? this.wif
: null);
3236 * Get the logical parent device of this device.
3238 * In case of DSA switch ports, the parent device will be the DSA switch
3239 * device itself, for VLAN devices, the parent refers to the base device
3242 * @returns {null|LuCI.network.Device}
3243 * Returns a `Network.Device` instance representing the parent device or
3244 * `null` when this device has no parent, as it is the case for e.g.
3245 * ordinary ethernet interfaces.
3247 getParent: function() {
3248 return this.dev
.parent
? Network
.prototype.instantiateDevice(this.dev
.parent
) : null;
3254 * @memberof LuCI.network
3258 * A `Network.WifiDevice` class instance represents a wireless radio device
3259 * present on the system and provides wireless capability information as
3260 * well as methods for enumerating related wireless networks.
3262 WifiDevice
= baseclass
.extend(/** @lends LuCI.network.WifiDevice.prototype */ {
3263 __init__: function(name
, radiostate
) {
3264 var uciWifiDevice
= uci
.get('wireless', name
);
3266 if (uciWifiDevice
!= null &&
3267 uciWifiDevice
['.type'] == 'wifi-device' &&
3268 uciWifiDevice
['.name'] != null) {
3269 this.sid
= uciWifiDevice
['.name'];
3272 this.sid
= this.sid
|| name
;
3280 ubus: function(/* ... */) {
3281 var v
= this._ubusdata
;
3283 for (var i
= 0; i
< arguments
.length
; i
++)
3285 v
= v
[arguments
[i
]];
3293 * Read the given UCI option value of this wireless device.
3295 * @param {string} opt
3296 * The UCI option name to read.
3298 * @returns {null|string|string[]}
3299 * Returns the UCI option value or `null` if the requested option is
3302 get: function(opt
) {
3303 return uci
.get('wireless', this.sid
, opt
);
3307 * Set the given UCI option of this network to the given value.
3309 * @param {string} opt
3310 * The name of the UCI option to set.
3312 * @param {null|string|string[]} val
3313 * The value to set or `null` to remove the given option from the
3316 set: function(opt
, value
) {
3317 return uci
.set('wireless', this.sid
, opt
, value
);
3321 * Checks whether this wireless radio is disabled.
3323 * @returns {boolean}
3324 * Returns `true` when the wireless radio is marked as disabled in `ubus`
3325 * runtime state or when the `disabled` option is set in the corresponding
3326 * UCI configuration.
3328 isDisabled: function() {
3329 return this.ubus('dev', 'disabled') || this.get('disabled') == '1';
3333 * Get the configuration name of this wireless radio.
3336 * Returns the UCI section name (e.g. `radio0`) of the corresponding
3337 * radio configuration which also serves as unique logical identifier
3338 * for the wireless phy.
3340 getName: function() {
3345 * Gets a list of supported hwmodes.
3347 * The hwmode values describe the frequency band and wireless standard
3348 * versions supported by the wireless phy.
3350 * @returns {string[]}
3351 * Returns an array of valid hwmode values for this radio. Currently
3352 * known mode values are:
3353 * - `a` - Legacy 802.11a mode, 5 GHz, up to 54 Mbit/s
3354 * - `b` - Legacy 802.11b mode, 2.4 GHz, up to 11 Mbit/s
3355 * - `g` - Legacy 802.11g mode, 2.4 GHz, up to 54 Mbit/s
3356 * - `n` - IEEE 802.11n mode, 2.4 or 5 GHz, up to 600 Mbit/s
3357 * - `ac` - IEEE 802.11ac mode, 5 GHz, up to 6770 Mbit/s
3358 * - `ax` - IEEE 802.11ax mode, 2.4 or 5 GHz
3360 getHWModes: function() {
3361 var hwmodes
= this.ubus('dev', 'iwinfo', 'hwmodes');
3362 return Array
.isArray(hwmodes
) ? hwmodes
: [ 'b', 'g' ];
3366 * Gets a list of supported htmodes.
3368 * The htmode values describe the wide-frequency options supported by
3371 * @returns {string[]}
3372 * Returns an array of valid htmode values for this radio. Currently
3373 * known mode values are:
3374 * - `HT20` - applicable to IEEE 802.11n, 20 MHz wide channels
3375 * - `HT40` - applicable to IEEE 802.11n, 40 MHz wide channels
3376 * - `VHT20` - applicable to IEEE 802.11ac, 20 MHz wide channels
3377 * - `VHT40` - applicable to IEEE 802.11ac, 40 MHz wide channels
3378 * - `VHT80` - applicable to IEEE 802.11ac, 80 MHz wide channels
3379 * - `VHT160` - applicable to IEEE 802.11ac, 160 MHz wide channels
3380 * - `HE20` - applicable to IEEE 802.11ax, 20 MHz wide channels
3381 * - `HE40` - applicable to IEEE 802.11ax, 40 MHz wide channels
3382 * - `HE80` - applicable to IEEE 802.11ax, 80 MHz wide channels
3383 * - `HE160` - applicable to IEEE 802.11ax, 160 MHz wide channels
3385 getHTModes: function() {
3386 var htmodes
= this.ubus('dev', 'iwinfo', 'htmodes');
3387 return (Array
.isArray(htmodes
) && htmodes
.length
) ? htmodes
: null;
3391 * Get a string describing the wireless radio hardware.
3394 * Returns the description string.
3396 getI18n: function() {
3397 var hw
= this.ubus('dev', 'iwinfo', 'hardware'),
3398 type
= L
.isObject(hw
) ? hw
.name
: null;
3400 if (this.ubus('dev', 'iwinfo', 'type') == 'wl')
3403 var hwmodes
= this.getHWModes(),
3406 hwmodes
.sort(function(a
, b
) {
3407 if (a
.length
!= b
.length
)
3408 return a
.length
- b
.length
;
3410 return strcmp(a
, b
);
3413 modestr
= hwmodes
.join('');
3415 return '%s 802.11%s Wireless Controller (%s)'.format(type
|| 'Generic', modestr
, this.getName());
3419 * A wireless scan result object describes a neighbouring wireless
3420 * network found in the vincinity.
3422 * @typedef {Object<string, number|string|LuCI.network.WifiEncryption>} WifiScanResult
3423 * @memberof LuCI.network
3425 * @property {string} ssid
3426 * The SSID / Mesh ID of the network.
3428 * @property {string} bssid
3429 * The BSSID if the network.
3431 * @property {string} mode
3432 * The operation mode of the network (`Master`, `Ad-Hoc`, `Mesh Point`).
3434 * @property {number} channel
3435 * The wireless channel of the network.
3437 * @property {number} signal
3438 * The received signal strength of the network in dBm.
3440 * @property {number} quality
3441 * The numeric quality level of the signal, can be used in conjunction
3442 * with `quality_max` to calculate a quality percentage.
3444 * @property {number} quality_max
3445 * The maximum possible quality level of the signal, can be used in
3446 * conjunction with `quality` to calculate a quality percentage.
3448 * @property {LuCI.network.WifiEncryption} encryption
3449 * The encryption used by the wireless network.
3453 * Trigger a wireless scan on this radio device and obtain a list of
3456 * @returns {Promise<Array<LuCI.network.WifiScanResult>>}
3457 * Returns a promise resolving to an array of scan result objects
3458 * describing the networks found in the vincinity.
3460 getScanList: function() {
3461 return callIwinfoScan(this.sid
);
3465 * Check whether the wireless radio is marked as up in the `ubus`
3468 * @returns {boolean}
3469 * Returns `true` when the radio device is up, else `false`.
3472 if (L
.isObject(_state
.radios
[this.sid
]))
3473 return (_state
.radios
[this.sid
].up
== true);
3479 * Get the wifi network of the given name belonging to this radio device
3481 * @param {string} network
3482 * The name of the wireless network to lookup. This may be either an uci
3483 * configuration section ID, a network ID in the form `radio#.network#`
3484 * or a Linux network device name like `wlan0` which is resolved to the
3485 * corresponding configuration section through `ubus` runtime information.
3487 * @returns {Promise<LuCI.network.WifiNetwork>}
3488 * Returns a promise resolving to a `Network.WifiNetwork` instance
3489 * representing the wireless network and rejecting with `null` if
3490 * the given network could not be found or is not associated with
3491 * this radio device.
3493 getWifiNetwork: function(network
) {
3494 return Network
.prototype.getWifiNetwork(network
).then(L
.bind(function(networkInstance
) {
3495 var uciWifiIface
= (networkInstance
.sid
? uci
.get('wireless', networkInstance
.sid
) : null);
3497 if (uciWifiIface
== null || uciWifiIface
['.type'] != 'wifi-iface' || uciWifiIface
.device
!= this.sid
)
3498 return Promise
.reject();
3500 return networkInstance
;
3505 * Get all wireless networks associated with this wireless radio device.
3507 * @returns {Promise<Array<LuCI.network.WifiNetwork>>}
3508 * Returns a promise resolving to an array of `Network.WifiNetwork`
3509 * instances respresenting the wireless networks associated with this
3512 getWifiNetworks: function() {
3513 return Network
.prototype.getWifiNetworks().then(L
.bind(function(networks
) {
3516 for (var i
= 0; i
< networks
.length
; i
++)
3517 if (networks
[i
].getWifiDeviceName() == this.getName())
3518 rv
.push(networks
[i
]);
3525 * Adds a new wireless network associated with this radio device to the
3526 * configuration and sets its options to the provided values.
3528 * @param {Object<string, string|string[]>} [options]
3529 * The options to set for the newly added wireless network.
3531 * @returns {Promise<null|LuCI.network.WifiNetwork>}
3532 * Returns a promise resolving to a `WifiNetwork` instance describing
3533 * the newly added wireless network or `null` if the given options
3536 addWifiNetwork: function(options
) {
3537 if (!L
.isObject(options
))
3540 options
.device
= this.sid
;
3542 return Network
.prototype.addWifiNetwork(options
);
3546 * Deletes the wireless network with the given name associated with this
3549 * @param {string} network
3550 * The name of the wireless network to lookup. This may be either an uci
3551 * configuration section ID, a network ID in the form `radio#.network#`
3552 * or a Linux network device name like `wlan0` which is resolved to the
3553 * corresponding configuration section through `ubus` runtime information.
3555 * @returns {Promise<boolean>}
3556 * Returns a promise resolving to `true` when the wireless network was
3557 * successfully deleted from the configuration or `false` when the given
3558 * network could not be found or if the found network was not associated
3559 * with this wireless radio device.
3561 deleteWifiNetwork: function(network
) {
3564 if (network
instanceof WifiNetwork
) {
3568 var uciWifiIface
= uci
.get('wireless', network
);
3570 if (uciWifiIface
== null || uciWifiIface
['.type'] != 'wifi-iface')
3571 sid
= getWifiSidByIfname(network
);
3574 if (sid
== null || uci
.get('wireless', sid
, 'device') != this.sid
)
3575 return Promise
.resolve(false);
3577 uci
.delete('wireless', network
);
3579 return Promise
.resolve(true);
3585 * @memberof LuCI.network
3589 * A `Network.WifiNetwork` instance represents a wireless network (vif)
3590 * configured on top of a radio device and provides functions for querying
3591 * the runtime state of the network. Most radio devices support multiple
3592 * such networks in parallel.
3594 WifiNetwork
= baseclass
.extend(/** @lends LuCI.network.WifiNetwork.prototype */ {
3595 __init__: function(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
) {
3606 ubus: function(/* ... */) {
3607 var v
= this._ubusdata
;
3609 for (var i
= 0; i
< arguments
.length
; i
++)
3611 v
= v
[arguments
[i
]];
3619 * Read the given UCI option value of this wireless network.
3621 * @param {string} opt
3622 * The UCI option name to read.
3624 * @returns {null|string|string[]}
3625 * Returns the UCI option value or `null` if the requested option is
3628 get: function(opt
) {
3629 return uci
.get('wireless', this.sid
, opt
);
3633 * Set the given UCI option of this network to the given value.
3635 * @param {string} opt
3636 * The name of the UCI option to set.
3638 * @param {null|string|string[]} val
3639 * The value to set or `null` to remove the given option from the
3642 set: function(opt
, value
) {
3643 return uci
.set('wireless', this.sid
, opt
, value
);
3647 * Checks whether this wireless network is disabled.
3649 * @returns {boolean}
3650 * Returns `true` when the wireless radio is marked as disabled in `ubus`
3651 * runtime state or when the `disabled` option is set in the corresponding
3652 * UCI configuration.
3654 isDisabled: function() {
3655 return this.ubus('dev', 'disabled') || this.get('disabled') == '1';
3659 * Get the configured operation mode of the wireless network.
3662 * Returns the configured operation mode. Possible values are:
3663 * - `ap` - Master (Access Point) mode
3664 * - `sta` - Station (client) mode
3665 * - `adhoc` - Ad-Hoc (IBSS) mode
3666 * - `mesh` - Mesh (IEEE 802.11s) mode
3667 * - `monitor` - Monitor mode
3669 getMode: function() {
3670 return this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3674 * Get the configured SSID of the wireless network.
3676 * @returns {null|string}
3677 * Returns the configured SSID value or `null` when this network is
3680 getSSID: function() {
3681 if (this.getMode() == 'mesh')
3684 return this.ubus('net', 'config', 'ssid') || this.get('ssid');
3688 * Get the configured Mesh ID of the wireless network.
3690 * @returns {null|string}
3691 * Returns the configured mesh ID value or `null` when this network
3692 * is not in mesh mode.
3694 getMeshID: function() {
3695 if (this.getMode() != 'mesh')
3698 return this.ubus('net', 'config', 'mesh_id') || this.get('mesh_id');
3702 * Get the configured BSSID of the wireless network.
3704 * @returns {null|string}
3705 * Returns the BSSID value or `null` if none has been specified.
3707 getBSSID: function() {
3708 return this.ubus('net', 'config', 'bssid') || this.get('bssid');
3712 * Get the names of the logical interfaces this wireless network is
3715 * @returns {string[]}
3716 * Returns an array of logical interface names.
3718 getNetworkNames: function() {
3719 return L
.toArray(this.ubus('net', 'config', 'network') || this.get('network'));
3723 * Get the internal network ID of this wireless network.
3725 * The network ID is a LuCI specific identifer in the form
3726 * `radio#.network#` to identify wireless networks by their corresponding
3727 * radio and network index numbers.
3730 * Returns the LuCI specific network ID.
3737 * Get the configuration ID of this wireless network.
3740 * Returns the corresponding UCI section ID of the network.
3742 getName: function() {
3747 * Get the Linux network device name.
3749 * @returns {null|string}
3750 * Returns the current Linux network device name as resolved from
3751 * `ubus` runtime information or `null` if this network has no
3752 * associated network device, e.g. when not configured or up.
3754 getIfname: function() {
3755 var ifname
= this.ubus('net', 'ifname') || this.ubus('net', 'iwinfo', 'ifname');
3757 if (ifname
== null || ifname
.match(/^(wifi|radio)\d/))
3758 ifname
= this.netid
;
3764 * Get the Linux VLAN network device names.
3766 * @returns {string[]}
3767 * Returns the current Linux VLAN network device name as resolved
3768 * from `ubus` runtime information or empty array if this network
3769 * has no associated VLAN network devices.
3771 getVlanIfnames: function() {
3772 var vlans
= L
.toArray(this.ubus('net', 'vlans')),
3775 for (var i
= 0; i
< vlans
.length
; i
++)
3776 ifnames
.push(vlans
[i
]['ifname']);
3782 * Get the name of the corresponding wifi radio device.
3784 * @returns {null|string}
3785 * Returns the name of the radio device this network is configured on
3786 * or `null` if it cannot be determined.
3788 getWifiDeviceName: function() {
3789 return this.ubus('radio') || this.get('device');
3793 * Get the corresponding wifi radio device.
3795 * @returns {null|LuCI.network.WifiDevice}
3796 * Returns a `Network.WifiDevice` instance representing the corresponding
3797 * wifi radio device or `null` if the related radio device could not be
3800 getWifiDevice: function() {
3801 var radioname
= this.getWifiDeviceName();
3803 if (radioname
== null)
3804 return Promise
.reject();
3806 return Network
.prototype.getWifiDevice(radioname
);
3810 * Check whether the radio network is up.
3812 * This function actually queries the up state of the related radio
3813 * device and assumes this network to be up as well when the parent
3814 * radio is up. This is due to the fact that OpenWrt does not control
3815 * virtual interfaces individually but within one common hostapd
3818 * @returns {boolean}
3819 * Returns `true` when the network is up, else `false`.
3822 var device
= this.getDevice();
3827 return device
.isUp();
3831 * Query the current operation mode from runtime information.
3834 * Returns the human readable mode name as reported by `ubus` runtime
3835 * state. Possible returned values are:
3847 getActiveMode: function() {
3848 var mode
= this.ubus('net', 'iwinfo', 'mode') || this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3851 case 'ap': return 'Master';
3852 case 'sta': return 'Client';
3853 case 'adhoc': return 'Ad-Hoc';
3854 case 'mesh': return 'Mesh';
3855 case 'monitor': return 'Monitor';
3856 default: return mode
;
3861 * Query the current operation mode from runtime information as
3862 * translated string.
3865 * Returns the translated, human readable mode name as reported by
3866 *`ubus` runtime state.
3868 getActiveModeI18n: function() {
3869 var mode
= this.getActiveMode();
3872 case 'Master': return _('Master');
3873 case 'Client': return _('Client');
3874 case 'Ad-Hoc': return _('Ad-Hoc');
3875 case 'Mash': return _('Mesh');
3876 case 'Monitor': return _('Monitor');
3877 default: return mode
;
3882 * Query the current SSID from runtime information.
3885 * Returns the current SSID or Mesh ID as reported by `ubus` runtime
3888 getActiveSSID: function() {
3889 return this.ubus('net', 'iwinfo', 'ssid') || this.ubus('net', 'config', 'ssid') || this.get('ssid');
3893 * Query the current BSSID from runtime information.
3896 * Returns the current BSSID or Mesh ID as reported by `ubus` runtime
3899 getActiveBSSID: function() {
3900 return this.ubus('net', 'iwinfo', 'bssid') || this.ubus('net', 'config', 'bssid') || this.get('bssid');
3904 * Query the current encryption settings from runtime information.
3907 * Returns a string describing the current encryption or `-` if the the
3908 * encryption state could not be found in `ubus` runtime information.
3910 getActiveEncryption: function() {
3911 return formatWifiEncryption(this.ubus('net', 'iwinfo', 'encryption')) || '-';
3915 * A wireless peer entry describes the properties of a remote wireless
3916 * peer associated with a local network.
3918 * @typedef {Object<string, boolean|number|string|LuCI.network.WifiRateEntry>} WifiPeerEntry
3919 * @memberof LuCI.network
3921 * @property {string} mac
3922 * The MAC address (BSSID).
3924 * @property {number} signal
3925 * The received signal strength.
3927 * @property {number} [signal_avg]
3928 * The average signal strength if supported by the driver.
3930 * @property {number} [noise]
3931 * The current noise floor of the radio. May be `0` or absent if not
3932 * supported by the driver.
3934 * @property {number} inactive
3935 * The amount of milliseconds the peer has been inactive, e.g. due
3938 * @property {number} connected_time
3939 * The amount of milliseconds the peer is associated to this network.
3941 * @property {number} [thr]
3942 * The estimated throughput of the peer, May be `0` or absent if not
3943 * supported by the driver.
3945 * @property {boolean} authorized
3946 * Specifies whether the peer is authorized to associate to this network.
3948 * @property {boolean} authenticated
3949 * Specifies whether the peer completed authentication to this network.
3951 * @property {string} preamble
3952 * The preamble mode used by the peer. May be `long` or `short`.
3954 * @property {boolean} wme
3955 * Specifies whether the peer supports WME/WMM capabilities.
3957 * @property {boolean} mfp
3958 * Specifies whether management frame protection is active.
3960 * @property {boolean} tdls
3961 * Specifies whether TDLS is active.
3963 * @property {number} [mesh llid]
3964 * The mesh LLID, may be `0` or absent if not applicable or supported
3967 * @property {number} [mesh plid]
3968 * The mesh PLID, may be `0` or absent if not applicable or supported
3971 * @property {string} [mesh plink]
3972 * The mesh peer link state description, may be an empty string (`''`)
3973 * or absent if not applicable or supported by the driver.
3975 * The following states are known:
3985 * @property {number} [mesh local PS]
3986 * The local powersafe mode for the peer link, may be an empty
3987 * string (`''`) or absent if not applicable or supported by
3990 * The following modes are known:
3991 * - `ACTIVE` (no power save)
3996 * @property {number} [mesh peer PS]
3997 * The remote powersafe mode for the peer link, may be an empty
3998 * string (`''`) or absent if not applicable or supported by
4001 * The following modes are known:
4002 * - `ACTIVE` (no power save)
4007 * @property {number} [mesh non-peer PS]
4008 * The powersafe mode for all non-peer neigbours, may be an empty
4009 * string (`''`) or absent if not applicable or supported by the driver.
4011 * The following modes are known:
4012 * - `ACTIVE` (no power save)
4017 * @property {LuCI.network.WifiRateEntry} rx
4018 * Describes the receiving wireless rate from the peer.
4020 * @property {LuCI.network.WifiRateEntry} tx
4021 * Describes the transmitting wireless rate to the peer.
4025 * A wireless rate entry describes the properties of a wireless
4026 * transmission rate to or from a peer.
4028 * @typedef {Object<string, boolean|number>} WifiRateEntry
4029 * @memberof LuCI.network
4031 * @property {number} [drop_misc]
4032 * The amount of received misc. packages that have been dropped, e.g.
4033 * due to corruption or missing authentication. Only applicable to
4036 * @property {number} packets
4037 * The amount of packets that have been received or sent.
4039 * @property {number} bytes
4040 * The amount of bytes that have been received or sent.
4042 * @property {number} [failed]
4043 * The amount of failed tranmission attempts. Only applicable to
4046 * @property {number} [retries]
4047 * The amount of retried transmissions. Only applicable to transmit
4050 * @property {boolean} is_ht
4051 * Specifies whether this rate is an HT (IEEE 802.11n) rate.
4053 * @property {boolean} is_vht
4054 * Specifies whether this rate is an VHT (IEEE 802.11ac) rate.
4056 * @property {number} mhz
4057 * The channel width in MHz used for the transmission.
4059 * @property {number} rate
4060 * The bitrate in bit/s of the transmission.
4062 * @property {number} [mcs]
4063 * The MCS index of the used transmission rate. Only applicable to
4066 * @property {number} [40mhz]
4067 * Specifies whether the tranmission rate used 40MHz wide channel.
4068 * Only applicable to HT or VHT rates.
4070 * Note: this option exists for backwards compatibility only and its
4071 * use is discouraged. The `mhz` field should be used instead to
4072 * determine the channel width.
4074 * @property {boolean} [short_gi]
4075 * Specifies whether a short guard interval is used for the transmission.
4076 * Only applicable to HT or VHT rates.
4078 * @property {number} [nss]
4079 * Specifies the number of spatial streams used by the transmission.
4080 * Only applicable to VHT rates.
4082 * @property {boolean} [he]
4083 * Specifies whether this rate is an HE (IEEE 802.11ax) rate.
4085 * @property {number} [he_gi]
4086 * Specifies whether the guard interval used for the transmission.
4087 * Only applicable to HE rates.
4089 * @property {number} [he_dcm]
4090 * Specifies whether dual concurrent modulation is used for the transmission.
4091 * Only applicable to HE rates.
4095 * Fetch the list of associated peers.
4097 * @returns {Promise<Array<LuCI.network.WifiPeerEntry>>}
4098 * Returns a promise resolving to an array of wireless peers associated
4099 * with this network.
4101 getAssocList: function() {
4103 var ifnames
= [ this.getIfname() ].concat(this.getVlanIfnames());
4105 for (var i
= 0; i
< ifnames
.length
; i
++)
4106 tasks
.push(callIwinfoAssoclist(ifnames
[i
]));
4108 return Promise
.all(tasks
).then(function(values
) {
4109 return Array
.prototype.concat
.apply([], values
);
4114 * Query the current operating frequency of the wireless network.
4116 * @returns {null|string}
4117 * Returns the current operating frequency of the network from `ubus`
4118 * runtime information in GHz or `null` if the information is not
4121 getFrequency: function() {
4122 var freq
= this.ubus('net', 'iwinfo', 'frequency');
4124 if (freq
!= null && freq
> 0)
4125 return '%.03f'.format(freq
/ 1000);
4131 * Query the current average bitrate of all peers associated to this
4134 * @returns {null|number}
4135 * Returns the average bit rate among all peers associated to the network
4136 * as reported by `ubus` runtime information or `null` if the information
4139 getBitRate: function() {
4140 var rate
= this.ubus('net', 'iwinfo', 'bitrate');
4142 if (rate
!= null && rate
> 0)
4143 return (rate
/ 1000);
4149 * Query the current wireless channel.
4151 * @returns {null|number}
4152 * Returns the wireless channel as reported by `ubus` runtime information
4153 * or `null` if it cannot be determined.
4155 getChannel: function() {
4156 return this.ubus('net', 'iwinfo', 'channel') || this.ubus('dev', 'config', 'channel') || this.get('channel');
4160 * Query the current wireless signal.
4162 * @returns {null|number}
4163 * Returns the wireless signal in dBm as reported by `ubus` runtime
4164 * information or `null` if it cannot be determined.
4166 getSignal: function() {
4167 return this.ubus('net', 'iwinfo', 'signal') || 0;
4171 * Query the current radio noise floor.
4174 * Returns the radio noise floor in dBm as reported by `ubus` runtime
4175 * information or `0` if it cannot be determined.
4177 getNoise: function() {
4178 return this.ubus('net', 'iwinfo', 'noise') || 0;
4182 * Query the current country code.
4185 * Returns the wireless country code as reported by `ubus` runtime
4186 * information or `00` if it cannot be determined.
4188 getCountryCode: function() {
4189 return this.ubus('net', 'iwinfo', 'country') || this.ubus('dev', 'config', 'country') || '00';
4193 * Query the current radio TX power.
4195 * @returns {null|number}
4196 * Returns the wireless network transmit power in dBm as reported by
4197 * `ubus` runtime information or `null` if it cannot be determined.
4199 getTXPower: function() {
4200 return this.ubus('net', 'iwinfo', 'txpower');
4204 * Query the radio TX power offset.
4206 * Some wireless radios have a fixed power offset, e.g. due to the
4207 * use of external amplifiers.
4210 * Returns the wireless network transmit power offset in dBm as reported
4211 * by `ubus` runtime information or `0` if there is no offset, or if it
4212 * cannot be determined.
4214 getTXPowerOffset: function() {
4215 return this.ubus('net', 'iwinfo', 'txpower_offset') || 0;
4219 * Calculate the current signal.
4223 * Returns the calculated signal level, which is the difference between
4224 * noise and signal (SNR), divided by 5.
4226 getSignalLevel: function(signal
, noise
) {
4227 if (this.getActiveBSSID() == '00:00:00:00:00:00')
4230 signal
= signal
|| this.getSignal();
4231 noise
= noise
|| this.getNoise();
4233 if (signal
< 0 && noise
< 0) {
4234 var snr
= -1 * (noise
- signal
);
4235 return Math
.floor(snr
/ 5);
4242 * Calculate the current signal quality percentage.
4245 * Returns the calculated signal quality in percent. The value is
4246 * calculated from the `quality` and `quality_max` indicators reported
4247 * by `ubus` runtime state.
4249 getSignalPercent: function() {
4250 var qc
= this.ubus('net', 'iwinfo', 'quality') || 0,
4251 qm
= this.ubus('net', 'iwinfo', 'quality_max') || 0;
4253 if (qc
> 0 && qm
> 0)
4254 return Math
.floor((100 / qm
) * qc
);
4260 * Get a short description string for this wireless network.
4263 * Returns a string describing this network, consisting of the
4264 * active operation mode, followed by either the SSID, BSSID or
4265 * internal network ID, depending on which information is available.
4267 getShortName: function() {
4268 return '%s "%s"'.format(
4269 this.getActiveModeI18n(),
4270 this.getActiveSSID() || this.getActiveBSSID() || this.getID());
4274 * Get a description string for this wireless network.
4277 * Returns a string describing this network, consisting of the
4278 * term `Wireless Network`, followed by the active operation mode,
4279 * the SSID, BSSID or internal network ID and the Linux network device
4280 * name, depending on which information is available.
4282 getI18n: function() {
4283 return '%s: %s "%s" (%s)'.format(
4284 _('Wireless Network'),
4285 this.getActiveModeI18n(),
4286 this.getActiveSSID() || this.getActiveBSSID() || this.getID(),
4291 * Get the primary logical interface this wireless network is attached to.
4293 * @returns {null|LuCI.network.Protocol}
4294 * Returns a `Network.Protocol` instance representing the logical
4295 * interface or `null` if this network is not attached to any logical
4298 getNetwork: function() {
4299 return this.getNetworks()[0];
4303 * Get the logical interfaces this wireless network is attached to.
4305 * @returns {Array<LuCI.network.Protocol>}
4306 * Returns an array of `Network.Protocol` instances representing the
4307 * logical interfaces this wireless network is attached to.
4309 getNetworks: function() {
4310 var networkNames
= this.getNetworkNames(),
4313 for (var i
= 0; i
< networkNames
.length
; i
++) {
4314 var uciInterface
= uci
.get('network', networkNames
[i
]);
4316 if (uciInterface
== null || uciInterface
['.type'] != 'interface')
4319 networks
.push(Network
.prototype.instantiateNetwork(networkNames
[i
]));
4322 networks
.sort(networkSort
);
4328 * Get the associated Linux network device.
4330 * @returns {LuCI.network.Device}
4331 * Returns a `Network.Device` instance representing the Linux network
4332 * device associted with this wireless network.
4334 getDevice: function() {
4335 return Network
.prototype.instantiateDevice(this.getIfname());
4339 * Check whether this wifi network supports deauthenticating clients.
4341 * @returns {boolean}
4342 * Returns `true` when this wifi network instance supports forcibly
4343 * deauthenticating clients, otherwise `false`.
4345 isClientDisconnectSupported: function() {
4346 return L
.isObject(this.ubus('hostapd', 'del_client'));
4350 * Forcibly disconnect the given client from the wireless network.
4352 * @param {string} mac
4353 * The MAC address of the client to disconnect.
4355 * @param {boolean} [deauth=false]
4356 * Specifies whether to deauthenticate (`true`) or disassociate (`false`)
4359 * @param {number} [reason=1]
4360 * Specifies the IEEE 802.11 reason code to disassoc/deauth the client
4361 * with. Default is `1` which corresponds to `Unspecified reason`.
4363 * @param {number} [ban_time=0]
4364 * Specifies the amount of milliseconds to ban the client from
4365 * reconnecting. By default, no ban time is set which allows the client
4366 * to reassociate / reauthenticate immediately.
4368 * @returns {Promise<number>}
4369 * Returns a promise resolving to the underlying ubus call result code
4370 * which is typically `0`, even for not existing MAC addresses.
4371 * The promise might reject with an error in case invalid arguments
4374 disconnectClient: function(mac
, deauth
, reason
, ban_time
) {
4375 if (reason
== null || reason
== 0)
4381 return rpc
.declare({
4382 object
: 'hostapd.%s'.format(this.getIfname()),
4383 method
: 'del_client',
4384 params
: [ 'addr', 'deauth', 'reason', 'ban_time' ]
4385 })(mac
, deauth
, reason
, ban_time
);