11 'require tools.widgets as widgets';
12 'require tools.network as nettools';
14 var isReadonlyView
= !L
.hasViewPermission() || null;
16 function count_changes(section_id
) {
17 var changes
= ui
.changes
.changes
, n
= 0;
19 if (!L
.isObject(changes
))
22 if (Array
.isArray(changes
.network
))
23 for (var i
= 0; i
< changes
.network
.length
; i
++)
24 n
+= (changes
.network
[i
][1] == section_id
);
26 if (Array
.isArray(changes
.dhcp
))
27 for (var i
= 0; i
< changes
.dhcp
.length
; i
++)
28 n
+= (changes
.dhcp
[i
][1] == section_id
);
33 function render_iface(dev
, alias
) {
34 var type
= dev
? dev
.getType() : 'ethernet',
35 up
= dev
? dev
.isUp() : false;
37 return E('span', { class: 'cbi-tooltip-container' }, [
38 E('img', { 'class' : 'middle', 'src': L
.resource('icons/%s%s.png').format(
39 alias
? 'alias' : type
,
40 up
? '' : '_disabled') }),
41 E('span', { 'class': 'cbi-tooltip ifacebadge large' }, [
42 E('img', { 'src': L
.resource('icons/%s%s.png').format(
43 type
, up
? '' : '_disabled') }),
44 L
.itemlist(E('span', { 'class': 'left' }), [
45 _('Type'), dev
? dev
.getTypeI18n() : null,
46 _('Device'), dev
? dev
.getName() : _('Not present'),
47 _('Connected'), up
? _('yes') : _('no'),
48 _('MAC'), dev
? dev
.getMAC() : null,
49 _('RX'), dev
? '%.2mB (%d %s)'.format(dev
.getRXBytes(), dev
.getRXPackets(), _('Pkts.')) : null,
50 _('TX'), dev
? '%.2mB (%d %s)'.format(dev
.getTXBytes(), dev
.getTXPackets(), _('Pkts.')) : null
56 function render_status(node
, ifc
, with_device
) {
57 var desc
= null, c
= [];
60 desc
= _('Virtual dynamic interface');
61 else if (ifc
.isAlias())
62 desc
= _('Alias Interface');
63 else if (!uci
.get('network', ifc
.getName()))
64 return L
.itemlist(node
, [
65 null, E('em', _('Interface is marked for deletion'))
68 var i18n
= ifc
.getI18n();
70 desc
= desc
? '%s (%s)'.format(desc
, i18n
) : i18n
;
72 var changecount
= with_device
? 0 : count_changes(ifc
.getName()),
73 ipaddrs
= changecount
? [] : ifc
.getIPAddrs(),
74 ip6addrs
= changecount
? [] : ifc
.getIP6Addrs(),
75 errors
= ifc
.getErrors(),
76 maindev
= ifc
.getL3Device() || ifc
.getDevice(),
77 macaddr
= maindev
? maindev
.getMAC() : null;
79 return L
.itemlist(node
, [
80 _('Protocol'), with_device
? null : (desc
|| '?'),
81 _('Device'), with_device
? (maindev
? maindev
.getShortName() : E('em', _('Not present'))) : null,
82 _('Uptime'), (!changecount
&& ifc
.isUp()) ? '%t'.format(ifc
.getUptime()) : null,
83 _('MAC'), (!changecount
&& !ifc
.isDynamic() && !ifc
.isAlias() && macaddr
) ? macaddr
: null,
84 _('RX'), (!changecount
&& !ifc
.isDynamic() && !ifc
.isAlias() && maindev
) ? '%.2mB (%d %s)'.format(maindev
.getRXBytes(), maindev
.getRXPackets(), _('Pkts.')) : null,
85 _('TX'), (!changecount
&& !ifc
.isDynamic() && !ifc
.isAlias() && maindev
) ? '%.2mB (%d %s)'.format(maindev
.getTXBytes(), maindev
.getTXPackets(), _('Pkts.')) : null,
86 _('IPv4'), ipaddrs
[0],
87 _('IPv4'), ipaddrs
[1],
88 _('IPv4'), ipaddrs
[2],
89 _('IPv4'), ipaddrs
[3],
90 _('IPv4'), ipaddrs
[4],
91 _('IPv6'), ip6addrs
[0],
92 _('IPv6'), ip6addrs
[1],
93 _('IPv6'), ip6addrs
[2],
94 _('IPv6'), ip6addrs
[3],
95 _('IPv6'), ip6addrs
[4],
96 _('IPv6'), ip6addrs
[5],
97 _('IPv6'), ip6addrs
[6],
98 _('IPv6'), ip6addrs
[7],
99 _('IPv6'), ip6addrs
[8],
100 _('IPv6'), ip6addrs
[9],
101 _('IPv6-PD'), changecount
? null : ifc
.getIP6Prefix(),
102 _('Information'), with_device
? null : (ifc
.get('auto') != '0' ? null : _('Not started on boot')),
103 _('Error'), errors
? errors
[0] : null,
104 _('Error'), errors
? errors
[1] : null,
105 _('Error'), errors
? errors
[2] : null,
106 _('Error'), errors
? errors
[3] : null,
107 _('Error'), errors
? errors
[4] : null,
108 null, changecount
? E('a', {
110 click
: L
.bind(ui
.changes
.displayChanges
, ui
.changes
)
111 }, _('Interface has %d pending changes').format(changecount
)) : null
115 function render_modal_status(node
, ifc
) {
116 var dev
= ifc
? (ifc
.getDevice() || ifc
.getL3Device() || ifc
.getL3Device()) : null;
120 'src': L
.resource('icons/%s%s.png').format(dev
? dev
.getType() : 'ethernet', (dev
&& dev
.isUp()) ? '' : '_disabled'),
121 'title': dev
? dev
.getTypeI18n() : _('Not present')
123 ifc
? render_status(E('span'), ifc
, true) : E('em', _('Interface not present or not connected yet.'))
129 function render_ifacebox_status(node
, ifc
) {
130 var dev
= ifc
.getL3Device() || ifc
.getDevice(),
131 subdevs
= ifc
.getDevices(),
132 c
= [ render_iface(dev
, ifc
.isAlias()) ];
134 if (subdevs
&& subdevs
.length
) {
137 for (var j
= 0; j
< subdevs
.length
; j
++)
138 sifs
.push(render_iface(subdevs
[j
]));
142 c
.push(E('span', {}, sifs
));
146 c
.push(E('small', {}, ifc
.isAlias() ? _('Alias of "%s"').format(ifc
.isAlias())
147 : (dev
? dev
.getName() : E('em', _('Not present')))));
149 dom
.content(node
, c
);
151 return firewall
.getZoneByNetwork(ifc
.getName()).then(L
.bind(function(zone
) {
152 this.style
.backgroundColor
= zone
? zone
.getColor() : '#EEEEEE';
153 this.title
= zone
? _('Part of zone %q').format(zone
.getName()) : _('No zone assigned');
154 }, node
.previousElementSibling
));
157 function iface_updown(up
, id
, ev
, force
) {
158 var row
= document
.querySelector('.cbi-section-table-row[data-sid="%s"]'.format(id
)),
159 dsc
= row
.querySelector('[data-name="_ifacestat"] > div'),
160 btns
= row
.querySelectorAll('.cbi-section-actions .reconnect, .cbi-section-actions .down');
163 btns
[+!up
].classList
.add('spinning');
165 btns
[0].disabled
= true;
166 btns
[1].disabled
= true;
169 L
.resolveDefault(fs
.exec_direct('/usr/libexec/luci-peeraddr')).then(function(res
) {
170 var info
= null; try { info
= JSON
.parse(res
); } catch(e
) {}
172 if (L
.isObject(info
) &&
173 Array
.isArray(info
.inbound_interfaces
) &&
174 info
.inbound_interfaces
.filter(function(i
) { return i
== id
})[0]) {
176 ui
.showModal(_('Confirm disconnect'), [
177 E('p', _('You appear to be currently connected to the device via the "%h" interface. Do you really want to shut down the interface?').format(id
)),
178 E('div', { 'class': 'right' }, [
180 'class': 'cbi-button cbi-button-neutral',
181 'click': function(ev
) {
182 btns
[1].classList
.remove('spinning');
183 btns
[1].disabled
= false;
184 btns
[0].disabled
= false;
191 'class': 'cbi-button cbi-button-negative important',
192 'click': function(ev
) {
193 dsc
.setAttribute('disconnect', '');
194 dom
.content(dsc
, E('em', _('Interface is shutting down...')));
203 dsc
.setAttribute('disconnect', '');
204 dom
.content(dsc
, E('em', _('Interface is shutting down...')));
209 dsc
.setAttribute(up
? 'reconnect' : 'disconnect', force
? 'force' : '');
210 dom
.content(dsc
, E('em', up
? _('Interface is reconnecting...') : _('Interface is shutting down...')));
214 function get_netmask(s
, use_cfgvalue
) {
215 var readfn
= use_cfgvalue
? 'cfgvalue' : 'formvalue',
216 addrs
= L
.toArray(s
[readfn
](s
.section
, 'ipaddr')),
217 mask
= s
[readfn
](s
.section
, 'netmask'),
218 firstsubnet
= mask
? addrs
[0] + '/' + mask
: addrs
.filter(function(a
) { return a
.indexOf('/') > 0 })[0];
220 if (firstsubnet
== null)
223 var subnetmask
= firstsubnet
.split('/')[1];
225 if (!isNaN(subnetmask
))
226 subnetmask
= network
.prefixToMask(+subnetmask
);
232 poll_status: function(map
, networks
) {
233 var resolveZone
= null;
235 for (var i
= 0; i
< networks
.length
; i
++) {
236 var ifc
= networks
[i
],
237 row
= map
.querySelector('.cbi-section-table-row[data-sid="%s"]'.format(ifc
.getName()));
242 var dsc
= row
.querySelector('[data-name="_ifacestat"] > div'),
243 box
= row
.querySelector('[data-name="_ifacebox"] .ifacebox-body'),
244 btn1
= row
.querySelector('.cbi-section-actions .reconnect'),
245 btn2
= row
.querySelector('.cbi-section-actions .down'),
246 stat
= document
.querySelector('[id="%s-ifc-status"]'.format(ifc
.getName())),
247 resolveZone
= render_ifacebox_status(box
, ifc
),
248 disabled
= ifc
? !ifc
.isUp() : true,
249 dynamic
= ifc
? ifc
.isDynamic() : false;
251 if (dsc
.hasAttribute('reconnect')) {
252 dom
.content(dsc
, E('em', _('Interface is starting...')));
254 else if (dsc
.hasAttribute('disconnect')) {
255 dom
.content(dsc
, E('em', _('Interface is stopping...')));
257 else if (ifc
.getProtocol() || uci
.get('network', ifc
.getName()) == null) {
258 render_status(dsc
, ifc
, false);
260 else if (!ifc
.getProtocol()) {
261 var e
= map
.querySelector('[id="cbi-network-%s"] .cbi-button-edit'.format(ifc
.getName()));
262 if (e
) e
.disabled
= true;
264 var link
= L
.url('admin/system/opkg') + '?query=luci-proto';
266 E('em', _('Unsupported protocol type.')), E('br'),
267 E('a', { href
: link
}, _('Install protocol extensions...'))
271 dom
.content(dsc
, E('em', _('Interface not present or not connected yet.')));
275 var dev
= ifc
.getDevice();
278 'src': L
.resource('icons/%s%s.png').format(dev
? dev
.getType() : 'ethernet', (dev
&& dev
.isUp()) ? '' : '_disabled'),
279 'title': dev
? dev
.getTypeI18n() : _('Not present')
281 render_status(E('span'), ifc
, true)
285 btn1
.disabled
= isReadonlyView
|| btn1
.classList
.contains('spinning') || btn2
.classList
.contains('spinning') || dynamic
;
286 btn2
.disabled
= isReadonlyView
|| btn1
.classList
.contains('spinning') || btn2
.classList
.contains('spinning') || dynamic
|| disabled
;
289 return Promise
.all([ resolveZone
, network
.flushCache() ]);
294 network
.getDSLModemType(),
295 network
.getDevices(),
296 fs
.lines('/etc/iproute2/rt_tables'),
301 render: function(data
) {
302 var dslModemType
= data
[0],
306 var rtTables
= data
[2].map(function(l
) {
307 var m
= l
.trim().match(/^(\d+)\s+(\S+)$/);
308 return m
? [ +m
[1], m
[2] ] : null;
309 }).filter(function(e
) {
310 return e
&& e
[0] > 0;
313 m
= new form
.Map('network');
317 s
= m
.section(form
.GridSection
, 'interface', _('Interfaces'));
320 s
.addbtntitle
= _('Add new interface...');
322 s
.load = function() {
324 network
.getNetworks(),
326 ]).then(L
.bind(function(data
) {
327 this.networks
= data
[0];
328 this.zones
= data
[1];
332 s
.tab('general', _('General Settings'));
333 s
.tab('advanced', _('Advanced Settings'));
334 s
.tab('physical', _('Physical Settings'));
335 s
.tab('brport', _('Bridge port specific options'));
336 s
.tab('bridgevlan', _('Bridge VLAN filtering'));
337 s
.tab('firewall', _('Firewall Settings'));
338 s
.tab('dhcp', _('DHCP Server'));
340 s
.cfgsections = function() {
341 return this.networks
.map(function(n
) { return n
.getName() })
342 .filter(function(n
) { return n
!= 'loopback' });
345 s
.modaltitle = function(section_id
) {
346 return _('Interfaces') + ' » ' + section_id
.toUpperCase();
349 s
.renderRowActions = function(section_id
) {
350 var tdEl
= this.super('renderRowActions', [ section_id
, _('Edit') ]),
351 net
= this.networks
.filter(function(n
) { return n
.getName() == section_id
})[0],
352 disabled
= net
? !net
.isUp() : true,
353 dynamic
= net
? net
.isDynamic() : false;
355 dom
.content(tdEl
.lastChild
, [
357 'class': 'cbi-button cbi-button-neutral reconnect',
358 'click': iface_updown
.bind(this, true, section_id
),
359 'title': _('Reconnect this interface'),
360 'disabled': dynamic
? 'disabled' : null
363 'class': 'cbi-button cbi-button-neutral down',
364 'click': iface_updown
.bind(this, false, section_id
),
365 'title': _('Shutdown this interface'),
366 'disabled': (dynamic
|| disabled
) ? 'disabled' : null
368 tdEl
.lastChild
.firstChild
,
369 tdEl
.lastChild
.lastChild
372 if (!dynamic
&& net
&& !uci
.get('network', net
.getName())) {
373 tdEl
.lastChild
.childNodes
[0].disabled
= true;
374 tdEl
.lastChild
.childNodes
[2].disabled
= true;
375 tdEl
.lastChild
.childNodes
[3].disabled
= true;
381 s
.addModalOptions = function(s
) {
382 var protoval
= uci
.get('network', s
.section
, 'proto'),
383 protoclass
= protoval
? network
.getProtocol(protoval
) : null,
384 o
, ifname_single
, ifname_multi
, proto_select
, proto_switch
, type
, stp
, igmp
, ss
, so
;
389 return network
.getNetwork(s
.section
).then(L
.bind(function(ifc
) {
390 var protocols
= network
.getProtocols();
392 protocols
.sort(function(a
, b
) {
393 return a
.getProtocol() > b
.getProtocol();
396 o
= s
.taboption('general', form
.DummyValue
, '_ifacestat_modal', _('Status'));
398 o
.cfgvalue
= L
.bind(function(section_id
) {
399 var net
= this.networks
.filter(function(n
) { return n
.getName() == section_id
})[0];
401 return render_modal_status(E('div', {
402 'id': '%s-ifc-status'.format(section_id
),
403 'class': 'ifacebadge large'
406 o
.write = function() {};
408 proto_select
= s
.taboption('general', form
.ListValue
, 'proto', _('Protocol'));
409 proto_select
.modalonly
= true;
411 proto_switch
= s
.taboption('general', form
.Button
, '_switch_proto');
412 proto_switch
.modalonly
= true;
413 proto_switch
.title
= _('Really switch protocol?');
414 proto_switch
.inputtitle
= _('Switch protocol');
415 proto_switch
.inputstyle
= 'apply';
416 proto_switch
.onclick
= L
.bind(function(ev
) {
418 .then(L
.bind(m
.load
, m
))
419 .then(L
.bind(m
.render
, m
))
420 .then(L
.bind(this.renderMoreOptionsModal
, this, s
.section
));
423 o
= s
.taboption('general', form
.Flag
, 'auto', _('Bring up on boot'));
425 o
.default = o
.enabled
;
427 if (L
.hasSystemFeature('firewall')) {
428 o
= s
.taboption('firewall', widgets
.ZoneSelect
, '_zone', _('Create / Assign firewall-zone'), _('Choose the firewall zone you want to assign to this interface. Select <em>unspecified</em> to remove the interface from the associated zone or fill out the <em>custom</em> field to define a new zone and attach the interface to it.'));
429 o
.network
= ifc
.getName();
432 o
.cfgvalue = function(section_id
) {
433 return firewall
.getZoneByNetwork(ifc
.getName()).then(function(zone
) {
434 return (zone
!= null ? zone
.getName() : null);
438 o
.write
= o
.remove = function(section_id
, value
) {
440 firewall
.getZoneByNetwork(ifc
.getName()),
441 (value
!= null) ? firewall
.getZone(value
) : null
442 ]).then(function(data
) {
443 var old_zone
= data
[0],
446 if (old_zone
== null && new_zone
== null && (value
== null || value
== ''))
449 if (old_zone
!= null && new_zone
!= null && old_zone
.getName() == new_zone
.getName())
452 if (old_zone
!= null)
453 old_zone
.deleteNetwork(ifc
.getName());
455 if (new_zone
!= null)
456 new_zone
.addNetwork(ifc
.getName());
457 else if (value
!= null)
458 return firewall
.addZone(value
).then(function(new_zone
) {
459 new_zone
.addNetwork(ifc
.getName());
465 for (var i
= 0; i
< protocols
.length
; i
++) {
466 proto_select
.value(protocols
[i
].getProtocol(), protocols
[i
].getI18n());
468 if (protocols
[i
].getProtocol() != uci
.get('network', s
.section
, 'proto'))
469 proto_switch
.depends('proto', protocols
[i
].getProtocol());
472 if (L
.hasSystemFeature('dnsmasq') || L
.hasSystemFeature('odhcpd')) {
473 o
= s
.taboption('dhcp', form
.SectionValue
, '_dhcp', form
.TypedSection
, 'dhcp');
474 o
.depends('proto', 'static');
477 ss
.uciconfig
= 'dhcp';
478 ss
.addremove
= false;
481 ss
.tab('general', _('General Setup'));
482 ss
.tab('advanced', _('Advanced Settings'));
483 ss
.tab('ipv6', _('IPv6 Settings'));
485 ss
.filter = function(section_id
) {
486 return (uci
.get('dhcp', section_id
, 'interface') == ifc
.getName());
489 ss
.renderSectionPlaceholder = function() {
490 return E('div', { 'class': 'cbi-section-create' }, [
491 E('p', _('No DHCP Server configured for this interface') + '   '),
493 'class': 'cbi-button cbi-button-add',
494 'title': _('Setup DHCP Server'),
495 'click': ui
.createHandlerFn(this, function(section_id
, ev
) {
496 this.map
.save(function() {
497 uci
.add('dhcp', 'dhcp', section_id
);
498 uci
.set('dhcp', section_id
, 'interface', section_id
);
499 uci
.set('dhcp', section_id
, 'start', 100);
500 uci
.set('dhcp', section_id
, 'limit', 150);
501 uci
.set('dhcp', section_id
, 'leasetime', '12h');
504 }, _('Setup DHCP Server'))
508 ss
.taboption('general', form
.Flag
, 'ignore', _('Ignore interface'), _('Disable <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr> for this interface.'));
510 so
= ss
.taboption('general', form
.Value
, 'start', _('Start'), _('Lowest leased address as offset from the network address.'));
512 so
.datatype
= 'or(uinteger,ip4addr("nomask"))';
515 so
= ss
.taboption('general', form
.Value
, 'limit', _('Limit'), _('Maximum number of leased addresses.'));
517 so
.datatype
= 'uinteger';
520 so
= ss
.taboption('general', form
.Value
, 'leasetime', _('Lease time'), _('Expiry time of leased addresses, minimum is 2 minutes (<code>2m</code>).'));
524 so
= ss
.taboption('advanced', form
.Flag
, 'dynamicdhcp', _('Dynamic <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr>'), _('Dynamically allocate DHCP addresses for clients. If disabled, only clients having static leases will be served.'));
525 so
.default = so
.enabled
;
527 ss
.taboption('advanced', form
.Flag
, 'force', _('Force'), _('Force DHCP on this network even if another server is detected.'));
529 // XXX: is this actually useful?
530 //ss.taboption('advanced', form.Value, 'name', _('Name'), _('Define a name for this network.'));
532 so
= ss
.taboption('advanced', form
.Value
, 'netmask', _('<abbr title="Internet Protocol Version 4">IPv4</abbr>-Netmask'), _('Override the netmask sent to clients. Normally it is calculated from the subnet that is served.'));
534 so
.datatype
= 'ip4addr';
536 so
.render = function(option_index
, section_id
, in_table
) {
537 this.placeholder
= get_netmask(s
, true);
538 return form
.Value
.prototype.render
.apply(this, [ option_index
, section_id
, in_table
]);
541 so
.validate = function(section_id
, value
) {
542 var uielem
= this.getUIElement(section_id
);
544 uielem
.setPlaceholder(get_netmask(s
, false));
545 return form
.Value
.prototype.validate
.apply(this, [ section_id
, value
]);
548 ss
.taboption('advanced', form
.DynamicList
, 'dhcp_option', _('DHCP-Options'), _('Define additional DHCP options, for example "<code>6,192.168.2.1,192.168.2.2</code>" which advertises different DNS servers to clients.'));
550 for (var i
= 0; i
< ss
.children
.length
; i
++)
551 if (ss
.children
[i
].option
!= 'ignore')
552 ss
.children
[i
].depends('ignore', '0');
554 so
= ss
.taboption('ipv6', form
.ListValue
, 'ra', _('Router Advertisement-Service'));
555 so
.value('', _('disabled'));
556 so
.value('server', _('server mode'));
557 so
.value('relay', _('relay mode'));
558 so
.value('hybrid', _('hybrid mode'));
560 so
= ss
.taboption('ipv6', form
.ListValue
, 'dhcpv6', _('DHCPv6-Service'));
561 so
.value('', _('disabled'));
562 so
.value('server', _('server mode'));
563 so
.value('relay', _('relay mode'));
564 so
.value('hybrid', _('hybrid mode'));
566 so
= ss
.taboption('ipv6', form
.ListValue
, 'ndp', _('NDP-Proxy'));
567 so
.value('', _('disabled'));
568 so
.value('relay', _('relay mode'));
569 so
.value('hybrid', _('hybrid mode'));
571 so
= ss
.taboption('ipv6', form
.Flag
, 'master', _('Master'), _('Set this interface as master for the dhcpv6 relay.'));
572 so
.depends('dhcpv6', 'relay');
573 so
.depends('dhcpv6', 'hybrid');
575 so
= ss
.taboption('ipv6', form
.ListValue
, 'ra_management', _('DHCPv6-Mode'), _('Default is stateless + stateful'));
576 so
.value('0', _('stateless'));
577 so
.value('1', _('stateless + stateful'));
578 so
.value('2', _('stateful-only'));
579 so
.depends('dhcpv6', 'server');
580 so
.depends('dhcpv6', 'hybrid');
583 so
= ss
.taboption('ipv6', form
.Flag
, 'ra_default', _('Always announce default router'), _('Announce as default router even if no public prefix is available.'));
584 so
.depends('ra', 'server');
585 so
.depends('ra', 'hybrid');
587 ss
.taboption('ipv6', form
.DynamicList
, 'dns', _('Announced DNS servers'));
588 ss
.taboption('ipv6', form
.DynamicList
, 'domain', _('Announced DNS domains'));
591 ifc
.renderFormOptions(s
);
592 nettools
.addDeviceOptions(s
, null, true);
594 // Common interface options
595 o
= nettools
.replaceOption(s
, 'advanced', form
.Flag
, 'defaultroute', _('Use default gateway'), _('If unchecked, no default route is configured'));
596 o
.default = o
.enabled
;
598 o
= nettools
.replaceOption(s
, 'advanced', form
.Flag
, 'peerdns', _('Use DNS servers advertised by peer'), _('If unchecked, the advertised DNS server addresses are ignored'));
599 o
.default = o
.enabled
;
601 o
= nettools
.replaceOption(s
, 'advanced', form
.DynamicList
, 'dns', _('Use custom DNS servers'));
602 o
.depends('peerdns', '0');
603 o
.datatype
= 'ipaddr';
605 o
= nettools
.replaceOption(s
, 'advanced', form
.DynamicList
, 'dns_search', _('DNS search domains'));
606 o
.depends('peerdns', '0');
607 o
.datatype
= 'hostname';
609 o
= nettools
.replaceOption(s
, 'advanced', form
.Value
, 'dns_metric', _('DNS weight'), _('The DNS server entries in the local resolv.conf are primarily sorted by the weight specified here'));
610 o
.datatype
= 'uinteger';
613 o
= nettools
.replaceOption(s
, 'advanced', form
.Value
, 'metric', _('Use gateway metric'));
614 o
.datatype
= 'uinteger';
617 o
= nettools
.replaceOption(s
, 'advanced', form
.Value
, 'ip4table', _('Override IPv4 routing table'));
618 o
.datatype
= 'or(uinteger, string)';
619 for (var i
= 0; i
< rtTables
.length
; i
++)
620 o
.value(rtTables
[i
][1], '%s (%d)'.format(rtTables
[i
][1], rtTables
[i
][0]));
622 o
= nettools
.replaceOption(s
, 'advanced', form
.Value
, 'ip6table', _('Override IPv6 routing table'));
623 o
.datatype
= 'or(uinteger, string)';
624 for (var i
= 0; i
< rtTables
.length
; i
++)
625 o
.value(rtTables
[i
][1], '%s (%d)'.format(rtTables
[i
][0], rtTables
[i
][1]));
627 o
= nettools
.replaceOption(s
, 'advanced', form
.Flag
, 'delegate', _('Delegate IPv6 prefixes'), _('Enable downstream delegation of IPv6 prefixes available on this interface'));
628 o
.default = o
.enabled
;
630 o
= nettools
.replaceOption(s
, 'advanced', form
.Value
, 'ip6assign', _('IPv6 assignment length'), _('Assign a part of given length of every public IPv6-prefix to this interface'));
631 o
.value('', _('disabled'));
633 o
.datatype
= 'max(128)';
635 o
= nettools
.replaceOption(s
, 'advanced', form
.Value
, 'ip6hint', _('IPv6 assignment hint'), _('Assign prefix parts using this hexadecimal subprefix ID for this interface.'));
637 o
.validate = function(section_id
, value
) {
638 if (value
== null || value
== '')
641 var n
= parseInt(value
, 16);
643 if (!/^(0x)?[0-9a-fA-F]+$/.test(value
) || isNaN(n
) || n
>= 0xffffffff)
644 return _('Expecting a hexadecimal assignment hint');
648 for (var i
= 33; i
<= 64; i
++)
649 o
.depends('ip6assign', String(i
));
652 o
= nettools
.replaceOption(s
, 'advanced', form
.DynamicList
, 'ip6class', _('IPv6 prefix filter'), _('If set, downstream subnets are only allocated from the given IPv6 prefix classes.'));
653 o
.value('local', 'local (%s)'.format(_('Local ULA')));
655 var prefixClasses
= {};
657 this.networks
.forEach(function(net
) {
658 var prefixes
= net
._ubus('ipv6-prefix');
659 if (Array
.isArray(prefixes
)) {
660 prefixes
.forEach(function(pfx
) {
661 if (L
.isObject(pfx
) && typeof(pfx
['class']) == 'string') {
662 prefixClasses
[pfx
['class']] = prefixClasses
[pfx
['class']] || {};
663 prefixClasses
[pfx
['class']][net
.getName()] = true;
669 Object
.keys(prefixClasses
).sort().forEach(function(c
) {
670 var networks
= Object
.keys(prefixClasses
[c
]).sort().join(', ');
671 o
.value(c
, (c
!= networks
) ? '%s (%s)'.format(c
, networks
) : c
);
675 o
= nettools
.replaceOption(s
, 'advanced', form
.Value
, 'ip6ifaceid', _('IPv6 suffix'), _("Optional. Allowed values: 'eui64', 'random', fixed value like '::1' or '::1:2'. When IPv6 prefix (like 'a:b:c:d::') is received from a delegating server, use the suffix (like '::1') to form the IPv6 address ('a:b:c:d::1') for the interface."));
676 o
.datatype
= 'ip6hostid';
677 o
.placeholder
= '::1';
679 o
= nettools
.replaceOption(s
, 'advanced', form
.Value
, 'ip6weight', _('IPv6 preference'), _('When delegating prefixes to multiple downstreams, interfaces with a higher preference value are considered first when allocating subnets.'));
680 o
.datatype
= 'uinteger';
683 for (var i
= 0; i
< s
.children
.length
; i
++) {
691 case '_switch_proto':
692 case '_ifacestat_modal':
696 case 'ifname_single':
697 case 'igmp_snooping':
701 for (var j
= 0; j
< protocols
.length
; j
++) {
702 if (!protocols
[j
].isVirtual()) {
704 for (var k
= 0; k
< o
.deps
.length
; k
++)
705 deps
.push(Object
.assign({ proto
: protocols
[j
].getProtocol() }, o
.deps
[k
]));
707 deps
.push({ proto
: protocols
[j
].getProtocol() });
715 for (var j
= 0; j
< o
.deps
.length
; j
++)
716 o
.deps
[j
].proto
= protoval
;
718 o
.depends('proto', protoval
);
722 this.activeSection
= s
.section
;
726 s
.handleModalCancel = function(/* ... */) {
727 var type
= uci
.get('network', this.activeSection
|| this.addedSection
, 'type'),
728 ifname
= (type
== 'bridge') ? 'br-%s'.format(this.activeSection
|| this.addedSection
) : null;
730 uci
.sections('network', 'bridge-vlan', function(bvs
) {
731 if (ifname
!= null && bvs
.device
== ifname
)
732 uci
.remove('network', bvs
['.name']);
735 return form
.GridSection
.prototype.handleModalCancel
.apply(this, arguments
);
738 s
.handleAdd = function(ev
) {
739 var m2
= new form
.Map('network'),
740 s2
= m2
.section(form
.NamedSection
, '_new_'),
741 protocols
= network
.getProtocols(),
742 proto
, name
, bridge
, ifname_single
, ifname_multi
;
744 protocols
.sort(function(a
, b
) {
745 return a
.getProtocol() > b
.getProtocol();
748 s2
.render = function() {
751 this.renderUCISection('_new_')
752 ]).then(this.renderContents
.bind(this));
755 name
= s2
.option(form
.Value
, 'name', _('Name'));
756 name
.rmempty
= false;
757 name
.datatype
= 'uciname';
758 name
.placeholder
= _('New interface name…');
759 name
.validate = function(section_id
, value
) {
760 if (uci
.get('network', value
) != null)
761 return _('The interface name is already used');
763 var pr
= network
.getProtocol(proto
.formvalue(section_id
), value
),
764 ifname
= pr
.isVirtual() ? '%s-%s'.format(pr
.getProtocol(), value
) : 'br-%s'.format(value
);
766 if (value
.length
> 15)
767 return _('The interface name is too long');
772 proto
= s2
.option(form
.ListValue
, 'proto', _('Protocol'));
773 proto
.validate
= name
.validate
;
775 bridge
= s2
.option(form
.Flag
, 'type', _('Bridge interfaces'), _('Creates a bridge over specified interface(s)'));
776 bridge
.modalonly
= true;
777 bridge
.disabled
= '';
778 bridge
.enabled
= 'bridge';
780 ifname_single
= s2
.option(widgets
.DeviceSelect
, 'ifname_single', _('Interface'));
781 ifname_single
.noaliases
= false;
782 ifname_single
.optional
= false;
784 ifname_multi
= s2
.option(widgets
.DeviceSelect
, 'ifname_multi', _('Interface'));
785 ifname_multi
.nobridges
= true;
786 ifname_multi
.noaliases
= true;
787 ifname_multi
.multiple
= true;
788 ifname_multi
.optional
= true;
789 ifname_multi
.display_size
= 6;
791 for (var i
= 0; i
< protocols
.length
; i
++) {
792 proto
.value(protocols
[i
].getProtocol(), protocols
[i
].getI18n());
794 if (!protocols
[i
].isVirtual()) {
795 bridge
.depends({ proto
: protocols
[i
].getProtocol() });
796 ifname_single
.depends({ type
: '', proto
: protocols
[i
].getProtocol() });
797 ifname_multi
.depends({ type
: 'bridge', proto
: protocols
[i
].getProtocol() });
801 m2
.render().then(L
.bind(function(nodes
) {
802 ui
.showModal(_('Add new interface...'), [
804 E('div', { 'class': 'right' }, [
807 'click': ui
.hideModal
808 }, _('Cancel')), ' ',
810 'class': 'cbi-button cbi-button-positive important',
811 'click': ui
.createHandlerFn(this, function(ev
) {
812 var nameval
= name
.isValid('_new_') ? name
.formvalue('_new_') : null,
813 protoval
= proto
.isValid('_new_') ? proto
.formvalue('_new_') : null,
814 protoclass
= protoval
? network
.getProtocol(protoval
, nameval
) : null;
816 if (nameval
== null || protoval
== null || nameval
== '' || protoval
== '')
819 return protoclass
.isCreateable(nameval
).then(function(checkval
) {
820 if (checkval
!= null) {
821 ui
.addNotification(null,
822 E('p', _('New interface for "%s" can not be created: %s').format(protoclass
.getI18n(), checkval
)));
827 return m
.save(function() {
828 var section_id
= uci
.add('network', 'interface', nameval
);
830 protoclass
.set('proto', protoval
);
832 if (ifname_single
.isActive('_new_')) {
833 protoclass
.addDevice(ifname_single
.formvalue('_new_'));
835 else if (ifname_multi
.isActive('_new_')) {
836 protoclass
.set('type', 'bridge');
837 L
.toArray(ifname_multi
.formvalue('_new_')).map(function(dev
) {
838 protoclass
.addDevice(dev
);
842 m
.children
[0].addedSection
= section_id
;
843 }).then(L
.bind(m
.children
[0].renderMoreOptionsModal
, m
.children
[0], nameval
));
846 }, _('Create interface'))
850 nodes
.querySelector('[id="%s"] input[type="text"]'.format(name
.cbid('_new_'))).focus();
854 s
.handleRemove = function(section_id
, ev
) {
855 return network
.deleteNetwork(section_id
).then(L
.bind(function(section_id
, ev
) {
856 return form
.GridSection
.prototype.handleRemove
.apply(this, [section_id
, ev
]);
857 }, this, section_id
, ev
));
860 o
= s
.option(form
.DummyValue
, '_ifacebox');
862 o
.textvalue = function(section_id
) {
863 var net
= this.section
.networks
.filter(function(n
) { return n
.getName() == section_id
})[0],
864 zone
= net
? this.section
.zones
.filter(function(z
) { return !!z
.getNetworks().filter(function(n
) { return n
== section_id
})[0] })[0] : null;
869 var node
= E('div', { 'class': 'ifacebox' }, [
871 'class': 'ifacebox-head',
872 'style': 'background-color:%s'.format(zone
? zone
.getColor() : '#EEEEEE'),
873 'title': zone
? _('Part of zone %q').format(zone
.getName()) : _('No zone assigned')
874 }, E('strong', net
.getName().toUpperCase())),
876 'class': 'ifacebox-body',
877 'id': '%s-ifc-devices'.format(section_id
),
878 'data-network': section_id
881 'src': L
.resource('icons/ethernet_disabled.png'),
882 'style': 'width:16px; height:16px'
884 E('br'), E('small', '?')
888 render_ifacebox_status(node
.childNodes
[1], net
);
893 o
= s
.option(form
.DummyValue
, '_ifacestat');
895 o
.textvalue = function(section_id
) {
896 var net
= this.section
.networks
.filter(function(n
) { return n
.getName() == section_id
})[0];
901 var node
= E('div', { 'id': '%s-ifc-description'.format(section_id
) });
903 render_status(node
, net
, false);
908 o
= s
.taboption('advanced', form
.Flag
, 'delegate', _('Use builtin IPv6-management'));
910 o
.default = o
.enabled
;
912 o
= s
.taboption('advanced', form
.Flag
, 'force_link', _('Force link'), _('Set interface properties regardless of the link carrier (If set, carrier sense events do not invoke hotplug handlers).'));
915 '1': [{ proto
: 'static' }],
920 // Device configuration
921 s
= m
.section(form
.GridSection
, 'device', _('Devices'));
924 s
.addbtntitle
= _('Add device configuration…');
926 s
.cfgsections = function() {
927 var sections
= uci
.sections('network', 'device'),
928 section_ids
= sections
.sort(function(a
, b
) { return a
.name
> b
.name
}).map(function(s
) { return s
['.name'] });
930 for (var i
= 0; i
< netDevs
.length
; i
++) {
931 if (sections
.filter(function(s
) { return s
.name
== netDevs
[i
].getName() }).length
)
934 if (netDevs
[i
].getType() == 'wifi' && !netDevs
[i
].isUp())
937 /* Unless http://lists.openwrt.org/pipermail/openwrt-devel/2020-July/030397.html is implemented,
938 we cannot properly redefine bridges as devices, so filter them away for now... */
940 var m
= netDevs
[i
].isBridge() ? netDevs
[i
].getName().match(/^br-([A-Za-z0-9_]+)$/) : null,
941 s
= m
? uci
.get('network', m
[1]) : null;
943 if (s
&& s
['.type'] == 'interface' && s
.type
== 'bridge')
946 section_ids
.push('dev:%s'.format(netDevs
[i
].getName()));
952 s
.renderMoreOptionsModal = function(section_id
, ev
) {
953 var m
= section_id
.match(/^dev:(.+)$/);
956 var devtype
= getDevType(section_id
);
958 section_id
= uci
.add('network', 'device');
960 uci
.set('network', section_id
, 'name', m
[1]);
961 uci
.set('network', section_id
, 'type', (devtype
!= 'ethernet') ? devtype
: null);
963 this.addedSection
= section_id
;
966 return this.super('renderMoreOptionsModal', [section_id
, ev
]);
969 s
.renderRowActions = function(section_id
) {
970 var trEl
= this.super('renderRowActions', [ section_id
, _('Configure…') ]),
971 deleteBtn
= trEl
.querySelector('button:last-child');
973 deleteBtn
.firstChild
.data
= _('Reset');
974 deleteBtn
.disabled
= section_id
.match(/^dev:/) ? true : null;
979 s
.modaltitle = function(section_id
) {
980 var m
= section_id
.match(/^dev:(.+)$/),
981 name
= m
? m
[1] : uci
.get('network', section_id
, 'name');
983 return name
? '%s: %q'.format(getDevTypeDesc(section_id
), name
) : _('Add device configuration');
986 s
.addModalOptions = function(s
) {
987 var isNew
= (uci
.get('network', s
.section
, 'name') == null),
988 dev
= getDevice(s
.section
);
990 nettools
.addDeviceOptions(s
, dev
, isNew
);
993 s
.handleModalCancel = function(/* ... */) {
994 var name
= uci
.get('network', this.addedSection
, 'name')
996 uci
.sections('network', 'bridge-vlan', function(bvs
) {
997 if (name
!= null && bvs
.device
== name
)
998 uci
.remove('network', bvs
['.name']);
1001 return form
.GridSection
.prototype.handleModalCancel
.apply(this, arguments
);
1004 function getDevice(section_id
) {
1005 var m
= section_id
.match(/^dev:(.+)$/),
1006 name
= m
? m
[1] : uci
.get('network', section_id
, 'name');
1008 return netDevs
.filter(function(d
) { return d
.getName() == name
})[0];
1011 function getDevType(section_id
) {
1012 var cfgtype
= uci
.get('network', section_id
, 'type'),
1013 dev
= getDevice(section_id
);
1015 switch (cfgtype
|| (dev
? dev
.getType() : '')) {
1047 function getDevTypeDesc(section_id
) {
1048 switch (getDevType(section_id
) || '') {
1050 return E('em', [ _('Device not present') ]);
1053 return _('VLAN (802.1q)');
1056 return _('VLAN (802.1ad)');
1059 return _('Bridge device');
1062 return _('Tunnel device');
1065 return _('MAC VLAN');
1068 return _('Virtual Ethernet');
1071 return _('Network device');
1075 o
= s
.option(form
.DummyValue
, 'name', _('Device'));
1076 o
.modalonly
= false;
1077 o
.textvalue = function(section_id
) {
1078 var dev
= getDevice(section_id
),
1079 ext
= section_id
.match(/^dev:/),
1080 icon
= render_iface(dev
);
1083 icon
.querySelector('img').style
.opacity
= '.5';
1085 return E('span', { 'class': 'ifacebadge' }, [
1087 E('span', { 'style': ext
? 'opacity:.5' : null }, [
1088 dev
? dev
.getName() : (uci
.get('network', section_id
, 'name') || '?')
1093 o
= s
.option(form
.DummyValue
, 'type', _('Type'));
1094 o
.textvalue
= getDevTypeDesc
;
1095 o
.modalonly
= false;
1097 o
= s
.option(form
.DummyValue
, 'macaddr', _('MAC Address'));
1098 o
.modalonly
= false;
1099 o
.textvalue = function(section_id
) {
1100 var dev
= getDevice(section_id
),
1101 val
= uci
.get('network', section_id
, 'macaddr'),
1102 mac
= dev
? dev
.getMAC() : null;
1104 return val
? E('strong', {
1105 'data-tooltip': _('The value is overridden by configuration. Original: %s').format(mac
|| _('unknown'))
1106 }, [ val
.toUpperCase() ]) : (mac
|| '-');
1109 o
= s
.option(form
.DummyValue
, 'mtu', _('MTU'));
1110 o
.modalonly
= false;
1111 o
.textvalue = function(section_id
) {
1112 var dev
= getDevice(section_id
),
1113 val
= uci
.get('network', section_id
, 'mtu'),
1114 mtu
= dev
? dev
.getMTU() : null;
1116 return val
? E('strong', {
1117 'data-tooltip': _('The value is overridden by configuration. Original: %s').format(mtu
|| _('unknown'))
1118 }, [ val
]) : (mtu
|| '-').toString();
1121 s
= m
.section(form
.TypedSection
, 'globals', _('Global network options'));
1122 s
.addremove
= false;
1125 o
= s
.option(form
.Value
, 'ula_prefix', _('IPv6 ULA-Prefix'));
1126 o
.datatype
= 'cidr6';
1128 o
= s
.option(form
.Flag
, 'packet_steering', _('Packet Steering'), _('Enable packet steering across all CPUs. May help or hinder network speed.'));
1132 if (dslModemType
!= null) {
1133 s
= m
.section(form
.TypedSection
, 'dsl', _('DSL'));
1136 o
= s
.option(form
.ListValue
, 'annex', _('Annex'));
1137 o
.value('a', _('Annex A + L + M (all)'));
1138 o
.value('b', _('Annex B (all)'));
1139 o
.value('j', _('Annex J (all)'));
1140 o
.value('m', _('Annex M (all)'));
1141 o
.value('bdmt', _('Annex B G.992.1'));
1142 o
.value('b2', _('Annex B G.992.3'));
1143 o
.value('b2p', _('Annex B G.992.5'));
1144 o
.value('at1', _('ANSI T1.413'));
1145 o
.value('admt', _('Annex A G.992.1'));
1146 o
.value('alite', _('Annex A G.992.2'));
1147 o
.value('a2', _('Annex A G.992.3'));
1148 o
.value('a2p', _('Annex A G.992.5'));
1149 o
.value('l', _('Annex L G.992.3 POTS 1'));
1150 o
.value('m2', _('Annex M G.992.3'));
1151 o
.value('m2p', _('Annex M G.992.5'));
1153 o
= s
.option(form
.ListValue
, 'tone', _('Tone'));
1154 o
.value('', _('auto'));
1155 o
.value('a', _('A43C + J43 + A43'));
1156 o
.value('av', _('A43C + J43 + A43 + V43'));
1157 o
.value('b', _('B43 + B43C'));
1158 o
.value('bv', _('B43 + B43C + V43'));
1160 if (dslModemType
== 'vdsl') {
1161 o
= s
.option(form
.ListValue
, 'xfer_mode', _('Encapsulation mode'));
1162 o
.value('', _('auto'));
1163 o
.value('atm', _('ATM (Asynchronous Transfer Mode)'));
1164 o
.value('ptm', _('PTM/EFM (Packet Transfer Mode)'));
1166 o
= s
.option(form
.ListValue
, 'line_mode', _('DSL line mode'));
1167 o
.value('', _('auto'));
1168 o
.value('adsl', _('ADSL'));
1169 o
.value('vdsl', _('VDSL'));
1171 o
= s
.option(form
.ListValue
, 'ds_snr_offset', _('Downstream SNR offset'));
1174 for (var i
= -100; i
<= 100; i
+= 5)
1175 o
.value(i
, _('%.1f dB').format(i
/ 10));
1178 s
.option(form
.Value
, 'firmware', _('Firmware File'));
1182 // Show ATM bridge section if we have the capabilities
1183 if (L
.hasSystemFeature('br2684ctl')) {
1184 s
= m
.section(form
.TypedSection
, 'atm-bridge', _('ATM Bridges'), _('ATM bridges expose encapsulated ethernet in AAL5 connections as virtual Linux network interfaces which can be used in conjunction with DHCP or PPP to dial into the provider network.'));
1188 s
.addbtntitle
= _('Add ATM Bridge');
1190 s
.handleAdd = function(ev
) {
1191 var sections
= uci
.sections('network', 'atm-bridge'),
1194 for (var i
= 0; i
< sections
.length
; i
++) {
1195 var unit
= +sections
[i
].unit
;
1197 if (!isNaN(unit
) && unit
> max_unit
)
1201 return this.map
.save(function() {
1202 var sid
= uci
.add('network', 'atm-bridge');
1204 uci
.set('network', sid
, 'unit', max_unit
+ 1);
1205 uci
.set('network', sid
, 'atmdev', 0);
1206 uci
.set('network', sid
, 'encaps', 'llc');
1207 uci
.set('network', sid
, 'payload', 'bridged');
1208 uci
.set('network', sid
, 'vci', 35);
1209 uci
.set('network', sid
, 'vpi', 8);
1213 s
.tab('general', _('General Setup'));
1214 s
.tab('advanced', _('Advanced Settings'));
1216 o
= s
.taboption('general', form
.Value
, 'vci', _('ATM Virtual Channel Identifier (VCI)'));
1217 s
.taboption('general', form
.Value
, 'vpi', _('ATM Virtual Path Identifier (VPI)'));
1219 o
= s
.taboption('general', form
.ListValue
, 'encaps', _('Encapsulation mode'));
1220 o
.value('llc', _('LLC'));
1221 o
.value('vc', _('VC-Mux'));
1223 s
.taboption('advanced', form
.Value
, 'atmdev', _('ATM device number'));
1224 s
.taboption('advanced', form
.Value
, 'unit', _('Bridge unit number'));
1226 o
= s
.taboption('advanced', form
.ListValue
, 'payload', _('Forwarding mode'));
1227 o
.value('bridged', _('bridged'));
1228 o
.value('routed', _('routed'));
1232 return m
.render().then(L
.bind(function(m
, nodes
) {
1233 poll
.add(L
.bind(function() {
1234 var section_ids
= m
.children
[0].cfgsections(),
1237 for (var i
= 0; i
< section_ids
.length
; i
++) {
1238 var row
= nodes
.querySelector('.cbi-section-table-row[data-sid="%s"]'.format(section_ids
[i
])),
1239 dsc
= row
.querySelector('[data-name="_ifacestat"] > div'),
1240 btn1
= row
.querySelector('.cbi-section-actions .reconnect'),
1241 btn2
= row
.querySelector('.cbi-section-actions .down');
1243 if (dsc
.getAttribute('reconnect') == '') {
1244 dsc
.setAttribute('reconnect', '1');
1245 tasks
.push(fs
.exec('/sbin/ifup', [section_ids
[i
]]).catch(function(e
) {
1246 ui
.addNotification(null, E('p', e
.message
));
1249 else if (dsc
.getAttribute('disconnect') == '') {
1250 dsc
.setAttribute('disconnect', '1');
1251 tasks
.push(fs
.exec('/sbin/ifdown', [section_ids
[i
]]).catch(function(e
) {
1252 ui
.addNotification(null, E('p', e
.message
));
1255 else if (dsc
.getAttribute('reconnect') == '1') {
1256 dsc
.removeAttribute('reconnect');
1257 btn1
.classList
.remove('spinning');
1258 btn1
.disabled
= false;
1260 else if (dsc
.getAttribute('disconnect') == '1') {
1261 dsc
.removeAttribute('disconnect');
1262 btn2
.classList
.remove('spinning');
1263 btn2
.disabled
= false;
1267 return Promise
.all(tasks
)
1268 .then(L
.bind(network
.getNetworks
, network
))
1269 .then(L
.bind(this.poll_status
, this, nodes
));