mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-18 10:35:27 +00:00
Kea / Dhcp - DHCPv4 replacement [https://github.com/opnsense/core/issues/6971], add "Auto collect option data" checkbox on the subnet.
Our current default (legacy) scenario is to use the configured interface addresss as gateway and primary dns, but since we don't want to complicate the templates and make the choice explicit, we're adding an option to auto-collect and persist on configuration save/apply. This commit auto updates selected option data fields on request with the first matching address for the requested subnet found in ifconfig.
This commit is contained in:
parent
62bf0f5779
commit
8e63fbb302
@ -11,6 +11,12 @@
|
||||
<type>textbox</type>
|
||||
<help>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</help>
|
||||
</field>
|
||||
<field>
|
||||
<id>subnet4.option_data_autocollect</id>
|
||||
<label>Auto collect option data</label>
|
||||
<type>checkbox</type>
|
||||
<help>Automatically update option data for relevant attributes as routers, dns servers and ntp servers when applying settings from the gui.</help>
|
||||
</field>
|
||||
<field>
|
||||
<id>subnet4.option_data.routers</id>
|
||||
<label>Routers (gateway)</label>
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,6 +23,10 @@
|
||||
<AddressFamily>ipv4</AddressFamily>
|
||||
<Required>Y</Required>
|
||||
</subnet>
|
||||
<option_data_autocollect type="BooleanField">
|
||||
<Default>1</Default>
|
||||
<Required>Y</Required>
|
||||
</option_data_autocollect>
|
||||
<option_data>
|
||||
<domain_name_servers type="NetworkField">
|
||||
<NetMaskAllowed>N</NetMaskAllowed>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user