-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dnsmasq: Backend migration and add dhcp support for #8329
This rather large commit implements most relevant dhcp options and rewrites dnsmasq's backend. By default dnsmasq is disabled, eventually we do want dnsmasq enabled for dhcp services by default, but dns itself disabled. For this reason we support port "0" as implemented at dnsmasq (not listening for dns). For cases where users want to integrate dns and dhcp services, the advise is to make dnsmasq listen on a non standard port and point unbound to the zones where dnsmasq is responsible for. This has the advantage of a direct connection between dhcp registered hosts and the requesting service. In these cases dnsmasq's dns service acts like a "connector". In the long run we should deprecate `regdhcpstatic` and `regdhcp` as these either belong to legacy isc-dhcp or hook kea entries (which are better served via unbound). The first mvc migration phase implemented IndexController.php, which we rename to SettingsController.php now as these results in more logical ui endpoints. Since we don't bind to addresses directly (unless specifically configured and adviced only for static setups), we can skip the newwanip event which means we don't restart the service on interface changes. dnsmasq is able to filter the relevant networks on the fly, which is the advised scenario and can cope more easily with changes. When different clients need to receive different options, we can use "tags" now. Requests can add tags to filter options which will be offered to the client, in the most simple scenario one would tag on a range or a host reservation, but more advanced choices can also be achieved using match statements (for example architecture [client-arch])
- Loading branch information
1 parent
2cc3610
commit 15b7945
Showing
26 changed files
with
1,419 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (C) 2025 Deciso B.V. | ||
* Copyright (C) 2014-2023 Franco Fichtner <[email protected]> | ||
* Copyright (C) 2010 Ermal Luçi | ||
* Copyright (C) 2005-2006 Colin Smith <[email protected]> | ||
|
@@ -40,8 +41,7 @@ function dnsmasq_configure() | |
{ | ||
return [ | ||
'dns' => ['dnsmasq_configure_do'], | ||
'local' => ['dnsmasq_configure_do'], | ||
'newwanip' => ['dnsmasq_configure_do'], | ||
'local' => ['dnsmasq_configure_do'] | ||
]; | ||
} | ||
|
||
|
@@ -90,6 +90,48 @@ function dnsmasq_xmlrpc_sync() | |
return $result; | ||
} | ||
|
||
/** | ||
* Register automatic firewall rules | ||
*/ | ||
function dnsmasq_firewall(\OPNsense\Firewall\Plugin $fw) | ||
{ | ||
global $config; | ||
|
||
$mdl = new \OPNsense\Dnsmasq\Dnsmasq(); | ||
if (!$mdl->enable->isEmpty() && !$mdl->dhcp_default_fw_rules->isEmpty()) { | ||
$dhcp_ifs = $mdl->getDhcpInterfaces(); | ||
if (empty($dhcp_ifs)) { | ||
return; | ||
} | ||
foreach ($fw->getInterfaceMapping() as $intf => $intfinfo) { | ||
if (in_array($intf, $dhcp_ifs)) { | ||
$baserule = [ | ||
'interface' => $intf, | ||
'protocol' => 'udp', | ||
'log' => !isset($config['syslog']['nologdefaultpass']), | ||
'#ref' => "ui/dnsmasq/settings#general", | ||
'descr' => 'allow access to DHCP server' | ||
]; | ||
$fw->registerFilterRule( | ||
1, | ||
['direction' => 'in', 'from_port' => 68, 'to' => '255.255.255.255', 'to_port' => 67], | ||
$baserule | ||
); | ||
$fw->registerFilterRule( | ||
1, | ||
['direction' => 'in', 'from_port' => 68, 'to' => '(self)', 'to_port' => 67], | ||
$baserule | ||
); | ||
$fw->registerFilterRule( | ||
1, | ||
['direction' => 'out', 'from_port' => 67, 'from' => '(self)', 'to_port' => 68], | ||
$baserule | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function dnsmasq_configure_do($verbose = false) | ||
{ | ||
global $config; | ||
|
@@ -103,102 +145,12 @@ function dnsmasq_configure_do($verbose = false) | |
|
||
service_log('Starting Dnsmasq DNS...', $verbose); | ||
|
||
$args = ''; | ||
if (!isset($config['system']['webgui']['nodnsrebindcheck'])) { | ||
$args .= '--rebind-localhost-ok --stop-dns-rebind'; | ||
} | ||
|
||
$args .= ' -H /var/etc/dnsmasq-hosts '; | ||
$args .= ' -H /var/etc/dnsmasq-leases '; | ||
|
||
/* Setup listen port, if non-default */ | ||
if (isset($config['dnsmasq']['port']) && is_port($config['dnsmasq']['port'])) { | ||
$args .= " --port={$config['dnsmasq']['port']} "; | ||
} | ||
|
||
if (!empty($config['dnsmasq']['interface'])) { | ||
$ifs = []; | ||
foreach (explode(',', $config['dnsmasq']['interface']) as $ifname) { | ||
if (!empty($config['interfaces'][$ifname]) && !empty($config['interfaces'][$ifname]['if'])) { | ||
$ifs[] = $config['interfaces'][$ifname]['if']; | ||
} | ||
} | ||
$args .= " --interface=" . implode(',', $ifs) . " "; | ||
|
||
if (!empty($addresses) && !empty($config['dnsmasq']['strictbind'])) { | ||
$args .= ' --bind-interfaces '; | ||
} | ||
} | ||
|
||
if (!empty($config['dnsmasq']['no_private_reverse'])) { | ||
$args .= ' --bogus-priv '; | ||
} | ||
|
||
foreach (config_read_array('dnsmasq', 'domainoverrides') as $override) { | ||
$ip = $override['ip']; | ||
if (!empty($ip) && !empty($override['port'])) { | ||
$ip .= '#' . $override['port']; | ||
} | ||
if (!empty($ip) && !empty($override['srcip'])) { | ||
$ip .= '@' . $override['srcip']; | ||
} | ||
|
||
$args .= ' --server=' . escapeshellarg('/' . $override['domain'] . '/' . $ip); | ||
|
||
if (!isset($config['system']['webgui']['nodnsrebindcheck'])) { | ||
$args .= ' --rebind-domain-ok=' . escapeshellarg('/' . $override['domain'] . '/') . ' '; | ||
} | ||
} | ||
|
||
if (!empty($config['dnsmasq']['strict_order'])) { | ||
$args .= ' --strict-order '; | ||
} else { | ||
$args .= ' --all-servers '; | ||
} | ||
|
||
if (!empty($config['dnsmasq']['domain_needed'])) { | ||
$args .= ' --domain-needed '; | ||
} | ||
|
||
if (!empty($config['dnsmasq']['dnssec'])) { | ||
$args .= ' --dnssec '; | ||
$args .= ' --trust-anchor=.,20326,8,2,E06D44B80B8F1D39A95C0B0D7C65D08458E880409BBC683457104237C7F8EC8D '; | ||
$args .= ' --trust-anchor=.,38696,8,2,683D2D0ACB8C9B712A1948B27F741219298D0A450D612C483AF444A4C0FB2B16 '; | ||
} | ||
|
||
if (!empty($config['dnsmasq']['log_queries'])) { | ||
$args .= ' --log-queries=extra '; | ||
} | ||
|
||
if (!empty($config['dnsmasq']['no_hosts'])) { | ||
$args .= ' --no-hosts '; | ||
} | ||
|
||
if (!empty($config['dnsmasq']['dns_forward_max'])) { | ||
$args .= " --dns-forward-max={$config['dnsmasq']['dns_forward_max']} "; | ||
} else { | ||
$args .= ' --dns-forward-max=5000 '; | ||
} | ||
|
||
if (!empty($config['dnsmasq']['cache_size'])) { | ||
$args .= " --cache-size={$config['dnsmasq']['cache_size']} "; | ||
} else { | ||
$args .= ' --cache-size=10000 '; | ||
} | ||
|
||
if (!empty($config['dnsmasq']['local_ttl'])) { | ||
$args .= " --local-ttl={$config['dnsmasq']['local_ttl']} "; | ||
} else { | ||
$args .= ' --local-ttl=1 '; | ||
} | ||
|
||
$args .= ' --conf-dir=/usr/local/etc/dnsmasq.conf.d,\*.conf '; | ||
|
||
_dnsmasq_add_host_entries(); | ||
|
||
mwexec("/usr/local/sbin/dnsmasq {$args}"); | ||
mwexec("/usr/local/sbin/dnsmasq"); | ||
|
||
if (!empty($config['dnsmasq']['regdhcp'])) { | ||
/* XXX: deprecate when moving ISC to plugins ? */ | ||
$domain = $config['system']['domain']; | ||
if (!empty($config['dnsmasq']['regdhcpdomain'])) { | ||
$domain = $config['dnsmasq']['regdhcpdomain']; | ||
|
@@ -222,11 +174,11 @@ function _dnsmasq_add_host_entries() | |
} | ||
|
||
foreach ($dnsmasqcfg['hosts'] as $host) { | ||
if (!empty($host['host'])) { | ||
if (!empty($host['host']) && empty($host['domain'])) { | ||
$lhosts .= "{$host['ip']}\t{$host['host']}\n"; | ||
} elseif (!empty($host['host'])) { | ||
/* XXX: The question is if we do want "host" as a global alias */ | ||
$lhosts .= "{$host['ip']}\t{$host['host']}.{$host['domain']} {$host['host']}\n"; | ||
} else { | ||
$lhosts .= "{$host['ip']}\t{$host['domain']}\n"; | ||
} | ||
if (!empty($host['aliases'])) { | ||
foreach (explode(",", $host['aliases']) as $alias) { | ||
|
@@ -240,6 +192,7 @@ function _dnsmasq_add_host_entries() | |
} | ||
|
||
if (!empty($dnsmasqcfg['regdhcpstatic'])) { | ||
/* XXX: deprecate when ISC is moved to plugins? It doesn't make much sense to offer these registrations for KEA*/ | ||
foreach (plugins_run('static_mapping', [null, true, legacy_interfaces_details()]) as $map) { | ||
foreach ($map as $host) { | ||
if (empty($host['hostname'])) { | ||
|
79 changes: 79 additions & 0 deletions
79
src/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/Api/LeasesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (C) 2025 Deciso B.V. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | ||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | ||
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
namespace OPNsense\Dnsmasq\Api; | ||
|
||
use OPNsense\Base\ApiControllerBase; | ||
use OPNsense\Core\Backend; | ||
use OPNsense\Core\Config; | ||
|
||
class LeasesController extends ApiControllerBase | ||
{ | ||
public function searchAction() | ||
{ | ||
$selected_interfaces = $this->request->get('selected_interfaces'); | ||
$backend = new Backend(); | ||
$interfaces = []; | ||
|
||
$leases = json_decode($backend->configdpRun('dnsmasq list leases'), true) ?? []; | ||
$ifconfig = json_decode($backend->configdRun('interface list ifconfig'), true); | ||
$mac_db = json_decode($backend->configdRun('interface list macdb'), true) ?? []; | ||
|
||
$ifmap = []; | ||
foreach (Config::getInstance()->object()->interfaces->children() as $if => $if_props) { | ||
$ifmap[(string)$if_props->if] = [ | ||
'descr' => (string)$if_props->descr ?: strtoupper($if), | ||
'key' => $if | ||
]; | ||
} | ||
|
||
if (!empty($leases) && isset($leases['records'])) { | ||
$records = $leases['records']; | ||
foreach ($records as &$record) { | ||
$record['if_descr'] = ''; | ||
$record['if_name'] = ''; | ||
if (!empty($record['if']) && isset($ifmap[$record['if']])) { | ||
$record['if_descr'] = $ifmap[$record['if']]['descr']; | ||
$record['if_name'] = $ifmap[$record['if']]['key']; | ||
$interfaces[$ifmap[$record['if']]['key']] = $ifmap[$record['if']]['descr']; | ||
} | ||
$mac = strtoupper(substr(str_replace(':', '', $record['hwaddr']), 0, 6)); | ||
$record['mac_info'] = isset($mac_db[$mac]) ? $mac_db[$mac] : ''; | ||
} | ||
} else { | ||
$records = []; | ||
} | ||
|
||
$response = $this->searchRecordsetBase($records, null, 'address', function ($key) use ($selected_interfaces) { | ||
return empty($selected_interfaces) || in_array($key['if_name'], $selected_interfaces); | ||
}); | ||
|
||
$response['interfaces'] = $interfaces; | ||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.