From ab5cbcd3ca9569eaa0e9b203d7fb65514b2f3bae Mon Sep 17 00:00:00 2001 From: Jason Crowley <65243090+jasonpcrowley@users.noreply.github.com> Date: Thu, 14 Oct 2021 14:23:40 -0500 Subject: [PATCH] Updated guess_interface_from_ip to more accurately identify the interface using the subnet with the largest mask in the route table. (#5281) --- src/etc/inc/interfaces.inc | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/etc/inc/interfaces.inc b/src/etc/inc/interfaces.inc index a63c29966..ef615a9ae 100644 --- a/src/etc/inc/interfaces.inc +++ b/src/etc/inc/interfaces.inc @@ -3773,14 +3773,25 @@ function guess_interface_from_ip($ipaddress) /* create a route table we can search */ exec("/usr/bin/netstat -rnWf " . $family, $output, $ret); + + /* search for the route with the largest subnet mask */ + $largest_mask = 0; + $best_if = null; foreach ($output as $line) { $fields = preg_split("/\s+/", $line); if (is_subnet($fields[0])) { if (ip_in_subnet($ipaddress, $fields[0])) { - return $fields[5]; + list($ip, $mask) = explode('/', $fields[0]); + if ($mask > $largest_mask) { + $best_if = $fields[5]; + $largest_mask = $mask; + } } } } + if (isset($best_if)) { + return $best_if; + } $ret = exec_command("/sbin/route -n get {$ipaddress} | /usr/bin/awk '/interface/ { print \$2; };'"); if (empty($ret)) {