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