interfaces_assign.php 20.3 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2
<?php

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) Jim McBeath
    Copyright (C) 2003-2005 Manuel Kasper <mk@neon1.net>
    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
require_once("guiconfig.inc");
Ad Schellevis's avatar
Ad Schellevis committed
32
require_once("filter.inc");
33
require_once("vpn.inc");
Ad Schellevis's avatar
Ad Schellevis committed
34
require_once("rrd.inc");
35 36
require_once("system.inc");
require_once("interfaces.inc");
37
require_once("openvpn.inc");
38
require_once("pfsense-utils.inc");
39
require_once("services.inc");
40
require_once("unbound.inc");
Ad Schellevis's avatar
Ad Schellevis committed
41

42 43 44 45 46 47 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
function list_interfaces() {
    global $config;
    $interfaces  = array();

    // define config sections to fetch interfaces from.
    $config_sections = array();
    $config_sections['wireless.clone'] = array('descr' => 'cloneif', 'key' => 'cloneif');
    $config_sections['vlans.vlan'] = array('descr' => "tag,if,descr", 'format' => gettext('vlan %s on %s') . ' (%s)', 'key' => 'vlanif');
    $config_sections['bridges.bridged'] = array('descr' => 'bridgeif, descr', 'key' => 'bridgeif', 'format' => '%s (%s)');
    $config_sections['gifs.gif'] = array('descr' => 'remote-addr,descr', 'key' => 'gifif', 'format' => 'gif %s (%s)');
    $config_sections['gres.gre'] = array('descr' => 'remote-addr,descr', 'key' => 'greif', 'format' => 'gre %s (%s)');
    $config_sections['laggs.lagg'] = array('descr' => 'laggif, descr', 'key' => 'laggif', 'format' => '%s (%s)', 'fields' => 'members');
    $config_sections['ppps.ppp'] = array('descr' => 'if,ports,descr,username', 'key' => 'if','format' => '%s (%s) - %s %s', 'fields' => 'type');

    // add physical network interfaces
    foreach (get_interface_list() as $key => $intf_item) {
        $interfaces[$key] = array('descr' => $key . ' (' . $intf_item['mac'] . ')', 'section' => 'interfaces');
    }
    // collect interfaces from defined config sections
    foreach ($config_sections as $key => $value) {
        $cnf_location = explode(".", $key);
        if (!empty($config[$cnf_location[0]][$cnf_location[1]])) {
            foreach ($config[$cnf_location[0]][$cnf_location[1]] as $cnf_item) {
                $interface_item = array("section" => $key);
                // construct item description
                $descr = array();
                foreach (explode(',', $value['descr']) as $fieldname) {
                    if (isset($cnf_item[trim($fieldname)])) {
                        $descr[] = $cnf_item[trim($fieldname)];
                    } else {
                        $descr[] = "";
                    }
                }
                if (!empty($value['format'])) {
                    $interface_item['descr'] = vsprintf($value['format'], $descr);
                } else {
                    $interface_item['descr'] = implode(" ", $descr);
                }
                // copy requested additional fields into temp structure
                if (isset($value['fields'])) {
                    foreach (explode(',', $value['fields']) as $fieldname) {
                        if (isset($cnf_item[$fieldname])) {
                            $interface_item[$fieldname] = $cnf_item[$fieldname];
                        }
                    }
                }
                $interfaces[$cnf_item[$value['key']]] = $interface_item;
            }
        }
    }
    /* QinQ interfaces can't be directly extracted from config without additional logic */
    if (isset($config['qinqs']['qinqentry'])) {
        foreach ($config['qinqs']['qinqentry'] as $qinq) {
            $interfaces["vlan{$qinq['tag']}"]= array('descr' => "VLAN {$qinq['tag']}");
            foreach (explode(' ', $qinq['members']) as $qinqif) { // QinQ members
                $interfaces["vlan{$qinq['tag']}_{$qinqif}"] = array( 'descr' => "QinQ {$qinqif}");
            }
        }
    }

    // enforce constraints
    foreach ($interfaces as $intf_id => $intf_details) {
        // LAGG members cannot be assigned
        if (isset($intf_details['members']) && $intf_details['section'] == 'laggs.lagg') {
            foreach (explode(',', ($intf_details['members'])) as $intf) {
                if (isset($interfaces[trim($intf)])) {
                    unset($interfaces[trim($intf)]);
                }
            }
        }
    }

    /* add tun0 interface (required for sixxs-aiccu) */
    //   This is a temporary solution to allow using sixxs-aiccu without manual code change
    //   until the aiccu service is correctly included in the web interface.
    //   (to avoid additional temporary code in the interface_assign_description function).
    $tunfound = "";
    exec("/sbin/ifconfig | /usr/bin/grep -c '^tun0'", $tunfound);
    if (intval($tunfound[0]) > 0) {
        $interfaces['tun0'] = array('descr' => 'sixxs-aiccu');
    }

    return $interfaces;
Ad Schellevis's avatar
Ad Schellevis committed
125 126 127
}


128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $input_errors = array();
    if (isset($_POST['add_x']) && isset($_POST['if_add'])) {
        // ** Add new **
        // if interface is already used, redirect.
        foreach ($config['interfaces'] as $ifname => $ifdata) {
            if ($ifdata['if'] == $_PORT['if_add']) {
                header("Location: interfaces_assign.php");
                exit;
            }
        }

        /* find next free optional interface number */
        if(empty($config['interfaces']['lan'])) {
            $newifname = gettext("lan");
            $descr = gettext("LAN");
        } else {
            for ($i = 1; $i <= count($config['interfaces']); $i++) {
146 147 148
                if (empty($config['interfaces']["opt{$i}"])) {
                    break;
                }
149 150 151 152 153 154 155 156
            }
            $newifname = 'opt' . $i;
            $descr = "OPT" . $i;
        }

        $config['interfaces'][$newifname] = array();
        $config['interfaces'][$newifname]['descr'] = $descr;
        $config['interfaces'][$newifname]['if'] = $_POST['if_add'];
157 158 159 160
        $interfaces = list_interfaces();
        if ($interfaces[$_POST['if_add']]['section'] == 'ppps.ppp') {
            $config['interfaces'][$newifname]['ipaddr'] = $interfaces[$_POST['if_add']]['type'];
        }
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        if (match_wireless_interface($_POST['if_add'])) {
            $config['interfaces'][$newifname]['wireless'] = array();
            interface_sync_wireless_clones($config['interfaces'][$newifname], false);
        }

        uksort($config['interfaces'], "compare_interface_friendly_names");
        write_config();
        header("Location: interfaces_assign.php");
        exit;
    } elseif (!empty($_POST['id']) && !empty($_POST['action']) && $_POST['action'] == 'del' & !empty($config['interfaces'][$_POST['id']]) ) {
        // ** Delete interface **
        $id = $_POST['id'];
        if (link_interface_to_group($id)) {
            $input_errors[] = gettext("The interface is part of a group. Please remove it from the group to continue");
        } else if (link_interface_to_bridge($id)) {
            $input_errors[] = gettext("The interface is part of a bridge. Please remove it from the bridge to continue");
        } else if (link_interface_to_gre($id)) {
            $input_errors[] = gettext("The interface is part of a gre tunnel. Please delete the tunnel to continue");
        } else if (link_interface_to_gif($id)) {
            $input_errors[] = gettext("The interface is part of a gif tunnel. Please delete the tunnel to continue");
        } else {
            // no validation errors, delete entry
            unset($config['interfaces'][$id]['enable']);
            $realid = get_real_interface($id);
185
            interface_bring_down($id, true);   /* down the interface */
186 187 188

            unset($config['interfaces'][$id]);  /* delete the specified OPTn or LAN*/

189
            if (isset($config['dhcpd'][$id])) {
190 191 192
                unset($config['dhcpd'][$id]);
                services_dhcpd_configure();
            }
193
            if (isset($config['filter']['rule'])) {
194
                foreach ($config['filter']['rule'] as $x => $rule) {
195
                    if ($rule['interface'] == $id) {
196 197 198 199
                        unset($config['filter']['rule'][$x]);
                    }
                }
            }
200
            if (isset($config['nat']['rule'])) {
201
                foreach ($config['nat']['rule'] as $x => $rule) {
202
                    if ($rule['interface'] == $id) {
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
                        unset($config['nat']['rule'][$x]['interface']);
                    }
                }
            }

            write_config();

            /* If we are in firewall/routing mode (not single interface)
             * then ensure that we are not running DHCP on the wan which
             * will make a lot of ISP's unhappy.
             */
            if(!empty($config['interfaces']['lan']) && !empty($config['dhcpd']['wan']) && !empty($config['dhcpd']['wan']) ) {
                unset($config['dhcpd']['wan']);
            }
            link_interface_to_vlans($realid, "update");
            // redirect
            header("Location: interfaces_assign.php");
            exit;
        }
    } elseif (isset($_POST['Submit'])) {
        // ** Change interface **
        /* input validation */
        /* Build a list of the port names so we can see how the interfaces map */
        $portifmap = array();
        $interfaces = list_interfaces();
        foreach ($interfaces as $portname => $portinfo) {
            $portifmap[$portname] = array();
        }

        /* Go through the list of ports selected by the user,
        build a list of port-to-interface mappings in portifmap */
        foreach ($_POST as $ifname => $ifport) {
            if ($ifname == 'lan' || $ifname == 'wan' || substr($ifname, 0, 3) == 'opt') {
                $portifmap[$ifport][] = strtoupper($ifname);
            }
        }

        /* Deliver error message for any port with more than one assignment */
        foreach ($portifmap as $portname => $ifnames) {
            if (count($ifnames) > 1) {
              $errstr = sprintf(gettext('Port %s was assigned to %d interfaces:'), $portname, count($ifnames));
              foreach ($portifmap[$portname] as $ifn) {
                  $errstr .= " " . $ifn;
              }
              $input_errors[] = $errstr;
            } elseif (count($ifnames) == 1 && preg_match('/^bridge[0-9]/', $portname) && isset($config['bridges']['bridged'])) {
                foreach ($config['bridges']['bridged'] as $bridge) {
                    if ($bridge['bridgeif'] != $portname) {
                        continue;
                    }

                    $members = explode(",", strtoupper($bridge['members']));
                    foreach ($members as $member) {
                        if ($member == $ifnames[0]) {
                            $input_errors[] = sprintf(gettext("You cannot set port %s to interface %s because this interface is a member of %s."), $portname, $member, $portname);
                            break;
                        }
                    }
                }
            }
        }

        if (isset($config['vlans']['vlan'])) {
            foreach ($config['vlans']['vlan'] as $vlan) {
                if (!does_interface_exist($vlan['if'])) {
                    $input_errors[] = sprintf(gettext("VLAN parent interface %s does not exist."), $vlan['if']);
                }
            }
        }

        if (count($input_errors) == 0) {
          /* No errors detected, so update the config */
275
          $changes = 0;
276
          foreach ($_POST as $ifname => $ifport) {
277 278 279
              if (!is_array($ifport) && ($ifname == 'lan' || $ifname == 'wan' || substr($ifname, 0, 3) == 'opt')) {
                  $reloadif = false;
                  if (!empty($config['interfaces'][$ifname]['if']) && $config['interfaces'][$ifname]['if'] <> $ifport) {
280
                      interface_bring_down($ifname, true);
281 282 283 284 285 286 287
                      /* Mark this to be reconfigured in any case. */
                      $reloadif = true;
                  }
                  $config['interfaces'][$ifname]['if'] = $ifport;
                  if ($interfaces[$ifport]['section'] == 'ppps.ppp') {
                      $config['interfaces'][$ifname]['ipaddr'] = $interfaces[$ifport]['type'];
                  }
288

289 290 291 292 293 294
                  if (substr($ifport, 0, 3) == 'gre' || substr($ifport, 0, 3) == 'gif') {
                      unset($config['interfaces'][$ifname]['ipaddr']);
                      unset($config['interfaces'][$ifname]['subnet']);
                      unset($config['interfaces'][$ifname]['ipaddrv6']);
                      unset($config['interfaces'][$ifname]['subnetv6']);
                  }
295

296 297 298 299
                  /* check for wireless interfaces, set or clear ['wireless'] */
                  if (match_wireless_interface($ifport)) {
                      if (empty($config['interfaces'][$ifname]['wireless'])) {
                          $config['interfaces'][$ifname]['wireless'] = array();
300
                      }
301 302 303
                  } elseif (isset($config['interfaces'][$ifname]['wireless'])) {
                      unset($config['interfaces'][$ifname]['wireless']);
                  }
304

305 306 307 308
                  /* make sure there is a descr for all interfaces */
                  if (!isset($config['interfaces'][$ifname]['descr'])) {
                      $config['interfaces'][$ifname]['descr'] = strtoupper($ifname);
                  }
309 310


311 312 313
                  if ($reloadif) {
                      if (match_wireless_interface($ifport)) {
                          interface_sync_wireless_clones($config['interfaces'][$ifname], false);
314
                      }
315 316 317 318
                      /* Reload all for the interface. */
                      interface_configure($ifname, true);
                      // count changes
                      $changes++;
319 320 321
                  }
              }
          }
322
          write_config();
323
          if ($changes > 0) {
324
              // reload filter, rrd when interfaces have changed (original from apply action)
325
              filter_configure();
326
              enable_rrd_graphing();
327
          }
328 329 330 331 332
          // redirect
          header("Location: interfaces_assign.php");
          exit;
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
333 334
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
/* collect (unused) interfaces */
$interfaces = list_interfaces();
legacy_html_escape_form_data($interfaces);
$unused_interfaces= array();
foreach ($interfaces as $portname => $portinfo) {
    $portused = false;
    foreach ($config['interfaces'] as $ifname => $ifdata) {
        if ($ifdata['if'] == $portname) {
            $portused = true;
            break;
        }
    }
    if (!$portused) {
        $unused_interfaces[$portname] = $portinfo;
    }
Ad Schellevis's avatar
Ad Schellevis committed
350 351 352 353 354
}

include("head.inc");
?>

Ad Schellevis's avatar
Ad Schellevis committed
355
<body>
356 357 358 359 360 361 362 363
  <script type="text/javascript">
  $( document ).ready(function() {
    // link delete buttons
    $(".act_delete").click(function(event){
      event.preventDefault();
      var id = $(this).data("id");
      // delete single
      BootstrapDialog.show({
Fabian Franz's avatar
Fabian Franz committed
364
        type:BootstrapDialog.TYPE_DANGER,
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
        title: "<?= gettext("Interfaces");?>",
        message: "<?=gettext("Do you really want to delete this interface?"); ?>",
        buttons: [{
                  label: "<?= gettext("No");?>",
                  action: function(dialogRef) {
                      dialogRef.close();
                  }}, {
                  label: "<?= gettext("Yes");?>",
                  action: function(dialogRef) {
                    $("#id").val(id);
                    $("#action").val("del");
                    $("#iform").submit()
                }
              }]
      });
    });

  });
  </script>
Ad Schellevis's avatar
Ad Schellevis committed
384
<?php include("fbegin.inc"); ?>
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  <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);
      }?>
        <section class="col-xs-12">
          <div class="tab-content content-box col-xs-12">
            <form  method="post" name="iform" id="iform">
              <input type="hidden" id="action" name="action" value="">
              <input type="hidden" id="id" name="id" value="">

              <div class="table-responsive">
                <table class="table table-striped">
                  <thead>
                    <tr>
                      <th><?=gettext("Interface"); ?></th>
                      <th><?=gettext("Network port"); ?></th>
                      <th colspan="2"></th>
                    </tr>
                  </thead>
                  <tbody>
<?php
                  foreach ($config['interfaces'] as $ifname => $iface):?>
                      <tr>
                        <td>
412
                          <strong><u><span onclick="location.href='/interfaces.php?if=<?=$ifname;?>'" style="cursor: pointer;"><?=!empty($iface['descr']) ? $iface['descr'] : strtoupper($ifname);?></span></u></strong>
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
                        </td>
                        <td>
                          <select name="<?=$ifname;?>" id="<?=$ifname;?>">
<?php
                          foreach ($interfaces as $portname => $portinfo):?>
                            <option  value="<?=$portname;?>"  <?= $portname == $iface['if'] ? " selected=\"selected\"" : "";?>>
                              <?=$portinfo['descr'];?>
                            </option>
<?php
                          endforeach;?>
                          </select>
                        </td>
                        <td>
<?php
                        if ($ifname != 'wan'):?>
428
                          <button title="<?=gettext("delete interface");?>" data-toggle="tooltip" data-id="<?=$ifname;?>" class="btn btn-default act_delete" type="submit">
429
                            <span class="fa fa-trash text-muted"></span>
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
                          </button>
<?php
                        endif;?>
                        </td>
                      </tr>
<?php
                      endforeach;
                      if (count($unused_interfaces) > 0):?>
                      <tr>
                        <td>
                          <strong><?=gettext("Available network ports:");?></strong>
                        </td>
                        <td>
                          <select name="if_add" id="if_add">
<?php
                          foreach ($unused_interfaces as $portname => $portinfo): ?>
                            <option  value="<?=$portname;?>"> <?=$portinfo['descr'];?></option>
<?php
                          endforeach; ?>
                          </select>
                        </td>
                        <td>
452
                          <button name="add_x" type="submit" value="<?=$portname;?>" class="btn btn-primary" title="<?=gettext("add selected interface");?>" data-toggle="tooltip">
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
                            <span class="glyphicon glyphicon-plus"></span>
                          </button>
                        </td>
                      </tr>
                      <tr>
                          <td colspan="3">
                            <?= gettext('Interfaces that are configured as members of a LAGG interface will not be shown.') ?>
                          </td>
                      </tr>
                      <tr>
                        <td colspan="2"></td>
                        <td>
                          <input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" />
                        </td>
                      </tr>
<?php
                      endif; ?>
                    </tbody>
                  </table>
                </div>
              </div>
            </form>
          </div>
        </section>
      </div>
    </div>
  </section>
Ad Schellevis's avatar
Ad Schellevis committed
480
<?php include("foot.inc"); ?>