2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-06-11 21:39:30 +02:00
|
|
|
# Shorewall-perl 4.0 -- /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
|
|
|
#
|
|
|
|
# (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
|
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 = ();
|
2007-11-16 00:24:54 +01:00
|
|
|
our $VERSION = 4.0.6;
|
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;
|
|
|
|
|
|
|
|
for my $e ( split ',' , $list ) {
|
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
|
|
|
{
|
2007-05-01 19:50:50 +02:00
|
|
|
my ($fullinterface, $networks, $addresses, $proto, $ports, $ipsec, $mark) = @_;
|
2007-03-15 02:04:43 +01:00
|
|
|
|
|
|
|
my $rule = '';
|
|
|
|
my $pre_nat;
|
|
|
|
my $add_snat_aliases = $config{ADD_SNAT_ALIASES};
|
|
|
|
my $destnets = '';
|
|
|
|
my $target = '-j MASQUERADE ';
|
|
|
|
|
|
|
|
#
|
|
|
|
# Handle IPSEC options, if any
|
|
|
|
#
|
2007-03-26 01:18:20 +02:00
|
|
|
if ( $ipsec ne '-' ) {
|
2007-03-31 19:44:16 +02:00
|
|
|
fatal_error "Non-empty IPSEC column requires policy match support in your kernel and iptables" unless $globals{ORIGINAL_POLICY_MATCH};
|
2007-03-15 02:04:43 +01:00
|
|
|
|
|
|
|
if ( $ipsec =~ /^yes$/i ) {
|
|
|
|
$rule .= '-m policy --pol ipsec --dir out ';
|
|
|
|
} elsif ( $ipsec =~ /^no$/i ) {
|
|
|
|
$rule .= '-m policy --pol none --dir out ';
|
|
|
|
} else {
|
|
|
|
$rule .= do_ipsec_options $ipsec;
|
|
|
|
}
|
2007-07-09 21:39:28 +02:00
|
|
|
} elsif ( $capabilities{POLICY_MATCH} ) {
|
|
|
|
$rule .= '-m policy --pol none --dir out ';
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Leading '+'
|
|
|
|
#
|
2007-05-20 15:49:50 +02:00
|
|
|
$pre_nat = 1 if $fullinterface =~ s/^\+//;
|
2007-03-15 02:04:43 +01:00
|
|
|
#
|
|
|
|
# Parse the remaining part of the INTERFACE column
|
|
|
|
#
|
|
|
|
if ( $fullinterface =~ /^([^:]+)::([^:]*)$/ ) {
|
2007-03-22 00:14:33 +01:00
|
|
|
$add_snat_aliases = 0;
|
2007-03-15 02:04:43 +01:00
|
|
|
$destnets = $2;
|
|
|
|
$fullinterface = $1;
|
|
|
|
} elsif ( $fullinterface =~ /^([^:]+:[^:]+):([^:]+)$/ ) {
|
|
|
|
$destnets = $2;
|
|
|
|
$fullinterface = $1;
|
|
|
|
} elsif ( $fullinterface =~ /^([^:]+):$/ ) {
|
2007-03-22 00:14:33 +01:00
|
|
|
$add_snat_aliases = 0;
|
2007-03-15 02:04:43 +01:00
|
|
|
$fullinterface = $1;
|
|
|
|
} elsif ( $fullinterface =~ /^([^:]+):([^:]*)$/ ) {
|
|
|
|
my ( $one, $two ) = ( $1, $2 );
|
|
|
|
if ( $2 =~ /\./ ) {
|
|
|
|
$fullinterface = $one;
|
|
|
|
$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
|
|
|
|
|
|
|
#
|
|
|
|
# Isolate and verify the interface part
|
|
|
|
#
|
|
|
|
( my $interface = $fullinterface ) =~ s/:.*//;
|
|
|
|
|
2007-11-16 02:21:33 +01:00
|
|
|
if ( $interface =~ /(.*)[(](\w*)[)]$/ ) {
|
|
|
|
$interface = $1;
|
|
|
|
my $realm = $2;
|
|
|
|
$fullinterface =~ s/[(]\w*[)]//;
|
|
|
|
$realm = lookup_provider( $realm ) unless $realm =~ /^\d+$/;
|
|
|
|
|
|
|
|
$rule .= "-m realm --realm $realm ";
|
|
|
|
}
|
|
|
|
|
2007-09-10 17:52:57 +02:00
|
|
|
fatal_error "Unknown interface ($interface)" unless find_interface( $interface )->{root};
|
2007-03-15 02:04:43 +01:00
|
|
|
|
2007-09-12 17:03:47 +02:00
|
|
|
my $chainref = ensure_chain('nat', $pre_nat ? snat_chain $interface : masq_chain $interface);
|
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 '-';
|
2007-03-15 02:04:43 +01:00
|
|
|
#
|
|
|
|
# Handle Protocol and Ports
|
|
|
|
#
|
|
|
|
$rule .= do_proto $proto, $ports, '';
|
2007-05-01 19:50:50 +02:00
|
|
|
#
|
|
|
|
# Handle Mark
|
|
|
|
#
|
2007-05-01 20:30:10 +02:00
|
|
|
$rule .= do_test( $mark, 0xFF) if $mark ne '-';
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-19 00:06:46 +01:00
|
|
|
my $detectaddress = 0;
|
2007-06-11 15:19:15 +02:00
|
|
|
my $exceptionrule = '';
|
2007-11-16 00:24:54 +01:00
|
|
|
my $randomize = '';
|
2007-03-15 02:04:43 +01:00
|
|
|
#
|
|
|
|
# Parse the ADDRESSES column
|
|
|
|
#
|
2007-03-26 01:18:20 +02:00
|
|
|
if ( $addresses ne '-' ) {
|
2007-11-16 00:24:54 +01:00
|
|
|
if ( $addresses eq 'random' ) {
|
|
|
|
$randomize = '--random ';
|
2007-03-15 02:04:43 +01:00
|
|
|
} else {
|
2007-11-16 00:24:54 +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/.*://;
|
|
|
|
for my $addr ( split /,/, $addresses ) {
|
|
|
|
$target .= "--to $addr ";
|
2007-03-23 22:24:28 +01:00
|
|
|
}
|
2007-11-16 00:24:54 +01:00
|
|
|
} elsif ( $addresses =~ /^SAME:/ ) {
|
|
|
|
fatal_error "':random' is not supported by the SAME target" if $randomize;
|
|
|
|
$target = '-j SAME ';
|
|
|
|
$addresses =~ s/.*://;
|
|
|
|
for my $addr ( split /,/, $addresses ) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
my $addrlist = '';
|
|
|
|
for my $addr ( split /,/, $addresses ) {
|
|
|
|
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, '', '' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$target .= $addrlist;
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
|
|
|
}
|
2007-11-16 00:24:54 +01:00
|
|
|
|
|
|
|
$target .= $randomize;
|
2007-03-22 00:14:33 +01:00
|
|
|
} else {
|
|
|
|
$add_snat_aliases = 0;
|
2007-03-19 00:06:46 +01:00
|
|
|
}
|
2007-03-15 02:04:43 +01:00
|
|
|
#
|
|
|
|
# And Generate the Rule(s)
|
|
|
|
#
|
2007-07-26 20:36:18 +02:00
|
|
|
expand_rule( $chainref ,
|
2007-06-11 15:19:15 +02:00
|
|
|
POSTROUTE_RESTRICT ,
|
|
|
|
$rule ,
|
|
|
|
$networks ,
|
|
|
|
$destnets ,
|
|
|
|
'' ,
|
|
|
|
$target ,
|
|
|
|
'' ,
|
|
|
|
'' ,
|
|
|
|
$exceptionrule );
|
2007-03-15 02:04:43 +01:00
|
|
|
|
2007-04-28 17:07:08 +02:00
|
|
|
if ( $detectaddress ) {
|
2007-08-26 17:12:04 +02:00
|
|
|
decr_cmd_level( $chainref );
|
2007-04-28 17:07:08 +02:00
|
|
|
add_command( $chainref , 'fi' );
|
|
|
|
}
|
|
|
|
|
2007-03-22 02:51:11 +01:00
|
|
|
if ( $add_snat_aliases ) {
|
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-22 02:51:11 +01:00
|
|
|
for my $address ( split /,/, $addresses ) {
|
|
|
|
my ( $addrs, $port ) = split /:/, $address;
|
|
|
|
next unless $addrs;
|
2007-04-25 18:00:15 +02:00
|
|
|
next if $addrs eq 'detect';
|
2007-03-22 02:51:11 +01:00
|
|
|
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;
|
2007-03-23 22:24:28 +01:00
|
|
|
if ( defined $alias ) {
|
|
|
|
push @addresses_to_add, $addr, "$interface:$alias";
|
|
|
|
$alias++;
|
|
|
|
} else {
|
|
|
|
push @addresses_to_add, $addr, $interface;
|
|
|
|
}
|
2007-03-22 02:51:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-15 02:04:43 +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-16 00:24:54 +01:00
|
|
|
first_entry( sub { progress_message2 "$doing $fn..."; require_capability 'NAT_ENABLED' , 'a non-empty masq file' , 's'; } );
|
|
|
|
|
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 = '';
|
|
|
|
|
|
|
|
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 ) {
|
|
|
|
if ( $interface =~ s/:$// ) {
|
|
|
|
$add_ip_aliases = '';
|
|
|
|
} else {
|
2007-03-22 02:51:11 +01:00
|
|
|
my ( $iface , undef ) = split /:/, $interface;
|
|
|
|
emit "del_ip_addr $external $iface" unless $config{RETAIN_ALIASES};
|
2007-03-15 02:04:43 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$interface =~ s/:$//;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
add_nat_rule input_chain( $interface ) , "-d $external $policyin -j DNAT --to-destination $internal";
|
|
|
|
add_nat_rule output_chain( $interface ) , "-s $internal $policyout -j SNAT --to-source $external";
|
|
|
|
}
|
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-07-07 18:34:38 +02:00
|
|
|
progress_message " NAT entry \"$currentline\" $done";
|
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-16 00:24:54 +01:00
|
|
|
first_entry( sub { progress_message2 "$doing $fn..."; require_capability 'NAT_ENABLED' , 'a non-empty nat file' , 's'; } );
|
|
|
|
|
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 ( $external, $interface, $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 {
|
|
|
|
do_one_nat $external, $interface, $internal, $allints, $localnat;
|
|
|
|
}
|
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-16 00:24:54 +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
|
|
|
|
2007-05-09 21:03:09 +02:00
|
|
|
my ( $type, $net1, $interface, $net2 ) = split_line 4, 4, 'netmap file';
|
|
|
|
|
2007-04-29 20:33:30 +02:00
|
|
|
fatal_error "Unknown Interface ($interface)" unless known_interface $interface;
|
|
|
|
|
2007-03-25 23:04:24 +02:00
|
|
|
if ( $type eq 'DNAT' ) {
|
2007-09-12 17:03:47 +02:00
|
|
|
add_rule ensure_chain( 'nat' , input_chain $interface ) , "-d $net1 -j NETMAP --to $net2";
|
2007-03-25 23:04:24 +02:00
|
|
|
} elsif ( $type eq 'SNAT' ) {
|
2007-09-12 17:03:47 +02:00
|
|
|
add_rule ensure_chain( 'nat' , output_chain $interface ) , "-s $net1 -j NETMAP --to $net2";
|
2007-03-25 23:04:24 +02:00
|
|
|
} else {
|
2007-03-30 04:05:11 +02:00
|
|
|
fatal_error "Invalid type ($type)";
|
2007-03-25 23:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
progress_message " Network $net1 on $interface mapped to $net2 ($type)";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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;
|