gwlb.inc 42.8 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4 5 6
    Copyright (C) 2008 Bill Marquette, Seth Mos
    Copyright (C) 2010 Ermal Luçi
    All rights reserved.
Ad Schellevis's avatar
Ad Schellevis committed
7

8 9
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
Ad Schellevis's avatar
Ad Schellevis committed
10

11 12
    1. Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
Ad Schellevis's avatar
Ad Schellevis committed
13

14 15 16
    2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
Ad Schellevis's avatar
Ad Schellevis committed
17

18 19 20 21 22 23 24 25 26 27
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
28
*/
Ad Schellevis's avatar
Ad Schellevis committed
29

30 31
function return_apinger_defaults()
{
32 33 34 35 36 37 38 39 40 41 42
    return array(
      "latencylow" => "200",
      "latencyhigh" => "500",
      "losslow" => "10",
      "losshigh" => "20",
      "interval" => "1",
      "down" => "10",
      "avg_delay_samples" => "10",
      "avg_loss_samples" => "50",
      "avg_loss_delay_samples" => "20"
    );
Ad Schellevis's avatar
Ad Schellevis committed
43 44
}

45 46
function setup_gateways_monitor_full()
{
47 48
    killbypid('/var/run/apinger.pid');
    setup_gateways_monitor();
49 50
}

Ad Schellevis's avatar
Ad Schellevis committed
51 52 53 54
/*
 * Creates monitoring configuration file and
 * adds appropriate static routes.
 */
55 56
function setup_gateways_monitor()
{
57
    global $config;
Ad Schellevis's avatar
Ad Schellevis committed
58

59 60
    @mkdir('/var/db/rrd', 0775);
    @chown('/var/db/rrd', 'nobody');
61

62
    $gateways_arr_all = return_gateways_array(true);
Ad Schellevis's avatar
Ad Schellevis committed
63

64 65
    $apinger_default = return_apinger_defaults();
    $apingerconfig = <<<EOD
Ad Schellevis's avatar
Ad Schellevis committed
66

67
# OPNsense apinger configuration file. Automatically Generated!
Ad Schellevis's avatar
Ad Schellevis committed
68 69 70 71 72 73 74 75 76

## User and group the pinger should run as
user "root"
group "wheel"

## Mailer to use (default: "/usr/lib/sendmail -t")
#mailer "/var/qmail/bin/qmail-inject"

## Location of the pid-file (default: "/var/run/apinger.pid")
77
pid_file "/var/run/apinger.pid"
Ad Schellevis's avatar
Ad Schellevis committed
78 79 80 81 82

## Format of timestamp (%s macro) (default: "%b %d %H:%M:%S")
#timestamp_format "%Y%m%d%H%M%S"

status {
83 84 85 86 87
  ## File where the status information should be written to
  file "/var/run/apinger.status"
  ## Interval between file updates
  ## when 0 or not set, file is written only when SIGUSR1 is received
  interval 5s
Ad Schellevis's avatar
Ad Schellevis committed
88 89 90 91 92 93 94 95 96
}

########################################
# RRDTool status gathering configuration
# Interval between RRD updates
rrd interval 60s;

## These parameters can be overridden in a specific alarm configuration
alarm default {
97 98 99
  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' "
  combine 10s
Ad Schellevis's avatar
Ad Schellevis committed
100 101 102 103 104
}

## "Down" alarm definition.
## This alarm will be fired when target doesn't respond for 30 seconds.
alarm down "down" {
105
  time {$apinger_default['down']}s
Ad Schellevis's avatar
Ad Schellevis committed
106 107 108 109 110 111
}

## "Delay" alarm definition.
## This alarm will be fired when responses are delayed more than 200ms
## it will be canceled, when the delay drops below 100ms
alarm delay "delay" {
112 113
  delay_low {$apinger_default['latencylow']}ms
  delay_high {$apinger_default['latencyhigh']}ms
Ad Schellevis's avatar
Ad Schellevis committed
114 115 116 117 118 119
}

## "Loss" alarm definition.
## This alarm will be fired when packet loss goes over 20%
## it will be canceled, when the loss drops below 10%
alarm loss "loss" {
120 121
  percent_low {$apinger_default['losslow']}
  percent_high {$apinger_default['losshigh']}
Ad Schellevis's avatar
Ad Schellevis committed
122 123 124
}

target default {
125 126
  ## How often the probe should be sent
  interval {$apinger_default['interval']}s
Ad Schellevis's avatar
Ad Schellevis committed
127

128 129 130
  ## How many replies should be used to compute average delay
  ## for controlling "delay" alarms
  avg_delay_samples {$apinger_default['avg_delay_samples']}
Ad Schellevis's avatar
Ad Schellevis committed
131

132 133
  ## How many probes should be used to compute average loss
  avg_loss_samples {$apinger_default['avg_loss_samples']}
Ad Schellevis's avatar
Ad Schellevis committed
134

135 136 137
  ## The delay (in samples) after which loss is computed
  ## without this delays larger than interval would be treated as loss
  avg_loss_delay_samples {$apinger_default['avg_loss_delay_samples']}
Ad Schellevis's avatar
Ad Schellevis committed
138

139 140
  ## Names of the alarms that may be generated for the target
  alarms "down","delay","loss"
Ad Schellevis's avatar
Ad Schellevis committed
141

142 143
  ## Location of the RRD
  #rrd file "/var/db/rrd/apinger-%t.rrd"
Ad Schellevis's avatar
Ad Schellevis committed
144 145 146 147
}

EOD;

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    $monitor_ips = array();
    foreach($gateways_arr_all as $name => $gateway) {
        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)) {
            continue;
        }

        /*
         * Interface IP is needed since apinger will bind a socket to it.
         * 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
         * or is_ipaddrv6 to identify packet type would be wrong, especially as
         * further checks (that can cope with the "dynamic" case) are present inside
         * the if block. So using $gateway['ipprotocol'] is the better option.
         */
        if ($gateway['ipprotocol'] == "inet") { // This is an IPv4 gateway...
            $gwifip = find_interface_ip($gateway['interface']);
            if (!is_ipaddrv4($gwifip)) {
                continue; //Skip this target
            }

            /* flush the monitor unconditionally */
            if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
                log_error("Removing static route for monitor {$gateway['monitor']}");
                mwexec("/sbin/route delete -host " . escapeshellarg($gateway['monitor']), true);
            }

            /* Do not monitor if such was requested */
            if (isset($gateway['disabled']) || isset($gateway['monitor_disable'])) {
                continue;
            }

            /*
             * If the gateway is the same as the monitor we do not add a
             * route as this will break the routing table.
             * Add static routes for each gateway with their monitor IP
             * not strictly necessary but is a added level of protection.
             */
            if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
                log_error("Adding static route for monitor through {$gateway['gateway']}");
                mwexec("/sbin/route add -host " . escapeshellarg($gateway['monitor']) .
                  " " . escapeshellarg($gateway['gateway']), true);
            }
        } elseif ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway...
            if ($gateway['monitor'] == $gateway['gateway']) {
                /* link locals really need a different src ip */
                if (is_linklocal($gateway['gateway'])) {
                    $gwifip = find_interface_ipv6_ll($gateway['interface']);
                } else {
                    $gwifip = find_interface_ipv6($gateway['interface']);
                }
            } else {
                /* 'monitor' has been set, so makes sure it has precedence over
                 * 'gateway' in defining the source IP. Otherwise if 'gateway'
                 * is a local link and 'monitor' is global routable then the
                 * ICMP6 response would not find its way back home...
                 */
                $gwifip = find_interface_ipv6($gateway['interface']);
                if (is_linklocal($gateway['monitor'])) {
                    if (!strstr($gateway['monitor'], '%')) {
                        $gateway['monitor'] .= "%{$gateway['interface']}";
                    }
                } else {
                    // Monitor is a routable address, so use a routable address for the "src" part
                    $gwifip = find_interface_ipv6($gateway['interface']);
                }
            }

            if (!is_ipaddrv6($gwifip)) {
                continue; //Skip this target
            }

            /* flush the monitor unconditionally */
            if (is_ipaddrv6($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
                log_error("Removing static route for monitor {$gateway['monitor']}");
                mwexec("/sbin/route delete -host -inet6 " . escapeshellarg($gateway['monitor']), true);
            }

            /* Do not monitor if such was requested */
            if (isset($gateway['disabled']) || isset($gateway['monitor_disable'])) {
                continue;
            }

            /*
             * If the gateway is the same as the monitor we do not add a
             * route as this will break the routing table.
             * Add static routes for each gateway with their monitor IP
             * not strictly necessary but is a added level of protection.
             */
            if (is_ipaddrv6($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
                log_error("Adding static route for monitor through {$gateway['gateway']}");
                mwexec("/sbin/route add -host -inet6 " . escapeshellarg($gateway['monitor']) .
                  " " . escapeshellarg($gateway['gateway']), true);
            }
        } else {
            continue;
        }

        $monitor_ips[] = $gateway['monitor'];
        $apingercfg = "target \"{$gateway['monitor']}\" {\n";
        $apingercfg .= "  description \"{$name}\"\n";
        $apingercfg .= "  srcip \"{$gwifip}\"\n";

        ## How often the probe should be sent
        if (!empty($gateway['interval']) &&  is_numeric($gateway['interval'])) {
            $interval = intval($gateway['interval']);  # Restrict to Integer
            if ($interval <  1) {
                $interval =  1;  # Minimum
            }
            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
            if ($avg_delay_samples <  1) {
                $avg_delay_samples =  1;  # Minimum
            }
            if ($avg_delay_samples != $apinger_default['avg_delay_samples']) {
                # If not default value
                $apingercfg .= "  avg_delay_samples " . $avg_delay_samples . "\n";
            }
        }

        ## How many probes should be used to compute average loss
        if (!empty($gateway['avg_loss_samples']) && is_numeric($gateway['avg_loss_samples'])) {
            $avg_loss_samples = intval($gateway['avg_loss_samples']);  # Restrict to Integer
            if ($avg_loss_samples < 1) {
                $avg_loss_samples = 1;  # Minimum
            }
            if ($avg_loss_samples != $apinger_default['avg_loss_samples']) {
                # If not default value
                $apingercfg .= "  avg_loss_samples " . $avg_loss_samples . "\n";
            }
        }

        ## The delay (in samples) after which loss is computed
        ## without this delays larger than interval would be treated as loss
        if (!empty($gateway['avg_loss_delay_samples']) && is_numeric($gateway['avg_loss_delay_samples'])) {
            $avg_loss_delay_samples = intval($gateway['avg_loss_delay_samples']);  # Restrict to Integer
            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
                $apingercfg .= "  avg_loss_delay_samples " . $avg_loss_delay_samples . "\n";
            }
        }

        $alarms = "";
        $alarmscfg = "";
        $override = false;
        if (!empty($gateway['losslow'])) {
            $alarmscfg .= "alarm loss \"{$name}loss\" {\n";
            $alarmscfg .= "\tpercent_low {$gateway['losslow']}\n";
            $alarmscfg .= "\tpercent_high {$gateway['losshigh']}\n";
            $alarmscfg .= "}\n";
            $alarms .= "\"{$name}loss\"";
            $override = true;
        } else {
            if ($override == true) {
                $alarms .= ",";
            }
            $alarms .= "\"loss\"";
            $override = true;
        }
        if (!empty($gateway['latencylow'])) {
            $alarmscfg .= "alarm delay \"{$name}delay\" {\n";
            $alarmscfg .= "\tdelay_low {$gateway['latencylow']}ms\n";
            $alarmscfg .= "\tdelay_high {$gateway['latencyhigh']}ms\n";
            $alarmscfg .= "}\n";
            if ($override == true) {
                $alarms .= ",";
            }
            $alarms .= "\"{$name}delay\"";
            $override = true;
        } else {
            if ($override == true) {
                $alarms .= ",";
            }
            $alarms .= "\"delay\"";
            $override = true;
        }
        if (!empty($gateway['down'])) {
            $alarmscfg .= "alarm down \"{$name}down\" {\n";
            $alarmscfg .= "\ttime {$gateway['down']}s\n";
            $alarmscfg .= "}\n";
            if ($override == true) {
                $alarms .= ",";
            }
            $alarms .= "\"{$name}down\"";
            $override = true;
        } else {
            if ($override == true) {
                $alarms .= ",";
            }
            $alarms .= "\"down\"";
            $override = true;
        }
        if ($override == true) {
            $apingercfg .= "\talarms override {$alarms};\n";
        }

        if (isset($gateway['force_down'])) {
            $apingercfg .= "\tforce_down on\n";
        }

        $apingercfg .= "  rrd file \"/var/db/rrd/{$gateway['name']}-quality.rrd\"\n";
        $apingercfg .= "}\n";
        $apingercfg .= "\n";

        $apingerconfig .= $alarmscfg;
        $apingerconfig .= $apingercfg;

        # Create gateway quality RRD with settings more suitable for OPNsense graph set,
        # since apinger uses default step (300; 5 minutes) and other settings that don't
        # match the OPNsense gateway quality graph set.
        create_gateway_quality_rrd("/var/db/rrd/{$gateway['name']}-quality.rrd");
    }

    $gateways_arr = return_gateways_array();
    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;
Ad Schellevis's avatar
Ad Schellevis committed
404 405 406
}

/* return the status of the apinger targets as a array */
407 408
function return_gateways_status($byname = false)
{
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
    global $config;

    $apingerstatus = array();

    /* Always get the latest status from apinger */
    killbypid('/var/run/apinger.pid', 'USR1');
    if (file_exists('/var/run/apinger.status')) {
        $apingerstatus = file('/var/run/apinger.status');
    }

    $status = array();
    foreach ($apingerstatus as $line) {
        $info = explode('|', $line);
        if ($byname == false) {
            $target = $info[0];
        } else {
            $target = $info[2];
        }

        $status[$target] = array();
        $status[$target]['monitorip'] = $info[0];
        $status[$target]['srcip'] = $info[1];
        $status[$target]['name'] = $info[2];
        $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]['loss'] = empty($info[7]) ? "0.0%" : round($info[7], 1) . "%";
        $status[$target]['status'] = trim($info[8]);
    }

    /* tack on any gateways that have monitoring disabled
     * or are down, which could cause gateway groups to fail */
    $gateways_arr = return_gateways_array();
    foreach($gateways_arr as $gwitem) {
        if (!isset($gwitem['monitor_disable'])) {
            continue;
        }
        if (!isset($gwitem['monitorip']) || !is_ipaddr($gwitem['monitorip'])) {
            $realif = $gwitem['interface'];
            $tgtip = get_interface_gateway($realif);
            if (!is_ipaddr($tgtip)) {
                $tgtip = "none";
            }
            $srcip = find_interface_ip($realif);
        } else {
            $tgtip = $gwitem['monitorip'];
            $srcip = find_interface_ip($realif);
        }
        if ($byname == true) {
            $target = $gwitem['name'];
        } else {
            $target = $tgtip;
        }

        /* failsafe for down interfaces */
        if ($target == "none") {
            $target = $gwitem['name'];
            $status[$target]['name'] = $gwitem['name'];
            $status[$target]['lastcheck'] = date('r');
            $status[$target]['delay'] = "0.0ms";
            $status[$target]['loss'] = "100.0%";
            $status[$target]['status'] = "down";
        } else {
            $status[$target]['monitorip'] = $tgtip;
            $status[$target]['srcip'] = $srcip;
            $status[$target]['name'] = $gwitem['name'];
            $status[$target]['lastcheck'] = date('r');
            $status[$target]['delay'] = "0.0ms";
            $status[$target]['loss'] = "0.0%";
            $status[$target]['status'] = "none";
        }
    }
    return($status);
Ad Schellevis's avatar
Ad Schellevis committed
481 482 483
}

/* Return all configured gateways on the system */
Franco Fichtner's avatar
Franco Fichtner committed
484 485
function return_gateways_array($disabled = false, $localhost = false, $inactive = false)
{
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
    global $config;

    $gateways_arr = array();

    $found_defaultv4 = 0;
    $found_defaultv6 = 0;

    $interfaces = legacy_interface_listget();
    $interfaces_v4 = array();
    $interfaces_v6 = array();

    $i = -1;
    /* Process/add all the configured gateways. */
    if (isset($config['gateways']['gateway_item'])) {
        foreach ($config['gateways']['gateway_item'] as $gateway) {
            /* Increment it here to do not skip items */
            $i++;

            if (empty($config['interfaces'][$gateway['interface']])) {
              if ($inactive === false) {
                  continue;
              } else {
                  $gateway['inactive'] = true;
              }
            }
            $wancfg = $config['interfaces'][$gateway['interface']];

            /* skip disabled interfaces */
            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") {
                    /* we know which interfaces is dynamic, this should be made a function */
                    $gateway['gateway'] = get_interface_gateway($gateway['interface']);
                    /* no IP address found, set to dynamic */
                    if (!is_ipaddrv4($gateway['gateway'])) {
                        $gateway['gateway'] = "dynamic";
                    }
                    $gateway['dynamic'] = true;
                } elseif ($gateway['ipprotocol'] == "inet6") {
                    /* if the gateway is dynamic and we can find the IPv6, Great! */
                    /* we know which interfaces is dynamic, this should be made a function, and for v6 too */
                    $gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
                    /* no IPv6 address found, set to dynamic */
                    if (!is_ipaddrv6($gateway['gateway'])) {
                        $gateway['gateway'] = "dynamic";
                    }
                    $gateway['dynamic'] = true;
                }
            } else {
                /* getting this detection right is hard at this point because we still don't
                 * store the address family in the gateway item */
                if (is_ipaddrv4($gateway['gateway'])) {
                    $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'];
            }

            $gateway['friendlyiface'] = $gateway['interface'];

            /* special treatment for tunnel interfaces */
            if ($gateway['ipprotocol'] == "inet6") {
                $gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
                $interfaces_v6[$gateway['friendlyiface']] = $gateway['friendlyiface'];
            } else {
                $gateway['interface'] = get_real_interface($gateway['interface'], "all", false, false);
                $interfaces_v4[$gateway['friendlyiface']] = $gateway['friendlyiface'];
            }

            /* entry has a default flag, use it */
            if (isset($gateway['defaultgw'])) {
                if ($gateway['ipprotocol'] == "inet") {
                    $gateway['defaultgw'] = true;
                    $found_defaultv4 = 1;
                } else if ($gateway['ipprotocol'] == "inet6") {
                    $gateway['defaultgw'] = true;
                    $found_defaultv6 = 1;
                }
            }
            /* include the gateway index as the attribute */
            $gateway['attribute'] = $i;

            $gateways_arr[$gateway['name']] = $gateway;
        }
    }
    unset($gateway);

    /* Loop through all interfaces with a gateway and add it to a array */
    if ($disabled == false) {
        $iflist = get_configured_interface_with_descr();
    } else {
        $iflist = get_configured_interface_with_descr(false, true);
    }

    /* Process/add dynamic v4 gateways. */
    foreach($iflist as $ifname => $friendly ) {
        if (! interface_has_gateway($ifname)) {
            continue;
        }

        if (empty($config['interfaces'][$ifname])) {
            continue;
        }

        $ifcfg = &$config['interfaces'][$ifname];
        if (!isset($ifcfg['enable'])) {
            continue;
        }

        if (!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) {
            continue;
        }

        if (isset($interfaces_v4[$ifname])) {
            continue;
        }

        $ctype = "";
        switch($ifcfg['ipaddr']) {
            case "dhcp":
            case "pppoe":
            case "pptp":
            case "ppp":
                $ctype = strtoupper($ifcfg['ipaddr']);
                break;
            default:
                if (substr($ifcfg['if'], 0, 4) ==  "ovpn") {
                    // if current iface is an ovpn server endpoint then skip it
                    if (substr($ifcfg['if'], 4, 1) == 's') {
                        continue 2;
                    }

                    $ctype = "VPNv4";
                }
                break;
        }
        $ctype = "_". strtoupper($ctype);

        $gateway = array();
        $gateway['dynamic'] = false;
        $gateway['ipprotocol'] = "inet";
        $gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
        $gateway['interface'] = get_real_interface($ifname);
        $gateway['friendlyiface'] = $ifname;
        $gateway['name'] = "{$friendly}{$ctype}";
        $gateway['attribute'] = "system";

        if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
            $gateway['defaultgw'] = true;
            $gateway['dynamic'] = true;
            $found_defaultv4 = 1;
        }
        /* Loopback dummy for dynamic interfaces without a IP */
        if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) {
            $gateway['gateway'] = "dynamic";
        }

        /* automatically skip known static and dynamic gateways we have a array entry for */
        foreach($gateways_arr as $gateway_item) {
            if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
              ($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) {
                continue 2;
            }
        }

        if (is_ipaddrv4($gateway['gateway'])) {
            $gateway['monitor'] = $gateway['gateway'];
        }

        $gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
        $gateway['monitor_disable'] = true;
        $gateways_arr[$gateway['name']] = $gateway;
    }
    unset($gateway);

    /* 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'])) {
            break;
        }

        if (!interface_has_gatewayv6($ifname)) {
            continue;
        }

        if (empty($config['interfaces'][$ifname])) {
            continue;
        }

        $ifcfg = &$config['interfaces'][$ifname];
        if (!isset($ifcfg['enable'])) {
            continue;
        }

        if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
            continue;
        }

        if (isset($interfaces_v6[$ifname])) {
            continue;
        }

        $ctype = "";
        switch($ifcfg['ipaddrv6']) {
            case "slaac":
            case "dhcp6":
            case "6to4":
            case "6rd":
                $ctype = strtoupper($ifcfg['ipaddrv6']);
                break;
            default:
                $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 (substr($ifcfg['if'], 4, 1) == 's') {
                        continue 2;
                    }

                    $ctype = "VPNv6";
                } elseif ($tunnelif == "gif" || $tunnelif == "gre") {
                    $ctype = "TUNNELv6";
                }
                break;
        }
        $ctype = "_". strtoupper($ctype);

        $gateway = array();
        $gateway['dynamic'] = false;
        $gateway['ipprotocol'] = "inet6";
        $gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
        $gateway['interface'] = get_real_interface($ifname, "inet6");
        switch($ifcfg['ipaddrv6']) {
            case "6rd":
            case "6to4":
                $gateway['dynamic'] = "default";
                break;
        }
        $gateway['friendlyiface'] = $ifname;
        $gateway['name'] = "{$friendly}{$ctype}";
        $gateway['attribute'] = "system";

        if (($gateway['dynamic'] === "default")  && ($found_defaultv6 == 0)) {
            $gateway['defaultgw'] = true;
            $gateway['dynamic'] = true;
            $found_defaultv6 = 1;
        }

        /* Loopback dummy for dynamic interfaces without a IP */
        if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true) {
            $gateway['gateway'] = "dynamic";
        }

        /* automatically skip known static and dynamic gateways we have a array entry for */
        foreach($gateways_arr as $gateway_item) {
            if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
              ($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) {
                continue 2;
            }
        }

        if (is_ipaddrv6($gateway['gateway'])) {
            $gateway['monitor'] = $gateway['gateway'];
        }

        $gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
        $gateway['monitor_disable'] = true;
        $gateways_arr[$gateway['name']] = $gateway;
    }
    unset($gateway);

    /* FIXME: Should this be enabled.
     * Some interface like wan might be default but have no info recorded
     * the config. */
    /* this is a fallback if all else fails and we want to get packets out @smos */
    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"))) {
                if (file_exists("/tmp/{$gateway['interface']}_defaultgw")) {
                    $gateway['defaultgw'] = true;
                    $found_defaultv4 = 1;
                }
            }
            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);
Ad Schellevis's avatar
Ad Schellevis committed
804 805
}

Franco Fichtner's avatar
Franco Fichtner committed
806 807
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr)
{
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    global $config;
    /*
     * 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.
     */
    $upgw = "";
    $dfltgwdown = false;
    $dfltgwfound = false;
    foreach ($gateways_arr as $gwname => $gwsttng) {
        if (($gwsttng['ipprotocol'] == $ipprotocol) && isset($gwsttng['defaultgw'])) {
            $dfltgwfound = true;
            $dfltgwname = $gwname;
            if (!isset($gwsttng['monitor_disable']) && stristr($gateways_status[$gwname]['status'], "down")) {
                $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 */
        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") {
            $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 (is_ipaddrv6($gateways_arr[$upgw]['gateway'])) {
                $inetfamily = "-inet6";
            } else {
                $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 {
            $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']}");
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
865 866 867 868 869 870
}

/*
 * Return an array with all gateway groups with name as key
 * All gateway groups will be processed before returning the array.
 */
Franco Fichtner's avatar
Franco Fichtner committed
871 872
function return_gateway_groups_array()
{
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
    global $config;

    /* fetch the current gateways status */
    $gateways_status = return_gateways_status(true);
    $gateways_arr = return_gateways_array();
    $gateway_groups_array = array();

    if (isset($config['system']['gw_switch_default'])) {
        fixup_default_gateway("inet", $gateways_status, $gateways_arr);
        fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
    }
    if (isset($config['gateways']['gateway_group'])) {
        $carplist = get_configured_carp_interface_list();
        foreach ($config['gateways']['gateway_group'] as $group) {
            /* create array with group gateways members separated by tier */
            $tiers = array();
            $backupplan = array();
            $gwvip_arr = array();
            foreach ($group['item'] as $item) {
                list($gwname, $tier, $vipname) = explode("|", $item);

                if (is_ipaddr($carplist[$vipname])) {
                    if (!is_array($gwvip_arr[$group['name']])) {
                        $gwvip_arr[$group['name']] = array();
                    }
                    $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])) {
                    $backupplan[$tier] = array();
                }
                $backupplan[$tier][] = $gwname;

                /* check if the gateway is available before adding it to the array */
                if (is_array($gateways_status[$gwname])) {
                    $status = $gateways_status[$gwname];
                    $gwdown = false;
                    if (stristr($status['status'], 'down')) {
                        $msg = sprintf(gettext('MONITOR: %s is down, removing from routing group %s'), $gwname, $group['name']);
                        $gwdown = true;
                    } elseif (stristr($status['status'], 'loss') && strstr($group['trigger'], 'loss')) {
                        /* packet loss */
                        $msg = sprintf(gettext('MONITOR: %s has packet loss, removing from routing group %s'), $gwname, $group['name']);
                        $gwdown = true;
                    } elseif (stristr($status['status'], 'delay') && strstr($group['trigger'] , 'latency')) {
                        /* high latency */
                        $msg = sprintf(gettext('MONITOR: %s has high latency, removing from routing group %s'), $gwname, $group['name']);
                        $gwdown = true;
                    }
                    if ($gwdown == true) {
                        log_error($msg);
                        notify_via_growl($msg);
                        notify_via_smtp($msg);
                    } else {
                        /* Online add member */
                        if (!is_array($tiers[$tier])) {
                            $tiers[$tier] = array();
                        }
                        $tiers[$tier][] = $gwname;
                    }
                } elseif (isset($gateways_arr[$gwname]['monitor_disable'])) {
                    $tiers[$tier][] = $gwname;
                }
            }
            $tiers_count = count($tiers);
            if ($tiers_count == 0) {
                /* Oh dear, we have no members! Engage Plan B */
                if (!file_exists('/var/run/booting')) {
                    $msg = sprintf(gettext('Gateways status could not be determined, considering all as up/active. (Group: %s)'), $group['name']);
                    log_error($msg);
                    notify_via_growl($msg);
                    //notify_via_smtp($msg);
                }
                $tiers = $backupplan;
            }
            /* sort the tiers array by the tier key */
            ksort($tiers);

            /* we do not really foreach the tiers as we stop after the first tier */
            foreach ($tiers as $tieridx => $tier) {
                /* process all gateways in this tier */
                foreach ($tier as $member) {
                    /* determine interface gateway */
                    if (isset($gateways_arr[$member])) {
                        $gateway = $gateways_arr[$member];
                        $int = $gateway['interface'];
                        $gatewayip = "";
                        if (is_ipaddr($gateway['gateway'])) {
                            $gatewayip = $gateway['gateway'];
                        } elseif (!empty($int)) {
                            $gatewayip = get_interface_gateway($gateway['friendlyiface']);
                        }

                        if (!empty($int)) {
                            $gateway_groups_array[$group['name']]['ipprotocol'] = $gateway['ipprotocol'];
                            if (is_ipaddr($gatewayip)) {
                                $groupmember = array();
                                $groupmember['int']  = $int;
                                $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];
                                }
                                $gateway_groups_array[$group['name']][] = $groupmember;
                            }
                        }
                    }
                }
                /* we should have the 1st available tier now, exit stage left */
                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}!");
                }
            }
        }
    }

    return ($gateway_groups_array);
Ad Schellevis's avatar
Ad Schellevis committed
993 994
}

995 996 997
function lookup_gateway_ip_by_name($name)
{
    $gateways_arr = return_gateways_array(true, true);
998

999 1000 1001 1002 1003
    foreach ($gateways_arr as $gname => $gw) {
        if ($gw['name'] === $name || $gname === $name) {
            return $gw['gateway'];
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
1004

1005
    return false;
Ad Schellevis's avatar
Ad Schellevis committed
1006 1007
}

1008 1009 1010 1011 1012 1013 1014
function lookup_gateway_interface_by_name($name)
{
    $gateways_arr = return_gateways_array(false, true);
    if (!empty($gateways_arr[$name])) {
        $interfacegw = $gateways_arr[$name]['friendlyiface'];
        return ($interfacegw);
    }
Ad Schellevis's avatar
Ad Schellevis committed
1015

1016
    return (false);
Ad Schellevis's avatar
Ad Schellevis committed
1017 1018
}

Franco Fichtner's avatar
Franco Fichtner committed
1019 1020
function get_interface_gateway($interface, &$dynamic = false)
{
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    global $config;

    $gw = NULL;

    if (isset($config['interfaces'][$interface])) {
        $gwcfg = $config['interfaces'][$interface];
        if (!empty($gwcfg['gateway']) && isset($config['gateways']['gateway_item'])) {
            foreach($config['gateways']['gateway_item'] as $gateway) {
                if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
                    $gw = $gateway['gateway'];
                    break;
                }
            }
        }

        // for dynamic interfaces we handle them through the $interface_router file.
        if (!is_ipaddrv4($gw) && !is_ipaddrv4($gwcfg['ipaddr'])) {
            $realif = get_real_interface($interface);
            if (file_exists("/tmp/{$realif}_router")) {
                $gw = trim(file_get_contents("/tmp/{$realif}_router"), " \n");
                $dynamic = true;
            }
            if (file_exists("/tmp/{$realif}_defaultgw")) {
                $dynamic = "default";
            }
        }
    }

    /* return gateway */
    return ($gw);
Ad Schellevis's avatar
Ad Schellevis committed
1051 1052
}

Franco Fichtner's avatar
Franco Fichtner committed
1053 1054
function get_interface_gateway_v6($interface, &$dynamic = false)
{
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
    global $config;

    $gw = NULL;
    $gwcfg = $config['interfaces'][$interface];
    if (!empty($gwcfg['gatewayv6']) && isset($config['gateways']['gateway_item'])) {
        foreach($config['gateways']['gateway_item'] as $gateway) {
            if (($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
                $gw = $gateway['gateway'];
                break;
            }
        }
    }

    // for dynamic interfaces we handle them through the $interface_router file.
    if (!is_ipaddrv6($gw) && ( !isset($gwcfg['ipaddrv6']) || !is_ipaddrv6($gwcfg['ipaddrv6']))) {
          $realif = get_real_interface($interface);
          if (file_exists("/tmp/{$realif}_routerv6")) {
              $gw = trim(file_get_contents("/tmp/{$realif}_routerv6"), " \n");
              $dynamic = true;
          }
          if (file_exists("/tmp/{$realif}_defaultgwv6")) {
              $dynamic = "default";
          }
    }
    /* return gateway */
    return ($gw);
Ad Schellevis's avatar
Ad Schellevis committed
1081 1082 1083 1084
}

/* Check a IP address against a gateway IP or name
 * to verify it's address family */
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
function validate_address_family($ipaddr, $gwname)
{
    $v4ip = false;
    $v6ip = false;
    $v4gw = false;
    $v6gw = false;

    if (is_ipaddrv4($ipaddr)) {
        $v4ip = true;
    }
    if (is_ipaddrv6($ipaddr)) {
        $v6ip = true;
    }
    if (is_ipaddrv4($gwname)) {
        $v4gw = true;
    }
    if (is_ipaddrv6($gwname)) {
        $v6gw = true;
    }

    if ($v4ip && $v4gw) {
        return true;
    }
    if ($v6ip && $v6gw) {
        return true;
    }

    /* still no match, carry on, lookup gateways */
    if (is_ipaddrv4(lookup_gateway_ip_by_name($gwname))) {
        $v4gw = true;
    }
    if (is_ipaddrv6(lookup_gateway_ip_by_name($gwname))) {
        $v6gw = true;
    }

    $gw_array = return_gateways_array();
    if (is_array($gw_array[$gwname])) {
        switch($gw_array[$gwname]['ipprotocol']) {
            case "inet":
                $v4gw = true;
                break;
            case "inet6":
                $v6gw = true;
                break;
        }
    }

    if ($v4ip && $v4gw) {
        return true;
    }
    if ($v6ip && $v6gw) {
        return true;
    }

    return false;
Ad Schellevis's avatar
Ad Schellevis committed
1140
}