2007-03-22 01:14:56 +01:00
|
|
|
#
|
2007-12-01 18:45:43 +01:00
|
|
|
# Shorewall-perl 4.1 -- /usr/share/shorewall-perl/Shorewall/IPAddrs.pm
|
2007-03-22 01:14:56 +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-22 01:14:56 +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-22 01:14:56 +01:00
|
|
|
#
|
2008-02-06 00:35:53 +01:00
|
|
|
# This module provides interfaces for dealing with IPv4 addresses, protocol names, and
|
|
|
|
# port names. It also exports functions for validating protocol- and port- (service)
|
|
|
|
# related constructs.
|
2007-07-26 20:36:18 +02:00
|
|
|
#
|
2007-03-22 01:14:56 +01:00
|
|
|
package Shorewall::IPAddrs;
|
|
|
|
require Exporter;
|
2008-03-16 17:51:53 +01:00
|
|
|
use Shorewall::Config qw( :DEFAULT split_list require_capability in_hex8);
|
2007-03-22 01:14:56 +01:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
2007-05-11 17:39:11 +02:00
|
|
|
our @EXPORT = qw( ALLIPv4
|
2007-10-19 21:43:14 +02:00
|
|
|
TCP
|
|
|
|
UDP
|
|
|
|
ICMP
|
2008-03-07 01:36:16 +01:00
|
|
|
SCTP
|
2007-05-11 17:39:11 +02:00
|
|
|
|
2007-05-08 21:05:25 +02:00
|
|
|
validate_address
|
2007-05-08 20:25:16 +02:00
|
|
|
validate_net
|
2008-03-16 17:51:53 +01:00
|
|
|
decompose_net
|
2007-07-02 01:37:45 +02:00
|
|
|
validate_host
|
2007-05-08 20:25:16 +02:00
|
|
|
validate_range
|
2007-03-23 22:24:28 +01:00
|
|
|
ip_range_explicit
|
2007-09-10 17:52:57 +02:00
|
|
|
allipv4
|
|
|
|
rfc1918_neworks
|
2008-02-05 17:43:11 +01:00
|
|
|
resolve_proto
|
|
|
|
proto_name
|
|
|
|
validate_port
|
|
|
|
validate_portpair
|
|
|
|
validate_port_list
|
|
|
|
validate_icmp
|
2007-03-22 01:14:56 +01:00
|
|
|
);
|
|
|
|
our @EXPORT_OK = qw( );
|
2008-02-06 00:35:53 +01:00
|
|
|
our $VERSION = 4.1.5;
|
2007-03-22 01:14:56 +01:00
|
|
|
|
2007-05-11 17:39:11 +02:00
|
|
|
#
|
|
|
|
# Some IPv4 useful stuff
|
|
|
|
#
|
|
|
|
our @allipv4 = ( '0.0.0.0/0' );
|
|
|
|
|
2008-03-07 01:36:16 +01:00
|
|
|
use constant { ALLIPv4 => '0.0.0.0/0' , ICMP => 1, TCP => 6, UDP => 17 , SCTP => 132 };
|
2007-05-11 17:39:11 +02:00
|
|
|
|
|
|
|
our @rfc1918_networks = ( "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16" );
|
|
|
|
|
2008-03-19 23:40:03 +01:00
|
|
|
sub vlsm_to_mask( $ ) {
|
|
|
|
my $vlsm = $_[0];
|
|
|
|
|
|
|
|
in_hex8 ( ( 0xFFFFFFFF << ( 32 - $vlsm ) ) && 0xFFFFFFFF );
|
|
|
|
}
|
2008-03-16 17:51:53 +01:00
|
|
|
|
2007-03-22 01:14:56 +01:00
|
|
|
sub valid_address( $ ) {
|
|
|
|
my $address = $_[0];
|
|
|
|
|
|
|
|
my @address = split /\./, $address;
|
|
|
|
return 0 unless @address == 4;
|
|
|
|
for my $a ( @address ) {
|
|
|
|
return 0 unless $a =~ /^\d+$/ && $a < 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
}
|
|
|
|
|
2007-09-21 18:55:28 +02:00
|
|
|
sub validate_address( $$ ) {
|
|
|
|
my ( $addr, $allow_name ) = @_;
|
2007-05-15 22:02:42 +02:00
|
|
|
|
2008-01-17 20:02:40 +01:00
|
|
|
my @addrs = ( $addr );
|
|
|
|
|
2007-05-15 22:02:42 +02:00
|
|
|
unless ( valid_address $addr ) {
|
2007-09-21 18:55:28 +02:00
|
|
|
fatal_error "Invalid IP Address ($addr)" unless $allow_name;
|
2008-01-17 20:02:40 +01:00
|
|
|
fatal_error "Unknown Host ($addr)" unless (@addrs = gethostbyname $addr);
|
|
|
|
|
|
|
|
if ( defined wantarray ) {
|
|
|
|
shift @addrs for (1..4);
|
|
|
|
for ( @addrs ) {
|
|
|
|
my (@a) = unpack('C4',$_);
|
|
|
|
$_ = join('.', @a );
|
|
|
|
}
|
|
|
|
}
|
2007-05-08 21:05:25 +02:00
|
|
|
}
|
2008-01-17 20:02:40 +01:00
|
|
|
|
|
|
|
defined wantarray ? wantarray ? @addrs : $addrs[0] : undef;
|
2007-05-08 21:05:25 +02:00
|
|
|
}
|
|
|
|
|
2007-03-22 01:14:56 +01:00
|
|
|
sub decodeaddr( $ ) {
|
|
|
|
my $address = $_[0];
|
|
|
|
|
|
|
|
my @address = split /\./, $address;
|
|
|
|
|
|
|
|
my $result = shift @address;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-22 01:14:56 +01:00
|
|
|
for my $a ( @address ) {
|
|
|
|
$result = ( $result << 8 ) | $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub encodeaddr( $ ) {
|
|
|
|
my $addr = $_[0];
|
|
|
|
my $result = $addr & 0xff;
|
|
|
|
|
|
|
|
for my $i ( 1..3 ) {
|
|
|
|
my $a = ($addr = $addr >> 8) & 0xff;
|
|
|
|
$result = "$a.$result";
|
|
|
|
}
|
|
|
|
|
|
|
|
$result;
|
|
|
|
}
|
|
|
|
|
2008-03-16 17:51:53 +01:00
|
|
|
sub validate_net( $$ ) {
|
|
|
|
my ($net, $vlsm, $rest) = split( '/', $_[0], 3 );
|
|
|
|
my $allow_name = $_[1];
|
|
|
|
|
|
|
|
fatal_error "Missing address" if $net eq '';
|
|
|
|
fatal_error "An ipset name ($net) is not allowed in this context" if substr( $net, 0, 1 ) eq '+';
|
|
|
|
|
|
|
|
if ( defined $vlsm ) {
|
|
|
|
fatal_error "Invalid VLSM ($vlsm)" unless $vlsm =~ /^\d+$/ && $vlsm <= 32;
|
|
|
|
fatal_error "Invalid Network address ($_[0])" if defined $rest;
|
|
|
|
fatal_error "Invalid IP address ($net)" unless valid_address $net;
|
|
|
|
} else {
|
|
|
|
fatal_error "Invalid Network address ($_[0])" if $_[0] =~ '/' || ! defined $net;
|
|
|
|
validate_address $net, $_[1];
|
|
|
|
$vlsm = 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( defined wantarray ) {
|
|
|
|
fatal_error "Internal Error in validate_net()" if $allow_name;
|
|
|
|
if ( wantarray ) {
|
|
|
|
( decodeaddr( $net ) , $vlsm );
|
|
|
|
} else {
|
|
|
|
"$net/$vlsm";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-08 20:25:16 +02:00
|
|
|
sub validate_range( $$ ) {
|
|
|
|
my ( $low, $high ) = @_;
|
|
|
|
|
2007-09-21 18:55:28 +02:00
|
|
|
validate_address $low, 0;
|
|
|
|
validate_address $high, 0;
|
2007-05-08 20:25:16 +02:00
|
|
|
|
|
|
|
my $first = decodeaddr $low;
|
|
|
|
my $last = decodeaddr $high;
|
|
|
|
|
2007-06-16 23:08:12 +02:00
|
|
|
fatal_error "Invalid IP Range ($low-$high)" unless $first <= $last;
|
2007-07-26 20:36:18 +02:00
|
|
|
}
|
2007-05-08 20:25:16 +02:00
|
|
|
|
2007-03-22 01:14:56 +01:00
|
|
|
sub ip_range_explicit( $ ) {
|
|
|
|
my $range = $_[0];
|
|
|
|
my @result;
|
|
|
|
|
|
|
|
my ( $low, $high ) = split /-/, $range;
|
|
|
|
|
2007-09-21 18:55:28 +02:00
|
|
|
validate_address $low, 0;
|
2007-03-22 01:14:56 +01:00
|
|
|
|
|
|
|
push @result, $low;
|
|
|
|
|
|
|
|
if ( defined $high ) {
|
2007-09-21 18:55:28 +02:00
|
|
|
validate_address $high, 0;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-22 01:14:56 +01:00
|
|
|
my $first = decodeaddr $low;
|
|
|
|
my $last = decodeaddr $high;
|
2007-10-19 21:43:14 +02:00
|
|
|
my $diff = $last - $first;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-10-19 21:43:14 +02:00
|
|
|
fatal_error "Invalid IP Range ($range)" unless $diff >= 0 && $diff <= 256;
|
2007-03-22 01:14:56 +01:00
|
|
|
|
|
|
|
while ( ++$first <= $last ) {
|
|
|
|
push @result, encodeaddr( $first );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@result;
|
|
|
|
}
|
|
|
|
|
2008-03-16 17:51:53 +01:00
|
|
|
sub decompose_net( $ ) {
|
|
|
|
my $net = $_[0];
|
|
|
|
|
|
|
|
return ( qw/0x00000000 0x00000000/ ) if $net eq '-';
|
|
|
|
|
|
|
|
( $net, my $vlsm ) = validate_net( $net , 0 );
|
|
|
|
|
2008-03-19 23:40:03 +01:00
|
|
|
( in_hex8( $net ) , vlsm_to_mask( $vlsm ) );
|
2008-03-16 17:51:53 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-02-24 18:05:27 +01:00
|
|
|
sub validate_host( $$ ) {
|
|
|
|
my ( $host, $allow_name ) = $_[0];
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-02 01:37:45 +02:00
|
|
|
if ( $host =~ /^(\d+\.\d+\.\d+\.\d+)-(\d+\.\d+\.\d+\.\d+)$/ ) {
|
|
|
|
validate_range $1, $2;
|
|
|
|
} else {
|
2008-02-24 18:05:27 +01:00
|
|
|
validate_net( $host, $allow_name );
|
2007-07-02 01:37:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-10 17:52:57 +02:00
|
|
|
sub allipv4() {
|
|
|
|
@allipv4;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rfc1918_networks() {
|
|
|
|
@rfc1918_networks
|
|
|
|
}
|
|
|
|
|
2008-02-05 17:43:11 +01:00
|
|
|
#
|
2008-02-06 00:35:53 +01:00
|
|
|
# Protocol/port validation
|
2008-02-05 17:43:11 +01:00
|
|
|
#
|
|
|
|
|
|
|
|
our %nametoproto = ( all => 0, ALL => 0, icmp => 1, ICMP => 1, tcp => 6, TCP => 6, udp => 17, UDP => 17 );
|
|
|
|
our @prototoname = ( 'all', 'icmp', '', '', '', '', 'tcp', '', '', '', '', '', '', '', '', '', '', 'udp' );
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the protocol number if the passed argument is a valid protocol number or name. Returns undef otherwise
|
|
|
|
#
|
|
|
|
sub resolve_proto( $ ) {
|
|
|
|
my $proto = $_[0];
|
|
|
|
my $number;
|
|
|
|
|
|
|
|
$proto =~ /^(\d+)$/ ? $proto <= 65535 ? $proto : undef : defined( $number = $nametoproto{$proto} ) ? $number : scalar getprotobyname $proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub proto_name( $ ) {
|
|
|
|
my $proto = $_[0];
|
|
|
|
|
|
|
|
$proto =~ /^(\d+)$/ ? $prototoname[ $proto ] || scalar getprotobynumber $proto : $proto
|
|
|
|
}
|
|
|
|
|
|
|
|
sub validate_port( $$ ) {
|
|
|
|
my ($proto, $port) = @_;
|
|
|
|
|
|
|
|
my $value;
|
|
|
|
|
|
|
|
if ( $port =~ /^(\d+)$/ ) {
|
|
|
|
return $port if $port <= 65535;
|
|
|
|
} else {
|
|
|
|
$proto = getprotobyname $proto if $proto =~ /^(\d+)$/;
|
|
|
|
$value = getservbyname( $port, $proto );
|
|
|
|
}
|
|
|
|
|
|
|
|
fatal_error "Invalid/Unknown $proto port/service ($port)" unless defined $value;
|
|
|
|
|
|
|
|
$value;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub validate_portpair( $$ ) {
|
|
|
|
my ($proto, $portpair) = @_;
|
|
|
|
|
|
|
|
fatal_error "Invalid port range ($portpair)" if $portpair =~ tr/:/:/ > 1;
|
|
|
|
|
|
|
|
$portpair = "0$portpair" if substr( $portpair, 0, 1 ) eq ':';
|
|
|
|
$portpair = "${portpair}65535" if substr( $portpair, -1, 1 ) eq ':';
|
|
|
|
|
|
|
|
my @ports = split /:/, $portpair, 2;
|
|
|
|
|
|
|
|
$_ = validate_port( $proto, $_) for ( @ports );
|
|
|
|
|
|
|
|
if ( @ports == 2 ) {
|
|
|
|
fatal_error "Invalid port range ($portpair)" unless $ports[0] < $ports[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
join ':', @ports;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sub validate_port_list( $$ ) {
|
|
|
|
my $result = '';
|
|
|
|
my ( $proto, $list ) = @_;
|
|
|
|
my @list = split_list( $list, 'port' );
|
|
|
|
|
|
|
|
if ( @list > 1 && $list =~ /:/ ) {
|
|
|
|
require_capability( 'XMULTIPORT' , 'Port ranges in a port list', '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$proto = proto_name $proto;
|
|
|
|
|
|
|
|
for ( @list ) {
|
|
|
|
my $value = validate_portpair( $proto , $_ );
|
|
|
|
$result = $result ? join ',', $result, $value : $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result;
|
|
|
|
}
|
|
|
|
|
|
|
|
my %icmp_types = ( any => 'any',
|
|
|
|
'echo-reply' => 0,
|
|
|
|
'destination-unreachable' => 3,
|
|
|
|
'network-unreachable' => '3/0',
|
|
|
|
'host-unreachable' => '3/1',
|
|
|
|
'protocol-unreachable' => '3/2',
|
|
|
|
'port-unreachable' => '3/3',
|
|
|
|
'fragmentation-needed' => '3/4',
|
|
|
|
'source-route-failed' => '3/5',
|
|
|
|
'network-unknown' => '3/6',
|
|
|
|
'host-unknown' => '3/7',
|
|
|
|
'network-prohibited' => '3/9',
|
|
|
|
'host-prohibited' => '3/10',
|
|
|
|
'TOS-network-unreachable' => '3/11',
|
|
|
|
'TOS-host-unreachable' => '3/12',
|
|
|
|
'communication-prohibited' => '3/13',
|
|
|
|
'host-precedence-violation' => '3/14',
|
|
|
|
'precedence-cutoff' => '3/15',
|
|
|
|
'source-quench' => 4,
|
|
|
|
'redirect' => 5,
|
|
|
|
'network-redirect' => '5/0',
|
|
|
|
'host-redirect' => '5/1',
|
|
|
|
'TOS-network-redirect' => '5/2',
|
|
|
|
'TOS-host-redirect' => '5/3',
|
|
|
|
'echo-request' => '8',
|
|
|
|
'router-advertisement' => 9,
|
|
|
|
'router-solicitation' => 10,
|
|
|
|
'time-exceeded' => 11,
|
|
|
|
'ttl-zero-during-transit' => '11/0',
|
|
|
|
'ttl-zero-during-reassembly' => '11/1',
|
|
|
|
'parameter-problem' => 12,
|
|
|
|
'ip-header-bad' => '12/0',
|
|
|
|
'required-option-missing' => '12/1',
|
|
|
|
'timestamp-request' => 13,
|
|
|
|
'timestamp-reply' => 14,
|
|
|
|
'address-mask-request' => 17,
|
|
|
|
'address-mask-reply' => 18 );
|
|
|
|
|
|
|
|
sub validate_icmp( $ ) {
|
|
|
|
my $type = $_[0];
|
|
|
|
|
|
|
|
my $value = $icmp_types{$type};
|
|
|
|
|
|
|
|
return $value if defined $value;
|
|
|
|
|
|
|
|
if ( $type =~ /^(\d+)(\/(\d+))?$/ ) {
|
|
|
|
return $type if $1 < 256 && ( ! $2 || $3 < 256 );
|
|
|
|
}
|
|
|
|
|
|
|
|
fatal_error "Invalid ICMP Type ($type)"
|
|
|
|
}
|
|
|
|
|
2007-03-22 01:14:56 +01:00
|
|
|
1;
|