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