Skip to content

Commit

Permalink
dnsmasq: Backend migration and add dhcp support for #8329
Browse files Browse the repository at this point in the history
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
AdSchellevis committed Feb 19, 2025
1 parent 2cc3610 commit 15b7945
Show file tree
Hide file tree
Showing 26 changed files with 1,419 additions and 250 deletions.
16 changes: 14 additions & 2 deletions plist
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,16 @@
/usr/local/opnsense/mvc/app/controllers/OPNsense/Diagnostics/forms/ping.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Diagnostics/forms/portprobe.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Diagnostics/forms/traceroute.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/Api/LeasesController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/Api/ServiceController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/Api/SettingsController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/IndexController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/LeasesController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/SettingsController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/forms/dialogDHCPboot.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/forms/dialogDHCPmatch.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/forms/dialogDHCPoption.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/forms/dialogDHCPrange.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/forms/dialogDHCPtag.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/forms/dialogDomainOverride.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/forms/dialogHostOverride.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Dnsmasq/forms/general.xml
Expand Down Expand Up @@ -921,7 +928,8 @@
/usr/local/opnsense/mvc/app/views/OPNsense/Diagnostics/traffic.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Diagnostics/treeview.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Diagnostics/vip.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Dnsmasq/index.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Dnsmasq/leases.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Dnsmasq/settings.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Firewall/alias.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Firewall/alias_util.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Firewall/category.volt
Expand Down Expand Up @@ -1072,12 +1080,14 @@
/usr/local/opnsense/scripts/dhcp/cleanup_leases4.php
/usr/local/opnsense/scripts/dhcp/cleanup_leases6.php
/usr/local/opnsense/scripts/dhcp/dnsmasq_watcher.py
/usr/local/opnsense/scripts/dhcp/get_dnsmasq_leases.py
/usr/local/opnsense/scripts/dhcp/get_kea_leases.py
/usr/local/opnsense/scripts/dhcp/get_leases.py
/usr/local/opnsense/scripts/dhcp/get_leases6.py
/usr/local/opnsense/scripts/dhcp/prefixes.php
/usr/local/opnsense/scripts/dhcp/prefixes.sh
/usr/local/opnsense/scripts/dhcp/unbound_watcher.py
/usr/local/opnsense/scripts/dns/dnsmasq_dhcp_options.py
/usr/local/opnsense/scripts/dns/query_dns.py
/usr/local/opnsense/scripts/filter/delete_table.py
/usr/local/opnsense/scripts/filter/download_geoip.py
Expand Down Expand Up @@ -1380,6 +1390,8 @@
/usr/local/opnsense/service/templates/OPNsense/Captiveportal/rc.conf.d
/usr/local/opnsense/service/templates/OPNsense/Cron/+TARGETS
/usr/local/opnsense/service/templates/OPNsense/Cron/user.cron
/usr/local/opnsense/service/templates/OPNsense/Dnsmasq/+TARGETS
/usr/local/opnsense/service/templates/OPNsense/Dnsmasq/dnsmasq.conf
/usr/local/opnsense/service/templates/OPNsense/Filter/+TARGETS
/usr/local/opnsense/service/templates/OPNsense/Filter/filter_geoip.conf
/usr/local/opnsense/service/templates/OPNsense/Filter/filter_tables.conf
Expand Down
147 changes: 50 additions & 97 deletions src/etc/inc/plugins.inc.d/dnsmasq.inc
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]>
Expand Down Expand Up @@ -40,8 +41,7 @@ function dnsmasq_configure()
{
return [
'dns' => ['dnsmasq_configure_do'],
'local' => ['dnsmasq_configure_do'],
'newwanip' => ['dnsmasq_configure_do'],
'local' => ['dnsmasq_configure_do']
];
}

Expand Down Expand Up @@ -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;
Expand All @@ -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'];
Expand All @@ -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) {
Expand All @@ -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'])) {
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
class ServiceController extends ApiMutableServiceControllerBase
{
protected static $internalServiceClass = '\OPNsense\Dnsmasq\Dnsmasq';
//protected static $internalServiceTemplate = 'OPNsense/Dnsmasq';
protected static $internalServiceTemplate = 'OPNsense/Dnsmasq';
protected static $internalServiceEnabled = 'enable';
protected static $internalServiceName = 'dnsmasq';
}
Loading

0 comments on commit 15b7945

Please sign in to comment.