<commit_before><?php

/*
 * Copyright (C) 2014-2020 Deciso B.V.
 * Copyright (C) 2010 <NAME> <<EMAIL>>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 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.
 *
 * 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.
 */

require_once("guiconfig.inc");
require_once("interfaces.inc");
require_once("plugins.inc.d/dpinger.inc");

$gateways = new \OPNsense\Routing\Gateways(legacy_interfaces_details());
$a_gateways = array_values($gateways->gatewaysIndexedByName(true, false, true));
$dpinger_default = dpinger_defaults();

// form processing
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $pconfig = $_POST;
    if (isset($pconfig['id']) && isset($a_gateways[$pconfig['id']])) {
        $id = $pconfig['id'];
    }
    $input_errors = array();

    /* input validation */
    $reqdfields = explode(" ", "name interface");
    $reqdfieldsn = array(gettext("Name"), gettext("Interface"));

    do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors);

    if (!isset($pconfig['name'])) {
        $input_errors[] = gettext("A valid gateway name must be specified.");
    }

    $valid = is_validaliasname($pconfig['name']);
    if ($valid === false) {
        $input_errors[] = sprintf(gettext('The name must be less than 32 characters long and may only consist of the following characters: %s'), 'a-z, A-Z, 0-9, _');
    } elseif ($valid === null) {
        $input_errors[] = sprintf(gettext('The name cannot be the internally reserved keyword "%s".'), $pconfig['name']);
    }

    /* skip system gateways which have been automatically added */
    if (!empty($pconfig['gateway']) && !is_ipaddr($pconfig['gateway']) &&
        $pconfig['attribute'] !== "system" && $pconfig['gateway'] != "dynamic") {
        $input_errors[] = gettext("A valid gateway IP address must be specified.");
    }

    $vips = [];

    foreach (config_read_array('virtualip', 'vip') as $vip) {
        if ($pconfig['interface'] == $vip['interface']) {
            $vips[] = $vip;
        }
    }

    if (!empty($pconfig['gateway']) && is_ipaddr($pconfig['gateway'])) {
        if (is_ipaddrv4($pconfig['gateway'])) {
            list ($parent_ip, $parent_sn) = explode('/', find_interface_network(get_real_interface($pconfig['interface']), false));
            $parent_ip = empty($pconfig['ajaxip']) ? $parent_ip : $pconfig['ajaxip'];
            $parent_sn = empty($pconfig['ajaxnet']) ? $parent_sn : $pconfig['ajaxnet'];
            if (empty($parent_ip) || empty($parent_sn)) {
                $input_errors[] = gettext("Cannot add IPv4 Gateway Address because no IPv4 address could be found on the interface.");
            } else {
                $subnets = array(gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn);
                foreach ($vips as $vip) {
                    if (!is_ipaddrv4($vip['subnet'])) {
                        continue;
                    }
                    $subnets[] = gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
                }

                $found = false;

                foreach ($subnets as $subnet) {
                    if (ip_in_subnet($pconfig['gateway'], $subnet)) {
                        $found = true;
                        break;
                    }
                }

                if (!$found && !isset($pconfig['fargw'])) {
                    $input_errors[] = sprintf(gettext('The gateway address "%s" does not lie within one of the chosen interface\'s IPv4 subnets.'), $pconfig['gateway']);
                }
            }
        } elseif (is_ipaddrv6($pconfig['gateway'])) {
            /* do not do a subnet match on a link local address, it's valid */
            if (!is_linklocal($pconfig['gateway'])) {
                list ($parent_ip, $parent_sn) = explode('/', find_interface_networkv6(get_real_interface($pconfig['interface'], 'inet6'), false));
                $parent_ip = empty($pconfig['ajaxip']) ? $parent_ip : $pconfig['ajaxip'];
                $parent_sn = empty($pconfig['ajaxnet']) ? $parent_sn : $pconfig['ajaxnet'];
                if (empty($parent_ip) || empty($parent_sn)) {
                    $input_errors[] = gettext("Cannot add IPv6 Gateway Address because no IPv6 address could be found on the interface.");
                } else {
                    $subnets = array(gen_subnetv6($parent_ip, $parent_sn) . "/" . $parent_sn);
                    foreach ($vips as $vip) {
                        if (!is_ipaddrv6($vip['subnet'])) {
                            continue;
                        }
                        $subnets[] = gen_subnetv6($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
                    }

                    $found = false;

                    foreach ($subnets as $subnet) {
                        if (ip_in_subnet($pconfig['gateway'], $subnet)) {
                            $found = true;
                            break;
                        }
                    }

                    if (!$found) {
                        $input_errors[] = sprintf(gettext('The gateway address "%s" does not lie within one of the chosen interface\'s IPv6 subnets.'), $pconfig['gateway']);
                    }
                }
            }
        }

        if (!empty($config['interfaces'][$pconfig['interface']]['ipaddr'])) {
            if (is_ipaddr($config['interfaces'][$pconfig['interface']]['ipaddr']) && (empty($pconfig['gateway']) || $pconfig['gateway'] == "dynamic")) {
                $input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv4 configuration.");
            }
        }
        if (!empty($config['interfaces'][$pconfig['interface']]['ipaddrv6'])) {
            if (is_ipaddr($config['interfaces'][$pconfig['interface']]['ipaddrv6']) && (empty($pconfig['gateway']) || $pconfig['gateway'] == "dynamic")) {
                $input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv6 configuration.");
            }
        }
    }
    if (($pconfig['monitor'] != '') && !is_ipaddr($pconfig['monitor']) && $pconfig['monitor'] != 'dynamic') {
        $input_errors[] = gettext("A valid monitor IP address must be specified.");
    }
    /* only allow correct IPv4 and IPv6 gateway addresses */<commit_msg>gateways: far gateway IPv6 for https://github.com/opnsense/core/issues/4436<commit_after><?php

/*
 * Copyright (C) 2014-2020 Deciso B.V.
 * Copyright (C) 2010 <NAME> <<EMAIL>>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 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.
 *
 * 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.
 */

require_once("guiconfig.inc");
require_once("interfaces.inc");
require_once("plugins.inc.d/dpinger.inc");

$gateways = new \OPNsense\Routing\Gateways(legacy_interfaces_details());
$a_gateways = array_values($gateways->gatewaysIndexedByName(true, false, true));
$dpinger_default = dpinger_defaults();

// form processing
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $pconfig = $_POST;
    if (isset($pconfig['id']) && isset($a_gateways[$pconfig['id']])) {
        $id = $pconfig['id'];
    }
    $input_errors = array();

    /* input validation */
    $reqdfields = explode(" ", "name interface");
    $reqdfieldsn = array(gettext("Name"), gettext("Interface"));

    do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors);

    if (!isset($pconfig['name'])) {
        $input_errors[] = gettext("A valid gateway name must be specified.");
    }

    $valid = is_validaliasname($pconfig['name']);
    if ($valid === false) {
        $input_errors[] = sprintf(gettext('The name must be less than 32 characters long and may only consist of the following characters: %s'), 'a-z, A-Z, 0-9, _');
    } elseif ($valid === null) {
        $input_errors[] = sprintf(gettext('The name cannot be the internally reserved keyword "%s".'), $pconfig['name']);
    }

    /* skip system gateways which have been automatically added */
    if (!empty($pconfig['gateway']) && !is_ipaddr($pconfig['gateway']) &&
        $pconfig['attribute'] !== "system" && $pconfig['gateway'] != "dynamic") {
        $input_errors[] = gettext("A valid gateway IP address must be specified.");
    }

    $vips = [];

    foreach (config_read_array('virtualip', 'vip') as $vip) {
        if ($pconfig['interface'] == $vip['interface']) {
            $vips[] = $vip;
        }
    }

    if (!empty($pconfig['gateway']) && is_ipaddr($pconfig['gateway'])) {
        if (is_ipaddrv4($pconfig['gateway'])) {
            list ($parent_ip, $parent_sn) = explode('/', find_interface_network(get_real_interface($pconfig['interface']), false));
            $parent_ip = empty($pconfig['ajaxip']) ? $parent_ip : $pconfig['ajaxip'];
            $parent_sn = empty($pconfig['ajaxnet']) ? $parent_sn : $pconfig['ajaxnet'];
            if (empty($parent_ip) || empty($parent_sn)) {
                $input_errors[] = gettext("Cannot add IPv4 Gateway Address because no IPv4 address could be found on the interface.");
            } else {
                $subnets = array(gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn);
                foreach ($vips as $vip) {
                    if (!is_ipaddrv4($vip['subnet'])) {
                        continue;
                    }
                    $subnets[] = gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
                }

                $found = false;

                foreach ($subnets as $subnet) {
                    if (ip_in_subnet($pconfig['gateway'], $subnet)) {
                        $found = true;
                        break;
                    }
                }

                if (!$found && !isset($pconfig['fargw'])) {
                    $input_errors[] = sprintf(gettext('The gateway address "%s" does not lie within one of the chosen interface\'s IPv4 subnets.'), $pconfig['gateway']);
                }
            }
        } elseif (is_ipaddrv6($pconfig['gateway'])) {
            /* do not do a subnet match on a link local address, it's valid */
            if (!is_linklocal($pconfig['gateway'])) {
                list ($parent_ip, $parent_sn) = explode('/', find_interface_networkv6(get_real_interface($pconfig['interface'], 'inet6'), false));
                $parent_ip = empty($pconfig['ajaxip']) ? $parent_ip : $pconfig['ajaxip'];
                $parent_sn = empty($pconfig['ajaxnet']) ? $parent_sn : $pconfig['ajaxnet'];
                if (empty($parent_ip) || empty($parent_sn)) {
                    $input_errors[] = gettext("Cannot add IPv6 Gateway Address because no IPv6 address could be found on the interface.");
                } else {
                    $subnets = array(gen_subnetv6($parent_ip, $parent_sn) . "/" . $parent_sn);
                    foreach ($vips as $vip) {
                        if (!is_ipaddrv6($vip['subnet'])) {
                            continue;
                        }
                        $subnets[] = gen_subnetv6($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
                    }

                    $found = false;

                    foreach ($subnets as $subnet) {
                        if (ip_in_subnet($pconfig['gateway'], $subnet)) {
                            $found = true;
                            break;
                        }
                    }

                    if (!$found && !isset($pconfig['fargw'])) {
                        $input_errors[] = sprintf(gettext('The gateway address "%s" does not lie within one of the chosen interface\'s IPv6 subnets.'), $pconfig['gateway']);
                    }
                }
            }
        }

        if (!empty($config['interfaces'][$pconfig['interface']]['ipaddr'])) {
            if (is_ipaddr($config['interfaces'][$pconfig['interface']]['ipaddr']) && (empty($pconfig['gateway']) || $pconfig['gateway'] == "dynamic")) {
                $input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv4 configuration.");
            }
        }
        if (!empty($config['interfaces'][$pconfig['interface']]['ipaddrv6'])) {
            if (is_ipaddr($config['interfaces'][$pconfig['interface']]['ipaddrv6']) && (empty($pconfig['gateway']) || $pconfig['gateway'] == "dynamic")) {
                $input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv6 configuration.");
            }
        }
    }
    if (($pconfig['monitor'] != '') && !is_ipaddr($pconfig['monitor']) && $pconfig['monitor'] != 'dynamic') {
        $input_errors[] = gettext("A valid monitor IP address must be specified.");
    }
    /* only allow correct IPv4 and IPv6 gateway addresses */
{'fraction_non_alphanumeric': 0.14725684954336377, 'fraction_numerical': 0.0053996400239984, 'mean_word_length': 2.5174677608440796, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2209707', 'n_tokens_mistral': 4372, 'n_tokens_neox': 4093, 'n_words': 1138}