From 58a2efd4f95a7fd94f58b2c411c4b01aa286eee8 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Fri, 12 Apr 2019 16:13:02 +0200 Subject: [PATCH] Routing, gateways. use new gateways class in setup_gateways_monitor() and do some minor cleanups. * monitor field is the responsibility of the class, so there's no need to check it again (duplicate) * interface was previously overwritten with technical name, which is now in "if" field, replace occurrences * simplify reasons to ignore gateways a bit (if, elseif block on top of loop) for https://github.com/opnsense/core/issues/2279 --- src/etc/inc/gwlb.inc | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/etc/inc/gwlb.inc b/src/etc/inc/gwlb.inc index 201b23d85..25f5ee552 100644 --- a/src/etc/inc/gwlb.inc +++ b/src/etc/inc/gwlb.inc @@ -63,26 +63,17 @@ function setup_gateways_monitor($verbose = false, $gwname = null) @mkdir('/var/db/rrd', 0775); @chown('/var/db/rrd', 'nobody'); - $gateways_arr_all = return_gateways_array(true); + $gateways = new \OPNsense\Routing\Gateways(legacy_interfaces_details()); $monitor_ips = array(); $dpinger_default = return_dpinger_defaults(); - foreach ($gateways_arr_all as $name => $gateway) { + foreach ($gateways->gatewaysIndexedByName(true) as $name => $gateway) { if (!empty($gwname) && $gwname != $name) { continue; - } - - if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) { - if (is_ipaddr($gateway['gateway'])) { - $gateway['monitor'] = $gateway['gateway']; - } else { - /* No chance to get an ip to monitor skip target. */ - continue; - } - } - - /* if the monitor address is already used before, skip */ - if (in_array($gateway['monitor'], $monitor_ips)) { + } elseif (empty($gateway['monitor'])) { + continue; + } elseif (in_array($gateway['monitor'], $monitor_ips)) { + /* if the monitor address is already used before, skip */ continue; } @@ -96,7 +87,7 @@ function setup_gateways_monitor($verbose = false, $gwname = null) * $gateway['ipprotocol'] is the better option. */ if ($gateway['ipprotocol'] == "inet") { // This is an IPv4 gateway... - $gwifip = find_interface_ip($gateway['interface']); + $gwifip = find_interface_ip($gateway['if']); if (!is_ipaddrv4($gwifip)) { continue; //Skip this target } @@ -125,10 +116,10 @@ function setup_gateways_monitor($verbose = false, $gwname = null) } elseif ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway... if (is_linklocal($gateway['monitor'])) { /* link local monitor needs a link local address for the "src" part */ - $gwifip = find_interface_ipv6_ll($gateway['interface']); + $gwifip = find_interface_ipv6_ll($gateway['if']); } else { /* monitor is a routable address, so use a routable address for the "src" part */ - $gwifip = find_interface_ipv6($gateway['interface']); + $gwifip = find_interface_ipv6($gateway['if']); } if (!is_ipaddrv6($gwifip)) { @@ -141,13 +132,13 @@ function setup_gateways_monitor($verbose = false, $gwname = null) * then the ICMP6 response would not find its way back home. */ if (is_linklocal($gateway['monitor']) && strpos($gateway['monitor'], '%') === false) { - $gateway['monitor'] .= "%{$gateway['interface']}"; + $gateway['monitor'] .= "%{$gateway['if']}"; } if (is_linklocal($gateway['gateway']) && strpos($gateway['gateway'], '%') === false) { - $gateway['gateway'] .= "%{$gateway['interface']}"; + $gateway['gateway'] .= "%{$gateway['if']}"; } if (is_linklocal($gwifip) && strpos($gwifip, '%') === false) { - $gwifip .= "%{$gateway['interface']}"; + $gwifip .= "%{$gateway['if']}"; } /* flush the monitor unconditionally */