interfaces_qinq_edit.php 13.9 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
	Copyright (C) 2014-2015 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
	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.
*/

30
require_once("guiconfig.inc");
31
require_once("interfaces.inc");
Ad Schellevis's avatar
Ad Schellevis committed
32 33 34 35 36 37 38 39 40

if (!is_array($config['qinqs']['qinqentry']))
	$config['qinqs']['qinqentry'] = array();

$a_qinqs = &$config['qinqs']['qinqentry'];

$portlist = get_interface_list();

/* add LAGG interfaces */
41 42
if (isset($config['laggs']['lagg'])) {
        foreach ($config['laggs']['lagg'] as $lagg) {
Ad Schellevis's avatar
Ad Schellevis committed
43
                $portlist[$lagg['laggif']] = $lagg;
44
	}
Ad Schellevis's avatar
Ad Schellevis committed
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
}

if (count($portlist) < 1) {
	header("Location: interfaces_qinq.php");
	exit;
}

if (is_numericint($_GET['id']))
	$id = $_GET['id'];
if (isset($_POST['id']) && is_numericint($_POST['id']))
	$id = $_POST['id'];

if (isset($id) && $a_qinqs[$id]) {
	$pconfig['if'] = $a_qinqs[$id]['if'];
	$pconfig['tag'] = $a_qinqs[$id]['tag'];
	$pconfig['members'] = $a_qinqs[$id]['members'];
	$pconfig['descr'] = html_entity_decode($a_qinqs[$id]['descr']);
/*
	$pconfig['autoassign'] = isset($a_qinqs[$id]['autoassign']);
	$pconfig['autoenable'] = isset($a_qinqs[$id]['autoenable']);
*/
	$pconfig['autogroup'] = isset($a_qinqs[$id]['autogroup']);
	$pconfig['autoadjustmtu'] = isset($a_qinqs[$id]['autoadjustmtu']);
}

if ($_POST) {

	unset($input_errors);
	$pconfig = $_POST;

	if (empty($_POST['tag']))
		$input_errors[] = gettext("First level tag cannot be empty.");
	if (isset($id) && $a_qinqs[$id]['tag'] != $_POST['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'] != $_POST['if'])
		$input_errors[] = gettext("You are editing an existing entry and modifying the interface is not allowed.");
	if (!isset($id)) {
		foreach ($a_qinqs as $qinqentry)
			if ($qinqentry['tag'] == $_POST['tag'] && $qinqentry['if'] == $_POST['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'] == $_POST['tag'] && $vlan['if'] == $_POST['if'])
					$input_errors[] = gettext("A normal VLAN exists with this tag please remove it to use this tag for QinQ first level.");
		}
	}

	$qinqentry = array();
	$qinqentry['if'] = $_POST['if'];
	$qinqentry['tag'] = $_POST['tag'];
/*
	if ($_POST['autoassign'] == "yes") {
		$qinqentry['autoassign'] = true;
	if ($_POST['autoenable'] == "yes")
		$qinqentry['autoenable'] = true;
	if ($_POST['autoadjust'] == "yes")
		$qinqentry['autoadjustmtu'] = true;
*/
	if ($_POST['autogroup'] == "yes")
		$qinqentry['autogroup'] = true;

	$members = "";
	$isfirst = 0;
	/* item is a normal qinqentry type */
	for($x=0; $x<9999; $x++) {
		if($_POST["members{$x}"] <> "") {
			$member = explode("-", $_POST["members{$x}"]);
			if (count($member) > 1) {
				if (preg_match("/([^0-9])+/", $member[0], $match)  ||
					preg_match("/([^0-9])+/", $member[1], $match))
					$input_errors[] = gettext("Tags can contain only numbers or a range in format #-#.");

				for ($i = $member[0]; $i <= $member[1]; $i++) {
					if ($isfirst > 0)
						$members .= " ";
					$members .= $i;
					$isfirst++;
				}
			} else {
124
				if (preg_match("/([^0-9 ])+/", $_POST["members{$x}"], $match))
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 146
					$input_errors[] = gettext("Tags can contain only numbers or a range in format #-#.");

				if ($isfirst > 0)
					$members .= " ";
				$members .= $_POST["members{$x}"];
				$isfirst++;
			}
		}
	}

	if (!$input_errors) {
		$qinqentry['members'] = $members;
		$qinqentry['descr'] = $_POST['descr'];
		$qinqentry['vlanif'] = "{$_POST['if']}_{$_POST['tag']}";
		$nmembers = explode(" ", $members);

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

			if ((count($delmembers) > 0) || (count($addmembers) > 0)) {
147
				$fd = fopen('/tmp/netgraphcmd', 'w');
Ad Schellevis's avatar
Ad Schellevis committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161
				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);
162
				mwexec('/usr/sbin/ngctl -f /tmp/netgraphcmd');
Ad Schellevis's avatar
Ad Schellevis committed
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
			}
			$a_qinqs[$id] = $qinqentry;
		} else {
			interface_qinq_configure($qinqentry);
			$a_qinqs[] = $qinqentry;
		}
		if ($_POST['autogroup'] == "yes") {
			if (!is_array($config['ifgroups']['ifgroupentry']))
				$config['ifgroups']['ifgroupentry'] = array();
			foreach ($config['ifgroups']['ifgroupentry'] as $gid => $group) {
				if ($group['ifname'] == "QinQ") {
					$found = true;
					break;
				}
			}
			$additions = "";
			foreach($nmembers as $qtag)
				$additions .= "{$qinqentry['vlanif']}_{$qtag} ";
			$additions .= "{$qinqentry['vlanif']}";
			if ($found == true)
				$config['ifgroups']['ifgroupentry'][$gid]['members'] .= " {$additions}";
			else {
				$gentry = array();
				$gentry['ifname'] = "QinQ";
				$gentry['members'] = "{$additions}";
				$gentry['descr'] = gettext("QinQ VLANs group");
				$config['ifgroups']['ifgroupentry'][] = $gentry;
			}
		}

		write_config();

		header("Location: interfaces_qinq.php");
		exit;
	} else {
		$pconfig['descr'] = $_POST['descr'];
		$pconfig['tag'] = $_POST['tag'];
		$pconfig['members'] = $members;
	}
}

include("head.inc");

?>


209
<body>
Ad Schellevis's avatar
Ad Schellevis committed
210
	<script type="text/javascript">
Ad Schellevis's avatar
Ad Schellevis committed
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
//<![CDATA[
// Global Variables
var rowname = new Array(9999);
var rowtype = new Array(9999);
var newrow  = new Array(9999);
var rowsize = new Array(9999);

for (i = 0; i < 9999; i++) {
        rowname[i] = 'members';
        rowtype[i] = 'select';
        newrow[i] = '';
        rowsize[i] = '30';
}

var field_counter_js = 0;
var loaded = 0;
var is_streaming_progress_bar = 0;
var temp_streaming_text = "";

var addRowTo = (function() {
    return (function (tableId) {
        var d, tbody, tr, td, bgc, i, ii, j;
        d = document;
        tbody = d.getElementById(tableId).getElementsByTagName("tbody").item(0);
        tr = d.createElement("tr");
        for (i = 0; i < field_counter_js; i++) {
                td = d.createElement("td");
		td.innerHTML="<input type='hidden' value='" + totalrows +"' name='" + rowname[i] + "_row-" + totalrows + "' /><input size='" + rowsize[i] + "' class='formfld unknown' name='" + rowname[i] + totalrows + "' /> ";
                tr.appendChild(td);
        }
        td = d.createElement("td");
        td.rowSpan = "1";

244
        td.innerHTML = '<a onclick="removeRow(this);return false;" href="#"><span class="glyphicon glyphicon-remove" alt="remove"><\/span><\/a>';
Ad Schellevis's avatar
Ad Schellevis committed
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
        tr.appendChild(td);
        tbody.appendChild(tr);
        totalrows++;
    });
})();

function removeRow(el) {
    var cel;
    while (el && el.nodeName.toLowerCase() != "tr")
            el = el.parentNode;

    if (el && el.parentNode) {
        cel = el.getElementsByTagName("td").item(0);
        el.parentNode.removeChild(el);
    }
}

	rowname[0] = <?=gettext("members");?>;
	rowtype[0] = "textbox";
	rowsize[0] = "30";

	rowname[2] = <?=gettext("detail");?>;
	rowtype[2] = "textbox";
	rowsize[2] = "50";
//]]>
</script>
<input type='hidden' name='members_type' value='textbox' class="formfld unknown" />

Ad Schellevis's avatar
Ad Schellevis committed
273 274 275
<?php include("fbegin.inc"); ?>

	<section class="page-content-main">
276
		<div class="container-fluid">
Ad Schellevis's avatar
Ad Schellevis committed
277
			<div class="row">
278

279
				<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
Ad Schellevis's avatar
Ad Schellevis committed
280 281
				<div id="inputerrors"></div>

282

Ad Schellevis's avatar
Ad Schellevis committed
283
			    <section class="col-xs-12">
284 285 286 287 288 289 290 291 292 293

				<div class="content-box">

					 <header class="content-box-head container-fluid">
				        <h3><?=gettext("Interface QinQ Edit");?></h3>
				    </header>

				    <div class="content-box-main">

						<form action="interfaces_qinq_edit.php" method="post" name="iform" id="iform">
294
			                <table class="table table-striped table-sort">
Ad Schellevis's avatar
Ad Schellevis committed
295 296 297
									  <tr>
									    <td width="22%" valign="top" class="vncellreq"><?=gettext("Parent interface");?></td>
									    <td width="78%" class="vtable">
298
									    <select name="if" id="if" class="selectpicker">
Ad Schellevis's avatar
Ad Schellevis committed
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
									    <?php
									        foreach ($portlist as $ifn => $ifinfo) {
											if (is_jumbo_capable($ifn)) {
												echo "<option value=\"{$ifn}\"";
									                        if ($ifn == $pconfig['if'])
													echo " selected=\"selected\"";
									                        echo ">";
									                        echo htmlspecialchars($ifn . " (" . $ifinfo['mac'] . ")");
									                        echo "</option>";
									                }
										}
									    ?>
									    </select>
									    <br />
									    <span class="vexpl"><?=gettext("Only QinQ capable interfaces will be shown.");?></span></td>
									  </tr>
									  <tr>
									    <td width="22%" valign="top" class="vncellreq"><?=gettext("First level tag");?></td>
									    <td width="78%" class="vtable">
									      <input name="tag" type="text" class="formfld unknown" id="tag" size="10" value="<?=htmlspecialchars($pconfig['tag']);?>" />
									      <br />
									      <span class="vexpl">
										<?=gettext("This is the first level VLAN tag. On top of this are stacked the member VLANs defined below.");?>
									      </span>
									    </td>
									  </tr>
									  <tr>
										<td width="22%" valign="top" class="vncell"><?=gettext("Options");?></td>
										<td width="78%" class="vtable">
											<input name="autogroup" type="checkbox" value="yes" id="autogroup" <?php if ($pconfig['autogroup']) echo "checked=\"checked\""; ?> />
											<span class="vexpl"><?=gettext("Adds interface to QinQ interface groups so you can write filter rules easily.");?></span>
										</td>
									  </tr>
									  <tr>
									    <td width="22%" valign="top" class="vncell"><?=gettext("Description");?></td>
									    <td width="78%" class="vtable">
									      <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>" />
									      <br />
									      <span class="vexpl">
									        <?=gettext("You may enter a description here for your reference (not parsed).");?>
									      </span>
									    </td>
									  </tr>
									  <tr>
									    <td width="22%" valign="top" class="vncellreq"><div id="membersnetworkport"><?=gettext("Member (s)");?></div></td>
									    <td width="78%" class="vtable">
										<span class="vexpl">
346
											<?=gettext("You can specify ranges in the input below. The format is pretty simple i.e 9-100 or 10 20...");?>
Ad Schellevis's avatar
Ad Schellevis committed
347 348 349 350 351 352 353
										</span>
										<br />
									      <table id="maintable" summary="main table">
									        <tbody>
									          <tr>
									            <td><div id="onecolumn"><?=gettext("Tag");?></div></td>
									          </tr>
354

Ad Schellevis's avatar
Ad Schellevis committed
355 356 357 358 359 360 361 362 363 364
										<?php
										$counter = 0;
										$members = $pconfig['members'];
										if ($members <> "") {
											$item = explode(" ", $members);
											foreach($item as $ww) {
												$member = $item[$counter];
										?>
									        <tr>
										<td class="vtable">
365
										        <input type="text" name="members<?php echo $counter; ?>" class="formselect" id="members<?php echo $counter; ?>" value="<?php echo $member;?>" />
Ad Schellevis's avatar
Ad Schellevis committed
366 367
										</td>
									        <td>
368
										<a onclick="removeRow(this); return false;" href="#"><span class="glyphicon glyphicon-remove" alt="remove"></span></a>
Ad Schellevis's avatar
Ad Schellevis committed
369 370 371 372
										      </td>
									          </tr>
									<?php
											$counter++;
373

Ad Schellevis's avatar
Ad Schellevis committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
											} // end foreach
										} // end if
									?>
									        </tbody>
											  </table>
												<a onclick="javascript:addRowTo('maintable'); return false;" href="#" title="<?=gettext("add another entry");?>" class="btn btn-default btn-xs"/><span class="glyphicon glyphicon-plus"></span>
									      </a>
											</td>
									  </tr>
									  <tr>
									    <td width="22%" valign="top">&nbsp;</td>
									    <td width="78%">
									      <input id="submit" name="submit" type="submit" class="btn btn-primary" value="<?=gettext("Save");?>" />
									      <a href="interfaces_qinq.php"><input id="cancelbutton" name="cancelbutton" type="button" class="btn btn-default" value="<?=gettext("Cancel");?>" /></a>
									      <?php if (isset($id) && $a_qinqs[$id]): ?>
									      <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
									      <?php endif; ?>
									    </td>
									  </tr>
									</table>
394 395 396
						</form>
				    </div>
				</div>
Ad Schellevis's avatar
Ad Schellevis committed
397 398 399 400
			    </section>
			</div>
		</div>
	</section>
Ad Schellevis's avatar
Ad Schellevis committed
401 402 403 404 405 406 407 408 409 410 411


<script type="text/javascript">
//<![CDATA[
	field_counter_js = 1;
	rows = 1;
	totalrows = <?php echo $counter; ?>;
	loaded = <?php echo $counter; ?>;
//]]>
</script>

412
<?php include("foot.inc"); ?>