-
Notifications
You must be signed in to change notification settings - Fork 759
/
interfaces_assign.php
508 lines (464 loc) · 20.1 KB
/
interfaces_assign.php
1
2
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
29
30
31
32
33
34
35
36
37
38
39
40
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
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
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
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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php
/*
* Copyright (C) 2014-2022 Deciso B.V.
* Copyright (C) 2004 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.
*/
require_once("guiconfig.inc");
require_once("filter.inc");
require_once("system.inc");
require_once("interfaces.inc");
function link_interface_to_group($int)
{
global $config;
$result = [];
if (isset($config['ifgroups']['ifgroupentry'])) {
foreach ($config['ifgroups']['ifgroupentry'] as $group) {
if (in_array($int, preg_split('/[ |,]+/', $group['members']))) {
$result[$group['ifname']] = $int;
}
}
}
return $result;
}
function list_devices($devices)
{
global $config;
$interfaces = [];
/* add physical network interfaces */
foreach (get_interface_list() as $key => $item) {
$interfaces[$key] = ['descr' => $key . ' (' . $item['mac'] . ')', 'type' => 'hardware'];
}
/* add virtual network interfaces */
foreach ($devices as $device) {
if (!empty($device['names'])) {
foreach ($device['names'] as $key => $values) {
if (!empty($values)) {
$interfaces[$key] = $values;
$interfaces[$key]['type'] = $device['type'];
}
}
}
}
/* enforce constraints */
foreach ($interfaces as $id => $details) {
if (!empty($details['exclude'])) {
foreach ($details['exclude'] as $device) {
if (isset($interfaces[$device])) {
unset($interfaces[$device]);
}
}
}
}
return $interfaces;
}
$a_devices = plugins_devices();
$interfaces = list_devices($a_devices);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input_errors = [];
if (isset($_POST['add_x']) && isset($_POST['if_add'])) {
foreach (legacy_config_get_interfaces() as $ifname => $ifdata) {
/* if interface is already used redirect */
if ($ifdata['if'] == $_POST['if_add']) {
header(url_safe('Location: /interfaces_assign.php'));
exit;
}
}
$is_ppp = false;
foreach (config_read_array('ppps', 'ppp') as $ppp) {
if ($ppp['if'] = $_POST['if_add']) {
$is_ppp = true;
break;
}
}
if (!$is_ppp && !does_interface_exist($_POST['if_add'])) {
$input_errors[] = sprintf(gettext('The interface "%s" does not exist. Make sure to apply its configuration first.'), $_POST['if_add']);
}
if (count($input_errors) == 0) {
/* find next free optional interface number */
for ($i = 1; $i <= count($config['interfaces']); $i++) {
if (empty($config['interfaces']["opt{$i}"])) {
break;
}
}
$newifname = 'opt' . $i;
$descr = !empty($_POST['new_entry_descr']) ? $_POST['new_entry_descr'] : 'OPT' . $i;
$config['interfaces'][$newifname] = array();
$config['interfaces'][$newifname]['descr'] = preg_replace('/[^a-z_0-9]/i', '', $descr);
$config['interfaces'][$newifname]['if'] = $_POST['if_add'];
switch ($interfaces[$_POST['if_add']]['type']) {
case 'ppp':
$config['interfaces'][$newifname]['ipaddr'] = $interfaces[$_POST['if_add']]['ipaddr'];
break;
case 'wlan':
$config['interfaces'][$newifname]['wireless'] = [];
interface_sync_wireless_clones($config['interfaces'][$newifname], false);
break;
default:
break;
}
write_config();
header(url_safe('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");
} elseif (link_interface_to_bridge($id)) {
$input_errors[] = gettext("The interface is part of a bridge. Please remove it from the bridge to continue");
} elseif (link_interface_to_gre($id)) {
$input_errors[] = gettext("The interface is part of a gre tunnel. Please delete the tunnel to continue");
} elseif (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']);
interface_reset($id);
if (isset($config['dhcpd'][$id])) {
unset($config['dhcpd'][$id]);
plugins_configure('dhcp', false, array('inet'));
}
if (isset($config['dhcpdv6'][$id])) {
unset($config['dhcpdv6'][$id]);
plugins_configure('dhcp', false, array('inet6'));
}
if (isset($config['filter']['rule'])) {
foreach ($config['filter']['rule'] as $x => $rule) {
/* XXX this doesn't match floating rules with multiple values */
if (isset($rule['interface']) && $rule['interface'] == $id) {
unset($config['filter']['rule'][$x]);
}
}
}
if (isset($config['nat']['rule'])) {
foreach ($config['nat']['rule'] as $x => $rule) {
if ($rule['interface'] == $id) {
unset($config['nat']['rule'][$x]['interface']);
}
}
}
unset($config['interfaces'][$id]);
write_config();
header(url_safe('Location: /interfaces_assign.php'));
exit;
}
} elseif (isset($_POST['Submit'])) { // ** Change interface **
/* build a list of the device names so we can see how the interfaces map as submitted */
$dev_if_map = [];
foreach ($interfaces as $device => $unused) {
$dev_if_map[$device] = [];
}
foreach ($_POST as $ifname => $ifdev) {
if ($ifname == 'lan' || $ifname == 'wan' || substr($ifname, 0, 3) == 'opt') {
$dev_if_map[$ifdev][] = $ifname;
}
}
/* check the requested configuration for consistency */
foreach ($dev_if_map as $devname => $ifnames) {
if (count($ifnames) > 1) {
$input_errors[] = sprintf(gettext('Device %s was assigned to %d interfaces: %s'), $devname, count($ifnames), implode(', ', $dev_if_map[$devname]));
} elseif (count($ifnames) == 1 && preg_match('/^bridge[0-9]/', $devname)) {
foreach (interface_parent_devices($devname, true) as $member) {
if ($member == $ifnames[0]) {
$input_errors[] = sprintf(gettext('You cannot set device %s to interface %s because it cannot be a member of itself.'), $devname, $member);
break;
}
}
}
}
/* prevent save when interface doesn't exist in case the user was pointed here after a config import */
foreach ($_POST as $ifname => $ifdev) {
if ($ifname == 'lan' || $ifname == 'wan' || substr($ifname, 0, 3) == 'opt') {
if (!empty($ifdev) && !isset($interfaces[$ifdev])) {
$input_errors[] = sprintf(gettext("Device %s does not exist."), $ifdev);
}
}
}
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) {
$changes = 0;
foreach ($_POST as $ifname => $ifdev) {
if (!is_array($ifdev) && ($ifname == 'lan' || $ifname == 'wan' || substr($ifname, 0, 3) == 'opt')) {
$reloadif = false;
if (!empty($config['interfaces'][$ifname]['if']) && $config['interfaces'][$ifname]['if'] != $ifdev) {
interface_reset($ifname);
$reloadif = true;
}
$config['interfaces'][$ifname]['if'] = $ifdev;
switch ($interfaces[$ifdev]['type']) {
case 'ppp':
$config['interfaces'][$ifname]['ipaddr'] = $interfaces[$ifdev]['ipaddr'];
break;
case 'wlan':
if (strpos($config['interfaces'][$ifname]['if'], '_wlan') === false) {
/* change from implied clone to explicit */
$config['interfaces'][$ifname]['if'] .= '_wlan0';
}
break;
default:
break;
}
foreach ($a_devices as $device) {
if ($device['configurable']) {
continue;
}
if (preg_match('/' . $device['pattern'] . '/', $ifdev)) {
unset($config['interfaces'][$ifname]['ipaddr']);
unset($config['interfaces'][$ifname]['subnet']);
unset($config['interfaces'][$ifname]['ipaddrv6']);
unset($config['interfaces'][$ifname]['subnetv6']);
}
}
/* set or clear wireless configuration */
if ($interfaces[$ifdev]['type'] == 'wlan') {
config_read_array('interfaces', $ifname, 'wireless');
} elseif (isset($config['interfaces'][$ifname]['wireless'])) {
unset($config['interfaces'][$ifname]['wireless']);
}
/* make sure there is a descr for all interfaces */
if (!isset($config['interfaces'][$ifname]['descr'])) {
$config['interfaces'][$ifname]['descr'] = strtoupper($ifname);
}
if ($reloadif) {
if (isset($config['interfaces'][$ifname]['wireless'])) {
interface_sync_wireless_clones($config['interfaces'][$ifname], false);
}
/* Reload all for the interface. */
interface_configure(false, $ifname, true);
// count changes
$changes++;
}
}
}
/* XXX huh? */
write_config();
if ($changes > 0) {
filter_configure();
}
header(url_safe('Location: /interfaces_assign.php'));
exit;
}
}
}
legacy_html_escape_form_data($interfaces);
$unused_interfaces= array();
$all_interfaces = legacy_config_get_interfaces();
$ifdetails = legacy_interfaces_details();
$intfkeys = array_keys($interfaces);
natcasesort($intfkeys);
foreach ($intfkeys as $devname) {
$devused = false;
if (!empty($ifdetails[$devname]) && !empty($ifdetails[$devname]['status'])) {
$interfaces[$devname]['status'] = $ifdetails[$devname]['status'];
} elseif (empty($ifdetails[$devname])) {
$interfaces[$devname]['status'] = 'no carrier';
} else {
/* quirky value to populate status key for virtual interfaces */
$interfaces[$devname]['status'] = 'likely up';
}
foreach ($all_interfaces as $ifname => $ifdata) {
if ($ifdata['if'] == $devname) {
$devused = true;
break;
}
}
if (!$devused) {
$unused_interfaces[$devname] = $interfaces[$devname];
}
}
/* add missing interfaces so people know what to change */
foreach (legacy_config_get_interfaces(array("virtual" => false)) as $iface) {
if (!empty($iface['if']) && !isset($interfaces[$iface['if']])) {
$interfaces[$iface['if']] = [
'descr' => sprintf('%s (%s)', $iface['if'], 'missing'),
'status' => 'missing'
];
}
}
include("head.inc");
?>
<body>
<script>
$( document ).ready(function() {
// link delete buttons
$(".act_delete").click(function(event){
event.preventDefault();
var id = $(this).data("id");
// delete single
BootstrapDialog.show({
type:BootstrapDialog.TYPE_DANGER,
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()
}
}]
});
});
$("#if_add").change(function(event){
event.preventDefault();
let descr = $("#if_add option:selected").data('ifdescr');
if (descr) {
$("#new_entry_descr").val(descr);
}
});
});
</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);
}?>
<section class="col-xs-12">
<form method="post" name="iform" id="iform">
<div class="tab-content content-box col-xs-12 __mb">
<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 table-condensed table-hover">
<thead>
<tr>
<th><?= gettext('Interface') ?></th>
<th><?= gettext('Identifier') ?>
<span data-toggle="tooltip" title="<?=gettext("Technical identifier of the interface, used by hasync for example");?>">
<i style="cursor: pointer;" class="fa fa-question-circle" data-toggle="collapse" ></i>
</span>
</th>
<th><?= gettext('Device') ?></th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach (legacy_config_get_interfaces(['virtual' => false]) as $ifname => $iface): ?>
<?= legacy_html_escape_form_data($iface) ?>
<tr>
<td>
<a href="/interfaces.php?if=<?= html_safe($ifname) ?>">[<?= $iface['descr'] ?>]</a>
</td>
<td><?= $ifname ?></td>
<td>
<select name="<?=$ifname;?>" id="<?=$ifname;?>" class="selectpicker" data-size="10">
<?php foreach ($interfaces as $device => $info): ?>
<option data-icon="fa fa-plug <?=in_array($info['status'], ['no carrier', 'missing']) ? "text-danger": "text-success";?>"
value="<?=$device;?>" <?= $device == $iface['if'] ? " selected=\"selected\"" : "";?>>
<?=$info['descr'];?>
</option>
<?php endforeach ?>
</select>
</td>
<td>
<?php if (empty($iface['lock'])): ?>
<button title="<?= html_safe(gettext('Delete')) ?>" data-toggle="tooltip" data-id="<?=$ifname;?>" class="btn btn-default act_delete" type="submit">
<i class="fa fa-trash fa-fw"></i>
</button>
<?php endif ?>
</td>
</tr>
<?php endforeach ?>
</tbody>
<tfoot>
<tr>
<td colspan="2"></td>
<td><button name="Submit" type="submit" class="btn btn-primary" value="yes"><?= gettext('Save') ?></button></td>
<td></td>
</tr>
</tfoot>
</table>
</div>
</div>
<div class="tab-content content-box col-xs-12">
<div class="table-responsive">
<table class="table table-striped table-condensed">
<?php if (count($unused_interfaces)): ?>
<thead>
<tr>
<th colspan="2"><i class="fa fa-plus fa-fw"></i> <?= gettext('Assign a new interface') ?></th>
</tr>
</thead>
<tbody>
<tr>
<th><?= gettext('Device') ?></td>
<td>
<select name="if_add" id="if_add" class="selectpicker" data-size="10">
<?php foreach ($unused_interfaces as $device => $info): ?>
<option data-icon="fa fa-plug <?=$info['status'] == 'no carrier' ? "text-danger": "text-success";?>"
data-ifdescr="<?=!empty($info['ifdescr']) ? $info['ifdescr'] : '';?>"
value="<?=$device;?>">
<?=$info['descr'];?>
</option>
<?php endforeach ?>
</select>
</td>
</tr>
<tr>
<th><?= gettext('Description') ?></th>
<td>
<input id="new_entry_descr" name="new_entry_descr" type="text" class="form-control">
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
<td>
<button name="add_x" type="submit" class="btn btn-primary"><?= gettext('Add') ?></button>
</td>
</tr>
</tfoot>
<?php else: ?>
<thead>
<tr>
<th colspan="2"><i class="fa fa-minus fa-fw"></i> <?= gettext('No devices available for assignment') ?></th>
</tr>
</thead>
<?php endif ?>
</table>
</div>
</div>
</form>
</section>
</div>
</div>
</section>
<?php include("foot.inc");