e06633357fc5aed07bbaed9685d94e68e11fd8e8
[project/luci.git] / docs / jsapi / network.js.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="utf-8">
5 <title>JSDoc: Source: network.js</title>
6
7 <script src="scripts/prettify/prettify.js"> </script>
8 <script src="scripts/prettify/lang-css.js"> </script>
9 <!--[if lt IE 9]>
10 <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11 <![endif]-->
12 <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13 <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14 </head>
15
16 <body>
17
18 <div id="main">
19
20 <h1 class="page-title">Source: network.js</h1>
21
22
23
24
25
26
27 <section>
28 <article>
29 <pre class="prettyprint source linenums"><code>'use strict';
30 'require uci';
31 'require rpc';
32 'require validation';
33
34 var proto_errors = {
35 CONNECT_FAILED: _('Connection attempt failed'),
36 INVALID_ADDRESS: _('IP address in invalid'),
37 INVALID_GATEWAY: _('Gateway address is invalid'),
38 INVALID_LOCAL_ADDRESS: _('Local IP address is invalid'),
39 MISSING_ADDRESS: _('IP address is missing'),
40 MISSING_PEER_ADDRESS: _('Peer address is missing'),
41 NO_DEVICE: _('Network device is not present'),
42 NO_IFACE: _('Unable to determine device name'),
43 NO_IFNAME: _('Unable to determine device name'),
44 NO_WAN_ADDRESS: _('Unable to determine external IP address'),
45 NO_WAN_LINK: _('Unable to determine upstream interface'),
46 PEER_RESOLVE_FAIL: _('Unable to resolve peer host name'),
47 PIN_FAILED: _('PIN code rejected')
48 };
49
50 var iface_patterns_ignore = [
51 /^wmaster\d+/,
52 /^wifi\d+/,
53 /^hwsim\d+/,
54 /^imq\d+/,
55 /^ifb\d+/,
56 /^mon\.wlan\d+/,
57 /^sit\d+/,
58 /^gre\d+/,
59 /^gretap\d+/,
60 /^ip6gre\d+/,
61 /^ip6tnl\d+/,
62 /^tunl\d+/,
63 /^lo$/
64 ];
65
66 var iface_patterns_wireless = [
67 /^wlan\d+/,
68 /^wl\d+/,
69 /^ath\d+/,
70 /^\w+\.network\d+/
71 ];
72
73 var iface_patterns_virtual = [ ];
74
75 var callLuciNetworkDevices = rpc.declare({
76 object: 'luci-rpc',
77 method: 'getNetworkDevices',
78 expect: { '': {} }
79 });
80
81 var callLuciWirelessDevices = rpc.declare({
82 object: 'luci-rpc',
83 method: 'getWirelessDevices',
84 expect: { '': {} }
85 });
86
87 var callLuciBoardJSON = rpc.declare({
88 object: 'luci-rpc',
89 method: 'getBoardJSON'
90 });
91
92 var callLuciHostHints = rpc.declare({
93 object: 'luci-rpc',
94 method: 'getHostHints',
95 expect: { '': {} }
96 });
97
98 var callIwinfoAssoclist = rpc.declare({
99 object: 'iwinfo',
100 method: 'assoclist',
101 params: [ 'device', 'mac' ],
102 expect: { results: [] }
103 });
104
105 var callIwinfoScan = rpc.declare({
106 object: 'iwinfo',
107 method: 'scan',
108 params: [ 'device' ],
109 nobatch: true,
110 expect: { results: [] }
111 });
112
113 var callNetworkInterfaceDump = rpc.declare({
114 object: 'network.interface',
115 method: 'dump',
116 expect: { 'interface': [] }
117 });
118
119 var callNetworkProtoHandlers = rpc.declare({
120 object: 'network',
121 method: 'get_proto_handlers',
122 expect: { '': {} }
123 });
124
125 var _init = null,
126 _state = null,
127 _protocols = {},
128 _protospecs = {};
129
130 function getProtocolHandlers(cache) {
131 return callNetworkProtoHandlers().then(function(protos) {
132 /* Register "none" protocol */
133 if (!protos.hasOwnProperty('none'))
134 Object.assign(protos, { none: { no_device: false } });
135
136 /* Hack: emulate relayd protocol */
137 if (!protos.hasOwnProperty('relay'))
138 Object.assign(protos, { relay: { no_device: true } });
139
140 Object.assign(_protospecs, protos);
141
142 return Promise.all(Object.keys(protos).map(function(p) {
143 return Promise.resolve(L.require('protocol.%s'.format(p))).catch(function(err) {
144 if (L.isObject(err) &amp;&amp; err.name != 'NetworkError')
145 L.error(err);
146 });
147 })).then(function() {
148 return protos;
149 });
150 }).catch(function() {
151 return {};
152 });
153 }
154
155 function getWifiStateBySid(sid) {
156 var s = uci.get('wireless', sid);
157
158 if (s != null &amp;&amp; s['.type'] == 'wifi-iface') {
159 for (var radioname in _state.radios) {
160 for (var i = 0; i &lt; _state.radios[radioname].interfaces.length; i++) {
161 var netstate = _state.radios[radioname].interfaces[i];
162
163 if (typeof(netstate.section) != 'string')
164 continue;
165
166 var s2 = uci.get('wireless', netstate.section);
167
168 if (s2 != null &amp;&amp; s['.type'] == s2['.type'] &amp;&amp; s['.name'] == s2['.name']) {
169 if (s2['.anonymous'] == false &amp;&amp; netstate.section.charAt(0) == '@')
170 return null;
171
172 return [ radioname, _state.radios[radioname], netstate ];
173 }
174 }
175 }
176 }
177
178 return null;
179 }
180
181 function getWifiStateByIfname(ifname) {
182 for (var radioname in _state.radios) {
183 for (var i = 0; i &lt; _state.radios[radioname].interfaces.length; i++) {
184 var netstate = _state.radios[radioname].interfaces[i];
185
186 if (typeof(netstate.ifname) != 'string')
187 continue;
188
189 if (netstate.ifname == ifname)
190 return [ radioname, _state.radios[radioname], netstate ];
191 }
192 }
193
194 return null;
195 }
196
197 function isWifiIfname(ifname) {
198 for (var i = 0; i &lt; iface_patterns_wireless.length; i++)
199 if (iface_patterns_wireless[i].test(ifname))
200 return true;
201
202 return false;
203 }
204
205 function getWifiSidByNetid(netid) {
206 var m = /^(\w+)\.network(\d+)$/.exec(netid);
207 if (m) {
208 var sections = uci.sections('wireless', 'wifi-iface');
209 for (var i = 0, n = 0; i &lt; sections.length; i++) {
210 if (sections[i].device != m[1])
211 continue;
212
213 if (++n == +m[2])
214 return sections[i]['.name'];
215 }
216 }
217
218 return null;
219 }
220
221 function getWifiSidByIfname(ifname) {
222 var sid = getWifiSidByNetid(ifname);
223
224 if (sid != null)
225 return sid;
226
227 var res = getWifiStateByIfname(ifname);
228
229 if (res != null &amp;&amp; L.isObject(res[2]) &amp;&amp; typeof(res[2].section) == 'string')
230 return res[2].section;
231
232 return null;
233 }
234
235 function getWifiNetidBySid(sid) {
236 var s = uci.get('wireless', sid);
237 if (s != null &amp;&amp; s['.type'] == 'wifi-iface') {
238 var radioname = s.device;
239 if (typeof(s.device) == 'string') {
240 var i = 0, netid = null, sections = uci.sections('wireless', 'wifi-iface');
241 for (var i = 0, n = 0; i &lt; sections.length; i++) {
242 if (sections[i].device != s.device)
243 continue;
244
245 n++;
246
247 if (sections[i]['.name'] != s['.name'])
248 continue;
249
250 return [ '%s.network%d'.format(s.device, n), s.device ];
251 }
252
253 }
254 }
255
256 return null;
257 }
258
259 function getWifiNetidByNetname(name) {
260 var sections = uci.sections('wireless', 'wifi-iface');
261 for (var i = 0; i &lt; sections.length; i++) {
262 if (typeof(sections[i].network) != 'string')
263 continue;
264
265 var nets = sections[i].network.split(/\s+/);
266 for (var j = 0; j &lt; nets.length; j++) {
267 if (nets[j] != name)
268 continue;
269
270 return getWifiNetidBySid(sections[i]['.name']);
271 }
272 }
273
274 return null;
275 }
276
277 function isVirtualIfname(ifname) {
278 for (var i = 0; i &lt; iface_patterns_virtual.length; i++)
279 if (iface_patterns_virtual[i].test(ifname))
280 return true;
281
282 return false;
283 }
284
285 function isIgnoredIfname(ifname) {
286 for (var i = 0; i &lt; iface_patterns_ignore.length; i++)
287 if (iface_patterns_ignore[i].test(ifname))
288 return true;
289
290 return false;
291 }
292
293 function appendValue(config, section, option, value) {
294 var values = uci.get(config, section, option),
295 isArray = Array.isArray(values),
296 rv = false;
297
298 if (isArray == false)
299 values = L.toArray(values);
300
301 if (values.indexOf(value) == -1) {
302 values.push(value);
303 rv = true;
304 }
305
306 uci.set(config, section, option, isArray ? values : values.join(' '));
307
308 return rv;
309 }
310
311 function removeValue(config, section, option, value) {
312 var values = uci.get(config, section, option),
313 isArray = Array.isArray(values),
314 rv = false;
315
316 if (isArray == false)
317 values = L.toArray(values);
318
319 for (var i = values.length - 1; i >= 0; i--) {
320 if (values[i] == value) {
321 values.splice(i, 1);
322 rv = true;
323 }
324 }
325
326 if (values.length > 0)
327 uci.set(config, section, option, isArray ? values : values.join(' '));
328 else
329 uci.unset(config, section, option);
330
331 return rv;
332 }
333
334 function prefixToMask(bits, v6) {
335 var w = v6 ? 128 : 32,
336 m = [];
337
338 if (bits > w)
339 return null;
340
341 for (var i = 0; i &lt; w / 16; i++) {
342 var b = Math.min(16, bits);
343 m.push((0xffff &lt;&lt; (16 - b)) &amp; 0xffff);
344 bits -= b;
345 }
346
347 if (v6)
348 return String.prototype.format.apply('%x:%x:%x:%x:%x:%x:%x:%x', m).replace(/:0(?::0)+$/, '::');
349 else
350 return '%d.%d.%d.%d'.format(m[0] >>> 8, m[0] &amp; 0xff, m[1] >>> 8, m[1] &amp; 0xff);
351 }
352
353 function maskToPrefix(mask, v6) {
354 var m = v6 ? validation.parseIPv6(mask) : validation.parseIPv4(mask);
355
356 if (!m)
357 return null;
358
359 var bits = 0;
360
361 for (var i = 0, z = false; i &lt; m.length; i++) {
362 z = z || !m[i];
363
364 while (!z &amp;&amp; (m[i] &amp; (v6 ? 0x8000 : 0x80))) {
365 m[i] = (m[i] &lt;&lt; 1) &amp; (v6 ? 0xffff : 0xff);
366 bits++;
367 }
368
369 if (m[i])
370 return null;
371 }
372
373 return bits;
374 }
375
376 function initNetworkState(refresh) {
377 if (_state == null || refresh) {
378 _init = _init || Promise.all([
379 L.resolveDefault(callNetworkInterfaceDump(), []),
380 L.resolveDefault(callLuciBoardJSON(), {}),
381 L.resolveDefault(callLuciNetworkDevices(), {}),
382 L.resolveDefault(callLuciWirelessDevices(), {}),
383 L.resolveDefault(callLuciHostHints(), {}),
384 getProtocolHandlers(),
385 uci.load(['network', 'wireless', 'luci'])
386 ]).then(function(data) {
387 var netifd_ifaces = data[0],
388 board_json = data[1],
389 luci_devs = data[2];
390
391 var s = {
392 isTunnel: {}, isBridge: {}, isSwitch: {}, isWifi: {},
393 ifaces: netifd_ifaces, radios: data[3], hosts: data[4],
394 netdevs: {}, bridges: {}, switches: {}
395 };
396
397 for (var name in luci_devs) {
398 var dev = luci_devs[name];
399
400 if (isVirtualIfname(name))
401 s.isTunnel[name] = true;
402
403 if (!s.isTunnel[name] &amp;&amp; isIgnoredIfname(name))
404 continue;
405
406 s.netdevs[name] = s.netdevs[name] || {
407 idx: dev.ifindex,
408 name: name,
409 rawname: name,
410 flags: dev.flags,
411 stats: dev.stats,
412 macaddr: dev.mac,
413 type: dev.type,
414 mtu: dev.mtu,
415 qlen: dev.qlen,
416 ipaddrs: [],
417 ip6addrs: []
418 };
419
420 if (Array.isArray(dev.ipaddrs))
421 for (var i = 0; i &lt; dev.ipaddrs.length; i++)
422 s.netdevs[name].ipaddrs.push(dev.ipaddrs[i].address + '/' + dev.ipaddrs[i].netmask);
423
424 if (Array.isArray(dev.ip6addrs))
425 for (var i = 0; i &lt; dev.ip6addrs.length; i++)
426 s.netdevs[name].ip6addrs.push(dev.ip6addrs[i].address + '/' + dev.ip6addrs[i].netmask);
427 }
428
429 for (var name in luci_devs) {
430 var dev = luci_devs[name];
431
432 if (!dev.bridge)
433 continue;
434
435 var b = {
436 name: name,
437 id: dev.id,
438 stp: dev.stp,
439 ifnames: []
440 };
441
442 for (var i = 0; dev.ports &amp;&amp; i &lt; dev.ports.length; i++) {
443 var subdev = s.netdevs[dev.ports[i]];
444
445 if (subdev == null)
446 continue;
447
448 b.ifnames.push(subdev);
449 subdev.bridge = b;
450 }
451
452 s.bridges[name] = b;
453 s.isBridge[name] = true;
454 }
455
456 if (L.isObject(board_json.switch)) {
457 for (var switchname in board_json.switch) {
458 var layout = board_json.switch[switchname],
459 netdevs = {},
460 nports = {},
461 ports = [],
462 pnum = null,
463 role = null;
464
465 if (L.isObject(layout) &amp;&amp; Array.isArray(layout.ports)) {
466 for (var i = 0, port; (port = layout.ports[i]) != null; i++) {
467 if (typeof(port) == 'object' &amp;&amp; typeof(port.num) == 'number' &amp;&amp;
468 (typeof(port.role) == 'string' || typeof(port.device) == 'string')) {
469 var spec = {
470 num: port.num,
471 role: port.role || 'cpu',
472 index: (port.index != null) ? port.index : port.num
473 };
474
475 if (port.device != null) {
476 spec.device = port.device;
477 spec.tagged = spec.need_tag;
478 netdevs[port.num] = port.device;
479 }
480
481 ports.push(spec);
482
483 if (port.role != null)
484 nports[port.role] = (nports[port.role] || 0) + 1;
485 }
486 }
487
488 ports.sort(function(a, b) {
489 if (a.role != b.role)
490 return (a.role &lt; b.role) ? -1 : 1;
491
492 return (a.index - b.index);
493 });
494
495 for (var i = 0, port; (port = ports[i]) != null; i++) {
496 if (port.role != role) {
497 role = port.role;
498 pnum = 1;
499 }
500
501 if (role == 'cpu')
502 port.label = 'CPU (%s)'.format(port.device);
503 else if (nports[role] > 1)
504 port.label = '%s %d'.format(role.toUpperCase(), pnum++);
505 else
506 port.label = role.toUpperCase();
507
508 delete port.role;
509 delete port.index;
510 }
511
512 s.switches[switchname] = {
513 ports: ports,
514 netdevs: netdevs
515 };
516 }
517 }
518 }
519
520 if (L.isObject(board_json.dsl) &amp;&amp; L.isObject(board_json.dsl.modem)) {
521 s.hasDSLModem = board_json.dsl.modem;
522 }
523
524 _init = null;
525
526 return (_state = s);
527 });
528 }
529
530 return (_state != null ? Promise.resolve(_state) : _init);
531 }
532
533 function ifnameOf(obj) {
534 if (obj instanceof Protocol)
535 return obj.getIfname();
536 else if (obj instanceof Device)
537 return obj.getName();
538 else if (obj instanceof WifiDevice)
539 return obj.getName();
540 else if (obj instanceof WifiNetwork)
541 return obj.getIfname();
542 else if (typeof(obj) == 'string')
543 return obj.replace(/:.+$/, '');
544
545 return null;
546 }
547
548 function networkSort(a, b) {
549 return a.getName() > b.getName();
550 }
551
552 function deviceSort(a, b) {
553 var typeWeigth = { wifi: 2, alias: 3 },
554 weightA = typeWeigth[a.getType()] || 1,
555 weightB = typeWeigth[b.getType()] || 1;
556
557 if (weightA != weightB)
558 return weightA - weightB;
559
560 return a.getName() > b.getName();
561 }
562
563 function formatWifiEncryption(enc) {
564 if (!L.isObject(enc))
565 return null;
566
567 if (!enc.enabled)
568 return 'None';
569
570 var ciphers = Array.isArray(enc.ciphers)
571 ? enc.ciphers.map(function(c) { return c.toUpperCase() }) : [ 'NONE' ];
572
573 if (Array.isArray(enc.wep)) {
574 var has_open = false,
575 has_shared = false;
576
577 for (var i = 0; i &lt; enc.wep.length; i++)
578 if (enc.wep[i] == 'open')
579 has_open = true;
580 else if (enc.wep[i] == 'shared')
581 has_shared = true;
582
583 if (has_open &amp;&amp; has_shared)
584 return 'WEP Open/Shared (%s)'.format(ciphers.join(', '));
585 else if (has_open)
586 return 'WEP Open System (%s)'.format(ciphers.join(', '));
587 else if (has_shared)
588 return 'WEP Shared Auth (%s)'.format(ciphers.join(', '));
589
590 return 'WEP';
591 }
592
593 if (Array.isArray(enc.wpa)) {
594 var versions = [],
595 suites = Array.isArray(enc.authentication)
596 ? enc.authentication.map(function(a) { return a.toUpperCase() }) : [ 'NONE' ];
597
598 for (var i = 0; i &lt; enc.wpa.length; i++)
599 switch (enc.wpa[i]) {
600 case 1:
601 versions.push('WPA');
602 break;
603
604 default:
605 versions.push('WPA%d'.format(enc.wpa[i]));
606 break;
607 }
608
609 if (versions.length > 1)
610 return 'mixed %s %s (%s)'.format(versions.join('/'), suites.join(', '), ciphers.join(', '));
611
612 return '%s %s (%s)'.format(versions[0], suites.join(', '), ciphers.join(', '));
613 }
614
615 return 'Unknown';
616 }
617
618 function enumerateNetworks() {
619 var uciInterfaces = uci.sections('network', 'interface'),
620 networks = {};
621
622 for (var i = 0; i &lt; uciInterfaces.length; i++)
623 networks[uciInterfaces[i]['.name']] = this.instantiateNetwork(uciInterfaces[i]['.name']);
624
625 for (var i = 0; i &lt; _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);
629
630 var rv = [];
631
632 for (var network in networks)
633 if (networks.hasOwnProperty(network))
634 rv.push(networks[network]);
635
636 rv.sort(networkSort);
637
638 return rv;
639 }
640
641
642 var Hosts, Network, Protocol, Device, WifiDevice, WifiNetwork;
643
644 /**
645 * @class
646 * @memberof LuCI
647 * @hideconstructor
648 * @classdesc
649 *
650 * The `LuCI.Network` class combines data from multiple `ubus` apis to
651 * provide an abstraction of the current network configuration state.
652 *
653 * It provides methods to enumerate interfaces and devices, to query
654 * current configuration details and to manipulate settings.
655 */
656 Network = L.Class.extend(/** @lends LuCI.Network.prototype */ {
657 /**
658 * Converts the given prefix size in bits to a netmask.
659 *
660 * @method
661 *
662 * @param {number} bits
663 * The prefix size in bits.
664 *
665 * @param {boolean} [v6=false]
666 * Whether to convert the bits value into an IPv4 netmask (`false`) or
667 * an IPv6 netmask (`true`).
668 *
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.
673 */
674 prefixToMask: prefixToMask,
675
676 /**
677 * Converts the given netmask to a prefix size in bits.
678 *
679 * @method
680 *
681 * @param {string} netmask
682 * The netmask to convert into a bit count.
683 *
684 * @param {boolean} [v6=false]
685 * Whether to parse the given netmask as IPv4 (`false`) or IPv6 (`true`)
686 * address.
687 *
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.
691 */
692 maskToPrefix: maskToPrefix,
693
694 /**
695 * An encryption entry describes active wireless encryption settings
696 * such as the used key management protocols, active ciphers and
697 * protocol versions.
698 *
699 * @typedef {Object&lt;string, boolean|Array&lt;number|string>>} LuCI.Network.WifiEncryption
700 * @memberof LuCI.Network
701 *
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.
706 *
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.
711 *
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.
717 *
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.
724 *
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
730 * WEP network.
731 */
732
733 /**
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)`.
737 *
738 * @method
739 *
740 * @param {LuCI.Network.WifiEncryption} encryption
741 * The wireless encryption entry to convert.
742 *
743 * @returns {null|string}
744 * Returns the description string for the given encryption entry or
745 * `null` if the given entry was invalid.
746 */
747 formatWifiEncryption: formatWifiEncryption,
748
749 /**
750 * Flushes the local network state cache and fetches updated information
751 * from the remote `ubus` apis.
752 *
753 * @returns {Promise&lt;Object>}
754 * Returns a promise resolving to the internal network state object.
755 */
756 flushCache: function() {
757 initNetworkState(true);
758 return _init;
759 },
760
761 /**
762 * Instantiates the given {@link LuCI.Network.Protocol Protocol} backend,
763 * optionally using the given network name.
764 *
765 * @param {string} protoname
766 * The protocol backend to use, e.g. `static` or `dhcp`.
767 *
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.
773 *
774 * @returns {null|LuCI.Network.Protocol}
775 * Returns the instantiated protocol backend class or `null` if the given
776 * protocol isn't known.
777 */
778 getProtocol: function(protoname, netname) {
779 var v = _protocols[protoname];
780 if (v != null)
781 return new v(netname || '__dummy__');
782
783 return null;
784 },
785
786 /**
787 * Obtains instances of all known {@link LuCI.Network.Protocol Protocol}
788 * backend classes.
789 *
790 * @returns {Array&lt;LuCI.Network.Protocol>}
791 * Returns an array of protocol class instances.
792 */
793 getProtocols: function() {
794 var rv = [];
795
796 for (var protoname in _protocols)
797 rv.push(new _protocols[protoname]('__dummy__'));
798
799 return rv;
800 },
801
802 /**
803 * Registers a new {@link LuCI.Network.Protocol Protocol} subclass
804 * with the given methods and returns the resulting subclass value.
805 *
806 * This functions internally calls
807 * {@link LuCI.Class.extend Class.extend()} on the `Network.Protocol`
808 * base class.
809 *
810 * @param {string} protoname
811 * The name of the new protocol to register.
812 *
813 * @param {Object&lt;string, *>} methods
814 * The member methods and values of the new `Protocol` subclass to
815 * be passed to {@link LuCI.Class.extend Class.extend()}.
816 *
817 * @returns {LuCI.Network.Protocol}
818 * Returns the new `Protocol` subclass.
819 */
820 registerProtocol: function(protoname, methods) {
821 var spec = L.isObject(_protospecs) ? _protospecs[protoname] : null;
822 var proto = Protocol.extend(Object.assign({
823 getI18n: function() {
824 return protoname;
825 },
826
827 isFloating: function() {
828 return false;
829 },
830
831 isVirtual: function() {
832 return (L.isObject(spec) &amp;&amp; spec.no_device == true);
833 },
834
835 renderFormOptions: function(section) {
836
837 }
838 }, methods, {
839 __init__: function(name) {
840 this.sid = name;
841 },
842
843 getProtocol: function() {
844 return protoname;
845 }
846 }));
847
848 _protocols[protoname] = proto;
849
850 return proto;
851 },
852
853 /**
854 * Registers a new regular expression pattern to recognize
855 * virtual interfaces.
856 *
857 * @param {RegExp} pat
858 * A `RegExp` instance to match a virtual interface name
859 * such as `6in4-wan` or `tun0`.
860 */
861 registerPatternVirtual: function(pat) {
862 iface_patterns_virtual.push(pat);
863 },
864
865 /**
866 * Registers a new human readable translation string for a `Protocol`
867 * error code.
868 *
869 * @param {string} code
870 * The `ubus` protocol error code to register a translation for, e.g.
871 * `NO_DEVICE`.
872 *
873 * @param {string} message
874 * The message to use as translation for the given protocol error code.
875 *
876 * @returns {boolean}
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.
880 */
881 registerErrorCode: function(code, message) {
882 if (typeof(code) == 'string' &amp;&amp;
883 typeof(message) == 'string' &amp;&amp;
884 !proto_errors.hasOwnProperty(code)) {
885 proto_errors[code] = message;
886 return true;
887 }
888
889 return false;
890 },
891
892 /**
893 * Adds a new network of the given name and update it with the given
894 * uci option values.
895 *
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.
898 *
899 * @param {string} name
900 * The name of the network to add. Must be in the format `[a-zA-Z0-9_]+`.
901 *
902 * @param {Object&lt;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.
905 *
906 * @returns {Promise&lt;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
910 * existed.
911 */
912 addNetwork: function(name, options) {
913 return this.getNetwork(name).then(L.bind(function(existingNetwork) {
914 if (name != null &amp;&amp; /^[a-zA-Z0-9_]+$/.test(name) &amp;&amp; existingNetwork == null) {
915 var sid = uci.add('network', 'interface', name);
916
917 if (sid != null) {
918 if (L.isObject(options))
919 for (var key in options)
920 if (options.hasOwnProperty(key))
921 uci.set('network', sid, key, options[key]);
922
923 return this.instantiateNetwork(sid);
924 }
925 }
926 else if (existingNetwork != null &amp;&amp; existingNetwork.isEmpty()) {
927 if (L.isObject(options))
928 for (var key in options)
929 if (options.hasOwnProperty(key))
930 existingNetwork.set(key, options[key]);
931
932 return existingNetwork;
933 }
934 }, this));
935 },
936
937 /**
938 * Get a {@link LuCI.Network.Protocol Protocol} instance describing
939 * the network with the given name.
940 *
941 * @param {string} name
942 * The logical interface name of the network get, e.g. `lan` or `wan`.
943 *
944 * @returns {Promise&lt;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.
948 */
949 getNetwork: function(name) {
950 return initNetworkState().then(L.bind(function() {
951 var section = (name != null) ? uci.get('network', name) : null;
952
953 if (section != null &amp;&amp; section['.type'] == 'interface') {
954 return this.instantiateNetwork(name);
955 }
956 else if (name != null) {
957 for (var i = 0; i &lt; _state.ifaces.length; i++)
958 if (_state.ifaces[i].interface == name)
959 return this.instantiateNetwork(name, _state.ifaces[i].proto);
960 }
961
962 return null;
963 }, this));
964 },
965
966 /**
967 * Gets an array containing all known networks.
968 *
969 * @returns {Promise&lt;Array&lt;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.
973 */
974 getNetworks: function() {
975 return initNetworkState().then(L.bind(enumerateNetworks, this));
976 },
977
978 /**
979 * Deletes the given network and its references from the network and
980 * firewall configuration.
981 *
982 * @param {string} name
983 * The name of the network to delete.
984 *
985 * @returns {Promise&lt;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.
989 */
990 deleteNetwork: function(name) {
991 var requireFirewall = Promise.resolve(L.require('firewall')).catch(function() {});
992
993 return Promise.all([ requireFirewall, initNetworkState() ]).then(function() {
994 var uciInterface = uci.get('network', name);
995
996 if (uciInterface != null &amp;&amp; uciInterface['.type'] == 'interface') {
997 uci.remove('network', name);
998
999 uci.sections('luci', 'ifstate', function(s) {
1000 if (s.interface == name)
1001 uci.remove('luci', s['.name']);
1002 });
1003
1004 uci.sections('network', 'alias', function(s) {
1005 if (s.interface == name)
1006 uci.remove('network', s['.name']);
1007 });
1008
1009 uci.sections('network', 'route', function(s) {
1010 if (s.interface == name)
1011 uci.remove('network', s['.name']);
1012 });
1013
1014 uci.sections('network', 'route6', function(s) {
1015 if (s.interface == name)
1016 uci.remove('network', s['.name']);
1017 });
1018
1019 uci.sections('wireless', 'wifi-iface', function(s) {
1020 var networks = L.toArray(s.network).filter(function(network) { return network != name });
1021
1022 if (networks.length > 0)
1023 uci.set('wireless', s['.name'], 'network', networks.join(' '));
1024 else
1025 uci.unset('wireless', s['.name'], 'network');
1026 });
1027
1028 if (L.firewall)
1029 return L.firewall.deleteNetwork(name).then(function() { return true });
1030
1031 return true;
1032 }
1033
1034 return false;
1035 });
1036 },
1037
1038 /**
1039 * Rename the given network and its references to a new name.
1040 *
1041 * @param {string} oldName
1042 * The current name of the network.
1043 *
1044 * @param {string} newName
1045 * The name to rename the network to, must be in the format
1046 * `[a-z-A-Z0-9_]+`.
1047 *
1048 * @returns {Promise&lt;boolean>}
1049 * Returns a promise resolving to either `true` if the network was
1050 * successfully renamed or `false` if the new name was invalid, if
1051 * a network with the new name already exists or if the network to
1052 * rename could not be found.
1053 */
1054 renameNetwork: function(oldName, newName) {
1055 return initNetworkState().then(function() {
1056 if (newName == null || !/^[a-zA-Z0-9_]+$/.test(newName) || uci.get('network', newName) != null)
1057 return false;
1058
1059 var oldNetwork = uci.get('network', oldName);
1060
1061 if (oldNetwork == null || oldNetwork['.type'] != 'interface')
1062 return false;
1063
1064 var sid = uci.add('network', 'interface', newName);
1065
1066 for (var key in oldNetwork)
1067 if (oldNetwork.hasOwnProperty(key) &amp;&amp; key.charAt(0) != '.')
1068 uci.set('network', sid, key, oldNetwork[key]);
1069
1070 uci.sections('luci', 'ifstate', function(s) {
1071 if (s.interface == oldName)
1072 uci.set('luci', s['.name'], 'interface', newName);
1073 });
1074
1075 uci.sections('network', 'alias', function(s) {
1076 if (s.interface == oldName)
1077 uci.set('network', s['.name'], 'interface', newName);
1078 });
1079
1080 uci.sections('network', 'route', function(s) {
1081 if (s.interface == oldName)
1082 uci.set('network', s['.name'], 'interface', newName);
1083 });
1084
1085 uci.sections('network', 'route6', function(s) {
1086 if (s.interface == oldName)
1087 uci.set('network', s['.name'], 'interface', newName);
1088 });
1089
1090 uci.sections('wireless', 'wifi-iface', function(s) {
1091 var networks = L.toArray(s.network).map(function(network) { return (network == oldName ? newName : network) });
1092
1093 if (networks.length > 0)
1094 uci.set('wireless', s['.name'], 'network', networks.join(' '));
1095 });
1096
1097 uci.remove('network', oldName);
1098
1099 return true;
1100 });
1101 },
1102
1103 /**
1104 * Get a {@link LuCI.Network.Device Device} instance describing the
1105 * given network device.
1106 *
1107 * @param {string} name
1108 * The name of the network device to get, e.g. `eth0` or `br-lan`.
1109 *
1110 * @returns {Promise&lt;null|LuCI.Network.Device>}
1111 * Returns a promise resolving to the `Device` instance describing
1112 * the network device or `null` if the given device name could not
1113 * be found.
1114 */
1115 getDevice: function(name) {
1116 return initNetworkState().then(L.bind(function() {
1117 if (name == null)
1118 return null;
1119
1120 if (_state.netdevs.hasOwnProperty(name) || isWifiIfname(name))
1121 return this.instantiateDevice(name);
1122
1123 var netid = getWifiNetidBySid(name);
1124 if (netid != null)
1125 return this.instantiateDevice(netid[0]);
1126
1127 return null;
1128 }, this));
1129 },
1130
1131 /**
1132 * Get a sorted list of all found network devices.
1133 *
1134 * @returns {Promise&lt;Array&lt;LuCI.Network.Device>>}
1135 * Returns a promise resolving to a sorted array of `Device` class
1136 * instances describing the network devices found on the system.
1137 */
1138 getDevices: function() {
1139 return initNetworkState().then(L.bind(function() {
1140 var devices = {};
1141
1142 /* find simple devices */
1143 var uciInterfaces = uci.sections('network', 'interface');
1144 for (var i = 0; i &lt; uciInterfaces.length; i++) {
1145 var ifnames = L.toArray(uciInterfaces[i].ifname);
1146
1147 for (var j = 0; j &lt; ifnames.length; j++) {
1148 if (ifnames[j].charAt(0) == '@')
1149 continue;
1150
1151 if (isIgnoredIfname(ifnames[j]) || isVirtualIfname(ifnames[j]) || isWifiIfname(ifnames[j]))
1152 continue;
1153
1154 devices[ifnames[j]] = this.instantiateDevice(ifnames[j]);
1155 }
1156 }
1157
1158 for (var ifname in _state.netdevs) {
1159 if (devices.hasOwnProperty(ifname))
1160 continue;
1161
1162 if (isIgnoredIfname(ifname) || isVirtualIfname(ifname) || isWifiIfname(ifname))
1163 continue;
1164
1165 devices[ifname] = this.instantiateDevice(ifname);
1166 }
1167
1168 /* find VLAN devices */
1169 var uciSwitchVLANs = uci.sections('network', 'switch_vlan');
1170 for (var i = 0; i &lt; uciSwitchVLANs.length; i++) {
1171 if (typeof(uciSwitchVLANs[i].ports) != 'string' ||
1172 typeof(uciSwitchVLANs[i].device) != 'string' ||
1173 !_state.switches.hasOwnProperty(uciSwitchVLANs[i].device))
1174 continue;
1175
1176 var ports = uciSwitchVLANs[i].ports.split(/\s+/);
1177 for (var j = 0; j &lt; ports.length; j++) {
1178 var m = ports[j].match(/^(\d+)([tu]?)$/);
1179 if (m == null)
1180 continue;
1181
1182 var netdev = _state.switches[uciSwitchVLANs[i].device].netdevs[m[1]];
1183 if (netdev == null)
1184 continue;
1185
1186 if (!devices.hasOwnProperty(netdev))
1187 devices[netdev] = this.instantiateDevice(netdev);
1188
1189 _state.isSwitch[netdev] = true;
1190
1191 if (m[2] != 't')
1192 continue;
1193
1194 var vid = uciSwitchVLANs[i].vid || uciSwitchVLANs[i].vlan;
1195 vid = (vid != null ? +vid : null);
1196
1197 if (vid == null || vid &lt; 0 || vid > 4095)
1198 continue;
1199
1200 var vlandev = '%s.%d'.format(netdev, vid);
1201
1202 if (!devices.hasOwnProperty(vlandev))
1203 devices[vlandev] = this.instantiateDevice(vlandev);
1204
1205 _state.isSwitch[vlandev] = true;
1206 }
1207 }
1208
1209 /* find wireless interfaces */
1210 var uciWifiIfaces = uci.sections('wireless', 'wifi-iface'),
1211 networkCount = {};
1212
1213 for (var i = 0; i &lt; uciWifiIfaces.length; i++) {
1214 if (typeof(uciWifiIfaces[i].device) != 'string')
1215 continue;
1216
1217 networkCount[uciWifiIfaces[i].device] = (networkCount[uciWifiIfaces[i].device] || 0) + 1;
1218
1219 var netid = '%s.network%d'.format(uciWifiIfaces[i].device, networkCount[uciWifiIfaces[i].device]);
1220
1221 devices[netid] = this.instantiateDevice(netid);
1222 }
1223
1224 var rv = [];
1225
1226 for (var netdev in devices)
1227 if (devices.hasOwnProperty(netdev))
1228 rv.push(devices[netdev]);
1229
1230 rv.sort(deviceSort);
1231
1232 return rv;
1233 }, this));
1234 },
1235
1236 /**
1237 * Test if a given network device name is in the list of patterns for
1238 * device names to ignore.
1239 *
1240 * Ignored device names are usually Linux network devices which are
1241 * spawned implicitly by kernel modules such as `tunl0` or `hwsim0`
1242 * and which are unsuitable for use in network configuration.
1243 *
1244 * @param {string} name
1245 * The device name to test.
1246 *
1247 * @returns {boolean}
1248 * Returns `true` if the given name is in the ignore pattern list,
1249 * else returns `false`.
1250 */
1251 isIgnoredDevice: function(name) {
1252 return isIgnoredIfname(name);
1253 },
1254
1255 /**
1256 * Get a {@link LuCI.Network.WifiDevice WifiDevice} instance describing
1257 * the given wireless radio.
1258 *
1259 * @param {string} devname
1260 * The configuration name of the wireless radio to lookup, e.g. `radio0`
1261 * for the first mac80211 phy on the system.
1262 *
1263 * @returns {Promise&lt;null|LuCI.Network.WifiDevice>}
1264 * Returns a promise resolving to the `WifiDevice` instance describing
1265 * the underlying radio device or `null` if the wireless radio could not
1266 * be found.
1267 */
1268 getWifiDevice: function(devname) {
1269 return initNetworkState().then(L.bind(function() {
1270 var existingDevice = uci.get('wireless', devname);
1271
1272 if (existingDevice == null || existingDevice['.type'] != 'wifi-device')
1273 return null;
1274
1275 return this.instantiateWifiDevice(devname, _state.radios[devname] || {});
1276 }, this));
1277 },
1278
1279 /**
1280 * Obtain a list of all configured radio devices.
1281 *
1282 * @returns {Promise&lt;Array&lt;LuCI.Network.WifiDevice>>}
1283 * Returns a promise resolving to an array of `WifiDevice` instances
1284 * describing the wireless radios configured in the system.
1285 * The order of the array corresponds to the order of the radios in
1286 * the configuration.
1287 */
1288 getWifiDevices: function() {
1289 return initNetworkState().then(L.bind(function() {
1290 var uciWifiDevices = uci.sections('wireless', 'wifi-device'),
1291 rv = [];
1292
1293 for (var i = 0; i &lt; uciWifiDevices.length; i++) {
1294 var devname = uciWifiDevices[i]['.name'];
1295 rv.push(this.instantiateWifiDevice(devname, _state.radios[devname] || {}));
1296 }
1297
1298 return rv;
1299 }, this));
1300 },
1301
1302 /**
1303 * Get a {@link LuCI.Network.WifiNetwork WifiNetwork} instance describing
1304 * the given wireless network.
1305 *
1306 * @param {string} netname
1307 * The name of the wireless network to lookup. This may be either an uci
1308 * configuration section ID, a network ID in the form `radio#.network#`
1309 * or a Linux network device name like `wlan0` which is resolved to the
1310 * corresponding configuration section through `ubus` runtime information.
1311 *
1312 * @returns {Promise&lt;null|LuCI.Network.WifiNetwork>}
1313 * Returns a promise resolving to the `WifiNetwork` instance describing
1314 * the wireless network or `null` if the corresponding network could not
1315 * be found.
1316 */
1317 getWifiNetwork: function(netname) {
1318 var sid, res, netid, radioname, radiostate, netstate;
1319
1320 return initNetworkState().then(L.bind(function() {
1321 sid = getWifiSidByNetid(netname);
1322
1323 if (sid != null) {
1324 res = getWifiStateBySid(sid);
1325 netid = netname;
1326 radioname = res ? res[0] : null;
1327 radiostate = res ? res[1] : null;
1328 netstate = res ? res[2] : null;
1329 }
1330 else {
1331 res = getWifiStateByIfname(netname);
1332
1333 if (res != null) {
1334 radioname = res[0];
1335 radiostate = res[1];
1336 netstate = res[2];
1337 sid = netstate.section;
1338 netid = L.toArray(getWifiNetidBySid(sid))[0];
1339 }
1340 else {
1341 res = getWifiStateBySid(netname);
1342
1343 if (res != null) {
1344 radioname = res[0];
1345 radiostate = res[1];
1346 netstate = res[2];
1347 sid = netname;
1348 netid = L.toArray(getWifiNetidBySid(sid))[0];
1349 }
1350 else {
1351 res = getWifiNetidBySid(netname);
1352
1353 if (res != null) {
1354 netid = res[0];
1355 radioname = res[1];
1356 sid = netname;
1357 }
1358 }
1359 }
1360 }
1361
1362 return this.instantiateWifiNetwork(sid || netname, radioname, radiostate, netid, netstate);
1363 }, this));
1364 },
1365
1366 /**
1367 * Adds a new wireless network to the configuration and sets its options
1368 * to the provided values.
1369 *
1370 * @param {Object&lt;string, string|string[]>} options
1371 * The options to set for the newly added wireless network. This object
1372 * must at least contain a `device` property which is set to the radio
1373 * name the new network belongs to.
1374 *
1375 * @returns {Promise&lt;null|LuCI.Network.WifiNetwork>}
1376 * Returns a promise resolving to a `WifiNetwork` instance describing
1377 * the newly added wireless network or `null` if the given options
1378 * were invalid or if the associated radio device could not be found.
1379 */
1380 addWifiNetwork: function(options) {
1381 return initNetworkState().then(L.bind(function() {
1382 if (options == null ||
1383 typeof(options) != 'object' ||
1384 typeof(options.device) != 'string')
1385 return null;
1386
1387 var existingDevice = uci.get('wireless', options.device);
1388 if (existingDevice == null || existingDevice['.type'] != 'wifi-device')
1389 return null;
1390
1391 /* XXX: need to add a named section (wifinet#) here */
1392 var sid = uci.add('wireless', 'wifi-iface');
1393 for (var key in options)
1394 if (options.hasOwnProperty(key))
1395 uci.set('wireless', sid, key, options[key]);
1396
1397 var radioname = existingDevice['.name'],
1398 netid = getWifiNetidBySid(sid) || [];
1399
1400 return this.instantiateWifiNetwork(sid, radioname, _state.radios[radioname], netid[0], null);
1401 }, this));
1402 },
1403
1404 /**
1405 * Deletes the given wireless network from the configuration.
1406 *
1407 * @param {string} netname
1408 * The name of the network to remove. This may be either a
1409 * network ID in the form `radio#.network#` or a Linux network device
1410 * name like `wlan0` which is resolved to the corresponding configuration
1411 * section through `ubus` runtime information.
1412 *
1413 * @returns {Promise&lt;boolean>}
1414 * Returns a promise resolving to `true` if the wireless network has been
1415 * successfully deleted from the configuration or `false` if it could not
1416 * be found.
1417 */
1418 deleteWifiNetwork: function(netname) {
1419 return initNetworkState().then(L.bind(function() {
1420 var sid = getWifiSidByIfname(netname);
1421
1422 if (sid == null)
1423 return false;
1424
1425 uci.remove('wireless', sid);
1426 return true;
1427 }, this));
1428 },
1429
1430 /* private */
1431 getStatusByRoute: function(addr, mask) {
1432 return initNetworkState().then(L.bind(function() {
1433 var rv = [];
1434
1435 for (var i = 0; i &lt; _state.ifaces.length; i++) {
1436 if (!Array.isArray(_state.ifaces[i].route))
1437 continue;
1438
1439 for (var j = 0; j &lt; _state.ifaces[i].route.length; j++) {
1440 if (typeof(_state.ifaces[i].route[j]) != 'object' ||
1441 typeof(_state.ifaces[i].route[j].target) != 'string' ||
1442 typeof(_state.ifaces[i].route[j].mask) != 'number')
1443 continue;
1444
1445 if (_state.ifaces[i].route[j].table)
1446 continue;
1447
1448 if (_state.ifaces[i].route[j].target != addr ||
1449 _state.ifaces[i].route[j].mask != mask)
1450 continue;
1451
1452 rv.push(_state.ifaces[i]);
1453 }
1454 }
1455
1456 return rv;
1457 }, this));
1458 },
1459
1460 /* private */
1461 getStatusByAddress: function(addr) {
1462 return initNetworkState().then(L.bind(function() {
1463 var rv = [];
1464
1465 for (var i = 0; i &lt; _state.ifaces.length; i++) {
1466 if (Array.isArray(_state.ifaces[i]['ipv4-address']))
1467 for (var j = 0; j &lt; _state.ifaces[i]['ipv4-address'].length; j++)
1468 if (typeof(_state.ifaces[i]['ipv4-address'][j]) == 'object' &amp;&amp;
1469 _state.ifaces[i]['ipv4-address'][j].address == addr)
1470 return _state.ifaces[i];
1471
1472 if (Array.isArray(_state.ifaces[i]['ipv6-address']))
1473 for (var j = 0; j &lt; _state.ifaces[i]['ipv6-address'].length; j++)
1474 if (typeof(_state.ifaces[i]['ipv6-address'][j]) == 'object' &amp;&amp;
1475 _state.ifaces[i]['ipv6-address'][j].address == addr)
1476 return _state.ifaces[i];
1477
1478 if (Array.isArray(_state.ifaces[i]['ipv6-prefix-assignment']))
1479 for (var j = 0; j &lt; _state.ifaces[i]['ipv6-prefix-assignment'].length; j++)
1480 if (typeof(_state.ifaces[i]['ipv6-prefix-assignment'][j]) == 'object' &amp;&amp;
1481 typeof(_state.ifaces[i]['ipv6-prefix-assignment'][j]['local-address']) == 'object' &amp;&amp;
1482 _state.ifaces[i]['ipv6-prefix-assignment'][j]['local-address'].address == addr)
1483 return _state.ifaces[i];
1484 }
1485
1486 return null;
1487 }, this));
1488 },
1489
1490 /**
1491 * Get IPv4 wan networks.
1492 *
1493 * This function looks up all networks having a default `0.0.0.0/0` route
1494 * and returns them as array.
1495 *
1496 * @returns {Promise&lt;Array&lt;LuCI.Network.Protocol>>}
1497 * Returns a promise resolving to an array of `Protocol` subclass
1498 * instances describing the found default route interfaces.
1499 */
1500 getWANNetworks: function() {
1501 return this.getStatusByRoute('0.0.0.0', 0).then(L.bind(function(statuses) {
1502 var rv = [];
1503
1504 for (var i = 0; i &lt; statuses.length; i++)
1505 rv.push(this.instantiateNetwork(statuses[i].interface, statuses[i].proto));
1506
1507 return rv;
1508 }, this));
1509 },
1510
1511 /**
1512 * Get IPv6 wan networks.
1513 *
1514 * This function looks up all networks having a default `::/0` route
1515 * and returns them as array.
1516 *
1517 * @returns {Promise&lt;Array&lt;LuCI.Network.Protocol>>}
1518 * Returns a promise resolving to an array of `Protocol` subclass
1519 * instances describing the found IPv6 default route interfaces.
1520 */
1521 getWAN6Networks: function() {
1522 return this.getStatusByRoute('::', 0).then(L.bind(function(statuses) {
1523 var rv = [];
1524
1525 for (var i = 0; i &lt; statuses.length; i++)
1526 rv.push(this.instantiateNetwork(statuses[i].interface, statuses[i].proto));
1527
1528 return rv;
1529 }, this));
1530 },
1531
1532 /**
1533 * Describes an swconfig switch topology by specifying the CPU
1534 * connections and external port labels of a switch.
1535 *
1536 * @typedef {Object&lt;string, Object|Array>} SwitchTopology
1537 * @memberof LuCI.Network
1538 *
1539 * @property {Object&lt;number, string>} netdevs
1540 * The `netdevs` property points to an object describing the CPU port
1541 * connections of the switch. The numeric key of the enclosed object is
1542 * the port number, the value contains the Linux network device name the
1543 * port is hardwired to.
1544 *
1545 * @property {Array&lt;Object&lt;string, boolean|number|string>>} ports
1546 * The `ports` property points to an array describing the populated
1547 * ports of the switch in the external label order. Each array item is
1548 * an object containg the following keys:
1549 * - `num` - the internal switch port number
1550 * - `label` - the label of the port, e.g. `LAN 1` or `CPU (eth0)`
1551 * - `device` - the connected Linux network device name (CPU ports only)
1552 * - `tagged` - a boolean indicating whether the port must be tagged to
1553 * function (CPU ports only)
1554 */
1555
1556 /**
1557 * Returns the topologies of all swconfig switches found on the system.
1558 *
1559 * @returns {Promise&lt;Object&lt;string, LuCI.Network.SwitchTopology>>}
1560 * Returns a promise resolving to an object containing the topologies
1561 * of each switch. The object keys correspond to the name of the switches
1562 * such as `switch0`, the values are
1563 * {@link LuCI.Network.SwitchTopology SwitchTopology} objects describing
1564 * the layout.
1565 */
1566 getSwitchTopologies: function() {
1567 return initNetworkState().then(function() {
1568 return _state.switches;
1569 });
1570 },
1571
1572 /* private */
1573 instantiateNetwork: function(name, proto) {
1574 if (name == null)
1575 return null;
1576
1577 proto = (proto == null ? uci.get('network', name, 'proto') : proto);
1578
1579 var protoClass = _protocols[proto] || Protocol;
1580 return new protoClass(name);
1581 },
1582
1583 /* private */
1584 instantiateDevice: function(name, network, extend) {
1585 if (extend != null)
1586 return new (Device.extend(extend))(name, network);
1587
1588 return new Device(name, network);
1589 },
1590
1591 /* private */
1592 instantiateWifiDevice: function(radioname, radiostate) {
1593 return new WifiDevice(radioname, radiostate);
1594 },
1595
1596 /* private */
1597 instantiateWifiNetwork: function(sid, radioname, radiostate, netid, netstate) {
1598 return new WifiNetwork(sid, radioname, radiostate, netid, netstate);
1599 },
1600
1601 /**
1602 * Obtains the the network device name of the given object.
1603 *
1604 * @param {LuCI.Network.Protocol|LuCI.Network.Device|LuCI.Network.WifiDevice|LuCI.Network.WifiNetwork|string} obj
1605 * The object to get the device name from.
1606 *
1607 * @returns {null|string}
1608 * Returns a string containing the device name or `null` if the given
1609 * object could not be converted to a name.
1610 */
1611 getIfnameOf: function(obj) {
1612 return ifnameOf(obj);
1613 },
1614
1615 /**
1616 * Queries the internal DSL modem type from board information.
1617 *
1618 * @returns {Promise&lt;null|string>}
1619 * Returns a promise resolving to the type of the internal modem
1620 * (e.g. `vdsl`) or to `null` if no internal modem is present.
1621 */
1622 getDSLModemType: function() {
1623 return initNetworkState().then(function() {
1624 return _state.hasDSLModem ? _state.hasDSLModem.type : null;
1625 });
1626 },
1627
1628 /**
1629 * Queries aggregated information about known hosts.
1630 *
1631 * This function aggregates information from various sources such as
1632 * DHCP lease databases, ARP and IPv6 neighbour entries, wireless
1633 * association list etc. and returns a {@link LuCI.Network.Hosts Hosts}
1634 * class instance describing the found hosts.
1635 *
1636 * @returns {Promise&lt;LuCI.Network.Hosts>}
1637 * Returns a `Hosts` instance describing host known on the system.
1638 */
1639 getHostHints: function() {
1640 return initNetworkState().then(function() {
1641 return new Hosts(_state.hosts);
1642 });
1643 }
1644 });
1645
1646 /**
1647 * @class
1648 * @memberof LuCI.Network
1649 * @hideconstructor
1650 * @classdesc
1651 *
1652 * The `LuCI.Network.Hosts` class encapsulates host information aggregated
1653 * from multiple sources and provides convenience functions to access the
1654 * host information by different criteria.
1655 */
1656 Hosts = L.Class.extend(/** @lends LuCI.Network.Hosts.prototype */ {
1657 __init__: function(hosts) {
1658 this.hosts = hosts;
1659 },
1660
1661 /**
1662 * Lookup the hostname associated with the given MAC address.
1663 *
1664 * @param {string} mac
1665 * The MAC address to lookup.
1666 *
1667 * @returns {null|string}
1668 * Returns the hostname associated with the given MAC or `null` if
1669 * no matching host could be found or if no hostname is known for
1670 * the corresponding host.
1671 */
1672 getHostnameByMACAddr: function(mac) {
1673 return this.hosts[mac] ? this.hosts[mac].name : null;
1674 },
1675
1676 /**
1677 * Lookup the IPv4 address associated with the given MAC address.
1678 *
1679 * @param {string} mac
1680 * The MAC address to lookup.
1681 *
1682 * @returns {null|string}
1683 * Returns the IPv4 address associated with the given MAC or `null` if
1684 * no matching host could be found or if no IPv4 address is known for
1685 * the corresponding host.
1686 */
1687 getIPAddrByMACAddr: function(mac) {
1688 return this.hosts[mac] ? this.hosts[mac].ipv4 : null;
1689 },
1690
1691 /**
1692 * Lookup the IPv6 address associated with the given MAC address.
1693 *
1694 * @param {string} mac
1695 * The MAC address to lookup.
1696 *
1697 * @returns {null|string}
1698 * Returns the IPv6 address associated with the given MAC or `null` if
1699 * no matching host could be found or if no IPv6 address is known for
1700 * the corresponding host.
1701 */
1702 getIP6AddrByMACAddr: function(mac) {
1703 return this.hosts[mac] ? this.hosts[mac].ipv6 : null;
1704 },
1705
1706 /**
1707 * Lookup the hostname associated with the given IPv4 address.
1708 *
1709 * @param {string} ipaddr
1710 * The IPv4 address to lookup.
1711 *
1712 * @returns {null|string}
1713 * Returns the hostname associated with the given IPv4 or `null` if
1714 * no matching host could be found or if no hostname is known for
1715 * the corresponding host.
1716 */
1717 getHostnameByIPAddr: function(ipaddr) {
1718 for (var mac in this.hosts)
1719 if (this.hosts[mac].ipv4 == ipaddr &amp;&amp; this.hosts[mac].name != null)
1720 return this.hosts[mac].name;
1721 return null;
1722 },
1723
1724 /**
1725 * Lookup the MAC address associated with the given IPv4 address.
1726 *
1727 * @param {string} ipaddr
1728 * The IPv4 address to lookup.
1729 *
1730 * @returns {null|string}
1731 * Returns the MAC address associated with the given IPv4 or `null` if
1732 * no matching host could be found or if no MAC address is known for
1733 * the corresponding host.
1734 */
1735 getMACAddrByIPAddr: function(ipaddr) {
1736 for (var mac in this.hosts)
1737 if (this.hosts[mac].ipv4 == ipaddr)
1738 return mac;
1739 return null;
1740 },
1741
1742 /**
1743 * Lookup the hostname associated with the given IPv6 address.
1744 *
1745 * @param {string} ipaddr
1746 * The IPv6 address to lookup.
1747 *
1748 * @returns {null|string}
1749 * Returns the hostname associated with the given IPv6 or `null` if
1750 * no matching host could be found or if no hostname is known for
1751 * the corresponding host.
1752 */
1753 getHostnameByIP6Addr: function(ip6addr) {
1754 for (var mac in this.hosts)
1755 if (this.hosts[mac].ipv6 == ip6addr &amp;&amp; this.hosts[mac].name != null)
1756 return this.hosts[mac].name;
1757 return null;
1758 },
1759
1760 /**
1761 * Lookup the MAC address associated with the given IPv6 address.
1762 *
1763 * @param {string} ipaddr
1764 * The IPv6 address to lookup.
1765 *
1766 * @returns {null|string}
1767 * Returns the MAC address associated with the given IPv6 or `null` if
1768 * no matching host could be found or if no MAC address is known for
1769 * the corresponding host.
1770 */
1771 getMACAddrByIP6Addr: function(ip6addr) {
1772 for (var mac in this.hosts)
1773 if (this.hosts[mac].ipv6 == ip6addr)
1774 return mac;
1775 return null;
1776 },
1777
1778 /**
1779 * Return an array of (MAC address, name hint) tuples sorted by
1780 * MAC address.
1781 *
1782 * @param {boolean} [preferIp6=false]
1783 * Whether to prefer IPv6 addresses (`true`) or IPv4 addresses (`false`)
1784 * as name hint when no hostname is known for a specific MAC address.
1785 *
1786 * @returns {Array&lt;Array&lt;string>>}
1787 * Returns an array of arrays containing a name hint for each found
1788 * MAC address on the system. The array is sorted ascending by MAC.
1789 *
1790 * Each item of the resulting array is a two element array with the
1791 * MAC being the first element and the name hint being the second
1792 * element. The name hint is either the hostname, an IPv4 or an IPv6
1793 * address related to the MAC address.
1794 *
1795 * If no hostname but both IPv4 and IPv6 addresses are known, the
1796 * `preferIP6` flag specifies whether the IPv6 or the IPv4 address
1797 * is used as hint.
1798 */
1799 getMACHints: function(preferIp6) {
1800 var rv = [];
1801 for (var mac in this.hosts) {
1802 var hint = this.hosts[mac].name ||
1803 this.hosts[mac][preferIp6 ? 'ipv6' : 'ipv4'] ||
1804 this.hosts[mac][preferIp6 ? 'ipv4' : 'ipv6'];
1805
1806 rv.push([mac, hint]);
1807 }
1808 return rv.sort(function(a, b) { return a[0] > b[0] });
1809 }
1810 });
1811
1812 /**
1813 * @class
1814 * @memberof LuCI.Network
1815 * @hideconstructor
1816 * @classdesc
1817 *
1818 * The `Network.Protocol` class serves as base for protocol specific
1819 * subclasses which describe logical UCI networks defined by `config
1820 * interface` sections in `/etc/config/network`.
1821 */
1822 Protocol = L.Class.extend(/** @lends LuCI.Network.Protocol.prototype */ {
1823 __init__: function(name) {
1824 this.sid = name;
1825 },
1826
1827 _get: function(opt) {
1828 var val = uci.get('network', this.sid, opt);
1829
1830 if (Array.isArray(val))
1831 return val.join(' ');
1832
1833 return val || '';
1834 },
1835
1836 _ubus: function(field) {
1837 for (var i = 0; i &lt; _state.ifaces.length; i++) {
1838 if (_state.ifaces[i].interface != this.sid)
1839 continue;
1840
1841 return (field != null ? _state.ifaces[i][field] : _state.ifaces[i]);
1842 }
1843 },
1844
1845 /**
1846 * Read the given UCI option value of this network.
1847 *
1848 * @param {string} opt
1849 * The UCI option name to read.
1850 *
1851 * @returns {null|string|string[]}
1852 * Returns the UCI option value or `null` if the requested option is
1853 * not found.
1854 */
1855 get: function(opt) {
1856 return uci.get('network', this.sid, opt);
1857 },
1858
1859 /**
1860 * Set the given UCI option of this network to the given value.
1861 *
1862 * @param {string} opt
1863 * The name of the UCI option to set.
1864 *
1865 * @param {null|string|string[]} val
1866 * The value to set or `null` to remove the given option from the
1867 * configuration.
1868 */
1869 set: function(opt, val) {
1870 return uci.set('network', this.sid, opt, val);
1871 },
1872
1873 /**
1874 * Get the associared Linux network device of this network.
1875 *
1876 * @returns {null|string}
1877 * Returns the name of the associated network device or `null` if
1878 * it could not be determined.
1879 */
1880 getIfname: function() {
1881 var ifname;
1882
1883 if (this.isFloating())
1884 ifname = this._ubus('l3_device');
1885 else
1886 ifname = this._ubus('device') || this._ubus('l3_device');
1887
1888 if (ifname != null)
1889 return ifname;
1890
1891 var res = getWifiNetidByNetname(this.sid);
1892 return (res != null ? res[0] : null);
1893 },
1894
1895 /**
1896 * Get the name of this network protocol class.
1897 *
1898 * This function will be overwritten by subclasses created by
1899 * {@link LuCI.Network#registerProtocol Network.registerProtocol()}.
1900 *
1901 * @abstract
1902 * @returns {string}
1903 * Returns the name of the network protocol implementation, e.g.
1904 * `static` or `dhcp`.
1905 */
1906 getProtocol: function() {
1907 return null;
1908 },
1909
1910 /**
1911 * Return a human readable description for the protcol, such as
1912 * `Static address` or `DHCP client`.
1913 *
1914 * This function should be overwritten by subclasses.
1915 *
1916 * @abstract
1917 * @returns {string}
1918 * Returns the description string.
1919 */
1920 getI18n: function() {
1921 switch (this.getProtocol()) {
1922 case 'none': return _('Unmanaged');
1923 case 'static': return _('Static address');
1924 case 'dhcp': return _('DHCP client');
1925 default: return _('Unknown');
1926 }
1927 },
1928
1929 /**
1930 * Get the type of the underlying interface.
1931 *
1932 * This function actually is a convenience wrapper around
1933 * `proto.get("type")` and is mainly used by other `LuCI.Network` code
1934 * to check whether the interface is declared as bridge in UCI.
1935 *
1936 * @returns {null|string}
1937 * Returns the value of the `type` option of the associated logical
1938 * interface or `null` if no `type` option is set.
1939 */
1940 getType: function() {
1941 return this._get('type');
1942 },
1943
1944 /**
1945 * Get the name of the associated logical interface.
1946 *
1947 * @returns {string}
1948 * Returns the logical interface name, such as `lan` or `wan`.
1949 */
1950 getName: function() {
1951 return this.sid;
1952 },
1953
1954 /**
1955 * Get the uptime of the logical interface.
1956 *
1957 * @returns {number}
1958 * Returns the uptime of the associated interface in seconds.
1959 */
1960 getUptime: function() {
1961 return this._ubus('uptime') || 0;
1962 },
1963
1964 /**
1965 * Get the logical interface expiry time in seconds.
1966 *
1967 * For protocols that have a concept of a lease, such as DHCP or
1968 * DHCPv6, this function returns the remaining time in seconds
1969 * until the lease expires.
1970 *
1971 * @returns {number}
1972 * Returns the amount of seconds until the lease expires or `-1`
1973 * if it isn't applicable to the associated protocol.
1974 */
1975 getExpiry: function() {
1976 var u = this._ubus('uptime'),
1977 d = this._ubus('data');
1978
1979 if (typeof(u) == 'number' &amp;&amp; d != null &amp;&amp;
1980 typeof(d) == 'object' &amp;&amp; typeof(d.leasetime) == 'number') {
1981 var r = d.leasetime - (u % d.leasetime);
1982 return (r > 0 ? r : 0);
1983 }
1984
1985 return -1;
1986 },
1987
1988 /**
1989 * Get the metric value of the logical interface.
1990 *
1991 * @returns {number}
1992 * Returns the current metric value used for device and network
1993 * routes spawned by the associated logical interface.
1994 */
1995 getMetric: function() {
1996 return this._ubus('metric') || 0;
1997 },
1998
1999 /**
2000 * Get the requested firewall zone name of the logical interface.
2001 *
2002 * Some protocol implementations request a specific firewall zone
2003 * to trigger inclusion of their resulting network devices into the
2004 * firewall rule set.
2005 *
2006 * @returns {null|string}
2007 * Returns the requested firewall zone name as published in the
2008 * `ubus` runtime information or `null` if the remote protocol
2009 * handler didn't request a zone.
2010 */
2011 getZoneName: function() {
2012 var d = this._ubus('data');
2013
2014 if (L.isObject(d) &amp;&amp; typeof(d.zone) == 'string')
2015 return d.zone;
2016
2017 return null;
2018 },
2019
2020 /**
2021 * Query the first (primary) IPv4 address of the logical interface.
2022 *
2023 * @returns {null|string}
2024 * Returns the primary IPv4 address registered by the protocol handler
2025 * or `null` if no IPv4 addresses were set.
2026 */
2027 getIPAddr: function() {
2028 var addrs = this._ubus('ipv4-address');
2029 return ((Array.isArray(addrs) &amp;&amp; addrs.length) ? addrs[0].address : null);
2030 },
2031
2032 /**
2033 * Query all IPv4 addresses of the logical interface.
2034 *
2035 * @returns {string[]}
2036 * Returns an array of IPv4 addresses in CIDR notation which have been
2037 * registered by the protocol handler. The order of the resulting array
2038 * follows the order of the addresses in `ubus` runtime information.
2039 */
2040 getIPAddrs: function() {
2041 var addrs = this._ubus('ipv4-address'),
2042 rv = [];
2043
2044 if (Array.isArray(addrs))
2045 for (var i = 0; i &lt; addrs.length; i++)
2046 rv.push('%s/%d'.format(addrs[i].address, addrs[i].mask));
2047
2048 return rv;
2049 },
2050
2051 /**
2052 * Query the first (primary) IPv4 netmask of the logical interface.
2053 *
2054 * @returns {null|string}
2055 * Returns the netmask of the primary IPv4 address registered by the
2056 * protocol handler or `null` if no IPv4 addresses were set.
2057 */
2058 getNetmask: function() {
2059 var addrs = this._ubus('ipv4-address');
2060 if (Array.isArray(addrs) &amp;&amp; addrs.length)
2061 return prefixToMask(addrs[0].mask, false);
2062 },
2063
2064 /**
2065 * Query the gateway (nexthop) of the default route associated with
2066 * this logical interface.
2067 *
2068 * @returns {string}
2069 * Returns a string containing the IPv4 nexthop address of the associated
2070 * default route or `null` if no default route was found.
2071 */
2072 getGatewayAddr: function() {
2073 var routes = this._ubus('route');
2074
2075 if (Array.isArray(routes))
2076 for (var i = 0; i &lt; routes.length; i++)
2077 if (typeof(routes[i]) == 'object' &amp;&amp;
2078 routes[i].target == '0.0.0.0' &amp;&amp;
2079 routes[i].mask == 0)
2080 return routes[i].nexthop;
2081
2082 return null;
2083 },
2084
2085 /**
2086 * Query the IPv4 DNS servers associated with the logical interface.
2087 *
2088 * @returns {string[]}
2089 * Returns an array of IPv4 DNS servers registered by the remote
2090 * protocol backend.
2091 */
2092 getDNSAddrs: function() {
2093 var addrs = this._ubus('dns-server'),
2094 rv = [];
2095
2096 if (Array.isArray(addrs))
2097 for (var i = 0; i &lt; addrs.length; i++)
2098 if (!/:/.test(addrs[i]))
2099 rv.push(addrs[i]);
2100
2101 return rv;
2102 },
2103
2104 /**
2105 * Query the first (primary) IPv6 address of the logical interface.
2106 *
2107 * @returns {null|string}
2108 * Returns the primary IPv6 address registered by the protocol handler
2109 * in CIDR notation or `null` if no IPv6 addresses were set.
2110 */
2111 getIP6Addr: function() {
2112 var addrs = this._ubus('ipv6-address');
2113
2114 if (Array.isArray(addrs) &amp;&amp; L.isObject(addrs[0]))
2115 return '%s/%d'.format(addrs[0].address, addrs[0].mask);
2116
2117 addrs = this._ubus('ipv6-prefix-assignment');
2118
2119 if (Array.isArray(addrs) &amp;&amp; L.isObject(addrs[0]) &amp;&amp; L.isObject(addrs[0]['local-address']))
2120 return '%s/%d'.format(addrs[0]['local-address'].address, addrs[0]['local-address'].mask);
2121
2122 return null;
2123 },
2124
2125 /**
2126 * Query all IPv6 addresses of the logical interface.
2127 *
2128 * @returns {string[]}
2129 * Returns an array of IPv6 addresses in CIDR notation which have been
2130 * registered by the protocol handler. The order of the resulting array
2131 * follows the order of the addresses in `ubus` runtime information.
2132 */
2133 getIP6Addrs: function() {
2134 var addrs = this._ubus('ipv6-address'),
2135 rv = [];
2136
2137 if (Array.isArray(addrs))
2138 for (var i = 0; i &lt; addrs.length; i++)
2139 if (L.isObject(addrs[i]))
2140 rv.push('%s/%d'.format(addrs[i].address, addrs[i].mask));
2141
2142 addrs = this._ubus('ipv6-prefix-assignment');
2143
2144 if (Array.isArray(addrs))
2145 for (var i = 0; i &lt; addrs.length; i++)
2146 if (L.isObject(addrs[i]) &amp;&amp; L.isObject(addrs[i]['local-address']))
2147 rv.push('%s/%d'.format(addrs[i]['local-address'].address, addrs[i]['local-address'].mask));
2148
2149 return rv;
2150 },
2151
2152 /**
2153 * Query the gateway (nexthop) of the IPv6 default route associated with
2154 * this logical interface.
2155 *
2156 * @returns {string}
2157 * Returns a string containing the IPv6 nexthop address of the associated
2158 * default route or `null` if no default route was found.
2159 */
2160 getGateway6Addr: function() {
2161 var routes = this._ubus('route');
2162
2163 if (Array.isArray(routes))
2164 for (var i = 0; i &lt; routes.length; i++)
2165 if (typeof(routes[i]) == 'object' &amp;&amp;
2166 routes[i].target == '::' &amp;&amp;
2167 routes[i].mask == 0)
2168 return routes[i].nexthop;
2169
2170 return null;
2171 },
2172
2173 /**
2174 * Query the IPv6 DNS servers associated with the logical interface.
2175 *
2176 * @returns {string[]}
2177 * Returns an array of IPv6 DNS servers registered by the remote
2178 * protocol backend.
2179 */
2180 getDNS6Addrs: function() {
2181 var addrs = this._ubus('dns-server'),
2182 rv = [];
2183
2184 if (Array.isArray(addrs))
2185 for (var i = 0; i &lt; addrs.length; i++)
2186 if (/:/.test(addrs[i]))
2187 rv.push(addrs[i]);
2188
2189 return rv;
2190 },
2191
2192 /**
2193 * Query the routed IPv6 prefix associated with the logical interface.
2194 *
2195 * @returns {null|string}
2196 * Returns the routed IPv6 prefix registered by the remote protocol
2197 * handler or `null` if no prefix is present.
2198 */
2199 getIP6Prefix: function() {
2200 var prefixes = this._ubus('ipv6-prefix');
2201
2202 if (Array.isArray(prefixes) &amp;&amp; L.isObject(prefixes[0]))
2203 return '%s/%d'.format(prefixes[0].address, prefixes[0].mask);
2204
2205 return null;
2206 },
2207
2208 /**
2209 * Query interface error messages published in `ubus` runtime state.
2210 *
2211 * Interface errors are emitted by remote protocol handlers if the setup
2212 * of the underlying logical interface failed, e.g. due to bad
2213 * configuration or network connectivity issues.
2214 *
2215 * This function will translate the found error codes to human readable
2216 * messages using the descriptions registered by
2217 * {@link LuCI.Network#registerErrorCode Network.registerErrorCode()}
2218 * and fall back to `"Unknown error (%s)"` where `%s` is replaced by the
2219 * error code in case no translation can be found.
2220 *
2221 * @returns {string[]}
2222 * Returns an array of translated interface error messages.
2223 */
2224 getErrors: function() {
2225 var errors = this._ubus('errors'),
2226 rv = null;
2227
2228 if (Array.isArray(errors)) {
2229 for (var i = 0; i &lt; errors.length; i++) {
2230 if (!L.isObject(errors[i]) || typeof(errors[i].code) != 'string')
2231 continue;
2232
2233 rv = rv || [];
2234 rv.push(proto_errors[errors[i].code] || _('Unknown error (%s)').format(errors[i].code));
2235 }
2236 }
2237
2238 return rv;
2239 },
2240
2241 /**
2242 * Checks whether the underlying logical interface is declared as bridge.
2243 *
2244 * @returns {boolean}
2245 * Returns `true` when the interface is declared with `option type bridge`
2246 * and when the associated protocol implementation is not marked virtual
2247 * or `false` when the logical interface is no bridge.
2248 */
2249 isBridge: function() {
2250 return (!this.isVirtual() &amp;&amp; this.getType() == 'bridge');
2251 },
2252
2253 /**
2254 * Get the name of the opkg package providing the protocol functionality.
2255 *
2256 * This function should be overwritten by protocol specific subclasses.
2257 *
2258 * @abstract
2259 *
2260 * @returns {string}
2261 * Returns the name of the opkg package required for the protocol to
2262 * function, e.g. `odhcp6c` for the `dhcpv6` prototocol.
2263 */
2264 getOpkgPackage: function() {
2265 return null;
2266 },
2267
2268 /**
2269 * Checks whether the protocol functionality is installed.
2270 *
2271 * This function exists for compatibility with old code, it always
2272 * returns `true`.
2273 *
2274 * @deprecated
2275 * @abstract
2276 *
2277 * @returns {boolean}
2278 * Returns `true` if the protocol support is installed, else `false`.
2279 */
2280 isInstalled: function() {
2281 return true;
2282 },
2283
2284 /**
2285 * Checks whether this protocol is "virtual".
2286 *
2287 * A "virtual" protocol is a protocol which spawns its own interfaces
2288 * on demand instead of using existing physical interfaces.
2289 *
2290 * Examples for virtual protocols are `6in4` which `gre` spawn tunnel
2291 * network device on startup, examples for non-virtual protcols are
2292 * `dhcp` or `static` which apply IP configuration to existing interfaces.
2293 *
2294 * This function should be overwritten by subclasses.
2295 *
2296 * @returns {boolean}
2297 * Returns a boolean indicating whether the underlying protocol spawns
2298 * dynamic interfaces (`true`) or not (`false`).
2299 */
2300 isVirtual: function() {
2301 return false;
2302 },
2303
2304 /**
2305 * Checks whether this protocol is "floating".
2306 *
2307 * A "floating" protocol is a protocol which spawns its own interfaces
2308 * on demand, like a virtual one but which relies on an existinf lower
2309 * level interface to initiate the connection.
2310 *
2311 * An example for such a protocol is "pppoe".
2312 *
2313 * This function exists for backwards compatibility with older code
2314 * but should not be used anymore.
2315 *
2316 * @deprecated
2317 * @returns {boolean}
2318 * Returns a boolean indicating whether this protocol is floating (`true`)
2319 * or not (`false`).
2320 */
2321 isFloating: function() {
2322 return false;
2323 },
2324
2325 /**
2326 * Checks whether this logical interface is dynamic.
2327 *
2328 * A dynamic interface is an interface which has been created at runtime,
2329 * e.g. as sub-interface of another interface, but which is not backed by
2330 * any user configuration. Such dynamic interfaces cannot be edited but
2331 * only brought down or restarted.
2332 *
2333 * @returns {boolean}
2334 * Returns a boolean indicating whether this interface is dynamic (`true`)
2335 * or not (`false`).
2336 */
2337 isDynamic: function() {
2338 return (this._ubus('dynamic') == true);
2339 },
2340
2341 /**
2342 * Checks whether this interface is an alias interface.
2343 *
2344 * Alias interfaces are interfaces layering on top of another interface
2345 * and are denoted by a special `@interfacename` notation in the
2346 * underlying `ifname` option.
2347 *
2348 * @returns {null|string}
2349 * Returns the name of the parent interface if this logical interface
2350 * is an alias or `null` if it is not an alias interface.
2351 */
2352 isAlias: function() {
2353 var ifnames = L.toArray(uci.get('network', this.sid, 'ifname')),
2354 parent = null;
2355
2356 for (var i = 0; i &lt; ifnames.length; i++)
2357 if (ifnames[i].charAt(0) == '@')
2358 parent = ifnames[i].substr(1);
2359 else if (parent != null)
2360 parent = null;
2361
2362 return parent;
2363 },
2364
2365 /**
2366 * Checks whether this logical interface is "empty", meaning that ut
2367 * has no network devices attached.
2368 *
2369 * @returns {boolean}
2370 * Returns `true` if this logical interface is empty, else `false`.
2371 */
2372 isEmpty: function() {
2373 if (this.isFloating())
2374 return false;
2375
2376 var empty = true,
2377 ifname = this._get('ifname');
2378
2379 if (ifname != null &amp;&amp; ifname.match(/\S+/))
2380 empty = false;
2381
2382 if (empty == true &amp;&amp; getWifiNetidBySid(this.sid) != null)
2383 empty = false;
2384
2385 return empty;
2386 },
2387
2388 /**
2389 * Checks whether this logical interface is configured and running.
2390 *
2391 * @returns {boolean}
2392 * Returns `true` when the interface is active or `false` when it is not.
2393 */
2394 isUp: function() {
2395 return (this._ubus('up') == true);
2396 },
2397
2398 /**
2399 * Add the given network device to the logical interface.
2400 *
2401 * @param {LuCI.Network.Protocol|LuCI.Network.Device|LuCI.Network.WifiDevice|LuCI.Network.WifiNetwork|string} device
2402 * The object or device name to add to the logical interface. In case the
2403 * given argument is not a string, it is resolved though the
2404 * {@link LuCI.Network#getIfnameOf Network.getIfnameOf()} function.
2405 *
2406 * @returns {boolean}
2407 * Returns `true` if the device name has been added or `false` if any
2408 * argument was invalid, if the device was already part of the logical
2409 * interface or if the logical interface is virtual.
2410 */
2411 addDevice: function(ifname) {
2412 ifname = ifnameOf(ifname);
2413
2414 if (ifname == null || this.isFloating())
2415 return false;
2416
2417 var wif = getWifiSidByIfname(ifname);
2418
2419 if (wif != null)
2420 return appendValue('wireless', wif, 'network', this.sid);
2421
2422 return appendValue('network', this.sid, 'ifname', ifname);
2423 },
2424
2425 /**
2426 * Remove the given network device from the logical interface.
2427 *
2428 * @param {LuCI.Network.Protocol|LuCI.Network.Device|LuCI.Network.WifiDevice|LuCI.Network.WifiNetwork|string} device
2429 * The object or device name to remove from the logical interface. In case
2430 * the given argument is not a string, it is resolved though the
2431 * {@link LuCI.Network#getIfnameOf Network.getIfnameOf()} function.
2432 *
2433 * @returns {boolean}
2434 * Returns `true` if the device name has been added or `false` if any
2435 * argument was invalid, if the device was already part of the logical
2436 * interface or if the logical interface is virtual.
2437 */
2438 deleteDevice: function(ifname) {
2439 var rv = false;
2440
2441 ifname = ifnameOf(ifname);
2442
2443 if (ifname == null || this.isFloating())
2444 return false;
2445
2446 var wif = getWifiSidByIfname(ifname);
2447
2448 if (wif != null)
2449 rv = removeValue('wireless', wif, 'network', this.sid);
2450
2451 if (removeValue('network', this.sid, 'ifname', ifname))
2452 rv = true;
2453
2454 return rv;
2455 },
2456
2457 /**
2458 * Returns the Linux network device associated with this logical
2459 * interface.
2460 *
2461 * @returns {LuCI.Network.Device}
2462 * Returns a `Network.Device` class instance representing the
2463 * expected Linux network device according to the configuration.
2464 */
2465 getDevice: function() {
2466 if (this.isVirtual()) {
2467 var ifname = '%s-%s'.format(this.getProtocol(), this.sid);
2468 _state.isTunnel[this.getProtocol() + '-' + this.sid] = true;
2469 return L.network.instantiateDevice(ifname, this);
2470 }
2471 else if (this.isBridge()) {
2472 var ifname = 'br-%s'.format(this.sid);
2473 _state.isBridge[ifname] = true;
2474 return new Device(ifname, this);
2475 }
2476 else {
2477 var ifnames = L.toArray(uci.get('network', this.sid, 'ifname'));
2478
2479 for (var i = 0; i &lt; ifnames.length; i++) {
2480 var m = ifnames[i].match(/^([^:/]+)/);
2481 return ((m &amp;&amp; m[1]) ? L.network.instantiateDevice(m[1], this) : null);
2482 }
2483
2484 ifname = getWifiNetidByNetname(this.sid);
2485
2486 return (ifname != null ? L.network.instantiateDevice(ifname[0], this) : null);
2487 }
2488 },
2489
2490 /**
2491 * Returns the layer 2 linux network device currently associated
2492 * with this logical interface.
2493 *
2494 * @returns {LuCI.Network.Device}
2495 * Returns a `Network.Device` class instance representing the Linux
2496 * network device currently associated with the logical interface.
2497 */
2498 getL2Device: function() {
2499 var ifname = this._ubus('device');
2500 return (ifname != null ? L.network.instantiateDevice(ifname, this) : null);
2501 },
2502
2503 /**
2504 * Returns the layer 3 linux network device currently associated
2505 * with this logical interface.
2506 *
2507 * @returns {LuCI.Network.Device}
2508 * Returns a `Network.Device` class instance representing the Linux
2509 * network device currently associated with the logical interface.
2510 */
2511 getL3Device: function() {
2512 var ifname = this._ubus('l3_device');
2513 return (ifname != null ? L.network.instantiateDevice(ifname, this) : null);
2514 },
2515
2516 /**
2517 * Returns a list of network sub-devices associated with this logical
2518 * interface.
2519 *
2520 * @returns {null|Array&lt;LuCI.Network.Device>}
2521 * Returns an array of of `Network.Device` class instances representing
2522 * the sub-devices attached to this logical interface or `null` if the
2523 * logical interface does not support sub-devices, e.g. because it is
2524 * virtual and not a bridge.
2525 */
2526 getDevices: function() {
2527 var rv = [];
2528
2529 if (!this.isBridge() &amp;&amp; !(this.isVirtual() &amp;&amp; !this.isFloating()))
2530 return null;
2531
2532 var ifnames = L.toArray(uci.get('network', this.sid, 'ifname'));
2533
2534 for (var i = 0; i &lt; ifnames.length; i++) {
2535 if (ifnames[i].charAt(0) == '@')
2536 continue;
2537
2538 var m = ifnames[i].match(/^([^:/]+)/);
2539 if (m != null)
2540 rv.push(L.network.instantiateDevice(m[1], this));
2541 }
2542
2543 var uciWifiIfaces = uci.sections('wireless', 'wifi-iface');
2544
2545 for (var i = 0; i &lt; uciWifiIfaces.length; i++) {
2546 if (typeof(uciWifiIfaces[i].device) != 'string')
2547 continue;
2548
2549 var networks = L.toArray(uciWifiIfaces[i].network);
2550
2551 for (var j = 0; j &lt; networks.length; j++) {
2552 if (networks[j] != this.sid)
2553 continue;
2554
2555 var netid = getWifiNetidBySid(uciWifiIfaces[i]['.name']);
2556
2557 if (netid != null)
2558 rv.push(L.network.instantiateDevice(netid[0], this));
2559 }
2560 }
2561
2562 rv.sort(deviceSort);
2563
2564 return rv;
2565 },
2566
2567 /**
2568 * Checks whether this logical interface contains the given device
2569 * object.
2570 *
2571 * @param {LuCI.Network.Protocol|LuCI.Network.Device|LuCI.Network.WifiDevice|LuCI.Network.WifiNetwork|string} device
2572 * The object or device name to check. In case the given argument is not
2573 * a string, it is resolved though the
2574 * {@link LuCI.Network#getIfnameOf Network.getIfnameOf()} function.
2575 *
2576 * @returns {boolean}
2577 * Returns `true` when this logical interface contains the given network
2578 * device or `false` if not.
2579 */
2580 containsDevice: function(ifname) {
2581 ifname = ifnameOf(ifname);
2582
2583 if (ifname == null)
2584 return false;
2585 else if (this.isVirtual() &amp;&amp; '%s-%s'.format(this.getProtocol(), this.sid) == ifname)
2586 return true;
2587 else if (this.isBridge() &amp;&amp; 'br-%s'.format(this.sid) == ifname)
2588 return true;
2589
2590 var ifnames = L.toArray(uci.get('network', this.sid, 'ifname'));
2591
2592 for (var i = 0; i &lt; ifnames.length; i++) {
2593 var m = ifnames[i].match(/^([^:/]+)/);
2594 if (m != null &amp;&amp; m[1] == ifname)
2595 return true;
2596 }
2597
2598 var wif = getWifiSidByIfname(ifname);
2599
2600 if (wif != null) {
2601 var networks = L.toArray(uci.get('wireless', wif, 'network'));
2602
2603 for (var i = 0; i &lt; networks.length; i++)
2604 if (networks[i] == this.sid)
2605 return true;
2606 }
2607
2608 return false;
2609 }
2610 });
2611
2612 /**
2613 * @class
2614 * @memberof LuCI.Network
2615 * @hideconstructor
2616 * @classdesc
2617 *
2618 * A `Network.Device` class instance represents an underlying Linux network
2619 * device and allows querying device details such as packet statistics or MTU.
2620 */
2621 Device = L.Class.extend(/** @lends LuCI.Network.Device.prototype */ {
2622 __init__: function(ifname, network) {
2623 var wif = getWifiSidByIfname(ifname);
2624
2625 if (wif != null) {
2626 var res = getWifiStateBySid(wif) || [],
2627 netid = getWifiNetidBySid(wif) || [];
2628
2629 this.wif = new WifiNetwork(wif, res[0], res[1], netid[0], res[2], { ifname: ifname });
2630 this.ifname = this.wif.getIfname();
2631 }
2632
2633 this.ifname = this.ifname || ifname;
2634 this.dev = _state.netdevs[this.ifname];
2635 this.network = network;
2636 },
2637
2638 _devstate: function(/* ... */) {
2639 var rv = this.dev;
2640
2641 for (var i = 0; i &lt; arguments.length; i++)
2642 if (L.isObject(rv))
2643 rv = rv[arguments[i]];
2644 else
2645 return null;
2646
2647 return rv;
2648 },
2649
2650 /**
2651 * Get the name of the network device.
2652 *
2653 * @returns {string}
2654 * Returns the name of the device, e.g. `eth0` or `wlan0`.
2655 */
2656 getName: function() {
2657 return (this.wif != null ? this.wif.getIfname() : this.ifname);
2658 },
2659
2660 /**
2661 * Get the MAC address of the device.
2662 *
2663 * @returns {null|string}
2664 * Returns the MAC address of the device or `null` if not applicable,
2665 * e.g. for non-ethernet tunnel devices.
2666 */
2667 getMAC: function() {
2668 var mac = this._devstate('macaddr');
2669 return mac ? mac.toUpperCase() : null;
2670 },
2671
2672 /**
2673 * Get the MTU of the device.
2674 *
2675 * @returns {number}
2676 * Returns the MTU of the device.
2677 */
2678 getMTU: function() {
2679 return this._devstate('mtu');
2680 },
2681
2682 /**
2683 * Get the IPv4 addresses configured on the device.
2684 *
2685 * @returns {string[]}
2686 * Returns an array of IPv4 address strings.
2687 */
2688 getIPAddrs: function() {
2689 var addrs = this._devstate('ipaddrs');
2690 return (Array.isArray(addrs) ? addrs : []);
2691 },
2692
2693 /**
2694 * Get the IPv6 addresses configured on the device.
2695 *
2696 * @returns {string[]}
2697 * Returns an array of IPv6 address strings.
2698 */
2699 getIP6Addrs: function() {
2700 var addrs = this._devstate('ip6addrs');
2701 return (Array.isArray(addrs) ? addrs : []);
2702 },
2703
2704 /**
2705 * Get the type of the device..
2706 *
2707 * @returns {string}
2708 * Returns a string describing the type of the network device:
2709 * - `alias` if it is an abstract alias device (`@` notation)
2710 * - `wifi` if it is a wireless interface (e.g. `wlan0`)
2711 * - `bridge` if it is a bridge device (e.g. `br-lan`)
2712 * - `tunnel` if it is a tun or tap device (e.g. `tun0`)
2713 * - `vlan` if it is a vlan device (e.g. `eth0.1`)
2714 * - `switch` if it is a switch device (e.g.`eth1` connected to switch0)
2715 * - `ethernet` for all other device types
2716 */
2717 getType: function() {
2718 if (this.ifname != null &amp;&amp; this.ifname.charAt(0) == '@')
2719 return 'alias';
2720 else if (this.wif != null || isWifiIfname(this.ifname))
2721 return 'wifi';
2722 else if (_state.isBridge[this.ifname])
2723 return 'bridge';
2724 else if (_state.isTunnel[this.ifname])
2725 return 'tunnel';
2726 else if (this.ifname.indexOf('.') > -1)
2727 return 'vlan';
2728 else if (_state.isSwitch[this.ifname])
2729 return 'switch';
2730 else
2731 return 'ethernet';
2732 },
2733
2734 /**
2735 * Get a short description string for the device.
2736 *
2737 * @returns {string}
2738 * Returns the device name for non-wifi devices or a string containing
2739 * the operation mode and SSID for wifi devices.
2740 */
2741 getShortName: function() {
2742 if (this.wif != null)
2743 return this.wif.getShortName();
2744
2745 return this.ifname;
2746 },
2747
2748 /**
2749 * Get a long description string for the device.
2750 *
2751 * @returns {string}
2752 * Returns a string containing the type description and device name
2753 * for non-wifi devices or operation mode and ssid for wifi ones.
2754 */
2755 getI18n: function() {
2756 if (this.wif != null) {
2757 return '%s: %s "%s"'.format(
2758 _('Wireless Network'),
2759 this.wif.getActiveMode(),
2760 this.wif.getActiveSSID() || this.wif.getActiveBSSID() || this.wif.getID() || '?');
2761 }
2762
2763 return '%s: "%s"'.format(this.getTypeI18n(), this.getName());
2764 },
2765
2766 /**
2767 * Get a string describing the device type.
2768 *
2769 * @returns {string}
2770 * Returns a string describing the type, e.g. "Wireless Adapter" or
2771 * "Bridge".
2772 */
2773 getTypeI18n: function() {
2774 switch (this.getType()) {
2775 case 'alias':
2776 return _('Alias Interface');
2777
2778 case 'wifi':
2779 return _('Wireless Adapter');
2780
2781 case 'bridge':
2782 return _('Bridge');
2783
2784 case 'switch':
2785 return _('Ethernet Switch');
2786
2787 case 'vlan':
2788 return (_state.isSwitch[this.ifname] ? _('Switch VLAN') : _('Software VLAN'));
2789
2790 case 'tunnel':
2791 return _('Tunnel Interface');
2792
2793 default:
2794 return _('Ethernet Adapter');
2795 }
2796 },
2797
2798 /**
2799 * Get the associated bridge ports of the device.
2800 *
2801 * @returns {null|Array&lt;LuCI.Network.Device>}
2802 * Returns an array of `Network.Device` instances representing the ports
2803 * (slave interfaces) of the bridge or `null` when this device isn't
2804 * a Linux bridge.
2805 */
2806 getPorts: function() {
2807 var br = _state.bridges[this.ifname],
2808 rv = [];
2809
2810 if (br == null || !Array.isArray(br.ifnames))
2811 return null;
2812
2813 for (var i = 0; i &lt; br.ifnames.length; i++)
2814 rv.push(L.network.instantiateDevice(br.ifnames[i].name));
2815
2816 rv.sort(deviceSort);
2817
2818 return rv;
2819 },
2820
2821 /**
2822 * Get the bridge ID
2823 *
2824 * @returns {null|string}
2825 * Returns the ID of this network bridge or `null` if this network
2826 * device is not a Linux bridge.
2827 */
2828 getBridgeID: function() {
2829 var br = _state.bridges[this.ifname];
2830 return (br != null ? br.id : null);
2831 },
2832
2833 /**
2834 * Get the bridge STP setting
2835 *
2836 * @returns {boolean}
2837 * Returns `true` when this device is a Linux bridge and has `stp`
2838 * enabled, else `false`.
2839 */
2840 getBridgeSTP: function() {
2841 var br = _state.bridges[this.ifname];
2842 return (br != null ? !!br.stp : false);
2843 },
2844
2845 /**
2846 * Checks whether this device is up.
2847 *
2848 * @returns {boolean}
2849 * Returns `true` when the associated device is running pr `false`
2850 * when it is down or absent.
2851 */
2852 isUp: function() {
2853 var up = this._devstate('flags', 'up');
2854
2855 if (up == null)
2856 up = (this.getType() == 'alias');
2857
2858 return up;
2859 },
2860
2861 /**
2862 * Checks whether this device is a Linux bridge.
2863 *
2864 * @returns {boolean}
2865 * Returns `true` when the network device is present and a Linux bridge,
2866 * else `false`.
2867 */
2868 isBridge: function() {
2869 return (this.getType() == 'bridge');
2870 },
2871
2872 /**
2873 * Checks whether this device is part of a Linux bridge.
2874 *
2875 * @returns {boolean}
2876 * Returns `true` when this network device is part of a bridge,
2877 * else `false`.
2878 */
2879 isBridgePort: function() {
2880 return (this._devstate('bridge') != null);
2881 },
2882
2883 /**
2884 * Get the amount of transmitted bytes.
2885 *
2886 * @returns {number}
2887 * Returns the amount of bytes transmitted by the network device.
2888 */
2889 getTXBytes: function() {
2890 var stat = this._devstate('stats');
2891 return (stat != null ? stat.tx_bytes || 0 : 0);
2892 },
2893
2894 /**
2895 * Get the amount of received bytes.
2896 *
2897 * @returns {number}
2898 * Returns the amount of bytes received by the network device.
2899 */
2900 getRXBytes: function() {
2901 var stat = this._devstate('stats');
2902 return (stat != null ? stat.rx_bytes || 0 : 0);
2903 },
2904
2905 /**
2906 * Get the amount of transmitted packets.
2907 *
2908 * @returns {number}
2909 * Returns the amount of packets transmitted by the network device.
2910 */
2911 getTXPackets: function() {
2912 var stat = this._devstate('stats');
2913 return (stat != null ? stat.tx_packets || 0 : 0);
2914 },
2915
2916 /**
2917 * Get the amount of received packets.
2918 *
2919 * @returns {number}
2920 * Returns the amount of packets received by the network device.
2921 */
2922 getRXPackets: function() {
2923 var stat = this._devstate('stats');
2924 return (stat != null ? stat.rx_packets || 0 : 0);
2925 },
2926
2927 /**
2928 * Get the primary logical interface this device is assigned to.
2929 *
2930 * @returns {null|LuCI.Network.Protocol}
2931 * Returns a `Network.Protocol` instance representing the logical
2932 * interface this device is attached to or `null` if it is not
2933 * assigned to any logical interface.
2934 */
2935 getNetwork: function() {
2936 return this.getNetworks()[0];
2937 },
2938
2939 /**
2940 * Get the logical interfaces this device is assigned to.
2941 *
2942 * @returns {Array&lt;LuCI.Network.Protocol>}
2943 * Returns an array of `Network.Protocol` instances representing the
2944 * logical interfaces this device is assigned to.
2945 */
2946 getNetworks: function() {
2947 if (this.networks == null) {
2948 this.networks = [];
2949
2950 var networks = enumerateNetworks.apply(L.network);
2951
2952 for (var i = 0; i &lt; networks.length; i++)
2953 if (networks[i].containsDevice(this.ifname) || networks[i].getIfname() == this.ifname)
2954 this.networks.push(networks[i]);
2955
2956 this.networks.sort(networkSort);
2957 }
2958
2959 return this.networks;
2960 },
2961
2962 /**
2963 * Get the related wireless network this device is related to.
2964 *
2965 * @returns {null|LuCI.Network.WifiNetwork}
2966 * Returns a `Network.WifiNetwork` instance representing the wireless
2967 * network corresponding to this network device or `null` if this device
2968 * is not a wireless device.
2969 */
2970 getWifiNetwork: function() {
2971 return (this.wif != null ? this.wif : null);
2972 }
2973 });
2974
2975 /**
2976 * @class
2977 * @memberof LuCI.Network
2978 * @hideconstructor
2979 * @classdesc
2980 *
2981 * A `Network.WifiDevice` class instance represents a wireless radio device
2982 * present on the system and provides wireless capability information as
2983 * well as methods for enumerating related wireless networks.
2984 */
2985 WifiDevice = L.Class.extend(/** @lends LuCI.Network.WifiDevice.prototype */ {
2986 __init__: function(name, radiostate) {
2987 var uciWifiDevice = uci.get('wireless', name);
2988
2989 if (uciWifiDevice != null &amp;&amp;
2990 uciWifiDevice['.type'] == 'wifi-device' &amp;&amp;
2991 uciWifiDevice['.name'] != null) {
2992 this.sid = uciWifiDevice['.name'];
2993 }
2994
2995 this.sid = this.sid || name;
2996 this._ubusdata = {
2997 radio: name,
2998 dev: radiostate
2999 };
3000 },
3001
3002 /* private */
3003 ubus: function(/* ... */) {
3004 var v = this._ubusdata;
3005
3006 for (var i = 0; i &lt; arguments.length; i++)
3007 if (L.isObject(v))
3008 v = v[arguments[i]];
3009 else
3010 return null;
3011
3012 return v;
3013 },
3014
3015 /**
3016 * Read the given UCI option value of this wireless device.
3017 *
3018 * @param {string} opt
3019 * The UCI option name to read.
3020 *
3021 * @returns {null|string|string[]}
3022 * Returns the UCI option value or `null` if the requested option is
3023 * not found.
3024 */
3025 get: function(opt) {
3026 return uci.get('wireless', this.sid, opt);
3027 },
3028
3029 /**
3030 * Set the given UCI option of this network to the given value.
3031 *
3032 * @param {string} opt
3033 * The name of the UCI option to set.
3034 *
3035 * @param {null|string|string[]} val
3036 * The value to set or `null` to remove the given option from the
3037 * configuration.
3038 */
3039 set: function(opt, value) {
3040 return uci.set('wireless', this.sid, opt, value);
3041 },
3042
3043 /**
3044 * Checks whether this wireless radio is disabled.
3045 *
3046 * @returns {boolean}
3047 * Returns `true` when the wireless radio is marked as disabled in `ubus`
3048 * runtime state or when the `disabled` option is set in the corresponding
3049 * UCI configuration.
3050 */
3051 isDisabled: function() {
3052 return this.ubus('dev', 'disabled') || this.get('disabled') == '1';
3053 },
3054
3055 /**
3056 * Get the configuration name of this wireless radio.
3057 *
3058 * @returns {string}
3059 * Returns the UCI section name (e.g. `radio0`) of the corresponding
3060 * radio configuration which also serves as unique logical identifier
3061 * for the wireless phy.
3062 */
3063 getName: function() {
3064 return this.sid;
3065 },
3066
3067 /**
3068 * Gets a list of supported hwmodes.
3069 *
3070 * The hwmode values describe the frequency band and wireless standard
3071 * versions supported by the wireless phy.
3072 *
3073 * @returns {string[]}
3074 * Returns an array of valid hwmode values for this radio. Currently
3075 * known mode values are:
3076 * - `a` - Legacy 802.11a mode, 5 GHz, up to 54 Mbit/s
3077 * - `b` - Legacy 802.11b mode, 2.4 GHz, up to 11 Mbit/s
3078 * - `g` - Legacy 802.11g mode, 2.4 GHz, up to 54 Mbit/s
3079 * - `n` - IEEE 802.11n mode, 2.4 or 5 GHz, up to 600 Mbit/s
3080 * - `ac` - IEEE 802.11ac mode, 5 GHz, up to 6770 Mbit/s
3081 */
3082 getHWModes: function() {
3083 var hwmodes = this.ubus('dev', 'iwinfo', 'hwmodes');
3084 return Array.isArray(hwmodes) ? hwmodes : [ 'b', 'g' ];
3085 },
3086
3087 /**
3088 * Gets a list of supported htmodes.
3089 *
3090 * The htmode values describe the wide-frequency options supported by
3091 * the wireless phy.
3092 *
3093 * @returns {string[]}
3094 * Returns an array of valid htmode values for this radio. Currently
3095 * known mode values are:
3096 * - `HT20` - applicable to IEEE 802.11n, 20 MHz wide channels
3097 * - `HT40` - applicable to IEEE 802.11n, 40 MHz wide channels
3098 * - `VHT20` - applicable to IEEE 802.11ac, 20 MHz wide channels
3099 * - `VHT40` - applicable to IEEE 802.11ac, 40 MHz wide channels
3100 * - `VHT80` - applicable to IEEE 802.11ac, 80 MHz wide channels
3101 * - `VHT160` - applicable to IEEE 802.11ac, 160 MHz wide channels
3102 */
3103 getHTModes: function() {
3104 var htmodes = this.ubus('dev', 'iwinfo', 'htmodes');
3105 return (Array.isArray(htmodes) &amp;&amp; htmodes.length) ? htmodes : null;
3106 },
3107
3108 /**
3109 * Get a string describing the wireless radio hardware.
3110 *
3111 * @returns {string}
3112 * Returns the description string.
3113 */
3114 getI18n: function() {
3115 var hw = this.ubus('dev', 'iwinfo', 'hardware'),
3116 type = L.isObject(hw) ? hw.name : null;
3117
3118 if (this.ubus('dev', 'iwinfo', 'type') == 'wl')
3119 type = 'Broadcom';
3120
3121 var hwmodes = this.getHWModes(),
3122 modestr = '';
3123
3124 hwmodes.sort(function(a, b) {
3125 return (a.length != b.length ? a.length > b.length : a > b);
3126 });
3127
3128 modestr = hwmodes.join('');
3129
3130 return '%s 802.11%s Wireless Controller (%s)'.format(type || 'Generic', modestr, this.getName());
3131 },
3132
3133 /**
3134 * A wireless scan result object describes a neighbouring wireless
3135 * network found in the vincinity.
3136 *
3137 * @typedef {Object&lt;string, number|string|LuCI.Network.WifiEncryption>} WifiScanResult
3138 * @memberof LuCI.Network
3139 *
3140 * @property {string} ssid
3141 * The SSID / Mesh ID of the network.
3142 *
3143 * @property {string} bssid
3144 * The BSSID if the network.
3145 *
3146 * @property {string} mode
3147 * The operation mode of the network (`Master`, `Ad-Hoc`, `Mesh Point`).
3148 *
3149 * @property {number} channel
3150 * The wireless channel of the network.
3151 *
3152 * @property {number} signal
3153 * The received signal strength of the network in dBm.
3154 *
3155 * @property {number} quality
3156 * The numeric quality level of the signal, can be used in conjunction
3157 * with `quality_max` to calculate a quality percentage.
3158 *
3159 * @property {number} quality_max
3160 * The maximum possible quality level of the signal, can be used in
3161 * conjunction with `quality` to calculate a quality percentage.
3162 *
3163 * @property {LuCI.Network.WifiEncryption} encryption
3164 * The encryption used by the wireless network.
3165 */
3166
3167 /**
3168 * Trigger a wireless scan on this radio device and obtain a list of
3169 * nearby networks.
3170 *
3171 * @returns {Promise&lt;Array&lt;LuCI.Network.WifiScanResult>>}
3172 * Returns a promise resolving to an array of scan result objects
3173 * describing the networks found in the vincinity.
3174 */
3175 getScanList: function() {
3176 return callIwinfoScan(this.sid);
3177 },
3178
3179 /**
3180 * Check whether the wireless radio is marked as up in the `ubus`
3181 * runtime state.
3182 *
3183 * @returns {boolean}
3184 * Returns `true` when the radio device is up, else `false`.
3185 */
3186 isUp: function() {
3187 if (L.isObject(_state.radios[this.sid]))
3188 return (_state.radios[this.sid].up == true);
3189
3190 return false;
3191 },
3192
3193 /**
3194 * Get the wifi network of the given name belonging to this radio device
3195 *
3196 * @param {string} network
3197 * The name of the wireless network to lookup. This may be either an uci
3198 * configuration section ID, a network ID in the form `radio#.network#`
3199 * or a Linux network device name like `wlan0` which is resolved to the
3200 * corresponding configuration section through `ubus` runtime information.
3201 *
3202 * @returns {Promise&lt;LuCI.Network.WifiNetwork>}
3203 * Returns a promise resolving to a `Network.WifiNetwork` instance
3204 * representing the wireless network and rejecting with `null` if
3205 * the given network could not be found or is not associated with
3206 * this radio device.
3207 */
3208 getWifiNetwork: function(network) {
3209 return L.network.getWifiNetwork(network).then(L.bind(function(networkInstance) {
3210 var uciWifiIface = (networkInstance.sid ? uci.get('wireless', networkInstance.sid) : null);
3211
3212 if (uciWifiIface == null || uciWifiIface['.type'] != 'wifi-iface' || uciWifiIface.device != this.sid)
3213 return Promise.reject();
3214
3215 return networkInstance;
3216 }, this));
3217 },
3218
3219 /**
3220 * Get all wireless networks associated with this wireless radio device.
3221 *
3222 * @returns {Promise&lt;Array&lt;LuCI.Network.WifiNetwork>>}
3223 * Returns a promise resolving to an array of `Network.WifiNetwork`
3224 * instances respresenting the wireless networks associated with this
3225 * radio device.
3226 */
3227 getWifiNetworks: function() {
3228 var uciWifiIfaces = uci.sections('wireless', 'wifi-iface'),
3229 tasks = [];
3230
3231 for (var i = 0; i &lt; uciWifiIfaces.length; i++)
3232 if (uciWifiIfaces[i].device == this.sid)
3233 tasks.push(L.network.getWifiNetwork(uciWifiIfaces[i]['.name']));
3234
3235 return Promise.all(tasks);
3236 },
3237
3238 /**
3239 * Adds a new wireless network associated with this radio device to the
3240 * configuration and sets its options to the provided values.
3241 *
3242 * @param {Object&lt;string, string|string[]>} [options]
3243 * The options to set for the newly added wireless network.
3244 *
3245 * @returns {Promise&lt;null|LuCI.Network.WifiNetwork>}
3246 * Returns a promise resolving to a `WifiNetwork` instance describing
3247 * the newly added wireless network or `null` if the given options
3248 * were invalid.
3249 */
3250 addWifiNetwork: function(options) {
3251 if (!L.isObject(options))
3252 options = {};
3253
3254 options.device = this.sid;
3255
3256 return L.network.addWifiNetwork(options);
3257 },
3258
3259 /**
3260 * Deletes the wireless network with the given name associated with this
3261 * radio device.
3262 *
3263 * @param {string} network
3264 * The name of the wireless network to lookup. This may be either an uci
3265 * configuration section ID, a network ID in the form `radio#.network#`
3266 * or a Linux network device name like `wlan0` which is resolved to the
3267 * corresponding configuration section through `ubus` runtime information.
3268 *
3269 * @returns {Promise&lt;boolean>}
3270 * Returns a promise resolving to `true` when the wireless network was
3271 * successfully deleted from the configuration or `false` when the given
3272 * network could not be found or if the found network was not associated
3273 * with this wireless radio device.
3274 */
3275 deleteWifiNetwork: function(network) {
3276 var sid = null;
3277
3278 if (network instanceof WifiNetwork) {
3279 sid = network.sid;
3280 }
3281 else {
3282 var uciWifiIface = uci.get('wireless', network);
3283
3284 if (uciWifiIface == null || uciWifiIface['.type'] != 'wifi-iface')
3285 sid = getWifiSidByIfname(network);
3286 }
3287
3288 if (sid == null || uci.get('wireless', sid, 'device') != this.sid)
3289 return Promise.resolve(false);
3290
3291 uci.delete('wireless', network);
3292
3293 return Promise.resolve(true);
3294 }
3295 });
3296
3297 /**
3298 * @class
3299 * @memberof LuCI.Network
3300 * @hideconstructor
3301 * @classdesc
3302 *
3303 * A `Network.WifiNetwork` instance represents a wireless network (vif)
3304 * configured on top of a radio device and provides functions for querying
3305 * the runtime state of the network. Most radio devices support multiple
3306 * such networks in parallel.
3307 */
3308 WifiNetwork = L.Class.extend(/** @lends LuCI.Network.WifiNetwork.prototype */ {
3309 __init__: function(sid, radioname, radiostate, netid, netstate) {
3310 this.sid = sid;
3311 this.netid = netid;
3312 this._ubusdata = {
3313 radio: radioname,
3314 dev: radiostate,
3315 net: netstate
3316 };
3317 },
3318
3319 ubus: function(/* ... */) {
3320 var v = this._ubusdata;
3321
3322 for (var i = 0; i &lt; arguments.length; i++)
3323 if (L.isObject(v))
3324 v = v[arguments[i]];
3325 else
3326 return null;
3327
3328 return v;
3329 },
3330
3331 /**
3332 * Read the given UCI option value of this wireless network.
3333 *
3334 * @param {string} opt
3335 * The UCI option name to read.
3336 *
3337 * @returns {null|string|string[]}
3338 * Returns the UCI option value or `null` if the requested option is
3339 * not found.
3340 */
3341 get: function(opt) {
3342 return uci.get('wireless', this.sid, opt);
3343 },
3344
3345 /**
3346 * Set the given UCI option of this network to the given value.
3347 *
3348 * @param {string} opt
3349 * The name of the UCI option to set.
3350 *
3351 * @param {null|string|string[]} val
3352 * The value to set or `null` to remove the given option from the
3353 * configuration.
3354 */
3355 set: function(opt, value) {
3356 return uci.set('wireless', this.sid, opt, value);
3357 },
3358
3359 /**
3360 * Checks whether this wireless network is disabled.
3361 *
3362 * @returns {boolean}
3363 * Returns `true` when the wireless radio is marked as disabled in `ubus`
3364 * runtime state or when the `disabled` option is set in the corresponding
3365 * UCI configuration.
3366 */
3367 isDisabled: function() {
3368 return this.ubus('dev', 'disabled') || this.get('disabled') == '1';
3369 },
3370
3371 /**
3372 * Get the configured operation mode of the wireless network.
3373 *
3374 * @returns {string}
3375 * Returns the configured operation mode. Possible values are:
3376 * - `ap` - Master (Access Point) mode
3377 * - `sta` - Station (client) mode
3378 * - `adhoc` - Ad-Hoc (IBSS) mode
3379 * - `mesh` - Mesh (IEEE 802.11s) mode
3380 * - `monitor` - Monitor mode
3381 */
3382 getMode: function() {
3383 return this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3384 },
3385
3386 /**
3387 * Get the configured SSID of the wireless network.
3388 *
3389 * @returns {null|string}
3390 * Returns the configured SSID value or `null` when this network is
3391 * in mesh mode.
3392 */
3393 getSSID: function() {
3394 if (this.getMode() == 'mesh')
3395 return null;
3396
3397 return this.ubus('net', 'config', 'ssid') || this.get('ssid');
3398 },
3399
3400 /**
3401 * Get the configured Mesh ID of the wireless network.
3402 *
3403 * @returns {null|string}
3404 * Returns the configured mesh ID value or `null` when this network
3405 * is not in mesh mode.
3406 */
3407 getMeshID: function() {
3408 if (this.getMode() != 'mesh')
3409 return null;
3410
3411 return this.ubus('net', 'config', 'mesh_id') || this.get('mesh_id');
3412 },
3413
3414 /**
3415 * Get the configured BSSID of the wireless network.
3416 *
3417 * @returns {null|string}
3418 * Returns the BSSID value or `null` if none has been specified.
3419 */
3420 getBSSID: function() {
3421 return this.ubus('net', 'config', 'bssid') || this.get('bssid');
3422 },
3423
3424 /**
3425 * Get the names of the logical interfaces this wireless network is
3426 * attached to.
3427 *
3428 * @returns {string[]}
3429 * Returns an array of logical interface names.
3430 */
3431 getNetworkNames: function() {
3432 return L.toArray(this.ubus('net', 'config', 'network') || this.get('network'));
3433 },
3434
3435 /**
3436 * Get the internal network ID of this wireless network.
3437 *
3438 * The network ID is a LuCI specific identifer in the form
3439 * `radio#.network#` to identify wireless networks by their corresponding
3440 * radio and network index numbers.
3441 *
3442 * @returns {string}
3443 * Returns the LuCI specific network ID.
3444 */
3445 getID: function() {
3446 return this.netid;
3447 },
3448
3449 /**
3450 * Get the configuration ID of this wireless network.
3451 *
3452 * @returns {string}
3453 * Returns the corresponding UCI section ID of the network.
3454 */
3455 getName: function() {
3456 return this.sid;
3457 },
3458
3459 /**
3460 * Get the Linux network device name.
3461 *
3462 * @returns {null|string}
3463 * Returns the current Linux network device name as resolved from
3464 * `ubus` runtime information or `null` if this network has no
3465 * associated network device, e.g. when not configured or up.
3466 */
3467 getIfname: function() {
3468 var ifname = this.ubus('net', 'ifname') || this.ubus('net', 'iwinfo', 'ifname');
3469
3470 if (ifname == null || ifname.match(/^(wifi|radio)\d/))
3471 ifname = this.netid;
3472
3473 return ifname;
3474 },
3475
3476 /**
3477 * Get the name of the corresponding wifi radio device.
3478 *
3479 * @returns {null|string}
3480 * Returns the name of the radio device this network is configured on
3481 * or `null` if it cannot be determined.
3482 */
3483 getWifiDeviceName: function() {
3484 return this.ubus('radio') || this.get('device');
3485 },
3486
3487 /**
3488 * Get the corresponding wifi radio device.
3489 *
3490 * @returns {null|LuCI.Network.WifiDevice}
3491 * Returns a `Network.WifiDevice` instance representing the corresponding
3492 * wifi radio device or `null` if the related radio device could not be
3493 * found.
3494 */
3495 getWifiDevice: function() {
3496 var radioname = this.getWifiDeviceName();
3497
3498 if (radioname == null)
3499 return Promise.reject();
3500
3501 return L.network.getWifiDevice(radioname);
3502 },
3503
3504 /**
3505 * Check whether the radio network is up.
3506 *
3507 * This function actually queries the up state of the related radio
3508 * device and assumes this network to be up as well when the parent
3509 * radio is up. This is due to the fact that OpenWrt does not control
3510 * virtual interfaces individually but within one common hostapd
3511 * instance.
3512 *
3513 * @returns {boolean}
3514 * Returns `true` when the network is up, else `false`.
3515 */
3516 isUp: function() {
3517 var device = this.getDevice();
3518
3519 if (device == null)
3520 return false;
3521
3522 return device.isUp();
3523 },
3524
3525 /**
3526 * Query the current operation mode from runtime information.
3527 *
3528 * @returns {string}
3529 * Returns the human readable mode name as reported by `ubus` runtime
3530 * state. Possible returned values are:
3531 * - `Master`
3532 * - `Ad-Hoc`
3533 * - `Client`
3534 * - `Monitor`
3535 * - `Master (VLAN)`
3536 * - `WDS`
3537 * - `Mesh Point`
3538 * - `P2P Client`
3539 * - `P2P Go`
3540 * - `Unknown`
3541 */
3542 getActiveMode: function() {
3543 var mode = this.ubus('net', 'iwinfo', 'mode') || this.ubus('net', 'config', 'mode') || this.get('mode') || 'ap';
3544
3545 switch (mode) {
3546 case 'ap': return 'Master';
3547 case 'sta': return 'Client';
3548 case 'adhoc': return 'Ad-Hoc';
3549 case 'mesh': return 'Mesh';
3550 case 'monitor': return 'Monitor';
3551 default: return mode;
3552 }
3553 },
3554
3555 /**
3556 * Query the current operation mode from runtime information as
3557 * translated string.
3558 *
3559 * @returns {string}
3560 * Returns the translated, human readable mode name as reported by
3561 *`ubus` runtime state.
3562 */
3563 getActiveModeI18n: function() {
3564 var mode = this.getActiveMode();
3565
3566 switch (mode) {
3567 case 'Master': return _('Master');
3568 case 'Client': return _('Client');
3569 case 'Ad-Hoc': return _('Ad-Hoc');
3570 case 'Mash': return _('Mesh');
3571 case 'Monitor': return _('Monitor');
3572 default: return mode;
3573 }
3574 },
3575
3576 /**
3577 * Query the current SSID from runtime information.
3578 *
3579 * @returns {string}
3580 * Returns the current SSID or Mesh ID as reported by `ubus` runtime
3581 * information.
3582 */
3583 getActiveSSID: function() {
3584 return this.ubus('net', 'iwinfo', 'ssid') || this.ubus('net', 'config', 'ssid') || this.get('ssid');
3585 },
3586
3587 /**
3588 * Query the current BSSID from runtime information.
3589 *
3590 * @returns {string}
3591 * Returns the current BSSID or Mesh ID as reported by `ubus` runtime
3592 * information.
3593 */
3594 getActiveBSSID: function() {
3595 return this.ubus('net', 'iwinfo', 'bssid') || this.ubus('net', 'config', 'bssid') || this.get('bssid');
3596 },
3597
3598 /**
3599 * Query the current encryption settings from runtime information.
3600 *
3601 * @returns {string}
3602 * Returns a string describing the current encryption or `-` if the the
3603 * encryption state could not be found in `ubus` runtime information.
3604 */
3605 getActiveEncryption: function() {
3606 return formatWifiEncryption(this.ubus('net', 'iwinfo', 'encryption')) || '-';
3607 },
3608
3609 /**
3610 * A wireless peer entry describes the properties of a remote wireless
3611 * peer associated with a local network.
3612 *
3613 * @typedef {Object&lt;string, boolean|number|string|LuCI.Network.WifiRateEntry>} WifiPeerEntry
3614 * @memberof LuCI.Network
3615 *
3616 * @property {string} mac
3617 * The MAC address (BSSID).
3618 *
3619 * @property {number} signal
3620 * The received signal strength.
3621 *
3622 * @property {number} [signal_avg]
3623 * The average signal strength if supported by the driver.
3624 *
3625 * @property {number} [noise]
3626 * The current noise floor of the radio. May be `0` or absent if not
3627 * supported by the driver.
3628 *
3629 * @property {number} inactive
3630 * The amount of milliseconds the peer has been inactive, e.g. due
3631 * to powersave.
3632 *
3633 * @property {number} connected_time
3634 * The amount of milliseconds the peer is associated to this network.
3635 *
3636 * @property {number} [thr]
3637 * The estimated throughput of the peer, May be `0` or absent if not
3638 * supported by the driver.
3639 *
3640 * @property {boolean} authorized
3641 * Specifies whether the peer is authorized to associate to this network.
3642 *
3643 * @property {boolean} authenticated
3644 * Specifies whether the peer completed authentication to this network.
3645 *
3646 * @property {string} preamble
3647 * The preamble mode used by the peer. May be `long` or `short`.
3648 *
3649 * @property {boolean} wme
3650 * Specifies whether the peer supports WME/WMM capabilities.
3651 *
3652 * @property {boolean} mfp
3653 * Specifies whether management frame protection is active.
3654 *
3655 * @property {boolean} tdls
3656 * Specifies whether TDLS is active.
3657 *
3658 * @property {number} [mesh llid]
3659 * The mesh LLID, may be `0` or absent if not applicable or supported
3660 * by the driver.
3661 *
3662 * @property {number} [mesh plid]
3663 * The mesh PLID, may be `0` or absent if not applicable or supported
3664 * by the driver.
3665 *
3666 * @property {string} [mesh plink]
3667 * The mesh peer link state description, may be an empty string (`''`)
3668 * or absent if not applicable or supported by the driver.
3669 *
3670 * The following states are known:
3671 * - `LISTEN`
3672 * - `OPN_SNT`
3673 * - `OPN_RCVD`
3674 * - `CNF_RCVD`
3675 * - `ESTAB`
3676 * - `HOLDING`
3677 * - `BLOCKED`
3678 * - `UNKNOWN`
3679 *
3680 * @property {number} [mesh local PS]
3681 * The local powersafe mode for the peer link, may be an empty
3682 * string (`''`) or absent if not applicable or supported by
3683 * the driver.
3684 *
3685 * The following modes are known:
3686 * - `ACTIVE` (no power save)
3687 * - `LIGHT SLEEP`
3688 * - `DEEP SLEEP`
3689 * - `UNKNOWN`
3690 *
3691 * @property {number} [mesh peer PS]
3692 * The remote powersafe mode for the peer link, may be an empty
3693 * string (`''`) or absent if not applicable or supported by
3694 * the driver.
3695 *
3696 * The following modes are known:
3697 * - `ACTIVE` (no power save)
3698 * - `LIGHT SLEEP`
3699 * - `DEEP SLEEP`
3700 * - `UNKNOWN`
3701 *
3702 * @property {number} [mesh non-peer PS]
3703 * The powersafe mode for all non-peer neigbours, may be an empty
3704 * string (`''`) or absent if not applicable or supported by the driver.
3705 *
3706 * The following modes are known:
3707 * - `ACTIVE` (no power save)
3708 * - `LIGHT SLEEP`
3709 * - `DEEP SLEEP`
3710 * - `UNKNOWN`
3711 *
3712 * @property {LuCI.Network.WifiRateEntry} rx
3713 * Describes the receiving wireless rate from the peer.
3714 *
3715 * @property {LuCI.Network.WifiRateEntry} tx
3716 * Describes the transmitting wireless rate to the peer.
3717 */
3718
3719 /**
3720 * A wireless rate entry describes the properties of a wireless
3721 * transmission rate to or from a peer.
3722 *
3723 * @typedef {Object&lt;string, boolean|number>} WifiRateEntry
3724 * @memberof LuCI.Network
3725 *
3726 * @property {number} [drop_misc]
3727 * The amount of received misc. packages that have been dropped, e.g.
3728 * due to corruption or missing authentication. Only applicable to
3729 * receiving rates.
3730 *
3731 * @property {number} packets
3732 * The amount of packets that have been received or sent.
3733 *
3734 * @property {number} bytes
3735 * The amount of bytes that have been received or sent.
3736 *
3737 * @property {number} [failed]
3738 * The amount of failed tranmission attempts. Only applicable to
3739 * transmit rates.
3740 *
3741 * @property {number} [retries]
3742 * The amount of retried transmissions. Only applicable to transmit
3743 * rates.
3744 *
3745 * @property {boolean} is_ht
3746 * Specifies whether this rate is an HT (IEEE 802.11n) rate.
3747 *
3748 * @property {boolean} is_vht
3749 * Specifies whether this rate is an VHT (IEEE 802.11ac) rate.
3750 *
3751 * @property {number} mhz
3752 * The channel width in MHz used for the transmission.
3753 *
3754 * @property {number} rate
3755 * The bitrate in bit/s of the transmission.
3756 *
3757 * @property {number} [mcs]
3758 * The MCS index of the used transmission rate. Only applicable to
3759 * HT or VHT rates.
3760 *
3761 * @property {number} [40mhz]
3762 * Specifies whether the tranmission rate used 40MHz wide channel.
3763 * Only applicable to HT or VHT rates.
3764 *
3765 * Note: this option exists for backwards compatibility only and its
3766 * use is discouraged. The `mhz` field should be used instead to
3767 * determine the channel width.
3768 *
3769 * @property {boolean} [short_gi]
3770 * Specifies whether a short guard interval is used for the transmission.
3771 * Only applicable to HT or VHT rates.
3772 *
3773 * @property {number} [nss]
3774 * Specifies the number of spatial streams used by the transmission.
3775 * Only applicable to VHT rates.
3776 */
3777
3778 /**
3779 * Fetch the list of associated peers.
3780 *
3781 * @returns {Promise&lt;Array&lt;LuCI.Network.WifiPeerEntry>>}
3782 * Returns a promise resolving to an array of wireless peers associated
3783 * with this network.
3784 */
3785 getAssocList: function() {
3786 return callIwinfoAssoclist(this.getIfname());
3787 },
3788
3789 /**
3790 * Query the current operating frequency of the wireless network.
3791 *
3792 * @returns {null|string}
3793 * Returns the current operating frequency of the network from `ubus`
3794 * runtime information in GHz or `null` if the information is not
3795 * available.
3796 */
3797 getFrequency: function() {
3798 var freq = this.ubus('net', 'iwinfo', 'frequency');
3799
3800 if (freq != null &amp;&amp; freq > 0)
3801 return '%.03f'.format(freq / 1000);
3802
3803 return null;
3804 },
3805
3806 /**
3807 * Query the current average bitrate of all peers associated to this
3808 * wireless network.
3809 *
3810 * @returns {null|number}
3811 * Returns the average bit rate among all peers associated to the network
3812 * as reported by `ubus` runtime information or `null` if the information
3813 * is not available.
3814 */
3815 getBitRate: function() {
3816 var rate = this.ubus('net', 'iwinfo', 'bitrate');
3817
3818 if (rate != null &amp;&amp; rate > 0)
3819 return (rate / 1000);
3820
3821 return null;
3822 },
3823
3824 /**
3825 * Query the current wireless channel.
3826 *
3827 * @returns {null|number}
3828 * Returns the wireless channel as reported by `ubus` runtime information
3829 * or `null` if it cannot be determined.
3830 */
3831 getChannel: function() {
3832 return this.ubus('net', 'iwinfo', 'channel') || this.ubus('dev', 'config', 'channel') || this.get('channel');
3833 },
3834
3835 /**
3836 * Query the current wireless signal.
3837 *
3838 * @returns {null|number}
3839 * Returns the wireless signal in dBm as reported by `ubus` runtime
3840 * information or `null` if it cannot be determined.
3841 */
3842 getSignal: function() {
3843 return this.ubus('net', 'iwinfo', 'signal') || 0;
3844 },
3845
3846 /**
3847 * Query the current radio noise floor.
3848 *
3849 * @returns {number}
3850 * Returns the radio noise floor in dBm as reported by `ubus` runtime
3851 * information or `0` if it cannot be determined.
3852 */
3853 getNoise: function() {
3854 return this.ubus('net', 'iwinfo', 'noise') || 0;
3855 },
3856
3857 /**
3858 * Query the current country code.
3859 *
3860 * @returns {string}
3861 * Returns the wireless country code as reported by `ubus` runtime
3862 * information or `00` if it cannot be determined.
3863 */
3864 getCountryCode: function() {
3865 return this.ubus('net', 'iwinfo', 'country') || this.ubus('dev', 'config', 'country') || '00';
3866 },
3867
3868 /**
3869 * Query the current radio TX power.
3870 *
3871 * @returns {null|number}
3872 * Returns the wireless network transmit power in dBm as reported by
3873 * `ubus` runtime information or `null` if it cannot be determined.
3874 */
3875 getTXPower: function() {
3876 return this.ubus('net', 'iwinfo', 'txpower');
3877 },
3878
3879 /**
3880 * Query the radio TX power offset.
3881 *
3882 * Some wireless radios have a fixed power offset, e.g. due to the
3883 * use of external amplifiers.
3884 *
3885 * @returns {number}
3886 * Returns the wireless network transmit power offset in dBm as reported
3887 * by `ubus` runtime information or `0` if there is no offset, or if it
3888 * cannot be determined.
3889 */
3890 getTXPowerOffset: function() {
3891 return this.ubus('net', 'iwinfo', 'txpower_offset') || 0;
3892 },
3893
3894 /**
3895 * Calculate the current signal.
3896 *
3897 * @deprecated
3898 * @returns {number}
3899 * Returns the calculated signal level, which is the difference between
3900 * noise and signal (SNR), divided by 5.
3901 */
3902 getSignalLevel: function(signal, noise) {
3903 if (this.getActiveBSSID() == '00:00:00:00:00:00')
3904 return -1;
3905
3906 signal = signal || this.getSignal();
3907 noise = noise || this.getNoise();
3908
3909 if (signal &lt; 0 &amp;&amp; noise &lt; 0) {
3910 var snr = -1 * (noise - signal);
3911 return Math.floor(snr / 5);
3912 }
3913
3914 return 0;
3915 },
3916
3917 /**
3918 * Calculate the current signal quality percentage.
3919 *
3920 * @returns {number}
3921 * Returns the calculated signal quality in percent. The value is
3922 * calculated from the `quality` and `quality_max` indicators reported
3923 * by `ubus` runtime state.
3924 */
3925 getSignalPercent: function() {
3926 var qc = this.ubus('net', 'iwinfo', 'quality') || 0,
3927 qm = this.ubus('net', 'iwinfo', 'quality_max') || 0;
3928
3929 if (qc > 0 &amp;&amp; qm > 0)
3930 return Math.floor((100 / qm) * qc);
3931
3932 return 0;
3933 },
3934
3935 /**
3936 * Get a short description string for this wireless network.
3937 *
3938 * @returns {string}
3939 * Returns a string describing this network, consisting of the
3940 * active operation mode, followed by either the SSID, BSSID or
3941 * internal network ID, depending on which information is available.
3942 */
3943 getShortName: function() {
3944 return '%s "%s"'.format(
3945 this.getActiveModeI18n(),
3946 this.getActiveSSID() || this.getActiveBSSID() || this.getID());
3947 },
3948
3949 /**
3950 * Get a description string for this wireless network.
3951 *
3952 * @returns {string}
3953 * Returns a string describing this network, consisting of the
3954 * term `Wireless Network`, followed by the active operation mode,
3955 * the SSID, BSSID or internal network ID and the Linux network device
3956 * name, depending on which information is available.
3957 */
3958 getI18n: function() {
3959 return '%s: %s "%s" (%s)'.format(
3960 _('Wireless Network'),
3961 this.getActiveModeI18n(),
3962 this.getActiveSSID() || this.getActiveBSSID() || this.getID(),
3963 this.getIfname());
3964 },
3965
3966 /**
3967 * Get the primary logical interface this wireless network is attached to.
3968 *
3969 * @returns {null|LuCI.Network.Protocol}
3970 * Returns a `Network.Protocol` instance representing the logical
3971 * interface or `null` if this network is not attached to any logical
3972 * interface.
3973 */
3974 getNetwork: function() {
3975 return this.getNetworks()[0];
3976 },
3977
3978 /**
3979 * Get the logical interfaces this wireless network is attached to.
3980 *
3981 * @returns {Array&lt;LuCI.Network.Protocol>}
3982 * Returns an array of `Network.Protocol` instances representing the
3983 * logical interfaces this wireless network is attached to.
3984 */
3985 getNetworks: function() {
3986 var networkNames = this.getNetworkNames(),
3987 networks = [];
3988
3989 for (var i = 0; i &lt; networkNames.length; i++) {
3990 var uciInterface = uci.get('network', networkNames[i]);
3991
3992 if (uciInterface == null || uciInterface['.type'] != 'interface')
3993 continue;
3994
3995 networks.push(L.network.instantiateNetwork(networkNames[i]));
3996 }
3997
3998 networks.sort(networkSort);
3999
4000 return networks;
4001 },
4002
4003 /**
4004 * Get the associated Linux network device.
4005 *
4006 * @returns {LuCI.Network.Device}
4007 * Returns a `Network.Device` instance representing the Linux network
4008 * device associted with this wireless network.
4009 */
4010 getDevice: function() {
4011 return L.network.instantiateDevice(this.getIfname());
4012 }
4013 });
4014
4015 return Network;
4016 </code></pre>
4017 </article>
4018 </section>
4019
4020
4021
4022
4023 </div>
4024
4025 <nav>
4026 <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="LuCI.html">LuCI</a></li><li><a href="LuCI.Class.html">Class</a></li><li><a href="LuCI.dom.html">dom</a></li><li><a href="LuCI.fs.html">fs</a></li><li><a href="LuCI.Headers.html">Headers</a></li><li><a href="LuCI.Network.html">Network</a></li><li><a href="LuCI.Network.Device.html">Device</a></li><li><a href="LuCI.Network.Hosts.html">Hosts</a></li><li><a href="LuCI.Network.Protocol.html">Protocol</a></li><li><a href="LuCI.Network.WifiDevice.html">WifiDevice</a></li><li><a href="LuCI.Network.WifiNetwork.html">WifiNetwork</a></li><li><a href="LuCI.Poll.html">Poll</a></li><li><a href="LuCI.Request.html">Request</a></li><li><a href="LuCI.Request.poll.html">poll</a></li><li><a href="LuCI.Response.html">Response</a></li><li><a href="LuCI.rpc.html">rpc</a></li><li><a href="LuCI.uci.html">uci</a></li><li><a href="LuCI.view.html">view</a></li><li><a href="LuCI.XHR.html">XHR</a></li></ul>
4027 </nav>
4028
4029 <br class="clear">
4030
4031 <footer>
4032 Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Tue Nov 05 2019 09:33:05 GMT+0100 (Central European Standard Time)
4033 </footer>
4034
4035 <script> prettyPrint(); </script>
4036 <script src="scripts/linenumber.js"> </script>
4037 </body>
4038 </html>