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=();
77 my $configfwdfw = "${General::swroot}/firewall/config";
78 my $configinput = "${General::swroot}/firewall/input";
79 my $configoutgoing = "${General::swroot}/firewall/outgoing";
80 my $p2pfile = "${General::swroot}/firewall/p2protocols";
81 my $locationfile = "${General::swroot}/firewall/locationblock";
82 my $configgrp = "${General::swroot}/fwhosts/customgroups";
83 my $netsettings = "${General::swroot}/ethernet/settings";
85 &General
::readhash
("${General::swroot}/firewall/settings", \
%fwdfwsettings);
86 &General
::readhash
("${General::swroot}/optionsfw/settings", \
%fwoptions);
87 &General
::readhash
("$netsettings", \
%defaultNetworks);
88 &General
::readhasharray
($configfwdfw, \
%configfwdfw);
89 &General
::readhasharray
($configinput, \
%configinputfw);
90 &General
::readhasharray
($configoutgoing, \
%configoutgoingfw);
91 &General
::readhasharray
($configgrp, \
%customgrp);
93 # Check if the location settings file exists
94 if (-e
"$locationfile") {
96 &General
::readhash
("$locationfile", \
%locationsettings);
99 # Get all available locations.
100 my @locations = &Location
::Functions
::get_locations
();
102 my @log_limit_options = &make_log_limit_options
();
104 my $POLICY_INPUT_ALLOWED = 0;
105 my $POLICY_FORWARD_ALLOWED = ($fwdfwsettings{"POLICY"} eq "MODE2");
106 my $POLICY_OUTPUT_ALLOWED = ($fwdfwsettings{"POLICY1"} eq "MODE2");
108 my $POLICY_INPUT_ACTION = $fwoptions{"FWPOLICY2"};
109 my $POLICY_FORWARD_ACTION = $fwoptions{"FWPOLICY"};
110 my $POLICY_OUTPUT_ACTION = $fwoptions{"FWPOLICY1"};
119 # Destroy all existing ipsets.
120 run
("$IPSET destroy");
122 # Prepare firewall rules.
123 if (! -z
"${General::swroot}/firewall/input"){
124 &buildrules
(\
%configinputfw);
126 if (! -z
"${General::swroot}/firewall/outgoing"){
127 &buildrules
(\
%configoutgoingfw);
129 if (! -z
"${General::swroot}/firewall/config"){
130 &buildrules
(\
%configfwdfw);
133 # Load P2P block rules.
136 # Load Location block rules.
139 # Reload firewall policy.
140 run
("/usr/sbin/firewall-policy");
142 #Reload firewall.local if present
143 if ( -f
'/etc/sysconfig/firewall.local'){
144 run
("/etc/sysconfig/firewall.local reload");
149 # Executes or prints the given shell command.
158 print_error
("ERROR: $command");
166 print STDERR
"$message\n";
176 printf(" %2d: %s", $i++, $_);
184 return scalar @
$hash;
188 run
("$IPTABLES -F $CHAIN_INPUT");
189 run
("$IPTABLES -F $CHAIN_FORWARD");
190 run
("$IPTABLES -F $CHAIN_OUTPUT");
191 run
("$IPTABLES -t nat -F $CHAIN_NAT_SOURCE");
192 run
("$IPTABLES -t nat -F $CHAIN_NAT_DESTINATION");
193 run
("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
195 # Flush LOCATIONBLOCK chain.
196 run
("$IPTABLES -F LOCATIONBLOCK");
202 # Search for targets that need to be specially handled when adding
203 # forwarding rules. Additional rules will automatically get inserted
204 # into the INPUT/OUTPUT chains for these targets.
205 my @special_input_targets = ();
206 if (!$POLICY_FORWARD_ALLOWED) {
207 push(@special_input_targets, "ACCEPT");
210 if ($POLICY_INPUT_ACTION eq "DROP") {
211 push(@special_input_targets, ("ACCEPT", "REJECT"));
212 } elsif ($POLICY_INPUT_ACTION eq "REJECT") {
213 push(@special_input_targets, ("ACCEPT", "DROP"));
216 my @special_output_targets = ();
217 if ($POLICY_OUTPUT_ALLOWED) {
218 push(@special_output_targets, ("DROP", "REJECT"));
220 push(@special_output_targets, "ACCEPT");
222 if ($POLICY_OUTPUT_ACTION eq "DROP") {
223 push(@special_output_targets, ("ACCEPT", "REJECT"));
224 } elsif ($POLICY_OUTPUT_ACTION eq "REJECT") {
225 push(@special_output_targets, ("ACCEPT", "DROP"));
229 foreach my $key (sort {$a <=> $b} keys %$hash) {
230 # Skip disabled rules.
231 next unless ($$hash{$key}[2] eq 'ON');
233 # Count number of elements in this line
234 my $elements = &count_elements
($$hash{$key});
237 print_rule
($$hash{$key});
240 # Check if the target is valid.
241 my $target = $$hash{$key}[0];
242 if (!$target ~~ @VALID_TARGETS) {
243 print_error
("Invalid target '$target' for rule $key");
247 # Check if the chain is valid.
248 my $chain = $$hash{$key}[1];
249 if (!$chain ~~ @VALID_CHAINS) {
250 print_error
("Invalid chain '$chain' in rule $key");
254 # Collect all sources.
255 my @sources = &fwlib
::get_addresses
($hash, $key, "src");
257 # Collect all destinations.
258 my @destinations = &fwlib
::get_addresses
($hash, $key, "tgt");
260 # True if the destination is the firewall itself.
261 my $destination_is_firewall = ($$hash{$key}[5] eq "ipfire");
263 # Check if logging should be enabled.
264 my $LOG = ($$hash{$key}[17] eq 'ON');
266 # Check if NAT is enabled and initialize variables, that we use for that.
267 my $NAT = ($$hash{$key}[28] eq 'ON');
270 $NAT_MODE = uc($$hash{$key}[31]);
273 # Set up time constraints.
274 my @time_options = ();
275 if ($$hash{$key}[18] eq 'ON') {
276 push(@time_options, ("-m", "time"));
278 # Select all days of the week this match is active.
280 if ($$hash{$key}[19] ne '') {
281 push (@weekdays, "Mon");
283 if ($$hash{$key}[20] ne '') {
284 push (@weekdays, "Tue");
286 if ($$hash{$key}[21] ne '') {
287 push (@weekdays, "Wed");
289 if ($$hash{$key}[22] ne '') {
290 push (@weekdays, "Thu");
292 if ($$hash{$key}[23] ne '') {
293 push (@weekdays, "Fri");
295 if ($$hash{$key}[24] ne '') {
296 push (@weekdays, "Sat");
298 if ($$hash{$key}[25] ne '') {
299 push (@weekdays, "Sun");
302 push(@time_options, ("--weekdays", join(",", @weekdays)));
305 # Convert start time.
306 my $time_start = &format_time
($$hash{$key}[26]);
308 push(@time_options, ("--timestart", $time_start));
312 my $time_stop = &format_time
($$hash{$key}[27]);
314 push(@time_options, ("--timestop", $time_stop));
318 # Concurrent connection limit
319 my @ratelimit_options = ();
321 if (($elements ge 34) && ($$hash{$key}[32] eq 'ON')) {
322 my $conn_limit = $$hash{$key}[33];
324 if ($conn_limit ge 1) {
325 push(@ratelimit_options, ("-m", "connlimit"));
327 # Use the the entire source IP address
328 push(@ratelimit_options, "--connlimit-saddr");
329 push(@ratelimit_options, ("--connlimit-mask", "32"));
332 push(@ratelimit_options, ("--connlimit-upto", $conn_limit));
337 if (($elements ge 37) && ($$hash{$key}[34] eq 'ON')) {
338 my $rate_limit = "$$hash{$key}[35]/$$hash{$key}[36]";
341 push(@ratelimit_options, ("-m", "limit"));
342 push(@ratelimit_options, ("--limit", $rate_limit));
346 # Check which protocols are used in this rule and so that we can
347 # later group rules by protocols.
348 my @protocols = &get_protocols
($hash, $key);
350 print_error
("Invalid protocol configuration for rule $key");
354 foreach my $protocol (@protocols) {
355 # Check if the given protocol is supported.
356 if (($protocol ne "all") && (!$protocol ~~ @PROTOCOLS)) {
357 print_error
("Protocol $protocol is not supported (rule $key)");
361 # Prepare protocol options (like ICMP types, ports, etc...).
362 my @protocol_options = &get_protocol_options
($hash, $key, $protocol, 0);
364 # Check if this protocol knows ports.
365 my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS);
367 foreach my $src (@sources) {
368 # Skip invalid source.
369 next unless (defined $src);
373 my $source = @
$src[0];
374 if ($source ~~ @ANY_ADDRESSES) {
378 my $source_intf = @
$src[1];
380 foreach my $dst (@destinations) {
381 # Skip invalid rules.
382 next unless (defined $dst);
383 next if (!$dst || ($dst eq "none"));
385 # Sanitize destination.
386 my $destination = @
$dst[0];
387 if ($destination ~~ @ANY_ADDRESSES) {
391 my $destination_intf = @
$dst[1];
393 # Array with iptables arguments.
397 if ($protocol ne "all") {
398 push(@options, @protocol_options);
401 # Prepare source options.
402 my @source_options = ();
403 if ($source =~ /mac/) {
404 push(@source_options, $source);
405 } elsif ($source =~ /-m set/) {
406 # Grab location code from hash.
407 my $loc_src = $$hash{$key}[4];
409 # Check if the network list for this country already has been loaded.
410 unless($loaded_ipset_lists{$loc_src}) {
411 # Call function to load the networks list for this country.
412 &ipset_restore
($loc_src);
414 # Store to the hash that this list has been loaded.
415 $loaded_ipset_lists{$loc_src} = "1";
418 push(@source_options, $source);
420 push(@source_options, ("-s", $source));
423 # Prepare destination options.
424 my @destination_options = ();
425 if ($destination =~ /-m set/) {
426 # Grab location code from hash.
427 my $loc_dst = $$hash{$key}[6];
429 # Check if the network list for this country already has been loaded.
430 unless($loaded_ipset_lists{$loc_dst}) {
431 # Call function to load the networks list for this country.
432 &ipset_restore
($loc_dst);
434 # Store to the hash that this list has been loaded.
435 $loaded_ipset_lists{$loc_dst} = "1";
438 push(@destination_options, $destination);
439 } elsif ($destination) {
440 push(@destination_options, ("-d", $destination));
443 # Add source and destination interface to the filter rules.
444 # These are supposed to help filtering forged packets that originate
445 # from BLUE with an IP address from GREEN for instance.
446 my @source_intf_options = ();
448 push(@source_intf_options, ("-i", $source_intf));
451 my @destination_intf_options = ();
452 if ($destination_intf) {
453 push(@destination_intf_options, ("-o", $destination_intf));
456 # Add time constraint options.
457 push(@options, @time_options);
459 # Add ratelimiting option
460 push(@options, @ratelimit_options);
462 my $firewall_is_in_source_subnet = 1;
464 $firewall_is_in_source_subnet = &firewall_is_in_subnet
($source);
467 my $firewall_is_in_destination_subnet = 1;
469 $firewall_is_in_destination_subnet = &firewall_is_in_subnet
($destination);
474 my $nat_address = &fwlib
::get_nat_address
($$hash{$key}[29], $source);
476 # Skip NAT rules if the NAT address is unknown
477 # (i.e. no internet connection has been established, yet).
478 next unless ($nat_address);
481 if ($NAT_MODE eq "DNAT") {
482 my @nat_options = ();
483 if ($protocol ne "all") {
484 my @nat_protocol_options = &get_protocol_options
($hash, $key, $protocol, 1);
485 push(@nat_options, @nat_protocol_options);
489 push(@nat_options, @time_options);
491 # Determine if a REDIRECT rule should be created.
492 my $use_redirect = ($destination_is_firewall && !$destination && $protocol_has_ports);
494 # Make port-forwardings useable from the internal networks.
495 if (!$use_redirect) {
496 my @internal_addresses = &fwlib
::get_internal_firewall_ip_addresses
(1);
497 unless ($nat_address ~~ @internal_addresses) {
498 &add_dnat_mangle_rules
($nat_address, $source_intf, @nat_options);
502 # Add source options.
503 push(@nat_options, @source_options);
506 if (!$use_redirect) {
507 push(@nat_options, ("-d", $nat_address));
511 if ($protocol_has_ports) {
512 $dnat_port = &get_dnat_target_port
($hash, $key);
515 my @nat_action_options = ();
517 # Use iptables REDIRECT
519 push(@nat_action_options, ("-j", "REDIRECT"));
521 # Redirect to specified port if one has given.
523 push(@nat_action_options, ("--to-ports", $dnat_port));
528 if ($destination_is_firewall && !$destination) {
529 $destination = &fwlib
::get_external_address
();
531 next unless ($destination);
533 my ($dnat_address, $dnat_mask) = split("/", $destination);
534 @destination_options = ("-d", $dnat_address);
536 if ($protocol_has_ports) {
537 my $dnat_port = &get_dnat_target_port
($hash, $key);
540 $dnat_address .= ":$dnat_port";
544 push(@nat_action_options, ("-j", "DNAT", "--to-destination", $dnat_address));
548 run
("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '");
550 run
("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @nat_action_options");
553 } elsif ($NAT_MODE eq "SNAT") {
554 my @snat_options = ( "-m", "policy", "--dir", "out", "--pol", "none" );
555 my @nat_options = @options;
557 # Get addresses for the configured firewall interfaces.
558 my @local_addresses = &fwlib
::get_internal_firewall_ip_addresses
(1);
560 # Check if the nat_address is one of the local addresses.
561 foreach my $local_address (@local_addresses) {
562 if ($nat_address eq $local_address) {
563 # Clear SNAT options.
571 push(@nat_options, @destination_intf_options);
572 push(@nat_options, @source_options);
573 push(@nat_options, @destination_options);
576 run
("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @snat_options @log_limit_options -j LOG --log-prefix 'SNAT '");
578 run
("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @snat_options -j SNAT --to-source $nat_address");
582 push(@options, @source_options);
583 push(@options, @destination_options);
585 # Insert firewall rule.
587 run
("$IPTABLES -A $chain @options @source_intf_options @destination_intf_options @log_limit_options -j LOG --log-prefix '$chain '");
589 run
("$IPTABLES -A $chain @options @source_intf_options @destination_intf_options -j $target");
591 # Handle forwarding rules and add corresponding rules for firewall access.
592 if ($chain eq $CHAIN_FORWARD) {
593 # If the firewall is part of the destination subnet and access to the destination network
594 # is granted/forbidden for any network that the firewall itself is part of, we grant/forbid access
595 # for the firewall, too.
596 if ($firewall_is_in_destination_subnet && ($target ~~ @special_input_targets)) {
598 run
("$IPTABLES -A $CHAIN_INPUT @options @source_intf_options @log_limit_options -j LOG --log-prefix '$CHAIN_INPUT '");
600 run
("$IPTABLES -A $CHAIN_INPUT @options @source_intf_options -j $target");
604 if ($firewall_is_in_source_subnet && ($target ~~ @special_output_targets)) {
606 run
("$IPTABLES -A $CHAIN_OUTPUT @options @destination_intf_options @log_limit_options -j LOG --log-prefix '$CHAIN_OUTPUT '");
608 run
("$IPTABLES -A $CHAIN_OUTPUT @options @destination_intf_options -j $target");
617 # Formats the given timestamp into the iptables format which is "hh:mm" UTC.
621 # Convert the given time into minutes.
622 my $minutes = &time_convert_to_minutes
($val);
624 # Move the timestamp into UTC.
625 $minutes += &time_utc_offset
();
627 # Make sure $minutes is between 00:00 and 23:59.
632 if ($minutes > 1440) {
637 return sprintf("%02d:%02d", $minutes / 60, $minutes % 60);
640 # Calculates the offsets in minutes from the local timezone to UTC.
641 sub time_utc_offset
{
642 my @localtime = localtime(time);
643 my @gmtime = gmtime(time);
645 return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60);
648 # Takes a timestamp like "14:00" and converts it into minutes since midnight.
649 sub time_convert_to_minutes
{
650 my ($hrs, $min) = split(":", shift);
652 return ($hrs * 60) + $min;
656 open(FILE
, "<$p2pfile") or die "Unable to read $p2pfile";
658 foreach my $p2pentry (<FILE
>) {
659 my @p2pline = split(/\;/, $p2pentry);
660 next unless ($p2pline[2] eq "off");
662 push(@protocols, "--$p2pline[1]");
666 run
("$IPTABLES -F P2PBLOCK");
668 run
("$IPTABLES -A P2PBLOCK -m ipp2p @protocols -j DROP");
673 # The LOCATIONBLOCK chain now gets flushed by the flush() function.
675 # If location blocking is not enabled, we are finished here.
676 if ($locationsettings{'LOCATIONBLOCK_ENABLED'} ne "on") {
677 # Exit submodule. Process remaining script.
681 # Only check the RED interface, which is ppp0 in case of RED_TYPE being
682 # set to "PPPOE", and red0 in case of RED_TYPE not being empty otherwise.
683 if ($defaultNetworks{'RED_TYPE'} eq "PPPOE") {
684 run
("$IPTABLES -A LOCATIONBLOCK ! -i ppp0 -j RETURN");
685 } elsif ($defaultNetworks{'RED_DEV'} ne "") {
686 run
("$IPTABLES -A LOCATIONBLOCK ! -i $defaultNetworks{'RED_DEV'} -j RETURN");
689 # Do not check any private address space
690 foreach my $network (@PRIVATE_NETWORKS) {
691 run
("$IPTABLES -A LOCATIONBLOCK -s $network -j RETURN");
694 # Loop through all supported locations and
695 # create iptables rules, if blocking for this country
697 foreach my $location (@locations) {
698 if(exists $locationsettings{$location} && $locationsettings{$location} eq "on") {
699 # Check if the network list for this country already has been loaded.
700 unless($loaded_ipset_lists{$location}) {
701 # Call function to load the networks list for this country.
702 &ipset_restore
($location);
704 # Store to the hash that this list has been loaded.
705 $loaded_ipset_lists{$location} = "1";
708 # Call iptables and create rule to use the loaded ipset list.
709 run
("$IPTABLES -A LOCATIONBLOCK -m set --match-set CC_$location src -j DROP");
718 my $uses_source_ports = ($$hash{$key}[7] eq "ON");
719 my $uses_services = ($$hash{$key}[11] eq "ON");
723 # Rules which don't have source ports or services (like ICMP, ESP, ...).
724 if (!$uses_source_ports && !$uses_services) {
725 push(@protocols, $$hash{$key}[8]);
727 # Rules which either use ports or services.
728 } elsif ($uses_source_ports || $uses_services) {
729 # Check if service group or service
730 if ($$hash{$key}[14] eq 'cust_srv') {
731 push(@protocols, &fwlib
::get_srv_prot
($$hash{$key}[15]));
733 } elsif($$hash{$key}[14] eq 'cust_srvgrp'){
734 my $protos = &fwlib
::get_srvgrp_prot
($$hash{$key}[15]);
735 push(@protocols, split(",", $protos));
738 # Fetch the protocol for this rule.
739 my $protocol = lc($$hash{$key}[8]);
741 # Fetch source and destination ports for this rule.
742 my $source_ports = $$hash{$key}[10];
743 my $destination_ports = $$hash{$key}[15];
745 # Check if ports are set for protocols which do not support ports.
746 if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) {
747 print_error
("$protocol does not support ports");
751 push(@protocols, $protocol);
755 # Remove all empty elements
756 @protocols = map { $_ ?
$_ : () } @protocols;
758 # If no protocol has been defined, we assume "all".
760 push(@protocols, "all");
763 # Make all protocol names lowercase.
764 @protocols = map { lc } @protocols;
769 sub get_protocol_options
{
772 my $protocol = shift;
773 my $nat_options_wanted = shift;
776 # Nothing to do if no protocol is specified.
777 if ($protocol eq "all") {
780 push(@options, ("-p", $protocol));
783 if ($protocol ~~ @PROTOCOLS_WITH_PORTS) {
784 # Process source ports.
785 my $use_src_ports = ($$hash{$key}[7] eq "ON");
786 my $src_ports = $$hash{$key}[10];
788 if ($use_src_ports && $src_ports) {
789 push(@options, &format_ports
($src_ports, "src"));
792 # Process destination ports.
793 my $use_dst_ports = ($$hash{$key}[11] eq "ON");
794 my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat"));
796 if ($use_dst_ports) {
797 my $dst_ports_mode = $$hash{$key}[14];
798 my $dst_ports = $$hash{$key}[15];
800 if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) {
801 if ($nat_options_wanted && $use_dnat && $$hash{$key}[30]) {
802 $dst_ports = $$hash{$key}[30];
804 push(@options, &format_ports
($dst_ports, "dst"));
806 } elsif ($dst_ports_mode eq "cust_srv") {
807 if ($protocol eq "ICMP") {
808 push(@options, ("--icmp-type", &fwlib
::get_srv_port
($dst_ports, 3, "ICMP")));
810 $dst_ports = &fwlib
::get_srv_port
($dst_ports, 1, uc($protocol));
811 push(@options, &format_ports
($dst_ports, "dst"));
814 } elsif ($dst_ports_mode eq "cust_srvgrp") {
815 push(@options, &fwlib
::get_srvgrp_port
($dst_ports, uc($protocol)));
820 # Check if a single ICMP type is selected.
821 if ($protocol eq "icmp") {
822 my $icmp_type = $$hash{$key}[9];
824 if (($icmp_type ne "All ICMP-Types") && $icmp_type) {
825 push(@options, ("--icmp-type", $icmp_type));
837 if ($type eq "src") {
839 } elsif ($type eq "dst") {
845 if ($ports =~ /\|/) {
847 push(@options, ("-m", "multiport"));
851 push(@options, ($arg, $ports));
857 sub get_dnat_target_port
{
861 if ($$hash{$key}[14] eq "TGT_PORT") {
862 my $port = $$hash{$key}[15];
863 my $external_port = $$hash{$key}[30];
865 if ($external_port && ($port ne $external_port)) {
866 return $$hash{$key}[15];
871 sub add_dnat_mangle_rules
{
872 my $nat_address = shift;
873 my $interface = shift;
876 my $mark = 0x01000000;
877 foreach my $zone ("GREEN", "BLUE", "ORANGE") {
878 # Skip rule if not all required information exists.
879 next unless (exists $defaultNetworks{$zone . "_NETADDRESS"});
880 next unless (exists $defaultNetworks{$zone . "_NETMASK"});
882 next if ($interface && $interface ne $defaultNetworks{$zone . "_DEV"});
884 my @mangle_options = @options;
886 my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"};
887 $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"};
889 push(@mangle_options, ("-s", $netaddress, "-d", $nat_address));
890 push(@mangle_options, ("-j", "MARK", "--set-xmark", "$mark/$NAT_MASK"));
892 run
("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options");
898 sub make_log_limit_options
{
899 my @options = ("-m", "limit");
901 # Maybe we should get this from the configuration.
904 # We limit log messages to $limit messages per second.
905 push(@options, ("--limit", "$limit/second"));
907 # And we allow bursts of 2x $limit.
908 push(@options, ("--limit-burst", $limit * 2));
913 sub firewall_is_in_subnet
{
916 # ORANGE is missing here, because nothing may ever access
917 # the firewall from this network.
918 my $address = &fwlib
::get_internal_firewall_ip_address
($subnet, 0);
927 sub ipset_restore
($) {
930 my $file_prefix = "ipset4";
931 my $db_file = "$Location::Functions::ipset_db_directory/$ccode.$file_prefix";
933 # Check if the generated file exists.
935 # Run ipset and restore the list of the given country code.
936 run
("$IPSET restore < $db_file");