2007-03-15 22:55:22 +01:00
|
|
|
#
|
2008-05-04 02:18:47 +02:00
|
|
|
# Shorewall-perl 4.2 -- /usr/share/shorewall-perl/Shorewall/Nat.pm
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-09-08 18:09:51 +02:00
|
|
|
# This program is under GPL [http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt]
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2008-07-27 23:00:08 +02:00
|
|
|
# (c) 2007,2008 - Tom Eastep (teastep@shorewall.net)
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
|
|
|
# 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
|
2007-09-08 18:09:51 +02:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-04-19 01:55:25 +02:00
|
|
|
# This module contains code for dealing with the /etc/shorewall/masq,
|
|
|
|
# /etc/shorewall/nat and /etc/shorewall/netmap files.
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-03-15 02:04:43 +01:00
|
|
|
package Shorewall::Nat;
|
|
|
|
require Exporter;
|
2007-11-16 00:24:54 +01:00
|
|
|
use Shorewall::Config qw(:DEFAULT :internal);
|
2007-05-11 17:39:11 +02:00
|
|
|
use Shorewall::IPAddrs;
|
2007-03-15 02:04:43 +01:00
|
|
|
use Shorewall::Zones;
|
2007-11-16 00:24:54 +01:00
|
|
|
use Shorewall::Chains qw(:DEFAULT :internal);
|
2007-03-22 01:14:56 +01:00
|
|
|
use Shorewall::IPAddrs;
|
2007-11-16 02:21:33 +01:00
|
|
|
use Shorewall::Providers qw( lookup_provider );
|
2007-03-15 02:04:43 +01:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
2007-03-25 23:04:24 +02:00
|
|
|
our @EXPORT = qw( setup_masq setup_nat setup_netmap add_addresses );
|
2007-03-15 02:04:43 +01:00
|
|
|
our @EXPORT_OK = ();
|
2008-02-06 00:50:00 +01:00
|
|
|
our $VERSION = 4.1.5;
|
2007-03-15 02:04:43 +01:00
|
|
|
|
2007-06-05 18:49:13 +02:00
|
|
|
our @addresses_to_add;
|
|
|
|
our %addresses_to_add;
|
2007-03-22 00:14:33 +01:00
|
|
|
|
2007-06-15 00:07:45 +02:00
|
|
|
#
|
|
|
|
# Initialize globals -- we take this novel approach to globals initialization to allow
|
|
|
|
# the compiler to run multiple times in the same process. The
|
|
|
|
# initialize() function does globals initialization for this
|
|
|
|
# module and is called from an INIT block below. The function is
|
|
|
|
# also called by Shorewall::Compiler::compiler at the beginning of
|
2007-07-26 20:36:18 +02:00
|
|
|
# the second and subsequent calls to that function.
|
2007-06-15 00:07:45 +02:00
|
|
|
#
|
|
|
|
|
2007-06-14 01:02:39 +02:00
|
|
|
sub initialize() {
|
|
|
|
@addresses_to_add = ();
|
|
|
|
%addresses_to_add = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT {
|
|
|
|
initialize;
|
|
|
|
}
|
|
|
|
|
2007-03-15 02:04:43 +01:00
|
|
|
#
|
|
|
|
# Handle IPSEC Options in a masq record
|
|
|
|
#
|
2007-04-08 16:42:26 +02:00
|
|
|
sub do_ipsec_options($)
|
2007-03-15 02:04:43 +01:00
|
|
|
{
|
|
|
|
my %validoptions = ( strict => NOTHING,
|
2007-04-18 22:53:25 +02:00
|
|
|
next => NOTHING,
|
|
|
|
reqid => NUMERIC,
|
|
|
|
spi => NUMERIC,
|
|
|
|
proto => IPSECPROTO,
|
|
|
|
mode => IPSECMODE,
|
|
|
|
"tunnel-src" => NETWORK,
|
|
|
|
"tunnel-dst" => NETWORK,
|
2007-03-15 02:04:43 +01:00
|
|
|
);
|
|
|
|
my $list=$_[0];
|
2007-04-27 17:06:17 +02:00
|
|
|
my $options = '-m policy --pol ipsec --dir out ';
|
2007-03-15 02:04:43 +01:00
|
|
|
my $fmt;
|
|
|
|
|
2008-01-26 02:07:57 +01:00
|
|
|
for my $e ( split_list $list, 'option' ) {
|
2007-04-18 22:53:25 +02:00
|
|
|
my $val = undef;
|
2007-03-15 02:04:43 +01:00
|
|
|
my $invert = '';
|
|
|
|
|
2007-04-18 22:53:25 +02:00
|
|
|
if ( $e =~ /([\w-]+)!=(.+)/ ) {
|
|
|
|
$val = $2;
|
|
|
|
$e = $1;
|
2007-03-15 02:04:43 +01:00
|
|
|
$invert = '! ';
|
2007-04-18 22:53:25 +02:00
|
|
|
} elsif ( $e =~ /([\w-]+)=(.+)/ ) {
|
|
|
|
$val = $2;
|
|
|
|
$e = $1;
|
|
|
|
}
|
2007-03-15 02:04:43 +01:00
|
|
|
|
|
|
|
$fmt = $validoptions{$e};
|
|
|
|
|
|
|
|
fatal_error "Invalid Option ($e)" unless $fmt;
|
|
|
|
|
|
|
|
if ( $fmt eq NOTHING ) {
|
2007-04-22 16:29:30 +02:00
|
|
|
fatal_error "Option \"$e\" does not take a value" if defined $val;
|
2007-03-15 02:04:43 +01:00
|
|
|
} else {
|
2007-04-22 16:29:30 +02:00
|
|
|
fatal_error "Missing value for option \"$e\"" unless defined $val;
|
2007-03-15 02:04:43 +01:00
|
|
|
fatal_error "Invalid value ($val) for option \"$e\"" unless $val =~ /^($fmt)$/;
|
|
|
|
}
|
|
|
|
|
|
|
|
$options .= $invert;
|
2007-04-27 17:06:17 +02:00
|
|
|
$options .= "--$e ";
|
|
|
|
$options .= "$val " if defined $val;
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
|
|
|
|
2007-04-27 17:06:17 +02:00
|
|
|
$options;
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Process a single rule from the the masq file
|
|
|
|
#
|
2007-05-01 19:50:50 +02:00
|
|
|
sub setup_one_masq($$$$$$$)
|
2007-03-15 02:04:43 +01:00
|
|
|
{
|
2008-01-15 03:39:21 +01:00
|
|
|
my ($interfacelist, $networks, $addresses, $proto, $ports, $ipsec, $mark) = @_;
|
2007-03-15 02:04:43 +01:00
|
|
|
|
|
|
|
my $pre_nat;
|
|
|
|
my $add_snat_aliases = $config{ADD_SNAT_ALIASES};
|
|
|
|
my $destnets = '';
|
2008-01-15 20:02:00 +01:00
|
|
|
my $baserule = '';
|
2007-03-15 02:04:43 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Leading '+'
|
|
|
|
#
|
2008-01-15 03:39:21 +01:00
|
|
|
$pre_nat = 1 if $interfacelist =~ s/^\+//;
|
2007-03-15 02:04:43 +01:00
|
|
|
#
|
|
|
|
# Parse the remaining part of the INTERFACE column
|
|
|
|
#
|
2008-01-15 03:39:21 +01:00
|
|
|
if ( $interfacelist =~ /^([^:]+)::([^:]*)$/ ) {
|
2007-03-22 00:14:33 +01:00
|
|
|
$add_snat_aliases = 0;
|
2007-03-15 02:04:43 +01:00
|
|
|
$destnets = $2;
|
2008-01-15 03:39:21 +01:00
|
|
|
$interfacelist = $1;
|
|
|
|
} elsif ( $interfacelist =~ /^([^:]+:[^:]+):([^:]+)$/ ) {
|
2007-03-15 02:04:43 +01:00
|
|
|
$destnets = $2;
|
2008-01-15 03:39:21 +01:00
|
|
|
$interfacelist = $1;
|
|
|
|
} elsif ( $interfacelist =~ /^([^:]+):$/ ) {
|
2007-03-22 00:14:33 +01:00
|
|
|
$add_snat_aliases = 0;
|
2008-01-15 03:39:21 +01:00
|
|
|
$interfacelist = $1;
|
|
|
|
} elsif ( $interfacelist =~ /^([^:]+):([^:]*)$/ ) {
|
2007-03-15 02:04:43 +01:00
|
|
|
my ( $one, $two ) = ( $1, $2 );
|
|
|
|
if ( $2 =~ /\./ ) {
|
2008-01-15 03:39:21 +01:00
|
|
|
$interfacelist = $one;
|
2007-03-15 02:04:43 +01:00
|
|
|
$destnets = $two;
|
2007-03-27 01:17:46 +02:00
|
|
|
}
|
2007-04-08 16:42:26 +02:00
|
|
|
}
|
2007-03-15 02:04:43 +01:00
|
|
|
#
|
|
|
|
# If there is no source or destination then allow all addresses
|
|
|
|
#
|
2007-03-26 01:18:20 +02:00
|
|
|
$networks = ALLIPv4 if $networks eq '-';
|
|
|
|
$destnets = ALLIPv4 if $destnets eq '-';
|
2008-01-15 20:02:00 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Handle IPSEC options, if any
|
|
|
|
#
|
|
|
|
if ( $ipsec ne '-' ) {
|
|
|
|
fatal_error "Non-empty IPSEC column requires policy match support in your kernel and iptables" unless $globals{ORIGINAL_POLICY_MATCH};
|
|
|
|
|
|
|
|
if ( $ipsec =~ /^yes$/i ) {
|
|
|
|
$baserule .= '-m policy --pol ipsec --dir out ';
|
|
|
|
} elsif ( $ipsec =~ /^no$/i ) {
|
|
|
|
$baserule .= '-m policy --pol none --dir out ';
|
|
|
|
} else {
|
|
|
|
$baserule .= do_ipsec_options $ipsec;
|
|
|
|
}
|
|
|
|
} elsif ( $capabilities{POLICY_MATCH} ) {
|
|
|
|
$baserule .= '-m policy --pol none --dir out ';
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2008-01-15 20:02:00 +01:00
|
|
|
#
|
|
|
|
# Handle Protocol and Ports
|
|
|
|
#
|
|
|
|
$baserule .= do_proto $proto, $ports, '';
|
|
|
|
|
|
|
|
#
|
|
|
|
# Handle Mark
|
|
|
|
#
|
|
|
|
$baserule .= do_test( $mark, 0xFF) if $mark ne '-';
|
|
|
|
|
2008-01-26 02:07:57 +01:00
|
|
|
for my $fullinterface (split_list $interfacelist, 'interface' ) {
|
2008-01-15 03:39:21 +01:00
|
|
|
my $rule = '';
|
|
|
|
my $target = '-j MASQUERADE ';
|
|
|
|
#
|
|
|
|
# Isolate and verify the interface part
|
|
|
|
#
|
|
|
|
( my $interface = $fullinterface ) =~ s/:.*//;
|
|
|
|
|
|
|
|
if ( $interface =~ /(.*)[(](\w*)[)]$/ ) {
|
|
|
|
$interface = $1;
|
2008-02-11 19:40:58 +01:00
|
|
|
my $provider = $2;
|
2008-01-15 03:39:21 +01:00
|
|
|
$fullinterface =~ s/[(]\w*[)]//;
|
2008-02-11 19:40:58 +01:00
|
|
|
my $realm = lookup_provider( $provider ) unless $provider =~ /^\d+$/;
|
|
|
|
|
|
|
|
fatal_error "$provider is not a shared-interface provider" unless $realm;
|
2008-01-15 03:39:21 +01:00
|
|
|
|
|
|
|
$rule .= "-m realm --realm $realm ";
|
|
|
|
}
|
|
|
|
|
2008-01-20 00:36:27 +01:00
|
|
|
fatal_error "Unknown interface ($interface)" unless my $interfaceref = known_interface( $interface );
|
|
|
|
|
|
|
|
unless ( $interfaceref->{root} ) {
|
|
|
|
$rule .= "-o $interface ";
|
|
|
|
$interface = $interfaceref->{name};
|
|
|
|
}
|
2008-01-15 03:39:21 +01:00
|
|
|
|
|
|
|
my $chainref = ensure_chain('nat', $pre_nat ? snat_chain $interface : masq_chain $interface);
|
2008-01-15 20:02:00 +01:00
|
|
|
|
2008-01-15 03:39:21 +01:00
|
|
|
my $detectaddress = 0;
|
|
|
|
my $exceptionrule = '';
|
|
|
|
my $randomize = '';
|
|
|
|
#
|
|
|
|
# Parse the ADDRESSES column
|
|
|
|
#
|
|
|
|
if ( $addresses ne '-' ) {
|
|
|
|
if ( $addresses eq 'random' ) {
|
|
|
|
$randomize = '--random ';
|
2007-11-16 00:24:54 +01:00
|
|
|
} else {
|
2008-01-15 03:39:21 +01:00
|
|
|
$addresses =~ s/:random$// and $randomize = '--random ';
|
|
|
|
|
|
|
|
if ( $addresses =~ /^SAME:nodst:/ ) {
|
|
|
|
fatal_error "':random' is not supported by the SAME target" if $randomize;
|
|
|
|
$target = '-j SAME --nodst ';
|
|
|
|
$addresses =~ s/.*://;
|
2008-01-26 02:07:57 +01:00
|
|
|
for my $addr ( split_list $addresses, 'address' ) {
|
2008-01-15 03:39:21 +01:00
|
|
|
$target .= "--to $addr ";
|
|
|
|
}
|
|
|
|
} elsif ( $addresses =~ /^SAME:/ ) {
|
|
|
|
fatal_error "':random' is not supported by the SAME target" if $randomize;
|
|
|
|
$target = '-j SAME ';
|
|
|
|
$addresses =~ s/.*://;
|
2008-01-26 02:07:57 +01:00
|
|
|
for my $addr ( split_list $addresses, 'address' ) {
|
2008-01-15 03:39:21 +01:00
|
|
|
$target .= "--to $addr ";
|
|
|
|
}
|
|
|
|
} elsif ( $addresses eq 'detect' ) {
|
|
|
|
my $variable = get_interface_address $interface;
|
|
|
|
$target = "-j SNAT --to-source $variable";
|
|
|
|
|
|
|
|
if ( interface_is_optional $interface ) {
|
|
|
|
add_commands( $chainref,
|
|
|
|
'',
|
|
|
|
"if [ \"$variable\" != 0.0.0.0 ]; then" );
|
|
|
|
incr_cmd_level( $chainref );
|
|
|
|
$detectaddress = 1;
|
|
|
|
}
|
2008-08-22 16:18:31 +02:00
|
|
|
} elsif ( $addresses eq 'NONAT' ) {
|
2008-08-20 01:28:47 +02:00
|
|
|
$target = '-j RETURN';
|
|
|
|
$add_snat_aliases = 0;
|
2008-01-15 03:39:21 +01:00
|
|
|
} else {
|
|
|
|
my $addrlist = '';
|
2008-01-26 02:07:57 +01:00
|
|
|
for my $addr ( split_list $addresses , 'address' ) {
|
2008-01-15 03:39:21 +01:00
|
|
|
if ( $addr =~ /^.*\..*\..*\./ ) {
|
|
|
|
$target = '-j SNAT ';
|
|
|
|
$addrlist .= "--to-source $addr ";
|
|
|
|
$exceptionrule = do_proto( $proto, '', '' ) if $addr =~ /:/;
|
|
|
|
} else {
|
|
|
|
$addr =~ s/^://;
|
|
|
|
$addrlist .= "--to-ports $addr ";
|
|
|
|
$exceptionrule = do_proto( $proto, '', '' );
|
|
|
|
}
|
2007-11-16 00:24:54 +01:00
|
|
|
}
|
2008-01-15 20:02:00 +01:00
|
|
|
|
2008-01-15 03:39:21 +01:00
|
|
|
$target .= $addrlist;
|
2007-11-16 00:24:54 +01:00
|
|
|
}
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
2008-01-15 20:02:00 +01:00
|
|
|
|
2008-01-15 03:39:21 +01:00
|
|
|
$target .= $randomize;
|
|
|
|
} else {
|
|
|
|
$add_snat_aliases = 0;
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
2008-01-15 03:39:21 +01:00
|
|
|
#
|
|
|
|
# And Generate the Rule(s)
|
|
|
|
#
|
|
|
|
expand_rule( $chainref ,
|
|
|
|
POSTROUTE_RESTRICT ,
|
2008-01-15 20:02:00 +01:00
|
|
|
$baserule . $rule ,
|
2008-01-15 03:39:21 +01:00
|
|
|
$networks ,
|
|
|
|
$destnets ,
|
|
|
|
'' ,
|
|
|
|
$target ,
|
|
|
|
'' ,
|
|
|
|
'' ,
|
|
|
|
$exceptionrule );
|
|
|
|
|
|
|
|
if ( $detectaddress ) {
|
|
|
|
decr_cmd_level( $chainref );
|
|
|
|
add_command( $chainref , 'fi' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $add_snat_aliases ) {
|
|
|
|
my ( $interface, $alias , $remainder ) = split( /:/, $fullinterface, 3 );
|
|
|
|
fatal_error "Invalid alias ($alias:$remainder)" if defined $remainder;
|
2008-01-26 02:07:57 +01:00
|
|
|
for my $address ( split_list $addresses, 'address' ) {
|
2008-01-15 03:39:21 +01:00
|
|
|
my ( $addrs, $port ) = split /:/, $address;
|
|
|
|
next unless $addrs;
|
|
|
|
next if $addrs eq 'detect';
|
|
|
|
for my $addr ( ip_range_explicit $addrs ) {
|
|
|
|
unless ( $addresses_to_add{$addr} ) {
|
|
|
|
emit "del_ip_addr $addr $interface" unless $config{RETAIN_ALIASES};
|
|
|
|
$addresses_to_add{$addr} = 1;
|
|
|
|
if ( defined $alias ) {
|
|
|
|
push @addresses_to_add, $addr, "$interface:$alias";
|
|
|
|
$alias++;
|
|
|
|
} else {
|
|
|
|
push @addresses_to_add, $addr, $interface;
|
|
|
|
}
|
2007-03-23 22:24:28 +01:00
|
|
|
}
|
2007-03-22 02:51:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-15 03:39:21 +01:00
|
|
|
|
2007-07-07 18:34:38 +02:00
|
|
|
progress_message " Masq record \"$currentline\" $done";
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Process the masq file
|
|
|
|
#
|
2007-04-08 16:42:26 +02:00
|
|
|
sub setup_masq()
|
2007-03-15 02:04:43 +01:00
|
|
|
{
|
2007-03-30 17:57:08 +02:00
|
|
|
my $fn = open_file 'masq';
|
2007-03-15 02:04:43 +01:00
|
|
|
|
2007-11-27 16:50:20 +01:00
|
|
|
first_entry( sub { progress_message2 "$doing $fn..."; require_capability 'NAT_ENABLED' , 'a non-empty masq file' , 's'; } );
|
2007-11-16 00:24:54 +01:00
|
|
|
|
2007-03-29 19:02:13 +02:00
|
|
|
while ( read_a_line ) {
|
2007-03-15 02:04:43 +01:00
|
|
|
|
2007-05-10 17:29:41 +02:00
|
|
|
my ($fullinterface, $networks, $addresses, $proto, $ports, $ipsec, $mark ) = split_line1 2, 7, 'masq file';
|
2007-05-09 21:03:09 +02:00
|
|
|
|
2007-03-15 02:04:43 +01:00
|
|
|
if ( $fullinterface eq 'COMMENT' ) {
|
2007-05-09 20:22:40 +02:00
|
|
|
process_comment;
|
2007-03-15 02:04:43 +01:00
|
|
|
} else {
|
2007-05-01 19:50:50 +02:00
|
|
|
setup_one_masq $fullinterface, $networks, $addresses, $proto, $ports, $ipsec, $mark;
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-10 17:52:57 +02:00
|
|
|
clear_comment;
|
2007-03-15 02:04:43 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Validate the ALL INTERFACES or LOCAL column in the NAT file
|
|
|
|
#
|
|
|
|
sub validate_nat_column( $$ ) {
|
|
|
|
my $ref = $_[1];
|
|
|
|
my $val = $$ref;
|
|
|
|
|
|
|
|
if ( defined $val ) {
|
|
|
|
unless ( ( $val = "\L$val" ) eq 'yes' ) {
|
|
|
|
if ( ( $val eq 'no' ) || ( $val eq '-' ) ) {
|
|
|
|
$$ref = '';
|
|
|
|
} else {
|
2007-03-30 04:05:11 +02:00
|
|
|
fatal_error "Invalid value ($val) for $_[0]";
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$$ref = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Process a record from the NAT file
|
|
|
|
#
|
|
|
|
sub do_one_nat( $$$$$ )
|
|
|
|
{
|
2007-03-27 20:41:55 +02:00
|
|
|
my ( $external, $fullinterface, $internal, $allints, $localnat ) = @_;
|
|
|
|
|
2007-05-17 16:17:29 +02:00
|
|
|
my ( $interface, $alias, $remainder ) = split( /:/, $fullinterface, 3 );
|
|
|
|
|
|
|
|
fatal_error "Invalid alias ($alias:$remainder)" if defined $remainder;
|
2007-03-15 02:04:43 +01:00
|
|
|
|
|
|
|
sub add_nat_rule( $$ ) {
|
2007-09-12 17:03:47 +02:00
|
|
|
add_rule ensure_chain( 'nat', $_[0] ) , $_[1];
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 02:04:43 +01:00
|
|
|
my $add_ip_aliases = $config{ADD_IP_ALIASES};
|
|
|
|
|
|
|
|
my $policyin = '';
|
|
|
|
my $policyout = '';
|
2008-01-20 00:36:27 +01:00
|
|
|
my $rulein = '';
|
|
|
|
my $ruleout = '';
|
|
|
|
|
|
|
|
fatal_error "Unknown interface ($interface)" unless my $interfaceref = known_interface( $interface );
|
|
|
|
|
|
|
|
unless ( $interfaceref->{root} ) {
|
|
|
|
$rulein = "-i $interface ";
|
|
|
|
$ruleout = "-o $interface ";
|
|
|
|
$interface = $interfaceref->{name};
|
|
|
|
}
|
2007-03-15 02:04:43 +01:00
|
|
|
|
|
|
|
if ( $capabilities{POLICY_MATCH} ) {
|
|
|
|
$policyin = ' -m policy --pol none --dir in';
|
|
|
|
$policyout = '-m policy --pol none --dir out';
|
|
|
|
}
|
|
|
|
|
2007-03-30 04:05:11 +02:00
|
|
|
fatal_error "Invalid nat file entry" unless defined $interface && defined $internal;
|
2007-03-15 02:04:43 +01:00
|
|
|
|
|
|
|
if ( $add_ip_aliases ) {
|
2007-12-08 18:37:04 +01:00
|
|
|
if ( defined( $alias ) && $alias eq '' ) {
|
2007-03-15 02:04:43 +01:00
|
|
|
$add_ip_aliases = '';
|
|
|
|
} else {
|
2007-12-08 18:37:04 +01:00
|
|
|
emit "del_ip_addr $external $interface" unless $config{RETAIN_ALIASES};
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
validate_nat_column 'ALL INTERFACES', \$allints;
|
|
|
|
validate_nat_column 'LOCAL' , \$localnat;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 02:04:43 +01:00
|
|
|
if ( $allints ) {
|
|
|
|
add_nat_rule 'nat_in' , "-d $external $policyin -j DNAT --to-destination $internal";
|
|
|
|
add_nat_rule 'nat_out' , "-s $internal $policyout -j SNAT --to-source $external";
|
|
|
|
} else {
|
2008-01-20 00:36:27 +01:00
|
|
|
add_nat_rule input_chain( $interface ) , $rulein . "-d $external $policyin -j DNAT --to-destination $internal";
|
|
|
|
add_nat_rule output_chain( $interface ) , $ruleout . "-s $internal $policyout -j SNAT --to-source $external";
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-05-14 21:11:26 +02:00
|
|
|
add_nat_rule 'OUTPUT' , "-d $external $policyout -j DNAT --to-destination $internal " if $localnat;
|
2007-03-15 02:04:43 +01:00
|
|
|
|
2007-03-22 02:51:11 +01:00
|
|
|
if ( $add_ip_aliases ) {
|
|
|
|
unless ( $addresses_to_add{$external} ) {
|
|
|
|
$addresses_to_add{$external} = 1;
|
2007-03-27 20:41:55 +02:00
|
|
|
push @addresses_to_add, ( $external , $fullinterface );
|
2007-03-22 02:51:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Process NAT file
|
|
|
|
#
|
|
|
|
sub setup_nat() {
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
my $fn = open_file 'nat';
|
2007-03-15 02:04:43 +01:00
|
|
|
|
2007-11-27 16:50:20 +01:00
|
|
|
first_entry( sub { progress_message2 "$doing $fn..."; require_capability 'NAT_ENABLED' , 'a non-empty nat file' , 's'; } );
|
2007-11-16 00:24:54 +01:00
|
|
|
|
2007-03-29 19:02:13 +02:00
|
|
|
while ( read_a_line ) {
|
2007-03-15 02:04:43 +01:00
|
|
|
|
2008-01-15 03:39:21 +01:00
|
|
|
my ( $external, $interfacelist, $internal, $allints, $localnat ) = split_line1 3, 5, 'nat file';
|
2007-05-09 21:03:09 +02:00
|
|
|
|
2007-03-15 02:04:43 +01:00
|
|
|
if ( $external eq 'COMMENT' ) {
|
2007-05-09 20:22:40 +02:00
|
|
|
process_comment;
|
2007-03-15 02:04:43 +01:00
|
|
|
} else {
|
2008-01-15 03:39:21 +01:00
|
|
|
( $interfacelist, my $digit ) = split /:/, $interfacelist;
|
|
|
|
|
|
|
|
$digit = defined $digit ? ":$digit" : '';
|
|
|
|
|
2008-01-26 02:07:57 +01:00
|
|
|
for my $interface ( split_list $interfacelist , 'interface' ) {
|
2008-01-26 00:52:33 +01:00
|
|
|
fatal_error "Invalid Interface List ($interfacelist)" unless defined $interface && $interface ne '';
|
2008-01-15 03:39:21 +01:00
|
|
|
do_one_nat $external, "${interface}${digit}", $internal, $allints, $localnat;
|
|
|
|
}
|
|
|
|
|
|
|
|
progress_message " NAT entry \"$currentline\" $done";
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
|
|
|
|
2007-09-10 17:52:57 +02:00
|
|
|
clear_comment;
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
2007-03-22 00:26:23 +01:00
|
|
|
|
2007-03-25 23:04:24 +02:00
|
|
|
#
|
|
|
|
# Setup Network Mapping
|
|
|
|
#
|
|
|
|
sub setup_netmap() {
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
my $fn = open_file 'netmap';
|
2007-03-25 23:04:24 +02:00
|
|
|
|
2007-11-27 16:50:20 +01:00
|
|
|
first_entry( sub { progress_message2 "$doing $fn..."; require_capability 'NAT_ENABLED' , 'a non-empty netmap file' , 's'; } );
|
2007-03-25 23:04:24 +02:00
|
|
|
|
2007-11-16 00:24:54 +01:00
|
|
|
while ( read_a_line ) {
|
2007-03-29 19:02:13 +02:00
|
|
|
|
2008-01-20 00:36:27 +01:00
|
|
|
my ( $type, $net1, $interfacelist, $net2 ) = split_line 4, 4, 'netmap file';
|
2007-04-29 20:33:30 +02:00
|
|
|
|
2008-01-26 02:07:57 +01:00
|
|
|
for my $interface ( split_list $interfacelist, 'interface' ) {
|
2008-01-20 00:36:27 +01:00
|
|
|
|
|
|
|
my $rulein = '';
|
|
|
|
my $ruleout = '';
|
|
|
|
my $iface = $interface;
|
|
|
|
|
|
|
|
fatal_error "Unknown interface ($interface)" unless my $interfaceref = find_interface( $interface );
|
|
|
|
|
|
|
|
unless ( $interfaceref->{root} ) {
|
|
|
|
$rulein = "-i $interface ";
|
|
|
|
$ruleout = "-o $interface ";
|
|
|
|
$interface = $interfaceref->{name};
|
|
|
|
}
|
2007-03-25 23:04:24 +02:00
|
|
|
|
2008-01-20 00:36:27 +01:00
|
|
|
if ( $type eq 'DNAT' ) {
|
|
|
|
add_rule ensure_chain( 'nat' , input_chain $interface ) , $rulein . "-d $net1 -j NETMAP --to $net2";
|
|
|
|
} elsif ( $type eq 'SNAT' ) {
|
|
|
|
add_rule ensure_chain( 'nat' , output_chain $interface ) , $ruleout . "-s $net1 -j NETMAP --to $net2";
|
|
|
|
} else {
|
|
|
|
fatal_error "Invalid type ($type)";
|
|
|
|
}
|
2007-03-25 23:04:24 +02:00
|
|
|
|
2008-01-20 00:36:27 +01:00
|
|
|
progress_message " Network $net1 on $iface mapped to $net2 ($type)";
|
|
|
|
}
|
2007-03-25 23:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-03-22 03:13:30 +01:00
|
|
|
sub add_addresses () {
|
|
|
|
if ( @addresses_to_add ) {
|
|
|
|
my $arg = '';
|
|
|
|
|
|
|
|
while ( @addresses_to_add ) {
|
|
|
|
my $addr = shift @addresses_to_add;
|
|
|
|
my $interface = shift @addresses_to_add;
|
|
|
|
$arg = "$arg $addr $interface";
|
|
|
|
}
|
|
|
|
|
|
|
|
emit "add_ip_aliases $arg";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 00:26:23 +01:00
|
|
|
1;
|