From 5509fabfa3c3df1460c9d32a2397a4e25dbe9e55 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Wed, 16 Dec 2020 11:56:46 +0100 Subject: [PATCH] gateways: dpinger. choose a better bind candidate for IPv4. when an interface offers multiple addresses, we should at least try to bind to the address which can access the gateway, for example an interface with the following addresses configured: 10.0.1.1/24 10.0.2.1/24 10.0.3.1/24 and a gateway configured on 10.0.2.100 should try to bind on 10.0.2.1. when we can't find a candidate, fall back to the first. closes https://github.com/opnsense/core/pull/4221 --- src/etc/inc/plugins.inc.d/dpinger.inc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/etc/inc/plugins.inc.d/dpinger.inc b/src/etc/inc/plugins.inc.d/dpinger.inc index 30addb851..d32231411 100644 --- a/src/etc/inc/plugins.inc.d/dpinger.inc +++ b/src/etc/inc/plugins.inc.d/dpinger.inc @@ -129,7 +129,27 @@ function dpinger_configure_do($verbose = false, $gwname = null) * $gateway['ipprotocol'] is the better option. */ if ($gateway['ipprotocol'] == "inet") { // This is an IPv4 gateway... - $gwifip = find_interface_ip($gateway['if'], $ifconfig_details); + $gwifip = null; + if (!empty($ifconfig_details[$gateway['if']]) && + !empty($ifconfig_details[$gateway['if']]['ipv4']) && + !empty($gateway['gateway']) + ) { + $ifdetails = $ifconfig_details[$gateway['if']]; + $match = ip2ulong($gateway['gateway']); + foreach ($ifdetails['ipv4'] as $ipv4) { + $ip_min = gen_subnet($ipv4['ipaddr'], $ipv4['subnetbits']); + $ip_max = gen_subnet_max($ipv4['ipaddr'], $ipv4['subnetbits']); + if ($match >= ip2ulong($ip_min) && $match <= ip2ulong($ip_max)) { + $gwifip = $ipv4['ipaddr']; + break; + } + } + if ($gwifip == null) { + $gwifip = $ifconfig_details[$gateway['if']]['ipv4'][0]; + log_error(sprintf('Choose to bind %s on %s since we could not find a proper match.', $name, $gwifip)); + } + + } if (!is_ipaddrv4($gwifip)) { log_error(sprintf('The %s IPv4 gateway address is invalid, skipping.', $name)); continue;