system_routes_edit.php 13.7 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    Copyright (C) 2014-2015 Deciso B.V.
    Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
    Copyright (C) 2010 Scott Ullrich
    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.
Ad Schellevis's avatar
Ad Schellevis committed
29 30 31 32
*/

require_once("guiconfig.inc");
require_once("filter.inc");
33
require_once("interfaces.inc");
34
require_once("pfsense-utils.inc");
Ad Schellevis's avatar
Ad Schellevis committed
35 36 37

$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/system_routes.php');

38
if (!isset($config['staticroutes']) || !is_array($config['staticroutes'])) {
39
    $config['staticroutes'] = array();
40 41
}

42
if (!isset($config['staticroutes']['route']) || !is_array($config['staticroutes']['route'])) {
43
    $config['staticroutes']['route'] = array();
44
}
Ad Schellevis's avatar
Ad Schellevis committed
45 46 47 48

$a_routes = &$config['staticroutes']['route'];
$a_gateways = return_gateways_array(true, true);

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    if (isset($_GET['id']) && isset($a_routes[$_GET['id']])) {
        $id = $_GET['id'];
        $configId = $id;
    } elseif (isset($_GET['dup']) && isset($a_routes[$_GET['dup']])) {
        $configId = $_GET['dup'];
    }
    $pconfig = array();

    if (isset($configId)) {
        list($pconfig['network'],$pconfig['network_subnet']) =
            explode('/', $a_routes[$configId]['network']);
        $pconfig['gateway'] = $a_routes[$configId]['gateway'];
        $pconfig['descr'] = $a_routes[$configId]['descr'];
        $pconfig['disabled'] = isset($a_routes[$configId]['disabled']);
    } else {
        $pconfig['network'] = null;
        $pconfig['network_subnet'] = null;
        $pconfig['gateway'] = null;
        $pconfig['disabled'] = false;
    }
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['id']) && isset($a_routes[$_POST['id']])) {
        $id = $_POST['id'];
    }
74 75
    global $aliastable;

76
    $input_errors = array();
77 78 79 80 81 82 83 84 85 86 87
    $pconfig = $_POST;

    /* input validation */
    $reqdfields = explode(" ", "network network_subnet gateway");
    $reqdfieldsn = explode(
        ",",
        gettext("Destination network") . "," .
        gettext("Destination network bit count") . "," .
        gettext("Gateway")
    );

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

90
    if (($pconfig['network'] && !is_ipaddr($pconfig['network']) && !is_alias($pconfig['network']))) {
91 92
        $input_errors[] = gettext("A valid IPv4 or IPv6 destination network must be specified.");
    }
93
    if (($_POST['network_subnet'] && !is_numeric($pconfig['network_subnet']))) {
94 95
        $input_errors[] = gettext("A valid destination network bit count must be specified.");
    }
96 97
    if (($_POST['gateway']) && is_ipaddr($pconfig['network'])) {
        if (!isset($a_gateways[$pconfig['gateway']])) {
98 99
            $input_errors[] = gettext("A valid gateway must be specified.");
        }
100 101
        if (!validate_address_family($pconfig['network'], lookup_gateway_ip_by_name($pconfig['gateway']))) {
            $input_errors[] = gettext("The gateway '{$a_gateways[$pconfig['gateway']]['gateway']}' is a different Address Family as network '{$pconfig['network']}'.");
102 103 104 105 106 107
        }
    }

    /* check for overlaps */
    $current_targets = get_staticroutes(true);
    $new_targets = array();
108 109
    if (is_ipaddrv6($pconfig['network'])) {
        $osn = gen_subnetv6($pconfig['network'], $pconfig['network_subnet']) . "/" . $pconfig['network_subnet'];
110 111
        $new_targets[] = $osn;
    }
112 113
    if (is_ipaddrv4($pconfig['network'])) {
        if ($pconfig['network_subnet'] > 32) {
114 115
            $input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
        } else {
116
            $osn = gen_subnet($pconfig['network'], $pconfig['network_subnet']) . "/" . $pconfig['network_subnet'];
117 118
            $new_targets[] = $osn;
        }
119 120
    } elseif (is_alias($pconfig['network'])) {
        $osn = $pconfig['network'];
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        foreach (preg_split('/\s+/', $aliastable[$osn]) as $tgt) {
            if (is_ipaddrv4($tgt)) {
                $tgt .= "/32";
            }
            if (is_ipaddrv6($tgt)) {
                $tgt .= "/128";
            }
            if (!is_subnet($tgt)) {
                continue;
            }
            if (!is_subnetv6($tgt)) {
                continue;
            }
            $new_targets[] = $tgt;
        }
    }
    if (!isset($id)) {
        $id = count($a_routes);
    }
    $oroute = $a_routes[$id];
    $old_targets = array();
    if (!empty($oroute)) {
        if (is_alias($oroute['network'])) {
            foreach (filter_expand_alias_array($oroute['network']) as $tgt) {
                if (is_ipaddrv4($tgt)) {
                    $tgt .= "/32";
                } elseif (is_ipaddrv6($tgt)) {
                    $tgt .= "/128";
                }
                if (!is_subnet($tgt)) {
                    continue;
                }
                $old_targets[] = $tgt;
            }
        } else {
            $old_targets[] = $oroute['network'];
        }
    }

    $overlaps = array_intersect($current_targets, $new_targets);
    $overlaps = array_diff($overlaps, $old_targets);
    if (count($overlaps)) {
        $input_errors[] = gettext("A route to these destination networks already exists") . ": " . implode(", ", $overlaps);
    }

    if (is_array($config['interfaces'])) {
        foreach ($config['interfaces'] as $if) {
168
            if (is_ipaddrv4($pconfig['network'])
169 170 171
                && isset($if['ipaddr']) && isset($if['subnet'])
                && is_ipaddrv4($if['ipaddr']) && is_numeric($if['subnet'])
                && ($_POST['network_subnet'] == $if['subnet'])
172
                && (gen_subnet($pconfig['network'], $pconfig['network_subnet']) == gen_subnet($if['ipaddr'], $if['subnet']))) {
173
                    $input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
174
            } elseif (is_ipaddrv6($pconfig['network'])
175 176 177
                && isset($if['ipaddrv6']) && isset($if['subnetv6'])
                && is_ipaddrv6($if['ipaddrv6']) && is_numeric($if['subnetv6'])
                && ($_POST['network_subnet'] == $if['subnetv6'])
178
                && (gen_subnetv6($pconfig['network'], $pconfig['network_subnet']) == gen_subnetv6($if['ipaddrv6'], $if['subnetv6']))) {
179 180 181 182 183
                    $input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
            }
        }
    }

184
    if (count($input_errors) == 0){
185 186
        $route = array();
        $route['network'] = $osn;
187 188 189
        $route['gateway'] = $pconfig['gateway'];
        $route['descr'] = $pconfig['descr'];
        if (!empty($pconfig['disabled'])) {
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
            $route['disabled'] = true;
        } else {
            unset($route['disabled']);
        }

        if (file_exists('/tmp/.system_routes.apply')) {
            $toapplylist = unserialize(file_get_contents('/tmp/.system_routes.apply'));
        } else {
            $toapplylist = array();
        }
        $a_routes[$id] = $route;

        if (!empty($oroute)) {
            $delete_targets = array_diff($old_targets, $new_targets);
            if (count($delete_targets)) {
                foreach ($delete_targets as $dts) {
                    if (is_ipaddrv6($dts)) {
                        $family = '-inet6';
                    }
                    $toapplylist[] = "/sbin/route delete {$family} {$dts}";
                }
            }
        }
        file_put_contents('/tmp/.system_routes.apply', serialize($toapplylist));
        mark_subsystem_dirty('staticroutes');
        write_config();

        header("Location: system_routes.php");
        exit;
    }
Ad Schellevis's avatar
Ad Schellevis committed
220 221
}

222 223
legacy_html_escape_form_data($a_gateways);
legacy_html_escape_form_data($pconfig);
224

Ad Schellevis's avatar
Ad Schellevis committed
225 226
include("head.inc");

227
?>
Ad Schellevis's avatar
Ad Schellevis committed
228

229 230 231 232 233 234 235 236 237 238 239 240 241
<body>
  <script type="text/javascript">
    $( document ).ready(function() {
        // hook in, ipv4/ipv6 selector events
        hook_ipv4v6('ipv4v6net', 'network-id');
    });
  </script>
  <?php include("fbegin.inc"); ?>
  <section class="page-content-main">
    <div class="container-fluid">
      <div class="row">
<?php if (isset($input_errors) && count($input_errors) > 0) {
      print_input_errors($input_errors);
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
        <section class="col-xs-12">
          <div class="content-box">
            <form action="system_routes_edit.php" method="post" name="iform" id="iform">
              <div class="table-responsive">
                <table class="table table-striped">
                  <tr>
                    <td width="22%"></td>
                    <td width="78%" align="right">
                      <small><?=gettext("full help"); ?> </small>
                      <i class="fa fa-toggle-off text-danger"  style="cursor: pointer;" id="show_all_help_page" type="button"></i></a>
                    </td>
                  </tr>
                  <tr>
                    <td><a id="help_for_network" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Destination network"); ?></td>
                    <td>
                      <input name="network" type="text" id="network" value="<?=$pconfig['network'];?>" />
                      /
                      <select name="network_subnet" data-network-id="network" class="ipv4v6net" id="network_netbits">
  <?php               for ($i = 128; $i >= 0; $i--) :
  ?>
                        <option value="<?=$i;?>" <?= isset($pconfig['network_subnet']) && $i == $pconfig['network_subnet'] ? "selected=\"selected\"" : "";?>>
                          <?=$i;?>
                        </option>
  <?php
                      endfor; ?>
                      </select>
                      <div class="hidden" for="help_for_network">
                        <?=gettext("Destination network for this static route"); ?>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td><a id="help_for_gateway" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Gateway"); ?></td>
                    <td>
                      <select name="gateway" id="gateway" class="selectpicker">
<?php
                      foreach ($a_gateways as $gateway):?>
                        <option value="<?=$gateway['name'];?>" <?=$gateway['name'] == $pconfig['gateway'] ? "selected=\"selected\"" : "";?>>
                          <?=$gateway['name'] . " - " . $gateway['gateway'];?>
                        </option>
<?php
                      endforeach;?>
                      </select>
                      <div class="hidden" for="help_for_gateway">
                          <?=gettext("Choose which gateway this route applies to or");?>
                          <a href="/system_gateways_edit.php"><?=gettext("add a new one.");?></a>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td><a id="help_for_disabled" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Disabled");?></td>
                    <td width="78%" class="vtable">
                      <input name="disabled" type="checkbox" value="yes" <?= !empty($pconfig['disabled']) ? "checked=\"checked\"" : "";?>/>
                      <div class="hidden" for="help_for_disabled">
                        <strong><?=gettext("Disable this static route");?></strong><br/>
                        <?=gettext("Set this option to disable this static route without removing it from the list.");?>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td><a id="help_for_descr" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Description"); ?></td>
                    <td>
                      <input name="descr" type="text" value="<?=$pconfig['descr'];?>" />
                      <div for="help_for_descr" class="hidden">
                        <?=gettext("You may enter a description here for your reference (not parsed)."); ?>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td></td>
                    <td>
                      <input id="save" name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save");?>" />
                      <input id="cancel" type="button" class="btn btn-default" value="<?=gettext("Cancel");?>" onclick="window.location.href='<?=$referer;?>'" />
<?php
                      if (isset($id) && $a_routes[$id]) :?>
                        <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
<?php
                      endif; ?>
                    </td>
                  </tr>
                </table>
              </div>
            </form>
          </div>
        </section>
      </div>
    </div>
  </section>
331
<?php include("foot.inc");