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