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