2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2020 IPFire Team <info@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 experimental
'smartmatch';
25 require '/var/ipfire/general-functions.pl';
26 require "${General::swroot}/lang.pl";
27 require "/usr/lib/firewall/firewall-lib.pl";
28 require "${General::swroot}/location-functions.pl";
30 # Set to one to enable debugging mode.
33 my $IPTABLES = "iptables --wait";
37 my $CHAIN_INPUT = "INPUTFW";
38 my $CHAIN_FORWARD = "FORWARDFW";
39 my $CHAIN_OUTPUT = "OUTGOINGFW";
40 my $CHAIN = $CHAIN_FORWARD;
41 my $CHAIN_NAT_SOURCE = "NAT_SOURCE";
42 my $CHAIN_NAT_DESTINATION = "NAT_DESTINATION";
43 my $CHAIN_MANGLE_NAT_DESTINATION_FIX = "NAT_DESTINATION";
44 my @VALID_CHAINS = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT);
45 my @ANY_ADDRESSES = ("0.0.0.0/0.0.0.0", "0.0.0.0/0", "0/0");
47 my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip");
48 my @PROTOCOLS_WITH_PORTS = ("tcp", "udp");
50 my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT");
52 my @PRIVATE_NETWORKS = (
60 my $NAT_MASK = 0x0f000000;
64 my %defaultNetworks=();
68 my %configoutgoingfw=();
70 my %locationsettings = (
71 "LOCATIONBLOCK_ENABLED" => "off"
73 my %loaded_ipset_lists=();
75 my $configfwdfw = "${General::swroot}/firewall/config";
76 my $configinput = "${General::swroot}/firewall/input";
77 my $configoutgoing = "${General::swroot}/firewall/outgoing";
78 my $locationfile = "${General::swroot}/firewall/locationblock";
79 my $configgrp = "${General::swroot}/fwhosts/customgroups";
80 my $netsettings = "${General::swroot}/ethernet/settings";
82 &General
::readhash
("${General::swroot}/firewall/settings", \
%fwdfwsettings);
83 &General
::readhash
("${General::swroot}/optionsfw/settings", \
%fwoptions);
84 &General
::readhash
("$netsettings", \
%defaultNetworks);
85 &General
::readhasharray
($configfwdfw, \
%configfwdfw);
86 &General
::readhasharray
($configinput, \
%configinputfw);
87 &General
::readhasharray
($configoutgoing, \
%configoutgoingfw);
88 &General
::readhasharray
($configgrp, \
%customgrp);
90 # Check if the location settings file exists
91 if (-e
"$locationfile") {
93 &General
::readhash
("$locationfile", \
%locationsettings);
96 # Get all available locations.
97 my @locations = &Location
::Functions
::get_locations
();
99 my @log_limit_options = &make_log_limit_options
();
101 my $POLICY_INPUT_ALLOWED = 0;
102 my $POLICY_FORWARD_ALLOWED = ($fwdfwsettings{"POLICY"} eq "MODE2");
103 my $POLICY_OUTPUT_ALLOWED = ($fwdfwsettings{"POLICY1"} eq "MODE2");
105 my $POLICY_INPUT_ACTION = $fwoptions{"FWPOLICY2"};
106 my $POLICY_FORWARD_ACTION = $fwoptions{"FWPOLICY"};
107 my $POLICY_OUTPUT_ACTION = $fwoptions{"FWPOLICY1"};
109 #workaround to suppress a warning when a variable is used only once
110 my @dummy = ( $Location::Functions
::ipset_db_directory
);
120 # Destroy all existing ipsets.
121 run
("$IPSET destroy");
123 # Prepare firewall rules.
124 if (! -z
"${General::swroot}/firewall/input"){
125 &buildrules
(\
%configinputfw);
127 if (! -z
"${General::swroot}/firewall/outgoing"){
128 &buildrules
(\
%configoutgoingfw);
130 if (! -z
"${General::swroot}/firewall/config"){
131 &buildrules
(\
%configfwdfw);
134 # Load Location block rules.
137 # Reload firewall policy.
138 run
("/usr/sbin/firewall-policy");
140 #Reload firewall.local if present
141 if ( -f
'/etc/sysconfig/firewall.local'){
142 run
("/etc/sysconfig/firewall.local reload");
147 # Executes or prints the given shell command.
156 print_error
("ERROR: $command");
164 print STDERR
"$message\n";
174 printf(" %2d: %s", $i++, $_);
182 return scalar @
$hash;
186 run
("$IPTABLES -F $CHAIN_INPUT");
187 run
("$IPTABLES -F $CHAIN_FORWARD");
188 run
("$IPTABLES -F $CHAIN_OUTPUT");
189 run
("$IPTABLES -t nat -F $CHAIN_NAT_SOURCE");
190 run
("$IPTABLES -t nat -F $CHAIN_NAT_DESTINATION");
191 run
("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
193 # Flush LOCATIONBLOCK chain.
194 run
("$IPTABLES -F LOCATIONBLOCK");
200 # Search for targets that need to be specially handled when adding
201 # forwarding rules. Additional rules will automatically get inserted
202 # into the INPUT/OUTPUT chains for these targets.
203 my @special_input_targets = ();
204 if (!$POLICY_FORWARD_ALLOWED) {
205 push(@special_input_targets, "ACCEPT");
208 if ($POLICY_INPUT_ACTION eq "DROP") {
209 push(@special_input_targets, ("ACCEPT", "REJECT"));
210 } elsif ($POLICY_INPUT_ACTION eq "REJECT") {
211 push(@special_input_targets, ("ACCEPT", "DROP"));
214 my @special_output_targets = ();
215 if ($POLICY_OUTPUT_ALLOWED) {
216 push(@special_output_targets, ("DROP", "REJECT"));
218 push(@special_output_targets, "ACCEPT");
220 if ($POLICY_OUTPUT_ACTION eq "DROP") {
221 push(@special_output_targets, ("ACCEPT", "REJECT"));
222 } elsif ($POLICY_OUTPUT_ACTION eq "REJECT") {
223 push(@special_output_targets, ("ACCEPT", "DROP"));
227 foreach my $key (sort {$a <=> $b} keys %$hash) {
228 # Skip disabled rules.
229 next unless ($$hash{$key}[2] eq 'ON');
231 # Count number of elements in this line
232 my $elements = &count_elements
($$hash{$key});
235 print_rule
($$hash{$key});
238 # Check if the target is valid.
239 my $target = $$hash{$key}[0];
240 if (!$target ~~ @VALID_TARGETS) {
241 print_error
("Invalid target '$target' for rule $key");
245 # Check if the chain is valid.
246 my $chain = $$hash{$key}[1];
247 if (!$chain ~~ @VALID_CHAINS) {
248 print_error
("Invalid chain '$chain' in rule $key");
252 # Collect all sources.
253 my @sources = &fwlib
::get_addresses
($hash, $key, "src");
255 # Collect all destinations.
256 my @destinations = &fwlib
::get_addresses
($hash, $key, "tgt");
258 # True if the destination is the firewall itself.
259 my $destination_is_firewall = ($$hash{$key}[5] eq "ipfire");
261 # Check if logging should be enabled.
262 my $LOG = ($$hash{$key}[17] eq 'ON');
264 # Check if NAT is enabled and initialize variables, that we use for that.
265 my $NAT = ($$hash{$key}[28] eq 'ON');
268 $NAT_MODE = uc($$hash{$key}[31]);
271 # Set up time constraints.
272 my @time_options = ();
273 if ($$hash{$key}[18] eq 'ON') {
274 push(@time_options, ("-m", "time"));
276 # Select all days of the week this match is active.
278 if ($$hash{$key}[19] ne '') {
279 push (@weekdays, "Mon");
281 if ($$hash{$key}[20] ne '') {
282 push (@weekdays, "Tue");
284 if ($$hash{$key}[21] ne '') {
285 push (@weekdays, "Wed");
287 if ($$hash{$key}[22] ne '') {
288 push (@weekdays, "Thu");
290 if ($$hash{$key}[23] ne '') {
291 push (@weekdays, "Fri");
293 if ($$hash{$key}[24] ne '') {
294 push (@weekdays, "Sat");
296 if ($$hash{$key}[25] ne '') {
297 push (@weekdays, "Sun");
300 push(@time_options, ("--weekdays", join(",", @weekdays)));
303 # Convert start time.
304 my $time_start = &format_time
($$hash{$key}[26]);
306 push(@time_options, ("--timestart", $time_start));
310 my $time_stop = &format_time
($$hash{$key}[27]);
312 push(@time_options, ("--timestop", $time_stop));
316 # Concurrent connection limit
317 my @ratelimit_options = ();
319 if (($elements ge 34) && ($$hash{$key}[32] eq 'ON')) {
320 my $conn_limit = $$hash{$key}[33];
322 if ($conn_limit ge 1) {
323 push(@ratelimit_options, ("-m", "connlimit"));
325 # Use the the entire source IP address
326 push(@ratelimit_options, "--connlimit-saddr");
327 push(@ratelimit_options, ("--connlimit-mask", "32"));
330 push(@ratelimit_options, ("--connlimit-upto", $conn_limit));
335 if (($elements ge 37) && ($$hash{$key}[34] eq 'ON')) {
336 my $rate_limit = "$$hash{$key}[35]/$$hash{$key}[36]";
339 push(@ratelimit_options, ("-m", "limit"));
340 push(@ratelimit_options, ("--limit", $rate_limit));
344 # Check which protocols are used in this rule and so that we can
345 # later group rules by protocols.
346 my @protocols = &get_protocols
($hash, $key);
348 print_error
("Invalid protocol configuration for rule $key");
352 foreach my $protocol (@protocols) {
353 # Check if the given protocol is supported.
354 if (($protocol ne "all") && (!$protocol ~~ @PROTOCOLS)) {
355 print_error
("Protocol $protocol is not supported (rule $key)");
359 # Prepare protocol options (like ICMP types, ports, etc...).
360 my @protocol_options = &get_protocol_options
($hash, $key, $protocol, 0);
362 # Check if this protocol knows ports.
363 my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS);
365 foreach my $src (@sources) {
366 # Skip invalid source.
367 next unless (defined $src);
371 my $source = @
$src[0];
372 if ($source ~~ @ANY_ADDRESSES) {
376 my $source_intf = @
$src[1];
378 foreach my $dst (@destinations) {
379 # Skip invalid rules.
380 next unless (defined $dst);
381 next if (!$dst || ($dst eq "none"));
383 # Sanitize destination.
384 my $destination = @
$dst[0];
385 if ($destination ~~ @ANY_ADDRESSES) {
389 my $destination_intf = @
$dst[1];
391 # Array with iptables arguments.
395 if ($protocol ne "all") {
396 push(@options, @protocol_options);
399 # Prepare source options.
400 my @source_options = ();
401 if ($source =~ /mac/) {
402 push(@source_options, $source);
403 } elsif ($source =~ /-m set/) {
404 # Grab location code from hash.
405 my $loc_src = $$hash{$key}[4];
407 # Check if the network list for this country already has been loaded.
408 unless($loaded_ipset_lists{$loc_src}) {
409 # Call function to load the networks list for this country.
410 &ipset_restore
($loc_src);
412 # Store to the hash that this list has been loaded.
413 $loaded_ipset_lists{$loc_src} = "1";
416 push(@source_options, $source);
418 push(@source_options, ("-s", $source));
421 # Prepare destination options.
422 my @destination_options = ();
423 if ($destination =~ /-m set/) {
424 # Grab location code from hash.
425 my $loc_dst = $$hash{$key}[6];
427 # Check if the network list for this country already has been loaded.
428 unless($loaded_ipset_lists{$loc_dst}) {
429 # Call function to load the networks list for this country.
430 &ipset_restore
($loc_dst);
432 # Store to the hash that this list has been loaded.
433 $loaded_ipset_lists{$loc_dst} = "1";
436 push(@destination_options, $destination);
437 } elsif ($destination) {
438 push(@destination_options, ("-d", $destination));
441 # Add source and destination interface to the filter rules.
442 # These are supposed to help filtering forged packets that originate
443 # from BLUE with an IP address from GREEN for instance.
444 my @source_intf_options = ();
446 push(@source_intf_options, ("-i", $source_intf));
449 my @destination_intf_options = ();
450 if ($destination_intf) {
451 push(@destination_intf_options, ("-o", $destination_intf));
454 # Add time constraint options.
455 push(@options, @time_options);
457 # Add ratelimiting option
458 push(@options, @ratelimit_options);
460 my $firewall_is_in_source_subnet = 1;
462 $firewall_is_in_source_subnet = &firewall_is_in_subnet
($source);
465 my $firewall_is_in_destination_subnet = 1;
467 $firewall_is_in_destination_subnet = &firewall_is_in_subnet
($destination);
472 my $nat_address = &fwlib
::get_nat_address
($$hash{$key}[29], $source);
474 # Skip NAT rules if the NAT address is unknown
475 # (i.e. no internet connection has been established, yet).
476 next unless ($nat_address);
479 if ($NAT_MODE eq "DNAT") {
480 my @nat_options = ();
481 if ($protocol ne "all") {
482 my @nat_protocol_options = &get_protocol_options
($hash, $key, $protocol, 1);
483 push(@nat_options, @nat_protocol_options);
487 push(@nat_options, @time_options);
489 # Determine if a REDIRECT rule should be created.
490 my $use_redirect = ($destination_is_firewall && !$destination && $protocol_has_ports);
492 # Make port-forwardings useable from the internal networks.
493 if (!$use_redirect) {
494 my @internal_addresses = &fwlib
::get_internal_firewall_ip_addresses
(1);
495 unless ($nat_address ~~ @internal_addresses) {
496 &add_dnat_mangle_rules
($nat_address, $source_intf, @nat_options);
500 # Add source options.
501 push(@nat_options, @source_options);
504 if (!$use_redirect) {
505 push(@nat_options, ("-d", $nat_address));
509 if ($protocol_has_ports) {
510 $dnat_port = &get_dnat_target_port
($hash, $key);
513 my @nat_action_options = ();
515 # Use iptables REDIRECT
517 push(@nat_action_options, ("-j", "REDIRECT"));
519 # Redirect to specified port if one has given.
521 push(@nat_action_options, ("--to-ports", $dnat_port));
526 if ($destination_is_firewall && !$destination) {
527 $destination = &fwlib
::get_external_address
();
529 next unless ($destination);
531 my ($dnat_address, $dnat_mask) = split("/", $destination);
532 @destination_options = ("-d", $dnat_address);
534 if ($protocol_has_ports) {
535 my $dnat_port = &get_dnat_target_port
($hash, $key);
538 $dnat_address .= ":$dnat_port";
542 push(@nat_action_options, ("-j", "DNAT", "--to-destination", $dnat_address));
546 run
("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '");
548 run
("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @nat_action_options");
551 } elsif ($NAT_MODE eq "SNAT") {
552 my @snat_options = ( "-m", "policy", "--dir", "out", "--pol", "none" );
553 my @nat_options = @options;
555 # Get addresses for the configured firewall interfaces.
556 my @local_addresses = &fwlib
::get_internal_firewall_ip_addresses
(1);
558 # Check if the nat_address is one of the local addresses.
559 foreach my $local_address (@local_addresses) {
560 if ($nat_address eq $local_address) {
561 # Clear SNAT options.
569 push(@nat_options, @destination_intf_options);
570 push(@nat_options, @source_options);
571 push(@nat_options, @destination_options);
574 run
("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @snat_options @log_limit_options -j LOG --log-prefix 'SNAT '");
576 run
("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @snat_options -j SNAT --to-source $nat_address");
580 push(@options, @source_options);
581 push(@options, @destination_options);
583 # Insert firewall rule.
585 run
("$IPTABLES -A $chain @options @source_intf_options @destination_intf_options @log_limit_options -j LOG --log-prefix '$chain '");
587 run
("$IPTABLES -A $chain @options @source_intf_options @destination_intf_options -j $target");
589 # Handle forwarding rules and add corresponding rules for firewall access.
590 if ($chain eq $CHAIN_FORWARD) {
591 # If the firewall is part of the destination subnet and access to the destination network
592 # is granted/forbidden for any network that the firewall itself is part of, we grant/forbid access
593 # for the firewall, too.
594 if ($firewall_is_in_destination_subnet && ($target ~~ @special_input_targets)) {
596 run
("$IPTABLES -A $CHAIN_INPUT @options @source_intf_options @log_limit_options -j LOG --log-prefix '$CHAIN_INPUT '");
598 run
("$IPTABLES -A $CHAIN_INPUT @options @source_intf_options -j $target");
602 if ($firewall_is_in_source_subnet && ($target ~~ @special_output_targets)) {
604 run
("$IPTABLES -A $CHAIN_OUTPUT @options @destination_intf_options @log_limit_options -j LOG --log-prefix '$CHAIN_OUTPUT '");
606 run
("$IPTABLES -A $CHAIN_OUTPUT @options @destination_intf_options -j $target");
615 # Formats the given timestamp into the iptables format which is "hh:mm" UTC.
619 # Convert the given time into minutes.
620 my $minutes = &time_convert_to_minutes
($val);
622 # Move the timestamp into UTC.
623 $minutes += &time_utc_offset
();
625 # Make sure $minutes is between 00:00 and 23:59.
630 if ($minutes > 1440) {
635 return sprintf("%02d:%02d", $minutes / 60, $minutes % 60);
638 # Calculates the offsets in minutes from the local timezone to UTC.
639 sub time_utc_offset
{
640 my @localtime = localtime(time);
641 my @gmtime = gmtime(time);
643 return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60);
646 # Takes a timestamp like "14:00" and converts it into minutes since midnight.
647 sub time_convert_to_minutes
{
648 my ($hrs, $min) = split(":", shift);
650 return ($hrs * 60) + $min;
654 # The LOCATIONBLOCK chain now gets flushed by the flush() function.
656 # If location blocking is not enabled, we are finished here.
657 if ($locationsettings{'LOCATIONBLOCK_ENABLED'} ne "on") {
658 # Exit submodule. Process remaining script.
662 # Only check the RED interface, which is ppp0 in case of RED_TYPE being
663 # set to "PPPOE", and red0 in case of RED_TYPE not being empty otherwise.
664 if ($defaultNetworks{'RED_TYPE'} eq "PPPOE") {
665 run
("$IPTABLES -A LOCATIONBLOCK ! -i ppp0 -j RETURN");
666 } elsif ($defaultNetworks{'RED_DEV'} ne "") {
667 run
("$IPTABLES -A LOCATIONBLOCK ! -i $defaultNetworks{'RED_DEV'} -j RETURN");
670 # Do not check any private address space
671 foreach my $network (@PRIVATE_NETWORKS) {
672 run
("$IPTABLES -A LOCATIONBLOCK -s $network -j RETURN");
675 # Loop through all supported locations and
676 # create iptables rules, if blocking for this country
678 foreach my $location (@locations) {
679 if(exists $locationsettings{$location} && $locationsettings{$location} eq "on") {
680 # Check if the network list for this country already has been loaded.
681 unless($loaded_ipset_lists{$location}) {
682 # Call function to load the networks list for this country.
683 &ipset_restore
($location);
685 # Store to the hash that this list has been loaded.
686 $loaded_ipset_lists{$location} = "1";
689 # Call iptables and create rule to use the loaded ipset list.
690 run
("$IPTABLES -A LOCATIONBLOCK -m set --match-set CC_$location src -j DROP");
699 my $uses_source_ports = ($$hash{$key}[7] eq "ON");
700 my $uses_services = ($$hash{$key}[11] eq "ON");
704 # Rules which don't have source ports or services (like ICMP, ESP, ...).
705 if (!$uses_source_ports && !$uses_services) {
706 push(@protocols, $$hash{$key}[8]);
708 # Rules which either use ports or services.
709 } elsif ($uses_source_ports || $uses_services) {
710 # Check if service group or service
711 if ($$hash{$key}[14] eq 'cust_srv') {
712 push(@protocols, &fwlib
::get_srv_prot
($$hash{$key}[15]));
714 } elsif($$hash{$key}[14] eq 'cust_srvgrp'){
715 my $protos = &fwlib
::get_srvgrp_prot
($$hash{$key}[15]);
716 push(@protocols, split(",", $protos));
719 # Fetch the protocol for this rule.
720 my $protocol = lc($$hash{$key}[8]);
722 # Fetch source and destination ports for this rule.
723 my $source_ports = $$hash{$key}[10];
724 my $destination_ports = $$hash{$key}[15];
726 # Check if ports are set for protocols which do not support ports.
727 if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) {
728 print_error
("$protocol does not support ports");
732 push(@protocols, $protocol);
736 # Remove all empty elements
737 @protocols = map { $_ ?
$_ : () } @protocols;
739 # If no protocol has been defined, we assume "all".
741 push(@protocols, "all");
744 # Make all protocol names lowercase.
745 @protocols = map { lc } @protocols;
750 sub get_protocol_options
{
753 my $protocol = shift;
754 my $nat_options_wanted = shift;
757 # Nothing to do if no protocol is specified.
758 if ($protocol eq "all") {
761 push(@options, ("-p", $protocol));
764 if ($protocol ~~ @PROTOCOLS_WITH_PORTS) {
765 # Process source ports.
766 my $use_src_ports = ($$hash{$key}[7] eq "ON");
767 my $src_ports = $$hash{$key}[10];
769 if ($use_src_ports && $src_ports) {
770 push(@options, &format_ports
($src_ports, "src"));
773 # Process destination ports.
774 my $use_dst_ports = ($$hash{$key}[11] eq "ON");
775 my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat"));
777 if ($use_dst_ports) {
778 my $dst_ports_mode = $$hash{$key}[14];
779 my $dst_ports = $$hash{$key}[15];
781 if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) {
782 if ($nat_options_wanted && $use_dnat && $$hash{$key}[30]) {
783 $dst_ports = $$hash{$key}[30];
785 push(@options, &format_ports
($dst_ports, "dst"));
787 } elsif ($dst_ports_mode eq "cust_srv") {
788 if ($protocol eq "ICMP") {
789 push(@options, ("--icmp-type", &fwlib
::get_srv_port
($dst_ports, 3, "ICMP")));
791 $dst_ports = &fwlib
::get_srv_port
($dst_ports, 1, uc($protocol));
792 push(@options, &format_ports
($dst_ports, "dst"));
795 } elsif ($dst_ports_mode eq "cust_srvgrp") {
796 push(@options, &fwlib
::get_srvgrp_port
($dst_ports, uc($protocol)));
801 # Check if a single ICMP type is selected.
802 if ($protocol eq "icmp") {
803 my $icmp_type = $$hash{$key}[9];
805 if (($icmp_type ne "All ICMP-Types") && $icmp_type) {
806 push(@options, ("--icmp-type", $icmp_type));
818 if ($type eq "src") {
820 } elsif ($type eq "dst") {
826 if ($ports =~ /\|/) {
828 push(@options, ("-m", "multiport"));
832 push(@options, ($arg, $ports));
838 sub get_dnat_target_port
{
842 if ($$hash{$key}[14] eq "TGT_PORT") {
843 my $port = $$hash{$key}[15];
844 my $external_port = $$hash{$key}[30];
846 if ($external_port && ($port ne $external_port)) {
847 return $$hash{$key}[15];
852 sub add_dnat_mangle_rules
{
853 my $nat_address = shift;
854 my $interface = shift;
857 my $mark = 0x01000000;
858 foreach my $zone ("GREEN", "BLUE", "ORANGE") {
859 # Skip rule if not all required information exists.
860 next unless (exists $defaultNetworks{$zone . "_NETADDRESS"});
861 next unless (exists $defaultNetworks{$zone . "_NETMASK"});
863 next if ($interface && $interface ne $defaultNetworks{$zone . "_DEV"});
865 my @mangle_options = @options;
867 my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"};
868 $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"};
870 push(@mangle_options, ("-s", $netaddress, "-d", $nat_address));
871 push(@mangle_options, ("-j", "MARK", "--set-xmark", "$mark/$NAT_MASK"));
873 run
("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options");
879 sub make_log_limit_options
{
880 my @options = ("-m", "limit");
882 # Maybe we should get this from the configuration.
885 # We limit log messages to $limit messages per second.
886 push(@options, ("--limit", "$limit/second"));
888 # And we allow bursts of 2x $limit.
889 push(@options, ("--limit-burst", $limit * 2));
894 sub firewall_is_in_subnet
{
897 # ORANGE is missing here, because nothing may ever access
898 # the firewall from this network.
899 my $address = &fwlib
::get_internal_firewall_ip_address
($subnet, 0);
908 sub ipset_restore
($) {
911 my $file_prefix = "ipset4";
912 my $db_file = "$Location::Functions::ipset_db_directory/$ccode.$file_prefix";
914 # Check if the generated file exists.
916 # Run ipset and restore the list of the given country code.
917 run
("$IPSET restore < $db_file");