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