]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blobdiff - config/firewall/rules.pl
firewall: Add chain name to logged rules.
[people/teissler/ipfire-2.x.git] / config / firewall / rules.pl
index d2991b897c6f64a4447266991358ee9ff616bb66..4bb40a4f9c7b7f65a2b460bdea1d50117fe186ad 100755 (executable)
@@ -33,10 +33,11 @@ my $IPTABLES = "iptables --wait";
 # iptables chains
 my $CHAIN_INPUT           = "INPUTFW";
 my $CHAIN_FORWARD         = "FORWARDFW";
-my $CHAIN_OUTPUT          = "OUTPUTFW";
+my $CHAIN_OUTPUT          = "OUTGOINGFW";
 my $CHAIN                 = $CHAIN_FORWARD;
 my $CHAIN_NAT_SOURCE      = "NAT_SOURCE";
 my $CHAIN_NAT_DESTINATION = "NAT_DESTINATION";
+my $CHAIN_MANGLE_NAT_DESTINATION_FIX = "NAT_DESTINATION";
 my @VALID_CHAINS          = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT);
 
 my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip");
@@ -69,6 +70,8 @@ my $netsettings               = "${General::swroot}/ethernet/settings";
 &General::readhasharray($configgrp, \%customgrp);
 &General::get_aliases(\%aliases);
 
+my @log_limit_options = &make_log_limit_options();
+
 # MAIN
 &main();
 
@@ -94,6 +97,10 @@ sub run {
                print "$command\n";
        } else {
                system "$command";
+
+               if ($?) {
+                       print_error("ERROR: $command");
+               }
        }
 }
 
@@ -116,11 +123,12 @@ sub print_rule {
 }
 
 sub flush {
-       run("$IPTABLES -F FORWARDFW");
-       run("$IPTABLES -F INPUTFW");
-       run("$IPTABLES -F OUTGOINGFW");
-       run("$IPTABLES -t nat -F NAT_DESTINATION");
-       run("$IPTABLES -t nat -F NAT_SOURCE");
+       run("$IPTABLES -F $CHAIN_INPUT");
+       run("$IPTABLES -F $CHAIN_FORWARD");
+       run("$IPTABLES -F $CHAIN_OUTPUT");
+       run("$IPTABLES -t nat -F $CHAIN_NAT_SOURCE");
+       run("$IPTABLES -t nat -F $CHAIN_NAT_DESTINATION");
+       run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
 }
 
 sub preparerules {
@@ -166,8 +174,6 @@ sub buildrules {
                # Collect all destinations.
                my @destinations = &get_addresses($hash, $key, "tgt");
 
-               my $time_constraints = "";
-
                # Check if logging should be enabled.
                my $LOG = ($$hash{$key}[17] eq 'ON');
 
@@ -258,17 +264,16 @@ sub buildrules {
                                                push(@options, @protocol_options);
                                        }
 
-                                       # Append source.
+                                       # Prepare source options.
+                                       my @source_options = ();
                                        if ($source =~ /mac/) {
-                                               push(@options, $source);
+                                               push(@source_options, $source);
                                        } else {
-                                               push(@options, ("-s", $source));
+                                               push(@source_options, ("-s", $source));
                                        }
 
-                                       # Append destination for non-DNAT rules.
-                                       unless ($NAT && ($NAT_MODE eq "DNAT")) {
-                                               push(@options, ("-d", $destination));
-                                       }
+                                       # Prepare destination options.
+                                       my @destination_options = ("-d", $destination);
 
                                        # Add time constraint options.
                                        push(@options, @time_options);
@@ -283,53 +288,49 @@ sub buildrules {
 
                                                # Destination NAT
                                                if ($NAT_MODE eq "DNAT") {
-                                                       my ($dnat_address, $dnat_mask) = split("/", $destination);
+                                                       # Make port-forwardings useable from the internal networks.
+                                                       &add_dnat_mangle_rules($nat_address, @options);
 
                                                        my @nat_options = @options;
+                                                       push(@nat_options, @source_options);
                                                        push(@nat_options, ("-d", $nat_address));
-                                                       push(@options,     ("-d", $dnat_address));
+
+                                                       my ($dnat_address, $dnat_mask) = split("/", $destination);
+                                                       @destination_options = ("-d", $dnat_address);
 
                                                        if ($protocol_has_ports) {
                                                                my $dnat_port = &get_dnat_target_port($hash, $key);
 
                                                                if ($dnat_port) {
                                                                        $dnat_address .= ":$dnat_port";
-
-                                                                       # Replace --dport with the translated one.
-                                                                       my @new_options = ();
-                                                                       my $skip_count = 0;
-                                                                       foreach my $option (@options) {
-                                                                               next if ($skip_count-- > 0);
-
-                                                                               if ($option eq "--dport") {
-                                                                                       push(@new_options, ("--dport", $dnat_port));
-                                                                                       $skip_count = 1;
-                                                                                       next;
-                                                                               }
-
-                                                                               push(@new_options, $option);
-                                                                       }
-                                                                       @options = @new_options;
                                                                }
                                                        }
 
                                                        if ($LOG) {
-                                                               run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j LOG --log-prefix 'DNAT'");
+                                                               run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '");
                                                        }
                                                        run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j DNAT --to-destination $dnat_address");
 
                                                # Source NAT
                                                } elsif ($NAT_MODE eq "SNAT") {
+                                                       my @nat_options = @options;
+
+                                                       push(@nat_options, @source_options);
+                                                       push(@nat_options, @destination_options);
+
                                                        if ($LOG) {
-                                                               run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @options -j LOG --log-prefix 'SNAT'");
+                                                               run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @log_limit_options -j LOG --log-prefix 'SNAT '");
                                                        }
-                                                       run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @options -j SNAT --to-source $nat_address");
+                                                       run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address");
                                                }
                                        }
 
+                                       push(@options, @source_options);
+                                       push(@options, @destination_options);
+
                                        # Insert firewall rule.
                                        if ($LOG && !$NAT) {
-                                               run("$IPTABLES -A $chain @options -j LOG");
+                                               run("$IPTABLES -A $chain @options @log_limit_options -j LOG --log-prefix '$chain '");
                                        }
                                        run("$IPTABLES -A $chain @options -j $target");
                                }
@@ -671,11 +672,11 @@ sub get_protocol_options {
        if ($use_dst_ports) {
                my $dst_ports_mode = $$hash{$key}[14];
                my $dst_ports      = $$hash{$key}[15];
-               if ($use_dnat && $$hash{$key}[30]) {
-                       $dst_ports = $$hash{$key}[30];
-               }
 
                if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) {
+                       if ($use_dnat && $$hash{$key}[30]) {
+                               $dst_ports = $$hash{$key}[30];
+                       }
                        push(@options, &format_ports($dst_ports, "dst"));
 
                } elsif ($dst_ports_mode eq "cust_srv") {
@@ -721,7 +722,9 @@ sub format_ports {
                push(@options, ("-m", "multiport"));
        }
 
-       push(@options, ($arg, $ports));
+       if ($ports) {
+               push(@options, ($arg, $ports));
+       }
 
        return @options;
 }
@@ -731,6 +734,50 @@ sub get_dnat_target_port {
        my $key  = shift;
 
        if ($$hash{$key}[14] eq "TGT_PORT") {
-               return $$hash{$key}[15];
+               my $port = $$hash{$key}[15];
+               my $external_port = $$hash{$key}[30];
+
+               if ($external_port && ($port ne $external_port)) {
+                       return $$hash{$key}[15];
+               }
        }
 }
+
+sub add_dnat_mangle_rules {
+       my $nat_address = shift;
+       my @options = @_;
+
+       my $mark = 0;
+       foreach my $zone ("GREEN", "BLUE", "ORANGE") {
+               $mark++;
+
+               # Skip rule if not all required information exists.
+               next unless (exists $defaultNetworks{$zone . "_NETADDRESS"});
+               next unless (exists $defaultNetworks{$zone . "_NETMASK"});
+
+               my @mangle_options = @options;
+
+               my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"};
+               $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"};
+
+               push(@mangle_options, ("-s", $netaddress, "-d", $nat_address));
+               push(@mangle_options, ("-j", "MARK", "--set-mark", $mark));
+
+               run("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options");
+       }
+}
+
+sub make_log_limit_options {
+       my @options = ("-m", "limit");
+
+       # Maybe we should get this from the configuration.
+       my $limit = 10;
+
+       # We limit log messages to $limit messages per minute.
+       push(@options, ("--limit", "$limit/min"));
+
+       # And we allow bursts of 2x $limit.
+       push(@options, ("--limit-burst", $limit * 2));
+
+       return @options;
+}