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