Commit 3ca65169 authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(legacy) spaces and curly braces in gwlb.inc

(cherry picked from commit 880980a7)
parent af021d13
...@@ -29,23 +29,23 @@ ...@@ -29,23 +29,23 @@
function return_apinger_defaults() function return_apinger_defaults()
{ {
return array( return array(
"latencylow" => "200", "latencylow" => "200",
"latencyhigh" => "500", "latencyhigh" => "500",
"losslow" => "10", "losslow" => "10",
"losshigh" => "20", "losshigh" => "20",
"interval" => "1", "interval" => "1",
"down" => "10", "down" => "10",
"avg_delay_samples" => "10", "avg_delay_samples" => "10",
"avg_loss_samples" => "50", "avg_loss_samples" => "50",
"avg_loss_delay_samples" => "20" "avg_loss_delay_samples" => "20"
); );
} }
function setup_gateways_monitor_full() function setup_gateways_monitor_full()
{ {
killbypid('/var/run/apinger.pid'); killbypid('/var/run/apinger.pid');
setup_gateways_monitor(); setup_gateways_monitor();
} }
/* /*
...@@ -54,15 +54,15 @@ function setup_gateways_monitor_full() ...@@ -54,15 +54,15 @@ function setup_gateways_monitor_full()
*/ */
function setup_gateways_monitor() function setup_gateways_monitor()
{ {
global $config; global $config;
@mkdir('/var/db/rrd', 0775); @mkdir('/var/db/rrd', 0775);
@chown('/var/db/rrd', 'nobody'); @chown('/var/db/rrd', 'nobody');
$gateways_arr_all = return_gateways_array(true); $gateways_arr_all = return_gateways_array(true);
$apinger_default = return_apinger_defaults(); $apinger_default = return_apinger_defaults();
$apingerconfig = <<<EOD $apingerconfig = <<<EOD
# OPNsense apinger configuration file. Automatically Generated! # OPNsense apinger configuration file. Automatically Generated!
...@@ -80,11 +80,11 @@ pid_file "/var/run/apinger.pid" ...@@ -80,11 +80,11 @@ pid_file "/var/run/apinger.pid"
#timestamp_format "%Y%m%d%H%M%S" #timestamp_format "%Y%m%d%H%M%S"
status { status {
## File where the status information should be written to ## File where the status information should be written to
file "/var/run/apinger.status" file "/var/run/apinger.status"
## Interval between file updates ## Interval between file updates
## when 0 or not set, file is written only when SIGUSR1 is received ## when 0 or not set, file is written only when SIGUSR1 is received
interval 5s interval 5s
} }
######################################## ########################################
...@@ -94,720 +94,774 @@ rrd interval 60s; ...@@ -94,720 +94,774 @@ rrd interval 60s;
## These parameters can be overridden in a specific alarm configuration ## These parameters can be overridden in a specific alarm configuration
alarm default { alarm default {
command on "/usr/local/opnsense/service/configd_ctl.py -m 'dyndns reload %T' 'ipsecdns reload' 'openvpn reload %T' 'filter reload' " command on "/usr/local/opnsense/service/configd_ctl.py -m 'dyndns reload %T' 'ipsecdns reload' 'openvpn reload %T' 'filter reload' "
command off "/usr/local/opnsense/service/configd_ctl.py -m 'dyndns reload %T' 'ipsecdns reload' 'openvpn reload %T' 'filter reload' " command off "/usr/local/opnsense/service/configd_ctl.py -m 'dyndns reload %T' 'ipsecdns reload' 'openvpn reload %T' 'filter reload' "
combine 10s combine 10s
} }
## "Down" alarm definition. ## "Down" alarm definition.
## This alarm will be fired when target doesn't respond for 30 seconds. ## This alarm will be fired when target doesn't respond for 30 seconds.
alarm down "down" { alarm down "down" {
time {$apinger_default['down']}s time {$apinger_default['down']}s
} }
## "Delay" alarm definition. ## "Delay" alarm definition.
## This alarm will be fired when responses are delayed more than 200ms ## This alarm will be fired when responses are delayed more than 200ms
## it will be canceled, when the delay drops below 100ms ## it will be canceled, when the delay drops below 100ms
alarm delay "delay" { alarm delay "delay" {
delay_low {$apinger_default['latencylow']}ms delay_low {$apinger_default['latencylow']}ms
delay_high {$apinger_default['latencyhigh']}ms delay_high {$apinger_default['latencyhigh']}ms
} }
## "Loss" alarm definition. ## "Loss" alarm definition.
## This alarm will be fired when packet loss goes over 20% ## This alarm will be fired when packet loss goes over 20%
## it will be canceled, when the loss drops below 10% ## it will be canceled, when the loss drops below 10%
alarm loss "loss" { alarm loss "loss" {
percent_low {$apinger_default['losslow']} percent_low {$apinger_default['losslow']}
percent_high {$apinger_default['losshigh']} percent_high {$apinger_default['losshigh']}
} }
target default { target default {
## How often the probe should be sent ## How often the probe should be sent
interval {$apinger_default['interval']}s interval {$apinger_default['interval']}s
## How many replies should be used to compute average delay ## How many replies should be used to compute average delay
## for controlling "delay" alarms ## for controlling "delay" alarms
avg_delay_samples {$apinger_default['avg_delay_samples']} avg_delay_samples {$apinger_default['avg_delay_samples']}
## How many probes should be used to compute average loss ## How many probes should be used to compute average loss
avg_loss_samples {$apinger_default['avg_loss_samples']} avg_loss_samples {$apinger_default['avg_loss_samples']}
## The delay (in samples) after which loss is computed ## The delay (in samples) after which loss is computed
## without this delays larger than interval would be treated as loss ## without this delays larger than interval would be treated as loss
avg_loss_delay_samples {$apinger_default['avg_loss_delay_samples']} avg_loss_delay_samples {$apinger_default['avg_loss_delay_samples']}
## Names of the alarms that may be generated for the target ## Names of the alarms that may be generated for the target
alarms "down","delay","loss" alarms "down","delay","loss"
## Location of the RRD ## Location of the RRD
#rrd file "/var/db/rrd/apinger-%t.rrd" #rrd file "/var/db/rrd/apinger-%t.rrd"
} }
EOD; EOD;
$monitor_ips = array(); $monitor_ips = array();
foreach($gateways_arr_all as $name => $gateway) { foreach($gateways_arr_all as $name => $gateway) {
if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) { if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) {
if (is_ipaddr($gateway['gateway'])) { if (is_ipaddr($gateway['gateway'])) {
$gateway['monitor'] = $gateway['gateway']; $gateway['monitor'] = $gateway['gateway'];
} else { } else {
/* No chance to get an ip to monitor skip target. */ /* No chance to get an ip to monitor skip target. */
continue; continue;
} }
} }
/* if the monitor address is already used before, skip */ /* if the monitor address is already used before, skip */
if (in_array($gateway['monitor'], $monitor_ips)) { if (in_array($gateway['monitor'], $monitor_ips)) {
continue; continue;
} }
/* /*
* Interface IP is needed since apinger will bind a socket to it. * Interface IP is needed since apinger will bind a socket to it.
* However the config GUI should already have checked this and when * However the config GUI should already have checked this and when
* PPoE is used the IP address is set to "dynamic". So using is_ipaddrv4 * PPoE is used the IP address is set to "dynamic". So using is_ipaddrv4
* or is_ipaddrv6 to identify packet type would be wrong, especially as * or is_ipaddrv6 to identify packet type would be wrong, especially as
* further checks (that can cope with the "dynamic" case) are present inside * further checks (that can cope with the "dynamic" case) are present inside
* the if block. So using $gateway['ipprotocol'] is the better option. * the if block. So using $gateway['ipprotocol'] is the better option.
*/ */
if ($gateway['ipprotocol'] == "inet") { // This is an IPv4 gateway... if ($gateway['ipprotocol'] == "inet") { // This is an IPv4 gateway...
$gwifip = find_interface_ip($gateway['interface']); $gwifip = find_interface_ip($gateway['interface']);
if (!is_ipaddrv4($gwifip)) { if (!is_ipaddrv4($gwifip)) {
continue; //Skip this target continue; //Skip this target
} }
/* flush the monitor unconditionally */ /* flush the monitor unconditionally */
if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) { if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
log_error("Removing static route for monitor {$gateway['monitor']}"); log_error("Removing static route for monitor {$gateway['monitor']}");
mwexec("/sbin/route delete -host " . escapeshellarg($gateway['monitor']), true); mwexec("/sbin/route delete -host " . escapeshellarg($gateway['monitor']), true);
} }
/* Do not monitor if such was requested */ /* Do not monitor if such was requested */
if (isset($gateway['disabled']) || isset($gateway['monitor_disable'])) { if (isset($gateway['disabled']) || isset($gateway['monitor_disable'])) {
continue; continue;
} }
/* /*
* If the gateway is the same as the monitor we do not add a * If the gateway is the same as the monitor we do not add a
* route as this will break the routing table. * route as this will break the routing table.
* Add static routes for each gateway with their monitor IP * Add static routes for each gateway with their monitor IP
* not strictly necessary but is a added level of protection. * not strictly necessary but is a added level of protection.
*/ */
if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) { if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
log_error("Adding static route for monitor through {$gateway['gateway']}"); log_error("Adding static route for monitor through {$gateway['gateway']}");
mwexec("/sbin/route add -host " . escapeshellarg($gateway['monitor']) . mwexec("/sbin/route add -host " . escapeshellarg($gateway['monitor']) .
" " . escapeshellarg($gateway['gateway']), true); " " . escapeshellarg($gateway['gateway']), true);
} }
} elseif ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway... } elseif ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway...
if ($gateway['monitor'] == $gateway['gateway']) { if ($gateway['monitor'] == $gateway['gateway']) {
/* link locals really need a different src ip */ /* link locals really need a different src ip */
if (is_linklocal($gateway['gateway'])) { if (is_linklocal($gateway['gateway'])) {
$gwifip = find_interface_ipv6_ll($gateway['interface']); $gwifip = find_interface_ipv6_ll($gateway['interface']);
} else { } else {
$gwifip = find_interface_ipv6($gateway['interface']); $gwifip = find_interface_ipv6($gateway['interface']);
} }
} else { } else {
/* 'monitor' has been set, so makes sure it has precedence over /* 'monitor' has been set, so makes sure it has precedence over
* 'gateway' in defining the source IP. Otherwise if 'gateway' * 'gateway' in defining the source IP. Otherwise if 'gateway'
* is a local link and 'monitor' is global routable then the * is a local link and 'monitor' is global routable then the
* ICMP6 response would not find its way back home... * ICMP6 response would not find its way back home...
*/ */
$gwifip = find_interface_ipv6($gateway['interface']); $gwifip = find_interface_ipv6($gateway['interface']);
if (is_linklocal($gateway['monitor'])) { if (is_linklocal($gateway['monitor'])) {
if (!strstr($gateway['monitor'], '%')) { if (!strstr($gateway['monitor'], '%')) {
$gateway['monitor'] .= "%{$gateway['interface']}"; $gateway['monitor'] .= "%{$gateway['interface']}";
} }
} else { } else {
// Monitor is a routable address, so use a routable address for the "src" part // 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['interface']);
} }
} }
if (!is_ipaddrv6($gwifip)) { if (!is_ipaddrv6($gwifip)) {
continue; //Skip this target continue; //Skip this target
} }
/* flush the monitor unconditionally */ /* flush the monitor unconditionally */
if (is_ipaddrv6($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) { if (is_ipaddrv6($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
log_error("Removing static route for monitor {$gateway['monitor']}"); log_error("Removing static route for monitor {$gateway['monitor']}");
mwexec("/sbin/route delete -host -inet6 " . escapeshellarg($gateway['monitor']), true); mwexec("/sbin/route delete -host -inet6 " . escapeshellarg($gateway['monitor']), true);
} }
/* Do not monitor if such was requested */ /* Do not monitor if such was requested */
if (isset($gateway['disabled']) || isset($gateway['monitor_disable'])) { if (isset($gateway['disabled']) || isset($gateway['monitor_disable'])) {
continue; continue;
} }
/* /*
* If the gateway is the same as the monitor we do not add a * If the gateway is the same as the monitor we do not add a
* route as this will break the routing table. * route as this will break the routing table.
* Add static routes for each gateway with their monitor IP * Add static routes for each gateway with their monitor IP
* not strictly necessary but is a added level of protection. * not strictly necessary but is a added level of protection.
*/ */
if (is_ipaddrv6($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) { if (is_ipaddrv6($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
log_error("Adding static route for monitor through {$gateway['gateway']}"); log_error("Adding static route for monitor through {$gateway['gateway']}");
mwexec("/sbin/route add -host -inet6 " . escapeshellarg($gateway['monitor']) . mwexec("/sbin/route add -host -inet6 " . escapeshellarg($gateway['monitor']) .
" " . escapeshellarg($gateway['gateway']), true); " " . escapeshellarg($gateway['gateway']), true);
} }
} else { } else {
continue; continue;
} }
$monitor_ips[] = $gateway['monitor']; $monitor_ips[] = $gateway['monitor'];
$apingercfg = "target \"{$gateway['monitor']}\" {\n"; $apingercfg = "target \"{$gateway['monitor']}\" {\n";
$apingercfg .= " description \"{$name}\"\n"; $apingercfg .= " description \"{$name}\"\n";
$apingercfg .= " srcip \"{$gwifip}\"\n"; $apingercfg .= " srcip \"{$gwifip}\"\n";
## How often the probe should be sent ## How often the probe should be sent
if (!empty($gateway['interval']) && is_numeric($gateway['interval'])) { if (!empty($gateway['interval']) && is_numeric($gateway['interval'])) {
$interval = intval($gateway['interval']); # Restrict to Integer $interval = intval($gateway['interval']); # Restrict to Integer
if ($interval < 1) $interval = 1; # Minimum if ($interval < 1) {
if ($interval != $apinger_default['interval']) # If not default value $interval = 1; # Minimum
$apingercfg .= " interval " . $interval . "s\n"; }
} if ($interval != $apinger_default['interval']) {
$apingercfg .= " interval " . $interval . "s\n"; # If not default value
## How many replies should be used to compute average delay }
## for controlling "delay" alarms }
if (!empty($gateway['avg_delay_samples']) && is_numeric($gateway['avg_delay_samples'])) {
$avg_delay_samples = intval($gateway['avg_delay_samples']); # Restrict to Integer ## How many replies should be used to compute average delay
if ($avg_delay_samples < 1) $avg_delay_samples = 1; # Minimum ## for controlling "delay" alarms
if ($avg_delay_samples != $apinger_default['avg_delay_samples']) # If not default value if (!empty($gateway['avg_delay_samples']) && is_numeric($gateway['avg_delay_samples'])) {
$apingercfg .= " avg_delay_samples " . $avg_delay_samples . "\n"; $avg_delay_samples = intval($gateway['avg_delay_samples']); # Restrict to Integer
} if ($avg_delay_samples < 1) {
$avg_delay_samples = 1; # Minimum
## How many probes should be used to compute average loss }
if (!empty($gateway['avg_loss_samples']) && is_numeric($gateway['avg_loss_samples'])) { if ($avg_delay_samples != $apinger_default['avg_delay_samples']) {
$avg_loss_samples = intval($gateway['avg_loss_samples']); # Restrict to Integer # If not default value
if ($avg_loss_samples < 1) $avg_loss_samples = 1; # Minimum $apingercfg .= " avg_delay_samples " . $avg_delay_samples . "\n";
if ($avg_loss_samples != $apinger_default['avg_loss_samples']) # If not default value }
$apingercfg .= " avg_loss_samples " . $avg_loss_samples . "\n"; }
}
## How many probes should be used to compute average loss
## The delay (in samples) after which loss is computed if (!empty($gateway['avg_loss_samples']) && is_numeric($gateway['avg_loss_samples'])) {
## without this delays larger than interval would be treated as loss $avg_loss_samples = intval($gateway['avg_loss_samples']); # Restrict to Integer
if (!empty($gateway['avg_loss_delay_samples']) && is_numeric($gateway['avg_loss_delay_samples'])) { if ($avg_loss_samples < 1) {
$avg_loss_delay_samples = intval($gateway['avg_loss_delay_samples']); # Restrict to Integer $avg_loss_samples = 1; # Minimum
if ($avg_loss_delay_samples < 1) $avg_loss_delay_samples = 1; # Minimum }
if ($avg_loss_delay_samples != $apinger_default['avg_loss_delay_samples']) # If not default value if ($avg_loss_samples != $apinger_default['avg_loss_samples']) {
$apingercfg .= " avg_loss_delay_samples " . $avg_loss_delay_samples . "\n"; # If not default value
} $apingercfg .= " avg_loss_samples " . $avg_loss_samples . "\n";
}
$alarms = ""; }
$alarmscfg = "";
$override = false; ## The delay (in samples) after which loss is computed
if (!empty($gateway['losslow'])) { ## without this delays larger than interval would be treated as loss
$alarmscfg .= "alarm loss \"{$name}loss\" {\n"; if (!empty($gateway['avg_loss_delay_samples']) && is_numeric($gateway['avg_loss_delay_samples'])) {
$alarmscfg .= "\tpercent_low {$gateway['losslow']}\n"; $avg_loss_delay_samples = intval($gateway['avg_loss_delay_samples']); # Restrict to Integer
$alarmscfg .= "\tpercent_high {$gateway['losshigh']}\n"; if ($avg_loss_delay_samples < 1) {
$alarmscfg .= "}\n"; $avg_loss_delay_samples = 1; # Minimum
$alarms .= "\"{$name}loss\""; }
$override = true; if ($avg_loss_delay_samples != $apinger_default['avg_loss_delay_samples']) {
} else { # If not default value
if ($override == true) $apingercfg .= " avg_loss_delay_samples " . $avg_loss_delay_samples . "\n";
$alarms .= ","; }
$alarms .= "\"loss\""; }
$override = true;
} $alarms = "";
if (!empty($gateway['latencylow'])) { $alarmscfg = "";
$alarmscfg .= "alarm delay \"{$name}delay\" {\n"; $override = false;
$alarmscfg .= "\tdelay_low {$gateway['latencylow']}ms\n"; if (!empty($gateway['losslow'])) {
$alarmscfg .= "\tdelay_high {$gateway['latencyhigh']}ms\n"; $alarmscfg .= "alarm loss \"{$name}loss\" {\n";
$alarmscfg .= "}\n"; $alarmscfg .= "\tpercent_low {$gateway['losslow']}\n";
if ($override == true) $alarmscfg .= "\tpercent_high {$gateway['losshigh']}\n";
$alarms .= ","; $alarmscfg .= "}\n";
$alarms .= "\"{$name}delay\""; $alarms .= "\"{$name}loss\"";
$override = true; $override = true;
} else { } else {
if ($override == true) if ($override == true) {
$alarms .= ","; $alarms .= ",";
$alarms .= "\"delay\""; }
$override = true; $alarms .= "\"loss\"";
} $override = true;
if (!empty($gateway['down'])) { }
$alarmscfg .= "alarm down \"{$name}down\" {\n"; if (!empty($gateway['latencylow'])) {
$alarmscfg .= "\ttime {$gateway['down']}s\n"; $alarmscfg .= "alarm delay \"{$name}delay\" {\n";
$alarmscfg .= "}\n"; $alarmscfg .= "\tdelay_low {$gateway['latencylow']}ms\n";
if ($override == true) $alarmscfg .= "\tdelay_high {$gateway['latencyhigh']}ms\n";
$alarms .= ","; $alarmscfg .= "}\n";
$alarms .= "\"{$name}down\""; if ($override == true) {
$override = true; $alarms .= ",";
} else { }
if ($override == true) $alarms .= "\"{$name}delay\"";
$alarms .= ","; $override = true;
$alarms .= "\"down\""; } else {
$override = true; if ($override == true) {
} $alarms .= ",";
if ($override == true) }
$apingercfg .= "\talarms override {$alarms};\n"; $alarms .= "\"delay\"";
$override = true;
if (isset($gateway['force_down'])) }
$apingercfg .= "\tforce_down on\n"; if (!empty($gateway['down'])) {
$alarmscfg .= "alarm down \"{$name}down\" {\n";
$apingercfg .= " rrd file \"/var/db/rrd/{$gateway['name']}-quality.rrd\"\n"; $alarmscfg .= "\ttime {$gateway['down']}s\n";
$apingercfg .= "}\n"; $alarmscfg .= "}\n";
$apingercfg .= "\n"; if ($override == true) {
$alarms .= ",";
$apingerconfig .= $alarmscfg; }
$apingerconfig .= $apingercfg; $alarms .= "\"{$name}down\"";
$override = true;
# Create gateway quality RRD with settings more suitable for OPNsense graph set, } else {
# since apinger uses default step (300; 5 minutes) and other settings that don't if ($override == true) {
# match the OPNsense gateway quality graph set. $alarms .= ",";
create_gateway_quality_rrd("/var/db/rrd/{$gateway['name']}-quality.rrd"); }
} $alarms .= "\"down\"";
$override = true;
$gateways_arr = return_gateways_array(); }
if (!is_array($gateways_arr)) { if ($override == true) {
log_error("No gateways to monitor. Apinger will not be run."); $apingercfg .= "\talarms override {$alarms};\n";
killbypid('/var/run/apinger.pid'); }
@unlink('/var/run/apinger.status');
return; if (isset($gateway['force_down'])) {
} $apingercfg .= "\tforce_down on\n";
}
@file_put_contents('/var/etc/apinger.conf', $apingerconfig);
unset($apingerconfig); $apingercfg .= " rrd file \"/var/db/rrd/{$gateway['name']}-quality.rrd\"\n";
$apingercfg .= "}\n";
/* Restart apinger process */ $apingercfg .= "\n";
if (isvalidpid('/var/run/apinger.pid')) {
killbypid('/var/run/apinger.pid', 'HUP'); $apingerconfig .= $alarmscfg;
} else { $apingerconfig .= $apingercfg;
/* start a new apinger process */
@unlink('/var/run/apinger.status'); # Create gateway quality RRD with settings more suitable for OPNsense graph set,
sleep(1); # since apinger uses default step (300; 5 minutes) and other settings that don't
mwexec_bg('/usr/local/sbin/apinger -c /var/etc/apinger.conf'); # match the OPNsense gateway quality graph set.
sleep(1); create_gateway_quality_rrd("/var/db/rrd/{$gateway['name']}-quality.rrd");
killbypid('/var/run/apinger.pid', 'USR1'); }
}
$gateways_arr = return_gateways_array();
return 0; if (!is_array($gateways_arr)) {
log_error("No gateways to monitor. Apinger will not be run.");
killbypid('/var/run/apinger.pid');
@unlink('/var/run/apinger.status');
return;
}
@file_put_contents('/var/etc/apinger.conf', $apingerconfig);
unset($apingerconfig);
/* Restart apinger process */
if (isvalidpid('/var/run/apinger.pid')) {
killbypid('/var/run/apinger.pid', 'HUP');
} else {
/* start a new apinger process */
@unlink('/var/run/apinger.status');
sleep(1);
mwexec_bg('/usr/local/sbin/apinger -c /var/etc/apinger.conf');
sleep(1);
killbypid('/var/run/apinger.pid', 'USR1');
}
return 0;
} }
/* return the status of the apinger targets as a array */ /* return the status of the apinger targets as a array */
function return_gateways_status($byname = false) function return_gateways_status($byname = false)
{ {
global $config; global $config;
$apingerstatus = array(); $apingerstatus = array();
/* Always get the latest status from apinger */ /* Always get the latest status from apinger */
killbypid('/var/run/apinger.pid', 'USR1'); killbypid('/var/run/apinger.pid', 'USR1');
if (file_exists('/var/run/apinger.status')) { if (file_exists('/var/run/apinger.status')) {
$apingerstatus = file('/var/run/apinger.status'); $apingerstatus = file('/var/run/apinger.status');
} }
$status = array(); $status = array();
foreach ($apingerstatus as $line) { foreach ($apingerstatus as $line) {
$info = explode('|', $line); $info = explode('|', $line);
if ($byname == false) { if ($byname == false) {
$target = $info[0]; $target = $info[0];
} else { } else {
$target = $info[2]; $target = $info[2];
} }
$status[$target] = array(); $status[$target] = array();
$status[$target]['monitorip'] = $info[0]; $status[$target]['monitorip'] = $info[0];
$status[$target]['srcip'] = $info[1]; $status[$target]['srcip'] = $info[1];
$status[$target]['name'] = $info[2]; $status[$target]['name'] = $info[2];
$status[$target]['lastcheck'] = $info[5] ? date('r', $info[5]) : date('r'); $status[$target]['lastcheck'] = $info[5] ? date('r', $info[5]) : date('r');
$status[$target]['delay'] = empty($info[6]) ? "0ms" : round($info[6], 1) ."ms" ; $status[$target]['delay'] = empty($info[6]) ? "0ms" : round($info[6], 1) ."ms" ;
$status[$target]['loss'] = empty($info[7]) ? "0.0%" : round($info[7], 1) . "%"; $status[$target]['loss'] = empty($info[7]) ? "0.0%" : round($info[7], 1) . "%";
$status[$target]['status'] = trim($info[8]); $status[$target]['status'] = trim($info[8]);
} }
/* tack on any gateways that have monitoring disabled /* tack on any gateways that have monitoring disabled
* or are down, which could cause gateway groups to fail */ * or are down, which could cause gateway groups to fail */
$gateways_arr = return_gateways_array(); $gateways_arr = return_gateways_array();
foreach($gateways_arr as $gwitem) { foreach($gateways_arr as $gwitem) {
if(!isset($gwitem['monitor_disable'])) if (!isset($gwitem['monitor_disable'])) {
continue; continue;
if(!isset($gwitem['monitorip']) || !is_ipaddr($gwitem['monitorip'])) { }
$realif = $gwitem['interface']; if (!isset($gwitem['monitorip']) || !is_ipaddr($gwitem['monitorip'])) {
$tgtip = get_interface_gateway($realif); $realif = $gwitem['interface'];
if (!is_ipaddr($tgtip)) $tgtip = get_interface_gateway($realif);
$tgtip = "none"; if (!is_ipaddr($tgtip)) {
$srcip = find_interface_ip($realif); $tgtip = "none";
} else { }
$tgtip = $gwitem['monitorip']; $srcip = find_interface_ip($realif);
$srcip = find_interface_ip($realif); } else {
} $tgtip = $gwitem['monitorip'];
if($byname == true) $srcip = find_interface_ip($realif);
$target = $gwitem['name']; }
else if ($byname == true) {
$target = $tgtip; $target = $gwitem['name'];
} else {
/* failsafe for down interfaces */ $target = $tgtip;
if($target == "none") { }
$target = $gwitem['name'];
$status[$target]['name'] = $gwitem['name']; /* failsafe for down interfaces */
$status[$target]['lastcheck'] = date('r'); if ($target == "none") {
$status[$target]['delay'] = "0.0ms"; $target = $gwitem['name'];
$status[$target]['loss'] = "100.0%"; $status[$target]['name'] = $gwitem['name'];
$status[$target]['status'] = "down"; $status[$target]['lastcheck'] = date('r');
} else { $status[$target]['delay'] = "0.0ms";
$status[$target]['monitorip'] = $tgtip; $status[$target]['loss'] = "100.0%";
$status[$target]['srcip'] = $srcip; $status[$target]['status'] = "down";
$status[$target]['name'] = $gwitem['name']; } else {
$status[$target]['lastcheck'] = date('r'); $status[$target]['monitorip'] = $tgtip;
$status[$target]['delay'] = "0.0ms"; $status[$target]['srcip'] = $srcip;
$status[$target]['loss'] = "0.0%"; $status[$target]['name'] = $gwitem['name'];
$status[$target]['status'] = "none"; $status[$target]['lastcheck'] = date('r');
} $status[$target]['delay'] = "0.0ms";
} $status[$target]['loss'] = "0.0%";
return($status); $status[$target]['status'] = "none";
}
}
return($status);
} }
/* Return all configured gateways on the system */ /* Return all configured gateways on the system */
function return_gateways_array($disabled = false, $localhost = false, $inactive = false) function return_gateways_array($disabled = false, $localhost = false, $inactive = false)
{ {
global $config; global $config;
$gateways_arr = array(); $gateways_arr = array();
$found_defaultv4 = 0; $found_defaultv4 = 0;
$found_defaultv6 = 0; $found_defaultv6 = 0;
$interfaces = legacy_interface_listget(); $interfaces = legacy_interface_listget();
$interfaces_v4 = array(); $interfaces_v4 = array();
$interfaces_v6 = array(); $interfaces_v6 = array();
$i = -1; $i = -1;
/* Process/add all the configured gateways. */ /* Process/add all the configured gateways. */
if (isset($config['gateways']['gateway_item'])) { if (isset($config['gateways']['gateway_item'])) {
foreach ($config['gateways']['gateway_item'] as $gateway) { foreach ($config['gateways']['gateway_item'] as $gateway) {
/* Increment it here to do not skip items */ /* Increment it here to do not skip items */
$i++; $i++;
if (empty($config['interfaces'][$gateway['interface']])) { if (empty($config['interfaces'][$gateway['interface']])) {
if ($inactive === false) if ($inactive === false) {
continue; continue;
else } else {
$gateway['inactive'] = true; $gateway['inactive'] = true;
} }
$wancfg = $config['interfaces'][$gateway['interface']]; }
$wancfg = $config['interfaces'][$gateway['interface']];
/* skip disabled interfaces */
if ($disabled === false && (!isset($wancfg['enable']) || isset($gateway['disabled']))) /* skip disabled interfaces */
continue; if ($disabled === false && (!isset($wancfg['enable']) || isset($gateway['disabled']))) {
continue;
/* if the gateway is dynamic and we can find the IPv4, Great! */ }
if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
if ($gateway['ipprotocol'] == "inet") { /* if the gateway is dynamic and we can find the IPv4, Great! */
/* we know which interfaces is dynamic, this should be made a function */ if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
$gateway['gateway'] = get_interface_gateway($gateway['interface']); if ($gateway['ipprotocol'] == "inet") {
/* no IP address found, set to dynamic */ /* we know which interfaces is dynamic, this should be made a function */
if (!is_ipaddrv4($gateway['gateway'])) $gateway['gateway'] = get_interface_gateway($gateway['interface']);
$gateway['gateway'] = "dynamic"; /* no IP address found, set to dynamic */
$gateway['dynamic'] = true; if (!is_ipaddrv4($gateway['gateway'])) {
} $gateway['gateway'] = "dynamic";
}
/* if the gateway is dynamic and we can find the IPv6, Great! */ $gateway['dynamic'] = true;
else if ($gateway['ipprotocol'] == "inet6") { } elseif ($gateway['ipprotocol'] == "inet6") {
/* we know which interfaces is dynamic, this should be made a function, and for v6 too */ /* if the gateway is dynamic and we can find the IPv6, Great! */
$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']); /* we know which interfaces is dynamic, this should be made a function, and for v6 too */
/* no IPv6 address found, set to dynamic */ $gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
if (!is_ipaddrv6($gateway['gateway'])) /* no IPv6 address found, set to dynamic */
$gateway['gateway'] = "dynamic"; if (!is_ipaddrv6($gateway['gateway'])) {
$gateway['dynamic'] = true; $gateway['gateway'] = "dynamic";
} }
} else { $gateway['dynamic'] = true;
/* getting this detection right is hard at this point because we still don't }
* store the address family in the gateway item */ } else {
if (is_ipaddrv4($gateway['gateway'])) /* getting this detection right is hard at this point because we still don't
$gateway['ipprotocol'] = "inet"; * store the address family in the gateway item */
else if(is_ipaddrv6($gateway['gateway'])) if (is_ipaddrv4($gateway['gateway'])) {
$gateway['ipprotocol'] = "inet6"; $gateway['ipprotocol'] = "inet";
} } elseif (is_ipaddrv6($gateway['gateway'])) {
$gateway['ipprotocol'] = "inet6";
if (isset($gateway['monitor_disable'])) { }
$gateway['monitor_disable'] = true; }
} elseif (empty($gateway['monitor'])) {
$gateway['monitor'] = $gateway['gateway']; if (isset($gateway['monitor_disable'])) {
} $gateway['monitor_disable'] = true;
} elseif (empty($gateway['monitor'])) {
$gateway['friendlyiface'] = $gateway['interface']; $gateway['monitor'] = $gateway['gateway'];
}
/* special treatment for tunnel interfaces */
if ($gateway['ipprotocol'] == "inet6") { $gateway['friendlyiface'] = $gateway['interface'];
$gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
$interfaces_v6[$gateway['friendlyiface']] = $gateway['friendlyiface']; /* special treatment for tunnel interfaces */
} else { if ($gateway['ipprotocol'] == "inet6") {
$gateway['interface'] = get_real_interface($gateway['interface'], "all", false, false); $gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
$interfaces_v4[$gateway['friendlyiface']] = $gateway['friendlyiface']; $interfaces_v6[$gateway['friendlyiface']] = $gateway['friendlyiface'];
} } else {
$gateway['interface'] = get_real_interface($gateway['interface'], "all", false, false);
/* entry has a default flag, use it */ $interfaces_v4[$gateway['friendlyiface']] = $gateway['friendlyiface'];
if (isset($gateway['defaultgw'])) { }
if ($gateway['ipprotocol'] == "inet") {
$gateway['defaultgw'] = true; /* entry has a default flag, use it */
$found_defaultv4 = 1; if (isset($gateway['defaultgw'])) {
} else if ($gateway['ipprotocol'] == "inet6") { if ($gateway['ipprotocol'] == "inet") {
$gateway['defaultgw'] = true; $gateway['defaultgw'] = true;
$found_defaultv6 = 1; $found_defaultv4 = 1;
} } else if ($gateway['ipprotocol'] == "inet6") {
} $gateway['defaultgw'] = true;
/* include the gateway index as the attribute */ $found_defaultv6 = 1;
$gateway['attribute'] = $i; }
}
$gateways_arr[$gateway['name']] = $gateway; /* include the gateway index as the attribute */
} $gateway['attribute'] = $i;
}
unset($gateway); $gateways_arr[$gateway['name']] = $gateway;
}
/* Loop through all interfaces with a gateway and add it to a array */ }
if ($disabled == false) unset($gateway);
$iflist = get_configured_interface_with_descr();
else /* Loop through all interfaces with a gateway and add it to a array */
$iflist = get_configured_interface_with_descr(false, true); if ($disabled == false) {
$iflist = get_configured_interface_with_descr();
/* Process/add dynamic v4 gateways. */ } else {
foreach($iflist as $ifname => $friendly ) { $iflist = get_configured_interface_with_descr(false, true);
if(! interface_has_gateway($ifname)) }
continue;
/* Process/add dynamic v4 gateways. */
if (empty($config['interfaces'][$ifname])) foreach($iflist as $ifname => $friendly ) {
continue; if (! interface_has_gateway($ifname)) {
continue;
$ifcfg = &$config['interfaces'][$ifname]; }
if(!isset($ifcfg['enable']))
continue; if (empty($config['interfaces'][$ifname])) {
continue;
if(!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) }
continue;
$ifcfg = &$config['interfaces'][$ifname];
if (isset($interfaces_v4[$ifname])) if (!isset($ifcfg['enable'])) {
continue; continue;
}
$ctype = "";
switch($ifcfg['ipaddr']) { if (!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) {
case "dhcp": continue;
case "pppoe": }
case "pptp":
case "ppp": if (isset($interfaces_v4[$ifname])) {
$ctype = strtoupper($ifcfg['ipaddr']); continue;
break; }
default:
if (substr($ifcfg['if'], 0, 4) == "ovpn") { $ctype = "";
// if current iface is an ovpn server endpoint then skip it switch($ifcfg['ipaddr']) {
if (substr($ifcfg['if'], 4, 1) == 's') case "dhcp":
continue 2; case "pppoe":
case "pptp":
$ctype = "VPNv4"; case "ppp":
} $ctype = strtoupper($ifcfg['ipaddr']);
break; break;
} default:
$ctype = "_". strtoupper($ctype); if (substr($ifcfg['if'], 0, 4) == "ovpn") {
// if current iface is an ovpn server endpoint then skip it
$gateway = array(); if (substr($ifcfg['if'], 4, 1) == 's') {
$gateway['dynamic'] = false; continue 2;
$gateway['ipprotocol'] = "inet"; }
$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
$gateway['interface'] = get_real_interface($ifname); $ctype = "VPNv4";
$gateway['friendlyiface'] = $ifname; }
$gateway['name'] = "{$friendly}{$ctype}"; break;
$gateway['attribute'] = "system"; }
$ctype = "_". strtoupper($ctype);
if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
$gateway['defaultgw'] = true; $gateway = array();
$gateway['dynamic'] = true; $gateway['dynamic'] = false;
$found_defaultv4 = 1; $gateway['ipprotocol'] = "inet";
} $gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
/* Loopback dummy for dynamic interfaces without a IP */ $gateway['interface'] = get_real_interface($ifname);
if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) $gateway['friendlyiface'] = $ifname;
$gateway['gateway'] = "dynamic"; $gateway['name'] = "{$friendly}{$ctype}";
$gateway['attribute'] = "system";
/* automatically skip known static and dynamic gateways we have a array entry for */
foreach($gateways_arr as $gateway_item) { if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) || $gateway['defaultgw'] = true;
($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) $gateway['dynamic'] = true;
continue 2; $found_defaultv4 = 1;
} }
/* Loopback dummy for dynamic interfaces without a IP */
if (is_ipaddrv4($gateway['gateway'])) if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) {
$gateway['monitor'] = $gateway['gateway']; $gateway['gateway'] = "dynamic";
}
$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
$gateway['monitor_disable'] = true; /* automatically skip known static and dynamic gateways we have a array entry for */
$gateways_arr[$gateway['name']] = $gateway; foreach($gateways_arr as $gateway_item) {
} if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
unset($gateway); ($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) {
continue 2;
/* Process/add dynamic v6 gateways. */ }
foreach($iflist as $ifname => $friendly ) { }
/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
if (!isset($config['system']['ipv6allow'])) if (is_ipaddrv4($gateway['gateway'])) {
break; $gateway['monitor'] = $gateway['gateway'];
}
if(! interface_has_gatewayv6($ifname))
continue; $gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
$gateway['monitor_disable'] = true;
if (empty($config['interfaces'][$ifname])) $gateways_arr[$gateway['name']] = $gateway;
continue; }
unset($gateway);
$ifcfg = &$config['interfaces'][$ifname];
if(!isset($ifcfg['enable'])) /* Process/add dynamic v6 gateways. */
continue; foreach($iflist as $ifname => $friendly ) {
/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
if(!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) if (!isset($config['system']['ipv6allow'])) {
continue; break;
}
if(isset($interfaces_v6[$ifname]))
continue; if (!interface_has_gatewayv6($ifname)) {
continue;
$ctype = ""; }
switch($ifcfg['ipaddrv6']) {
case "slaac": if (empty($config['interfaces'][$ifname])) {
case "dhcp6": continue;
case "6to4": }
case "6rd":
$ctype = strtoupper($ifcfg['ipaddrv6']); $ifcfg = &$config['interfaces'][$ifname];
break; if (!isset($ifcfg['enable'])) {
default: continue;
$tunnelif = substr($ifcfg['if'], 0, 3); }
if (substr($ifcfg['if'], 0, 4) == "ovpn") {
// if current iface is an ovpn server endpoint then skip it if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
if (substr($ifcfg['if'], 4, 1) == 's') continue;
continue 2; }
$ctype = "VPNv6"; if (isset($interfaces_v6[$ifname])) {
} else if ($tunnelif == "gif" || $tunnelif == "gre") continue;
$ctype = "TUNNELv6"; }
break;
} $ctype = "";
$ctype = "_". strtoupper($ctype); switch($ifcfg['ipaddrv6']) {
case "slaac":
$gateway = array(); case "dhcp6":
$gateway['dynamic'] = false; case "6to4":
$gateway['ipprotocol'] = "inet6"; case "6rd":
$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']); $ctype = strtoupper($ifcfg['ipaddrv6']);
$gateway['interface'] = get_real_interface($ifname, "inet6"); break;
switch($ifcfg['ipaddrv6']) { default:
case "6rd": $tunnelif = substr($ifcfg['if'], 0, 3);
case "6to4": if (substr($ifcfg['if'], 0, 4) == "ovpn") {
$gateway['dynamic'] = "default"; // if current iface is an ovpn server endpoint then skip it
break; if (substr($ifcfg['if'], 4, 1) == 's') {
} continue 2;
$gateway['friendlyiface'] = $ifname; }
$gateway['name'] = "{$friendly}{$ctype}";
$gateway['attribute'] = "system"; $ctype = "VPNv6";
} elseif ($tunnelif == "gif" || $tunnelif == "gre") {
if (($gateway['dynamic'] === "default") && ($found_defaultv6 == 0)) { $ctype = "TUNNELv6";
$gateway['defaultgw'] = true; }
$gateway['dynamic'] = true; break;
$found_defaultv6 = 1; }
} $ctype = "_". strtoupper($ctype);
/* Loopback dummy for dynamic interfaces without a IP */ $gateway = array();
if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true) $gateway['dynamic'] = false;
$gateway['gateway'] = "dynamic"; $gateway['ipprotocol'] = "inet6";
$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
/* automatically skip known static and dynamic gateways we have a array entry for */ $gateway['interface'] = get_real_interface($ifname, "inet6");
foreach($gateways_arr as $gateway_item) { switch($ifcfg['ipaddrv6']) {
if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) || case "6rd":
($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) case "6to4":
continue 2; $gateway['dynamic'] = "default";
} break;
}
if (is_ipaddrv6($gateway['gateway'])) $gateway['friendlyiface'] = $ifname;
$gateway['monitor'] = $gateway['gateway']; $gateway['name'] = "{$friendly}{$ctype}";
$gateway['attribute'] = "system";
$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
$gateway['monitor_disable'] = true; if (($gateway['dynamic'] === "default") && ($found_defaultv6 == 0)) {
$gateways_arr[$gateway['name']] = $gateway; $gateway['defaultgw'] = true;
} $gateway['dynamic'] = true;
unset($gateway); $found_defaultv6 = 1;
}
/* FIXME: Should this be enabled.
* Some interface like wan might be default but have no info recorded /* Loopback dummy for dynamic interfaces without a IP */
* the config. */ if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true) {
/* this is a fallback if all else fails and we want to get packets out @smos */ $gateway['gateway'] = "dynamic";
if ($found_defaultv4 == 0 || $found_defaultv6 == 0) { }
foreach ($gateways_arr as &$gateway) {
if (($gateway['friendlyiface'] == "wan") && ($found_defaultv4 == 0) && (!isset($gateway['ipprotocol']) || ($gateway['ipprotocol'] == "inet"))) { /* automatically skip known static and dynamic gateways we have a array entry for */
if (file_exists("/tmp/{$gateway['interface']}_defaultgw")) { foreach($gateways_arr as $gateway_item) {
$gateway['defaultgw'] = true; if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
$found_defaultv4 = 1; ($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) {
} continue 2;
} }
if (($gateway['friendlyiface'] == "wan") && ($found_defaultv6 == 0) && ($gateway['ipprotocol'] == "inet6")) { }
if (file_exists("/tmp/{$gateway['interface']}_defaultgwv6")) {
$gateway['defaultgw'] = true; if (is_ipaddrv6($gateway['gateway'])) {
$found_defaultv6 = 1; $gateway['monitor'] = $gateway['gateway'];
} }
}
} $gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
} $gateway['monitor_disable'] = true;
$gateways_arr[$gateway['name']] = $gateway;
if($localhost === true) { }
/* attach localhost for Null routes */ unset($gateway);
$gwlo4 = array();
$gwlo4['name'] = "Null4"; /* FIXME: Should this be enabled.
$gwlo4['interface'] = "lo0"; * Some interface like wan might be default but have no info recorded
$gwlo4['ipprotocol'] = "inet"; * the config. */
$gwlo4['gateway'] = "127.0.0.1"; /* this is a fallback if all else fails and we want to get packets out @smos */
$gwlo6 = array(); if ($found_defaultv4 == 0 || $found_defaultv6 == 0) {
$gwlo6['name'] = "Null6"; foreach ($gateways_arr as &$gateway) {
$gwlo6['interface'] = "lo0"; if (($gateway['friendlyiface'] == "wan") && ($found_defaultv4 == 0) && (!isset($gateway['ipprotocol']) || ($gateway['ipprotocol'] == "inet"))) {
$gwlo6['ipprotocol'] = "inet6"; if (file_exists("/tmp/{$gateway['interface']}_defaultgw")) {
$gwlo6['gateway'] = "::1"; $gateway['defaultgw'] = true;
$gateways_arr['Null4'] = $gwlo4; $found_defaultv4 = 1;
$gateways_arr['Null6'] = $gwlo6; }
} }
return($gateways_arr); if (($gateway['friendlyiface'] == "wan") && ($found_defaultv6 == 0) && ($gateway['ipprotocol'] == "inet6")) {
if (file_exists("/tmp/{$gateway['interface']}_defaultgwv6")) {
$gateway['defaultgw'] = true;
$found_defaultv6 = 1;
}
}
}
}
if ($localhost === true) {
/* attach localhost for Null routes */
$gwlo4 = array();
$gwlo4['name'] = "Null4";
$gwlo4['interface'] = "lo0";
$gwlo4['ipprotocol'] = "inet";
$gwlo4['gateway'] = "127.0.0.1";
$gwlo6 = array();
$gwlo6['name'] = "Null6";
$gwlo6['interface'] = "lo0";
$gwlo6['ipprotocol'] = "inet6";
$gwlo6['gateway'] = "::1";
$gateways_arr['Null4'] = $gwlo4;
$gateways_arr['Null6'] = $gwlo6;
}
return($gateways_arr);
} }
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) function fixup_default_gateway($ipprotocol, $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 = ""; $upgw = "";
$dfltgwdown = false; $dfltgwdown = false;
$dfltgwfound = false; $dfltgwfound = false;
foreach ($gateways_arr as $gwname => $gwsttng) { foreach ($gateways_arr as $gwname => $gwsttng) {
if (($gwsttng['ipprotocol'] == $ipprotocol) && isset($gwsttng['defaultgw'])) { if (($gwsttng['ipprotocol'] == $ipprotocol) && isset($gwsttng['defaultgw'])) {
$dfltgwfound = true; $dfltgwfound = true;
$dfltgwname = $gwname; $dfltgwname = $gwname;
if (!isset($gwsttng['monitor_disable']) && stristr($gateways_status[$gwname]['status'], "down")) if (!isset($gwsttng['monitor_disable']) && stristr($gateways_status[$gwname]['status'], "down")) {
$dfltgwdown = true; $dfltgwdown = 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 */ /* Keep a record of the last up gateway */
if (empty($upgw) && ($gwsttng['ipprotocol'] == $ipprotocol) && (isset($gwsttng['monitor_disable']) || !stristr($gateways_status[$gwname]['status'], "down")) && $gwsttng[$gwname]['friendlyiface'] != "lan") /* XXX: Blacklist lan for now since it might cause issues to those who have a gateway set for it */
$upgw = $gwname; if (empty($upgw) && ($gwsttng['ipprotocol'] == $ipprotocol) && (isset($gwsttng['monitor_disable']) || !stristr($gateways_status[$gwname]['status'], "down")) && $gwsttng[$gwname]['friendlyiface'] != "lan") {
if ($dfltgwdown == true && !empty($upgw)) $upgw = $gwname;
break; }
} if ($dfltgwdown == true && !empty($upgw)) {
if ($dfltgwfound == false) { break;
$gwname = convert_friendly_interface_to_friendly_descr("wan"); }
if (!empty($gateways_status[$gwname]) && stristr($gateways_status[$gwname]['status'], "down")) }
$dfltgwdown = true; if ($dfltgwfound == false) {
} $gwname = convert_friendly_interface_to_friendly_descr("wan");
if ($dfltgwdown == true && !empty($upgw)) { if (!empty($gateways_status[$gwname]) && stristr($gateways_status[$gwname]['status'], "down")) {
if ($gateways_arr[$upgw]['gateway'] == "dynamic") $dfltgwdown = true;
$gateways_arr[$upgw]['gateway'] = get_interface_gateway($gateways_arr[$upgw]['friendlyiface']); }
if (is_ipaddr($gateways_arr[$upgw]['gateway'])) { }
log_error("Default gateway down setting {$upgw} as default!"); if ($dfltgwdown == true && !empty($upgw)) {
if(is_ipaddrv6($gateways_arr[$upgw]['gateway'])) { if ($gateways_arr[$upgw]['gateway'] == "dynamic") {
$inetfamily = "-inet6"; $gateways_arr[$upgw]['gateway'] = get_interface_gateway($gateways_arr[$upgw]['friendlyiface']);
} else { }
$inetfamily = "-inet"; if (is_ipaddr($gateways_arr[$upgw]['gateway'])) {
} log_error("Default gateway down setting {$upgw} as default!");
mwexec("/sbin/route delete {$inetfamily} default {$gateways_arr[$upgw]['gateway']}"); if (is_ipaddrv6($gateways_arr[$upgw]['gateway'])) {
mwexec("/sbin/route add {$inetfamily} default {$gateways_arr[$upgw]['gateway']}"); $inetfamily = "-inet6";
} } else {
} else { $inetfamily = "-inet";
$defaultgw = trim(exec("/sbin/route -n get -{$ipprotocol} default | /usr/bin/awk '/gateway:/ {print $2}'"), " \n"); }
if(is_ipaddrv6($gateways_arr[$dfltgwname]['gateway'])) { mwexec("/sbin/route delete {$inetfamily} default {$gateways_arr[$upgw]['gateway']}");
$inetfamily = "-inet6"; mwexec("/sbin/route add {$inetfamily} default {$gateways_arr[$upgw]['gateway']}");
} else { }
$inetfamily = "-inet"; } else {
} $defaultgw = trim(exec("/sbin/route -n get -{$ipprotocol} default | /usr/bin/awk '/gateway:/ {print $2}'"), " \n");
if ($defaultgw != $gateways_arr[$dfltgwname]['gateway']) { if (is_ipaddrv6($gateways_arr[$dfltgwname]['gateway'])) {
mwexec("/sbin/route delete {$inetfamily} default {$gateways_arr[$dfltgwname]['gateway']}"); $inetfamily = "-inet6";
mwexec("/sbin/route add {$inetfamily} default {$gateways_arr[$dfltgwname]['gateway']}"); } else {
} $inetfamily = "-inet";
} }
if ($defaultgw != $gateways_arr[$dfltgwname]['gateway']) {
mwexec("/sbin/route delete {$inetfamily} default {$gateways_arr[$dfltgwname]['gateway']}");
mwexec("/sbin/route add {$inetfamily} default {$gateways_arr[$dfltgwname]['gateway']}");
}
}
} }
/* /*
...@@ -816,256 +870,271 @@ function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) ...@@ -816,256 +870,271 @@ function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr)
*/ */
function return_gateway_groups_array() function return_gateway_groups_array()
{ {
global $config; global $config;
/* fetch the current gateways status */ /* fetch the current gateways status */
$gateways_status = return_gateways_status(true); $gateways_status = return_gateways_status(true);
$gateways_arr = return_gateways_array(); $gateways_arr = return_gateways_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("inet", $gateways_status, $gateways_arr);
fixup_default_gateway("inet6", $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();
foreach ($config['gateways']['gateway_group'] as $group) { foreach ($config['gateways']['gateway_group'] as $group) {
/* create array with group gateways members separated by tier */ /* create array with group gateways members separated by tier */
$tiers = array(); $tiers = array();
$backupplan = array(); $backupplan = array();
$gwvip_arr = array(); $gwvip_arr = array();
foreach ($group['item'] as $item) { foreach ($group['item'] as $item) {
list($gwname, $tier, $vipname) = explode("|", $item); list($gwname, $tier, $vipname) = explode("|", $item);
if (is_ipaddr($carplist[$vipname])) { if (is_ipaddr($carplist[$vipname])) {
if (!is_array($gwvip_arr[$group['name']])) if (!is_array($gwvip_arr[$group['name']])) {
$gwvip_arr[$group['name']] = array(); $gwvip_arr[$group['name']] = array();
$gwvip_arr[$group['name']][$gwname] = $vipname; }
} $gwvip_arr[$group['name']][$gwname] = $vipname;
}
/* Do it here rather than reiterating again the group in case no member is up. */
if (!is_array($backupplan[$tier])) /* Do it here rather than reiterating again the group in case no member is up. */
$backupplan[$tier] = array(); if (!is_array($backupplan[$tier])) {
$backupplan[$tier][] = $gwname; $backupplan[$tier] = array();
}
/* check if the gateway is available before adding it to the array */ $backupplan[$tier][] = $gwname;
if (is_array($gateways_status[$gwname])) {
$status = $gateways_status[$gwname]; /* check if the gateway is available before adding it to the array */
$gwdown = false; if (is_array($gateways_status[$gwname])) {
if (stristr($status['status'], 'down')) { $status = $gateways_status[$gwname];
$msg = sprintf(gettext('MONITOR: %s is down, removing from routing group %s'), $gwname, $group['name']); $gwdown = false;
$gwdown = true; if (stristr($status['status'], 'down')) {
} elseif (stristr($status['status'], 'loss') && strstr($group['trigger'], 'loss')) { $msg = sprintf(gettext('MONITOR: %s is down, removing from routing group %s'), $gwname, $group['name']);
/* packet loss */ $gwdown = true;
$msg = sprintf(gettext('MONITOR: %s has packet loss, removing from routing group %s'), $gwname, $group['name']); } elseif (stristr($status['status'], 'loss') && strstr($group['trigger'], 'loss')) {
$gwdown = true; /* packet loss */
} elseif (stristr($status['status'], 'delay') && strstr($group['trigger'] , 'latency')) { $msg = sprintf(gettext('MONITOR: %s has packet loss, removing from routing group %s'), $gwname, $group['name']);
/* high latency */ $gwdown = true;
$msg = sprintf(gettext('MONITOR: %s has high latency, removing from routing group %s'), $gwname, $group['name']); } elseif (stristr($status['status'], 'delay') && strstr($group['trigger'] , 'latency')) {
$gwdown = true; /* high latency */
} $msg = sprintf(gettext('MONITOR: %s has high latency, removing from routing group %s'), $gwname, $group['name']);
if ($gwdown == true) { $gwdown = true;
log_error($msg); }
notify_via_growl($msg); if ($gwdown == true) {
notify_via_smtp($msg); log_error($msg);
} else { notify_via_growl($msg);
/* Online add member */ notify_via_smtp($msg);
if (!is_array($tiers[$tier])) } else {
$tiers[$tier] = array(); /* Online add member */
$tiers[$tier][] = $gwname; if (!is_array($tiers[$tier])) {
} $tiers[$tier] = array();
} elseif (isset($gateways_arr[$gwname]['monitor_disable'])) { }
$tiers[$tier][] = $gwname; $tiers[$tier][] = $gwname;
} }
} } elseif (isset($gateways_arr[$gwname]['monitor_disable'])) {
$tiers_count = count($tiers); $tiers[$tier][] = $gwname;
if ($tiers_count == 0) { }
/* Oh dear, we have no members! Engage Plan B */ }
if (!file_exists('/var/run/booting')) { $tiers_count = count($tiers);
$msg = sprintf(gettext('Gateways status could not be determined, considering all as up/active. (Group: %s)'), $group['name']); if ($tiers_count == 0) {
log_error($msg); /* Oh dear, we have no members! Engage Plan B */
notify_via_growl($msg); if (!file_exists('/var/run/booting')) {
//notify_via_smtp($msg); $msg = sprintf(gettext('Gateways status could not be determined, considering all as up/active. (Group: %s)'), $group['name']);
} log_error($msg);
$tiers = $backupplan; notify_via_growl($msg);
} //notify_via_smtp($msg);
/* sort the tiers array by the tier key */ }
ksort($tiers); $tiers = $backupplan;
}
/* we do not really foreach the tiers as we stop after the first tier */ /* sort the tiers array by the tier key */
foreach ($tiers as $tieridx => $tier) { ksort($tiers);
/* process all gateways in this tier */
foreach ($tier as $member) { /* we do not really foreach the tiers as we stop after the first tier */
/* determine interface gateway */ foreach ($tiers as $tieridx => $tier) {
if (isset($gateways_arr[$member])) { /* process all gateways in this tier */
$gateway = $gateways_arr[$member]; foreach ($tier as $member) {
$int = $gateway['interface']; /* determine interface gateway */
$gatewayip = ""; if (isset($gateways_arr[$member])) {
if(is_ipaddr($gateway['gateway'])) $gateway = $gateways_arr[$member];
$gatewayip = $gateway['gateway']; $int = $gateway['interface'];
else if (!empty($int)) $gatewayip = "";
$gatewayip = get_interface_gateway($gateway['friendlyiface']); if (is_ipaddr($gateway['gateway'])) {
$gatewayip = $gateway['gateway'];
if (!empty($int)) { } elseif (!empty($int)) {
$gateway_groups_array[$group['name']]['ipprotocol'] = $gateway['ipprotocol']; $gatewayip = get_interface_gateway($gateway['friendlyiface']);
if (is_ipaddr($gatewayip)) { }
$groupmember = array();
$groupmember['int'] = $int; if (!empty($int)) {
$groupmember['gwip'] = $gatewayip; $gateway_groups_array[$group['name']]['ipprotocol'] = $gateway['ipprotocol'];
$groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1; if (is_ipaddr($gatewayip)) {
if (is_array($gwvip_arr[$group['name']])&& !empty($gwvip_arr[$group['name']][$member])) $groupmember = array();
$groupmember['vip'] = $gwvip_arr[$group['name']][$member]; $groupmember['int'] = $int;
$gateway_groups_array[$group['name']][] = $groupmember; $groupmember['gwip'] = $gatewayip;
} $groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1;
} if (is_array($gwvip_arr[$group['name']])&& !empty($gwvip_arr[$group['name']][$member])) {
} $groupmember['vip'] = $gwvip_arr[$group['name']][$member];
} }
/* we should have the 1st available tier now, exit stage left */ $gateway_groups_array[$group['name']][] = $groupmember;
if (count($gateway_groups_array[$group['name']]) > 0) }
break; }
else }
log_error("GATEWAYS: Group {$group['name']} did not have any gateways up on tier {$tieridx}!"); }
} /* we should have the 1st available tier now, exit stage left */
} if (count($gateway_groups_array[$group['name']]) > 0) {
} break;
} else {
return ($gateway_groups_array); log_error("GATEWAYS: Group {$group['name']} did not have any gateways up on tier {$tieridx}!");
}
}
}
}
return ($gateway_groups_array);
} }
function lookup_gateway_ip_by_name($name) { function lookup_gateway_ip_by_name($name)
{
$gateways_arr = return_gateways_array(true, true); $gateways_arr = return_gateways_array(true, true);
foreach ($gateways_arr as $gname => $gw) { foreach ($gateways_arr as $gname => $gw) {
if ($gw['name'] === $name || $gname === $name) if ($gw['name'] === $name || $gname === $name) {
return $gw['gateway']; return $gw['gateway'];
} }
}
return false; return false;
} }
function lookup_gateway_interface_by_name($name) { function lookup_gateway_interface_by_name($name)
{
$gateways_arr = return_gateways_array(false, true); $gateways_arr = return_gateways_array(false, true);
if (!empty($gateways_arr[$name])) { if (!empty($gateways_arr[$name])) {
$interfacegw = $gateways_arr[$name]['friendlyiface']; $interfacegw = $gateways_arr[$name]['friendlyiface'];
return ($interfacegw); return ($interfacegw);
} }
return (false); return (false);
} }
function get_interface_gateway($interface, &$dynamic = false) function get_interface_gateway($interface, &$dynamic = false)
{ {
global $config; global $config;
$gw = NULL; $gw = NULL;
if (isset($config['interfaces'][$interface])) { if (isset($config['interfaces'][$interface])) {
$gwcfg = $config['interfaces'][$interface]; $gwcfg = $config['interfaces'][$interface];
if (!empty($gwcfg['gateway']) && isset($config['gateways']['gateway_item'])) { if (!empty($gwcfg['gateway']) && isset($config['gateways']['gateway_item'])) {
foreach($config['gateways']['gateway_item'] as $gateway) { foreach($config['gateways']['gateway_item'] as $gateway) {
if(($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) { if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
$gw = $gateway['gateway']; $gw = $gateway['gateway'];
break; break;
} }
} }
} }
// for dynamic interfaces we handle them through the $interface_router file. // for dynamic interfaces we handle them through the $interface_router file.
if (!is_ipaddrv4($gw) && !is_ipaddrv4($gwcfg['ipaddr'])) { if (!is_ipaddrv4($gw) && !is_ipaddrv4($gwcfg['ipaddr'])) {
$realif = get_real_interface($interface); $realif = get_real_interface($interface);
if (file_exists("/tmp/{$realif}_router")) { if (file_exists("/tmp/{$realif}_router")) {
$gw = trim(file_get_contents("/tmp/{$realif}_router"), " \n"); $gw = trim(file_get_contents("/tmp/{$realif}_router"), " \n");
$dynamic = true; $dynamic = true;
} }
if (file_exists("/tmp/{$realif}_defaultgw")) { if (file_exists("/tmp/{$realif}_defaultgw")) {
$dynamic = "default"; $dynamic = "default";
} }
} }
} }
/* return gateway */ /* return gateway */
return ($gw); return ($gw);
} }
function get_interface_gateway_v6($interface, &$dynamic = false) function get_interface_gateway_v6($interface, &$dynamic = false)
{ {
global $config; global $config;
$gw = NULL; $gw = NULL;
$gwcfg = $config['interfaces'][$interface]; $gwcfg = $config['interfaces'][$interface];
if (!empty($gwcfg['gatewayv6']) && isset($config['gateways']['gateway_item'])) { if (!empty($gwcfg['gatewayv6']) && isset($config['gateways']['gateway_item'])) {
foreach($config['gateways']['gateway_item'] as $gateway) { foreach($config['gateways']['gateway_item'] as $gateway) {
if(($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) { if (($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
$gw = $gateway['gateway']; $gw = $gateway['gateway'];
break; break;
} }
} }
} }
// for dynamic interfaces we handle them through the $interface_router file. // for dynamic interfaces we handle them through the $interface_router file.
if (!is_ipaddrv6($gw) && ( !isset($gwcfg['ipaddrv6']) || !is_ipaddrv6($gwcfg['ipaddrv6']))) { if (!is_ipaddrv6($gw) && ( !isset($gwcfg['ipaddrv6']) || !is_ipaddrv6($gwcfg['ipaddrv6']))) {
$realif = get_real_interface($interface); $realif = get_real_interface($interface);
if (file_exists("/tmp/{$realif}_routerv6")) { if (file_exists("/tmp/{$realif}_routerv6")) {
$gw = trim(file_get_contents("/tmp/{$realif}_routerv6"), " \n"); $gw = trim(file_get_contents("/tmp/{$realif}_routerv6"), " \n");
$dynamic = true; $dynamic = true;
} }
if (file_exists("/tmp/{$realif}_defaultgwv6")) if (file_exists("/tmp/{$realif}_defaultgwv6")) {
$dynamic = "default"; $dynamic = "default";
}
} }
/* return gateway */ /* return gateway */
return ($gw); return ($gw);
} }
/* Check a IP address against a gateway IP or name /* Check a IP address against a gateway IP or name
* to verify it's address family */ * to verify it's address family */
function validate_address_family($ipaddr, $gwname) { function validate_address_family($ipaddr, $gwname)
$v4ip = false; {
$v6ip = false; $v4ip = false;
$v4gw = false; $v6ip = false;
$v6gw = false; $v4gw = false;
$v6gw = false;
if(is_ipaddrv4($ipaddr))
$v4ip = true; if (is_ipaddrv4($ipaddr)) {
if(is_ipaddrv6($ipaddr)) $v4ip = true;
$v6ip = true; }
if(is_ipaddrv4($gwname)) if (is_ipaddrv6($ipaddr)) {
$v4gw = true; $v6ip = true;
if(is_ipaddrv6($gwname)) }
$v6gw = true; if (is_ipaddrv4($gwname)) {
$v4gw = true;
if($v4ip && $v4gw) }
return true; if (is_ipaddrv6($gwname)) {
if($v6ip && $v6gw) $v6gw = true;
return true; }
/* still no match, carry on, lookup gateways */ if ($v4ip && $v4gw) {
if(is_ipaddrv4(lookup_gateway_ip_by_name($gwname))) return true;
$v4gw = true; }
if(is_ipaddrv6(lookup_gateway_ip_by_name($gwname))) if ($v6ip && $v6gw) {
$v6gw = true; return true;
}
$gw_array = return_gateways_array();
if(is_array($gw_array[$gwname])) { /* still no match, carry on, lookup gateways */
switch($gw_array[$gwname]['ipprotocol']) { if (is_ipaddrv4(lookup_gateway_ip_by_name($gwname))) {
case "inet": $v4gw = true;
$v4gw = true; }
break; if (is_ipaddrv6(lookup_gateway_ip_by_name($gwname))) {
case "inet6": $v6gw = true;
$v6gw = true; }
break;
} $gw_array = return_gateways_array();
} if (is_array($gw_array[$gwname])) {
switch($gw_array[$gwname]['ipprotocol']) {
if($v4ip && $v4gw) case "inet":
return true; $v4gw = true;
if($v6ip && $v6gw) break;
return true; case "inet6":
$v6gw = true;
return false; break;
}
}
if ($v4ip && $v4gw) {
return true;
}
if ($v6ip && $v6gw) {
return true;
}
return false;
} }
?>
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