b539c3887a1d17cde919a683274ae5d23e8994f2
[project/luci.git] / modules / luci-mod-network / htdocs / luci-static / resources / view / network / dhcp.js
1 'use strict';
2 'require view';
3 'require dom';
4 'require poll';
5 'require rpc';
6 'require uci';
7 'require form';
8 'require network';
9 'require validation';
10 'require tools.widgets as widgets';
11
12 var callHostHints, callDUIDHints, callDHCPLeases, CBILeaseStatus, CBILease6Status;
13
14 callHostHints = rpc.declare({
15 object: 'luci-rpc',
16 method: 'getHostHints',
17 expect: { '': {} }
18 });
19
20 callDUIDHints = rpc.declare({
21 object: 'luci-rpc',
22 method: 'getDUIDHints',
23 expect: { '': {} }
24 });
25
26 callDHCPLeases = rpc.declare({
27 object: 'luci-rpc',
28 method: 'getDHCPLeases',
29 expect: { '': {} }
30 });
31
32 CBILeaseStatus = form.DummyValue.extend({
33 renderWidget: function(section_id, option_id, cfgvalue) {
34 return E([
35 E('h4', _('Active DHCP Leases')),
36 E('table', { 'id': 'lease_status_table', 'class': 'table' }, [
37 E('tr', { 'class': 'tr table-titles' }, [
38 E('th', { 'class': 'th' }, _('Hostname')),
39 E('th', { 'class': 'th' }, _('IPv4 address')),
40 E('th', { 'class': 'th' }, _('MAC address')),
41 E('th', { 'class': 'th' }, _('Lease time remaining'))
42 ]),
43 E('tr', { 'class': 'tr placeholder' }, [
44 E('td', { 'class': 'td' }, E('em', _('Collecting data...')))
45 ])
46 ])
47 ]);
48 }
49 });
50
51 CBILease6Status = form.DummyValue.extend({
52 renderWidget: function(section_id, option_id, cfgvalue) {
53 return E([
54 E('h4', _('Active DHCPv6 Leases')),
55 E('table', { 'id': 'lease6_status_table', 'class': 'table' }, [
56 E('tr', { 'class': 'tr table-titles' }, [
57 E('th', { 'class': 'th' }, _('Host')),
58 E('th', { 'class': 'th' }, _('IPv6 address')),
59 E('th', { 'class': 'th' }, _('DUID')),
60 E('th', { 'class': 'th' }, _('Lease time remaining'))
61 ]),
62 E('tr', { 'class': 'tr placeholder' }, [
63 E('td', { 'class': 'td' }, E('em', _('Collecting data...')))
64 ])
65 ])
66 ]);
67 }
68 });
69
70 function calculateNetwork(addr, mask) {
71 addr = validation.parseIPv4(String(addr));
72
73 if (!isNaN(mask))
74 mask = validation.parseIPv4(network.prefixToMask(+mask));
75 else
76 mask = validation.parseIPv4(String(mask));
77
78 if (addr == null || mask == null)
79 return null;
80
81 return [
82 [
83 addr[0] & (mask[0] >>> 0 & 255),
84 addr[1] & (mask[1] >>> 0 & 255),
85 addr[2] & (mask[2] >>> 0 & 255),
86 addr[3] & (mask[3] >>> 0 & 255)
87 ].join('.'),
88 mask.join('.')
89 ];
90 }
91
92 function getDHCPPools() {
93 return uci.load('dhcp').then(function() {
94 let sections = uci.sections('dhcp', 'dhcp'),
95 tasks = [], pools = [];
96
97 for (var i = 0; i < sections.length; i++) {
98 if (sections[i].ignore == '1' || !sections[i].interface)
99 continue;
100
101 tasks.push(network.getNetwork(sections[i].interface).then(L.bind(function(section_id, net) {
102 var cidr = net ? (net.getIPAddrs()[0] || '').split('/') : null;
103
104 if (cidr && cidr.length == 2) {
105 var net_mask = calculateNetwork(cidr[0], cidr[1]);
106
107 pools.push({
108 section_id: section_id,
109 network: net_mask[0],
110 netmask: net_mask[1]
111 });
112 }
113 }, null, sections[i]['.name'])));
114 }
115
116 return Promise.all(tasks).then(function() {
117 return pools;
118 });
119 });
120 }
121
122 function validateHostname(sid, s) {
123 if (s == null || s == '')
124 return true;
125
126 if (s.length > 256)
127 return _('Expecting: %s').format(_('valid hostname'));
128
129 var labels = s.replace(/^\*?\.?|\.$/g, '').split(/\./);
130
131 for (var i = 0; i < labels.length; i++)
132 if (!labels[i].match(/^[a-z0-9_](?:[a-z0-9-]{0,61}[a-z0-9])?$/i))
133 return _('Expecting: %s').format(_('valid hostname'));
134
135 return true;
136 }
137
138 function validateAddressList(sid, s) {
139 if (s == null || s == '')
140 return true;
141
142 var m = s.match(/^\/(.+)\/$/),
143 names = m ? m[1].split(/\//) : [ s ];
144
145 for (var i = 0; i < names.length; i++) {
146 var res = validateHostname(sid, names[i]);
147
148 if (res !== true)
149 return res;
150 }
151
152 return true;
153 }
154
155 function validateServerSpec(sid, s) {
156 if (s == null || s == '')
157 return true;
158
159 var m = s.match(/^(\/.*\/)?(.*)$/);
160 if (!m)
161 return _('Expecting: %s').format(_('valid hostname'));
162
163 if (m[1] != '//' && m[1] != '/#/') {
164 var res = validateAddressList(sid, m[1]);
165 if (res !== true)
166 return res;
167 }
168
169 if (m[2] == '' || m[2] == '#')
170 return true;
171
172 // ipaddr%scopeid#srvport@source@interface#srcport
173
174 m = m[2].match(/^([0-9a-f:.]+)(?:%[^#@]+)?(?:#(\d+))?(?:@([0-9a-f:.]+)(?:@[^#]+)?(?:#(\d+))?)?$/);
175
176 if (!m)
177 return _('Expecting: %s').format(_('valid IP address'));
178
179 if (validation.parseIPv4(m[1])) {
180 if (m[3] != null && !validation.parseIPv4(m[3]))
181 return _('Expecting: %s').format(_('valid IPv4 address'));
182 }
183 else if (validation.parseIPv6(m[1])) {
184 if (m[3] != null && !validation.parseIPv6(m[3]))
185 return _('Expecting: %s').format(_('valid IPv6 address'));
186 }
187 else {
188 return _('Expecting: %s').format(_('valid IP address'));
189 }
190
191 if ((m[2] != null && +m[2] > 65535) || (m[4] != null && +m[4] > 65535))
192 return _('Expecting: %s').format(_('valid port value'));
193
194 return true;
195 }
196
197 function validateMACAddr(pools, sid, s) {
198 if (s == null || s == '')
199 return true;
200
201 var leases = uci.sections('dhcp', 'host'),
202 this_macs = L.toArray(s).map(function(m) { return m.toUpperCase() });
203
204 for (var i = 0; i < pools.length; i++) {
205 var this_net_mask = calculateNetwork(this.section.formvalue(sid, 'ip'), pools[i].netmask);
206
207 if (!this_net_mask)
208 continue;
209
210 for (var j = 0; j < leases.length; j++) {
211 if (leases[j]['.name'] == sid || !leases[j].ip)
212 continue;
213
214 var lease_net_mask = calculateNetwork(leases[j].ip, pools[i].netmask);
215
216 if (!lease_net_mask || this_net_mask[0] != lease_net_mask[0])
217 continue;
218
219 var lease_macs = L.toArray(leases[j].mac).map(function(m) { return m.toUpperCase() });
220
221 for (var k = 0; k < lease_macs.length; k++)
222 for (var l = 0; l < this_macs.length; l++)
223 if (lease_macs[k] == this_macs[l])
224 return _('The MAC address %h is already used by another static lease in the same DHCP pool').format(this_macs[l]);
225 }
226 }
227
228 return true;
229 }
230
231 return view.extend({
232 load: function() {
233 return Promise.all([
234 callHostHints(),
235 callDUIDHints(),
236 getDHCPPools(),
237 network.getDevices()
238 ]);
239 },
240
241 render: function(hosts_duids_pools) {
242 var has_dhcpv6 = L.hasSystemFeature('dnsmasq', 'dhcpv6') || L.hasSystemFeature('odhcpd'),
243 hosts = hosts_duids_pools[0],
244 duids = hosts_duids_pools[1],
245 pools = hosts_duids_pools[2],
246 ndevs = hosts_duids_pools[3],
247 m, s, o, ss, so;
248
249 m = new form.Map('dhcp', _('DHCP and DNS'),
250 _('Dnsmasq is a lightweight <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr> server and <abbr title="Domain Name System">DNS</abbr> forwarder.'));
251
252 s = m.section(form.TypedSection, 'dnsmasq');
253 s.anonymous = true;
254 s.addremove = false;
255
256 s.tab('general', _('General Settings'));
257 s.tab('relay', _('Relay'));
258 s.tab('files', _('Resolv and Hosts Files'));
259 s.tab('pxe_tftp', _('PXE/TFTP Settings'));
260 s.tab('advanced', _('Advanced Settings'));
261 s.tab('leases', _('Static Leases'));
262 s.tab('hosts', _('Hostnames'));
263 s.tab('srvhosts', _('SRV'));
264 s.tab('mxhosts', _('MX'));
265 s.tab('ipsets', _('IP Sets'));
266
267 s.taboption('general', form.Flag, 'domainneeded',
268 _('Domain required'),
269 _('Do not forward DNS queries without dots or domain parts.'));
270
271 s.taboption('general', form.Flag, 'authoritative',
272 _('Authoritative'),
273 _('This is the only DHCP server in the local network.'));
274
275 s.taboption('general', form.Value, 'local',
276 _('Local server'),
277 _('Never forward matching domains and subdomains, resolve from DHCP or hosts files only.'));
278
279 s.taboption('general', form.Value, 'domain',
280 _('Local domain'),
281 _('Local domain suffix appended to DHCP names and hosts file entries.'));
282
283 o = s.taboption('general', form.Flag, 'logqueries',
284 _('Log queries'),
285 _('Write received DNS queries to syslog.'));
286 o.optional = true;
287
288 o = s.taboption('general', form.DynamicList, 'server',
289 _('DNS forwardings'),
290 _('List of upstream resolvers to forward queries to.'));
291 o.optional = true;
292 o.placeholder = '/example.org/10.1.2.3';
293 o.validate = validateServerSpec;
294
295 o = s.taboption('general', form.DynamicList, 'address',
296 _('Addresses'),
297 _('Resolve specified FQDNs to an IP.') + '<br />' +
298 _('Syntax: <code>/fqdn[/fqdn…]/[ipaddr]</code>.') + '<br />' +
299 _('<code>/#/</code> matches any domain. <code>/example.com/</code> returns NXDOMAIN.') + '<br />' +
300 _('<code>/example.com/#</code> returns NULL addresses (<code>0.0.0.0</code> and <code>::</code>) for example.com and its subdomains.'));
301 o.optional = true;
302 o.placeholder = '/router.local/router.lan/192.168.0.1';
303
304 o = s.taboption('general', form.DynamicList, 'ipset',
305 _('IP sets'),
306 _('List of IP sets to populate with the specified domain IPs.'));
307 o.optional = true;
308 o.placeholder = '/example.org/ipset,ipset6';
309
310 o = s.taboption('general', form.Flag, 'rebind_protection',
311 _('Rebind protection'),
312 _('Discard upstream responses containing <a href="%s">RFC1918</a> addresses.').format('https://datatracker.ietf.org/doc/html/rfc1918'));
313 o.rmempty = false;
314
315 o = s.taboption('general', form.Flag, 'rebind_localhost',
316 _('Allow localhost'),
317 _('Exempt <code>127.0.0.0/8</code> and <code>::1</code> from rebinding checks, e.g. for RBL services.'));
318 o.depends('rebind_protection', '1');
319
320 o = s.taboption('general', form.DynamicList, 'rebind_domain',
321 _('Domain whitelist'),
322 _('List of domains to allow RFC1918 responses for.'));
323 o.depends('rebind_protection', '1');
324 o.optional = true;
325 o.placeholder = 'ihost.netflix.com';
326 o.validate = validateAddressList;
327
328 o = s.taboption('general', form.Flag, 'localservice',
329 _('Local service only'),
330 _('Accept DNS queries only from hosts whose address is on a local subnet.'));
331 o.optional = false;
332 o.rmempty = false;
333
334 o = s.taboption('general', form.Flag, 'nonwildcard',
335 _('Non-wildcard'),
336 _('Bind dynamically to interfaces rather than wildcard address.'));
337 o.default = o.enabled;
338 o.optional = false;
339 o.rmempty = true;
340
341 o = s.taboption('general', form.DynamicList, 'interface',
342 _('Listen interfaces'),
343 _('Listen only on the specified interfaces, and loopback if not excluded explicitly.'));
344 o.optional = true;
345 o.placeholder = 'lan';
346
347 o = s.taboption('general', form.DynamicList, 'notinterface',
348 _('Exclude interfaces'),
349 _('Do not listen on the specified interfaces.'));
350 o.optional = true;
351 o.placeholder = 'loopback';
352
353 o = s.taboption('relay', form.SectionValue, '__relays__', form.TableSection, 'relay', null,
354 _('Relay DHCP requests elsewhere. OK: v4↔v4, v6↔v6. Not OK: v4↔v6, v6↔v4.')
355 + '<br />' + _('Note: you may also need a DHCP Proxy (currently unavailable) when specifying a non-standard Relay To port(<code>addr#port</code>).')
356 + '<br />' + _('You may add multiple unique Relay To on the same Listen addr.'));
357
358 ss = o.subsection;
359
360 ss.addremove = true;
361 ss.anonymous = true;
362 ss.sortable = true;
363 ss.rowcolors = true;
364 ss.nodescriptions = true;
365
366 so = ss.option(form.Value, 'id', _('ID'));
367 so.rmempty = false;
368 so.optional = true;
369
370 so = ss.option(widgets.NetworkSelect, 'interface', _('Interface'));
371 so.optional = true;
372 so.rmempty = false;
373 so.placeholder = 'lan';
374
375 so = ss.option(form.Value, 'local_addr', _('Listen address'));
376 so.rmempty = false;
377 so.datatype = 'ipaddr';
378
379 for (var family = 4; family <= 6; family += 2) {
380 for (var i = 0; i < ndevs.length; i++) {
381 var addrs = (family == 6) ? ndevs[i].getIP6Addrs() : ndevs[i].getIPAddrs();
382 for (var j = 0; j < addrs.length; j++)
383 so.value(addrs[j].split('/')[0]);
384 }
385 }
386
387 so = ss.option(form.Value, 'server_addr', _('Relay To address'));
388 so.rmempty = false;
389 so.optional = false;
390 so.placeholder = '192.168.10.1#535';
391
392 so.validate = function(section, value) {
393 var m = this.section.formvalue(section, 'local_addr'),
394 n = this.section.formvalue(section, 'server_addr'),
395 p;
396 if (n != null && n != '')
397 p = n.split('#');
398 if (p.length > 1 && !/^[0-9]+$/.test(p[1]))
399 return _('Expected port number.');
400 else
401 n = p[0];
402
403 if ((m == null || m == '') && (n == null || n == ''))
404 return _('Both Listen addr and Relay To must be specified.');
405
406 if ((validation.parseIPv6(m) && validation.parseIPv6(n)) ||
407 validation.parseIPv4(m) && validation.parseIPv4(n))
408 return true;
409 else
410 return _('Listen and Relay To IP family must be homogeneous.')
411 };
412
413 s.taboption('files', form.Flag, 'readethers',
414 _('Use <code>/etc/ethers</code>'),
415 _('Read <code>/etc/ethers</code> to configure the DHCP server.'));
416
417 s.taboption('files', form.Value, 'leasefile',
418 _('Lease file'),
419 _('File to store DHCP lease information.'));
420
421 o = s.taboption('files', form.Flag, 'noresolv',
422 _('Ignore resolv file'));
423 o.optional = true;
424
425 o = s.taboption('files', form.Value, 'resolvfile',
426 _('Resolv file'),
427 _('File with upstream resolvers.'));
428 o.depends('noresolv', '0');
429 o.placeholder = '/tmp/resolv.conf.d/resolv.conf.auto';
430 o.optional = true;
431
432 o = s.taboption('files', form.Flag, 'nohosts',
433 _('Ignore <code>/etc/hosts</code>'));
434 o.optional = true;
435
436 o = s.taboption('files', form.DynamicList, 'addnhosts',
437 _('Additional hosts files'));
438 o.optional = true;
439 o.placeholder = '/etc/dnsmasq.hosts';
440
441 o = s.taboption('advanced', form.Flag, 'quietdhcp',
442 _('Suppress logging'),
443 _('Suppress logging of the routine operation for the DHCP protocol.'));
444 o.optional = true;
445
446 o = s.taboption('advanced', form.Flag, 'sequential_ip',
447 _('Allocate IPs sequentially'),
448 _('Allocate IP addresses sequentially, starting from the lowest available address.'));
449 o.optional = true;
450
451 o = s.taboption('advanced', form.Flag, 'boguspriv',
452 _('Filter private'),
453 _('Do not forward reverse lookups for local networks.'));
454 o.default = o.enabled;
455
456 s.taboption('advanced', form.Flag, 'filterwin2k',
457 _('Filter SRV/SOA service discovery'),
458 _('Filters SRV/SOA service discovery, to avoid triggering dial-on-demand links.') + '<br />' +
459 _('May prevent VoIP or other services from working.'));
460
461 o = s.taboption('advanced', form.Flag, 'filter_aaaa',
462 _('Filter IPv6 AAAA records'),
463 _('Remove IPv6 addresses from the results and only return IPv4 addresses.') + '<br />' +
464 _('Can be useful if ISP has IPv6 nameservers but does not provide IPv6 routing.'));
465 o.optional = true;
466
467 o = s.taboption('advanced', form.Flag, 'filter_a',
468 _('Filter IPv4 A records'),
469 _('Remove IPv4 addresses from the results and only return IPv6 addresses.'));
470 o.optional = true;
471
472 s.taboption('advanced', form.Flag, 'localise_queries',
473 _('Localise queries'),
474 _('Return answers to DNS queries matching the subnet from which the query was received if multiple IPs are available.'));
475
476 if (L.hasSystemFeature('dnsmasq', 'dnssec')) {
477 o = s.taboption('advanced', form.Flag, 'dnssec',
478 _('DNSSEC'),
479 _('Validate DNS replies and cache DNSSEC data, requires upstream to support DNSSEC.'));
480 o.optional = true;
481
482 o = s.taboption('advanced', form.Flag, 'dnsseccheckunsigned',
483 _('DNSSEC check unsigned'),
484 _('Verify unsigned domain responses really come from unsigned domains.'));
485 o.default = o.enabled;
486 o.optional = true;
487 }
488
489 s.taboption('advanced', form.Flag, 'expandhosts',
490 _('Expand hosts'),
491 _('Add local domain suffix to names served from hosts files.'));
492
493 s.taboption('advanced', form.Flag, 'nonegcache',
494 _('No negative cache'),
495 _('Do not cache negative replies, e.g. for non-existent domains.'));
496
497 o = s.taboption('advanced', form.Value, 'serversfile',
498 _('Additional servers file'),
499 _('File listing upstream resolvers, optionally domain-specific, e.g. <code>server=1.2.3.4</code>, <code>server=/domain/1.2.3.4</code>.'));
500 o.placeholder = '/etc/dnsmasq.servers';
501
502 o = s.taboption('advanced', form.Flag, 'strictorder',
503 _('Strict order'),
504 _('Upstream resolvers will be queried in the order of the resolv file.'));
505 o.optional = true;
506
507 o = s.taboption('advanced', form.Flag, 'allservers',
508 _('All servers'),
509 _('Query all available upstream resolvers.'));
510 o.optional = true;
511
512 o = s.taboption('advanced', form.DynamicList, 'bogusnxdomain',
513 _('IPs to override with NXDOMAIN'),
514 _('List of IP addresses to convert into NXDOMAIN responses.'));
515 o.optional = true;
516 o.placeholder = '64.94.110.11';
517
518 o = s.taboption('advanced', form.Value, 'port',
519 _('DNS server port'),
520 _('Listening port for inbound DNS queries.'));
521 o.optional = true;
522 o.datatype = 'port';
523 o.placeholder = 53;
524
525 o = s.taboption('advanced', form.Value, 'queryport',
526 _('DNS query port'),
527 _('Fixed source port for outbound DNS queries.'));
528 o.optional = true;
529 o.datatype = 'port';
530 o.placeholder = _('any');
531
532 o = s.taboption('advanced', form.Value, 'dhcpleasemax',
533 _('Max. DHCP leases'),
534 _('Maximum allowed number of active DHCP leases.'));
535 o.optional = true;
536 o.datatype = 'uinteger';
537 o.placeholder = _('unlimited');
538
539 o = s.taboption('advanced', form.Value, 'ednspacket_max',
540 _('Max. EDNS0 packet size'),
541 _('Maximum allowed size of EDNS0 UDP packets.'));
542 o.optional = true;
543 o.datatype = 'uinteger';
544 o.placeholder = 1280;
545
546 o = s.taboption('advanced', form.Value, 'dnsforwardmax',
547 _('Max. concurrent queries'),
548 _('Maximum allowed number of concurrent DNS queries.'));
549 o.optional = true;
550 o.datatype = 'uinteger';
551 o.placeholder = 150;
552
553 o = s.taboption('advanced', form.Value, 'cachesize',
554 _('Size of DNS query cache'),
555 _('Number of cached DNS entries, 10000 is maximum, 0 is no caching.'));
556 o.optional = true;
557 o.datatype = 'range(0,10000)';
558 o.placeholder = 150;
559
560 o = s.taboption('pxe_tftp', form.Flag, 'enable_tftp',
561 _('Enable TFTP server'),
562 _('Enable the built-in single-instance TFTP server.'));
563 o.optional = true;
564
565 o = s.taboption('pxe_tftp', form.Value, 'tftp_root',
566 _('TFTP server root'),
567 _('Root directory for files served via TFTP. <em>Enable TFTP server</em> and <em>TFTP server root</em> turn on the TFTP server and serve files from <em>TFTP server root</em>.'));
568 o.depends('enable_tftp', '1');
569 o.optional = true;
570 o.placeholder = '/';
571
572 o = s.taboption('pxe_tftp', form.Value, 'dhcp_boot',
573 _('Network boot image'),
574 _('Filename of the boot image advertised to clients.'));
575 o.depends('enable_tftp', '1');
576 o.optional = true;
577 o.placeholder = 'pxelinux.0';
578
579 /* PXE - https://openwrt.org/docs/guide-user/base-system/dhcp#booting_options */
580 o = s.taboption('pxe_tftp', form.SectionValue, '__pxe__', form.GridSection, 'boot', null,
581 _('Special <abbr title="Preboot eXecution Environment">PXE</abbr> boot options for Dnsmasq.'));
582 ss = o.subsection;
583 ss.addremove = true;
584 ss.anonymous = true;
585 ss.nodescriptions = true;
586
587 so = ss.option(form.Value, 'filename',
588 _('Filename'),
589 _('Host requests this filename from the boot server.'));
590 so.optional = false;
591 so.placeholder = 'pxelinux.0';
592
593 so = ss.option(form.Value, 'servername',
594 _('Server name'),
595 _('The hostname of the boot server'));
596 so.optional = false;
597 so.placeholder = 'myNAS';
598
599 so = ss.option(form.Value, 'serveraddress',
600 _('Server address'),
601 _('The IP address of the boot server'));
602 so.optional = false;
603 so.placeholder = '192.168.1.2';
604
605 so = ss.option(form.DynamicList, 'dhcp_option',
606 _('DHCP Options'),
607 _('Options for the Network-ID. (Note: needs also Network-ID.) E.g. "<code>42,192.168.1.4</code>" for NTP server, "<code>3,192.168.4.4</code>" for default route. <code>0.0.0.0</code> means "the address of the system running dnsmasq".'));
608 so.optional = true;
609 so.placeholder = '42,192.168.1.4';
610
611 so = ss.option(widgets.DeviceSelect, 'networkid',
612 _('Network-ID'),
613 _('Apply DHCP Options to this net. (Empty = all clients).'));
614 so.optional = true;
615 so.noaliases = true;
616
617 so = ss.option(form.Flag, 'force',
618 _('Force'),
619 _('Always send DHCP Options. Sometimes needed, with e.g. PXELinux.'));
620 so.optional = true;
621
622 so = ss.option(form.Value, 'instance',
623 _('Instance'),
624 _('Dnsmasq instance to which this boot section is bound. If unspecified, the section is valid for all dnsmasq instances.'));
625 so.optional = true;
626
627 Object.values(L.uci.sections('dhcp', 'dnsmasq')).forEach(function(val, index) {
628 so.value(index, '%s (Domain: %s, Local: %s)'.format(index, val.domain || '?', val.local || '?'));
629 });
630
631 o = s.taboption('srvhosts', form.SectionValue, '__srvhosts__', form.TableSection, 'srvhost', null,
632 _('Bind service records to a domain name: specify the location of services. See <a href="%s">RFC2782</a>.').format('https://datatracker.ietf.org/doc/html/rfc2782')
633 + '<br />' + _('_service: _sip, _ldap, _imap, _stun, _xmpp-client, … . (Note: while _http is possible, no browsers support SRV records.)')
634 + '<br />' + _('_proto: _tcp, _udp, _sctp, _quic, … .')
635 + '<br />' + _('You may add multiple records for the same Target.')
636 + '<br />' + _('Larger weights (of the same prio) are given a proportionately higher probability of being selected.'));
637
638 ss = o.subsection;
639
640 ss.addremove = true;
641 ss.anonymous = true;
642 ss.sortable = true;
643 ss.rowcolors = true;
644
645 so = ss.option(form.Value, 'srv', _('SRV'), _('Syntax: <code>_service._proto.example.com</code>.'));
646 so.rmempty = false;
647 so.datatype = 'hostname';
648 so.placeholder = '_sip._tcp.example.com';
649
650 so = ss.option(form.Value, 'target', _('Target'), _('CNAME or fqdn'));
651 so.rmempty = false;
652 so.datatype = 'hostname';
653 so.placeholder = 'sip.example.com';
654
655 so = ss.option(form.Value, 'port', _('Port'));
656 so.rmempty = false;
657 so.datatype = 'port';
658 so.placeholder = '5060';
659
660 so = ss.option(form.Value, 'class', _('Priority'), _('Ordinal: lower comes first.'));
661 so.rmempty = true;
662 so.datatype = 'range(0,65535)';
663 so.placeholder = '10';
664
665 so = ss.option(form.Value, 'weight', _('Weight'));
666 so.rmempty = true;
667 so.datatype = 'range(0,65535)';
668 so.placeholder = '50';
669
670 o = s.taboption('mxhosts', form.SectionValue, '__mxhosts__', form.TableSection, 'mxhost', null,
671 _('Bind service records to a domain name: specify the location of services.')
672 + '<br />' + _('You may add multiple records for the same domain.'));
673
674 ss = o.subsection;
675
676 ss.addremove = true;
677 ss.anonymous = true;
678 ss.sortable = true;
679 ss.rowcolors = true;
680 ss.nodescriptions = true;
681
682 so = ss.option(form.Value, 'domain', _('Domain'));
683 so.rmempty = false;
684 so.datatype = 'hostname';
685 so.placeholder = 'example.com';
686
687 so = ss.option(form.Value, 'relay', _('Relay'));
688 so.rmempty = false;
689 so.datatype = 'hostname';
690 so.placeholder = 'relay.example.com';
691
692 so = ss.option(form.Value, 'pref', _('Priority'), _('Ordinal: lower comes first.'));
693 so.rmempty = true;
694 so.datatype = 'range(0,65535)';
695 so.placeholder = '0';
696
697 o = s.taboption('hosts', form.SectionValue, '__hosts__', form.GridSection, 'domain', null,
698 _('Hostnames are used to bind a domain name to an IP address. This setting is redundant for hostnames already configured with static leases, but it can be useful to rebind an FQDN.'));
699
700 ss = o.subsection;
701
702 ss.addremove = true;
703 ss.anonymous = true;
704 ss.sortable = true;
705
706 so = ss.option(form.Value, 'name', _('Hostname'));
707 so.rmempty = false;
708 so.datatype = 'hostname';
709
710 so = ss.option(form.Value, 'ip', _('IP address'));
711 so.rmempty = false;
712 so.datatype = 'ipaddr';
713
714 var ipaddrs = {};
715
716 Object.keys(hosts).forEach(function(mac) {
717 var addrs = L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4);
718
719 for (var i = 0; i < addrs.length; i++)
720 ipaddrs[addrs[i]] = hosts[mac].name || mac;
721 });
722
723 L.sortedKeys(ipaddrs, null, 'addr').forEach(function(ipv4) {
724 so.value(ipv4, '%s (%s)'.format(ipv4, ipaddrs[ipv4]));
725 });
726
727 o = s.taboption('ipsets', form.SectionValue, '__ipsets__', form.GridSection, 'ipset', null,
728 _('List of IP sets to populate with the specified domain IPs.'));
729
730 ss = o.subsection;
731
732 ss.addremove = true;
733 ss.anonymous = true;
734 ss.sortable = true;
735
736 so = ss.option(form.DynamicList, 'name', _('IP set'));
737 so.rmempty = false;
738 so.datatype = 'string';
739
740 so = ss.option(form.DynamicList, 'domain', _('Domain'));
741 so.rmempty = false;
742 so.datatype = 'hostname';
743
744 o = s.taboption('leases', form.SectionValue, '__leases__', form.GridSection, 'host', null,
745 _('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 />' +
746 _('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.'));
747
748 ss = o.subsection;
749
750 ss.addremove = true;
751 ss.anonymous = true;
752 ss.sortable = true;
753
754 so = ss.option(form.Value, 'name', _('Hostname'));
755 so.validate = validateHostname;
756 so.rmempty = true;
757 so.write = function(section, value) {
758 uci.set('dhcp', section, 'name', value);
759 uci.set('dhcp', section, 'dns', '1');
760 };
761 so.remove = function(section) {
762 uci.unset('dhcp', section, 'name');
763 uci.unset('dhcp', section, 'dns');
764 };
765
766 so = ss.option(form.Value, 'mac', _('MAC address'));
767 so.datatype = 'list(macaddr)';
768 so.rmempty = true;
769 so.cfgvalue = function(section) {
770 var macs = L.toArray(uci.get('dhcp', section, 'mac')),
771 result = [];
772
773 for (var i = 0, mac; (mac = macs[i]) != null; i++)
774 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))
775 result.push('%02X:%02X:%02X:%02X:%02X:%02X'.format(
776 parseInt(RegExp.$1, 16), parseInt(RegExp.$2, 16),
777 parseInt(RegExp.$3, 16), parseInt(RegExp.$4, 16),
778 parseInt(RegExp.$5, 16), parseInt(RegExp.$6, 16)));
779
780 return result.length ? result.join(' ') : null;
781 };
782 so.renderWidget = function(section_id, option_index, cfgvalue) {
783 var node = form.Value.prototype.renderWidget.apply(this, [section_id, option_index, cfgvalue]),
784 ipopt = this.section.children.filter(function(o) { return o.option == 'ip' })[0];
785
786 node.addEventListener('cbi-dropdown-change', L.bind(function(ipopt, section_id, ev) {
787 var mac = ev.detail.value.value;
788 if (mac == null || mac == '' || !hosts[mac])
789 return;
790
791 var iphint = L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4)[0];
792 if (iphint == null)
793 return;
794
795 var ip = ipopt.formvalue(section_id);
796 if (ip != null && ip != '')
797 return;
798
799 var node = ipopt.map.findElement('id', ipopt.cbid(section_id));
800 if (node)
801 dom.callClassMethod(node, 'setValue', iphint);
802 }, this, ipopt, section_id));
803
804 return node;
805 };
806 so.validate = validateMACAddr.bind(so, pools);
807 Object.keys(hosts).forEach(function(mac) {
808 var hint = hosts[mac].name || L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4)[0];
809 so.value(mac, hint ? '%s (%s)'.format(mac, hint) : mac);
810 });
811
812 so = ss.option(form.Value, 'ip', _('IPv4 address'));
813 so.datatype = 'or(ip4addr,"ignore")';
814 so.validate = function(section, value) {
815 var m = this.section.formvalue(section, 'mac'),
816 n = this.section.formvalue(section, 'name');
817
818 if ((m == null || m == '') && (n == null || n == ''))
819 return _('One of hostname or MAC address must be specified!');
820
821 if (value == null || value == '' || value == 'ignore')
822 return true;
823
824 var leases = uci.sections('dhcp', 'host');
825
826 for (var i = 0; i < leases.length; i++)
827 if (leases[i]['.name'] != section && leases[i].ip == value)
828 return _('The IP address %h is already used by another static lease').format(value);
829
830 for (var i = 0; i < pools.length; i++) {
831 var net_mask = calculateNetwork(value, pools[i].netmask);
832
833 if (net_mask && net_mask[0] == pools[i].network)
834 return true;
835 }
836
837 return _('The IP address is outside of any DHCP pool address range');
838 };
839
840 L.sortedKeys(ipaddrs, null, 'addr').forEach(function(ipv4) {
841 so.value(ipv4, ipaddrs[ipv4] ? '%s (%s)'.format(ipv4, ipaddrs[ipv4]) : ipv4);
842 });
843
844 so = ss.option(form.Value, 'leasetime', _('Lease time'));
845 so.rmempty = true;
846
847 so = ss.option(form.Value, 'duid', _('DUID'));
848 so.datatype = 'and(rangelength(20,36),hexstring)';
849 Object.keys(duids).forEach(function(duid) {
850 so.value(duid, '%s (%s)'.format(duid, duids[duid].hostname || duids[duid].macaddr || duids[duid].ip6addr || '?'));
851 });
852
853 so = ss.option(form.Value, 'hostid', _('IPv6 suffix (hex)'));
854
855 o = s.taboption('leases', CBILeaseStatus, '__status__');
856
857 if (has_dhcpv6)
858 o = s.taboption('leases', CBILease6Status, '__status6__');
859
860 return m.render().then(function(mapEl) {
861 poll.add(function() {
862 return callDHCPLeases().then(function(leaseinfo) {
863 var leases = Array.isArray(leaseinfo.dhcp_leases) ? leaseinfo.dhcp_leases : [],
864 leases6 = Array.isArray(leaseinfo.dhcp6_leases) ? leaseinfo.dhcp6_leases : [];
865
866 cbi_update_table(mapEl.querySelector('#lease_status_table'),
867 leases.map(function(lease) {
868 var exp;
869
870 if (lease.expires === false)
871 exp = E('em', _('unlimited'));
872 else if (lease.expires <= 0)
873 exp = E('em', _('expired'));
874 else
875 exp = '%t'.format(lease.expires);
876
877 var hint = lease.macaddr ? hosts[lease.macaddr] : null,
878 name = hint ? hint.name : null,
879 host = null;
880
881 if (name && lease.hostname && lease.hostname != name)
882 host = '%s (%s)'.format(lease.hostname, name);
883 else if (lease.hostname)
884 host = lease.hostname;
885
886 return [
887 host || '-',
888 lease.ipaddr,
889 lease.macaddr,
890 exp
891 ];
892 }),
893 E('em', _('There are no active leases')));
894
895 if (has_dhcpv6) {
896 cbi_update_table(mapEl.querySelector('#lease6_status_table'),
897 leases6.map(function(lease) {
898 var exp;
899
900 if (lease.expires === false)
901 exp = E('em', _('unlimited'));
902 else if (lease.expires <= 0)
903 exp = E('em', _('expired'));
904 else
905 exp = '%t'.format(lease.expires);
906
907 var hint = lease.macaddr ? hosts[lease.macaddr] : null,
908 name = hint ? (hint.name || L.toArray(hint.ipaddrs || hint.ipv4)[0] || L.toArray(hint.ip6addrs || hint.ipv6)[0]) : null,
909 host = null;
910
911 if (name && lease.hostname && lease.hostname != name && lease.ip6addr != name)
912 host = '%s (%s)'.format(lease.hostname, name);
913 else if (lease.hostname)
914 host = lease.hostname;
915 else if (name)
916 host = name;
917
918 return [
919 host || '-',
920 lease.ip6addrs ? lease.ip6addrs.join(' ') : lease.ip6addr,
921 lease.duid,
922 exp
923 ];
924 }),
925 E('em', _('There are no active leases')));
926 }
927 });
928 });
929
930 return mapEl;
931 });
932 }
933 });