9 CONNECT_FAILED
: _('Connection attempt failed'),
10 INVALID_ADDRESS
: _('IP address is invalid'),
11 INVALID_GATEWAY
: _('Gateway address is invalid'),
12 INVALID_LOCAL_ADDRESS
: _('Local IP address is invalid'),
13 MISSING_ADDRESS
: _('IP address is missing'),
14 MISSING_PEER_ADDRESS
: _('Peer address is missing'),
15 NO_DEVICE
: _('Network device is not present'),
16 NO_IFACE
: _('Unable to determine device name'),
17 NO_IFNAME
: _('Unable to determine device name'),
18 NO_WAN_ADDRESS
: _('Unable to determine external IP address'),
19 NO_WAN_LINK
: _('Unable to determine upstream interface'),
20 PEER_RESOLVE_FAIL
: _('Unable to resolve peer host name'),
21 PIN_FAILED
: _('PIN code rejected')
24 var iface_patterns_ignore
= [
40 var iface_patterns_wireless
= [
47 var iface_patterns_virtual
= [ ];
49 var callLuciNetworkDevices
= rpc
.declare({
51 method
: 'getNetworkDevices',
55 var callLuciWirelessDevices
= rpc
.declare({
57 method
: 'getWirelessDevices',
61 var callLuciBoardJSON
= rpc
.declare({
63 method
: 'getBoardJSON'
66 var callLuciHostHints
= rpc
.declare({
68 method
: 'getHostHints',
72 var callIwinfoAssoclist
= rpc
.declare({
75 params
: [ 'device', 'mac' ],
76 expect
: { results
: [] }
79 var callIwinfoScan
= rpc
.declare({
84 expect
: { results
: [] }
87 var callNetworkInterfaceDump
= rpc
.declare({
88 object
: 'network.interface',
90 expect
: { 'interface': [] }
93 var callNetworkProtoHandlers
= rpc
.declare({
95 method
: 'get_proto_handlers',
104 function getProtocolHandlers(cache
) {
105 return callNetworkProtoHandlers().then(function(protos
) {
106 /* Register "none" protocol */
107 if (!protos
.hasOwnProperty('none'))
108 Object
.assign(protos
, { none
: { no_device
: false } });
110 /* Hack: emulate relayd protocol */
111 if (!protos
.hasOwnProperty('relay') && L
.hasSystemFeature('relayd'))
112 Object
.assign(protos
, { relay
: { no_device
: true } });
114 Object
.assign(_protospecs
, protos
);
116 return Promise
.all(Object
.keys(protos
).map(function(p
) {
117 return Promise
.resolve(L
.require('protocol.%s'.format(p
))).catch(function(err
) {
118 if (L
.isObject(err
) && err
.name
!= 'NetworkError')
121 })).then(function() {
124 }).catch(function() {
129 function getWifiStateBySid(sid
) {
130 var s
= uci
.get('wireless', sid
);
132 if (s
!= null && s
['.type'] == 'wifi-iface') {
133 for (var radioname
in _state
.radios
) {
134 for (var i
= 0; i
< _state
.radios
[radioname
].interfaces
.length
; i
++) {
135 var netstate
= _state
.radios
[radioname
].interfaces
[i
];
137 if (typeof(netstate
.section
) != 'string')
140 var s2
= uci
.get('wireless', netstate
.section
);
142 if (s2
!= null && s
['.type'] == s2
['.type'] && s
['.name'] == s2
['.name']) {
143 if (s2
['.anonymous'] == false && netstate
.section
.charAt(0) == '@')
146 return [ radioname
, _state
.radios
[radioname
], netstate
];
155 function getWifiStateByIfname(ifname
) {
156 for (var radioname
in _state
.radios
) {
157 for (var i
= 0; i
< _state
.radios
[radioname
].interfaces
.length
; i
++) {
158 var netstate
= _state
.radios
[radioname
].interfaces
[i
];
160 if (typeof(netstate
.ifname
) != 'string')
163 if (netstate
.ifname
== ifname
)
164 return [ radioname
, _state
.radios
[radioname
], netstate
];
171 function isWifiIfname(ifname
) {
172 for (var i
= 0; i
< iface_patterns_wireless
.length
; i
++)
173 if (iface_patterns_wireless
[i
].test(ifname
))
179 function getWifiSidByNetid(netid
) {
180 var m
= /^(\w+)\.network(\d+)$/.exec(netid
);
182 var sections
= uci
.sections('wireless', 'wifi-iface');
183 for (var i
= 0, n
= 0; i
< sections
.length
; i
++) {
184 if (sections
[i
].device
!= m
[1])
188 return sections
[i
]['.name'];
195 function getWifiSidByIfname(ifname
) {
196 var sid
= getWifiSidByNetid(ifname
);
201 var res
= getWifiStateByIfname(ifname
);
203 if (res
!= null && L
.isObject(res
[2]) && typeof(res
[2].section
) == 'string')
204 return res
[2].section
;
209 function getWifiNetidBySid(sid
) {
210 var s
= uci
.get('wireless', sid
);
211 if (s
!= null && s
['.type'] == 'wifi-iface') {
212 var radioname
= s
.device
;
213 if (typeof(s
.device
) == 'string') {
214 var i
= 0, netid
= null, sections
= uci
.sections('wireless', 'wifi-iface');
215 for (var i
= 0, n
= 0; i
< sections
.length
; i
++) {
216 if (sections
[i
].device
!= s
.device
)
221 if (sections
[i
]['.name'] != s
['.name'])
224 return [ '%s.network%d'.format(s
.device
, n
), s
.device
];
233 function getWifiNetidByNetname(name
) {
234 var sections
= uci
.sections('wireless', 'wifi-iface');
235 for (var i
= 0; i
< sections
.length
; i
++) {
236 if (typeof(sections
[i
].network
) != 'string')
239 var nets
= sections
[i
].network
.split(/\s+/);
240 for (var j
= 0; j
< nets
.length
; j
++) {
244 return getWifiNetidBySid(sections
[i
]['.name']);
251 function isVirtualIfname(ifname
) {
252 for (var i
= 0; i
< iface_patterns_virtual
.length
; i
++)
253 if (iface_patterns_virtual
[i
].test(ifname
))
259 function isIgnoredIfname(ifname
) {
260 for (var i
= 0; i
< iface_patterns_ignore
.length
; i
++)
261 if (iface_patterns_ignore
[i
].test(ifname
))
267 function appendValue(config
, section
, option
, value
) {
268 var values
= uci
.get(config
, section
, option
),
269 isArray
= Array
.isArray(values
),
272 if (isArray
== false)
273 values
= L
.toArray(values
);
275 if (values
.indexOf(value
) == -1) {
280 uci
.set(config
, section
, option
, isArray
? values
: values
.join(' '));
285 function removeValue(config
, section
, option
, value
) {
286 var values
= uci
.get(config
, section
, option
),
287 isArray
= Array
.isArray(values
),
290 if (isArray
== false)
291 values
= L
.toArray(values
);
293 for (var i
= values
.length
- 1; i
>= 0; i
--) {
294 if (values
[i
] == value
) {
300 if (values
.length
> 0)
301 uci
.set(config
, section
, option
, isArray
? values
: values
.join(' '));
303 uci
.unset(config
, section
, option
);
308 function prefixToMask(bits
, v6
) {
309 var w
= v6
? 128 : 32,
315 for (var i
= 0; i
< w
/ 16; i
++) {
316 var b
= Math
.min(16, bits
);
317 m
.push((0xffff << (16 - b
)) & 0xffff);
322 return String
.prototype.format
.apply('%x:%x:%x:%x:%x:%x:%x:%x', m
).replace(/:0(?::0)+$/, '::');
324 return '%d.%d.%d.%d'.format(m
[0] >>> 8, m
[0] & 0xff, m
[1] >>> 8, m
[1] & 0xff);
327 function maskToPrefix(mask
, v6
) {
328 var m
= v6
? validation
.parseIPv6(mask
) : validation
.parseIPv4(mask
);
335 for (var i
= 0, z
= false; i
< m
.length
; i
++) {
338 while (!z
&& (m
[i
] & (v6
? 0x8000 : 0x80))) {
339 m
[i
] = (m
[i
] << 1) & (v6
? 0xffff : 0xff);
350 function initNetworkState(refresh
) {
351 if (_state
== null || refresh
) {
352 _init
= _init
|| Promise
.all([
353 L
.resolveDefault(callNetworkInterfaceDump(), []),
354 L
.resolveDefault(callLuciBoardJSON(), {}),
355 L
.resolveDefault(callLuciNetworkDevices(), {}),
356 L
.resolveDefault(callLuciWirelessDevices(), {}),
357 L
.resolveDefault(callLuciHostHints(), {}),
358 getProtocolHandlers(),
359 L
.resolveDefault(uci
.load('network')),
360 L
.resolveDefault(uci
.load('wireless')),
361 L
.resolveDefault(uci
.load('luci'))
362 ]).then(function(data
) {
363 var netifd_ifaces
= data
[0],
364 board_json
= data
[1],
368 isTunnel
: {}, isBridge
: {}, isSwitch
: {}, isWifi
: {},
369 ifaces
: netifd_ifaces
, radios
: data
[3], hosts
: data
[4],
370 netdevs
: {}, bridges
: {}, switches
: {}, hostapd
: {}
373 for (var name
in luci_devs
) {
374 var dev
= luci_devs
[name
];
376 if (isVirtualIfname(name
))
377 s
.isTunnel
[name
] = true;
379 if (!s
.isTunnel
[name
] && isIgnoredIfname(name
))
382 s
.netdevs
[name
] = s
.netdevs
[name
] || {
391 devtype
: dev
.devtype
,
394 wireless
: dev
.wireless
,
400 if (Array
.isArray(dev
.ipaddrs
))
401 for (var i
= 0; i
< dev
.ipaddrs
.length
; i
++)
402 s
.netdevs
[name
].ipaddrs
.push(dev
.ipaddrs
[i
].address
+ '/' + dev
.ipaddrs
[i
].netmask
);
404 if (Array
.isArray(dev
.ip6addrs
))
405 for (var i
= 0; i
< dev
.ip6addrs
.length
; i
++)
406 s
.netdevs
[name
].ip6addrs
.push(dev
.ip6addrs
[i
].address
+ '/' + dev
.ip6addrs
[i
].netmask
);
409 for (var name
in luci_devs
) {
410 var dev
= luci_devs
[name
];
422 for (var i
= 0; dev
.ports
&& i
< dev
.ports
.length
; i
++) {
423 var subdev
= s
.netdevs
[dev
.ports
[i
]];
428 b
.ifnames
.push(subdev
);
433 s
.isBridge
[name
] = true;
436 for (var name
in luci_devs
) {
437 var dev
= luci_devs
[name
];
439 if (!dev
.parent
|| dev
.devtype
!= 'dsa')
442 s
.isSwitch
[dev
.parent
] = true;
443 s
.isSwitch
[name
] = true;
446 if (L
.isObject(board_json
.switch)) {
447 for (var switchname
in board_json
.switch) {
448 var layout
= board_json
.switch[switchname
],
455 if (L
.isObject(layout
) && Array
.isArray(layout
.ports
)) {
456 for (var i
= 0, port
; (port
= layout
.ports
[i
]) != null; i
++) {
457 if (typeof(port
) == 'object' && typeof(port
.num
) == 'number' &&
458 (typeof(port
.role
) == 'string' || typeof(port
.device
) == 'string')) {
461 role
: port
.role
|| 'cpu',
462 index
: (port
.index
!= null) ? port
.index
: port
.num
465 if (port
.device
!= null) {
466 spec
.device
= port
.device
;
467 spec
.tagged
= spec
.need_tag
;
468 netdevs
[port
.num
] = port
.device
;
473 if (port
.role
!= null)
474 nports
[port
.role
] = (nports
[port
.role
] || 0) + 1;
478 ports
.sort(function(a
, b
) {
479 return L
.naturalCompare(a
.role
, b
.role
) || L
.naturalCompare(a
.index
, b
.index
);
482 for (var i
= 0, port
; (port
= ports
[i
]) != null; i
++) {
483 if (port
.role
!= role
) {
489 port
.label
= 'CPU (%s)'.format(port
.device
);
490 else if (nports
[role
] > 1)
491 port
.label
= '%s %d'.format(role
.toUpperCase(), pnum
++);
493 port
.label
= role
.toUpperCase();
499 s
.switches
[switchname
] = {
507 if (L
.isObject(board_json
.dsl
) && L
.isObject(board_json
.dsl
.modem
)) {
508 s
.hasDSLModem
= board_json
.dsl
.modem
;
515 if (L
.isObject(s
.radios
))
516 for (var radio
in s
.radios
)
517 if (L
.isObject(s
.radios
[radio
]) && Array
.isArray(s
.radios
[radio
].interfaces
))
518 for (var i
= 0; i
< s
.radios
[radio
].interfaces
.length
; i
++)
519 if (L
.isObject(s
.radios
[radio
].interfaces
[i
]) && s
.radios
[radio
].interfaces
[i
].ifname
)
520 objects
.push('hostapd.%s'.format(s
.radios
[radio
].interfaces
[i
].ifname
));
522 return (objects
.length
? L
.resolveDefault(rpc
.list
.apply(rpc
, objects
), {}) : Promise
.resolve({})).then(function(res
) {
524 var m
= k
.match(/^hostapd\.(.+)$/);
526 s
.hostapd
[m
[1]] = res
[k
];
534 return (_state
!= null ? Promise
.resolve(_state
) : _init
);
537 function ifnameOf(obj
) {
538 if (obj
instanceof Protocol
)
539 return obj
.getIfname();
540 else if (obj
instanceof Device
)
541 return obj
.getName();
542 else if (obj
instanceof WifiDevice
)
543 return obj
.getName();
544 else if (obj
instanceof WifiNetwork
)
545 return obj
.getIfname();
546 else if (typeof(obj
) == 'string')
547 return obj
.replace(/:.+$/, '');
552 function networkSort(a
, b
) {
553 return L
.naturalCompare(a
.getName(), b
.getName());
556 function deviceSort(a
, b
) {
557 var typeWeigth
= { wifi
: 2, alias
: 3 };
559 return L
.naturalCompare(typeWeigth
[a
.getType()] || 1, typeWeigth
[b
.getType()] || 1) ||
560 L
.naturalCompare(a
.getName(), b
.getName());
563 function formatWifiEncryption(enc
) {
564 if (!L
.isObject(enc
))
570 var ciphers
= Array
.isArray(enc
.ciphers
)
571 ? enc
.ciphers
.map(function(c
) { return c
.toUpperCase() }) : [ 'NONE' ];
573 if (Array
.isArray(enc
.wep
)) {
574 var has_open
= false,
577 for (var i
= 0; i
< enc
.wep
.length
; i
++)
578 if (enc
.wep
[i
] == 'open')
580 else if (enc
.wep
[i
] == 'shared')
583 if (has_open
&& has_shared
)
584 return 'WEP Open/Shared (%s)'.format(ciphers
.join(', '));
586 return 'WEP Open System (%s)'.format(ciphers
.join(', '));
588 return 'WEP Shared Auth (%s)'.format(ciphers
.join(', '));
593 if (Array
.isArray(enc
.wpa
)) {
595 suites
= Array
.isArray(enc
.authentication
)
596 ? enc
.authentication
.map(function(a
) { return a
.toUpperCase() }) : [ 'NONE' ];
598 for (var i
= 0; i
< enc
.wpa
.length
; i
++)
599 switch (enc
.wpa
[i
]) {
601 versions
.push('WPA');
605 versions
.push('WPA%d'.format(enc
.wpa
[i
]));
609 if (versions
.length
> 1)
610 return 'mixed %s %s (%s)'.format(versions
.join('/'), suites
.join(', '), ciphers
.join(', '));
612 return '%s %s (%s)'.format(versions
[0], suites
.join(', '), ciphers
.join(', '));
618 function enumerateNetworks() {
619 var uciInterfaces
= uci
.sections('network', 'interface'),
622 for (var i
= 0; i
< uciInterfaces
.length
; i
++)
623 networks
[uciInterfaces
[i
]['.name']] = this.instantiateNetwork(uciInterfaces
[i
]['.name']);
625 for (var i
= 0; i
< _state
.ifaces
.length
; i
++)
626 if (networks
[_state
.ifaces
[i
].interface] == null)
627 networks
[_state
.ifaces
[i
].interface] =
628 this.instantiateNetwork(_state
.ifaces
[i
].interface, _state
.ifaces
[i
].proto
);
632 for (var network
in networks
)
633 if (networks
.hasOwnProperty(network
))
634 rv
.push(networks
[network
]);
636 rv
.sort(networkSort
);
642 var Hosts
, Network
, Protocol
, Device
, WifiDevice
, WifiNetwork
;
650 * The `LuCI.network` class combines data from multiple `ubus` apis to
651 * provide an abstraction of the current network configuration state.
653 * It provides methods to enumerate interfaces and devices, to query
654 * current configuration details and to manipulate settings.
656 Network
= baseclass
.extend(/** @lends LuCI.network.prototype */ {
658 * Converts the given prefix size in bits to a netmask.
662 * @param {number} bits
663 * The prefix size in bits.
665 * @param {boolean} [v6=false]
666 * Whether to convert the bits value into an IPv4 netmask (`false`) or
667 * an IPv6 netmask (`true`).
669 * @returns {null|string}
670 * Returns a string containing the netmask corresponding to the bit count
671 * or `null` when the given amount of bits exceeds the maximum possible
672 * value of `32` for IPv4 or `128` for IPv6.
674 prefixToMask
: prefixToMask
,
677 * Converts the given netmask to a prefix size in bits.
681 * @param {string} netmask
682 * The netmask to convert into a bit count.
684 * @param {boolean} [v6=false]
685 * Whether to parse the given netmask as IPv4 (`false`) or IPv6 (`true`)
688 * @returns {null|number}
689 * Returns the number of prefix bits contained in the netmask or `null`
690 * if the given netmask value was invalid.
692 maskToPrefix
: maskToPrefix
,
695 * An encryption entry describes active wireless encryption settings
696 * such as the used key management protocols, active ciphers and
699 * @typedef {Object<string, boolean|Array<number|string>>} LuCI.network.WifiEncryption
700 * @memberof LuCI.network
702 * @property {boolean} enabled
703 * Specifies whether any kind of encryption, such as `WEP` or `WPA` is
704 * enabled. If set to `false`, then no encryption is active and the
705 * corresponding network is open.
707 * @property {string[]} [wep]
708 * When the `wep` property exists, the network uses WEP encryption.
709 * In this case, the property is set to an array of active WEP modes
710 * which might be either `open`, `shared` or both.
712 * @property {number[]} [wpa]
713 * When the `wpa` property exists, the network uses WPA security.
714 * In this case, the property is set to an array containing the WPA
715 * protocol versions used, e.g. `[ 1, 2 ]` for WPA/WPA2 mixed mode or
716 * `[ 3 ]` for WPA3-SAE.
718 * @property {string[]} [authentication]
719 * The `authentication` property only applies to WPA encryption and
720 * is defined when the `wpa` property is set as well. It points to
721 * an array of active authentication suites used by the network, e.g.
722 * `[ "psk" ]` for a WPA(2)-PSK network or `[ "psk", "sae" ]` for
723 * mixed WPA2-PSK/WPA3-SAE encryption.
725 * @property {string[]} [ciphers]
726 * If either WEP or WPA encryption is active, then the `ciphers`
727 * property will be set to an array describing the active encryption
728 * ciphers used by the network, e.g. `[ "tkip", "ccmp" ]` for a
729 * WPA/WPA2-PSK mixed network or `[ "wep-40", "wep-104" ]` for an
734 * Converts a given {@link LuCI.network.WifiEncryption encryption entry}
735 * into a human readable string such as `mixed WPA/WPA2 PSK (TKIP, CCMP)`
736 * or `WPA3 SAE (CCMP)`.
740 * @param {LuCI.network.WifiEncryption} encryption
741 * The wireless encryption entry to convert.
743 * @returns {null|string}
744 * Returns the description string for the given encryption entry or
745 * `null` if the given entry was invalid.
747 formatWifiEncryption
: formatWifiEncryption
,
750 * Flushes the local network state cache and fetches updated information
751 * from the remote `ubus` apis.
753 * @returns {Promise<Object>}
754 * Returns a promise resolving to the internal network state object.
756 flushCache: function() {
757 initNetworkState(true);
762 * Instantiates the given {@link LuCI.network.Protocol Protocol} backend,
763 * optionally using the given network name.
765 * @param {string} protoname
766 * The protocol backend to use, e.g. `static` or `dhcp`.
768 * @param {string} [netname=__dummy__]
769 * The network name to use for the instantiated protocol. This should be
770 * usually set to one of the interfaces described in /etc/config/network
771 * but it is allowed to omit it, e.g. to query protocol capabilities
772 * without the need for an existing interface.
774 * @returns {null|LuCI.network.Protocol}
775 * Returns the instantiated protocol backend class or `null` if the given
776 * protocol isn't known.
778 getProtocol: function(protoname
, netname
) {
779 var v
= _protocols
[protoname
];
781 return new v(netname
|| '__dummy__');
787 * Obtains instances of all known {@link LuCI.network.Protocol Protocol}
790 * @returns {Array<LuCI.network.Protocol>}
791 * Returns an array of protocol class instances.
793 getProtocols: function() {
796 for (var protoname
in _protocols
)
797 rv
.push(new _protocols
[protoname
]('__dummy__'));
803 * Registers a new {@link LuCI.network.Protocol Protocol} subclass
804 * with the given methods and returns the resulting subclass value.
806 * This functions internally calls
807 * {@link LuCI.Class.extend Class.extend()} on the `Network.Protocol`
810 * @param {string} protoname
811 * The name of the new protocol to register.
813 * @param {Object<string, *>} methods
814 * The member methods and values of the new `Protocol` subclass to
815 * be passed to {@link LuCI.Class.extend Class.extend()}.
817 * @returns {LuCI.network.Protocol}
818 * Returns the new `Protocol` subclass.
820 registerProtocol: function(protoname
, methods
) {
821 var spec
= L
.isObject(_protospecs
) ? _protospecs
[protoname
] : null;
822 var proto
= Protocol
.extend(Object
.assign({
823 getI18n: function() {
827 isFloating: function() {
831 isVirtual: function() {
832 return (L
.isObject(spec
) && spec
.no_device
== true);
835 renderFormOptions: function(section
) {
839 __init__: function(name
) {
843 getProtocol: function() {
848 _protocols
[protoname
] = proto
;
854 * Registers a new regular expression pattern to recognize
855 * virtual interfaces.
857 * @param {RegExp} pat
858 * A `RegExp` instance to match a virtual interface name
859 * such as `6in4-wan` or `tun0`.
861 registerPatternVirtual: function(pat
) {
862 iface_patterns_virtual
.push(pat
);
866 * Registers a new human readable translation string for a `Protocol`
869 * @param {string} code
870 * The `ubus` protocol error code to register a translation for, e.g.
873 * @param {string} message
874 * The message to use as translation for the given protocol error code.
877 * Returns `true` if the error code description has been added or `false`
878 * if either the arguments were invalid or if there already was a
879 * description for the given code.
881 registerErrorCode: function(code
, message
) {
882 if (typeof(code
) == 'string' &&
883 typeof(message
) == 'string' &&
884 !proto_errors
.hasOwnProperty(code
)) {
885 proto_errors
[code
] = message
;
893 * Adds a new network of the given name and update it with the given
896 * If a network with the given name already exist but is empty, then
897 * this function will update its option, otherwise it will do nothing.
899 * @param {string} name
900 * The name of the network to add. Must be in the format `[a-zA-Z0-9_]+`.
902 * @param {Object<string, string|string[]>} [options]
903 * An object of uci option values to set on the new network or to
904 * update in an existing, empty network.
906 * @returns {Promise<null|LuCI.network.Protocol>}
907 * Returns a promise resolving to the `Protocol` subclass instance
908 * describing the added network or resolving to `null` if the name
909 * was invalid or if a non-empty network of the given name already
912 addNetwork: function(name
, options
) {
913 return this.getNetwork(name
).then(L
.bind(function(existingNetwork
) {
914 if (name
!= null && /^[a-zA-Z0-9_]+$/.test(name
) && existingNetwork
== null) {
915 var sid
= uci
.add('network', 'interface', name
);
918 if (L
.isObject(options
))
919 for (var key
in options
)
920 if (options
.hasOwnProperty(key
))
921 uci
.set('network', sid
, key
, options
[key
]);
923 return this.instantiateNetwork(sid
);
926 else if (existingNetwork
!= null && existingNetwork
.isEmpty()) {
927 if (L
.isObject(options
))
928 for (var key
in options
)
929 if (options
.hasOwnProperty(key
))
930 existingNetwork
.set(key
, options
[key
]);
932 return existingNetwork
;
938 * Get a {@link LuCI.network.Protocol Protocol} instance describing
939 * the network with the given name.
941 * @param {string} name
942 * The logical interface name of the network get, e.g. `lan` or `wan`.
944 * @returns {Promise<null|LuCI.network.Protocol>}
945 * Returns a promise resolving to a
946 * {@link LuCI.network.Protocol Protocol} subclass instance describing
947 * the network or `null` if the network did not exist.
949 getNetwork: function(name
) {
950 return initNetworkState().then(L
.bind(function() {
951 var section
= (name
!= null) ? uci
.get('network', name
) : null;
953 if (section
!= null && section
['.type'] == 'interface') {
954 return this.instantiateNetwork(name
);
956 else if (name
!= null) {
957 for (var i
= 0; i
< _state
.ifaces
.length
; i
++)
958 if (_state
.ifaces
[i
].interface == name
)
959 return this.instantiateNetwork(name
, _state
.ifaces
[i
].proto
);
967 * Gets an array containing all known networks.
969 * @returns {Promise<Array<LuCI.network.Protocol>>}
970 * Returns a promise resolving to a name-sorted array of
971 * {@link LuCI.network.Protocol Protocol} subclass instances
972 * describing all known networks.
974 getNetworks: function() {
975 return initNetworkState().then(L
.bind(enumerateNetworks
, this));
979 * Deletes the given network and its references from the network and
980 * firewall configuration.
982 * @param {string} name
983 * The name of the network to delete.
985 * @returns {Promise<boolean>}
986 * Returns a promise resolving to either `true` if the network and
987 * references to it were successfully deleted from the configuration or
988 * `false` if the given network could not be found.
990 deleteNetwork: function(name
) {
991 var requireFirewall
= Promise
.resolve(L
.require('firewall')).catch(function() {}),
992 loadDHCP
= L
.resolveDefault(uci
.load('dhcp')),
993 network
= this.instantiateNetwork(name
);
995 return Promise
.all([ requireFirewall
, loadDHCP
, initNetworkState() ]).then(function(res
) {
996 var uciInterface
= uci
.get('network', name
),
999 if (uciInterface
!= null && uciInterface
['.type'] == 'interface') {
1000 return Promise
.resolve(network
? network
.deleteConfiguration() : null).then(function() {
1001 uci
.remove('network', name
);
1003 uci
.sections('luci', 'ifstate', function(s
) {
1004 if (s
.interface == name
)
1005 uci
.remove('luci', s
['.name']);
1008 uci
.sections('network', null, function(s
) {
1009 switch (s
['.type']) {
1013 if (s
.interface == name
)
1014 uci
.remove('network', s
['.name']);
1020 if (s
.in == name
|| s
.out
== name
)
1021 uci
.remove('network', s
['.name']);
1027 uci
.sections('wireless', 'wifi-iface', function(s
) {
1028 var networks
= L
.toArray(s
.network
).filter(function(network
) { return network
!= name
});
1030 if (networks
.length
> 0)
1031 uci
.set('wireless', s
['.name'], 'network', networks
.join(' '));
1033 uci
.unset('wireless', s
['.name'], 'network');
1036 uci
.sections('dhcp', 'dhcp', function(s
) {
1037 if (s
.interface == name
)
1038 uci
.remove('dhcp', s
['.name']);
1042 return firewall
.deleteNetwork(name
).then(function() { return true });
1045 }).catch(function() {
1055 * Rename the given network and its references to a new name.
1057 * @param {string} oldName
1058 * The current name of the network.
1060 * @param {string} newName
1061 * The name to rename the network to, must be in the format
1064 * @returns {Promise<boolean>}
1065 * Returns a promise resolving to either `true` if the network was
1066 * successfully renamed or `false` if the new name was invalid, if
1067 * a network with the new name already exists or if the network to
1068 * rename could not be found.
1070 renameNetwork: function(oldName
, newName
) {
1071 return initNetworkState().then(function() {
1072 if (newName
== null || !/^[a-zA-Z0-9_]+$/.test(newName
) || uci
.get('network', newName
) != null)
1075 var oldNetwork
= uci
.get('network', oldName
);
1077 if (oldNetwork
== null || oldNetwork
['.type'] != 'interface')
1080 var sid
= uci
.add('network', 'interface', newName
);
1082 for (var key
in oldNetwork
)
1083 if (oldNetwork
.hasOwnProperty(key
) && key
.charAt(0) != '.')
1084 uci
.set('network', sid
, key
, oldNetwork
[key
]);
1086 uci
.sections('luci', 'ifstate', function(s
) {
1087 if (s
.interface == oldName
)
1088 uci
.set('luci', s
['.name'], 'interface', newName
);
1091 uci
.sections('network', 'alias', function(s
) {
1092 if (s
.interface == oldName
)
1093 uci
.set('network', s
['.name'], 'interface', newName
);
1096 uci
.sections('network', 'route', function(s
) {
1097 if (s
.interface == oldName
)
1098 uci
.set('network', s
['.name'], 'interface', newName
);
1101 uci
.sections('network', 'route6', function(s
) {
1102 if (s
.interface == oldName
)
1103 uci
.set('network', s
['.name'], 'interface', newName
);
1106 uci
.sections('wireless', 'wifi-iface', function(s
) {
1107 var networks
= L
.toArray(s
.network
).map(function(network
) { return (network
== oldName
? newName
: network
) });
1109 if (networks
.length
> 0)
1110 uci
.set('wireless', s
['.name'], 'network', networks
.join(' '));
1113 uci
.remove('network', oldName
);
1120 * Get a {@link LuCI.network.Device Device} instance describing the
1121 * given network device.
1123 * @param {string} name
1124 * The name of the network device to get, e.g. `eth0` or `br-lan`.
1126 * @returns {Promise<null|LuCI.network.Device>}
1127 * Returns a promise resolving to the `Device` instance describing
1128 * the network device or `null` if the given device name could not
1131 getDevice: function(name
) {
1132 return initNetworkState().then(L
.bind(function() {
1136 if (_state
.netdevs
.hasOwnProperty(name
))
1137 return this.instantiateDevice(name
);
1139 var netid
= getWifiNetidBySid(name
);
1141 return this.instantiateDevice(netid
[0]);
1148 * Get a sorted list of all found network devices.
1150 * @returns {Promise<Array<LuCI.network.Device>>}
1151 * Returns a promise resolving to a sorted array of `Device` class
1152 * instances describing the network devices found on the system.
1154 getDevices: function() {
1155 return initNetworkState().then(L
.bind(function() {
1158 /* find simple devices */
1159 var uciInterfaces
= uci
.sections('network', 'interface');
1160 for (var i
= 0; i
< uciInterfaces
.length
; i
++) {
1161 var ifnames
= L
.toArray(uciInterfaces
[i
].ifname
);
1163 for (var j
= 0; j
< ifnames
.length
; j
++) {
1164 if (ifnames
[j
].charAt(0) == '@')
1167 if (isIgnoredIfname(ifnames
[j
]) || isVirtualIfname(ifnames
[j
]) || isWifiIfname(ifnames
[j
]))
1170 devices
[ifnames
[j
]] = this.instantiateDevice(ifnames
[j
]);
1174 for (var ifname
in _state
.netdevs
) {
1175 if (devices
.hasOwnProperty(ifname
))
1178 if (isIgnoredIfname(ifname
) || isWifiIfname(ifname
))
1181 if (_state
.netdevs
[ifname
].wireless
)
1184 devices
[ifname
] = this.instantiateDevice(ifname
);
1187 /* find VLAN devices */
1188 var uciSwitchVLANs
= uci
.sections('network', 'switch_vlan');
1189 for (var i
= 0; i
< uciSwitchVLANs
.length
; i
++) {
1190 if (typeof(uciSwitchVLANs
[i
].ports
) != 'string' ||
1191 typeof(uciSwitchVLANs
[i
].device
) != 'string' ||
1192 !_state
.switches
.hasOwnProperty(uciSwitchVLANs
[i
].device
))
1195 var ports
= uciSwitchVLANs
[i
].ports
.split(/\s+/);
1196 for (var j
= 0; j
< ports
.length
; j
++) {
1197 var m
= ports
[j
].match(/^(\d+)([tu]?)$/);
1201 var netdev
= _state
.switches
[uciSwitchVLANs
[i
].device
].netdevs
[m
[1]];
1205 if (!devices
.hasOwnProperty(netdev
))
1206 devices
[netdev
] = this.instantiateDevice(netdev
);
1208 _state
.isSwitch
[netdev
] = true;
1213 var vid
= uciSwitchVLANs
[i
].vid
|| uciSwitchVLANs
[i
].vlan
;
1214 vid
= (vid
!= null ? +vid
: null);
1216 if (vid
== null || vid
< 0 || vid
> 4095)
1219 var vlandev
= '%s.%d'.format(netdev
, vid
);
1221 if (!devices
.hasOwnProperty(vlandev
))
1222 devices
[vlandev
] = this.instantiateDevice(vlandev
);
1224 _state
.isSwitch
[vlandev
] = true;
1228 /* find bridge VLAN devices */
1229 var uciBridgeVLANs
= uci
.sections('network', 'bridge-vlan');
1230 for (var i
= 0; i
< uciBridgeVLANs
.length
; i
++) {
1231 var basedev
= uciBridgeVLANs
[i
].device
,
1232 local
= uciBridgeVLANs
[i
].local
,
1233 alias
= uciBridgeVLANs
[i
].alias
,
1234 vid
= +uciBridgeVLANs
[i
].vlan
,
1235 ports
= L
.toArray(uciBridgeVLANs
[i
].ports
);
1240 if (isNaN(vid
) || vid
< 0 || vid
> 4095)
1243 var vlandev
= '%s.%s'.format(basedev
, alias
|| vid
);
1245 _state
.isBridge
[basedev
] = true;
1247 if (!_state
.bridges
.hasOwnProperty(basedev
))
1248 _state
.bridges
[basedev
] = {
1253 if (!devices
.hasOwnProperty(vlandev
))
1254 devices
[vlandev
] = this.instantiateDevice(vlandev
);
1256 ports
.forEach(function(port_name
) {
1257 var m
= port_name
.match(/^([^:]+)(?::[ut*]+)?$/),
1258 p
= m
? m
[1] : null;
1263 if (_state
.bridges
[basedev
].ifnames
.filter(function(sd
) { return sd
.name
== p
}).length
)
1266 _state
.netdevs
[p
] = _state
.netdevs
[p
] || {
1271 devtype
: 'ethernet',
1276 _state
.bridges
[basedev
].ifnames
.push(_state
.netdevs
[p
]);
1277 _state
.netdevs
[p
].bridge
= _state
.bridges
[basedev
];
1281 /* find wireless interfaces */
1282 var uciWifiIfaces
= uci
.sections('wireless', 'wifi-iface'),
1285 for (var i
= 0; i
< uciWifiIfaces
.length
; i
++) {
1286 if (typeof(uciWifiIfaces
[i
].device
) != 'string')
1289 networkCount
[uciWifiIfaces
[i
].device
] = (networkCount
[uciWifiIfaces
[i
].device
] || 0) + 1;
1291 var netid
= '%s.network%d'.format(uciWifiIfaces
[i
].device
, networkCount
[uciWifiIfaces
[i
].device
]);
1293 devices
[netid
] = this.instantiateDevice(netid
);
1296 /* find uci declared devices */
1297 var uciDevices
= uci
.sections('network', 'device');
1299 for (var i
= 0; i
< uciDevices
.length
; i
++) {
1300 var type
= uciDevices
[i
].type
,
1301 name
= uciDevices
[i
].name
;
1303 if (!type
|| !name
|| devices
.hasOwnProperty(name
))
1306 if (type
== 'bridge')
1307 _state
.isBridge
[name
] = true;
1309 devices
[name
] = this.instantiateDevice(name
);
1314 for (var netdev
in devices
)
1315 if (devices
.hasOwnProperty(netdev
))
1316 rv
.push(devices
[netdev
]);
1318 rv
.sort(deviceSort
);
1325 * Test if a given network device name is in the list of patterns for
1326 * device names to ignore.
1328 * Ignored device names are usually Linux network devices which are
1329 * spawned implicitly by kernel modules such as `tunl0` or `hwsim0`
1330 * and which are unsuitable for use in network configuration.
1332 * @param {string} name
1333 * The device name to test.
1335 * @returns {boolean}
1336 * Returns `true` if the given name is in the ignore pattern list,
1337 * else returns `false`.
1339 isIgnoredDevice: function(name
) {
1340 return isIgnoredIfname(name
);
1344 * Get a {@link LuCI.network.WifiDevice WifiDevice} instance describing
1345 * the given wireless radio.
1347 * @param {string} devname
1348 * The configuration name of the wireless radio to lookup, e.g. `radio0`
1349 * for the first mac80211 phy on the system.
1351 * @returns {Promise<null|LuCI.network.WifiDevice>}
1352 * Returns a promise resolving to the `WifiDevice` instance describing
1353 * the underlying radio device or `null` if the wireless radio could not
1356 getWifiDevice: function(devname
) {
1357 return initNetworkState().then(L
.bind(function() {
1358 var existingDevice
= uci
.get('wireless', devname
);
1360 if (existingDevice
== null || existingDevice
['.type'] != 'wifi-device')
1363 return this.instantiateWifiDevice(devname
, _state
.radios
[devname
] || {});
1368 * Obtain a list of all configured radio devices.
1370 * @returns {Promise<Array<LuCI.network.WifiDevice>>}
1371 * Returns a promise resolving to an array of `WifiDevice` instances
1372 * describing the wireless radios configured in the system.
1373 * The order of the array corresponds to the order of the radios in
1374 * the configuration.
1376 getWifiDevices: function() {
1377 return initNetworkState().then(L
.bind(function() {
1378 var uciWifiDevices
= uci
.sections('wireless', 'wifi-device'),
1381 for (var i
= 0; i
< uciWifiDevices
.length
; i
++) {
1382 var devname
= uciWifiDevices
[i
]['.name'];
1383 rv
.push(this.instantiateWifiDevice(devname
, _state
.radios
[devname
] || {}));
1391 * Get a {@link LuCI.network.WifiNetwork WifiNetwork} instance describing
1392 * the given wireless network.
1394 * @param {string} netname
1395 * The name of the wireless network to lookup. This may be either an uci
1396 * configuration section ID, a network ID in the form `radio#.network#`
1397 * or a Linux network device name like `wlan0` which is resolved to the
1398 * corresponding configuration section through `ubus` runtime information.
1400 * @returns {Promise<null|LuCI.network.WifiNetwork>}
1401 * Returns a promise resolving to the `WifiNetwork` instance describing
1402 * the wireless network or `null` if the corresponding network could not
1405 getWifiNetwork: function(netname
) {
1406 return initNetworkState()
1407 .then(L
.bind(this.lookupWifiNetwork
, this, netname
));
1411 * Get an array of all {@link LuCI.network.WifiNetwork WifiNetwork}
1412 * instances describing the wireless networks present on the system.
1414 * @returns {Promise<Array<LuCI.network.WifiNetwork>>}
1415 * Returns a promise resolving to an array of `WifiNetwork` instances
1416 * describing the wireless networks. The array will be empty if no networks
1419 getWifiNetworks: function() {
1420 return initNetworkState().then(L
.bind(function() {
1421 var wifiIfaces
= uci
.sections('wireless', 'wifi-iface'),
1424 for (var i
= 0; i
< wifiIfaces
.length
; i
++)
1425 rv
.push(this.lookupWifiNetwork(wifiIfaces
[i
]['.name']));
1427 rv
.sort(function(a
, b
) {
1428 return L
.naturalCompare(a
.getID(), b
.getID());
1436 * Adds a new wireless network to the configuration and sets its options
1437 * to the provided values.
1439 * @param {Object<string, string|string[]>} options
1440 * The options to set for the newly added wireless network. This object
1441 * must at least contain a `device` property which is set to the radio
1442 * name the new network belongs to.
1444 * @returns {Promise<null|LuCI.network.WifiNetwork>}
1445 * Returns a promise resolving to a `WifiNetwork` instance describing
1446 * the newly added wireless network or `null` if the given options
1447 * were invalid or if the associated radio device could not be found.
1449 addWifiNetwork: function(options
) {
1450 return initNetworkState().then(L
.bind(function() {
1451 if (options
== null ||
1452 typeof(options
) != 'object' ||
1453 typeof(options
.device
) != 'string')
1456 var existingDevice
= uci
.get('wireless', options
.device
);
1457 if (existingDevice
== null || existingDevice
['.type'] != 'wifi-device')
1460 /* XXX: need to add a named section (wifinet#) here */
1461 var sid
= uci
.add('wireless', 'wifi-iface');
1462 for (var key
in options
)
1463 if (options
.hasOwnProperty(key
))
1464 uci
.set('wireless', sid
, key
, options
[key
]);
1466 var radioname
= existingDevice
['.name'],
1467 netid
= getWifiNetidBySid(sid
) || [];
1469 return this.instantiateWifiNetwork(sid
, radioname
, _state
.radios
[radioname
], netid
[0], null);
1474 * Deletes the given wireless network from the configuration.
1476 * @param {string} netname
1477 * The name of the network to remove. This may be either a
1478 * network ID in the form `radio#.network#` or a Linux network device
1479 * name like `wlan0` which is resolved to the corresponding configuration
1480 * section through `ubus` runtime information.
1482 * @returns {Promise<boolean>}
1483 * Returns a promise resolving to `true` if the wireless network has been
1484 * successfully deleted from the configuration or `false` if it could not
1487 deleteWifiNetwork: function(netname
) {
1488 return initNetworkState().then(L
.bind(function() {
1489 var sid
= getWifiSidByIfname(netname
);
1494 uci
.remove('wireless', sid
);
1500 getStatusByRoute: function(addr
, mask
) {
1501 return initNetworkState().then(L
.bind(function() {
1504 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
1505 if (!Array
.isArray(_state
.ifaces
[i
].route
))
1508 for (var j
= 0; j
< _state
.ifaces
[i
].route
.length
; j
++) {
1509 if (typeof(_state
.ifaces
[i
].route
[j
]) != 'object' ||
1510 typeof(_state
.ifaces
[i
].route
[j
].target
) != 'string' ||
1511 typeof(_state
.ifaces
[i
].route
[j
].mask
) != 'number')
1514 if (_state
.ifaces
[i
].route
[j
].table
)
1517 if (_state
.ifaces
[i
].route
[j
].target
!= addr
||
1518 _state
.ifaces
[i
].route
[j
].mask
!= mask
)
1521 rv
.push(_state
.ifaces
[i
]);
1525 rv
.sort(function(a
, b
) {
1526 return L
.naturalCompare(a
.metric
, b
.metric
) || L
.naturalCompare(a
.interface, b
.interface);
1534 getStatusByAddress: function(addr
) {
1535 return initNetworkState().then(L
.bind(function() {
1538 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
1539 if (Array
.isArray(_state
.ifaces
[i
]['ipv4-address']))
1540 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv4-address'].length
; j
++)
1541 if (typeof(_state
.ifaces
[i
]['ipv4-address'][j
]) == 'object' &&
1542 _state
.ifaces
[i
]['ipv4-address'][j
].address
== addr
)
1543 return _state
.ifaces
[i
];
1545 if (Array
.isArray(_state
.ifaces
[i
]['ipv6-address']))
1546 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv6-address'].length
; j
++)
1547 if (typeof(_state
.ifaces
[i
]['ipv6-address'][j
]) == 'object' &&
1548 _state
.ifaces
[i
]['ipv6-address'][j
].address
== addr
)
1549 return _state
.ifaces
[i
];
1551 if (Array
.isArray(_state
.ifaces
[i
]['ipv6-prefix-assignment']))
1552 for (var j
= 0; j
< _state
.ifaces
[i
]['ipv6-prefix-assignment'].length
; j
++)
1553 if (typeof(_state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]) == 'object' &&
1554 typeof(_state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]['local-address']) == 'object' &&
1555 _state
.ifaces
[i
]['ipv6-prefix-assignment'][j
]['local-address'].address
== addr
)
1556 return _state
.ifaces
[i
];
1564 * Get IPv4 wan networks.
1566 * This function looks up all networks having a default `0.0.0.0/0` route
1567 * and returns them as array.
1569 * @returns {Promise<Array<LuCI.network.Protocol>>}
1570 * Returns a promise resolving to an array of `Protocol` subclass
1571 * instances describing the found default route interfaces.
1573 getWANNetworks: function() {
1574 return this.getStatusByRoute('0.0.0.0', 0).then(L
.bind(function(statuses
) {
1575 var rv
= [], seen
= {};
1577 for (var i
= 0; i
< statuses
.length
; i
++) {
1578 if (!seen
.hasOwnProperty(statuses
[i
].interface)) {
1579 rv
.push(this.instantiateNetwork(statuses
[i
].interface, statuses
[i
].proto
));
1580 seen
[statuses
[i
].interface] = true;
1589 * Get IPv6 wan networks.
1591 * This function looks up all networks having a default `::/0` route
1592 * and returns them as array.
1594 * @returns {Promise<Array<LuCI.network.Protocol>>}
1595 * Returns a promise resolving to an array of `Protocol` subclass
1596 * instances describing the found IPv6 default route interfaces.
1598 getWAN6Networks: function() {
1599 return this.getStatusByRoute('::', 0).then(L
.bind(function(statuses
) {
1600 var rv
= [], seen
= {};
1602 for (var i
= 0; i
< statuses
.length
; i
++) {
1603 if (!seen
.hasOwnProperty(statuses
[i
].interface)) {
1604 rv
.push(this.instantiateNetwork(statuses
[i
].interface, statuses
[i
].proto
));
1605 seen
[statuses
[i
].interface] = true;
1614 * Describes an swconfig switch topology by specifying the CPU
1615 * connections and external port labels of a switch.
1617 * @typedef {Object<string, Object|Array>} SwitchTopology
1618 * @memberof LuCI.network
1620 * @property {Object<number, string>} netdevs
1621 * The `netdevs` property points to an object describing the CPU port
1622 * connections of the switch. The numeric key of the enclosed object is
1623 * the port number, the value contains the Linux network device name the
1624 * port is hardwired to.
1626 * @property {Array<Object<string, boolean|number|string>>} ports
1627 * The `ports` property points to an array describing the populated
1628 * ports of the switch in the external label order. Each array item is
1629 * an object containg the following keys:
1630 * - `num` - the internal switch port number
1631 * - `label` - the label of the port, e.g. `LAN 1` or `CPU (eth0)`
1632 * - `device` - the connected Linux network device name (CPU ports only)
1633 * - `tagged` - a boolean indicating whether the port must be tagged to
1634 * function (CPU ports only)
1638 * Returns the topologies of all swconfig switches found on the system.
1640 * @returns {Promise<Object<string, LuCI.network.SwitchTopology>>}
1641 * Returns a promise resolving to an object containing the topologies
1642 * of each switch. The object keys correspond to the name of the switches
1643 * such as `switch0`, the values are
1644 * {@link LuCI.network.SwitchTopology SwitchTopology} objects describing
1647 getSwitchTopologies: function() {
1648 return initNetworkState().then(function() {
1649 return _state
.switches
;
1654 instantiateNetwork: function(name
, proto
) {
1658 proto
= (proto
== null ? uci
.get('network', name
, 'proto') : proto
);
1660 var protoClass
= _protocols
[proto
] || Protocol
;
1661 return new protoClass(name
);
1665 instantiateDevice: function(name
, network
, extend
) {
1667 return new (Device
.extend(extend
))(name
, network
);
1669 return new Device(name
, network
);
1673 instantiateWifiDevice: function(radioname
, radiostate
) {
1674 return new WifiDevice(radioname
, radiostate
);
1678 instantiateWifiNetwork: function(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
) {
1679 return new WifiNetwork(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
);
1683 lookupWifiNetwork: function(netname
) {
1684 var sid
, res
, netid
, radioname
, radiostate
, netstate
;
1686 sid
= getWifiSidByNetid(netname
);
1689 res
= getWifiStateBySid(sid
);
1691 radioname
= res
? res
[0] : null;
1692 radiostate
= res
? res
[1] : null;
1693 netstate
= res
? res
[2] : null;
1696 res
= getWifiStateByIfname(netname
);
1700 radiostate
= res
[1];
1702 sid
= netstate
.section
;
1703 netid
= L
.toArray(getWifiNetidBySid(sid
))[0];
1706 res
= getWifiStateBySid(netname
);
1710 radiostate
= res
[1];
1713 netid
= L
.toArray(getWifiNetidBySid(sid
))[0];
1716 res
= getWifiNetidBySid(netname
);
1727 return this.instantiateWifiNetwork(sid
|| netname
, radioname
,
1728 radiostate
, netid
, netstate
,
1729 netstate
? _state
.hostapd
[netstate
.ifname
] : null);
1733 * Obtains the the network device name of the given object.
1735 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} obj
1736 * The object to get the device name from.
1738 * @returns {null|string}
1739 * Returns a string containing the device name or `null` if the given
1740 * object could not be converted to a name.
1742 getIfnameOf: function(obj
) {
1743 return ifnameOf(obj
);
1747 * Queries the internal DSL modem type from board information.
1749 * @returns {Promise<null|string>}
1750 * Returns a promise resolving to the type of the internal modem
1751 * (e.g. `vdsl`) or to `null` if no internal modem is present.
1753 getDSLModemType: function() {
1754 return initNetworkState().then(function() {
1755 return _state
.hasDSLModem
? _state
.hasDSLModem
.type
: null;
1760 * Queries aggregated information about known hosts.
1762 * This function aggregates information from various sources such as
1763 * DHCP lease databases, ARP and IPv6 neighbour entries, wireless
1764 * association list etc. and returns a {@link LuCI.network.Hosts Hosts}
1765 * class instance describing the found hosts.
1767 * @returns {Promise<LuCI.network.Hosts>}
1768 * Returns a `Hosts` instance describing host known on the system.
1770 getHostHints: function() {
1771 return initNetworkState().then(function() {
1772 return new Hosts(_state
.hosts
);
1779 * @memberof LuCI.network
1783 * The `LuCI.network.Hosts` class encapsulates host information aggregated
1784 * from multiple sources and provides convenience functions to access the
1785 * host information by different criteria.
1787 Hosts
= baseclass
.extend(/** @lends LuCI.network.Hosts.prototype */ {
1788 __init__: function(hosts
) {
1793 * Lookup the hostname associated with the given MAC address.
1795 * @param {string} mac
1796 * The MAC address to lookup.
1798 * @returns {null|string}
1799 * Returns the hostname associated with the given MAC or `null` if
1800 * no matching host could be found or if no hostname is known for
1801 * the corresponding host.
1803 getHostnameByMACAddr: function(mac
) {
1804 return this.hosts
[mac
]
1805 ? (this.hosts
[mac
].name
|| null)
1810 * Lookup the IPv4 address associated with the given MAC address.
1812 * @param {string} mac
1813 * The MAC address to lookup.
1815 * @returns {null|string}
1816 * Returns the IPv4 address associated with the given MAC or `null` if
1817 * no matching host could be found or if no IPv4 address is known for
1818 * the corresponding host.
1820 getIPAddrByMACAddr: function(mac
) {
1821 return this.hosts
[mac
]
1822 ? (L
.toArray(this.hosts
[mac
].ipaddrs
|| this.hosts
[mac
].ipv4
)[0] || null)
1827 * Lookup the IPv6 address associated with the given MAC address.
1829 * @param {string} mac
1830 * The MAC address to lookup.
1832 * @returns {null|string}
1833 * Returns the IPv6 address associated with the given MAC or `null` if
1834 * no matching host could be found or if no IPv6 address is known for
1835 * the corresponding host.
1837 getIP6AddrByMACAddr: function(mac
) {
1838 return this.hosts
[mac
]
1839 ? (L
.toArray(this.hosts
[mac
].ip6addrs
|| this.hosts
[mac
].ipv6
)[0] || null)
1844 * Lookup the hostname associated with the given IPv4 address.
1846 * @param {string} ipaddr
1847 * The IPv4 address to lookup.
1849 * @returns {null|string}
1850 * Returns the hostname associated with the given IPv4 or `null` if
1851 * no matching host could be found or if no hostname is known for
1852 * the corresponding host.
1854 getHostnameByIPAddr: function(ipaddr
) {
1855 for (var mac
in this.hosts
) {
1856 if (this.hosts
[mac
].name
== null)
1859 var addrs
= L
.toArray(this.hosts
[mac
].ipaddrs
|| this.hosts
[mac
].ipv4
);
1861 for (var i
= 0; i
< addrs
.length
; i
++)
1862 if (addrs
[i
] == ipaddr
)
1863 return this.hosts
[mac
].name
;
1870 * Lookup the MAC address associated with the given IPv4 address.
1872 * @param {string} ipaddr
1873 * The IPv4 address to lookup.
1875 * @returns {null|string}
1876 * Returns the MAC address associated with the given IPv4 or `null` if
1877 * no matching host could be found or if no MAC address is known for
1878 * the corresponding host.
1880 getMACAddrByIPAddr: function(ipaddr
) {
1881 for (var mac
in this.hosts
) {
1882 var addrs
= L
.toArray(this.hosts
[mac
].ipaddrs
|| this.hosts
[mac
].ipv4
);
1884 for (var i
= 0; i
< addrs
.length
; i
++)
1885 if (addrs
[i
] == ipaddr
)
1893 * Lookup the hostname associated with the given IPv6 address.
1895 * @param {string} ip6addr
1896 * The IPv6 address to lookup.
1898 * @returns {null|string}
1899 * Returns the hostname associated with the given IPv6 or `null` if
1900 * no matching host could be found or if no hostname is known for
1901 * the corresponding host.
1903 getHostnameByIP6Addr: function(ip6addr
) {
1904 for (var mac
in this.hosts
) {
1905 if (this.hosts
[mac
].name
== null)
1908 var addrs
= L
.toArray(this.hosts
[mac
].ip6addrs
|| this.hosts
[mac
].ipv6
);
1910 for (var i
= 0; i
< addrs
.length
; i
++)
1911 if (addrs
[i
] == ip6addr
)
1912 return this.hosts
[mac
].name
;
1919 * Lookup the MAC address associated with the given IPv6 address.
1921 * @param {string} ip6addr
1922 * The IPv6 address to lookup.
1924 * @returns {null|string}
1925 * Returns the MAC address associated with the given IPv6 or `null` if
1926 * no matching host could be found or if no MAC address is known for
1927 * the corresponding host.
1929 getMACAddrByIP6Addr: function(ip6addr
) {
1930 for (var mac
in this.hosts
) {
1931 var addrs
= L
.toArray(this.hosts
[mac
].ip6addrs
|| this.hosts
[mac
].ipv6
);
1933 for (var i
= 0; i
< addrs
.length
; i
++)
1934 if (addrs
[i
] == ip6addr
)
1942 * Return an array of (MAC address, name hint) tuples sorted by
1945 * @param {boolean} [preferIp6=false]
1946 * Whether to prefer IPv6 addresses (`true`) or IPv4 addresses (`false`)
1947 * as name hint when no hostname is known for a specific MAC address.
1949 * @returns {Array<Array<string>>}
1950 * Returns an array of arrays containing a name hint for each found
1951 * MAC address on the system. The array is sorted ascending by MAC.
1953 * Each item of the resulting array is a two element array with the
1954 * MAC being the first element and the name hint being the second
1955 * element. The name hint is either the hostname, an IPv4 or an IPv6
1956 * address related to the MAC address.
1958 * If no hostname but both IPv4 and IPv6 addresses are known, the
1959 * `preferIP6` flag specifies whether the IPv6 or the IPv4 address
1962 getMACHints: function(preferIp6
) {
1965 for (var mac
in this.hosts
) {
1966 var hint
= this.hosts
[mac
].name
||
1967 L
.toArray(this.hosts
[mac
][preferIp6
? 'ip6addrs' : 'ipaddrs'] || this.hosts
[mac
][preferIp6
? 'ipv6' : 'ipv4'])[0] ||
1968 L
.toArray(this.hosts
[mac
][preferIp6
? 'ipaddrs' : 'ip6addrs'] || this.hosts
[mac
][preferIp6
? 'ipv4' : 'ipv6'])[0];
1970 rv
.push([mac
, hint
]);
1973 return rv
.sort(function(a
, b
) {
1974 return L
.naturalCompare(a
[0], b
[0]);
1981 * @memberof LuCI.network
1985 * The `Network.Protocol` class serves as base for protocol specific
1986 * subclasses which describe logical UCI networks defined by `config
1987 * interface` sections in `/etc/config/network`.
1989 Protocol
= baseclass
.extend(/** @lends LuCI.network.Protocol.prototype */ {
1990 __init__: function(name
) {
1994 _get: function(opt
) {
1995 var val
= uci
.get('network', this.sid
, opt
);
1997 if (Array
.isArray(val
))
1998 return val
.join(' ');
2003 _ubus: function(field
) {
2004 for (var i
= 0; i
< _state
.ifaces
.length
; i
++) {
2005 if (_state
.ifaces
[i
].interface != this.sid
)
2008 return (field
!= null ? _state
.ifaces
[i
][field
] : _state
.ifaces
[i
]);
2013 * Read the given UCI option value of this network.
2015 * @param {string} opt
2016 * The UCI option name to read.
2018 * @returns {null|string|string[]}
2019 * Returns the UCI option value or `null` if the requested option is
2022 get: function(opt
) {
2023 return uci
.get('network', this.sid
, opt
);
2027 * Set the given UCI option of this network to the given value.
2029 * @param {string} opt
2030 * The name of the UCI option to set.
2032 * @param {null|string|string[]} val
2033 * The value to set or `null` to remove the given option from the
2036 set: function(opt
, val
) {
2037 return uci
.set('network', this.sid
, opt
, val
);
2041 * Get the associared Linux network device of this network.
2043 * @returns {null|string}
2044 * Returns the name of the associated network device or `null` if
2045 * it could not be determined.
2047 getIfname: function() {
2050 if (this.isFloating())
2051 ifname
= this._ubus('l3_device');
2053 ifname
= this._ubus('device') || this._ubus('l3_device');
2058 var res
= getWifiNetidByNetname(this.sid
);
2059 return (res
!= null ? res
[0] : null);
2063 * Get the name of this network protocol class.
2065 * This function will be overwritten by subclasses created by
2066 * {@link LuCI.network#registerProtocol Network.registerProtocol()}.
2070 * Returns the name of the network protocol implementation, e.g.
2071 * `static` or `dhcp`.
2073 getProtocol: function() {
2078 * Return a human readable description for the protcol, such as
2079 * `Static address` or `DHCP client`.
2081 * This function should be overwritten by subclasses.
2085 * Returns the description string.
2087 getI18n: function() {
2088 switch (this.getProtocol()) {
2089 case 'none': return _('Unmanaged');
2090 case 'static': return _('Static address');
2091 case 'dhcp': return _('DHCP client');
2092 default: return _('Unknown');
2097 * Get the type of the underlying interface.
2099 * This function actually is a convenience wrapper around
2100 * `proto.get("type")` and is mainly used by other `LuCI.network` code
2101 * to check whether the interface is declared as bridge in UCI.
2103 * @returns {null|string}
2104 * Returns the value of the `type` option of the associated logical
2105 * interface or `null` if no `type` option is set.
2107 getType: function() {
2108 return this._get('type');
2112 * Get the name of the associated logical interface.
2115 * Returns the logical interface name, such as `lan` or `wan`.
2117 getName: function() {
2122 * Get the uptime of the logical interface.
2125 * Returns the uptime of the associated interface in seconds.
2127 getUptime: function() {
2128 return this._ubus('uptime') || 0;
2132 * Get the logical interface expiry time in seconds.
2134 * For protocols that have a concept of a lease, such as DHCP or
2135 * DHCPv6, this function returns the remaining time in seconds
2136 * until the lease expires.
2139 * Returns the amount of seconds until the lease expires or `-1`
2140 * if it isn't applicable to the associated protocol.
2142 getExpiry: function() {
2143 var u
= this._ubus('uptime'),
2144 d
= this._ubus('data');
2146 if (typeof(u
) == 'number' && d
!= null &&
2147 typeof(d
) == 'object' && typeof(d
.leasetime
) == 'number') {
2148 var r
= d
.leasetime
- (u
% d
.leasetime
);
2149 return (r
> 0 ? r
: 0);
2156 * Get the metric value of the logical interface.
2159 * Returns the current metric value used for device and network
2160 * routes spawned by the associated logical interface.
2162 getMetric: function() {
2163 return this._ubus('metric') || 0;
2167 * Get the requested firewall zone name of the logical interface.
2169 * Some protocol implementations request a specific firewall zone
2170 * to trigger inclusion of their resulting network devices into the
2171 * firewall rule set.
2173 * @returns {null|string}
2174 * Returns the requested firewall zone name as published in the
2175 * `ubus` runtime information or `null` if the remote protocol
2176 * handler didn't request a zone.
2178 getZoneName: function() {
2179 var d
= this._ubus('data');
2181 if (L
.isObject(d
) && typeof(d
.zone
) == 'string')
2188 * Query the first (primary) IPv4 address of the logical interface.
2190 * @returns {null|string}
2191 * Returns the primary IPv4 address registered by the protocol handler
2192 * or `null` if no IPv4 addresses were set.
2194 getIPAddr: function() {
2195 var addrs
= this._ubus('ipv4-address');
2196 return ((Array
.isArray(addrs
) && addrs
.length
) ? addrs
[0].address
: null);
2200 * Query all IPv4 addresses of the logical interface.
2202 * @returns {string[]}
2203 * Returns an array of IPv4 addresses in CIDR notation which have been
2204 * registered by the protocol handler. The order of the resulting array
2205 * follows the order of the addresses in `ubus` runtime information.
2207 getIPAddrs: function() {
2208 var addrs
= this._ubus('ipv4-address'),
2211 if (Array
.isArray(addrs
))
2212 for (var i
= 0; i
< addrs
.length
; i
++)
2213 rv
.push('%s/%d'.format(addrs
[i
].address
, addrs
[i
].mask
));
2219 * Query the first (primary) IPv4 netmask of the logical interface.
2221 * @returns {null|string}
2222 * Returns the netmask of the primary IPv4 address registered by the
2223 * protocol handler or `null` if no IPv4 addresses were set.
2225 getNetmask: function() {
2226 var addrs
= this._ubus('ipv4-address');
2227 if (Array
.isArray(addrs
) && addrs
.length
)
2228 return prefixToMask(addrs
[0].mask
, false);
2232 * Query the gateway (nexthop) of the default route associated with
2233 * this logical interface.
2236 * Returns a string containing the IPv4 nexthop address of the associated
2237 * default route or `null` if no default route was found.
2239 getGatewayAddr: function() {
2240 var routes
= this._ubus('route');
2242 if (Array
.isArray(routes
))
2243 for (var i
= 0; i
< routes
.length
; i
++)
2244 if (typeof(routes
[i
]) == 'object' &&
2245 routes
[i
].target
== '0.0.0.0' &&
2246 routes
[i
].mask
== 0)
2247 return routes
[i
].nexthop
;
2253 * Query the IPv4 DNS servers associated with the logical interface.
2255 * @returns {string[]}
2256 * Returns an array of IPv4 DNS servers registered by the remote
2259 getDNSAddrs: function() {
2260 var addrs
= this._ubus('dns-server'),
2263 if (Array
.isArray(addrs
))
2264 for (var i
= 0; i
< addrs
.length
; i
++)
2265 if (!/:/.test(addrs
[i
]))
2272 * Query the first (primary) IPv6 address of the logical interface.
2274 * @returns {null|string}
2275 * Returns the primary IPv6 address registered by the protocol handler
2276 * in CIDR notation or `null` if no IPv6 addresses were set.
2278 getIP6Addr: function() {
2279 var addrs
= this._ubus('ipv6-address');
2281 if (Array
.isArray(addrs
) && L
.isObject(addrs
[0]))
2282 return '%s/%d'.format(addrs
[0].address
, addrs
[0].mask
);
2284 addrs
= this._ubus('ipv6-prefix-assignment');
2286 if (Array
.isArray(addrs
) && L
.isObject(addrs
[0]) && L
.isObject(addrs
[0]['local-address']))
2287 return '%s/%d'.format(addrs
[0]['local-address'].address
, addrs
[0]['local-address'].mask
);
2293 * Query all IPv6 addresses of the logical interface.
2295 * @returns {string[]}
2296 * Returns an array of IPv6 addresses in CIDR notation which have been
2297 * registered by the protocol handler. The order of the resulting array
2298 * follows the order of the addresses in `ubus` runtime information.
2300 getIP6Addrs: function() {
2301 var addrs
= this._ubus('ipv6-address'),
2304 if (Array
.isArray(addrs
))
2305 for (var i
= 0; i
< addrs
.length
; i
++)
2306 if (L
.isObject(addrs
[i
]))
2307 rv
.push('%s/%d'.format(addrs
[i
].address
, addrs
[i
].mask
));
2309 addrs
= this._ubus('ipv6-prefix-assignment');
2311 if (Array
.isArray(addrs
))
2312 for (var i
= 0; i
< addrs
.length
; i
++)
2313 if (L
.isObject(addrs
[i
]) && L
.isObject(addrs
[i
]['local-address']))
2314 rv
.push('%s/%d'.format(addrs
[i
]['local-address'].address
, addrs
[i
]['local-address'].mask
));
2320 * Query the gateway (nexthop) of the IPv6 default route associated with
2321 * this logical interface.
2324 * Returns a string containing the IPv6 nexthop address of the associated
2325 * default route or `null` if no default route was found.
2327 getGateway6Addr: function() {
2328 var routes
= this._ubus('route');
2330 if (Array
.isArray(routes
))
2331 for (var i
= 0; i
< routes
.length
; i
++)
2332 if (typeof(routes
[i
]) == 'object' &&
2333 routes
[i
].target
== '::' &&
2334 routes
[i
].mask
== 0)
2335 return routes
[i
].nexthop
;
2341 * Query the IPv6 DNS servers associated with the logical interface.
2343 * @returns {string[]}
2344 * Returns an array of IPv6 DNS servers registered by the remote
2347 getDNS6Addrs: function() {
2348 var addrs
= this._ubus('dns-server'),
2351 if (Array
.isArray(addrs
))
2352 for (var i
= 0; i
< addrs
.length
; i
++)
2353 if (/:/.test(addrs
[i
]))
2360 * Query the routed IPv6 prefix associated with the logical interface.
2362 * @returns {null|string}
2363 * Returns the routed IPv6 prefix registered by the remote protocol
2364 * handler or `null` if no prefix is present.
2366 getIP6Prefix: function() {
2367 var prefixes
= this._ubus('ipv6-prefix');
2369 if (Array
.isArray(prefixes
) && L
.isObject(prefixes
[0]))
2370 return '%s/%d'.format(prefixes
[0].address
, prefixes
[0].mask
);
2376 * Query interface error messages published in `ubus` runtime state.
2378 * Interface errors are emitted by remote protocol handlers if the setup
2379 * of the underlying logical interface failed, e.g. due to bad
2380 * configuration or network connectivity issues.
2382 * This function will translate the found error codes to human readable
2383 * messages using the descriptions registered by
2384 * {@link LuCI.network#registerErrorCode Network.registerErrorCode()}
2385 * and fall back to `"Unknown error (%s)"` where `%s` is replaced by the
2386 * error code in case no translation can be found.
2388 * @returns {string[]}
2389 * Returns an array of translated interface error messages.
2391 getErrors: function() {
2392 var errors
= this._ubus('errors'),
2395 if (Array
.isArray(errors
)) {
2396 for (var i
= 0; i
< errors
.length
; i
++) {
2397 if (!L
.isObject(errors
[i
]) || typeof(errors
[i
].code
) != 'string')
2401 rv
.push(proto_errors
[errors
[i
].code
] || _('Unknown error (%s)').format(errors
[i
].code
));
2409 * Checks whether the underlying logical interface is declared as bridge.
2411 * @returns {boolean}
2412 * Returns `true` when the interface is declared with `option type bridge`
2413 * and when the associated protocol implementation is not marked virtual
2414 * or `false` when the logical interface is no bridge.
2416 isBridge: function() {
2417 return (!this.isVirtual() && this.getType() == 'bridge');
2421 * Get the name of the opkg package providing the protocol functionality.
2423 * This function should be overwritten by protocol specific subclasses.
2428 * Returns the name of the opkg package required for the protocol to
2429 * function, e.g. `odhcp6c` for the `dhcpv6` prototocol.
2431 getOpkgPackage: function() {
2436 * Check function for the protocol handler if a new interface is createable.
2438 * This function should be overwritten by protocol specific subclasses.
2442 * @param {string} ifname
2443 * The name of the interface to be created.
2445 * @returns {Promise<void>}
2446 * Returns a promise resolving if new interface is createable, else
2447 * rejects with an error message string.
2449 isCreateable: function(ifname
) {
2450 return Promise
.resolve(null);
2454 * Checks whether the protocol functionality is installed.
2456 * This function exists for compatibility with old code, it always
2462 * @returns {boolean}
2463 * Returns `true` if the protocol support is installed, else `false`.
2465 isInstalled: function() {
2470 * Checks whether this protocol is "virtual".
2472 * A "virtual" protocol is a protocol which spawns its own interfaces
2473 * on demand instead of using existing physical interfaces.
2475 * Examples for virtual protocols are `6in4` which `gre` spawn tunnel
2476 * network device on startup, examples for non-virtual protcols are
2477 * `dhcp` or `static` which apply IP configuration to existing interfaces.
2479 * This function should be overwritten by subclasses.
2481 * @returns {boolean}
2482 * Returns a boolean indicating whether the underlying protocol spawns
2483 * dynamic interfaces (`true`) or not (`false`).
2485 isVirtual: function() {
2490 * Checks whether this protocol is "floating".
2492 * A "floating" protocol is a protocol which spawns its own interfaces
2493 * on demand, like a virtual one but which relies on an existinf lower
2494 * level interface to initiate the connection.
2496 * An example for such a protocol is "pppoe".
2498 * This function exists for backwards compatibility with older code
2499 * but should not be used anymore.
2502 * @returns {boolean}
2503 * Returns a boolean indicating whether this protocol is floating (`true`)
2506 isFloating: function() {
2511 * Checks whether this logical interface is dynamic.
2513 * A dynamic interface is an interface which has been created at runtime,
2514 * e.g. as sub-interface of another interface, but which is not backed by
2515 * any user configuration. Such dynamic interfaces cannot be edited but
2516 * only brought down or restarted.
2518 * @returns {boolean}
2519 * Returns a boolean indicating whether this interface is dynamic (`true`)
2522 isDynamic: function() {
2523 return (this._ubus('dynamic') == true);
2527 * Checks whether this interface is an alias interface.
2529 * Alias interfaces are interfaces layering on top of another interface
2530 * and are denoted by a special `@interfacename` notation in the
2531 * underlying `device` option.
2533 * @returns {null|string}
2534 * Returns the name of the parent interface if this logical interface
2535 * is an alias or `null` if it is not an alias interface.
2537 isAlias: function() {
2538 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'device')),
2541 for (var i
= 0; i
< ifnames
.length
; i
++)
2542 if (ifnames
[i
].charAt(0) == '@')
2543 parent
= ifnames
[i
].substr(1);
2544 else if (parent
!= null)
2551 * Checks whether this logical interface is "empty", meaning that ut
2552 * has no network devices attached.
2554 * @returns {boolean}
2555 * Returns `true` if this logical interface is empty, else `false`.
2557 isEmpty: function() {
2558 if (this.isFloating())
2562 device
= this._get('device');
2564 if (device
!= null && device
.match(/\S+/))
2567 if (empty
== true && getWifiNetidBySid(this.sid
) != null)
2574 * Checks whether this logical interface is configured and running.
2576 * @returns {boolean}
2577 * Returns `true` when the interface is active or `false` when it is not.
2580 return (this._ubus('up') == true);
2584 * Add the given network device to the logical interface.
2586 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2587 * The object or device name to add to the logical interface. In case the
2588 * given argument is not a string, it is resolved though the
2589 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2591 * @returns {boolean}
2592 * Returns `true` if the device name has been added or `false` if any
2593 * argument was invalid, if the device was already part of the logical
2594 * interface or if the logical interface is virtual.
2596 addDevice: function(device
) {
2597 device
= ifnameOf(device
);
2599 if (device
== null || this.isFloating())
2602 var wif
= getWifiSidByIfname(device
);
2605 return appendValue('wireless', wif
, 'network', this.sid
);
2607 return appendValue('network', this.sid
, 'device', device
);
2611 * Remove the given network device from the logical interface.
2613 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2614 * The object or device name to remove from the logical interface. In case
2615 * the given argument is not a string, it is resolved though the
2616 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2618 * @returns {boolean}
2619 * Returns `true` if the device name has been added or `false` if any
2620 * argument was invalid, if the device was already part of the logical
2621 * interface or if the logical interface is virtual.
2623 deleteDevice: function(device
) {
2626 device
= ifnameOf(device
);
2628 if (device
== null || this.isFloating())
2631 var wif
= getWifiSidByIfname(device
);
2634 rv
= removeValue('wireless', wif
, 'network', this.sid
);
2636 if (removeValue('network', this.sid
, 'device', device
))
2643 * Returns the Linux network device associated with this logical
2646 * @returns {LuCI.network.Device}
2647 * Returns a `Network.Device` class instance representing the
2648 * expected Linux network device according to the configuration.
2650 getDevice: function() {
2651 if (this.isVirtual()) {
2652 var ifname
= '%s-%s'.format(this.getProtocol(), this.sid
);
2653 _state
.isTunnel
[this.getProtocol() + '-' + this.sid
] = true;
2654 return Network
.prototype.instantiateDevice(ifname
, this);
2656 else if (this.isBridge()) {
2657 var ifname
= 'br-%s'.format(this.sid
);
2658 _state
.isBridge
[ifname
] = true;
2659 return new Device(ifname
, this);
2662 var ifnames
= L
.toArray(uci
.get('network', this.sid
, 'device'));
2664 for (var i
= 0; i
< ifnames
.length
; i
++) {
2665 var m
= ifnames
[i
].match(/^([^:/]+)/);
2666 return ((m
&& m
[1]) ? Network
.prototype.instantiateDevice(m
[1], this) : null);
2669 ifname
= getWifiNetidByNetname(this.sid
);
2671 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
[0], this) : null);
2676 * Returns the layer 2 linux network device currently associated
2677 * with this logical interface.
2679 * @returns {LuCI.network.Device}
2680 * Returns a `Network.Device` class instance representing the Linux
2681 * network device currently associated with the logical interface.
2683 getL2Device: function() {
2684 var ifname
= this._ubus('device');
2685 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
, this) : null);
2689 * Returns the layer 3 linux network device currently associated
2690 * with this logical interface.
2692 * @returns {LuCI.network.Device}
2693 * Returns a `Network.Device` class instance representing the Linux
2694 * network device currently associated with the logical interface.
2696 getL3Device: function() {
2697 var ifname
= this._ubus('l3_device');
2698 return (ifname
!= null ? Network
.prototype.instantiateDevice(ifname
, this) : null);
2702 * Returns a list of network sub-devices associated with this logical
2705 * @returns {null|Array<LuCI.network.Device>}
2706 * Returns an array of of `Network.Device` class instances representing
2707 * the sub-devices attached to this logical interface or `null` if the
2708 * logical interface does not support sub-devices, e.g. because it is
2709 * virtual and not a bridge.
2711 getDevices: function() {
2714 if (!this.isBridge() && !(this.isVirtual() && !this.isFloating()))
2717 var device
= uci
.get('network', this.sid
, 'device');
2719 if (device
&& device
.charAt(0) != '@') {
2720 var m
= device
.match(/^([^:/]+)/);
2722 rv
.push(Network
.prototype.instantiateDevice(m
[1], this));
2725 var uciWifiIfaces
= uci
.sections('wireless', 'wifi-iface');
2727 for (var i
= 0; i
< uciWifiIfaces
.length
; i
++) {
2728 if (typeof(uciWifiIfaces
[i
].device
) != 'string')
2731 var networks
= L
.toArray(uciWifiIfaces
[i
].network
);
2733 for (var j
= 0; j
< networks
.length
; j
++) {
2734 if (networks
[j
] != this.sid
)
2737 var netid
= getWifiNetidBySid(uciWifiIfaces
[i
]['.name']);
2740 rv
.push(Network
.prototype.instantiateDevice(netid
[0], this));
2744 rv
.sort(deviceSort
);
2750 * Checks whether this logical interface contains the given device
2753 * @param {LuCI.network.Protocol|LuCI.network.Device|LuCI.network.WifiDevice|LuCI.network.WifiNetwork|string} device
2754 * The object or device name to check. In case the given argument is not
2755 * a string, it is resolved though the
2756 * {@link LuCI.network#getIfnameOf Network.getIfnameOf()} function.
2758 * @returns {boolean}
2759 * Returns `true` when this logical interface contains the given network
2760 * device or `false` if not.
2762 containsDevice: function(device
) {
2763 device
= ifnameOf(device
);
2767 else if (this.isVirtual() && '%s-%s'.format(this.getProtocol(), this.sid
) == device
)
2769 else if (this.isBridge() && 'br-%s'.format(this.sid
) == device
)
2772 var name
= uci
.get('network', this.sid
, 'device');
2774 var m
= name
.match(/^([^:/]+)/);
2775 if (m
!= null && m
[1] == device
)
2779 var wif
= getWifiSidByIfname(device
);
2782 var networks
= L
.toArray(uci
.get('wireless', wif
, 'network'));
2784 for (var i
= 0; i
< networks
.length
; i
++)
2785 if (networks
[i
] == this.sid
)
2793 * Cleanup related configuration entries.
2795 * This function will be invoked if an interface is about to be removed
2796 * from the configuration and is responsible for performing any required
2797 * cleanup tasks, such as unsetting uci entries in related configurations.
2799 * It should be overwritten by protocol specific subclasses.
2803 * @returns {*|Promise<*>}
2804 * This function may return a promise which is awaited before the rest of
2805 * the configuration is removed. Any non-promise return value and any
2806 * resolved promise value is ignored. If the returned promise is rejected,
2807 * the interface removal will be aborted.
2809 deleteConfiguration: function() {}
2814 * @memberof LuCI.network
2818 * A `Network.Device` class instance represents an underlying Linux network
2819 * device and allows querying device details such as packet statistics or MTU.
2821 Device
= baseclass
.extend(/** @lends LuCI.network.Device.prototype */ {
2822 __init__: function(device
, network
) {
2823 var wif
= getWifiSidByIfname(device
);
2826 var res
= getWifiStateBySid(wif
) || [],
2827 netid
= getWifiNetidBySid(wif
) || [];
2829 this.wif
= new WifiNetwork(wif
, res
[0], res
[1], netid
[0], res
[2], { ifname
: device
});
2830 this.device
= this.wif
.getIfname();
2833 this.device
= this.device
|| device
;
2834 this.dev
= Object
.assign({}, _state
.netdevs
[this.device
]);
2835 this.network
= network
;
2839 uci
.sections('network', 'device', function(s
) {
2840 if (s
.name
== device
)
2844 this.config
= Object
.assign({}, conf
);
2847 _devstate: function(/* ... */) {
2850 for (var i
= 0; i
< arguments
.length
; i
++)
2852 rv
= rv
[arguments
[i
]];
2860 * Get the name of the network device.
2863 * Returns the name of the device, e.g. `eth0` or `wlan0`.
2865 getName: function() {
2866 return (this.wif
!= null ? this.wif
.getIfname() : this.device
);
2870 * Get the MAC address of the device.
2872 * @returns {null|string}
2873 * Returns the MAC address of the device or `null` if not applicable,
2874 * e.g. for non-ethernet tunnel devices.
2876 getMAC: function() {
2877 var mac
= this._devstate('macaddr');
2878 return mac
? mac
.toUpperCase() : null;
2882 * Get the MTU of the device.
2885 * Returns the MTU of the device.
2887 getMTU: function() {
2888 return this._devstate('mtu');
2892 * Get the IPv4 addresses configured on the device.
2894 * @returns {string[]}
2895 * Returns an array of IPv4 address strings.
2897 getIPAddrs: function() {
2898 var addrs
= this._devstate('ipaddrs');
2899 return (Array
.isArray(addrs
) ? addrs
: []);
2903 * Get the IPv6 addresses configured on the device.
2905 * @returns {string[]}
2906 * Returns an array of IPv6 address strings.
2908 getIP6Addrs: function() {
2909 var addrs
= this._devstate('ip6addrs');
2910 return (Array
.isArray(addrs
) ? addrs
: []);
2914 * Get the type of the device.
2917 * Returns a string describing the type of the network device:
2918 * - `alias` if it is an abstract alias device (`@` notation)
2919 * - `wifi` if it is a wireless interface (e.g. `wlan0`)
2920 * - `bridge` if it is a bridge device (e.g. `br-lan`)
2921 * - `tunnel` if it is a tun or tap device (e.g. `tun0`)
2922 * - `vlan` if it is a vlan device (e.g. `eth0.1`)
2923 * - `switch` if it is a switch device (e.g.`eth1` connected to switch0)
2924 * - `ethernet` for all other device types
2926 getType: function() {
2927 if (this.device
!= null && this.device
.charAt(0) == '@')
2929 else if (this.dev
.devtype
== 'wlan' || this.wif
!= null || isWifiIfname(this.device
))
2931 else if (this.dev
.devtype
== 'bridge' || _state
.isBridge
[this.device
])
2933 else if (_state
.isTunnel
[this.device
])
2935 else if (this.dev
.devtype
== 'vlan' || this.device
.indexOf('.') > -1)
2937 else if (this.dev
.devtype
== 'dsa' || _state
.isSwitch
[this.device
])
2939 else if (this.config
.type
== '8021q' || this.config
.type
== '8021ad')
2941 else if (this.config
.type
== 'bridge')
2948 * Get a short description string for the device.
2951 * Returns the device name for non-wifi devices or a string containing
2952 * the operation mode and SSID for wifi devices.
2954 getShortName: function() {
2955 if (this.wif
!= null)
2956 return this.wif
.getShortName();
2962 * Get a long description string for the device.
2965 * Returns a string containing the type description and device name
2966 * for non-wifi devices or operation mode and ssid for wifi ones.
2968 getI18n: function() {
2969 if (this.wif
!= null) {
2970 return '%s: %s "%s"'.format(
2971 _('Wireless Network'),
2972 this.wif
.getActiveMode(),
2973 this.wif
.getActiveSSID() || this.wif
.getActiveBSSID() || this.wif
.getID() || '?');
2976 return '%s: "%s"'.format(this.getTypeI18n(), this.getName());
2980 * Get a string describing the device type.
2983 * Returns a string describing the type, e.g. "Wireless Adapter" or
2986 getTypeI18n: function() {
2987 switch (this.getType()) {
2989 return _('Alias Interface');
2992 return _('Wireless Adapter');
2998 return (_state
.netdevs
[this.device
] && _state
.netdevs
[this.device
].devtype
== 'dsa')
2999 ? _('Switch port') : _('Ethernet Switch');
3002 return (_state
.isSwitch
[this.device
] ? _('Switch VLAN') : _('Software VLAN'));
3005 return _('Tunnel Interface');
3008 return _('Ethernet Adapter');
3013 * Get the associated bridge ports of the device.
3015 * @returns {null|Array<LuCI.network.Device>}
3016 * Returns an array of `Network.Device` instances representing the ports
3017 * (slave interfaces) of the bridge or `null` when this device isn't
3020 getPorts: function() {
3021 var br
= _state
.bridges
[this.device
],
3024 if (br
== null || !Array
.isArray(br
.ifnames
))
3027 for (var i
= 0; i
< br
.ifnames
.length
; i
++)
3028 rv
.push(Network
.prototype.instantiateDevice(br
.ifnames
[i
].name
));
3030 rv
.sort(deviceSort
);
3038 * @returns {null|string}
3039 * Returns the ID of this network bridge or `null` if this network
3040 * device is not a Linux bridge.
3042 getBridgeID: function() {
3043 var br
= _state
.bridges
[this.device
];
3044 return (br
!= null ? br
.id
: null);
3048 * Get the bridge STP setting
3050 * @returns {boolean}
3051 * Returns `true` when this device is a Linux bridge and has `stp`
3052 * enabled, else `false`.
3054 getBridgeSTP: function() {
3055 var br
= _state
.bridges
[this.device
];
3056 return (br
!= null ? !!br
.stp
: false);
3060 * Checks whether this device is up.
3062 * @returns {boolean}
3063 * Returns `true` when the associated device is running pr `false`
3064 * when it is down or absent.
3067 var up
= this._devstate('flags', 'up');
3070 up
= (this.getType() == 'alias');
3076 * Checks whether this device is a Linux bridge.
3078 * @returns {boolean}
3079 * Returns `true` when the network device is present and a Linux bridge,
3082 isBridge: function() {
3083 return (this.getType() == 'bridge');
3087 * Checks whether this device is part of a Linux bridge.
3089 * @returns {boolean}
3090 * Returns `true` when this network device is part of a bridge,
3093 isBridgePort: function() {
3094 return (this._devstate('bridge') != null);
3098 * Get the amount of transmitted bytes.
3101 * Returns the amount of bytes transmitted by the network device.
3103 getTXBytes: function() {
3104 var stat
= this._devstate('stats');
3105 return (stat
!= null ? stat
.tx_bytes
|| 0 : 0);
3109 * Get the amount of received bytes.
3112 * Returns the amount of bytes received by the network device.
3114 getRXBytes: function() {
3115 var stat
= this._devstate('stats');
3116 return (stat
!= null ? stat
.rx_bytes
|| 0 : 0);
3120 * Get the amount of transmitted packets.
3123 * Returns the amount of packets transmitted by the network device.
3125 getTXPackets: function() {
3126 var stat
= this._devstate('stats');
3127 return (stat
!= null ? stat
.tx_packets
|| 0 : 0);
3131 * Get the amount of received packets.
3134 * Returns the amount of packets received by the network device.
3136 getRXPackets: function() {
3137 var stat
= this._devstate('stats');
3138 return (stat
!= null ? stat
.rx_packets
|| 0 : 0);
3142 * Get the carrier state of the network device.
3144 * @returns {boolean}
3145 * Returns true if the device has a carrier, e.g. when a cable is
3146 * inserted into an ethernet port of false if there is none.
3148 getCarrier: function() {
3149 var link
= this._devstate('link');
3150 return (link
!= null ? link
.carrier
|| false : false);
3154 * Get the current link speed of the network device if available.
3156 * @returns {number|null}
3157 * Returns the current speed of the network device in Mbps. If the
3158 * device supports no ethernet speed levels, null is returned.
3159 * If the device supports ethernet speeds but has no carrier, -1 is
3162 getSpeed: function() {
3163 var link
= this._devstate('link');
3164 return (link
!= null ? link
.speed
|| null : null);
3168 * Get the current duplex mode of the network device if available.
3170 * @returns {string|null}
3171 * Returns the current duplex mode of the network device. Returns
3172 * either "full" or "half" if the device supports duplex modes or
3173 * null if the duplex mode is unknown or unsupported.
3175 getDuplex: function() {
3176 var link
= this._devstate('link'),
3177 duplex
= link
? link
.duplex
: null;
3179 return (duplex
!= 'unknown') ? duplex
: null;
3183 * Get the primary logical interface this device is assigned to.
3185 * @returns {null|LuCI.network.Protocol}
3186 * Returns a `Network.Protocol` instance representing the logical
3187 * interface this device is attached to or `null` if it is not
3188 * assigned to any logical interface.
3190 getNetwork: function() {
3191 return this.getNetworks()[0];
3195 * Get the logical interfaces this device is assigned to.
3197 * @returns {Array<LuCI.network.Protocol>}
3198 * Returns an array of `Network.Protocol` instances representing the
3199 * logical interfaces this device is assigned to.
3201 getNetworks: function() {
3202 if (this.networks
== null) {
3205 var networks
= enumerateNetworks
.apply(L
.network
);
3207 for (var i
= 0; i
< networks
.length
; i
++)
3208 if (networks
[i
].containsDevice(this.device
) || networks
[i
].getIfname() == this.device
)
3209 this.networks
.push(networks
[i
]);
3211 this.networks
.sort(networkSort
);
3214 return this.networks
;
3218 * Get the related wireless network this device is related to.
3220 * @returns {null|LuCI.network.WifiNetwork}
3221 * Returns a `Network.WifiNetwork` instance representing the wireless
3222 * network corresponding to this network device or `null` if this device
3223 * is not a wireless device.
3225 getWifiNetwork: function() {
3226 return (this.wif
!= null ? this.wif
: null);
3230 * Get the logical parent device of this device.
3232 * In case of DSA switch ports, the parent device will be the DSA switch
3233 * device itself, for VLAN devices, the parent refers to the base device
3236 * @returns {null|LuCI.network.Device}
3237 * Returns a `Network.Device` instance representing the parent device or
3238 * `null` when this device has no parent, as it is the case for e.g.
3239 * ordinary ethernet interfaces.
3241 getParent: function() {
3242 if (this.dev
.parent
)
3243 return Network
.prototype.instantiateDevice(this.dev
.parent
);
3245 if ((this.config
.type
== '8021q' || this.config
.type
== '802ad') && typeof(this.config
.ifname
) == 'string')
3246 return Network
.prototype.instantiateDevice(this.config
.ifname
);
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;
3399 var modes
= this.ubus('dev', 'iwinfo', 'hwmodes_text');
3401 if (this.ubus('dev', 'iwinfo', 'type') == 'wl')
3404 return '%s %s Wireless Controller (%s)'.format(
3406 modes
? '802.11' + modes
: 'unknown',
3411 * A wireless scan result object describes a neighbouring wireless
3412 * network found in the vincinity.
3414 * @typedef {Object<string, number|string|LuCI.network.WifiEncryption>} WifiScanResult
3415 * @memberof LuCI.network
3417 * @property {string} ssid
3418 * The SSID / Mesh ID of the network.
3420 * @property {string} bssid
3421 * The BSSID if the network.
3423 * @property {string} mode
3424 * The operation mode of the network (`Master`, `Ad-Hoc`, `Mesh Point`).
3426 * @property {number} channel
3427 * The wireless channel of the network.
3429 * @property {number} signal
3430 * The received signal strength of the network in dBm.
3432 * @property {number} quality
3433 * The numeric quality level of the signal, can be used in conjunction
3434 * with `quality_max` to calculate a quality percentage.
3436 * @property {number} quality_max
3437 * The maximum possible quality level of the signal, can be used in
3438 * conjunction with `quality` to calculate a quality percentage.
3440 * @property {LuCI.network.WifiEncryption} encryption
3441 * The encryption used by the wireless network.
3445 * Trigger a wireless scan on this radio device and obtain a list of
3448 * @returns {Promise<Array<LuCI.network.WifiScanResult>>}
3449 * Returns a promise resolving to an array of scan result objects
3450 * describing the networks found in the vincinity.
3452 getScanList: function() {
3453 return callIwinfoScan(this.sid
);
3457 * Check whether the wireless radio is marked as up in the `ubus`
3460 * @returns {boolean}
3461 * Returns `true` when the radio device is up, else `false`.
3464 if (L
.isObject(_state
.radios
[this.sid
]))
3465 return (_state
.radios
[this.sid
].up
== true);
3471 * Get the wifi network of the given name belonging to this radio device
3473 * @param {string} network
3474 * The name of the wireless network to lookup. This may be either an uci
3475 * configuration section ID, a network ID in the form `radio#.network#`
3476 * or a Linux network device name like `wlan0` which is resolved to the
3477 * corresponding configuration section through `ubus` runtime information.
3479 * @returns {Promise<LuCI.network.WifiNetwork>}
3480 * Returns a promise resolving to a `Network.WifiNetwork` instance
3481 * representing the wireless network and rejecting with `null` if
3482 * the given network could not be found or is not associated with
3483 * this radio device.
3485 getWifiNetwork: function(network
) {
3486 return Network
.prototype.getWifiNetwork(network
).then(L
.bind(function(networkInstance
) {
3487 var uciWifiIface
= (networkInstance
.sid
? uci
.get('wireless', networkInstance
.sid
) : null);
3489 if (uciWifiIface
== null || uciWifiIface
['.type'] != 'wifi-iface' || uciWifiIface
.device
!= this.sid
)
3490 return Promise
.reject();
3492 return networkInstance
;
3497 * Get all wireless networks associated with this wireless radio device.
3499 * @returns {Promise<Array<LuCI.network.WifiNetwork>>}
3500 * Returns a promise resolving to an array of `Network.WifiNetwork`
3501 * instances respresenting the wireless networks associated with this
3504 getWifiNetworks: function() {
3505 return Network
.prototype.getWifiNetworks().then(L
.bind(function(networks
) {
3508 for (var i
= 0; i
< networks
.length
; i
++)
3509 if (networks
[i
].getWifiDeviceName() == this.getName())
3510 rv
.push(networks
[i
]);
3517 * Adds a new wireless network associated with this radio device to the
3518 * configuration and sets its options to the provided values.
3520 * @param {Object<string, string|string[]>} [options]
3521 * The options to set for the newly added wireless network.
3523 * @returns {Promise<null|LuCI.network.WifiNetwork>}
3524 * Returns a promise resolving to a `WifiNetwork` instance describing
3525 * the newly added wireless network or `null` if the given options
3528 addWifiNetwork: function(options
) {
3529 if (!L
.isObject(options
))
3532 options
.device
= this.sid
;
3534 return Network
.prototype.addWifiNetwork(options
);
3538 * Deletes the wireless network with the given name associated with this
3541 * @param {string} network
3542 * The name of the wireless network to lookup. This may be either an uci
3543 * configuration section ID, a network ID in the form `radio#.network#`
3544 * or a Linux network device name like `wlan0` which is resolved to the
3545 * corresponding configuration section through `ubus` runtime information.
3547 * @returns {Promise<boolean>}
3548 * Returns a promise resolving to `true` when the wireless network was
3549 * successfully deleted from the configuration or `false` when the given
3550 * network could not be found or if the found network was not associated
3551 * with this wireless radio device.
3553 deleteWifiNetwork: function(network
) {
3556 if (network
instanceof WifiNetwork
) {
3560 var uciWifiIface
= uci
.get('wireless', network
);
3562 if (uciWifiIface
== null || uciWifiIface
['.type'] != 'wifi-iface')
3563 sid
= getWifiSidByIfname(network
);
3566 if (sid
== null || uci
.get('wireless', sid
, 'device') != this.sid
)
3567 return Promise
.resolve(false);
3569 uci
.delete('wireless', network
);
3571 return Promise
.resolve(true);
3577 * @memberof LuCI.network
3581 * A `Network.WifiNetwork` instance represents a wireless network (vif)
3582 * configured on top of a radio device and provides functions for querying
3583 * the runtime state of the network. Most radio devices support multiple
3584 * such networks in parallel.
3586 WifiNetwork
= baseclass
.extend(/** @lends LuCI.network.WifiNetwork.prototype */ {
3587 __init__: function(sid
, radioname
, radiostate
, netid
, netstate
, hostapd
) {
3598 ubus: function(/* ... */) {
3599 var v
= this._ubusdata
;
3601 for (var i
= 0; i
< arguments
.length
; i
++)
3603 v
= v
[arguments
[i
]];
3611 * Read the given UCI option value of this wireless network.
3613 * @param {string} opt
3614 * The UCI option name to read.
3616 * @returns {null|string|string[]}
3617 * Returns the UCI option value or `null` if the requested option is
3620 get: function(opt
) {
3621 return uci
.get('wireless', this.sid
, opt
);
3625 * Set the given UCI option of this network to the given value.
3627 * @param {string} opt
3628 * The name of the UCI option to set.
3630 * @param {null|string|string[]} val
3631 * The value to set or `null` to remove the given option from the
3634 set: function(opt
, value
) {
3635 return uci
.set('wireless', this.sid
, opt
, value
);
3639 * Checks whether this wireless network is disabled.
3641 * @returns {boolean}
3642 * Returns `true` when the wireless radio is marked as disabled in `ubus`
3643 * runtime state or when the `disabled` option is set in the corresponding
3644 * UCI configuration.
3646 isDisabled: function() {
3647 return this.ubus('dev', 'disabled') || this.get('disabled') == '1';
3651 * Get the configured operation mode of the wireless network.
3654 * Returns the configured operation mode. Possible values are:
3655 * - `ap` - Master (Access Point) mode
3656 * - `sta` - Station (client) mode
3657 * - `adhoc` - Ad-Hoc (IBSS) mode
3658 * - `mesh` - Mesh (IEEE 802.11s) mode
3659 * - `monitor` - Monitor mode
3661 getMode: function() {
3662 return this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3666 * Get the configured SSID of the wireless network.
3668 * @returns {null|string}
3669 * Returns the configured SSID value or `null` when this network is
3672 getSSID: function() {
3673 if (this.getMode() == 'mesh')
3676 return this.ubus('net', 'config', 'ssid') || this.get('ssid');
3680 * Get the configured Mesh ID of the wireless network.
3682 * @returns {null|string}
3683 * Returns the configured mesh ID value or `null` when this network
3684 * is not in mesh mode.
3686 getMeshID: function() {
3687 if (this.getMode() != 'mesh')
3690 return this.ubus('net', 'config', 'mesh_id') || this.get('mesh_id');
3694 * Get the configured BSSID of the wireless network.
3696 * @returns {null|string}
3697 * Returns the BSSID value or `null` if none has been specified.
3699 getBSSID: function() {
3700 return this.ubus('net', 'config', 'bssid') || this.get('bssid');
3704 * Get the names of the logical interfaces this wireless network is
3707 * @returns {string[]}
3708 * Returns an array of logical interface names.
3710 getNetworkNames: function() {
3711 return L
.toArray(this.ubus('net', 'config', 'network') || this.get('network'));
3715 * Get the internal network ID of this wireless network.
3717 * The network ID is a LuCI specific identifer in the form
3718 * `radio#.network#` to identify wireless networks by their corresponding
3719 * radio and network index numbers.
3722 * Returns the LuCI specific network ID.
3729 * Get the configuration ID of this wireless network.
3732 * Returns the corresponding UCI section ID of the network.
3734 getName: function() {
3739 * Get the Linux network device name.
3741 * @returns {null|string}
3742 * Returns the current Linux network device name as resolved from
3743 * `ubus` runtime information or `null` if this network has no
3744 * associated network device, e.g. when not configured or up.
3746 getIfname: function() {
3747 var ifname
= this.ubus('net', 'ifname') || this.ubus('net', 'iwinfo', 'ifname');
3749 if (ifname
== null || ifname
.match(/^(wifi|radio)\d/))
3750 ifname
= this.netid
;
3756 * Get the Linux VLAN network device names.
3758 * @returns {string[]}
3759 * Returns the current Linux VLAN network device name as resolved
3760 * from `ubus` runtime information or empty array if this network
3761 * has no associated VLAN network devices.
3763 getVlanIfnames: function() {
3764 var vlans
= L
.toArray(this.ubus('net', 'vlans')),
3767 for (var i
= 0; i
< vlans
.length
; i
++)
3768 ifnames
.push(vlans
[i
]['ifname']);
3774 * Get the name of the corresponding wifi radio device.
3776 * @returns {null|string}
3777 * Returns the name of the radio device this network is configured on
3778 * or `null` if it cannot be determined.
3780 getWifiDeviceName: function() {
3781 return this.ubus('radio') || this.get('device');
3785 * Get the corresponding wifi radio device.
3787 * @returns {null|LuCI.network.WifiDevice}
3788 * Returns a `Network.WifiDevice` instance representing the corresponding
3789 * wifi radio device or `null` if the related radio device could not be
3792 getWifiDevice: function() {
3793 var radioname
= this.getWifiDeviceName();
3795 if (radioname
== null)
3796 return Promise
.reject();
3798 return Network
.prototype.getWifiDevice(radioname
);
3802 * Check whether the radio network is up.
3804 * This function actually queries the up state of the related radio
3805 * device and assumes this network to be up as well when the parent
3806 * radio is up. This is due to the fact that OpenWrt does not control
3807 * virtual interfaces individually but within one common hostapd
3810 * @returns {boolean}
3811 * Returns `true` when the network is up, else `false`.
3814 var device
= this.getDevice();
3819 return device
.isUp();
3823 * Query the current operation mode from runtime information.
3826 * Returns the human readable mode name as reported by `ubus` runtime
3827 * state. Possible returned values are:
3839 getActiveMode: function() {
3840 var mode
= this.ubus('net', 'iwinfo', 'mode') || this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3843 case 'ap': return 'Master';
3844 case 'sta': return 'Client';
3845 case 'adhoc': return 'Ad-Hoc';
3846 case 'mesh': return 'Mesh';
3847 case 'monitor': return 'Monitor';
3848 default: return mode
;
3853 * Query the current operation mode from runtime information as
3854 * translated string.
3857 * Returns the translated, human readable mode name as reported by
3858 *`ubus` runtime state.
3860 getActiveModeI18n: function() {
3861 var mode
= this.getActiveMode();
3864 case 'Master': return _('Master');
3865 case 'Client': return _('Client');
3866 case 'Ad-Hoc': return _('Ad-Hoc');
3867 case 'Mash': return _('Mesh');
3868 case 'Monitor': return _('Monitor');
3869 default: return mode
;
3874 * Query the current SSID from runtime information.
3877 * Returns the current SSID or Mesh ID as reported by `ubus` runtime
3880 getActiveSSID: function() {
3881 return this.ubus('net', 'iwinfo', 'ssid') || this.ubus('net', 'config', 'ssid') || this.get('ssid');
3885 * Query the current BSSID from runtime information.
3888 * Returns the current BSSID or Mesh ID as reported by `ubus` runtime
3891 getActiveBSSID: function() {
3892 return this.ubus('net', 'iwinfo', 'bssid') || this.ubus('net', 'config', 'bssid') || this.get('bssid');
3896 * Query the current encryption settings from runtime information.
3899 * Returns a string describing the current encryption or `-` if the the
3900 * encryption state could not be found in `ubus` runtime information.
3902 getActiveEncryption: function() {
3903 return formatWifiEncryption(this.ubus('net', 'iwinfo', 'encryption')) || '-';
3907 * A wireless peer entry describes the properties of a remote wireless
3908 * peer associated with a local network.
3910 * @typedef {Object<string, boolean|number|string|LuCI.network.WifiRateEntry>} WifiPeerEntry
3911 * @memberof LuCI.network
3913 * @property {string} mac
3914 * The MAC address (BSSID).
3916 * @property {number} signal
3917 * The received signal strength.
3919 * @property {number} [signal_avg]
3920 * The average signal strength if supported by the driver.
3922 * @property {number} [noise]
3923 * The current noise floor of the radio. May be `0` or absent if not
3924 * supported by the driver.
3926 * @property {number} inactive
3927 * The amount of milliseconds the peer has been inactive, e.g. due
3930 * @property {number} connected_time
3931 * The amount of milliseconds the peer is associated to this network.
3933 * @property {number} [thr]
3934 * The estimated throughput of the peer, May be `0` or absent if not
3935 * supported by the driver.
3937 * @property {boolean} authorized
3938 * Specifies whether the peer is authorized to associate to this network.
3940 * @property {boolean} authenticated
3941 * Specifies whether the peer completed authentication to this network.
3943 * @property {string} preamble
3944 * The preamble mode used by the peer. May be `long` or `short`.
3946 * @property {boolean} wme
3947 * Specifies whether the peer supports WME/WMM capabilities.
3949 * @property {boolean} mfp
3950 * Specifies whether management frame protection is active.
3952 * @property {boolean} tdls
3953 * Specifies whether TDLS is active.
3955 * @property {number} [mesh llid]
3956 * The mesh LLID, may be `0` or absent if not applicable or supported
3959 * @property {number} [mesh plid]
3960 * The mesh PLID, may be `0` or absent if not applicable or supported
3963 * @property {string} [mesh plink]
3964 * The mesh peer link state description, may be an empty string (`''`)
3965 * or absent if not applicable or supported by the driver.
3967 * The following states are known:
3977 * @property {number} [mesh local PS]
3978 * The local powersafe mode for the peer link, may be an empty
3979 * string (`''`) or absent if not applicable or supported by
3982 * The following modes are known:
3983 * - `ACTIVE` (no power save)
3988 * @property {number} [mesh peer PS]
3989 * The remote powersafe mode for the peer link, may be an empty
3990 * string (`''`) or absent if not applicable or supported by
3993 * The following modes are known:
3994 * - `ACTIVE` (no power save)
3999 * @property {number} [mesh non-peer PS]
4000 * The powersafe mode for all non-peer neigbours, may be an empty
4001 * string (`''`) or absent if not applicable or supported by the driver.
4003 * The following modes are known:
4004 * - `ACTIVE` (no power save)
4009 * @property {LuCI.network.WifiRateEntry} rx
4010 * Describes the receiving wireless rate from the peer.
4012 * @property {LuCI.network.WifiRateEntry} tx
4013 * Describes the transmitting wireless rate to the peer.
4017 * A wireless rate entry describes the properties of a wireless
4018 * transmission rate to or from a peer.
4020 * @typedef {Object<string, boolean|number>} WifiRateEntry
4021 * @memberof LuCI.network
4023 * @property {number} [drop_misc]
4024 * The amount of received misc. packages that have been dropped, e.g.
4025 * due to corruption or missing authentication. Only applicable to
4028 * @property {number} packets
4029 * The amount of packets that have been received or sent.
4031 * @property {number} bytes
4032 * The amount of bytes that have been received or sent.
4034 * @property {number} [failed]
4035 * The amount of failed tranmission attempts. Only applicable to
4038 * @property {number} [retries]
4039 * The amount of retried transmissions. Only applicable to transmit
4042 * @property {boolean} is_ht
4043 * Specifies whether this rate is an HT (IEEE 802.11n) rate.
4045 * @property {boolean} is_vht
4046 * Specifies whether this rate is an VHT (IEEE 802.11ac) rate.
4048 * @property {number} mhz
4049 * The channel width in MHz used for the transmission.
4051 * @property {number} rate
4052 * The bitrate in bit/s of the transmission.
4054 * @property {number} [mcs]
4055 * The MCS index of the used transmission rate. Only applicable to
4058 * @property {number} [40mhz]
4059 * Specifies whether the tranmission rate used 40MHz wide channel.
4060 * Only applicable to HT or VHT rates.
4062 * Note: this option exists for backwards compatibility only and its
4063 * use is discouraged. The `mhz` field should be used instead to
4064 * determine the channel width.
4066 * @property {boolean} [short_gi]
4067 * Specifies whether a short guard interval is used for the transmission.
4068 * Only applicable to HT or VHT rates.
4070 * @property {number} [nss]
4071 * Specifies the number of spatial streams used by the transmission.
4072 * Only applicable to VHT rates.
4074 * @property {boolean} [he]
4075 * Specifies whether this rate is an HE (IEEE 802.11ax) rate.
4077 * @property {number} [he_gi]
4078 * Specifies whether the guard interval used for the transmission.
4079 * Only applicable to HE rates.
4081 * @property {number} [he_dcm]
4082 * Specifies whether dual concurrent modulation is used for the transmission.
4083 * Only applicable to HE rates.
4087 * Fetch the list of associated peers.
4089 * @returns {Promise<Array<LuCI.network.WifiPeerEntry>>}
4090 * Returns a promise resolving to an array of wireless peers associated
4091 * with this network.
4093 getAssocList: function() {
4095 var ifnames
= [ this.getIfname() ].concat(this.getVlanIfnames());
4097 for (var i
= 0; i
< ifnames
.length
; i
++)
4098 tasks
.push(callIwinfoAssoclist(ifnames
[i
]));
4100 return Promise
.all(tasks
).then(function(values
) {
4101 return Array
.prototype.concat
.apply([], values
);
4106 * Query the current operating frequency of the wireless network.
4108 * @returns {null|string}
4109 * Returns the current operating frequency of the network from `ubus`
4110 * runtime information in GHz or `null` if the information is not
4113 getFrequency: function() {
4114 var freq
= this.ubus('net', 'iwinfo', 'frequency');
4116 if (freq
!= null && freq
> 0)
4117 return '%.03f'.format(freq
/ 1000);
4123 * Query the current average bitrate of all peers associated to this
4126 * @returns {null|number}
4127 * Returns the average bit rate among all peers associated to the network
4128 * as reported by `ubus` runtime information or `null` if the information
4131 getBitRate: function() {
4132 var rate
= this.ubus('net', 'iwinfo', 'bitrate');
4134 if (rate
!= null && rate
> 0)
4135 return (rate
/ 1000);
4141 * Query the current wireless channel.
4143 * @returns {null|number}
4144 * Returns the wireless channel as reported by `ubus` runtime information
4145 * or `null` if it cannot be determined.
4147 getChannel: function() {
4148 return this.ubus('net', 'iwinfo', 'channel') || this.ubus('dev', 'config', 'channel') || this.get('channel');
4152 * Query the current wireless signal.
4154 * @returns {null|number}
4155 * Returns the wireless signal in dBm as reported by `ubus` runtime
4156 * information or `null` if it cannot be determined.
4158 getSignal: function() {
4159 return this.ubus('net', 'iwinfo', 'signal') || 0;
4163 * Query the current radio noise floor.
4166 * Returns the radio noise floor in dBm as reported by `ubus` runtime
4167 * information or `0` if it cannot be determined.
4169 getNoise: function() {
4170 return this.ubus('net', 'iwinfo', 'noise') || 0;
4174 * Query the current country code.
4177 * Returns the wireless country code as reported by `ubus` runtime
4178 * information or `00` if it cannot be determined.
4180 getCountryCode: function() {
4181 return this.ubus('net', 'iwinfo', 'country') || this.ubus('dev', 'config', 'country') || '00';
4185 * Query the current radio TX power.
4187 * @returns {null|number}
4188 * Returns the wireless network transmit power in dBm as reported by
4189 * `ubus` runtime information or `null` if it cannot be determined.
4191 getTXPower: function() {
4192 return this.ubus('net', 'iwinfo', 'txpower');
4196 * Query the radio TX power offset.
4198 * Some wireless radios have a fixed power offset, e.g. due to the
4199 * use of external amplifiers.
4202 * Returns the wireless network transmit power offset in dBm as reported
4203 * by `ubus` runtime information or `0` if there is no offset, or if it
4204 * cannot be determined.
4206 getTXPowerOffset: function() {
4207 return this.ubus('net', 'iwinfo', 'txpower_offset') || 0;
4211 * Calculate the current signal.
4215 * Returns the calculated signal level, which is the difference between
4216 * noise and signal (SNR), divided by 5.
4218 getSignalLevel: function(signal
, noise
) {
4219 if (this.getActiveBSSID() == '00:00:00:00:00:00')
4222 signal
= signal
|| this.getSignal();
4223 noise
= noise
|| this.getNoise();
4225 if (signal
< 0 && noise
< 0) {
4226 var snr
= -1 * (noise
- signal
);
4227 return Math
.floor(snr
/ 5);
4234 * Calculate the current signal quality percentage.
4237 * Returns the calculated signal quality in percent. The value is
4238 * calculated from the `quality` and `quality_max` indicators reported
4239 * by `ubus` runtime state.
4241 getSignalPercent: function() {
4242 var qc
= this.ubus('net', 'iwinfo', 'quality') || 0,
4243 qm
= this.ubus('net', 'iwinfo', 'quality_max') || 0;
4245 if (qc
> 0 && qm
> 0)
4246 return Math
.floor((100 / qm
) * qc
);
4252 * Get a short description string for this wireless network.
4255 * Returns a string describing this network, consisting of the
4256 * active operation mode, followed by either the SSID, BSSID or
4257 * internal network ID, depending on which information is available.
4259 getShortName: function() {
4260 return '%s "%s"'.format(
4261 this.getActiveModeI18n(),
4262 this.getActiveSSID() || this.getActiveBSSID() || this.getID());
4266 * Get a description string for this wireless network.
4269 * Returns a string describing this network, consisting of the
4270 * term `Wireless Network`, followed by the active operation mode,
4271 * the SSID, BSSID or internal network ID and the Linux network device
4272 * name, depending on which information is available.
4274 getI18n: function() {
4275 return '%s: %s "%s" (%s)'.format(
4276 _('Wireless Network'),
4277 this.getActiveModeI18n(),
4278 this.getActiveSSID() || this.getActiveBSSID() || this.getID(),
4283 * Get the primary logical interface this wireless network is attached to.
4285 * @returns {null|LuCI.network.Protocol}
4286 * Returns a `Network.Protocol` instance representing the logical
4287 * interface or `null` if this network is not attached to any logical
4290 getNetwork: function() {
4291 return this.getNetworks()[0];
4295 * Get the logical interfaces this wireless network is attached to.
4297 * @returns {Array<LuCI.network.Protocol>}
4298 * Returns an array of `Network.Protocol` instances representing the
4299 * logical interfaces this wireless network is attached to.
4301 getNetworks: function() {
4302 var networkNames
= this.getNetworkNames(),
4305 for (var i
= 0; i
< networkNames
.length
; i
++) {
4306 var uciInterface
= uci
.get('network', networkNames
[i
]);
4308 if (uciInterface
== null || uciInterface
['.type'] != 'interface')
4311 networks
.push(Network
.prototype.instantiateNetwork(networkNames
[i
]));
4314 networks
.sort(networkSort
);
4320 * Get the associated Linux network device.
4322 * @returns {LuCI.network.Device}
4323 * Returns a `Network.Device` instance representing the Linux network
4324 * device associted with this wireless network.
4326 getDevice: function() {
4327 return Network
.prototype.instantiateDevice(this.getIfname());
4331 * Check whether this wifi network supports deauthenticating clients.
4333 * @returns {boolean}
4334 * Returns `true` when this wifi network instance supports forcibly
4335 * deauthenticating clients, otherwise `false`.
4337 isClientDisconnectSupported: function() {
4338 return L
.isObject(this.ubus('hostapd', 'del_client'));
4342 * Forcibly disconnect the given client from the wireless network.
4344 * @param {string} mac
4345 * The MAC address of the client to disconnect.
4347 * @param {boolean} [deauth=false]
4348 * Specifies whether to deauthenticate (`true`) or disassociate (`false`)
4351 * @param {number} [reason=1]
4352 * Specifies the IEEE 802.11 reason code to disassoc/deauth the client
4353 * with. Default is `1` which corresponds to `Unspecified reason`.
4355 * @param {number} [ban_time=0]
4356 * Specifies the amount of milliseconds to ban the client from
4357 * reconnecting. By default, no ban time is set which allows the client
4358 * to reassociate / reauthenticate immediately.
4360 * @returns {Promise<number>}
4361 * Returns a promise resolving to the underlying ubus call result code
4362 * which is typically `0`, even for not existing MAC addresses.
4363 * The promise might reject with an error in case invalid arguments
4366 disconnectClient: function(mac
, deauth
, reason
, ban_time
) {
4367 if (reason
== null || reason
== 0)
4373 return rpc
.declare({
4374 object
: 'hostapd.%s'.format(this.getIfname()),
4375 method
: 'del_client',
4376 params
: [ 'addr', 'deauth', 'reason', 'ban_time' ]
4377 })(mac
, deauth
, reason
, ban_time
);