interfaces_qinq_edit.php 10 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
    Copyright (C) 2014-2015 Deciso B.V.
    Copyright (C) 2009 Ermal Luçi
    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
28
*/
29
error_reporting(E_ALL);
Ad Schellevis's avatar
Ad Schellevis committed
30

31
require_once("guiconfig.inc");
32
require_once("interfaces.inc");
Ad Schellevis's avatar
Ad Schellevis committed
33

34 35
if (!isset($config['qinqs']) || !is_array($config['qinqs'])) {
    $config['qinqs'] = array();
Ad Schellevis's avatar
Ad Schellevis committed
36
}
37 38
if (!isset($config['qinqs']['qinqentry']) || !is_array($config['qinqs']['qinqentry'])) {
    $config['qinqs']['qinqentry'] = array();
Ad Schellevis's avatar
Ad Schellevis committed
39
}
40
$a_qinqs = &$config['qinqs']['qinqentry'];
Ad Schellevis's avatar
Ad Schellevis committed
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    // read form data
    if (!empty($a_qinqs[$_GET['id']])) {
        $id = $_GET['id'];
    }
    $pconfig['if'] = isset($a_qinqs[$id]['if']) ? $a_qinqs[$id]['if'] : null;
    $pconfig['tag'] = isset($a_qinqs[$id]['tag']) ? $a_qinqs[$id]['tag'] : null;
    $pconfig['members'] = isset($a_qinqs[$id]['members']) ? explode(' ', $a_qinqs[$id]['members']) : array();
    $pconfig['descr'] = isset($a_qinqs[$id]['descr']) ? $a_qinqs[$id]['descr'] : null;
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // validate / save form data
    if (!empty($a_qinqs[$_POST['id']])) {
        $id = $_POST['id'];
    }
    $input_errors = array();
    $pconfig = $_POST;
Ad Schellevis's avatar
Ad Schellevis committed
58

59 60 61 62 63 64 65 66 67
    if (empty($pconfig['tag'])) {
        $input_errors[] = gettext("First level tag cannot be empty.");
    }
    if (isset($id) && $a_qinqs[$id]['tag'] != $pconfig['tag']) {
        $input_errors[] = gettext("You are editing an existing entry and modifying the first level tag is not allowed.");
    }
    if (isset($id) && $a_qinqs[$id]['if'] != $pconfig['if']) {
        $input_errors[] = gettext("You are editing an existing entry and modifying the interface is not allowed.");
    }
Ad Schellevis's avatar
Ad Schellevis committed
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82
    if (!isset($id)) {
        foreach ($a_qinqs as $qinqentry) {
            if ($qinqentry['tag'] == $pconfig['tag'] && $qinqentry['if'] == $pconfig['if']) {
                $input_errors[] = gettext("QinQ level already exists for this interface, edit it!");
            }
        }
        if (is_array($config['vlans']['vlan'])) {
            foreach ($config['vlans']['vlan'] as $vlan) {
                if ($vlan['tag'] == $pconfig['tag'] && $vlan['if'] == $pconfig['if']) {
                    $input_errors[] = gettext("A normal VLAN exists with this tag please remove it to use this tag for QinQ first level.");
                }
            }
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
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
    if (count($input_errors) == 0) {
        $qinqentry = array();
        $qinqentry['if'] = $pconfig['if'];
        $qinqentry['tag'] = $pconfig['tag'];
        $qinqentry['members'] = implode(" ", $pconfig['members']);
        $qinqentry['descr'] = $pconfig['descr'];
        $qinqentry['vlanif'] = "{$pconfig['if']}_{$pconfig['tag']}";

        if (isset($id)) {
            $omembers = explode(" ", $a_qinqs[$id]['members']);
            $delmembers = array_diff($omembers, $pconfig['members']);
            $addmembers = array_diff($pconfig['members'], $omembers);

            if ((count($delmembers) > 0) || (count($addmembers) > 0)) {
                // XXX needs improvement
                $fd = fopen('/tmp/netgraphcmd', 'w');
                foreach ($delmembers as $tag) {
                    fwrite($fd, "shutdown {$qinqentry['vlanif']}h{$tag}:\n");
                    fwrite($fd, "msg {$qinqentry['vlanif']}qinq: delfilter \\\"{$qinqentry['vlanif']}{$tag}\\\"\n");
                }

                foreach ($addmembers as $member) {
                    $qinq = array();
                    $qinq['if'] = $qinqentry['vlanif'];
                    $qinq['tag'] = $member;
                    $macaddr = get_interface_mac($qinqentry['vlanif']);
                    interface_qinq2_configure($qinq, $fd, $macaddr);
                }

                fclose($fd);
                mwexec('/usr/sbin/ngctl -f /tmp/netgraphcmd');
            }
            $a_qinqs[$id] = $qinqentry;
        } else {
            interface_qinq_configure($qinqentry);
            $a_qinqs[] = $qinqentry;
        }
Ad Schellevis's avatar
Ad Schellevis committed
121

122 123 124 125
        write_config();
        header("Location: interfaces_qinq.php");
        exit;
      }
Ad Schellevis's avatar
Ad Schellevis committed
126 127 128 129 130
}

include("head.inc");
?>

131
<body>
Ad Schellevis's avatar
Ad Schellevis committed
132
<?php include("fbegin.inc"); ?>
133 134 135 136 137 138 139 140 141 142 143 144 145 146
<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="content-box">
          <div class="table-responsive">
            <form method="post" name="iform" id="iform">
              <table class="table table-striped">
                <thead>
                  <tr>
                    <td width="22%"><strong><?=gettext("Interface QinQ Edit");?></strong></td>
                    <td width="78%" align="right">
                      <small><?=gettext("full help"); ?> </small>
Ad Schellevis's avatar
Ad Schellevis committed
147
                      <i class="fa fa-toggle-off text-danger"  style="cursor: pointer;" id="show_all_help_page" type="button"></i>
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
                      &nbsp;
                    </td>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td><a id="help_for_if" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Parent interface"); ?></td>
                    <td>
                      <select name="if" class="selectpicker">
<?php
                      $portlist = get_interface_list();
                      /* add LAGG interfaces */
                      if (isset($config['laggs']['lagg'])) {
                          foreach ($config['laggs']['lagg'] as $lagg) {
                              $portlist[$lagg['laggif']] = $lagg;
                          }
                      }
                      foreach ($portlist as $ifn => $ifinfo):
                        if (!is_jumbo_capable($ifn)) {
                            continue;
                        }?>
                        <option value="<?=$ifn;?>" <?=$ifn == $pconfig['if'] ? " selected=\"selected\"" : "";?>>
                          <?=htmlspecialchars($ifn);?>  ( <?= !empty($ifinfo['mac']) ? $ifinfo['mac'] :"" ;?> )
                        </option>
<?php
                      endforeach;?>
                      </select>
                      <div class="hidden" for="help_for_if">
                        <?=gettext("Only QinQ capable interfaces will be shown.");?>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td><a id="help_for_tag" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("First level tag");?></td>
                    <td>
                      <input name="tag" type="text" value="<?=$pconfig['tag'];?>" />
                      <div class="hidden" for="help_for_tag">
                        <?=gettext("This is the first level VLAN tag. On top of this are stacked the member VLANs defined below.");?>
                      </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 class="hidden" for="help_for_descr">
                        <?=gettext("You may enter a description here for your reference (not parsed).");?>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td><i class="fa fa-info-circle text-muted"></i>  <?=gettext("Member (s)");?></div></td>
                    <td>
                      <select name="members[]" multiple="multiple" data-size="10" class="selectpicker" data-live-search="true">
<?php
                      for ($vlanid =  1 ; $vlanid <= 4094 ; $vlanid++):?>
                      <option value="<?=$vlanid;?>" <?=in_array($vlanid, $pconfig['members']) ? "selected=\"selected\"" : "";?>>
                          <?=$vlanid;?>
                      </option>
<?php
                      endfor;?>
                      </select>
                    </td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td>
                      <input name="submit" type="submit" class="btn btn-primary" value="<?=gettext("Save");?>" />
                      <input type="button" class="btn btn-default" value="<?=gettext("Cancel");?>" onclick="window.location.href='<?=(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/interfaces_qinq.php');?>'" />
                      <?php if (isset($id) && $a_qinqs[$id]): ?>
                      <input name="id" type="hidden" value="<?=$id;?>" />
                      <?php endif; ?>
                    </td>
                  </tr>
                </tbody>
              </table>
            </form>
          </div>
        </div>
      </section>
    </div>
  </div>
</section>
231
<?php include("foot.inc"); ?>