-
Notifications
You must be signed in to change notification settings - Fork 697
/
VlanSettingsController.php
174 lines (162 loc) · 6.85 KB
/
VlanSettingsController.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
<?php
/*
* Copyright (C) 2022 Deciso B.V.
* 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.
*/
namespace OPNsense\Interfaces\Api;
use OPNsense\Core\Backend;
use OPNsense\Core\Config;
use OPNsense\Base\UserException;
use OPNsense\Base\ApiMutableModelControllerBase;
class VlanSettingsController extends ApiMutableModelControllerBase
{
protected static $internalModelName = 'vlan';
protected static $internalModelClass = 'OPNsense\Interfaces\Vlan';
private function generateVlanIfName($current=null)
{
$tmp = $this->request->getPost("vlan");
$prefix = (strpos($tmp['if'], 'vlan') === false ? "vlan" : "qinq");
if ($current != null && (string)$current->vlanif == "{$tmp['if']}_vlan{$tmp['tag']}") {
// keep legacy naming
return "{$tmp['if']}_vlan{$tmp['tag']}";
} elseif ($current != null && strpos((string)$current->vlanif, '_vlan') === false &&
strpos((string)$current->vlanif, $prefix) === 0
) {
// new naming convention and same type, name stays the same
return (string)$current->vlanif;
} else {
// autonumber new
$ifid = 0;
foreach ($this->getModel()->vlan->iterateItems() as $node) {
if (strpos((string)$node->vlanif . "_", $prefix) === 0) {
$ifid = max($ifid, (int)explode("_", (string)$node->vlanif)[1]);
}
}
return $prefix . "_" . ($ifid+1);
}
}
private function interfaceAssigned($if)
{
$configHandle = Config::getInstance()->object();
if (!empty($configHandle->interfaces)) {
foreach ($configHandle->interfaces->children() as $ifname => $node) {
if ((string)$node->if == $if) {
return true;
}
}
}
return false;
}
public function searchItemAction()
{
return $this->searchBase("vlan", ['vlanif','if','tag','pcp','descr'], "vlanif");
}
public function setItemAction($uuid)
{
$node = $this->getModel()->getNodeByReference('vlan.' . $uuid);
$old_vlanif = $node != null ? (string)$node->vlanif : null;
$new_vlanif = $this->generateVlanIfName($node);
$children = 0;
foreach ($this->getModel()->vlan->iterateItems() as $node) {
if ((string)$node->if == $old_vlanif) {
$children++;
}
}
if ($old_vlanif != null && $old_vlanif != $new_vlanif && $children > 0) {
$result = [
"result" => "failed",
"validations" => [
"vlan.vlanif" => gettext("This VLAN cannot be deleted because it is used in QinQ interfaces.")
]
];
} elseif ($old_vlanif != null && $old_vlanif != $new_vlanif && $this->interfaceAssigned($old_vlanif)) {
// Reassignment is only an issue when naming changes. These additional validations only apply
// for legacy interface nameing (e.g. <interface>_vlan_<tag>) and type changes vlan verses qinq.
$tmp = $this->request->getPost("vlan");
if ($tmp['tag'] != (string)$node->tag) {
$result = [
"result" => "failed",
"validations" => [
"vlan.tag" => gettext("Interface is assigned and you cannot change the VLAN tag while assigned.")
]
];
} else {
$result = [
"result" => "failed",
"validations" => [
"vlan.if" => gettext("Interface is assigned and you cannot change the parent while assigned.")
]
];
}
} else {
$result = $this->setBase("vlan", "vlan", $uuid, ["vlanif" => $new_vlanif]);
// store interface name for apply action
if ($result['result'] != 'failed' && $old_vlanif != $new_vlanif) {
file_put_contents("/tmp/.vlans.removed", "{$old_vlanif}\n", FILE_APPEND | LOCK_EX);
}
}
return $result;
}
public function addItemAction()
{
return $this->addBase("vlan", "vlan", [
"vlanif" => $this->generateVlanIfName()
]);
}
public function getItemAction($uuid = null)
{
return $this->getBase("vlan", "vlan", $uuid);
}
public function delItemAction($uuid)
{
$node = $this->getModel()->getNodeByReference('vlan.' . $uuid);
$old_vlanif = $node != null ? (string)$node->vlanif : null;
$children = 0;
foreach ($this->getModel()->vlan->iterateItems() as $node) {
if ((string)$node->if == $old_vlanif) {
$children++;
}
}
if ($children > 0) {
throw new UserException(gettext("This VLAN cannot be deleted because it is used in QinQ interfaces."));
} elseif ($old_vlanif != null && $this->interfaceAssigned($old_vlanif)) {
throw new UserException(gettext("This VLAN cannot be deleted because it is assigned as an interface."));
} else {
$result = $this->delBase("vlan", $uuid);
// store interface name for apply action
if ($result['result'] != 'failed') {
file_put_contents("/tmp/.vlans.removed", "{$old_vlanif}\n", FILE_APPEND | LOCK_EX);
}
return $result;
}
}
public function reconfigureAction()
{
$result = array("status" => "failed");
if ($this->request->isPost()) {
$result['status'] = strtolower(trim((new Backend())->configdRun('interface vlan configure')));
}
return $result;
}
}