system_routes_edit.php 14.3 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");
Ad Schellevis's avatar
Ad Schellevis committed
34 35 36

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

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

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

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

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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'];
    }
73 74
    global $aliastable;

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

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

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

89
    if (($pconfig['network'] && !is_ipaddr($pconfig['network']) && !is_alias($pconfig['network']))) {
90 91
        $input_errors[] = gettext("A valid IPv4 or IPv6 destination network must be specified.");
    }
92
    if (($_POST['network_subnet'] && !is_numeric($pconfig['network_subnet']))) {
93 94
        $input_errors[] = gettext("A valid destination network bit count must be specified.");
    }
95 96
    if (($_POST['gateway']) && is_ipaddr($pconfig['network'])) {
        if (!isset($a_gateways[$pconfig['gateway']])) {
97 98
            $input_errors[] = gettext("A valid gateway must be specified.");
        }
99 100
        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']}'.");
101 102 103 104 105 106
        }
    }

    /* check for overlaps */
    $current_targets = get_staticroutes(true);
    $new_targets = array();
107 108
    if (is_ipaddrv6($pconfig['network'])) {
        $osn = gen_subnetv6($pconfig['network'], $pconfig['network_subnet']) . "/" . $pconfig['network_subnet'];
109 110
        $new_targets[] = $osn;
    }
111 112
    if (is_ipaddrv4($pconfig['network'])) {
        if ($pconfig['network_subnet'] > 32) {
113 114
            $input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
        } else {
115
            $osn = gen_subnet($pconfig['network'], $pconfig['network_subnet']) . "/" . $pconfig['network_subnet'];
116 117
            $new_targets[] = $osn;
        }
118 119
    } elseif (is_alias($pconfig['network'])) {
        $osn = $pconfig['network'];
120 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
        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'])) {
166
        foreach (legacy_config_get_interfaces(array("virtual" => false)) as $if) {
167
            if (is_ipaddrv4($pconfig['network'])
168 169 170
                && isset($if['ipaddr']) && isset($if['subnet'])
                && is_ipaddrv4($if['ipaddr']) && is_numeric($if['subnet'])
                && ($_POST['network_subnet'] == $if['subnet'])
171
                && (gen_subnet($pconfig['network'], $pconfig['network_subnet']) == gen_subnet($if['ipaddr'], $if['subnet']))) {
172
                    $input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
173
            } elseif (is_ipaddrv6($pconfig['network'])
174 175 176
                && isset($if['ipaddrv6']) && isset($if['subnetv6'])
                && is_ipaddrv6($if['ipaddrv6']) && is_numeric($if['subnetv6'])
                && ($_POST['network_subnet'] == $if['subnetv6'])
177
                && (gen_subnetv6($pconfig['network'], $pconfig['network_subnet']) == gen_subnetv6($if['ipaddrv6'], $if['subnetv6']))) {
178 179 180 181 182
                    $input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
            }
        }
    }

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

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

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

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

228 229 230 231 232
<body>
  <script type="text/javascript">
    $( document ).ready(function() {
        // hook in, ipv4/ipv6 selector events
        hook_ipv4v6('ipv4v6net', 'network-id');
233 234 235 236 237 238
        // collect aliases
        var all_aliases = [];
        $("#aliases > option").each(function(){
            all_aliases.push($(this).val())
        });
        $("#network").typeahead({ source: all_aliases});
239 240 241
    });
  </script>
  <?php include("fbegin.inc"); ?>
242 243 244 245 246 247 248 249 250 251 252 253 254
  <!-- push all available aliases in a hidden select box -->
  <select class="hidden" id="aliases">
  <?php
      if (!empty($config['aliases']['alias'])):
        foreach ($config['aliases']['alias'] as $alias):
          if ($alias['type'] == 'network' || $alias['type'] == 'host'):?>
          <option value="<?=$alias['name'];?>"></option>
  <?php
          endif;
        endforeach;
      endif;
  ?>
  </select>
255 256 257 258 259
  <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);
260
} ?>
261 262
        <section class="col-xs-12">
          <div class="content-box">
263
            <form method="post" name="iform" id="iform">
264
              <div class="table-responsive">
265
                <table class="table table-striped opnsense_standard_table_form">
266 267 268 269
                  <tr>
                    <td width="22%"></td>
                    <td width="78%" align="right">
                      <small><?=gettext("full help"); ?> </small>
Ad Schellevis's avatar
Ad Schellevis committed
270
                      <i class="fa fa-toggle-off text-danger"  style="cursor: pointer;" id="show_all_help_page" type="button"></i>
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
                    </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>
349
<?php include("foot.inc");