]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/zoneconf.cgi
zoneconf: Move "None" option to the top
[ipfire-2.x.git] / html / cgi-bin / zoneconf.cgi
CommitLineData
1dcf513a
FB
1#!/usr/bin/perl
2###############################################################################
3# #
4# VLAN Management for IPFire #
5# Copyright (C) 2019 Florian Bührle <fbuehrle@ipfire.org> #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22use strict;
23use Scalar::Util qw(looks_like_number);
24
25require '/var/ipfire/general-functions.pl';
26require "${General::swroot}/lang.pl";
27require "${General::swroot}/header.pl";
28
29my $css = <<END
30<style>
31 table {
32 width: 100%;
33 }
34
35 tr {
36 height: 4em;
37 }
38
39 td:first-child {
40 width: 1px;
41 }
42
43 td {
44 padding: 5px;
45 padding-left: 10px;
46 padding-right: 10px;
47 border: 0.5px solid black;
48 }
49
50 table {
51 border-collapse: collapse;
52 }
53
54 td.h {
55 background-color: grey;
56 color: white;
57 font-weight: 800;
58 }
59
60 td.green {
61 background-color: $Header::colourgreen;
62 }
63
64 td.red {
65 background-color: $Header::colourred;
66 }
67
68 td.blue {
69 background-color: $Header::colourblue;
70 }
71
72 td.orange {
73 background-color: $Header::colourorange;
74 }
75
76 td.topleft {
77 background-color: white;
78 border-top-style: none;
79 border-left-style: none;
80 }
81
82 td.disabled {
83 background-color: #cccccc;
84 }
85
86 td.textcenter {
87 text-align: center;
88 }
89
90 #submit-container {
91 display: flex;
92 width: 100%;
93 justify-content: space-between;
94 padding-top: 20px;
95 text-align: left;
96 }
97
98 #submit-container.input {
99 margin-left: auto;
100 }
101
102 button {
103 margin-top: 1em;
104 }
105
106</style>
107END
108;
109
110my %ethsettings = ();
111my %vlansettings = ();
112my %cgiparams = ();
113
114&General::readhash("${General::swroot}/ethernet/settings",\%ethsettings);
115&General::readhash("${General::swroot}/ethernet/vlans",\%vlansettings);
116
117&Header::getcgihash(\%cgiparams);
118&Header::showhttpheaders();
119
120# Define all zones we will check for NIC assignment
121my @zones = ("green", "red", "orange", "blue");
122
123# Get all physical NICs present
124opendir(my $dh, "/sys/class/net/");
125my @nics = ();
126
127while (my $nic = readdir($dh)) {
128 if (-e "/sys/class/net/$nic/device") { # Indicates that the NIC is physical
129 push(@nics, [&Network::get_nic_property($nic, "address"), $nic, 0]);
130 }
131}
132
133closedir($dh);
134
135@nics = sort {$a->[0] cmp $b->[0]} @nics; # Sort nics by their MAC address
136
137# Name the physical NICs
138# Even though they may not be really named like this, we will name them ethX or wlanX
139my $ethcount = 0;
140my $wlancount = 0;
141
142foreach (@nics) {
143 my $nic = $_->[1];
144
145 if (-e "/sys/class/net/$nic/wireless") {
146 $_->[1] = "wlan$wlancount";
147 $_->[2] = 1;
148 $wlancount++;
149 } else {
150 $_->[1] = "eth$ethcount";
151 $ethcount++;
152 }
153}
154
155&Header::openpage($Lang::tr{"zoneconf title"}, 1, $css);
156&Header::openbigbox('100%', 'center');
157
158### Evaluate POST parameters ###
159
160if ($cgiparams{"ACTION"} eq $Lang::tr{"save"}) {
161 my %VALIDATE_nic_check = ();
162 my $VALIDATE_error = "";
163
164 foreach (@zones) {
165 my $uc = uc $_;
166 my $slave_string = "";
167 my $zone_mode = $cgiparams{"MODE $uc"};
168 my $VALIDATE_vlancount = 0;
169
170 $ethsettings{"${uc}_MACADDR"} = "";
171 $ethsettings{"${uc}_MODE"} = "";
172 $ethsettings{"${uc}_SLAVES"} = "";
173 $vlansettings{"${uc}_PARENT_DEV"} = "";
174 $vlansettings{"${uc}_VLAN_ID"} = "";
175 $vlansettings{"${uc}_MAC_ADDRESS"} = "";
176
177 # If RED is not in DHCP or static mode, we only set its MACADDR property
178 if ($uc eq "RED" && ! $cgiparams{"PPPACCESS"} eq "") {
179 foreach (@nics) {
180 my $mac = $_->[0];
181
182 if ($mac eq $cgiparams{"PPPACCESS"}) {
183 $ethsettings{"${uc}_MACADDR"} = $mac;
184
185 # Check if this interface is already accessed by any other zone
186 # If this is the case, show an error message
187 if ($VALIDATE_nic_check{"ACC $mac"}) {
188 $VALIDATE_error = $Lang::tr{"zoneconf val ppp assignment error"};
189 }
190
191 $VALIDATE_nic_check{"RESTRICT $mac"} = 1;
192 last;
193 }
194 }
195
196 next;
197 }
198
199 foreach (@nics) {
200 my $mac = $_->[0];
201 my $nic_access = $cgiparams{"ACCESS $uc $mac"};
202
203 if (! ($nic_access eq "NONE")) {
204 if ($VALIDATE_nic_check{"RESTRICT $mac"}) { # If this interface is already assigned to RED in PPP mode, throw an error
205 $VALIDATE_error = $Lang::tr{"zoneconf val ppp assignment error"};
206 next;
207 }
208
209 $VALIDATE_nic_check{"ACC $mac"} = 1;
210 }
211
212 if ($nic_access eq "NATIVE") {
213 if ($VALIDATE_nic_check{"NATIVE $mac"}) {
214 $VALIDATE_error = $Lang::tr{"zoneconf val native assignment error"};
215 next;
216 }
217
218 $VALIDATE_nic_check{"NATIVE $mac"} = 1;
219
220 if ($zone_mode eq "BRIDGE") {
221 $slave_string = "${slave_string}${mac} ";
222 } else {
223 $ethsettings{"${uc}_MACADDR"} = $mac;
224 }
225 } elsif ($nic_access eq "VLAN") {
226 my $vlan_tag = $cgiparams{"TAG $uc $mac"};
227
228 if ($VALIDATE_nic_check{"VLAN $mac $vlan_tag"}) {
229 $VALIDATE_error = $Lang::tr{"zoneconf val vlan tag assignment error"};
230 next;
231 }
232
233 $VALIDATE_nic_check{"VLAN $mac $vlan_tag"} = 1;
234
235 if (! looks_like_number($vlan_tag)) {
236 next;
237 }
238 if ($vlan_tag < 1 || $vlan_tag > 4095) {
239 next;
240 }
241
242 my $rnd_mac = &Network::random_mac();
243
244 $vlansettings{"${uc}_PARENT_DEV"} = $mac;
245 $vlansettings{"${uc}_VLAN_ID"} = $vlan_tag;
246 $vlansettings{"${uc}_MAC_ADDRESS"} = $rnd_mac;
247
248 if ($zone_mode eq "BRIDGE") {
249 $slave_string = "${slave_string}${rnd_mac} ";
250 }
251
252 $VALIDATE_vlancount++; # We can't allow more than one VLAN per zone
253 }
254 }
255
256 if ($VALIDATE_vlancount > 1) {
257 $VALIDATE_error = $Lang::tr{"zoneconf val vlan amount assignment error"};
258 next;
259 }
260
261 chop($slave_string);
262
263 if ($zone_mode eq "BRIDGE") {
264 $ethsettings{"${uc}_MODE"} = "bridge";
265 $ethsettings{"${uc}_SLAVES"} = $slave_string;
266 } elsif ($zone_mode eq "MACVTAP") {
267 $ethsettings{"${uc}_MODE"} = "macvtap";
268 }
269 }
270
271 if ($VALIDATE_error) {
272 &Header::openbox('100%', 'left', $Lang::tr{"error"});
273
274 print "$VALIDATE_error<br><a href='/cgi-bin/zoneconf.cgi'><button>$Lang::tr{'ok'}</button></a>";
275
276 &Header::closebox();
277 &Header::closebigbox();
278 &Header::closepage();
279
280 exit 0;
281 }
282
283 &General::writehash("${General::swroot}/ethernet/settings",\%ethsettings);
284 &General::writehash("${General::swroot}/ethernet/vlans",\%vlansettings);
285}
286
287&Header::openbox('100%', 'left', $Lang::tr{"zoneconf nic assignment"});
288
289### START OF TABLE ###
290
291print <<END
292 <form method='post' enctype='multipart/form-data'>
293 <table>
294 <tr>
295 <td class="h topleft" /td>
296END
297;
298
299# Fill the table header with all physical NICs
300foreach (@nics) {
301 my $mac = $_->[0];
302 my $nic = $_->[1];
303
304 print "<td class='h textcenter'>$nic<br>$mac</td>";
305}
306
307print "</tr>";
308
309foreach (@zones) {
310 print "<tr>";
311 my $uc = uc $_;
312
313 my $dev_name = $ethsettings{"${uc}_DEV"};
314
315 if ($dev_name eq "") { # If the zone is not activated, color it light grey
316 print "<td class='h disabled'>$uc</td>";
317
318 foreach (@nics) {
319 print "<td class='disabled'/>";
320 }
321
322 print "</tr>";
323 next;
324 }
325
326 if ($uc eq "RED") {
327 my $red_type = $ethsettings{"RED_TYPE"};
328 my $red_restricted = ($uc eq "RED" && ! ($red_type eq "STATIC" || $red_type eq "DHCP"));
329
330 # VLANs/Bridging is not possible if the RED interface is set to PPP, PPPoE, VDSL, ...
331 if ($red_restricted) {
332 print "<td class='h $_'>$uc<br>($red_type)</td>";
333
334 foreach (@nics) {
335 my $mac = $_->[0];
336 my $checked = "";
337
338 if ($mac eq $ethsettings{"${uc}_MACADDR"}) {
339 $checked = "checked";
340 }
341
342 print "<td class='textcenter'><input type='radio' id='PPPACCESS $mac' name='PPPACCESS' value='$mac' $checked></td>";
343 }
344
345 print "</tr>";
346 next; # We're done here
347 }
348 }
349
350 my %mode_selected = ();
351 my $zone_mode = $ethsettings{"${uc}_MODE"};
352
353 if ($zone_mode eq "") {
354 $mode_selected{"DEFAULT"} = "selected";
355 } elsif ($zone_mode eq "bridge") {
356 $mode_selected{"BRIDGE"} = "selected";
357 } elsif ($zone_mode eq "macvtap") {
358 $mode_selected{"MACVTAP"} = "selected";
359 }
360
361 print <<END
362 <td class='h $_'>$uc<br>
363 <select name="MODE $uc">
364 <option value="DEFAULT" $mode_selected{"DEFAULT"}>$Lang::tr{"zoneconf nicmode default"}</option>
365 <option value="BRIDGE" $mode_selected{"BRIDGE"}>$Lang::tr{"zoneconf nicmode bridge"}</option>
366 <option value="MACVTAP" $mode_selected{"MACVTAP"}>$Lang::tr{"zoneconf nicmode macvtap"}</option>
367 </select>
368 </td>
369END
370;
371
372 # ZONE_PARENT_DEV is set if this zone accesses any interface via a VLAN
373 my $zone_parent_dev = $vlansettings{"${uc}_PARENT_DEV"};
374
375 # If ZONE_PARENT_DEV is set to a NICs name (e.g. green0 or eth0) instead of a MAC address, we have to find out this NICs MAC address
376 $zone_parent_dev = &Network::get_mac_by_name($zone_parent_dev);
377
378 foreach (@nics) { # Check for all nics if they are assigned to the current zone
379 my %access_selected = ();
380 my $mac = $_->[0];
381 my $wlan = $_->[2];
382 my $field_disabled = "disabled"; # Only enable the VLAN ID input field if the current access mode is VLAN
383 my $zone_vlan_id = "";
384
385 # If the current NIC is accessed by the current zone via a VLAN, the ZONE_PARENT_DEV option corresponds to the current NIC
386 if ($mac eq $zone_parent_dev) {
387 $access_selected{"VLAN"} = "selected";
388 $field_disabled = "";
389 $zone_vlan_id = $vlansettings{"${uc}_VLAN_ID"};
390 }
391
392 # If the current zone is in bridge mode, all corresponding NICs (Native as well as VLAN) are set via the ZONE_SLAVES option
393 if ($zone_mode eq "bridge") {
394 my @slaves = split(/ /, $ethsettings{"${uc}_SLAVES"});
395
396 foreach (@slaves) {
397 # Slaves can be set to a NICs name so we have to find out its MAC address
398 $_ = &Network::get_mac_by_name($_);
399
400 if ($_ eq $mac) {
401 $access_selected{"NATIVE"} = "selected";
402 last;
403 }
404 }
405 } else { # Native access via ZONE_MACADDR is only set if the zone does not access a NIC via a VLAN and the zone is not in bridge mode
406 if ($mac eq $ethsettings{"${uc}_MACADDR"}) {
407 $access_selected{"NATIVE"} = "selected";
408 }
409 }
410
411 $access_selected{"NONE"} = ($access_selected{"NATIVE"} eq "") && ($access_selected{"VLAN"} eq "") ? "selected" : "";
412 my $vlan_disabled = ($wlan) ? "disabled" : "";
413
414 print <<END
415 <td class="textcenter">
416 <select name="ACCESS $uc $mac" onchange="document.getElementById('TAG $uc $mac').disabled = (this.value === 'VLAN' ? false : true)">
03fd6855 417 <option value="NONE" $access_selected{"NONE"}>- $Lang::tr{"zoneconf access none"} -</option>
1dcf513a
FB
418 <option value="NATIVE" $access_selected{"NATIVE"}>$Lang::tr{"zoneconf access native"}</option>
419 <option value="VLAN" $access_selected{"VLAN"} $vlan_disabled>$Lang::tr{"zoneconf access vlan"}</option>
1dcf513a
FB
420 </select>
421 <input type="number" id="TAG $uc $mac" name="TAG $uc $mac" min="1" max="4095" value="$zone_vlan_id" $field_disabled>
422 </td>
423END
424;
425
426 }
427 print "</tr>";
428}
429
430print <<END
431 </table>
432 <div id="submit-container">
433 <font color="red">$Lang::tr{"zoneconf warning incorrect configuration"}</font>
434 <input type="submit" name="ACTION" value="$Lang::tr{"save"}">
435 </div>
436 </form>
437END
438;
439
440### END OF TABLE ###
441
442&Header::closebox();
443&Header::closebigbox();
444&Header::closepage();