2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-12-01 18:45:43 +01:00
|
|
|
# Shorewall-perl 4.1 -- /usr/share/shorewall-perl/Shorewall/Providers.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 deals with the /etc/shorewall/providers and
|
|
|
|
# /etc/shorewall/route_rules files.
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-03-15 02:38:04 +01:00
|
|
|
package Shorewall::Providers;
|
|
|
|
require Exporter;
|
2007-11-16 00:24:54 +01:00
|
|
|
use Shorewall::Config qw(:DEFAULT :internal);
|
2007-05-14 16:14:38 +02:00
|
|
|
use Shorewall::IPAddrs;
|
2007-03-15 02:38:04 +01:00
|
|
|
use Shorewall::Zones;
|
2007-11-16 00:24:54 +01:00
|
|
|
use Shorewall::Chains qw(:DEFAULT :internal);
|
2007-03-15 02:38:04 +01:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
2007-03-21 18:16:47 +01:00
|
|
|
our @EXPORT = qw( setup_providers @routemarked_interfaces);
|
2007-11-16 02:21:33 +01:00
|
|
|
our @EXPORT_OK = qw( initialize lookup_provider );
|
2008-02-06 00:50:00 +01:00
|
|
|
our $VERSION = 4.1.5;
|
2007-03-15 02:38:04 +01:00
|
|
|
|
|
|
|
use constant { LOCAL_NUMBER => 255,
|
|
|
|
MAIN_NUMBER => 254,
|
|
|
|
DEFAULT_NUMBER => 253,
|
|
|
|
UNSPEC_NUMBER => 0
|
|
|
|
};
|
|
|
|
|
2007-11-16 01:14:43 +01:00
|
|
|
our @routemarked_providers;
|
2007-03-21 00:13:17 +01:00
|
|
|
our %routemarked_interfaces;
|
2007-03-21 18:16:47 +01:00
|
|
|
our @routemarked_interfaces;
|
2007-03-21 00:13:17 +01:00
|
|
|
|
2007-06-14 01:02:39 +02:00
|
|
|
our $balance;
|
|
|
|
our $first_default_route;
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-06-14 01:02:39 +02:00
|
|
|
our %providers;
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-06-14 01:02:39 +02:00
|
|
|
our @providers;
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-11-16 01:14:43 +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() {
|
2007-11-16 01:14:43 +01:00
|
|
|
@routemarked_providers = ();
|
2007-06-14 01:02:39 +02:00
|
|
|
%routemarked_interfaces = ();
|
|
|
|
@routemarked_interfaces = ();
|
|
|
|
$balance = 0;
|
|
|
|
$first_default_route = 1;
|
2008-02-10 00:51:28 +01:00
|
|
|
|
2008-02-13 20:11:02 +01:00
|
|
|
%providers = ( local => { number => LOCAL_NUMBER , mark => 0 , optional => 0 } ,
|
2008-02-10 00:51:28 +01:00
|
|
|
main => { number => MAIN_NUMBER , mark => 0 , optional => 0 } ,
|
|
|
|
default => { number => DEFAULT_NUMBER , mark => 0 , optional => 0 } ,
|
|
|
|
unspec => { number => UNSPEC_NUMBER , mark => 0 , optional => 0 } );
|
2007-06-14 01:02:39 +02:00
|
|
|
@providers = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT {
|
|
|
|
initialize;
|
|
|
|
}
|
2007-03-15 02:38:04 +01:00
|
|
|
|
|
|
|
#
|
2008-02-09 21:59:42 +01:00
|
|
|
# Set up marking for 'tracked' interfaces.
|
2007-03-15 02:38:04 +01:00
|
|
|
#
|
|
|
|
sub setup_route_marking() {
|
2007-11-16 00:24:54 +01:00
|
|
|
my $mask = $config{HIGH_ROUTE_MARKS} ? '0xFF00' : '0xFF';
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-05-04 17:12:29 +02:00
|
|
|
require_capability( 'CONNMARK_MATCH' , 'the provider \'track\' option' , 's' );
|
|
|
|
require_capability( 'CONNMARK' , 'the provider \'track\' option' , 's' );
|
2007-03-29 05:07:48 +02:00
|
|
|
|
2007-09-12 17:03:47 +02:00
|
|
|
add_rule $mangle_table->{PREROUTING} , "-m connmark ! --mark 0/$mask -j CONNMARK --restore-mark --mask $mask";
|
|
|
|
add_rule $mangle_table->{OUTPUT} , "-m connmark ! --mark 0/$mask -j CONNMARK --restore-mark --mask $mask";
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-09-12 17:03:47 +02:00
|
|
|
my $chainref = new_chain 'mangle', 'routemark';
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-11-16 01:14:43 +01:00
|
|
|
my %marked_interfaces;
|
|
|
|
|
|
|
|
for my $providerref ( @routemarked_providers ) {
|
|
|
|
my $interface = $providerref->{interface};
|
2008-02-09 23:39:23 +01:00
|
|
|
my $base = uc chain_base $interface;
|
|
|
|
|
|
|
|
add_command( $chainref, qq(if [ -n "${base}_IS_UP" ]; then) ), incr_cmd_level( $chainref ) if $providerref->{optional};
|
2007-11-16 01:14:43 +01:00
|
|
|
|
|
|
|
unless ( $marked_interfaces{$interface} ) {
|
|
|
|
add_rule $mangle_table->{PREROUTING} , "-i $interface -m mark --mark 0/$mask -j routemark";
|
|
|
|
$marked_interfaces{$interface} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $providerref->{shared} ) {
|
2007-11-16 17:44:02 +01:00
|
|
|
add_rule $chainref, " -i $interface -m mac --mac-source $providerref->{mac} -j MARK --set-mark $providerref->{mark}";
|
2007-11-16 01:14:43 +01:00
|
|
|
} else {
|
|
|
|
add_rule $chainref, " -i $interface -j MARK --set-mark $providerref->{mark}";
|
|
|
|
}
|
2008-02-09 21:59:42 +01:00
|
|
|
|
|
|
|
decr_cmd_level( $chainref), add_command( $chainref, "fi" ) if $providerref->{optional};
|
2007-03-15 02:38:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
add_rule $chainref, "-m mark ! --mark 0/$mask -j CONNMARK --save-mark --mask $mask";
|
|
|
|
}
|
|
|
|
|
2007-11-16 01:14:43 +01:00
|
|
|
sub copy_table( $$$ ) {
|
|
|
|
my ( $duplicate, $number, $realm ) = @_;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-11-17 01:14:41 +01:00
|
|
|
if ( $realm ) {
|
|
|
|
emit ( "ip route show table $duplicate | sed -r 's/ realm [[:alnum:]_]+//' | while read net route; do" )
|
|
|
|
} else {
|
|
|
|
emit ( "ip route show table $duplicate | while read net route; do" )
|
|
|
|
}
|
|
|
|
|
|
|
|
emit ( ' case $net in',
|
2007-07-13 23:12:23 +02:00
|
|
|
' default|nexthop)',
|
|
|
|
' ;;',
|
|
|
|
' *)',
|
2007-11-16 01:14:43 +01:00
|
|
|
" run_ip route add table $number \$net \$route $realm",
|
2007-07-13 23:12:23 +02:00
|
|
|
' ;;',
|
|
|
|
' esac',
|
|
|
|
"done\n"
|
|
|
|
);
|
|
|
|
}
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-11-16 01:14:43 +01:00
|
|
|
sub copy_and_edit_table( $$$$ ) {
|
|
|
|
my ( $duplicate, $number, $copy, $realm) = @_;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-11-17 01:14:41 +01:00
|
|
|
if ( $realm ) {
|
|
|
|
emit ( "ip route show table $duplicate | sed -r 's/ realm [[:alnum:]_]+//' | while read net route; do" )
|
|
|
|
} else {
|
|
|
|
emit ( "ip route show table $duplicate | while read net route; do" )
|
|
|
|
}
|
|
|
|
|
|
|
|
emit ( ' case $net in',
|
2007-07-13 23:12:23 +02:00
|
|
|
' default|nexthop)',
|
|
|
|
' ;;',
|
|
|
|
' *)',
|
|
|
|
' case $(find_device $route) in',
|
|
|
|
" $copy)",
|
2007-11-16 01:14:43 +01:00
|
|
|
" run_ip route add table $number \$net \$route $realm",
|
2007-07-13 23:12:23 +02:00
|
|
|
' ;;',
|
|
|
|
' esac',
|
|
|
|
' ;;',
|
|
|
|
' esac',
|
|
|
|
"done\n" );
|
|
|
|
}
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-11-16 02:21:33 +01:00
|
|
|
sub balance_default_route( $$$$ ) {
|
|
|
|
my ( $weight, $gateway, $interface, $realm ) = @_;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
$balance = 1;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
emit '';
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
if ( $first_default_route ) {
|
|
|
|
if ( $gateway ) {
|
2007-11-16 02:21:33 +01:00
|
|
|
emit "DEFAULT_ROUTE=\"nexthop via $gateway dev $interface weight $weight $realm\"";
|
2007-07-13 23:12:23 +02:00
|
|
|
} else {
|
2007-11-16 02:21:33 +01:00
|
|
|
emit "DEFAULT_ROUTE=\"nexthop dev $interface weight $weight $realm\"";
|
2007-07-13 23:12:23 +02:00
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
$first_default_route = 0;
|
|
|
|
} else {
|
|
|
|
if ( $gateway ) {
|
2007-11-16 02:21:33 +01:00
|
|
|
emit "DEFAULT_ROUTE=\"\$DEFAULT_ROUTE nexthop via $gateway dev $interface weight $weight $realm\"";
|
2007-07-13 23:12:23 +02:00
|
|
|
} else {
|
2007-11-16 02:21:33 +01:00
|
|
|
emit "DEFAULT_ROUTE=\"\$DEFAULT_ROUTE nexthop dev $interface weight $weight $realm\"";
|
2007-07-13 23:12:23 +02:00
|
|
|
}
|
2007-03-15 02:38:04 +01:00
|
|
|
}
|
2007-07-13 23:12:23 +02:00
|
|
|
}
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
sub add_a_provider( $$$$$$$$ ) {
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
my ($table, $number, $mark, $duplicate, $interface, $gateway, $options, $copy) = @_;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
fatal_error "Duplicate provider ($table)" if $providers{$table};
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-12-07 20:06:01 +01:00
|
|
|
my $num = numeric_value $number;
|
|
|
|
|
|
|
|
fatal_error "Invalid Provider number ($number)" unless defined $num;
|
|
|
|
|
|
|
|
$number = $num;
|
2007-09-21 18:55:28 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
for my $providerref ( values %providers ) {
|
|
|
|
fatal_error "Duplicate provider number ($number)" if $providerref->{number} == $number;
|
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-11-21 17:28:36 +01:00
|
|
|
( $interface, my $address ) = split /:/, $interface;
|
|
|
|
|
2007-11-21 20:39:20 +01:00
|
|
|
my $shared = 0;
|
|
|
|
|
|
|
|
if ( defined $address ) {
|
|
|
|
validate_address $address, 0;
|
|
|
|
$shared = 1;
|
2007-11-21 22:30:21 +01:00
|
|
|
require_capability 'REALM_MATCH', "Configuring multiple providers through one interface", "s";
|
2007-11-21 20:39:20 +01:00
|
|
|
}
|
2007-11-21 17:28:36 +01:00
|
|
|
|
2007-11-17 17:48:25 +01:00
|
|
|
fatal_error "Unknown Interface ($interface)" unless known_interface $interface;
|
|
|
|
|
2007-08-13 22:10:13 +02:00
|
|
|
my $provider = chain_base $table;
|
2008-02-09 23:39:23 +01:00
|
|
|
my $base = uc chain_base $interface;
|
2007-08-13 22:10:13 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
emit "#\n# Add Provider $table ($number)\n#";
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
emit "if interface_is_usable $interface; then";
|
|
|
|
push_indent;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
emit "qt ip route flush table $number";
|
|
|
|
emit "echo \"qt ip route flush table $number\" >> \${VARDIR}/undo_routing";
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
if ( $gateway eq 'detect' ) {
|
2007-11-21 22:30:21 +01:00
|
|
|
$gateway = get_interface_gateway $interface;
|
2007-07-13 23:12:23 +02:00
|
|
|
} elsif ( $gateway && $gateway ne '-' ) {
|
2007-09-21 18:55:28 +02:00
|
|
|
validate_address $gateway, 0;
|
2007-07-13 23:12:23 +02:00
|
|
|
} else {
|
2007-11-21 22:30:21 +01:00
|
|
|
fatal_error "Configuring multiple providers through one interface requires a gateway" if $shared;
|
2007-07-13 23:12:23 +02:00
|
|
|
$gateway = '';
|
|
|
|
emit "run_ip route add default dev $interface table $number";
|
|
|
|
}
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
my $val = 0;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
if ( $mark ne '-' ) {
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
$val = numeric_value $mark;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-12-07 20:06:01 +01:00
|
|
|
fatal_error "Invalid Mark Value ($mark)" unless defined $val;
|
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
verify_mark $mark;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
if ( $val < 256) {
|
|
|
|
fatal_error "Invalid Mark Value ($mark) with HIGH_ROUTE_MARKS=Yes" if $config{HIGH_ROUTE_MARKS};
|
2007-03-15 02:38:04 +01:00
|
|
|
} else {
|
2007-07-13 23:12:23 +02:00
|
|
|
fatal_error "Invalid Mark Value ($mark) with HIGH_ROUTE_MARKS=No" if ! $config{HIGH_ROUTE_MARKS};
|
2007-03-15 02:38:04 +01:00
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
for my $providerref ( values %providers ) {
|
|
|
|
fatal_error "Duplicate mark value ($mark)" if $providerref->{mark} == $val;
|
2007-03-15 02:38:04 +01:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-09-21 18:55:28 +02:00
|
|
|
my $pref = 10000 + $number - 1;
|
|
|
|
|
|
|
|
emit ( "qt ip rule del fwmark $mark" ) if $config{DELETE_THEN_ADD};
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-09-21 18:55:28 +02:00
|
|
|
emit ( "run_ip rule add fwmark $mark pref $pref table $number",
|
2007-07-13 23:12:23 +02:00
|
|
|
"echo \"qt ip rule del fwmark $mark\" >> \${VARDIR}/undo_routing"
|
|
|
|
);
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2008-02-11 19:30:45 +01:00
|
|
|
my ( $loose, $track, $balance , $optional, $mtu ) = (0,0,0,interface_is_optional( $interface ), '' );
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
unless ( $options eq '-' ) {
|
2008-01-26 02:07:57 +01:00
|
|
|
for my $option ( split_list $options, 'option' ) {
|
2007-07-13 23:12:23 +02:00
|
|
|
if ( $option eq 'track' ) {
|
2007-11-16 01:14:43 +01:00
|
|
|
$track = 1;
|
2007-07-13 23:12:23 +02:00
|
|
|
} elsif ( $option =~ /^balance=(\d+)$/ ) {
|
2007-11-16 02:21:33 +01:00
|
|
|
$balance = $1;
|
2007-07-13 23:12:23 +02:00
|
|
|
} elsif ( $option eq 'balance' ) {
|
2007-11-16 02:21:33 +01:00
|
|
|
$balance = 1;
|
2007-07-13 23:12:23 +02:00
|
|
|
} elsif ( $option eq 'loose' ) {
|
|
|
|
$loose = 1;
|
|
|
|
} elsif ( $option eq 'optional' ) {
|
2007-11-17 17:48:25 +01:00
|
|
|
set_interface_option $interface, 'optional', 1;
|
2007-07-13 23:12:23 +02:00
|
|
|
$optional = 1;
|
2008-02-11 19:30:45 +01:00
|
|
|
} elsif ( $option =~ /^src=(.*)$/ ) {
|
|
|
|
fatal_error "OPTION 'src' not allowed on shared interface" if $shared;
|
|
|
|
$address = validate_address( $1 , 1 );
|
|
|
|
} elsif ( $option =~ /^mtu=(\d+)$/ ) {
|
|
|
|
$mtu = "mtu $1 ";
|
2007-03-15 02:38:04 +01:00
|
|
|
} else {
|
2007-07-13 23:12:23 +02:00
|
|
|
fatal_error "Invalid option ($option)";
|
2007-03-15 02:38:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-17 17:48:25 +01:00
|
|
|
$providers{$table} = { provider => $table,
|
|
|
|
number => $number ,
|
|
|
|
mark => $val ,
|
|
|
|
interface => $interface ,
|
|
|
|
optional => $optional ,
|
|
|
|
gateway => $gateway ,
|
|
|
|
shared => $shared };
|
2007-11-16 01:14:43 +01:00
|
|
|
|
|
|
|
if ( $track ) {
|
|
|
|
fatal_error "The 'track' option requires a numeric value in the MARK column" if $mark eq '-';
|
|
|
|
|
|
|
|
if ( $routemarked_interfaces{$interface} ) {
|
|
|
|
fatal_error "Interface $interface is tracked through an earlier provider" if $routemarked_interfaces{$interface} > 1;
|
2007-12-10 23:53:20 +01:00
|
|
|
fatal_error "Multiple providers through the same interface must their IP address specified in the INTERFACES" unless $shared;
|
2007-11-16 01:14:43 +01:00
|
|
|
} else {
|
|
|
|
$routemarked_interfaces{$interface} = $shared ? 1 : 2;
|
|
|
|
push @routemarked_interfaces, $interface;
|
|
|
|
}
|
|
|
|
|
|
|
|
push @routemarked_providers, $providers{$table};
|
|
|
|
}
|
|
|
|
|
|
|
|
my $realm = '';
|
|
|
|
|
|
|
|
if ( $shared ) {
|
2007-11-18 02:48:54 +01:00
|
|
|
$providers{$table}{mac} = get_interface_mac( $gateway, $interface , $table );
|
2007-11-16 01:14:43 +01:00
|
|
|
$realm = "realm $number";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $duplicate ne '-' ) {
|
|
|
|
if ( $copy eq '-' ) {
|
|
|
|
copy_table ( $duplicate, $number, $realm );
|
|
|
|
} else {
|
|
|
|
if ( $copy eq 'none' ) {
|
|
|
|
$copy = $interface;
|
|
|
|
} else {
|
|
|
|
$copy =~ tr/,/|/;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_and_edit_table( $duplicate, $number ,$copy , $realm);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fatal_error 'A non-empty COPY column requires that a routing table be specified in the DUPLICATE column' if $copy ne '-';
|
|
|
|
}
|
|
|
|
|
2007-11-17 01:14:41 +01:00
|
|
|
if ( $gateway ) {
|
2008-02-11 19:30:45 +01:00
|
|
|
$address = get_interface_address $interface unless $address;
|
|
|
|
emit "run_ip route replace $gateway src $address dev $interface ${mtu}table $number $realm";
|
2007-11-17 01:14:41 +01:00
|
|
|
emit "run_ip route add default via $gateway dev $interface table $number $realm";
|
|
|
|
}
|
2007-08-13 22:10:13 +02:00
|
|
|
|
2007-11-16 02:21:33 +01:00
|
|
|
balance_default_route $balance , $gateway, $interface, $realm if $balance;
|
2007-11-18 02:48:54 +01:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
if ( $loose ) {
|
2007-09-21 18:55:28 +02:00
|
|
|
if ( $config{DELETE_THEN_ADD} ) {
|
|
|
|
emit ( "\nfind_interface_addresses $interface | while read address; do",
|
|
|
|
' qt ip rule del from $address',
|
|
|
|
'done'
|
|
|
|
);
|
|
|
|
}
|
2007-11-21 20:39:20 +01:00
|
|
|
} elsif ( $shared ) {
|
2007-11-21 22:30:21 +01:00
|
|
|
emit "qt ip rule del from $address" if $config{DELETE_THEN_ADD};
|
|
|
|
emit( "run_ip rule add from $address pref 20000 table $number" ,
|
|
|
|
"echo \"qt ip rule del from $address\" >> \${VARDIR}/undo_routing" );
|
2007-09-21 18:55:28 +02:00
|
|
|
} else {
|
2007-07-13 23:12:23 +02:00
|
|
|
my $rulebase = 20000 + ( 256 * ( $number - 1 ) );
|
2007-11-21 20:39:20 +01:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
emit "\nrulenum=0\n";
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-09-21 18:55:28 +02:00
|
|
|
emit ( "find_interface_addresses $interface | while read address; do" );
|
|
|
|
emit ( ' qt ip rule del from $address' ) if $config{DELETE_THEN_ADD};
|
|
|
|
emit ( " run_ip rule add from \$address pref \$(( $rulebase + \$rulenum )) table $number",
|
2007-07-13 23:12:23 +02:00
|
|
|
" echo \"qt ip rule del from \$address\" >> \${VARDIR}/undo_routing",
|
|
|
|
' rulenum=$(($rulenum + 1))',
|
|
|
|
'done'
|
|
|
|
);
|
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-09-21 18:55:28 +02:00
|
|
|
emit qq(\nprogress_message " Provider $table ($number) Added"\n);
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2008-02-09 23:39:23 +01:00
|
|
|
emit ( "${base}_IS_UP=Yes" ) if $optional;
|
2007-08-13 22:10:13 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
pop_indent;
|
|
|
|
emit 'else';
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
if ( $optional ) {
|
2007-08-13 22:10:13 +02:00
|
|
|
emit ( " error_message \"WARNING: Interface $interface is not configured -- Provider $table ($number) not Added\"",
|
2008-02-09 23:39:23 +01:00
|
|
|
" ${base}_IS_UP=" );
|
2007-07-13 23:12:23 +02:00
|
|
|
} else {
|
2007-08-09 17:16:08 +02:00
|
|
|
emit( " fatal_error \"Interface $interface is not configured -- Provider $table ($number) Cannot be Added\"" );
|
2007-07-13 23:12:23 +02:00
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
emit "fi\n";
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
sub add_an_rtrule( $$$$ ) {
|
|
|
|
my ( $source, $dest, $provider, $priority ) = @_;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
unless ( $providers{$provider} ) {
|
|
|
|
my $found = 0;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
if ( "\L$provider" =~ /^(0x[a-f0-9]+|0[0-7]*|[0-9]*)$/ ) {
|
|
|
|
my $provider_number = numeric_value $provider;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-08-26 17:12:04 +02:00
|
|
|
for ( keys %providers ) {
|
|
|
|
if ( $providers{$_}{number} == $provider_number ) {
|
|
|
|
$provider = $_;
|
2007-07-13 23:12:23 +02:00
|
|
|
$found = 1;
|
|
|
|
last;
|
2007-03-15 02:38:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
fatal_error "Unknown provider ($provider)" unless $found;
|
2007-03-15 02:38:04 +01:00
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
fatal_error "You must specify either the source or destination in a route_rules entry" if $source eq '-' && $dest eq '-';
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-09-21 18:55:28 +02:00
|
|
|
if ( $dest eq '-' ) {
|
|
|
|
$dest = 'to ' . ALLIPv4;
|
|
|
|
} else {
|
|
|
|
validate_net( $dest, 0 );
|
|
|
|
$dest = "to $dest";
|
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
if ( $source eq '-' ) {
|
2007-09-21 18:55:28 +02:00
|
|
|
$source = 'from ' . ALLIPv4;
|
2007-07-13 23:12:23 +02:00
|
|
|
} elsif ( $source =~ /:/ ) {
|
|
|
|
( my $interface, $source , my $remainder ) = split( /:/, $source, 3 );
|
|
|
|
fatal_error "Invalid SOURCE" if defined $remainder;
|
2007-09-21 18:55:28 +02:00
|
|
|
validate_net ( $source, 0 );
|
2007-07-13 23:12:23 +02:00
|
|
|
$source = "iif $interface from $source";
|
|
|
|
} elsif ( $source =~ /\..*\..*/ ) {
|
2007-09-23 17:21:18 +02:00
|
|
|
validate_net ( $source, 0 );
|
2007-07-13 23:12:23 +02:00
|
|
|
$source = "from $source";
|
|
|
|
} else {
|
|
|
|
$source = "iif $source";
|
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
fatal_error "Invalid priority ($priority)" unless $priority && $priority =~ /^\d{1,5}$/;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
$priority = "priority $priority";
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-09-21 18:55:28 +02:00
|
|
|
emit ( "qt ip rule del $source $dest $priority" ) if $config{DELETE_THEN_ADD};
|
2007-08-13 22:10:13 +02:00
|
|
|
|
2008-02-10 00:51:28 +01:00
|
|
|
my ( $optional, $number ) = ( $providers{$provider}{optional} , $providers{$provider}{number} );
|
2007-08-13 22:10:13 +02:00
|
|
|
|
2008-02-10 00:51:28 +01:00
|
|
|
if ( $optional ) {
|
|
|
|
my $base = uc chain_base( $providers{$provider}{interface} );
|
|
|
|
emit ( '', "if [ -n \$${base}_IS_UP ]; then" );
|
|
|
|
push_indent;
|
|
|
|
}
|
2007-08-13 22:10:13 +02:00
|
|
|
|
|
|
|
emit ( "run_ip rule add $source $dest $priority table $number",
|
|
|
|
"echo \"qt ip rule del $source $dest $priority\" >> \${VARDIR}/undo_routing" );
|
|
|
|
|
|
|
|
pop_indent, emit ( "fi\n" ) if $optional;
|
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
progress_message " Routing rule \"$currentline\" $done";
|
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-13 23:12:23 +02:00
|
|
|
sub setup_providers() {
|
|
|
|
my $providers = 0;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
my $fn = open_file 'providers';
|
2007-03-29 19:02:13 +02:00
|
|
|
|
|
|
|
while ( read_a_line ) {
|
|
|
|
unless ( $providers ) {
|
2007-03-29 20:57:53 +02:00
|
|
|
progress_message2 "$doing $fn ...";
|
2007-05-04 17:12:29 +02:00
|
|
|
require_capability( 'MANGLE_ENABLED' , 'a non-empty providers file' , 's' );
|
2007-03-29 20:57:53 +02:00
|
|
|
|
2007-03-29 19:02:13 +02:00
|
|
|
emit "\nif [ -z \"\$NOROUTES\" ]; then";
|
|
|
|
|
|
|
|
push_indent;
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( '#',
|
2007-03-29 19:02:13 +02:00
|
|
|
'# Undo any changes made since the last time that we [re]started -- this will not restore the default route',
|
|
|
|
'#',
|
2007-08-13 22:10:13 +02:00
|
|
|
'undo_routing' );
|
|
|
|
|
|
|
|
unless ( $config{KEEP_RT_TABLES} ) {
|
|
|
|
emit (
|
|
|
|
'#',
|
|
|
|
'# Save current routing table database so that it can be restored later',
|
|
|
|
'#',
|
|
|
|
'cp /etc/iproute2/rt_tables ${VARDIR}/' );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
emit ( '#',
|
2007-03-29 19:02:13 +02:00
|
|
|
'# Capture the default route(s) if we don\'t have it (them) already.',
|
|
|
|
'#',
|
2007-07-22 15:44:06 +02:00
|
|
|
'[ -f ${VARDIR}/default_route ] || ip route list | grep -E \'^\s*(default |nexthop )\' > ${VARDIR}/default_route',
|
2007-03-29 19:02:13 +02:00
|
|
|
'#',
|
|
|
|
'# Initialize the file that holds \'undo\' commands',
|
|
|
|
'#',
|
|
|
|
'> ${VARDIR}/undo_routing' );
|
2007-04-08 16:42:26 +02:00
|
|
|
|
2007-03-29 19:02:13 +02:00
|
|
|
save_progress_message 'Adding Providers...';
|
|
|
|
|
|
|
|
emit 'DEFAULT_ROUTE=';
|
|
|
|
}
|
2007-04-08 16:42:26 +02:00
|
|
|
|
2007-04-01 17:38:05 +02:00
|
|
|
my ( $table, $number, $mark, $duplicate, $interface, $gateway, $options, $copy ) = split_line 6, 8, 'providers file';
|
2007-03-15 02:38:04 +01:00
|
|
|
|
|
|
|
add_a_provider( $table, $number, $mark, $duplicate, $interface, $gateway, $options, $copy );
|
|
|
|
|
|
|
|
push @providers, $table;
|
|
|
|
|
|
|
|
$providers++;
|
|
|
|
|
2007-07-07 18:34:38 +02:00
|
|
|
progress_message " Provider \"$currentline\" $done";
|
2007-03-15 02:38:04 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $providers ) {
|
|
|
|
if ( $balance ) {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( 'if [ -n "$DEFAULT_ROUTE" ]; then',
|
2007-03-26 04:53:51 +02:00
|
|
|
' run_ip route replace default scope global $DEFAULT_ROUTE',
|
|
|
|
" progress_message \"Default route '\$(echo \$DEFAULT_ROUTE | sed 's/\$\\s*//')' Added\"",
|
|
|
|
'else',
|
|
|
|
' error_message "WARNING: No Default route added (all \'balance\' providers are down)"',
|
|
|
|
' restore_default_route',
|
|
|
|
'fi',
|
|
|
|
'' );
|
2007-03-15 02:38:04 +01:00
|
|
|
} else {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( '#',
|
2007-03-27 01:17:46 +02:00
|
|
|
'# We don\'t have any \'balance\' providers so we restore any default route that we\'ve saved',
|
|
|
|
'#',
|
|
|
|
'restore_default_route' );
|
2007-03-15 02:38:04 +01:00
|
|
|
}
|
|
|
|
|
2007-08-13 22:10:13 +02:00
|
|
|
unless ( $config{KEEP_RT_TABLES} ) {
|
|
|
|
emit( 'if [ -w /etc/iproute2/rt_tables ]; then',
|
|
|
|
' cat > /etc/iproute2/rt_tables <<EOF' );
|
2007-08-10 19:37:02 +02:00
|
|
|
|
2007-08-13 22:10:13 +02:00
|
|
|
push_indent;
|
2007-03-27 06:02:58 +02:00
|
|
|
|
2007-08-13 22:10:13 +02:00
|
|
|
emit_unindented join( "\n",
|
|
|
|
'#',
|
|
|
|
'# reserved values',
|
|
|
|
'#',
|
|
|
|
"255\tlocal",
|
|
|
|
"254\tmain",
|
|
|
|
"253\tdefault",
|
|
|
|
"0\tunspec",
|
|
|
|
'#',
|
|
|
|
'# local',
|
|
|
|
'#',
|
|
|
|
"EOF\n" );
|
|
|
|
|
|
|
|
emit "echocommand=\$(find_echo)\n";
|
|
|
|
|
|
|
|
for my $table ( @providers ) {
|
|
|
|
emit "\$echocommand \"$providers{$table}{number}\\t$table\" >> /etc/iproute2/rt_tables";
|
|
|
|
}
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-08-13 22:10:13 +02:00
|
|
|
pop_indent;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-08-13 22:10:13 +02:00
|
|
|
emit "fi\n";
|
2007-03-15 02:38:04 +01:00
|
|
|
}
|
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
my $fn = open_file 'route_rules';
|
2007-03-29 19:02:13 +02:00
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
if ( $fn ) {
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-11-16 00:24:54 +01:00
|
|
|
first_entry "$doing $fn...";
|
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
emit '';
|
2007-03-29 19:02:13 +02:00
|
|
|
|
|
|
|
while ( read_a_line ) {
|
2007-03-15 02:38:04 +01:00
|
|
|
|
2007-04-01 17:38:05 +02:00
|
|
|
my ( $source, $dest, $provider, $priority ) = split_line 4, 4, 'route_rules file';
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 02:38:04 +01:00
|
|
|
add_an_rtrule( $source, $dest, $provider , $priority );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-29 19:02:13 +02:00
|
|
|
emit "\nrun_ip route flush cache";
|
|
|
|
pop_indent;
|
|
|
|
emit "fi\n";
|
2007-04-08 16:42:26 +02:00
|
|
|
|
2007-03-29 19:02:13 +02:00
|
|
|
setup_route_marking if @routemarked_interfaces;
|
|
|
|
} else {
|
|
|
|
emit "\nundo_routing";
|
|
|
|
emit 'restore_default_route';
|
|
|
|
}
|
2007-03-15 02:38:04 +01:00
|
|
|
}
|
|
|
|
|
2007-11-16 02:21:33 +01:00
|
|
|
sub lookup_provider( $ ) {
|
|
|
|
my $provider = $_[0];
|
|
|
|
my $providerref = $providers{ $provider };
|
|
|
|
|
2008-02-11 19:52:49 +01:00
|
|
|
unless ( $providerref ) {
|
|
|
|
fatal_error "Unknown provider ($provider)" unless $provider =~ /^(0x[a-f0-9]+|0[0-7]*|[0-9]*)$/;
|
|
|
|
|
|
|
|
my $provider_number = numeric_value $provider;
|
|
|
|
|
|
|
|
for ( keys %providers ) {
|
|
|
|
if ( $providers{$_}{number} == $provider_number ) {
|
|
|
|
$providerref = $providers{$_};
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fatal_error "Unknown provider ($provider)" unless $providerref;
|
|
|
|
}
|
|
|
|
|
2007-11-16 02:21:33 +01:00
|
|
|
|
2008-02-11 19:40:58 +01:00
|
|
|
$providerref->{shared} ? $providerref->{number} : 0;
|
2007-11-16 02:21:33 +01:00
|
|
|
}
|
|
|
|
|
2007-03-15 02:38:04 +01:00
|
|
|
1;
|