Skip to content

Commit

Permalink
Interfaces/neighbor - implement new neighbor configuration for arp/nd…
Browse files Browse the repository at this point in the history
…p entries closes #6917

This commit adds a new component linked in Interfaces/Neighbors which offers the ability to manually register static leases and provides application control from other modules such as dhcpd. To minimize the risk, we're reusing the existing interfaces_staticarp_configure() hooks while only adjusting how static arp entries are being attached to the interface (match on addresses assigned when triggering with an interface).

Entries registered via dhcp will be visible from the ui as well together with its origin.
The previous version didn't cleanup old static entries, this version triggers a cleanup when executed for all interfaces using all earlier modifications processed via the same function (interfaces_neighbors_configure()).
  • Loading branch information
AdSchellevis committed Oct 12, 2023
1 parent 2ae4308 commit 1bec000
Show file tree
Hide file tree
Showing 15 changed files with 725 additions and 7 deletions.
11 changes: 11 additions & 0 deletions plist
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,19 @@
/usr/local/opnsense/mvc/app/controllers/OPNsense/IPsec/forms/dialogVTI.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/LaggSettingsController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/LoopbackSettingsController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/NeighborSettingsController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/VipSettingsController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/VlanSettingsController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/VxlanSettingsController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/LaggController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/LoopbackController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/NeighborController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/VipController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/VlanController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/VxlanController.php
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/forms/dialogLagg.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/forms/dialogLoopback.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/forms/dialogNeighbor.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/forms/dialogVip.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/forms/dialogVlan.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/forms/dialogVxlan.xml
Expand Down Expand Up @@ -503,6 +506,7 @@
/usr/local/opnsense/mvc/app/library/OPNsense/Core/Config.php
/usr/local/opnsense/mvc/app/library/OPNsense/Core/ConfigException.php
/usr/local/opnsense/mvc/app/library/OPNsense/Core/File.php
/usr/local/opnsense/mvc/app/library/OPNsense/Core/FileObject.php
/usr/local/opnsense/mvc/app/library/OPNsense/Core/Routing.php
/usr/local/opnsense/mvc/app/library/OPNsense/Core/Shell.php
/usr/local/opnsense/mvc/app/library/OPNsense/Core/Singleton.php
Expand Down Expand Up @@ -570,6 +574,7 @@
/usr/local/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/InterfaceField.php
/usr/local/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/JsonKeyValueStoreField.php
/usr/local/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/LegacyLinkField.php
/usr/local/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/MacAddressField.php
/usr/local/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ModelRelationField.php
/usr/local/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/NetworkAliasField.php
/usr/local/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/NetworkField.php
Expand Down Expand Up @@ -675,6 +680,7 @@
/usr/local/opnsense/mvc/app/models/OPNsense/IPsec/Swanctl.xml
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/ACL/ACL.xml
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/FieldTypes/LaggInterfaceField.php
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/FieldTypes/NeighborField.php
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/FieldTypes/VipField.php
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/FieldTypes/VipInterfaceField.php
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/FieldTypes/VipNetworkField.php
Expand All @@ -684,6 +690,9 @@
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/Loopback.php
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/Loopback.xml
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/Menu/Menu.xml
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/Neighbor.php
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/Neighbor.xml
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/Neighbor/dhcpd.php
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/Vip.php
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/Vip.xml
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/Vlan.php
Expand Down Expand Up @@ -800,6 +809,7 @@
/usr/local/opnsense/mvc/app/views/OPNsense/IPsec/vti.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Interface/lagg.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Interface/loopback.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Interface/neighbor.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Interface/vip.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Interface/vlan.volt
/usr/local/opnsense/mvc/app/views/OPNsense/Interface/vxlan.volt
Expand Down Expand Up @@ -1002,6 +1012,7 @@
/usr/local/opnsense/scripts/interfaces/ppp-rename.sh
/usr/local/opnsense/scripts/interfaces/ppp-uptime.sh
/usr/local/opnsense/scripts/interfaces/reconfigure_laggs.php
/usr/local/opnsense/scripts/interfaces/reconfigure_neighbors.php
/usr/local/opnsense/scripts/interfaces/reconfigure_vips.php
/usr/local/opnsense/scripts/interfaces/reconfigure_vlans.php
/usr/local/opnsense/scripts/interfaces/rtsold_resolvconf.sh
Expand Down
64 changes: 57 additions & 7 deletions src/etc/inc/interfaces.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3865,17 +3865,67 @@ function interfaces_staticarp_configure($if, $ifconfig_details = null)
mwexecf('/usr/sbin/arp -d -i %s -a', [$ifcfg['if']]);
}

if (isset($config['dhcpd'][$if]['staticmap'])) {
foreach ($config['dhcpd'][$if]['staticmap'] as $arpent) {
if (!$want && !isset($arpent['arp_table_static_entry'])) {
continue;
interfaces_neighbors_configure($ifcfg['if'], $ifconfig_details);
}

function interfaces_neighbors_configure($device = null, $ifconfig_details = null)
{
$subnets = [];

if (!empty($device)) {
if (empty($ifconfig_details) || empty($ifconfig_details[$device])) {
/* when called with an interface, require $ifconfig_details being passed */
return;
}
foreach (['ipv4', 'ipv6'] as $proto) {
if (!empty($ifconfig_details[$device])) {
foreach ($ifconfig_details[$device][$proto] as $item) {
$subnets[] = $item['ipaddr'] . '/' . $item['subnetbits'];
}
}
if (!isset($arpent['ipaddr'])) {
continue;
}
}

$current_neightbors = [];
foreach ((new \OPNsense\Interfaces\Neighbor())->neighbor->iterateItems() as $key => $node) {
$found = empty($if); /* unfiltered when no $if provided */
foreach ($subnets as $subnet) {
$found = ip_in_subnet((string)$node->ipaddress, $subnet);
if ($found) {
break;
}
}
if ($found) {
// IPv4 [arp] or IPv6 [ndp]
if (strpos($node->ipaddress, ":") === false) {
mwexecf('/usr/sbin/arp -s %s %s', [$node->ipaddress, $node->etheraddr]);
} else {
mwexecf('/usr/sbin/ndp -s %s %s', [$node->ipaddress, $node->etheraddr]);
}
}
$current_neightbors[] = (string)$node->ipaddress;
}

/* persist accounted addresses, without a cleanup that would be all seen since last cleanup */
$fobj = new \OPNsense\Core\FileObject('/tmp/interfaces_neighbors.json', 'a+');
$current = $fobj->readJson() ?? [];
$fobj->truncate(0)->writeJson(
!empty($device) ? array_unique(array_merge($current_neightbors, $current)) : $current_neightbors
);
unset($fobj);
/* only cleanup when applying all interfaces */
if (empty($device) && is_array($current)) {
foreach ($current as $item) {
if (is_string($item) && is_ipaddr($item) && !in_array($item, $current_neightbors)) {
if (strpos($item, ":") === false) {
mwexecf('/usr/sbin/arp -d %s', [$item]);
} else {
mwexecf('/usr/sbin/ndp -d %s', [$item]);
}
}
mwexecf('/usr/sbin/arp -s %s %s', [$arpent['ipaddr'], $arpent['mac']]);
}
}

}

function get_interfaces_info($include_unlinked = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* Copyright (C) 2023 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\Interfaces\Api;

use OPNsense\Core\Backend;
use OPNsense\Base\ApiMutableModelControllerBase;

class NeighborSettingsController extends ApiMutableModelControllerBase
{
protected static $internalModelName = 'neighbor';
protected static $internalModelClass = 'OPNsense\Interfaces\Neighbor';

public function searchItemAction()
{
return $this->searchBase("neighbor", ['etheraddr', 'ipaddress', 'descr', 'origin'], "vxlanid");
}

public function setItemAction($uuid)
{
return $this->setBase("neighbor", "neighbor", $uuid);
}

public function addItemAction()
{
return $this->addBase("neighbor", "neighbor");
}

public function getItemAction($uuid = null)
{
return $this->getBase("neighbor", "neighbor", $uuid);
}

public function delItemAction($uuid)
{
return $this->delBase("neighbor", $uuid);
}

public function reconfigureAction()
{
$result = ["status" => "failed"];
if ($this->request->isPost()) {
$result['status'] = strtolower(trim((new Backend())->configdRun('interface neighbor configure')));
}
return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* Copyright (C) 2019 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\Interfaces;

class NeighborController extends \OPNsense\Base\IndexController
{
public function indexAction()
{
$this->view->pick('OPNsense/Interface/neighbor');
$this->view->formDialogEdit = $this->getForm("dialogNeighbor");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<form>
<field>
<id>neighbor.etheraddr</id>
<label>Ether Address</label>
<type>text</type>
<help>Hardware MAC address of the cllient (format xx:xx:xx:xx:xx:xx)</help>
</field>
<field>
<id>neighbor.ipaddress</id>
<label>IP address</label>
<type>text</type>
<help>IP address to assign to the provided MAC address, which will either end up in the arp (IPv4) or ndp (IPv6) table</help>
</field>
<field>
<id>neighbor.descr</id>
<label>Description</label>
<type>text</type>
<help>You may enter a description here for your reference (not parsed).</help>
</field>
</form>

0 comments on commit 1bec000

Please sign in to comment.