diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Kea/forms/dialogSubnet.xml b/src/opnsense/mvc/app/controllers/OPNsense/Kea/forms/dialogSubnet.xml index 7dcde3f7d..c3b2688a8 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Kea/forms/dialogSubnet.xml +++ b/src/opnsense/mvc/app/controllers/OPNsense/Kea/forms/dialogSubnet.xml @@ -11,6 +11,12 @@ textbox List of pools, one per line in range or subnet format (e.g. 192.168.0.100 - 192.168.0.200 , 192.0.2.64/26 + + subnet4.option_data_autocollect + + checkbox + Automatically update option data for relevant attributes as routers, dns servers and ntp servers when applying settings from the gui. + subnet4.option_data.routers diff --git a/src/opnsense/mvc/app/models/OPNsense/Kea/KeaDhcpv4.php b/src/opnsense/mvc/app/models/OPNsense/Kea/KeaDhcpv4.php index 235e0d529..8c397eac2 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Kea/KeaDhcpv4.php +++ b/src/opnsense/mvc/app/models/OPNsense/Kea/KeaDhcpv4.php @@ -30,7 +30,40 @@ namespace OPNsense\Kea; use OPNsense\Base\BaseModel; use OPNsense\Core\Config; +use OPNsense\Core\Backend; +use OPNsense\Firewall\Util; + class KeaDhcpv4 extends BaseModel { + /** + * Before persisting data into the model, update option_data fields for selected subnets. + * setNodes() is used in most cases (at least from our base controller), which should make this a relatvily + * save entrypoint to enforce some data. + */ + public function setNodes($data) + { + $ifconfig = json_decode((new Backend())->configdRun('interface list ifconfig'), true) ?? []; + foreach ($this->subnets->subnet4->iterateItems() as $subnet) { + if (!empty((string)$subnet->option_data_autocollect)) { + // find first possible candidate to use as a gateway. + $host_ip = null; + foreach ($ifconfig as $if => $details) { + foreach ($details['ipv4'] as $net) { + if (Util::isIPInCIDR($net['ipaddr'], (string)$subnet->subnet)) { + $host_ip = $net['ipaddr']; + break 2; + } + } + } + + if (!empty($host_ip)) { + $subnet->option_data->routers = $host_ip; + $subnet->option_data->domain_name_servers = $host_ip; + $subnet->option_data->ntp_servers = $host_ip; + } + } + } + return parent::setNodes($data); + } } diff --git a/src/opnsense/mvc/app/models/OPNsense/Kea/KeaDhcpv4.xml b/src/opnsense/mvc/app/models/OPNsense/Kea/KeaDhcpv4.xml index d0d0c5583..476cf2d6e 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Kea/KeaDhcpv4.xml +++ b/src/opnsense/mvc/app/models/OPNsense/Kea/KeaDhcpv4.xml @@ -23,6 +23,10 @@ ipv4 Y + + 1 + Y + N