# 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";
&General::readhasharray($configgrp, \%customgrp);
&General::get_aliases(\%aliases);
+my @log_limit_options = &make_log_limit_options();
+
# MAIN
&main();
}
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");
}
# 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');
if ($dnat_port) {
$dnat_address .= ":$dnat_port";
-
- # Replace --dport with the translated one.
- my @new_nat_options = ();
- my $skip_count = 0;
- foreach my $option (@nat_options) {
- next if ($skip_count-- > 0);
-
- if ($option eq "--dport") {
- push(@new_nat_options, ("--dport", $dnat_port));
- $skip_count = 1;
- next;
- }
-
- push(@new_nat_options, $option);
- }
- @nat_options = @new_nat_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");
push(@nat_options, @destination_options);
if ($LOG) {
- run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_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 @nat_options -j SNAT --to-source $nat_address");
}
# 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];
+ }
}
}
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;
+}