2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-03-28 18:19:35 +02:00
|
|
|
# Shorewall-perl 3.9 -- /usr/share/shorewall-perl/Shorewall/Chains.pm
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
|
|
|
# This program is under GPL [http://www.gnu.org/copyleft/gpl.htm]
|
|
|
|
#
|
|
|
|
# (c) 2007 - Tom Eastep (teastep@shorewall.net)
|
|
|
|
#
|
|
|
|
# Complete documentation is available at http://shorewall.net
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of Version 2 of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
|
|
|
|
#
|
|
|
|
#
|
2007-03-14 04:12:22 +01:00
|
|
|
package Shorewall::Chains;
|
|
|
|
require Exporter;
|
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
use Shorewall::Common;
|
|
|
|
use Shorewall::Config;
|
|
|
|
use Shorewall::Zones;
|
|
|
|
use Shorewall::Interfaces;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
2007-03-14 04:12:22 +01:00
|
|
|
our @ISA = qw(Exporter);
|
2007-03-15 01:34:17 +01:00
|
|
|
our @EXPORT = qw( STANDARD
|
|
|
|
NATRULE
|
|
|
|
BUILTIN
|
|
|
|
NONAT
|
|
|
|
NATONLY
|
|
|
|
REDIRECT
|
|
|
|
ACTION
|
|
|
|
MACRO
|
|
|
|
LOGRULE
|
2007-03-18 19:50:34 +01:00
|
|
|
NO_RESTRICT
|
|
|
|
PREROUTE_RESTRICT
|
2007-03-25 21:27:25 +02:00
|
|
|
INPUT_RESTRICT
|
|
|
|
OUTPUT_RESTRICT
|
2007-03-18 19:50:34 +01:00
|
|
|
POSTROUTE_RESTRICT
|
2007-03-25 23:04:24 +02:00
|
|
|
ALL_RESTRICT
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-19 00:06:46 +01:00
|
|
|
add_command
|
2007-03-15 01:34:17 +01:00
|
|
|
add_rule
|
2007-03-14 04:12:22 +01:00
|
|
|
insert_rule
|
|
|
|
chain_base
|
|
|
|
forward_chain
|
|
|
|
input_chain
|
|
|
|
output_chain
|
|
|
|
masq_chain
|
|
|
|
syn_chain
|
|
|
|
mac_chain
|
|
|
|
macrecent_target
|
|
|
|
dynamic_fwd
|
|
|
|
dynamic_in
|
|
|
|
dynamic_out
|
2007-03-14 05:06:32 +01:00
|
|
|
dynamic_chains
|
2007-03-14 04:12:22 +01:00
|
|
|
dnat_chain
|
|
|
|
snat_chain
|
|
|
|
ecn_chain
|
|
|
|
first_chains
|
2007-03-14 05:06:32 +01:00
|
|
|
new_chain
|
|
|
|
ensure_chain
|
|
|
|
ensure_filter_chain
|
2007-03-21 00:13:17 +01:00
|
|
|
ensure_mangle_chain
|
2007-03-14 05:06:32 +01:00
|
|
|
new_standard_chain
|
|
|
|
new_builtin_chain
|
|
|
|
initialize_chain_table
|
|
|
|
finish_section
|
2007-03-15 01:34:17 +01:00
|
|
|
newexclusionchain
|
|
|
|
clearrule
|
|
|
|
do_proto
|
|
|
|
mac_match
|
|
|
|
numeric_value
|
|
|
|
verify_mark
|
|
|
|
verify_small_mark
|
|
|
|
validate_mark
|
|
|
|
do_test
|
|
|
|
do_ratelimit
|
|
|
|
do_user
|
2007-03-21 17:43:42 +01:00
|
|
|
do_tos
|
2007-03-15 01:34:17 +01:00
|
|
|
iprange_match
|
|
|
|
match_source_net
|
|
|
|
match_dest_net
|
|
|
|
match_orig_dest
|
|
|
|
match_ipsec_in
|
|
|
|
match_ipsec_out
|
|
|
|
log_rule_limit
|
|
|
|
log_rule
|
|
|
|
expand_rule
|
|
|
|
addnatjump
|
|
|
|
insertnatjump
|
2007-03-27 20:41:55 +02:00
|
|
|
get_interface_addresses
|
2007-03-15 02:18:29 +01:00
|
|
|
create_netfilter_load
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 04:12:22 +01:00
|
|
|
@policy_chains
|
|
|
|
%chain_table
|
|
|
|
$nat_table
|
|
|
|
$mangle_table
|
2007-03-14 05:29:14 +01:00
|
|
|
$filter_table
|
2007-03-15 01:34:17 +01:00
|
|
|
$section
|
|
|
|
%sections
|
|
|
|
$comment
|
|
|
|
%targets
|
|
|
|
);
|
2007-03-14 04:12:22 +01:00
|
|
|
our @EXPORT_OK = ();
|
|
|
|
our @VERSION = 1.00;
|
|
|
|
|
|
|
|
#
|
|
|
|
# Chain Table
|
|
|
|
#
|
|
|
|
# @policy_chains is a list of references to policy chains in the filter table
|
|
|
|
#
|
|
|
|
# %chain_table { <table> => { <chain1> => { name => <chain name>
|
2007-03-31 00:38:09 +02:00
|
|
|
# table => <table name>
|
2007-03-14 04:12:22 +01:00
|
|
|
# is_policy => 0|1
|
|
|
|
# is_optionsl => 0|1
|
|
|
|
# referenced => 0|1
|
|
|
|
# policy => <policy>
|
|
|
|
# loglevel => <level>
|
|
|
|
# synparams => <burst/limit>
|
|
|
|
# default => <default action>
|
|
|
|
# policy_chain => <ref to policy chain -- self-reference if this is a policy chain>
|
|
|
|
# rules => [ <rule1>
|
|
|
|
# <rule2>
|
|
|
|
# ...
|
|
|
|
# ]
|
2007-03-31 00:38:09 +02:00
|
|
|
# } ,
|
|
|
|
# <chain2> => ...
|
2007-03-30 22:18:48 +02:00
|
|
|
# }
|
|
|
|
# }
|
2007-03-14 04:12:22 +01:00
|
|
|
#
|
|
|
|
# 'is_optional' only applies to policy chains; when true, indicates that this is a provisional policy chain which might be
|
|
|
|
# replaced. Policy chains created under the IMPLICIT_CONTINUE=Yes option are optional.
|
|
|
|
#
|
|
|
|
# Only 'referenced' chains get written to the iptables-restore output.
|
|
|
|
#
|
|
|
|
# 'loglevel', 'synparams' and 'default' only apply to policy chains.
|
|
|
|
#
|
2007-03-14 05:29:14 +01:00
|
|
|
our @policy_chains;
|
|
|
|
our %chain_table = ( raw => {} ,
|
|
|
|
mangle => {},
|
|
|
|
nat => {},
|
|
|
|
filter => {} );
|
2007-03-14 04:12:22 +01:00
|
|
|
|
2007-03-14 05:29:14 +01:00
|
|
|
our $nat_table = $chain_table{nat};
|
|
|
|
our $mangle_table = $chain_table{mangle};
|
|
|
|
our $filter_table = $chain_table{filter};
|
2007-03-15 01:34:17 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# These get set to 1 as sections are encountered.
|
|
|
|
#
|
|
|
|
our %sections = ( ESTABLISHED => 0,
|
|
|
|
RELATED => 0,
|
|
|
|
NEW => 0
|
|
|
|
);
|
2007-03-14 05:29:14 +01:00
|
|
|
#
|
|
|
|
# Current rules file section.
|
|
|
|
#
|
|
|
|
our $section = 'ESTABLISHED';
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Contents of last COMMENT line.
|
|
|
|
#
|
|
|
|
our $comment = '';
|
|
|
|
# Target Table. Each entry maps a target to a set of flags defined as follows.
|
|
|
|
#
|
|
|
|
use constant { STANDARD => 1, #defined by Netfilter
|
|
|
|
NATRULE => 2, #Involved NAT
|
|
|
|
BUILTIN => 4, #A built-in action
|
|
|
|
NONAT => 8, #'NONAT' or 'ACCEPT+'
|
|
|
|
NATONLY => 16, #'DNAT-' or 'REDIRECT-'
|
|
|
|
REDIRECT => 32, #'REDIRECT'
|
|
|
|
ACTION => 64, #An action
|
|
|
|
MACRO => 128, #A Macro
|
|
|
|
LOGRULE => 256, #'LOG'
|
|
|
|
};
|
|
|
|
#
|
|
|
|
# As new targets (Actions and Macros) are discovered, they are added to the table
|
|
|
|
#
|
|
|
|
our %targets = ('ACCEPT' => STANDARD,
|
|
|
|
'ACCEPT+' => STANDARD + NONAT,
|
|
|
|
'ACCEPT!' => STANDARD,
|
|
|
|
'NONAT' => STANDARD + NONAT,
|
|
|
|
'DROP' => STANDARD,
|
|
|
|
'DROP!' => STANDARD,
|
|
|
|
'REJECT' => STANDARD,
|
|
|
|
'REJECT!' => STANDARD,
|
|
|
|
'DNAT' => NATRULE,
|
|
|
|
'DNAT-' => NATRULE + NATONLY,
|
|
|
|
'REDIRECT' => NATRULE + REDIRECT,
|
|
|
|
'REDIRECT-' => NATRULE + REDIRECT + NATONLY,
|
|
|
|
'LOG' => STANDARD + LOGRULE,
|
|
|
|
'CONTINUE' => STANDARD,
|
|
|
|
'QUEUE' => STANDARD,
|
|
|
|
'SAME' => NATRULE,
|
|
|
|
'SAME-' => NATRULE + NATONLY,
|
|
|
|
'dropBcast' => BUILTIN + ACTION,
|
|
|
|
'allowBcast' => BUILTIN + ACTION,
|
|
|
|
'dropNotSyn' => BUILTIN + ACTION,
|
|
|
|
'rejNotSyn' => BUILTIN + ACTION,
|
|
|
|
'dropInvalid' => BUILTIN + ACTION,
|
|
|
|
'allowInvalid' => BUILTIN + ACTION,
|
|
|
|
'allowinUPnP' => BUILTIN + ACTION,
|
|
|
|
'forwardUPnP' => BUILTIN + ACTION,
|
|
|
|
'Limit' => BUILTIN + ACTION,
|
|
|
|
);
|
2007-03-18 19:50:34 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# expand_rule() restrictions
|
|
|
|
#
|
|
|
|
use constant { NO_RESTRICT => 0,
|
|
|
|
PREROUTE_RESTRICT => 1,
|
2007-03-25 21:27:25 +02:00
|
|
|
INPUT_RESTRICT => 4,
|
|
|
|
OUTPUT_RESTRICT => 8,
|
|
|
|
POSTROUTE_RESTRICT => 16,
|
|
|
|
ALL_RESTRICT => 12
|
|
|
|
};
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Used to sequence 'exclusion' chains with names 'excl0', 'excl1', ...
|
|
|
|
#
|
|
|
|
my $exclseq = 0;
|
|
|
|
#
|
|
|
|
# Used to suppress duplicate match specifications.
|
|
|
|
#
|
|
|
|
my $iprangematch = 0;
|
|
|
|
#
|
2007-03-18 22:16:46 +01:00
|
|
|
# Sequence for naming temporary chains
|
2007-03-14 04:12:22 +01:00
|
|
|
#
|
2007-03-18 22:16:46 +01:00
|
|
|
my $chainseq;
|
2007-03-14 04:12:22 +01:00
|
|
|
#
|
2007-03-18 22:16:46 +01:00
|
|
|
# Add a run-time command to a chain. Arguments are:
|
|
|
|
#
|
|
|
|
# Chain reference , Command
|
|
|
|
#
|
2007-03-23 18:30:12 +01:00
|
|
|
|
|
|
|
#
|
2007-03-23 20:19:43 +01:00
|
|
|
# Count of the number of unclosed loops in generated shell code. We insert shell code
|
|
|
|
# into the Chain tables 'rules' array (proceeded by '~'). create_netfilter_load()
|
|
|
|
# emits that code inline for execution at run-time.
|
2007-03-23 18:30:12 +01:00
|
|
|
#
|
|
|
|
my $loopcount = 0;
|
|
|
|
|
2007-03-18 22:16:46 +01:00
|
|
|
sub add_command($$)
|
2007-03-14 04:12:22 +01:00
|
|
|
{
|
2007-03-18 22:16:46 +01:00
|
|
|
my ($chainref, $command) = @_;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-26 02:46:15 +02:00
|
|
|
push @{$chainref->{rules}}, join ('', '~', ' ' x $loopcount, $command );
|
2007-03-14 04:12:22 +01:00
|
|
|
|
|
|
|
$chainref->{referenced} = 1;
|
|
|
|
}
|
2007-03-23 20:19:43 +01:00
|
|
|
|
2007-03-18 19:50:34 +01:00
|
|
|
#
|
2007-03-18 22:16:46 +01:00
|
|
|
# Add a rule to a chain. Arguments are:
|
2007-03-18 19:50:34 +01:00
|
|
|
#
|
2007-03-18 22:16:46 +01:00
|
|
|
# Chain reference , Rule
|
2007-03-18 19:50:34 +01:00
|
|
|
#
|
2007-03-18 22:16:46 +01:00
|
|
|
sub add_rule($$)
|
2007-03-18 19:50:34 +01:00
|
|
|
{
|
2007-03-18 22:16:46 +01:00
|
|
|
my ($chainref, $rule) = @_;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-18 22:16:46 +01:00
|
|
|
$rule .= " -m comment --comment \"$comment\"" if $comment;
|
2007-03-18 19:50:34 +01:00
|
|
|
|
2007-03-23 18:30:12 +01:00
|
|
|
if ( $loopcount ) {
|
|
|
|
add_command $chainref , qq(echo "-A $chainref->{name} $rule" >&3);
|
|
|
|
} else {
|
|
|
|
push @{$chainref->{rules}}, $rule;
|
2007-03-18 19:50:34 +01:00
|
|
|
|
2007-03-23 18:30:12 +01:00
|
|
|
$chainref->{referenced} = 1;
|
|
|
|
}
|
2007-03-18 19:50:34 +01:00
|
|
|
|
2007-03-18 22:16:46 +01:00
|
|
|
$iprangematch = 0;
|
2007-03-18 19:50:34 +01:00
|
|
|
}
|
|
|
|
|
2007-03-14 04:12:22 +01:00
|
|
|
#
|
|
|
|
# Insert a rule into a chain. Arguments are:
|
|
|
|
#
|
|
|
|
# Table , Chain , Rule Number, Rule
|
|
|
|
#
|
|
|
|
sub insert_rule($$$)
|
|
|
|
{
|
|
|
|
my ($chainref, $number, $rule) = @_;
|
2007-03-23 18:30:12 +01:00
|
|
|
|
|
|
|
fatal_error 'Internal Error in insert_rule()' if $loopcount;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 04:12:22 +01:00
|
|
|
$rule .= "-m comment --comment \"$comment\"" if $comment;
|
|
|
|
|
|
|
|
splice @{$chainref->{rules}}, $number - 1, 0, $rule;
|
|
|
|
|
|
|
|
$chainref->{referenced} = 1;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 04:12:22 +01:00
|
|
|
$iprangematch = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Form the name of a chain.
|
|
|
|
#
|
|
|
|
sub chain_base($) {
|
|
|
|
my $chain = $_[0];
|
|
|
|
|
|
|
|
$chain =~ s/^@/at_/;
|
2007-03-26 17:22:36 +02:00
|
|
|
$chain =~ tr/[.\-%@]/_/;
|
2007-03-25 23:04:24 +02:00
|
|
|
$chain =~ s/\+$//;
|
2007-03-14 04:12:22 +01:00
|
|
|
$chain;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Forward Chain for an interface
|
|
|
|
#
|
|
|
|
sub forward_chain($)
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
chain_base( $_[0] ) . '_fwd';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Input Chain for an interface
|
|
|
|
#
|
|
|
|
sub input_chain($)
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
chain_base( $_[0] ) . '_in';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Output Chain for an interface
|
|
|
|
#
|
|
|
|
sub output_chain($)
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
chain_base( $_[0] ) . '_out';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Masquerade Chain for an interface
|
|
|
|
#
|
|
|
|
sub masq_chain($)
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
chain_base( $_[0] ) . '_masq';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Syn_chain
|
|
|
|
#
|
|
|
|
sub syn_chain ( $ ) {
|
|
|
|
'@' . $_[0];
|
|
|
|
}
|
|
|
|
#
|
|
|
|
# MAC Verification Chain for an interface
|
|
|
|
#
|
|
|
|
sub mac_chain( $ )
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
chain_base( $_[0] ) . '_mac';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub macrecent_target($)
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
$config{MACLIST_TTL} ? chain_base( $_[0] ) . '_rec' : 'RETURN';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Functions for creating dynamic zone rules
|
|
|
|
#
|
|
|
|
sub dynamic_fwd( $ )
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
chain_base( $_[0] ) . '_dynf';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub dynamic_in( $ )
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
chain_base( $_[0] ) . '_dyni';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub dynamic_out( $ ) # $1 = interface
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
chain_base( $_[0] ) . '_out';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub dynamic_chains( $ ) #$1 = interface
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
my $c = chain_base( $_[0] );
|
2007-03-14 04:12:22 +01:00
|
|
|
|
|
|
|
[ $c . '_dyni' , $c . '_dynf' , $c . '_dyno' ];
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# DNAT Chain from a zone
|
|
|
|
#
|
|
|
|
sub dnat_chain( $ )
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
chain_base( $_[0] ) . '_dnat';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# SNAT Chain to an interface
|
|
|
|
#
|
|
|
|
sub snat_chain( $ )
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
chain_base( $_[0] ) . '_snat';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# ECN Chain to an interface
|
|
|
|
#
|
|
|
|
sub ecn_chain( $ )
|
|
|
|
{
|
2007-03-26 17:22:36 +02:00
|
|
|
chain_base( $_[0] ) . '_ecn';
|
2007-03-14 04:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# First chains for an interface
|
|
|
|
#
|
|
|
|
sub first_chains( $ ) #$1 = interface
|
|
|
|
{
|
|
|
|
my $c = chain_base $_[0];
|
|
|
|
|
|
|
|
[ $c . '_fwd', $c . '_in' ];
|
|
|
|
}
|
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
#
|
|
|
|
# Create a new chain and return a reference to it.
|
|
|
|
#
|
|
|
|
sub new_chain($$)
|
|
|
|
{
|
|
|
|
my ($table, $chain) = @_;
|
|
|
|
my %ch;
|
|
|
|
my @rules;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
$ch{name} = $chain;
|
2007-03-31 19:44:16 +02:00
|
|
|
$ch{log} = 1 if $globals{LOGRULENUMBERS};
|
2007-03-14 05:06:32 +01:00
|
|
|
$ch{rules} = \@rules;
|
|
|
|
$ch{table} = $table;
|
|
|
|
$chain_table{$table}{$chain} = \%ch;
|
|
|
|
\%ch;
|
|
|
|
}
|
|
|
|
|
2007-03-18 22:16:46 +01:00
|
|
|
#
|
|
|
|
# Create an anonymous chain
|
|
|
|
#
|
|
|
|
sub new_anon_chain( $ ) {
|
|
|
|
my $chainref = $_[0];
|
|
|
|
my $seq = $chainseq++;
|
|
|
|
new_chain( $chainref->{table}, 'chain' . "$seq" );
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2007-03-14 05:06:32 +01:00
|
|
|
#
|
|
|
|
# Create a chain if it doesn't exist already
|
|
|
|
#
|
|
|
|
sub ensure_chain($$)
|
|
|
|
{
|
|
|
|
my ($table, $chain) = @_;
|
|
|
|
|
|
|
|
my $ref = $chain_table{$table}{$chain};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
return $ref if $ref;
|
|
|
|
|
|
|
|
new_chain $table, $chain;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub finish_chain_section( $$ );
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a filter chain if necessary. Optionally populate it with the appropriate ESTABLISHED,RELATED rule(s) and perform SYN rate limiting.
|
|
|
|
#
|
|
|
|
sub ensure_filter_chain( $$ )
|
|
|
|
{
|
|
|
|
my ($chain, $populate) = @_;
|
|
|
|
|
|
|
|
my $chainref = $filter_table->{$chain};
|
|
|
|
|
|
|
|
$chainref = new_chain 'filter' , $chain unless $chainref;
|
|
|
|
|
|
|
|
if ( $populate and ! $chainref->{referenced} ) {
|
|
|
|
if ( $section eq 'NEW' or $section eq 'DONE' ) {
|
|
|
|
finish_chain_section $chainref , 'ESTABLISHED,RELATED';
|
|
|
|
} elsif ( $section eq 'ESTABLISHED' ) {
|
|
|
|
finish_chain_section $chainref , 'ESTABLISHED';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$chainref->{referenced} = 1;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
$chainref;
|
|
|
|
}
|
|
|
|
|
2007-03-21 00:13:17 +01:00
|
|
|
sub ensure_mangle_chain($) {
|
|
|
|
my $chain = $_[0];
|
|
|
|
|
|
|
|
my $chainref = ensure_chain 'mangle', $chain;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-21 00:13:17 +01:00
|
|
|
$chainref->{referenced} = 1;
|
|
|
|
|
|
|
|
$chainref;
|
|
|
|
}
|
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
#
|
|
|
|
# Add a builtin chain
|
|
|
|
#
|
|
|
|
sub new_builtin_chain($$$)
|
|
|
|
{
|
2007-03-25 03:03:43 +02:00
|
|
|
my ( $table, $chain, $policy ) = @_;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-25 03:03:43 +02:00
|
|
|
my $chainref = new_chain $table, $chain;
|
2007-03-14 05:06:32 +01:00
|
|
|
$chainref->{referenced} = 1;
|
2007-03-25 03:03:43 +02:00
|
|
|
$chainref->{policy} = $policy;
|
2007-03-14 05:06:32 +01:00
|
|
|
$chainref->{builtin} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub new_standard_chain($) {
|
|
|
|
my $chainref = new_chain 'filter' ,$_[0];
|
|
|
|
$chainref->{referenced} = 1;
|
|
|
|
$chainref;
|
2007-03-27 01:17:46 +02:00
|
|
|
}
|
2007-03-14 05:06:32 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Add all builtin chains to the chain table
|
|
|
|
#
|
|
|
|
#
|
|
|
|
sub initialize_chain_table()
|
|
|
|
{
|
|
|
|
for my $chain qw/OUTPUT PREROUTING/ {
|
|
|
|
new_builtin_chain 'raw', $chain, 'ACCEPT';
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $chain qw/INPUT OUTPUT FORWARD/ {
|
|
|
|
new_builtin_chain 'filter', $chain, 'DROP';
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $chain qw/PREROUTING POSTROUTING OUTPUT/ {
|
|
|
|
new_builtin_chain 'nat', $chain, 'ACCEPT';
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $chain qw/PREROUTING INPUT FORWARD OUTPUT POSTROUTING/ {
|
|
|
|
new_builtin_chain 'mangle', $chain, 'ACCEPT';
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
if ( $capabilities{MANGLE_FORWARD} ) {
|
|
|
|
for my $chain qw/ FORWARD POSTROUTING / {
|
|
|
|
new_builtin_chain 'mangle', $chain, 'ACCEPT';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Add ESTABLISHED,RELATED rules and synparam jumps to the passed chain
|
|
|
|
#
|
|
|
|
sub finish_chain_section ($$) {
|
|
|
|
my ($chainref, $state ) = @_;
|
|
|
|
my $chain = $chainref->{name};
|
|
|
|
|
|
|
|
add_rule $chainref, "-m state --state $state -j ACCEPT" unless $config{FASTACCEPT};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
if ($sections{RELATED} ) {
|
|
|
|
if ( $chainref->{is_policy} ) {
|
|
|
|
if ( $chainref->{synparams} ) {
|
|
|
|
my $synchainref = ensure_chain 'filter', "\@$chain";
|
|
|
|
if ( $section eq 'DONE' ) {
|
|
|
|
if ( $chainref->{policy} =~ /^(ACCEPT|CONTINUE|QUEUE)$/ ) {
|
|
|
|
add_rule $chainref, "-p tcp --syn -j $synchainref->{name}";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
add_rule $chainref, "-p tcp --syn -j $synchainref->{name}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
my $policychainref = $chainref->{policychain};
|
|
|
|
if ( $policychainref->{synparams} ) {
|
|
|
|
my $synchainref = ensure_chain 'filter', "\@$policychainref->{name}";
|
|
|
|
add_rule $synchainref, "-p tcp --syn -j $synchainref->{name}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
}
|
2007-03-14 05:06:32 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Do section-end processing
|
|
|
|
#
|
|
|
|
sub finish_section ( $ ) {
|
|
|
|
my $sections = $_[0];
|
|
|
|
|
|
|
|
for my $zone ( @zones ) {
|
|
|
|
for my $zone1 ( @zones ) {
|
2007-03-15 00:09:05 +01:00
|
|
|
my $chainref = $chain_table{'filter'}{"$zone}2${zone1}"};
|
2007-03-14 05:06:32 +01:00
|
|
|
if ( $chainref->{referenced} ) {
|
|
|
|
finish_chain_section $chainref, $sections;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
sub newexclusionchain() {
|
|
|
|
my $seq = $exclseq++;
|
|
|
|
"excl${seq}";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub clearrule() {
|
2007-03-24 23:26:32 +01:00
|
|
|
$iprangematch = 0;
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Handle parsing of PROTO, DEST PORT(S) , SOURCE PORTS(S). Returns the appropriate match string.
|
|
|
|
#
|
|
|
|
sub do_proto( $$$ )
|
|
|
|
{
|
|
|
|
my ($proto, $ports, $sports ) = @_;
|
|
|
|
|
|
|
|
my $output = '';
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
$proto = '' if $proto eq '-';
|
|
|
|
$ports = '' if $ports eq '-';
|
|
|
|
$sports = '' if $sports eq '-';
|
|
|
|
|
|
|
|
if ( $proto ) {
|
|
|
|
if ( $proto =~ /^(tcp|udp|6|17)$/i ) {
|
|
|
|
$output = "-p $proto ";
|
|
|
|
if ( $ports ) {
|
|
|
|
my @ports = split /,/, $ports;
|
|
|
|
my $count = @ports;
|
|
|
|
|
|
|
|
if ( $count > 1 ) {
|
|
|
|
fatal_error "Port list requires Multiport support in your kernel/iptables: $ports" unless $capabilities{MULTIPORT};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
for my $port ( @ports ) {
|
2007-03-21 01:55:01 +01:00
|
|
|
if ( $port =~ /:/ ) {
|
|
|
|
fatal_error "Port range in a list requires Extended Multiport Support in your kernel/iptables: $ports" unless $capabilities{XMULTIPORT};
|
|
|
|
$count++;
|
|
|
|
}
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
fatal_error "Too many entries in port list: $ports" if $count > 15;
|
|
|
|
|
|
|
|
$output .= "-m multiport --dports $ports ";
|
|
|
|
} else {
|
|
|
|
$output .= "--dport $ports ";
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
if ( $sports ) {
|
|
|
|
my @ports = split /,/, $sports;
|
|
|
|
my $count = @ports;
|
|
|
|
|
|
|
|
if ( $count > 1 ) {
|
|
|
|
fatal_error "Port list requires Multiport support in your kernel/iptables: $sports" unless $capabilities{MULTIPORT};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
for my $port ( @ports ) {
|
2007-03-21 01:55:01 +01:00
|
|
|
if ( $port =~ /:/ ) {
|
|
|
|
fatal_error "Port range in a list requires Extended Multiport Support in your kernel/iptables: $sports" unless $capabilities{XMULTIPORT};
|
|
|
|
$count++;
|
|
|
|
}
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
fatal_error "Too many entries in port list: $sports" if $count > 15;
|
|
|
|
|
|
|
|
$output .= "-m multiport --sports $sports ";
|
|
|
|
} else {
|
|
|
|
$output .= "--sport $sports ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elsif ( $proto =~ /^(icmp|1)$/i ) {
|
|
|
|
$output .= "-p icmp --icmp-type $ports " if $ports;
|
|
|
|
fatal_error 'SOURCE PORT(S) not permitted with ICMP' if $sports;
|
|
|
|
} elsif ( $proto =~ /^(ipp2p(:(tcp|udp|all)))?$/i ) {
|
2007-03-29 03:29:27 +02:00
|
|
|
require_capability( 'IPP2P' , 'PROTO = ipp2p' );
|
2007-03-15 01:34:17 +01:00
|
|
|
$proto = $2 ? $3 : 'tcp';
|
|
|
|
$ports = 'ipp2p' unless $ports;
|
|
|
|
$output .= "-p $proto -m ipp2p --$ports ";
|
2007-03-20 21:06:52 +01:00
|
|
|
} else {
|
|
|
|
$output .= "-p $proto ";
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
|
|
|
} elsif ( $ports || $sports ) {
|
|
|
|
fatal_error "SOURCE/DEST PORT(S) not allowed without PROTO, rule \"$line\""
|
|
|
|
}
|
|
|
|
|
|
|
|
$output;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub mac_match( $ ) {
|
|
|
|
my $mac = $_[0];
|
|
|
|
|
|
|
|
$mac =~ s/^(!?)~//;
|
2007-03-25 03:03:43 +02:00
|
|
|
$mac =~ s/^!// if my $invert = ( $1 ? '! ' : '');
|
2007-03-15 01:34:17 +01:00
|
|
|
$mac =~ s/-/:/g;
|
|
|
|
|
|
|
|
"--match mac --mac-source ${invert}$mac ";
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Convert value to decimal number
|
|
|
|
#
|
|
|
|
sub numeric_value ( $ ) {
|
|
|
|
my $mark = $_[0];
|
2007-03-31 00:38:09 +02:00
|
|
|
fatal_error "Invalid Numeric Value" unless "\L$mark" =~ /^(0x[a-f0-9]+|0[0-7]*|[1-9]\d*)$/;
|
2007-03-15 01:34:17 +01:00
|
|
|
$mark =~ /^0x/ ? hex $mark : $mark =~ /^0/ ? oct $mark : $mark;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Mark validatation functions
|
|
|
|
#
|
|
|
|
sub verify_mark( $ ) {
|
|
|
|
my $mark = $_[0];
|
|
|
|
my $limit = $config{HIGH_ROUTE_MARKS} ? 0xFFFF : 0xFF;
|
|
|
|
|
|
|
|
fatal_error "Invalid Mark or Mask value: $mark"
|
2007-03-31 00:38:09 +02:00
|
|
|
unless numeric_value( $mark ) <= $limit;
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub verify_small_mark( $ ) {
|
|
|
|
verify_mark ( (my $mark) = $_[0] );
|
|
|
|
fatal_error "Mark value ($mark) too large" if numeric_value( $mark ) > 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub validate_mark( $ ) {
|
|
|
|
for ( split '/', $_[0] ) {
|
|
|
|
verify_mark $_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Generate an appropriate -m [conn]mark match string for the contents of a MARK column
|
|
|
|
#
|
|
|
|
|
|
|
|
sub do_test ( $$ )
|
|
|
|
{
|
|
|
|
my ($testval, $mask) = @_;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
return '' unless $testval and $testval ne '-';
|
|
|
|
|
|
|
|
my $invert = $testval =~ s/^!// ? '! ' : '';
|
|
|
|
my $match = $testval =~ s/:C$// ? '-m connmark ' : '-m mark ';
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
$testval .= '/0xFF' unless ( $testval =~ '/' );
|
|
|
|
|
|
|
|
"${invert}$match $testval ";
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Create a "-m limit" match for the passed LIMIT/BURST
|
|
|
|
#
|
|
|
|
sub do_ratelimit( $ ) {
|
|
|
|
my $rate = $_[0];
|
|
|
|
|
|
|
|
return '' unless $rate and $rate ne '-';
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
if ( $rate =~ /^([^:]+):([^:]+)$/ ) {
|
|
|
|
"-m limit --limit $1 --limit-burst $2 ";
|
|
|
|
} else {
|
|
|
|
"-m limit --limit $rate ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a "-m owner" match for the passed USER/GROUP
|
|
|
|
#
|
|
|
|
sub do_user( $ ) {
|
|
|
|
my $user = $_[0];
|
|
|
|
my $rule = ' -m owner';
|
|
|
|
|
|
|
|
return '' unless $user and $user ne '-';
|
|
|
|
|
|
|
|
if ( $user =~ /^(!)?(.*)\+(.*)$/ ) {
|
|
|
|
$rule .= "! --cmd-owner $2 " if $2;
|
|
|
|
$user = "!$1";
|
|
|
|
} elsif ( $user =~ /^(.*)\+(.*)$/ ) {
|
|
|
|
$rule .= "--cmd-owner $2 " if $2;
|
|
|
|
$user = $1;
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
if ( $user =~ /^!(.*):(.*)$/ ) {
|
|
|
|
$rule .= "! --uid-owner $1 " if $1;
|
|
|
|
$rule .= "! --gid-owner $2 " if $2;
|
|
|
|
} elsif ( $user =~ /^(.*):(.*)$/ ) {
|
|
|
|
$rule .= "--uid-owner $1 " if $1;
|
|
|
|
$rule .= "--gid-owner $2 " if $2;
|
|
|
|
} elsif ( $user =~ /^!/ ) {
|
|
|
|
$rule .= "! --uid-owner $user ";
|
|
|
|
} else {
|
|
|
|
$rule .= "--uid-owner $user ";
|
|
|
|
}
|
|
|
|
|
|
|
|
$rule;
|
|
|
|
}
|
2007-03-21 17:43:42 +01:00
|
|
|
|
|
|
|
#
|
2007-03-21 18:09:52 +01:00
|
|
|
# Create a "-m tos" match for the passed TOS
|
2007-03-21 17:43:42 +01:00
|
|
|
#
|
|
|
|
sub do_tos( $ ) {
|
|
|
|
my $tos = $_[0];
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-21 18:09:52 +01:00
|
|
|
$tos ne '-' ? "-m tos --tos $tos " : '';
|
2007-03-27 01:17:46 +02:00
|
|
|
}
|
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Avoid generating a second '-m iprange' in a single rule.
|
|
|
|
#
|
|
|
|
sub iprange_match() {
|
|
|
|
my $match = '';
|
2007-03-21 18:09:52 +01:00
|
|
|
|
2007-03-29 03:29:27 +02:00
|
|
|
require_capability( 'IPRANGE_MATCH' , 'Address Ranges' );
|
2007-03-15 01:34:17 +01:00
|
|
|
unless ( $iprangematch ) {
|
|
|
|
$match = '-m iprange ';
|
|
|
|
$iprangematch = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$match;
|
|
|
|
}
|
|
|
|
|
2007-03-24 16:59:17 +01:00
|
|
|
#
|
|
|
|
# Get set flags (ipsets).
|
|
|
|
#
|
|
|
|
sub get_set_flags( $$ ) {
|
|
|
|
my ( $setname, $option ) = @_;
|
|
|
|
my $options = $option;
|
|
|
|
|
|
|
|
fatal_error "Your kernel and/or iptables does not include ipset match: $setname" unless $capabilities{IPSET_MATCH};
|
|
|
|
|
|
|
|
if ( $setname =~ /(.*)\[([1-6])\]$/ ) {
|
|
|
|
$setname = $1;
|
|
|
|
my $count = $2;
|
|
|
|
$options .= ",$option" while --$count > 0;
|
|
|
|
} elsif ( $setname =~ /(.+)\[(.*)\]$/ ) {
|
|
|
|
$setname = $1;
|
|
|
|
$options = $2;
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-24 16:59:17 +01:00
|
|
|
$setname =~ s/^\+//;
|
|
|
|
|
|
|
|
"--set $setname $options"
|
|
|
|
}
|
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Match a Source. Currently only handles IP addresses and ranges
|
|
|
|
#
|
|
|
|
sub match_source_net( $ ) {
|
|
|
|
my $net = $_[0];
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
if ( $net =~ /^(!?).*\..*\..*\..*-.*\..*\..*\..*/ ) {
|
|
|
|
$net =~ s/!// if my $invert = $1 ? '! ' : '';
|
|
|
|
iprange_match . "${invert}--src-range $net ";
|
|
|
|
} elsif ( $net =~ /^(!?)~(.*)$/ ) {
|
|
|
|
( $net = $2 ) =~ s/-/:/g;
|
2007-03-24 16:59:17 +01:00
|
|
|
"-m mac --mac-source $1 $net ";
|
|
|
|
} elsif ( $net =~ /^(!?)\+/ ) {
|
2007-03-29 03:29:27 +02:00
|
|
|
require_capability( 'IPSET_MATCH' , 'ipset names in Shorewall configuration files' );
|
2007-03-26 02:46:15 +02:00
|
|
|
join( '', '-m set ', $1 ? '! ' : '', get_set_flags( $net, 'src' ) );
|
2007-03-15 01:34:17 +01:00
|
|
|
} elsif ( $net =~ /^!/ ) {
|
|
|
|
$net =~ s/!//;
|
|
|
|
"-s ! $net ";
|
|
|
|
} else {
|
|
|
|
$net eq ALLIPv4 ? '' : "-s $net ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Match a Source. Currently only handles IP addresses and ranges
|
|
|
|
#
|
|
|
|
sub match_dest_net( $ ) {
|
|
|
|
my $net = $_[0];
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
if ( $net =~ /^(!?).*\..*\..*\..*-.*\..*\..*\..*/ ) {
|
|
|
|
$net =~ s/!// if my $invert = $1 ? '! ' : '';
|
2007-03-21 18:09:52 +01:00
|
|
|
iprange_match . "${invert}--dst-range $net ";
|
2007-03-24 16:59:17 +01:00
|
|
|
} elsif ( $net =~ /^(!?)\+/ ) {
|
2007-03-29 03:29:27 +02:00
|
|
|
require_capability( 'IPSET_MATCH' , 'ipset names in Shorewall configuration files' );
|
2007-03-26 02:46:15 +02:00
|
|
|
join( '', '-m set ', $1 ? '! ' : '', get_set_flags( $net, 'dst' ) );
|
2007-03-15 01:34:17 +01:00
|
|
|
} elsif ( $net =~ /^!/ ) {
|
|
|
|
$net =~ s/!//;
|
|
|
|
"-d ! $net ";
|
|
|
|
} else {
|
|
|
|
$net eq ALLIPv4 ? '' : "-d $net ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Match original destination
|
|
|
|
#
|
|
|
|
sub match_orig_dest ( $ ) {
|
|
|
|
my $net = $_[0];
|
|
|
|
|
|
|
|
return '' if $net eq ALLIPv4;
|
2007-03-29 01:21:37 +02:00
|
|
|
return '' unless $capabilities{CONNTRACK_MATCH};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
if ( $net =~ /^!/ ) {
|
|
|
|
$net =~ s/!//;
|
|
|
|
"-m conntrack --ctorigdst ! $net ";
|
|
|
|
} else {
|
|
|
|
$net eq ALLIPv4 ? '' : "-m conntrack --ctorigdst $net ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Match Source IPSEC
|
|
|
|
#
|
|
|
|
sub match_ipsec_in( $$ ) {
|
|
|
|
my ( $zone , $hostref ) = @_;
|
|
|
|
my $match = '-m policy --dir in --pol ';
|
|
|
|
my $zoneref = $zones{$zone};
|
|
|
|
my $optionsref = $zoneref->{options};
|
|
|
|
|
|
|
|
if ( $zoneref->{type} eq 'ipsec4' ) {
|
|
|
|
$match .= "ipsec $optionsref->{in_out}{ipsec}$optionsref->{in}{ipsec}";
|
|
|
|
} elsif ( $capabilities{POLICY_MATCH} ) {
|
|
|
|
$match .= "$hostref->{ipsec} $optionsref->{in_out}{ipsec}$optionsref->{in}{ipsec}";
|
|
|
|
} else {
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Match Dest IPSEC
|
|
|
|
#
|
|
|
|
sub match_ipsec_out( $$ ) {
|
|
|
|
my ( $zone , $hostref ) = @_;
|
|
|
|
my $match = '-m policy --dir out --pol ';
|
|
|
|
my $zoneref = $zones{$zone};
|
|
|
|
my $optionsref = $zoneref->{options};
|
|
|
|
|
|
|
|
if ( $zoneref->{type} eq 'ipsec4' ) {
|
|
|
|
$match .= "ipsec $optionsref->{in_out}{ipsec}$optionsref->{out}{ipsec}";
|
|
|
|
} elsif ( $capabilities{POLICY_MATCH} ) {
|
|
|
|
$match .= "$hostref->{ipsec} $optionsref->{in_out}{ipsec}$optionsref->{out}{ipsec}"
|
|
|
|
} else {
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Generate a log message
|
|
|
|
#
|
|
|
|
sub log_rule_limit( $$$$$$$$ ) {
|
|
|
|
my ($level, $chainref, $chain, $disposition, $limit, $tag, $command, $predicates ) = @_;
|
|
|
|
|
|
|
|
my $prefix;
|
|
|
|
|
2007-03-31 19:44:16 +02:00
|
|
|
$limit = $globals{LOGLIMIT} unless $limit;
|
2007-03-15 01:34:17 +01:00
|
|
|
|
|
|
|
if ( $tag ) {
|
|
|
|
if ( $config{LOGTAGONLY} ) {
|
|
|
|
$chain = $tag;
|
|
|
|
$tag = '';
|
|
|
|
} else {
|
|
|
|
$tag .= ' ';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$tag = '' unless defined $tag;
|
|
|
|
}
|
|
|
|
|
2007-03-31 19:44:16 +02:00
|
|
|
if ( $globals{LOGRULENUMBERS} ) {
|
2007-03-15 01:34:17 +01:00
|
|
|
$prefix = (sprintf $config{LOGFORMAT} , $chain , $chainref->{log}++, $disposition ) . $tag;
|
|
|
|
} else {
|
|
|
|
$prefix = (sprintf $config{LOGFORMAT} , $chain , $disposition) . $tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( length $prefix > 29 ) {
|
|
|
|
$prefix = substr $prefix, 0, 29;
|
|
|
|
warning_message "Log Prefix shortened to \"$prefix\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $level eq 'ULOG' ) {
|
2007-03-31 19:44:16 +02:00
|
|
|
$prefix = "-j ULOG $globals{LOGPARMS} --ulog-prefix \"$prefix\" ";
|
2007-03-15 01:34:17 +01:00
|
|
|
} else {
|
2007-03-31 19:44:16 +02:00
|
|
|
$prefix = "-j LOG $globals{LOGPARMS} --log-level $level --log-prefix \"$prefix\" ";
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $command eq 'add' ) {
|
|
|
|
add_rule ( $chainref, $predicates . $prefix );
|
|
|
|
} else {
|
|
|
|
insert_rule ( $chainref , 1 , $predicates . $prefix );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub log_rule( $$$$ ) {
|
|
|
|
my ( $level, $chainref, $disposition, $predicates ) = @_;
|
|
|
|
|
2007-03-31 19:44:16 +02:00
|
|
|
log_rule_limit $level, $chainref, $chainref->{name} , $disposition, $globals{LOGLIMIT}, '', 'add', $predicates;
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
2007-03-23 23:47:21 +01:00
|
|
|
|
2007-03-25 18:38:00 +02:00
|
|
|
#
|
|
|
|
# Split a comma-separated source or destination host list but keep [...] together.
|
|
|
|
#
|
|
|
|
sub mysplit( $ ) {
|
|
|
|
my @input = split /,/, $_[0];
|
|
|
|
|
|
|
|
return @input unless $_[0] =~ /\[/;
|
|
|
|
|
|
|
|
my @result;
|
|
|
|
|
|
|
|
while ( @input ) {
|
|
|
|
my $element = shift @input;
|
|
|
|
|
|
|
|
if ( $element =~ /\[/ ) {
|
|
|
|
while ( ! ( $element =~ /\]/ ) ) {
|
|
|
|
last unless @input;
|
|
|
|
$element .= ( ',' . shift @input );
|
|
|
|
}
|
|
|
|
|
|
|
|
fatal_error "Invalid Host List ($_[0])" unless substr( $element, -1, 1 ) eq ']';
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-25 18:38:00 +02:00
|
|
|
push @result, $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
@result;
|
|
|
|
}
|
|
|
|
|
2007-03-23 23:47:21 +01:00
|
|
|
#
|
2007-03-27 20:41:55 +02:00
|
|
|
# Keep track of which interfaces have active 'address', 'addresses' and 'networks' variables
|
2007-03-23 23:47:21 +01:00
|
|
|
#
|
2007-03-27 20:41:55 +02:00
|
|
|
my %interfaceaddr;
|
2007-03-23 23:47:21 +01:00
|
|
|
my %interfaceaddrs;
|
2007-03-27 20:41:55 +02:00
|
|
|
my %interfacenets;
|
2007-03-23 23:47:21 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the name of the shell variable holding the first address of the passed interface
|
|
|
|
#
|
|
|
|
sub interface_address( $ ) {
|
|
|
|
chain_base( $_[0] ) . '_address';
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2007-03-27 20:41:55 +02:00
|
|
|
# Record that the ruleset requires the first IP address on the passed interface
|
2007-03-23 23:47:21 +01:00
|
|
|
#
|
2007-03-26 21:01:38 +02:00
|
|
|
sub get_interface_address ( $ ) {
|
|
|
|
my ( $interface ) = $_[0];
|
2007-03-27 20:41:55 +02:00
|
|
|
|
|
|
|
my $variable = interface_address( $interface );
|
2007-03-23 23:47:21 +01:00
|
|
|
|
2007-03-27 20:41:55 +02:00
|
|
|
$interfaceaddr{$interface} = "$variable=\$(find_first_interface_address $interface)";
|
|
|
|
|
|
|
|
"\$$variable";
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the name of the shell variable holding the addresses of the passed interface
|
|
|
|
#
|
|
|
|
sub interface_addresses( $ ) {
|
|
|
|
chain_base( $_[0] ) . '_addresses';
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Record that the ruleset requires the IP addresses on the passed interface
|
|
|
|
#
|
|
|
|
sub get_interface_addresses ( $ ) {
|
|
|
|
my ( $interface ) = $_[0];
|
|
|
|
|
|
|
|
my $variable = interface_addresses( $interface );
|
|
|
|
|
|
|
|
$interfaceaddr{$interface} = qq($variable=\$(get_interface_addresses $interface)
|
|
|
|
[ -n "\$$variable" ] || fatal_error "Unable to determine the IP address(es) of $interface"
|
|
|
|
);
|
|
|
|
|
|
|
|
"\$$variable";
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the name of the shell variable holding the networks routed out of the passed interface
|
|
|
|
#
|
|
|
|
sub interface_nets( $ ) {
|
|
|
|
chain_base( $_[0] ) . '_networks';
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Record that the ruleset requires the first IP address on the passed interface
|
|
|
|
#
|
|
|
|
sub get_interface_nets ( $ ) {
|
|
|
|
my ( $interface ) = $_[0];
|
|
|
|
|
|
|
|
my $variable = interface_nets( $interface );
|
|
|
|
|
|
|
|
$interfaceaddr{$interface} = qq($variable=\$(get_routed_networks $interface)
|
|
|
|
[ -n "\$$variable" ] || fatal_error "Unable to determine the routes through interface \\"$interface\\""
|
|
|
|
);
|
|
|
|
|
|
|
|
"\$$variable";
|
|
|
|
|
2007-03-23 23:47:21 +01:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# This function provides a uniform way to generate rules (something the original Shorewall sorely needed).
|
|
|
|
#
|
2007-03-18 19:50:34 +01:00
|
|
|
sub expand_rule( $$$$$$$$$$ )
|
2007-03-15 01:34:17 +01:00
|
|
|
{
|
2007-03-18 22:16:46 +01:00
|
|
|
my ($chainref , $restriction, $rule, $source, $dest, $origdest, $target, $loglevel , $disposition, $exceptionrule ) = @_;
|
2007-03-15 01:34:17 +01:00
|
|
|
my ($iiface, $diface, $inets, $dnets, $iexcl, $dexcl, $onets , $oexcl );
|
2007-03-18 22:16:46 +01:00
|
|
|
my $chain = $chainref->{name};
|
2007-03-23 17:12:36 +01:00
|
|
|
#
|
|
|
|
# Handle Log Level
|
|
|
|
#
|
|
|
|
my $logtag;
|
|
|
|
|
|
|
|
if ( $loglevel ) {
|
|
|
|
( $loglevel, $logtag ) = split /:/, $loglevel;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-23 17:12:36 +01:00
|
|
|
if ( $loglevel =~ /^none!?$/i ) {
|
2007-03-23 20:19:43 +01:00
|
|
|
return if $disposition eq 'LOG';
|
2007-03-23 17:12:36 +01:00
|
|
|
$loglevel = $logtag = '';
|
|
|
|
}
|
|
|
|
}
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Isolate Source Interface, if any
|
|
|
|
#
|
|
|
|
if ( $source ) {
|
|
|
|
if ( $source eq '-' ) {
|
|
|
|
$source = '';
|
|
|
|
} elsif ( $source =~ /^([^:]+):([^:]+)$/ ) {
|
|
|
|
$iiface = $1;
|
|
|
|
$inets = $2;
|
|
|
|
} elsif ( $source =~ /\+|~|\..*\./ ) {
|
|
|
|
$inets = $source;
|
|
|
|
} else {
|
|
|
|
$iiface = $source;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$source = '';
|
|
|
|
}
|
2007-03-23 17:12:36 +01:00
|
|
|
|
2007-03-18 22:16:46 +01:00
|
|
|
#
|
2007-03-15 01:34:17 +01:00
|
|
|
# Verify Inteface, if any
|
|
|
|
#
|
|
|
|
if ( $iiface ) {
|
|
|
|
fatal_error "Unknown Interface ($iiface): \"$line\"" unless known_interface $iiface;
|
2007-03-18 22:57:39 +01:00
|
|
|
|
2007-03-25 21:27:25 +02:00
|
|
|
if ( $restriction & POSTROUTE_RESTRICT ) {
|
2007-03-23 23:47:21 +01:00
|
|
|
#
|
|
|
|
# An interface in the SOURCE column of a masq file
|
|
|
|
#
|
2007-03-27 20:41:55 +02:00
|
|
|
my $networks = get_interface_nets ( $iiface );
|
|
|
|
|
|
|
|
add_command( $chainref , join( '', 'for source in ', $networks, '; do' ) );
|
|
|
|
|
2007-03-23 23:47:21 +01:00
|
|
|
$rule .= '-s $source ';
|
|
|
|
#
|
|
|
|
# While $loopcount > 0, calls to 'add_rule()' will be converted to calls to 'add_command()'
|
|
|
|
#
|
2007-03-23 17:12:36 +01:00
|
|
|
$loopcount++;
|
2007-03-18 22:16:46 +01:00
|
|
|
} else {
|
2007-03-25 21:27:25 +02:00
|
|
|
fatal_error "Source Interface ( $iiface ) not allowed when the source zone is $firewall_zone: $line"
|
|
|
|
if $restriction & OUTPUT_RESTRICT;
|
2007-03-18 22:16:46 +01:00
|
|
|
$rule .= "-i $iiface ";
|
|
|
|
}
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Isolate Destination Interface, if any
|
|
|
|
#
|
|
|
|
if ( $dest ) {
|
|
|
|
if ( $dest eq '-' ) {
|
|
|
|
$dest = '';
|
2007-03-25 21:27:25 +02:00
|
|
|
} elsif ( ( $restriction & PREROUTE_RESTRICT ) && $dest =~ /^detect:(.*)$/ ) {
|
2007-03-23 17:12:36 +01:00
|
|
|
#
|
|
|
|
# DETECT_DNAT_IPADDRS=Yes and we're generating the nat rule
|
|
|
|
#
|
|
|
|
my @interfaces = split /\s+/, $1;
|
|
|
|
|
|
|
|
if ( @interfaces > 1 ) {
|
2007-03-27 20:41:55 +02:00
|
|
|
my $list = "";
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-23 17:12:36 +01:00
|
|
|
for my $interface ( @interfaces ) {
|
2007-03-27 20:41:55 +02:00
|
|
|
$list = join( ' ', $list , get_interface_address( $interface ) );
|
2007-03-23 17:12:36 +01:00
|
|
|
}
|
2007-03-27 20:41:55 +02:00
|
|
|
|
|
|
|
add_command( $chainref , "for address in $list; do" );
|
|
|
|
|
2007-03-23 23:47:21 +01:00
|
|
|
$rule .= '-d $address ';
|
2007-03-23 17:12:36 +01:00
|
|
|
$loopcount++;
|
|
|
|
} else {
|
2007-03-27 20:41:55 +02:00
|
|
|
$rule .= join ( '', '-d ', get_interface_address( $interfaces[0] ), ' ' );
|
2007-03-23 17:12:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$dest = '';
|
2007-03-15 01:34:17 +01:00
|
|
|
} elsif ( $dest =~ /^([^:]+):([^:]+)$/ ) {
|
|
|
|
$diface = $1;
|
|
|
|
$dnets = $2;
|
|
|
|
} elsif ( $dest =~ /\+|~|\..*\./ ) {
|
|
|
|
$dnets = $dest;
|
|
|
|
} else {
|
|
|
|
$diface = $dest;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dest = '';
|
|
|
|
}
|
2007-03-23 23:47:21 +01:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Verify Destination Interface, if any
|
|
|
|
#
|
|
|
|
if ( $diface ) {
|
|
|
|
fatal_error "Unknown Interface ($diface) in rule \"$line\"" unless known_interface $diface;
|
2007-03-18 22:57:39 +01:00
|
|
|
|
2007-03-25 21:27:25 +02:00
|
|
|
if ( $restriction & PREROUTE_RESTRICT ) {
|
2007-03-23 23:47:21 +01:00
|
|
|
#
|
|
|
|
# ADDRESS 'detect' in the masq file.
|
|
|
|
#
|
2007-03-23 18:30:12 +01:00
|
|
|
add_command $chainref , "dests=\$(find_interface_addresses $diface)";
|
|
|
|
add_command $chainref , qq([ -z "\$dests" ] && fatal_error "Unable to determine the address(es) of interface \"$diface\"");
|
|
|
|
add_command $chainref , 'for dest in $dests; do';
|
2007-03-18 22:16:46 +01:00
|
|
|
$rule .= '-d $dest';
|
2007-03-23 17:12:36 +01:00
|
|
|
$loopcount++;
|
2007-03-18 22:16:46 +01:00
|
|
|
} else {
|
2007-03-25 21:27:25 +02:00
|
|
|
fatal_error "Destination Interface ( $diface ) not allowed when the destination zone is $firewall_zone: $line"
|
|
|
|
if $restriction & INPUT_RESTRICT;
|
2007-03-18 22:16:46 +01:00
|
|
|
$rule .= "-o $diface ";
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
}
|
2007-03-18 22:16:46 +01:00
|
|
|
|
2007-03-23 17:12:36 +01:00
|
|
|
if ( $origdest ) {
|
2007-03-29 01:21:37 +02:00
|
|
|
if ( $origdest eq '-' || ! $capabilities{CONNTRACK_MATCH} ) {
|
2007-03-27 01:17:46 +02:00
|
|
|
$origdest = '';
|
2007-03-23 17:12:36 +01:00
|
|
|
} elsif ( $origdest =~ /^detect:(.*)$/ ) {
|
2007-03-23 23:47:21 +01:00
|
|
|
#
|
|
|
|
# Either the filter part of a DNAT rule or 'detect' was given in the ORIG DEST column
|
|
|
|
#
|
2007-03-23 17:12:36 +01:00
|
|
|
my @interfaces = split /\s+/, $1;
|
2007-03-18 22:16:46 +01:00
|
|
|
|
2007-03-23 17:12:36 +01:00
|
|
|
if ( @interfaces > 1 ) {
|
2007-03-27 20:41:55 +02:00
|
|
|
my $list = "";
|
2007-03-18 22:16:46 +01:00
|
|
|
|
2007-03-23 20:19:43 +01:00
|
|
|
for my $interface ( @interfaces ) {
|
2007-03-27 20:41:55 +02:00
|
|
|
$list = join( ' ', $list , get_interface_address( $interface ) );
|
2007-03-23 17:12:36 +01:00
|
|
|
}
|
2007-03-23 23:47:21 +01:00
|
|
|
|
2007-03-27 20:41:55 +02:00
|
|
|
add_command( $chainref , "for address in $list; do" );
|
2007-03-23 23:47:21 +01:00
|
|
|
$rule .= '-m conntrack --ctorigdst $address ';
|
2007-03-23 17:12:36 +01:00
|
|
|
$loopcount++;
|
|
|
|
} else {
|
2007-03-26 21:01:38 +02:00
|
|
|
get_interface_address $interfaces[0];
|
2007-03-26 02:46:15 +02:00
|
|
|
$rule .= join( '', '-m conntrack --ctorigdst $', interface_address ( $interfaces[0] ), ' ' );
|
2007-03-23 23:47:21 +01:00
|
|
|
}
|
2007-03-15 01:34:17 +01:00
|
|
|
|
2007-03-23 17:12:36 +01:00
|
|
|
$origdest = '';
|
|
|
|
} else {
|
2007-03-23 23:47:21 +01:00
|
|
|
if ( $origdest =~ /^([^!]+)?!([^!]+)$/ ) {
|
|
|
|
#
|
|
|
|
# Exclusion
|
|
|
|
#
|
|
|
|
$onets = $1;
|
|
|
|
$oexcl = $2;
|
|
|
|
} else {
|
2007-03-23 17:12:36 +01:00
|
|
|
$oexcl = '';
|
2007-03-29 05:07:48 +02:00
|
|
|
$onets = $origdest;
|
2007-03-23 17:12:36 +01:00
|
|
|
}
|
2007-03-23 23:47:21 +01:00
|
|
|
|
2007-03-24 16:59:17 +01:00
|
|
|
unless ( $onets ) {
|
2007-03-24 04:56:16 +01:00
|
|
|
my @oexcl = mysplit $oexcl;
|
2007-03-23 23:47:21 +01:00
|
|
|
if ( @oexcl == 1 ) {
|
|
|
|
$rule .= "-m conntrack --ctorigdst ! $oexcl ";
|
|
|
|
$oexcl = '';
|
|
|
|
}
|
|
|
|
}
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
2007-03-23 17:12:36 +01:00
|
|
|
} else {
|
|
|
|
$oexcl = '';
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Determine if there is Source Exclusion
|
|
|
|
#
|
|
|
|
if ( $inets ) {
|
|
|
|
if ( $inets =~ /^([^!]+)?!([^!]+)$/ ) {
|
|
|
|
$inets = $1;
|
|
|
|
$iexcl = $2;
|
|
|
|
} else {
|
|
|
|
$iexcl = '';
|
|
|
|
}
|
|
|
|
|
2007-03-24 16:59:17 +01:00
|
|
|
unless ( $inets ) {
|
2007-03-24 04:56:16 +01:00
|
|
|
my @iexcl = mysplit $iexcl;
|
2007-03-15 01:34:17 +01:00
|
|
|
if ( @iexcl == 1 ) {
|
|
|
|
$rule .= match_source_net "!$iexcl ";
|
|
|
|
$iexcl = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$iexcl = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Determine if there is Destination Exclusion
|
2007-03-18 22:16:46 +01:00
|
|
|
#
|
2007-03-15 01:34:17 +01:00
|
|
|
if ( $dnets ) {
|
|
|
|
if ( $dnets =~ /^([^!]+)?!([^!]+)$/ ) {
|
|
|
|
$dnets = $1;
|
|
|
|
$dexcl = $2;
|
|
|
|
} else {
|
|
|
|
$dexcl = '';
|
|
|
|
}
|
|
|
|
|
2007-03-24 16:59:17 +01:00
|
|
|
unless ( $dnets ) {
|
2007-03-24 04:56:16 +01:00
|
|
|
my @dexcl = mysplit $dexcl;
|
2007-03-15 01:34:17 +01:00
|
|
|
if ( @dexcl == 1 ) {
|
|
|
|
$rule .= match_dest_net "!$dexcl ";
|
|
|
|
$dexcl = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dexcl = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$inets = ALLIPv4 unless $inets;
|
|
|
|
$dnets = ALLIPv4 unless $dnets;
|
|
|
|
$onets = ALLIPv4 unless $onets;
|
|
|
|
|
|
|
|
if ( $iexcl || $dexcl || $oexcl ) {
|
|
|
|
#
|
|
|
|
# We have non-trivial exclusion -- need to create an exclusion chain
|
|
|
|
#
|
|
|
|
my $echain = newexclusionchain;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Use the current rule and sent all possible matches to the exclusion chain
|
|
|
|
#
|
2007-03-24 04:56:16 +01:00
|
|
|
for my $onet ( mysplit $onets ) {
|
2007-03-15 01:34:17 +01:00
|
|
|
$onet = match_orig_dest $onet;
|
2007-03-24 04:56:16 +01:00
|
|
|
for my $inet ( mysplit $inets ) {
|
2007-03-15 01:34:17 +01:00
|
|
|
$inet = match_source_net $inet;
|
2007-03-24 04:56:16 +01:00
|
|
|
for my $dnet ( mysplit $dnets ) {
|
2007-03-26 02:46:15 +02:00
|
|
|
add_rule $chainref, join( '', $rule, $inet, match_dest_net( $dnet ), $onet, "-j $echain" );
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-23 17:12:36 +01:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# The final rule in the exclusion chain will not qualify the source or destination
|
|
|
|
#
|
|
|
|
$inets = ALLIPv4;
|
|
|
|
$dnets = ALLIPv4;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Create the Exclusion Chain
|
|
|
|
#
|
|
|
|
my $echainref = new_chain $chainref->{table}, $echain;
|
|
|
|
|
|
|
|
#
|
|
|
|
# Generate RETURNs for each exclusion
|
|
|
|
#
|
2007-03-24 04:56:16 +01:00
|
|
|
for my $net ( mysplit $iexcl ) {
|
2007-03-23 18:30:12 +01:00
|
|
|
add_rule $echainref, ( match_source_net $net ) . '-j RETURN';
|
|
|
|
}
|
2007-03-15 01:34:17 +01:00
|
|
|
|
2007-03-24 04:56:16 +01:00
|
|
|
for my $net ( mysplit $dexcl ) {
|
2007-03-23 18:30:12 +01:00
|
|
|
add_rule $echainref, ( match_dest_net $net ) . '-j RETURN';
|
|
|
|
}
|
2007-03-15 01:34:17 +01:00
|
|
|
|
2007-03-24 04:56:16 +01:00
|
|
|
for my $net ( mysplit $oexcl ) {
|
2007-03-23 18:30:12 +01:00
|
|
|
add_rule $echainref, ( match_orig_dest $net ) . '-j RETURN';
|
2007-03-23 17:12:36 +01:00
|
|
|
}
|
2007-03-23 18:30:12 +01:00
|
|
|
#
|
|
|
|
# Log rule
|
|
|
|
#
|
|
|
|
log_rule_limit $loglevel , $echainref , $chain, $disposition , '', $logtag , 'add' , '' if $loglevel;
|
|
|
|
#
|
|
|
|
# Generate Final Rule
|
|
|
|
#
|
|
|
|
add_rule $echainref, $exceptionrule . $target unless $disposition eq 'LOG';
|
2007-03-15 01:34:17 +01:00
|
|
|
} else {
|
|
|
|
#
|
|
|
|
# No exclusions
|
|
|
|
#
|
2007-03-24 04:56:16 +01:00
|
|
|
for my $onet ( mysplit $onets ) {
|
2007-03-15 01:34:17 +01:00
|
|
|
$onet = match_orig_dest $onet;
|
2007-03-24 04:56:16 +01:00
|
|
|
for my $inet ( mysplit $inets ) {
|
2007-03-15 01:34:17 +01:00
|
|
|
$inet = match_source_net $inet;
|
2007-03-24 04:56:16 +01:00
|
|
|
for my $dnet ( mysplit $dnets ) {
|
2007-03-26 02:46:15 +02:00
|
|
|
if ( $loglevel ) {
|
|
|
|
log_rule_limit
|
|
|
|
$loglevel ,
|
|
|
|
$chainref ,
|
|
|
|
$chain,
|
|
|
|
$disposition ,
|
|
|
|
'' ,
|
|
|
|
$logtag ,
|
|
|
|
'add' ,
|
|
|
|
join( '', $rule, $inet, match_dest_net( $dnet ), $onet );
|
|
|
|
}
|
|
|
|
|
|
|
|
unless ( $disposition eq 'LOG' ) {
|
|
|
|
add_rule
|
|
|
|
$chainref,
|
|
|
|
join( '', $rule, $inet, match_dest_net( $dnet ), $onet, $target );
|
|
|
|
}
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-23 17:12:36 +01:00
|
|
|
}
|
|
|
|
|
2007-03-23 20:54:08 +01:00
|
|
|
while ( $loopcount > 0 ) {
|
|
|
|
$loopcount--;
|
|
|
|
add_command $chainref, 'done';
|
|
|
|
}
|
2007-03-15 01:34:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# If the destination chain exists, then at the end of the source chain add a jump to the destination.
|
|
|
|
#
|
|
|
|
sub addnatjump( $$$ ) {
|
|
|
|
my ( $source , $dest, $predicates ) = @_;
|
|
|
|
|
|
|
|
my $destref = $nat_table->{$dest} || {};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
if ( $destref->{referenced} ) {
|
|
|
|
add_rule $nat_table->{$source} , $predicates . "-j $dest";
|
|
|
|
} else {
|
|
|
|
clearrule;
|
|
|
|
}
|
|
|
|
}
|
2007-03-28 21:09:27 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# If the destination chain exists, then at the position in the source chain given by $$countref, add a jump to the destination.
|
|
|
|
#
|
|
|
|
sub insertnatjump( $$$$ ) {
|
|
|
|
my ( $source, $dest, $countref, $predicates ) = @_;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
my $destref = $nat_table->{$dest} || {};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
if ( $destref->{referenced} ) {
|
|
|
|
insert_rule $nat_table->{$source} , ($$countref)++, $predicates . "-j $dest";
|
|
|
|
} else {
|
|
|
|
clearrule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-21 00:44:12 +01:00
|
|
|
my @builtins = qw(PREROUTING INPUT FORWARD OUTPUT POSTROUTING);
|
|
|
|
|
2007-03-22 19:24:33 +01:00
|
|
|
use constant { NULL_STATE => 0 ,
|
|
|
|
CAT_STATE => 1 ,
|
|
|
|
CMD_STATE => 2 };
|
|
|
|
|
|
|
|
my $state = NULL_STATE;
|
|
|
|
|
|
|
|
sub emitr( $ ) {
|
|
|
|
my $rule = $_[0];
|
|
|
|
|
2007-03-29 21:45:53 +02:00
|
|
|
if ( substr( $rule, 0, 1 ) eq '~' ) {
|
2007-03-22 19:24:33 +01:00
|
|
|
#
|
|
|
|
# A command
|
|
|
|
#
|
|
|
|
unless ( $state == CMD_STATE ) {
|
|
|
|
emit_unindented "__EOF__\n" if $state == CAT_STATE;
|
|
|
|
$state = CMD_STATE;
|
|
|
|
}
|
|
|
|
|
2007-03-29 19:02:13 +02:00
|
|
|
$rule = substr( $rule, 1 );
|
2007-03-22 19:24:33 +01:00
|
|
|
|
|
|
|
emit $rule;
|
|
|
|
} else {
|
|
|
|
unless ( $state == CAT_STATE ) {
|
2007-03-22 20:43:24 +01:00
|
|
|
emit '';
|
2007-03-22 19:24:33 +01:00
|
|
|
emit 'cat >&3 << __EOF__';
|
|
|
|
$state = CAT_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit_unindented $rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 02:18:29 +01:00
|
|
|
sub create_netfilter_load() {
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-27 06:02:58 +02:00
|
|
|
emitj( 'setup_netfilter()',
|
|
|
|
'{'
|
|
|
|
);
|
2007-03-27 20:41:55 +02:00
|
|
|
|
2007-03-22 19:24:33 +01:00
|
|
|
push_indent;
|
|
|
|
|
2007-03-27 20:41:55 +02:00
|
|
|
for ( values %interfaceaddr ) {
|
|
|
|
emit $_;
|
|
|
|
}
|
|
|
|
|
2007-03-26 21:01:38 +02:00
|
|
|
for ( values %interfaceaddrs ) {
|
|
|
|
emit $_;
|
|
|
|
}
|
|
|
|
|
2007-03-27 20:41:55 +02:00
|
|
|
for ( values %interfacenets ) {
|
|
|
|
emit $_;
|
|
|
|
}
|
|
|
|
|
2007-03-26 21:01:38 +02:00
|
|
|
emit '';
|
|
|
|
|
2007-03-29 21:45:53 +02:00
|
|
|
emit 'exec 3>${VARDIR}/.iptables-restore-input';
|
2007-03-22 19:24:33 +01:00
|
|
|
|
2007-03-15 02:18:29 +01:00
|
|
|
for my $table qw/raw nat mangle filter/ {
|
2007-03-22 19:24:33 +01:00
|
|
|
emitr "*$table";
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 02:18:29 +01:00
|
|
|
my @chains;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-21 00:44:12 +01:00
|
|
|
for my $chain ( @builtins ) {
|
|
|
|
my $chainref = $chain_table{$table}{$chain};
|
|
|
|
if ( $chainref ) {
|
2007-03-22 19:24:33 +01:00
|
|
|
emitr ":$chain $chainref->{policy} [0:0]";
|
2007-03-21 00:44:12 +01:00
|
|
|
push @chains, $chainref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 02:18:29 +01:00
|
|
|
for my $chain ( grep $chain_table{$table}{$_}->{referenced} , ( sort keys %{$chain_table{$table}} ) ) {
|
|
|
|
my $chainref = $chain_table{$table}{$chain};
|
2007-03-21 00:44:12 +01:00
|
|
|
unless ( $chainref->{builtin} ) {
|
2007-03-22 19:24:33 +01:00
|
|
|
emitr ":$chainref->{name} - [0:0]";
|
2007-03-21 00:44:12 +01:00
|
|
|
push @chains, $chainref;
|
2007-03-15 02:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $chainref ( @chains ) {
|
|
|
|
my $name = $chainref->{name};
|
|
|
|
for my $rule ( @{$chainref->{rules}} ) {
|
2007-03-19 00:06:46 +01:00
|
|
|
$rule = "-A $name $rule" unless substr( $rule, 0, 1) eq '~';
|
2007-03-22 19:24:33 +01:00
|
|
|
emitr $rule;
|
2007-03-15 02:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 19:24:33 +01:00
|
|
|
emitr 'COMMIT';
|
2007-03-15 02:18:29 +01:00
|
|
|
}
|
|
|
|
|
2007-03-22 19:24:33 +01:00
|
|
|
emit_unindented '__EOF__' unless $state == CMD_STATE;
|
2007-03-21 20:46:44 +01:00
|
|
|
emit '';
|
2007-03-25 19:06:23 +02:00
|
|
|
|
2007-03-29 21:45:53 +02:00
|
|
|
emitj( ' exec 3>&-',
|
|
|
|
'',
|
|
|
|
'iptables-restore < ${VARDIR}/.iptables-restore-input'
|
|
|
|
);
|
2007-03-25 19:06:23 +02:00
|
|
|
|
2007-03-27 06:02:58 +02:00
|
|
|
emitj( 'if [ $? != 0 ]; then',
|
2007-03-29 21:45:53 +02:00
|
|
|
' fatal_error "iptables-restore Failed. Input is in ${VARDIR}/.iptables-restore-input"',
|
2007-03-27 06:02:58 +02:00
|
|
|
"fi\n"
|
|
|
|
);
|
2007-03-21 18:54:17 +01:00
|
|
|
|
2007-03-22 19:24:33 +01:00
|
|
|
pop_indent;
|
|
|
|
|
2007-03-15 02:18:29 +01:00
|
|
|
emit "}\n";
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 04:12:22 +01:00
|
|
|
1;
|