]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/zoneconf.cgi
3b711d5209a37721d53aa7aff313a86d46a6ebea
2 ###############################################################################
4 # VLAN Management for IPFire #
5 # Copyright (C) 2019 Florian Bührle <fbuehrle@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
23 use Scalar
::Util
qw(looks_like_number);
25 require '/var/ipfire/general-functions.pl';
26 require "${General::swroot}/lang.pl";
27 require "${General::swroot}/header.pl";
47 border: 0.5px solid black;
51 border-collapse: collapse;
55 background-color: grey;
61 background-color: $Header::colourgreen;
65 background-color: $Header::colourred;
69 background-color: $Header::colourblue;
73 background-color: $Header::colourorange;
77 background-color: white;
78 border-top-style: none;
79 border-left-style: none;
83 background-color: #cccccc;
96 #submit-container.input {
108 my %ethsettings = ();
109 my %vlansettings = ();
112 &General
::readhash
("${General::swroot}/ethernet/settings",\
%ethsettings);
113 &General
::readhash
("${General::swroot}/ethernet/vlans",\
%vlansettings);
115 &Header
::getcgihash
(\
%cgiparams);
116 &Header
::showhttpheaders
();
118 # Define all zones we will check for NIC assignment
119 my @zones = ("green", "red", "orange", "blue");
121 # Get all physical NICs present
122 opendir(my $dh, "/sys/class/net/");
125 while (my $nic = readdir($dh)) {
126 if (-e
"/sys/class/net/$nic/device") { # Indicates that the NIC is physical
127 push(@nics, [&Network
::get_nic_property
($nic, "address"), $nic, 0]);
133 @nics = sort {$a->[0] cmp $b->[0]} @nics; # Sort nics by their MAC address
135 # Name the physical NICs
136 # Even though they may not be really named like this, we will name them ethX or wlanX
143 if (-e
"/sys/class/net/$nic/wireless") {
144 $_->[1] = "wlan$wlancount";
148 $_->[1] = "eth$ethcount";
153 &Header
::openpage
($Lang::tr
{"zoneconf title"}, 1, $css);
154 &Header
::openbigbox
('100%', 'center');
156 ### Evaluate POST parameters ###
158 if ($cgiparams{"ACTION"} eq $Lang::tr
{"save"}) {
159 my %VALIDATE_nic_check = ();
160 my $VALIDATE_error = "";
164 my $slave_string = "";
165 my $zone_mode = $cgiparams{"MODE $uc"};
166 my $VALIDATE_vlancount = 0;
168 $ethsettings{"${uc}_MACADDR"} = "";
169 $ethsettings{"${uc}_MODE"} = "";
170 $ethsettings{"${uc}_SLAVES"} = "";
171 $vlansettings{"${uc}_PARENT_DEV"} = "";
172 $vlansettings{"${uc}_VLAN_ID"} = "";
173 $vlansettings{"${uc}_MAC_ADDRESS"} = "";
175 # If RED is not in DHCP or static mode, we only set its MACADDR property
176 if ($uc eq "RED" && ! $cgiparams{"PPPACCESS"} eq "") {
180 if ($mac eq $cgiparams{"PPPACCESS"}) {
181 $ethsettings{"${uc}_MACADDR"} = $mac;
183 # Check if this interface is already accessed by any other zone
184 # If this is the case, show an error message
185 if ($VALIDATE_nic_check{"ACC $mac"}) {
186 $VALIDATE_error = $Lang::tr
{"zoneconf val ppp assignment error"};
189 $VALIDATE_nic_check{"RESTRICT $mac"} = 1;
199 my $nic_access = $cgiparams{"ACCESS $uc $mac"};
201 if (! ($nic_access eq "NONE")) {
202 if ($VALIDATE_nic_check{"RESTRICT $mac"}) { # If this interface is already assigned to RED in PPP mode, throw an error
203 $VALIDATE_error = $Lang::tr
{"zoneconf val ppp assignment error"};
207 $VALIDATE_nic_check{"ACC $mac"} = 1;
210 if ($nic_access eq "NATIVE") {
211 if ($VALIDATE_nic_check{"NATIVE $mac"}) {
212 $VALIDATE_error = $Lang::tr
{"zoneconf val native assignment error"};
216 $VALIDATE_nic_check{"NATIVE $mac"} = 1;
218 if ($zone_mode eq "BRIDGE") {
219 $slave_string = "${slave_string}${mac} ";
221 $ethsettings{"${uc}_MACADDR"} = $mac;
223 } elsif ($nic_access eq "VLAN") {
224 my $vlan_tag = $cgiparams{"TAG $uc $mac"};
226 if ($VALIDATE_nic_check{"VLAN $mac $vlan_tag"}) {
227 $VALIDATE_error = $Lang::tr
{"zoneconf val vlan tag assignment error"};
231 $VALIDATE_nic_check{"VLAN $mac $vlan_tag"} = 1;
233 if (! looks_like_number
($vlan_tag)) {
236 if ($vlan_tag < 1 || $vlan_tag > 4095) {
240 my $rnd_mac = &Network
::random_mac
();
242 $vlansettings{"${uc}_PARENT_DEV"} = $mac;
243 $vlansettings{"${uc}_VLAN_ID"} = $vlan_tag;
244 $vlansettings{"${uc}_MAC_ADDRESS"} = $rnd_mac;
246 if ($zone_mode eq "BRIDGE") {
247 $slave_string = "${slave_string}${rnd_mac} ";
250 $VALIDATE_vlancount++; # We can't allow more than one VLAN per zone
254 if ($VALIDATE_vlancount > 1) {
255 $VALIDATE_error = $Lang::tr
{"zoneconf val vlan amount assignment error"};
261 if ($zone_mode eq "BRIDGE") {
262 $ethsettings{"${uc}_MODE"} = "bridge";
263 $ethsettings{"${uc}_SLAVES"} = $slave_string;
264 } elsif ($zone_mode eq "MACVTAP") {
265 $ethsettings{"${uc}_MODE"} = "macvtap";
269 if ($VALIDATE_error) {
270 &Header
::openbox
('100%', 'left', $Lang::tr
{"error"});
272 print "$VALIDATE_error<br><a href='/cgi-bin/zoneconf.cgi'><button>$Lang::tr{'ok'}</button></a>";
275 &Header
::closebigbox
();
276 &Header
::closepage
();
281 &General
::writehash
("${General::swroot}/ethernet/settings",\
%ethsettings);
282 &General
::writehash
("${General::swroot}/ethernet/vlans",\
%vlansettings);
285 &Header
::openbox
('100%', 'left', $Lang::tr
{"zoneconf nic assignment"});
287 ### START OF TABLE ###
290 <form method='post' enctype='multipart/form-data'>
293 <td class="h topleft" /td>
297 # Fill the table header with all physical NICs
302 print "<td class='h textcenter'>$nic<br>$mac</td>";
311 my $dev_name = $ethsettings{"${uc}_DEV"};
313 if ($dev_name eq "") { # If the zone is not activated, color it light grey
314 print "<td class='h disabled'>$uc</td>";
317 print "<td class='disabled'/>";
325 my $red_type = $ethsettings{"RED_TYPE"};
326 my $red_restricted = ($uc eq "RED" && ! ($red_type eq "STATIC" || $red_type eq "DHCP"));
328 # VLANs/Bridging is not possible if the RED interface is set to PPP, PPPoE, VDSL, ...
329 if ($red_restricted) {
330 print "<td class='h $_'>$uc<br>($red_type)</td>";
336 if ($mac eq $ethsettings{"${uc}_MACADDR"}) {
337 $checked = "checked";
340 print "<td class='textcenter'><input type='radio' id='PPPACCESS $mac' name='PPPACCESS' value='$mac' $checked></td>";
344 next; # We're done here
348 my %mode_selected = ();
349 my $zone_mode = $ethsettings{"${uc}_MODE"};
351 if ($zone_mode eq "") {
352 $mode_selected{"DEFAULT"} = "selected";
353 } elsif ($zone_mode eq "bridge") {
354 $mode_selected{"BRIDGE"} = "selected";
355 } elsif ($zone_mode eq "macvtap") {
356 $mode_selected{"MACVTAP"} = "selected";
360 <td class='h $_'>$uc<br>
361 <select name="MODE $uc">
362 <option value="DEFAULT" $mode_selected{"DEFAULT"}>$Lang::tr{"zoneconf nicmode default"}</option>
363 <option value="BRIDGE" $mode_selected{"BRIDGE"}>$Lang::tr{"zoneconf nicmode bridge"}</option>
364 <option value="MACVTAP" $mode_selected{"MACVTAP"}>$Lang::tr{"zoneconf nicmode macvtap"}</option>
370 # ZONE_PARENT_DEV is set if this zone accesses any interface via a VLAN
371 my $zone_parent_dev = $vlansettings{"${uc}_PARENT_DEV"};
373 # 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
374 $zone_parent_dev = &Network
::get_mac_by_name
($zone_parent_dev);
376 foreach (@nics) { # Check for all nics if they are assigned to the current zone
377 my %access_selected = ();
380 my $field_disabled = "disabled"; # Only enable the VLAN ID input field if the current access mode is VLAN
381 my $zone_vlan_id = "";
383 # If the current NIC is accessed by the current zone via a VLAN, the ZONE_PARENT_DEV option corresponds to the current NIC
384 if ($mac eq $zone_parent_dev) {
385 $access_selected{"VLAN"} = "selected";
386 $field_disabled = "";
387 $zone_vlan_id = $vlansettings{"${uc}_VLAN_ID"};
390 # If the current zone is in bridge mode, all corresponding NICs (Native as well as VLAN) are set via the ZONE_SLAVES option
391 if ($zone_mode eq "bridge") {
392 my @slaves = split(/ /, $ethsettings{"${uc}_SLAVES"});
395 # Slaves can be set to a NICs name so we have to find out its MAC address
396 $_ = &Network
::get_mac_by_name
($_);
399 $access_selected{"NATIVE"} = "selected";
403 } 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
404 if ($mac eq $ethsettings{"${uc}_MACADDR"}) {
405 $access_selected{"NATIVE"} = "selected";
409 $access_selected{"NONE"} = ($access_selected{"NATIVE"} eq "") && ($access_selected{"VLAN"} eq "") ?
"selected" : "";
410 my $vlan_disabled = ($wlan) ?
"disabled" : "";
413 <td class="textcenter">
414 <select name="ACCESS $uc $mac" onchange="document.getElementById('TAG $uc $mac').disabled = (this.value === 'VLAN' ? false : true)">
415 <option value="NONE" $access_selected{"NONE"}>- $Lang::tr{"zoneconf access none"} -</option>
416 <option value="NATIVE" $access_selected{"NATIVE"}>$Lang::tr{"zoneconf access native"}</option>
417 <option value="VLAN" $access_selected{"VLAN"} $vlan_disabled>$Lang::tr{"zoneconf access vlan"}</option>
419 <input type="number" id="TAG $uc $mac" name="TAG $uc $mac" min="1" max="4095" value="$zone_vlan_id" $field_disabled>
431 <div id="submit-container">
432 <input type="submit" name="ACTION" value="$Lang::tr{"save"}">
441 &Header
::closebigbox
();
442 &Header
::closepage
();