# 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");
&General::readhasharray($configgrp, \%customgrp);
&General::get_aliases(\%aliases);
+my @log_limit_options = &make_log_limit_options();
+
# MAIN
&main();
print "$command\n";
} else {
system "$command";
+
+ if ($?) {
+ print_error("ERROR: $command");
+ }
}
}
}
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 {
# 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');
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);
# 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");
}
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") {
push(@options, ("-m", "multiport"));
}
- push(@options, ($arg, $ports));
+ if ($ports) {
+ push(@options, ($arg, $ports));
+ }
return @options;
}
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;
+}