11 var callHostHints
, callDUIDHints
, callDHCPLeases
, CBILeaseStatus
, CBILease6Status
;
13 callHostHints
= rpc
.declare({
15 method
: 'getHostHints',
19 callDUIDHints
= rpc
.declare({
21 method
: 'getDUIDHints',
25 callDHCPLeases
= rpc
.declare({
27 method
: 'getDHCPLeases',
31 CBILeaseStatus
= form
.DummyValue
.extend({
32 renderWidget: function(section_id
, option_id
, cfgvalue
) {
34 E('h4', _('Active DHCP Leases')),
35 E('table', { 'id': 'lease_status_table', 'class': 'table' }, [
36 E('tr', { 'class': 'tr table-titles' }, [
37 E('th', { 'class': 'th' }, _('Hostname')),
38 E('th', { 'class': 'th' }, _('IPv4 address')),
39 E('th', { 'class': 'th' }, _('MAC address')),
40 E('th', { 'class': 'th' }, _('Lease time remaining'))
42 E('tr', { 'class': 'tr placeholder' }, [
43 E('td', { 'class': 'td' }, E('em', _('Collecting data...')))
50 CBILease6Status
= form
.DummyValue
.extend({
51 renderWidget: function(section_id
, option_id
, cfgvalue
) {
53 E('h4', _('Active DHCPv6 Leases')),
54 E('table', { 'id': 'lease6_status_table', 'class': 'table' }, [
55 E('tr', { 'class': 'tr table-titles' }, [
56 E('th', { 'class': 'th' }, _('Host')),
57 E('th', { 'class': 'th' }, _('IPv6 address')),
58 E('th', { 'class': 'th' }, _('DUID')),
59 E('th', { 'class': 'th' }, _('Lease time remaining'))
61 E('tr', { 'class': 'tr placeholder' }, [
62 E('td', { 'class': 'td' }, E('em', _('Collecting data...')))
69 function calculateNetwork(addr
, mask
) {
70 addr
= validation
.parseIPv4(addr
);
73 mask
= validation
.parseIPv4(network
.prefixToMask(+mask
));
75 mask
= validation
.parseIPv4(mask
);
77 if (addr
== null || mask
== null)
82 addr
[0] & (mask
[0] >>> 0 & 255),
83 addr
[1] & (mask
[1] >>> 0 & 255),
84 addr
[2] & (mask
[2] >>> 0 & 255),
85 addr
[3] & (mask
[3] >>> 0 & 255)
91 function getDHCPPools() {
92 return uci
.load('dhcp').then(function() {
93 let sections
= uci
.sections('dhcp', 'dhcp'),
94 tasks
= [], pools
= [];
96 for (var i
= 0; i
< sections
.length
; i
++) {
97 if (sections
[i
].ignore
== '1' || !sections
[i
].interface)
100 tasks
.push(network
.getNetwork(sections
[i
].interface).then(L
.bind(function(section_id
, net
) {
101 var cidr
= (net
.getIPAddrs()[0] || '').split('/');
103 if (cidr
.length
== 2) {
104 var net_mask
= calculateNetwork(cidr
[0], cidr
[1]);
107 section_id
: section_id
,
108 network
: net_mask
[0],
112 }, null, sections
[i
]['.name'])));
115 return Promise
.all(tasks
).then(function() {
121 function validateHostname(sid
, s
) {
122 if (s
== null || s
== '')
126 return _('Expecting: %s').format(_('valid hostname'));
128 var labels
= s
.replace(/^\.+|\.$/g, '').split(/\./);
130 for (var i
= 0; i
< labels
.length
; i
++)
131 if (!labels
[i
].match(/^[a-z0-9_](?:[a-z0-9-]{0,61}[a-z0-9])?$/i))
132 return _('Expecting: %s').format(_('valid hostname'));
137 function validateAddressList(sid
, s
) {
138 if (s
== null || s
== '')
141 var m
= s
.match(/^\/(.+)\/$/),
142 names
= m
? m
[1].split(/\//) : [ s
];
144 for (var i
= 0; i
< names
.length
; i
++) {
145 var res
= validateHostname(sid
, names
[i
]);
154 function validateServerSpec(sid
, s
) {
155 if (s
== null || s
== '')
158 var m
= s
.match(/^(?:\/(.+)\/)?(.*)$/);
160 return _('Expecting: %s').format(_('valid hostname'));
162 var res
= validateAddressList(sid
, m
[1]);
166 if (m
[2] == '' || m
[2] == '#')
169 // ipaddr%scopeid#srvport@source@interface#srcport
171 m
= m
[2].match(/^([0-9a-f:.]+)(?:%[^#@]+)?(?:#(\d+))?(?:@([0-9a-f:.]+)(?:@[^#]+)?(?:#(\d+))?)?$/);
174 return _('Expecting: %s').format(_('valid IP address'));
176 if (validation
.parseIPv4(m
[1])) {
177 if (m
[3] != null && !validation
.parseIPv4(m
[3]))
178 return _('Expecting: %s').format(_('valid IPv4 address'));
180 else if (validation
.parseIPv6(m
[1])) {
181 if (m
[3] != null && !validation
.parseIPv6(m
[3]))
182 return _('Expecting: %s').format(_('valid IPv6 address'));
185 return _('Expecting: %s').format(_('valid IP address'));
188 if ((m
[2] != null && +m
[2] > 65535) || (m
[4] != null && +m
[4] > 65535))
189 return _('Expecting: %s').format(_('valid port value'));
194 function validateMACAddr(pools
, sid
, s
) {
195 if (s
== null || s
== '')
198 var leases
= uci
.sections('dhcp', 'host'),
199 this_macs
= L
.toArray(s
).map(function(m
) { return m
.toUpperCase() });
201 for (var i
= 0; i
< pools
.length
; i
++) {
202 var this_net_mask
= calculateNetwork(uci
.get('dhcp', sid
, 'ip'), pools
[i
].netmask
);
207 for (var j
= 0; j
< leases
.length
; j
++) {
208 if (leases
[j
]['.name'] == sid
|| !leases
[j
].ip
)
211 var lease_net_mask
= calculateNetwork(leases
[j
].ip
, pools
[i
].netmask
);
213 if (!lease_net_mask
|| this_net_mask
[0] != lease_net_mask
[0])
216 var lease_macs
= L
.toArray(leases
[j
].mac
).map(function(m
) { return m
.toUpperCase() });
218 for (var k
= 0; k
< lease_macs
.length
; k
++)
219 for (var l
= 0; l
< this_macs
.length
; l
++)
220 if (lease_macs
[k
] == this_macs
[l
])
221 return _('The MAC address %h is already used by another static lease in the same DHCP pool').format(this_macs
[l
]);
237 render: function(hosts_duids_pools
) {
238 var has_dhcpv6
= L
.hasSystemFeature('dnsmasq', 'dhcpv6') || L
.hasSystemFeature('odhcpd'),
239 hosts
= hosts_duids_pools
[0],
240 duids
= hosts_duids_pools
[1],
241 pools
= hosts_duids_pools
[2],
244 console
.debug(pools
);
246 m
= new form
.Map('dhcp', _('DHCP and DNS'), _('Dnsmasq is a combined <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr>-Server and <abbr title="Domain Name System">DNS</abbr>-Forwarder for <abbr title="Network Address Translation">NAT</abbr> firewalls'));
248 s
= m
.section(form
.TypedSection
, 'dnsmasq', _('Server Settings'));
252 s
.tab('general', _('General Settings'));
253 s
.tab('files', _('Resolv and Hosts Files'));
254 s
.tab('tftp', _('TFTP Settings'));
255 s
.tab('advanced', _('Advanced Settings'));
256 s
.tab('leases', _('Static Leases'));
258 s
.taboption('general', form
.Flag
, 'domainneeded',
259 _('Domain required'),
260 _('Don\'t forward <abbr title="Domain Name System">DNS</abbr>-Requests without <abbr title="Domain Name System">DNS</abbr>-Name'));
262 s
.taboption('general', form
.Flag
, 'authoritative',
264 _('This is the only <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr> in the local network'));
267 s
.taboption('files', form
.Flag
, 'readethers',
268 _('Use <code>/etc/ethers</code>'),
269 _('Read <code>/etc/ethers</code> to configure the <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr>-Server'));
271 s
.taboption('files', form
.Value
, 'leasefile',
273 _('file where given <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr>-leases will be stored'));
275 s
.taboption('files', form
.Flag
, 'noresolv',
276 _('Ignore resolve file')).optional
= true;
278 o
= s
.taboption('files', form
.Value
, 'resolvfile',
280 _('local <abbr title="Domain Name System">DNS</abbr> file'));
282 o
.depends('noresolv', '0');
283 o
.placeholder
= '/tmp/resolv.conf.d/resolv.conf.auto';
287 s
.taboption('files', form
.Flag
, 'nohosts',
288 _('Ignore <code>/etc/hosts</code>')).optional
= true;
290 s
.taboption('files', form
.DynamicList
, 'addnhosts',
291 _('Additional Hosts files')).optional
= true;
293 o
= s
.taboption('advanced', form
.Flag
, 'quietdhcp',
294 _('Suppress logging'),
295 _('Suppress logging of the routine operation of these protocols'));
298 o
= s
.taboption('advanced', form
.Flag
, 'sequential_ip',
299 _('Allocate IP sequentially'),
300 _('Allocate IP addresses sequentially, starting from the lowest available address'));
303 o
= s
.taboption('advanced', form
.Flag
, 'boguspriv',
305 _('Do not forward reverse lookups for local networks'));
306 o
.default = o
.enabled
;
308 s
.taboption('advanced', form
.Flag
, 'filterwin2k',
310 _('Do not forward requests that cannot be answered by public name servers'));
313 s
.taboption('advanced', form
.Flag
, 'localise_queries',
314 _('Localise queries'),
315 _('Localise hostname depending on the requesting subnet if multiple IPs are available'));
317 if (L
.hasSystemFeature('dnsmasq', 'dnssec')) {
318 o
= s
.taboption('advanced', form
.Flag
, 'dnssec',
322 o
= s
.taboption('advanced', form
.Flag
, 'dnsseccheckunsigned',
323 _('DNSSEC check unsigned'),
324 _('Requires upstream supports DNSSEC; verify unsigned domain responses really come from unsigned domains'));
325 o
.default = o
.enabled
;
329 s
.taboption('general', form
.Value
, 'local',
331 _('Local domain specification. Names matching this domain are never forwarded and are resolved from DHCP or hosts files only'));
333 s
.taboption('general', form
.Value
, 'domain',
335 _('Local domain suffix appended to DHCP names and hosts file entries'));
337 s
.taboption('advanced', form
.Flag
, 'expandhosts',
339 _('Add local domain suffix to names served from hosts files'));
341 s
.taboption('advanced', form
.Flag
, 'nonegcache',
342 _('No negative cache'),
343 _('Do not cache negative replies, e.g. for not existing domains'));
345 s
.taboption('advanced', form
.Value
, 'serversfile',
346 _('Additional servers file'),
347 _('This file may contain lines like \'server=/domain/1.2.3.4\' or \'server=1.2.3.4\' for domain-specific or full upstream <abbr title="Domain Name System">DNS</abbr> servers.'));
349 s
.taboption('advanced', form
.Flag
, 'strictorder',
351 _('<abbr title="Domain Name System">DNS</abbr> servers will be queried in the order of the resolvfile')).optional
= true;
353 s
.taboption('advanced', form
.Flag
, 'allservers',
355 _('Query all available upstream <abbr title="Domain Name System">DNS</abbr> servers')).optional
= true;
357 o
= s
.taboption('advanced', form
.DynamicList
, 'bogusnxdomain', _('Bogus NX Domain Override'),
358 _('List of hosts that supply bogus NX domain results'));
361 o
.placeholder
= '67.215.65.132';
364 s
.taboption('general', form
.Flag
, 'logqueries',
366 _('Write received DNS requests to syslog')).optional
= true;
368 o
= s
.taboption('general', form
.DynamicList
, 'server', _('DNS forwardings'),
369 _('List of <abbr title="Domain Name System">DNS</abbr> servers to forward requests to'));
372 o
.placeholder
= '/example.org/10.1.2.3';
373 o
.validate
= validateServerSpec
;
376 o
= s
.taboption('general', form
.DynamicList
, 'address', _('Addresses'),
377 _('List of domains to force to an IP address.'));
380 o
.placeholder
= '/router.local/192.168.0.1';
383 o
= s
.taboption('general', form
.Flag
, 'rebind_protection',
384 _('Rebind protection'),
385 _('Discard upstream RFC1918 responses'));
390 o
= s
.taboption('general', form
.Flag
, 'rebind_localhost',
391 _('Allow localhost'),
392 _('Allow upstream responses in the 127.0.0.0/8 range, e.g. for RBL services'));
394 o
.depends('rebind_protection', '1');
397 o
= s
.taboption('general', form
.DynamicList
, 'rebind_domain',
398 _('Domain whitelist'),
399 _('List of domains to allow RFC1918 responses for'));
402 o
.depends('rebind_protection', '1');
403 o
.placeholder
= 'ihost.netflix.com';
404 o
.validate
= validateAddressList
;
407 o
= s
.taboption('advanced', form
.Value
, 'port',
408 _('<abbr title="Domain Name System">DNS</abbr> server port'),
409 _('Listening port for inbound DNS queries'));
416 o
= s
.taboption('advanced', form
.Value
, 'queryport',
417 _('<abbr title="Domain Name System">DNS</abbr> query port'),
418 _('Fixed source port for outbound DNS queries'));
422 o
.placeholder
= _('any');
425 o
= s
.taboption('advanced', form
.Value
, 'dhcpleasemax',
426 _('<abbr title="maximal">Max.</abbr> <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr> leases'),
427 _('Maximum allowed number of active DHCP leases'));
430 o
.datatype
= 'uinteger';
431 o
.placeholder
= _('unlimited');
434 o
= s
.taboption('advanced', form
.Value
, 'ednspacket_max',
435 _('<abbr title="maximal">Max.</abbr> <abbr title="Extension Mechanisms for Domain Name System">EDNS0</abbr> packet size'),
436 _('Maximum allowed size of EDNS.0 UDP packets'));
439 o
.datatype
= 'uinteger';
440 o
.placeholder
= 1280;
443 o
= s
.taboption('advanced', form
.Value
, 'dnsforwardmax',
444 _('<abbr title="maximal">Max.</abbr> concurrent queries'),
445 _('Maximum allowed number of concurrent DNS queries'));
448 o
.datatype
= 'uinteger';
451 o
= s
.taboption('advanced', form
.Value
, 'cachesize',
452 _('Size of DNS query cache'),
453 _('Number of cached DNS entries (max is 10000, 0 is no caching)'));
455 o
.datatype
= 'range(0,10000)';
458 s
.taboption('tftp', form
.Flag
, 'enable_tftp',
459 _('Enable TFTP server')).optional
= true;
461 o
= s
.taboption('tftp', form
.Value
, 'tftp_root',
462 _('TFTP server root'),
463 _('Root directory for files served via TFTP'));
466 o
.depends('enable_tftp', '1');
470 o
= s
.taboption('tftp', form
.Value
, 'dhcp_boot',
471 _('Network boot image'),
472 _('Filename of the boot image advertised to clients'));
475 o
.depends('enable_tftp', '1');
476 o
.placeholder
= 'pxelinux.0';
478 o
= s
.taboption('general', form
.Flag
, 'localservice',
479 _('Local Service Only'),
480 _('Limit DNS service to subnets interfaces on which we are serving DNS.'));
484 o
= s
.taboption('general', form
.Flag
, 'nonwildcard',
486 _('Bind dynamically to interfaces rather than wildcard address (recommended as linux default)'));
487 o
.default = o
.enabled
;
491 o
= s
.taboption('general', form
.DynamicList
, 'interface',
492 _('Listen Interfaces'),
493 _('Limit listening to these interfaces, and loopback.'));
496 o
= s
.taboption('general', form
.DynamicList
, 'notinterface',
497 _('Exclude interfaces'),
498 _('Prevent listening on these interfaces.'));
501 o
= s
.taboption('leases', form
.SectionValue
, '__leases__', form
.GridSection
, 'host', null,
502 _('Static leases are used to assign fixed IP addresses and symbolic hostnames to DHCP clients. They are also required for non-dynamic interface configurations where only hosts with a corresponding lease are served.') + '<br />' +
503 _('Use the <em>Add</em> Button to add a new lease entry. The <em>MAC address</em> identifies the host, the <em>IPv4 address</em> specifies the fixed address to use, and the <em>Hostname</em> is assigned as a symbolic name to the requesting host. The optional <em>Lease time</em> can be used to set non-standard host-specific lease time, e.g. 12h, 3d or infinite.'));
510 so
= ss
.option(form
.Value
, 'name', _('Hostname'));
511 so
.validate
= validateHostname
;
513 so
.write = function(section
, value
) {
514 uci
.set('dhcp', section
, 'name', value
);
515 uci
.set('dhcp', section
, 'dns', '1');
517 so
.remove = function(section
) {
518 uci
.unset('dhcp', section
, 'name');
519 uci
.unset('dhcp', section
, 'dns');
522 so
= ss
.option(form
.Value
, 'mac', _('<abbr title="Media Access Control">MAC</abbr>-Address'));
523 so
.datatype
= 'list(macaddr)';
525 so
.cfgvalue = function(section
) {
526 var macs
= L
.toArray(uci
.get('dhcp', section
, 'mac')),
529 for (var i
= 0, mac
; (mac
= macs
[i
]) != null; i
++)
530 if (/^([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2})$/.test(mac
))
531 result
.push('%02X:%02X:%02X:%02X:%02X:%02X'.format(
532 parseInt(RegExp
.$1, 16), parseInt(RegExp
.$2, 16),
533 parseInt(RegExp
.$3, 16), parseInt(RegExp
.$4, 16),
534 parseInt(RegExp
.$5, 16), parseInt(RegExp
.$6, 16)));
536 return result
.length
? result
.join(' ') : null;
538 so
.renderWidget = function(section_id
, option_index
, cfgvalue
) {
539 var node
= form
.Value
.prototype.renderWidget
.apply(this, [section_id
, option_index
, cfgvalue
]),
540 ipopt
= this.section
.children
.filter(function(o
) { return o
.option
== 'ip' })[0];
542 node
.addEventListener('cbi-dropdown-change', L
.bind(function(ipopt
, section_id
, ev
) {
543 var mac
= ev
.detail
.value
.value
;
544 if (mac
== null || mac
== '' || !hosts
[mac
])
547 var iphint
= L
.toArray(hosts
[mac
].ipaddrs
|| hosts
[mac
].ipv4
)[0];
551 var ip
= ipopt
.formvalue(section_id
);
552 if (ip
!= null && ip
!= '')
555 var node
= ipopt
.map
.findElement('id', ipopt
.cbid(section_id
));
557 dom
.callClassMethod(node
, 'setValue', iphint
);
558 }, this, ipopt
, section_id
));
562 so
.validate
= validateMACAddr
.bind(so
, pools
);
563 Object
.keys(hosts
).forEach(function(mac
) {
564 var hint
= hosts
[mac
].name
|| L
.toArray(hosts
[mac
].ipaddrs
|| hosts
[mac
].ipv4
)[0];
565 so
.value(mac
, hint
? '%s (%s)'.format(mac
, hint
) : mac
);
568 so
= ss
.option(form
.Value
, 'ip', _('<abbr title="Internet Protocol Version 4">IPv4</abbr>-Address'));
569 so
.datatype
= 'or(ip4addr,"ignore")';
570 so
.validate = function(section
, value
) {
571 var mac
= this.map
.lookupOption('mac', section
),
572 name
= this.map
.lookupOption('name', section
),
573 m
= mac
? mac
[0].formvalue(section
) : null,
574 n
= name
? name
[0].formvalue(section
) : null;
576 if ((m
== null || m
== '') && (n
== null || n
== ''))
577 return _('One of hostname or mac address must be specified!');
579 if (value
== null || value
== '' || value
== 'ignore')
582 var leases
= uci
.sections('dhcp', 'host');
584 for (var i
= 0; i
< leases
.length
; i
++)
585 if (leases
[i
]['.name'] != section
&& leases
[i
].ip
== value
)
586 return _('The IP address %h is already used by another static lease').format(value
);
589 for (var i
= 0; i
< pools
.length
; i
++) {
590 var net_mask
= calculateNetwork(value
, pools
[i
].netmask
);
592 if (net_mask
&& net_mask
[0] == pools
[i
].network
)
596 return _('The IP address is outside of any DHCP pool address range');
601 Object
.keys(hosts
).forEach(function(mac
) {
602 var addrs
= L
.toArray(hosts
[mac
].ipaddrs
|| hosts
[mac
].ipv4
);
604 for (var i
= 0; i
< addrs
.length
; i
++)
605 ipaddrs
[addrs
[i
]] = hosts
[mac
].name
;
608 L
.sortedKeys(ipaddrs
, null, 'addr').forEach(function(ipv4
) {
609 so
.value(ipv4
, ipaddrs
[ipv4
] ? '%s (%s)'.format(ipv4
, ipaddrs
[ipv4
]) : ipv4
);
612 so
= ss
.option(form
.Value
, 'leasetime', _('Lease time'));
615 so
= ss
.option(form
.Value
, 'duid', _('<abbr title="The DHCP Unique Identifier">DUID</abbr>'));
616 so
.datatype
= 'and(rangelength(20,36),hexstring)';
617 Object
.keys(duids
).forEach(function(duid
) {
618 so
.value(duid
, '%s (%s)'.format(duid
, duids
[duid
].hostname
|| duids
[duid
].macaddr
|| duids
[duid
].ip6addr
|| '?'));
621 so
= ss
.option(form
.Value
, 'hostid', _('<abbr title="Internet Protocol Version 6">IPv6</abbr>-Suffix (hex)'));
623 o
= s
.taboption('leases', CBILeaseStatus
, '__status__');
626 o
= s
.taboption('leases', CBILease6Status
, '__status6__');
628 return m
.render().then(function(mapEl
) {
629 poll
.add(function() {
630 return callDHCPLeases().then(function(leaseinfo
) {
631 var leases
= Array
.isArray(leaseinfo
.dhcp_leases
) ? leaseinfo
.dhcp_leases
: [],
632 leases6
= Array
.isArray(leaseinfo
.dhcp6_leases
) ? leaseinfo
.dhcp6_leases
: [];
634 cbi_update_table(mapEl
.querySelector('#lease_status_table'),
635 leases
.map(function(lease
) {
638 if (lease
.expires
=== false)
639 exp
= E('em', _('unlimited'));
640 else if (lease
.expires
<= 0)
641 exp
= E('em', _('expired'));
643 exp
= '%t'.format(lease
.expires
);
646 lease
.hostname
|| '?',
652 E('em', _('There are no active leases')));
655 cbi_update_table(mapEl
.querySelector('#lease6_status_table'),
656 leases6
.map(function(lease
) {
659 if (lease
.expires
=== false)
660 exp
= E('em', _('unlimited'));
661 else if (lease
.expires
<= 0)
662 exp
= E('em', _('expired'));
664 exp
= '%t'.format(lease
.expires
);
666 var hint
= lease
.macaddr
? hosts
[lease
.macaddr
] : null,
667 name
= hint
? (hint
.name
|| L
.toArray(hint
.ipaddrs
|| hint
.ipv4
)[0] || L
.toArray(hint
.ip6addrs
|| hint
.ipv6
)[0]) : null,
670 if (name
&& lease
.hostname
&& lease
.hostname
!= name
&& lease
.ip6addr
!= name
)
671 host
= '%s (%s)'.format(lease
.hostname
, name
);
672 else if (lease
.hostname
)
673 host
= lease
.hostname
;
679 lease
.ip6addrs
? lease
.ip6addrs
.join(' ') : lease
.ip6addr
,
684 E('em', _('There are no active leases')));