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
This commit is contained in:
Ad Schellevis 2020-12-16 11:56:46 +01:00
parent 2ee04640a7
commit 5509fabfa3

View File

@ -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;