Commit 66511add authored by Ad Schellevis's avatar Ad Schellevis

(gwlb) refactor fixup_default_gateway(), the old version didn't even seem to work properly....

this whole procedure still seems a bit off when being called in return_gateway_groups_array(), maybe we should move this to a more sane location at some other point in time:
parent 51fea877
...@@ -714,63 +714,53 @@ function return_gateways_array($disabled = false, $localhost = false, $inactive ...@@ -714,63 +714,53 @@ function return_gateways_array($disabled = false, $localhost = false, $inactive
return $gateways_arr; return $gateways_arr;
} }
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) function fixup_default_gateway($gateways_status, $gateways_arr)
{ {
global $config; global $config;
/* /*
* NOTE: The code below is meant to replace the default gateway when it goes down. * NOTE: The code below is meant to replace the default gateway when it goes down.
* This facilitates services running on OPNsense itself and are not handled by a PBR to continue working. * This facilitates services running on OPNsense itself and are not handled by a PBR to continue working.
*/ */
$upgw = ""; foreach (array("inet", "inet6") as $ipprotocol) {
$dfltgwdown = false; $upgw = "";
$dfltgwfound = false; $dfltgwup = false;
foreach ($gateways_arr as $gwname => $gwsttng) { $dfltgwname = null;
if (($gwsttng['ipprotocol'] == $ipprotocol) && isset($gwsttng['defaultgw'])) { foreach ($gateways_arr as $gwname => $gwsttng) {
$dfltgwfound = true; if ($gwsttng['ipprotocol'] == $ipprotocol) {
$dfltgwname = $gwname; if (isset($gwsttng['defaultgw'])) {
if (!isset($gwsttng['monitor_disable']) && stristr($gateways_status[$gwname]['status'], "down")) { $dfltgwname = $gwname;
$dfltgwdown = true; if (isset($gwsttng['monitor_disable']) || !stristr($gateways_status[$gwname]['status'], "down")) {
$dfltgwup = true;
}
}
/* Keep a record of the last up gateway */
/* XXX: Blacklist lan for now since it might cause issues to those who have a gateway set for it */
if (empty($upgw) && $gwsttng[$gwname]['friendlyiface'] != "lan" &&
(isset($gwsttng['monitor_disable']) || !stristr($gateways_status[$gwname]['status'], "down"))) {
$upgw = $gwname;
}
if ($dfltgwdown && !empty($upgw)) {
break;
}
} }
} }
/* Keep a record of the last up gateway */
/* XXX: Blacklist lan for now since it might cause issues to those who have a gateway set for it */
if (empty($upgw) && ($gwsttng['ipprotocol'] == $ipprotocol) && (isset($gwsttng['monitor_disable']) || !stristr($gateways_status[$gwname]['status'], "down")) && $gwsttng[$gwname]['friendlyiface'] != "lan") {
$upgw = $gwname;
}
if ($dfltgwdown == true && !empty($upgw)) {
break;
}
}
if ($dfltgwfound == false) {
$gwname = convert_friendly_interface_to_friendly_descr("wan");
if (!empty($gateways_status[$gwname]) && stristr($gateways_status[$gwname]['status'], "down")) {
$dfltgwdown = true;
}
}
if ($dfltgwdown == true && !empty($upgw)) {
if ($gateways_arr[$upgw]['gateway'] == "dynamic") { if ($gateways_arr[$upgw]['gateway'] == "dynamic") {
$gateways_arr[$upgw]['gateway'] = get_interface_gateway($gateways_arr[$upgw]['friendlyiface']); $gwip = get_interface_gateway($gateways_arr[$upgw]['friendlyiface']);
} else {
$gwip = $gateways_arr[$upgw]['gateway'];
} }
if (is_ipaddr($gateways_arr[$upgw]['gateway'])) { if (!$dfltgwup && !empty($upgw)) {
log_error("Default gateway down setting {$upgw} as default!"); if (is_ipaddr($gwip)) {
if (is_ipaddrv6($gateways_arr[$upgw]['gateway'])) { log_error("Default gateway down setting {$upgw} as default!");
$inetfamily = "-inet6"; mwexec("/sbin/route delete -{$ipprotocol} default");
} else { mwexec("/sbin/route add -{$ipprotocol} default {$gwip}");
$inetfamily = "-inet";
} }
mwexec("/sbin/route delete {$inetfamily} default {$gateways_arr[$upgw]['gateway']}");
mwexec("/sbin/route add {$inetfamily} default {$gateways_arr[$upgw]['gateway']}");
}
} else {
$defaultgw = trim(exec("/sbin/route -n get -{$ipprotocol} default | /usr/bin/awk '/gateway:/ {print $2}'"), " \n");
if (is_ipaddrv6($gateways_arr[$dfltgwname]['gateway'])) {
$inetfamily = "-inet6";
} else { } else {
$inetfamily = "-inet"; $defaultgw = trim(exec("/sbin/route -n get -{$ipprotocol} default | /usr/bin/awk '/gateway:/ {print $2}'"), " \n");
} if ($defaultgw != $gwip) {
if ($defaultgw != $gateways_arr[$dfltgwname]['gateway']) { mwexec("/sbin/route delete -{$ipprotocol} default");
mwexec("/sbin/route delete {$inetfamily} default {$gateways_arr[$dfltgwname]['gateway']}"); mwexec("/sbin/route add -{$ipprotocol} default {$gwip}");
mwexec("/sbin/route add {$inetfamily} default {$gateways_arr[$dfltgwname]['gateway']}"); }
} }
} }
} }
...@@ -789,8 +779,7 @@ function return_gateway_groups_array() ...@@ -789,8 +779,7 @@ function return_gateway_groups_array()
$gateway_groups_array = array(); $gateway_groups_array = array();
if (isset($config['system']['gw_switch_default'])) { if (isset($config['system']['gw_switch_default'])) {
fixup_default_gateway("inet", $gateways_status, $gateways_arr); fixup_default_gateway($gateways_status, $gateways_arr);
fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
} }
if (isset($config['gateways']['gateway_group'])) { if (isset($config['gateways']['gateway_group'])) {
$carplist = get_configured_carp_interface_list(); $carplist = get_configured_carp_interface_list();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment