2010-03-04 22:11:33 +01:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
2009-02-22 18:30:14 +01:00
|
|
|
# This program is under GPL [http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt]
|
|
|
|
#
|
|
|
|
# (c) 1999-2009 - Tom Eastep (teastep@shorewall.net)
|
|
|
|
#
|
|
|
|
# Options are:
|
|
|
|
#
|
|
|
|
# -n Don't alter Routing
|
|
|
|
# -v and -q Standard Shorewall Verbosity control
|
2010-03-06 17:13:33 +01:00
|
|
|
# -t Timestamp progress messages
|
|
|
|
# -p Purge conntrack table
|
|
|
|
# -r Recover from failed start/restart
|
|
|
|
# -V <verbosity> Set verbosity level explicitly
|
|
|
|
# -R <restore> Overrides RESTOREFILE setting
|
2009-02-22 18:30:14 +01:00
|
|
|
#
|
|
|
|
# Commands are:
|
|
|
|
#
|
|
|
|
# start Starts the firewall
|
|
|
|
# refresh Refresh the firewall
|
|
|
|
# restart Restarts the firewall
|
|
|
|
# reload Reload the firewall
|
|
|
|
# clear Removes all firewall rules
|
|
|
|
# stop Stops the firewall
|
|
|
|
# status Displays firewall status
|
|
|
|
# version Displays the version of Shorewall that
|
|
|
|
# generated this program
|
|
|
|
#
|
|
|
|
################################################################################
|
2009-03-04 00:41:30 +01:00
|
|
|
# Functions imported from /usr/share/shorewall/prog.header6
|
2009-02-22 18:30:14 +01:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Conditionally produce message
|
|
|
|
#
|
|
|
|
progress_message() # $* = Message
|
|
|
|
{
|
|
|
|
local timestamp
|
|
|
|
timestamp=
|
|
|
|
|
2010-03-01 02:58:01 +01:00
|
|
|
if [ $VERBOSITY -gt 1 ]; then
|
2010-03-03 18:50:07 +01:00
|
|
|
[ -n "$g_timestamp" ] && timestamp="$(date +%H:%M:%S) "
|
2009-02-22 18:30:14 +01:00
|
|
|
echo "${timestamp}$@"
|
|
|
|
fi
|
|
|
|
|
2010-03-01 01:58:30 +01:00
|
|
|
if [ $LOG_VERBOSITY -gt 1 ]; then
|
2009-02-22 18:30:14 +01:00
|
|
|
timestamp="$(date +'%b %_d %T') "
|
|
|
|
echo "${timestamp}$@" >> $STARTUP_LOG
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
progress_message2() # $* = Message
|
|
|
|
{
|
|
|
|
local timestamp
|
|
|
|
timestamp=
|
|
|
|
|
2010-03-01 02:58:01 +01:00
|
|
|
if [ $VERBOSITY -gt 0 ]; then
|
2010-03-03 18:50:07 +01:00
|
|
|
[ -n "$g_timestamp" ] && timestamp="$(date +%H:%M:%S) "
|
2009-02-22 18:30:14 +01:00
|
|
|
echo "${timestamp}$@"
|
|
|
|
fi
|
|
|
|
|
2010-03-01 01:58:30 +01:00
|
|
|
if [ $LOG_VERBOSITY -gt 0 ]; then
|
2009-02-22 18:30:14 +01:00
|
|
|
timestamp="$(date +'%b %_d %T') "
|
|
|
|
echo "${timestamp}$@" >> $STARTUP_LOG
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
progress_message3() # $* = Message
|
|
|
|
{
|
|
|
|
local timestamp
|
|
|
|
timestamp=
|
|
|
|
|
2010-03-01 02:58:01 +01:00
|
|
|
if [ $VERBOSITY -ge 0 ]; then
|
2010-03-03 18:50:07 +01:00
|
|
|
[ -n "$g_timestamp" ] && timestamp="$(date +%H:%M:%S) "
|
2009-02-22 18:30:14 +01:00
|
|
|
echo "${timestamp}$@"
|
|
|
|
fi
|
|
|
|
|
2010-03-01 01:58:30 +01:00
|
|
|
if [ $LOG_VERBOSITY -ge 0 ]; then
|
2009-02-22 18:30:14 +01:00
|
|
|
timestamp="$(date +'%b %_d %T') "
|
|
|
|
echo "${timestamp}$@" >> $STARTUP_LOG
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set a standard chain's policy
|
|
|
|
#
|
|
|
|
setpolicy() # $1 = name of chain, $2 = policy
|
|
|
|
{
|
|
|
|
run_iptables -P $1 $2
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set a standard chain to enable established and related connections
|
|
|
|
#
|
|
|
|
setcontinue() # $1 = name of chain
|
|
|
|
{
|
|
|
|
run_iptables -A $1 -m state --state ESTABLISHED,RELATED -j ACCEPT
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Flush one of the Mangle table chains
|
|
|
|
#
|
|
|
|
flushmangle() # $1 = name of chain
|
|
|
|
{
|
|
|
|
run_iptables -t mangle -F $1
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Flush and delete all user-defined chains in the filter table
|
|
|
|
#
|
|
|
|
deleteallchains() {
|
|
|
|
run_iptables -F
|
|
|
|
run_iptables -X
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Find the value 'dev' in the passed arguments then echo the next value
|
|
|
|
#
|
|
|
|
|
|
|
|
find_device() {
|
|
|
|
while [ $# -gt 1 ]; do
|
|
|
|
[ "x$1" = xdev ] && echo $2 && return
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Find the value 'via' in the passed arguments then echo the next value
|
|
|
|
#
|
|
|
|
|
|
|
|
find_gateway() {
|
|
|
|
while [ $# -gt 1 ]; do
|
|
|
|
[ "x$1" = xvia ] && echo $2 && return
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Find the value 'mtu' in the passed arguments then echo the next value
|
|
|
|
#
|
|
|
|
|
|
|
|
find_mtu() {
|
|
|
|
while [ $# -gt 1 ]; do
|
|
|
|
[ "x$1" = xmtu ] && echo $2 && return
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Find the value 'peer' in the passed arguments then echo the next value up to
|
|
|
|
# "/"
|
|
|
|
#
|
|
|
|
|
|
|
|
find_peer() {
|
|
|
|
while [ $# -gt 1 ]; do
|
|
|
|
[ "x$1" = xpeer ] && echo ${2%/*} && return
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Try to find the gateway through an interface looking for 'nexthop'
|
|
|
|
|
|
|
|
find_nexthop() # $1 = interface
|
|
|
|
{
|
2009-04-18 18:28:25 +02:00
|
|
|
echo $(find_gateway `$IP -6 route list | grep "[[:space:]]nexthop.* $1"`)
|
2009-02-22 18:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Find the default route's interface
|
|
|
|
#
|
|
|
|
find_default_interface() {
|
2009-04-18 18:28:25 +02:00
|
|
|
$IP -6 route list | while read first rest; do
|
2009-02-22 18:30:14 +01:00
|
|
|
[ "$first" = default ] && echo $(find_device $rest) && return
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Determine if Interface is up
|
|
|
|
#
|
|
|
|
interface_is_up() {
|
2009-04-18 18:28:25 +02:00
|
|
|
[ -n "$($IP link list dev $1 2> /dev/null | grep -e '[<,]UP[,>]')" ]
|
2009-02-22 18:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Determine if interface is usable from a Netfilter prespective
|
|
|
|
#
|
|
|
|
interface_is_usable() # $1 = interface
|
|
|
|
{
|
2010-01-18 00:38:20 +01:00
|
|
|
[ "$1" = lo ] && return 0
|
2009-02-22 18:30:14 +01:00
|
|
|
interface_is_up $1 && [ "$(find_first_interface_address_if_any $1)" != :: ] && run_isusable_exit $1
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Find interface addresses--returns the set of addresses assigned to the passed
|
|
|
|
# device
|
|
|
|
#
|
|
|
|
find_interface_addresses() # $1 = interface
|
|
|
|
{
|
2009-04-18 18:28:25 +02:00
|
|
|
$IP -f inet6 addr show $1 2> /dev/null | grep 'inet6 2' | sed 's/\s*inet6 //;s/\/.*//;s/ peer.*//'
|
2009-02-22 18:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Get all interface addresses with VLSMs
|
|
|
|
#
|
|
|
|
|
|
|
|
find_interface_full_addresses() # $1 = interface
|
|
|
|
{
|
2009-04-18 18:28:25 +02:00
|
|
|
$IP -f inet6 addr show $1 2> /dev/null | grep 'inet6 ' | sed 's/\s*inet6 //;s/ scope.*//;s/ peer.*//'
|
2009-02-22 18:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# echo the list of networks routed out of a given interface
|
|
|
|
#
|
|
|
|
get_routed_networks() # $1 = interface name, $2-n = Fatal error message
|
|
|
|
{
|
|
|
|
local address
|
|
|
|
local rest
|
|
|
|
|
2009-04-18 18:28:25 +02:00
|
|
|
$IP -6 route show dev $1 2> /dev/null |
|
2009-02-22 18:30:14 +01:00
|
|
|
while read address rest; do
|
|
|
|
case "$address" in
|
|
|
|
default)
|
|
|
|
if [ $# -gt 1 ]; then
|
|
|
|
shift
|
|
|
|
fatal_error "$@"
|
|
|
|
else
|
|
|
|
echo "WARNING: default route ignored on interface $1" >&2
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
multicast|broadcast|prohibit|nat|throw|nexthop)
|
|
|
|
;;
|
|
|
|
2*)
|
|
|
|
[ "$address" = "${address%/*}" ] && address="${address}/128"
|
|
|
|
echo $address
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2009-03-02 04:25:16 +01:00
|
|
|
#
|
|
|
|
# Normalize an IPv6 Address by compressing out consecutive zero elements
|
|
|
|
#
|
2009-02-22 18:30:14 +01:00
|
|
|
normalize_address() # $1 = valid IPv6 Address
|
|
|
|
{
|
|
|
|
local address
|
|
|
|
address=$1
|
|
|
|
local j
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
case $address in
|
|
|
|
::*)
|
|
|
|
address=0$address
|
|
|
|
;;
|
|
|
|
*::*)
|
|
|
|
list_count $(split $address)
|
|
|
|
|
|
|
|
j=$?
|
|
|
|
|
|
|
|
if [ $j -eq 7 ]; then
|
|
|
|
address=${address%::*}:0:${address#*::}
|
|
|
|
elif [ $j -eq 8 ]; then
|
|
|
|
$address=${address%::*}:${address#*::}
|
|
|
|
break 2
|
|
|
|
else
|
|
|
|
address=${address%::*}:0::${address#*::}
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo $address
|
|
|
|
break 2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Reads correctly-formed and fully-qualified host and subnet addresses from STDIN. For each
|
|
|
|
# that defines a /120 or larger network, it sends to STDOUT:
|
|
|
|
#
|
|
|
|
# The corresponding subnet-router anycast address (all host address bits are zero)
|
|
|
|
# The corresponding anycast addresses defined by RFC 2526 (the last 128 addresses in the subnet)
|
|
|
|
#
|
|
|
|
convert_to_anycast() {
|
|
|
|
local address
|
|
|
|
local badress
|
|
|
|
local vlsm
|
|
|
|
local host
|
|
|
|
local o
|
|
|
|
local m
|
|
|
|
m=
|
|
|
|
local z
|
|
|
|
z=65535
|
|
|
|
local l
|
|
|
|
|
|
|
|
while read address; do
|
2009-08-20 23:32:15 +02:00
|
|
|
case $address in
|
2009-02-22 18:30:14 +01:00
|
|
|
2*|3*)
|
|
|
|
vlsm=${address#*/}
|
|
|
|
vlsm=${vlsm:=128}
|
|
|
|
|
|
|
|
if [ $vlsm -le 120 ]; then
|
|
|
|
#
|
|
|
|
# Defines a viable subnet -- first get the subnet-router anycast address
|
|
|
|
#
|
|
|
|
host=$((128 - $vlsm))
|
|
|
|
|
|
|
|
address=$(normalize_address ${address%/*})
|
|
|
|
|
|
|
|
while [ $host -ge 16 ]; do
|
|
|
|
address=${address%:*}
|
|
|
|
host=$(($host - 16))
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ $host -gt 0 ]; then
|
|
|
|
#
|
|
|
|
# VLSM is not a multiple of 16
|
|
|
|
#
|
|
|
|
host=$((16 - $host))
|
|
|
|
o=$((0x${address##*:}))
|
|
|
|
m=0
|
|
|
|
while [ $host -gt 0 ]; do
|
|
|
|
m=$((($m >> 1) | 0x8000))
|
|
|
|
z=$(($z >> 1))
|
|
|
|
host=$(($host - 1))
|
|
|
|
done
|
|
|
|
|
|
|
|
o=$(($o & $m))
|
|
|
|
|
|
|
|
badress=${address%:*}
|
|
|
|
|
|
|
|
address=$badress:$(printf %04x $o)
|
|
|
|
|
|
|
|
z=$(($o | $z))
|
|
|
|
|
|
|
|
if [ $vlsm -gt 112 ]; then
|
|
|
|
z=$(($z & 0xff80))
|
|
|
|
fi
|
|
|
|
|
|
|
|
badress=$badress:$(printf %04x $z)
|
|
|
|
else
|
|
|
|
badress=$address
|
|
|
|
fi
|
|
|
|
#
|
2009-08-20 23:32:15 +02:00
|
|
|
# Note: at this point $address and $badress are the same except possibly for
|
2009-02-22 18:30:14 +01:00
|
|
|
# the contents of the last half-word
|
|
|
|
#
|
|
|
|
list_count $(split $address)
|
|
|
|
|
|
|
|
l=$?
|
|
|
|
#
|
|
|
|
# Now generate the anycast addresses defined by RFC 2526
|
|
|
|
#
|
|
|
|
if [ $l -lt 8 ]; then
|
|
|
|
#
|
|
|
|
# The subnet-router address
|
|
|
|
#
|
|
|
|
echo $address::
|
|
|
|
|
|
|
|
while [ $l -lt 8 ]; do
|
|
|
|
badress=$badress:ffff
|
|
|
|
l=$(($l + 1 ))
|
|
|
|
done
|
|
|
|
else
|
|
|
|
#
|
|
|
|
# The subnet-router address
|
|
|
|
#
|
|
|
|
echo $address
|
|
|
|
fi
|
|
|
|
#
|
|
|
|
# And the RFC 2526 addresses
|
|
|
|
#
|
|
|
|
echo $badress/121
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Generate a list of anycast addresses for a given interface
|
2009-08-20 23:32:15 +02:00
|
|
|
#
|
2009-02-22 18:30:14 +01:00
|
|
|
|
|
|
|
get_interface_acasts() # $1 = interface
|
|
|
|
{
|
|
|
|
local addresses
|
|
|
|
addresses=
|
|
|
|
|
|
|
|
find_interface_full_addresses $1 | convert_to_anycast | sort -u
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Get a list of all configured anycast addresses on the system
|
|
|
|
#
|
|
|
|
get_all_acasts()
|
|
|
|
{
|
|
|
|
find_interface_full_addresses | convert_to_anycast | sort -u
|
|
|
|
}
|
|
|
|
|
2009-03-02 04:25:16 +01:00
|
|
|
#
|
|
|
|
# Detect the gateway through an interface
|
|
|
|
#
|
2009-02-22 18:30:14 +01:00
|
|
|
detect_gateway() # $1 = interface
|
|
|
|
{
|
|
|
|
local interface
|
|
|
|
interface=$1
|
|
|
|
#
|
|
|
|
# First assume that this is some sort of point-to-point interface
|
|
|
|
#
|
2009-04-18 18:28:25 +02:00
|
|
|
gateway=$( find_peer $($IP -6 addr list $interface ) )
|
2009-02-22 18:30:14 +01:00
|
|
|
#
|
|
|
|
# Maybe there's a default route through this gateway already
|
|
|
|
#
|
2009-04-18 18:28:25 +02:00
|
|
|
[ -n "$gateway" ] || gateway=$(find_gateway $($IP -6 route list dev $interface | grep '^default'))
|
2009-02-22 18:30:14 +01:00
|
|
|
#
|
|
|
|
# Last hope -- is there a load-balancing route through the interface?
|
|
|
|
#
|
|
|
|
[ -n "$gateway" ] || gateway=$(find_nexthop $interface)
|
|
|
|
#
|
|
|
|
# Be sure we found one
|
|
|
|
#
|
|
|
|
[ -n "$gateway" ] && echo $gateway
|
|
|
|
}
|
|
|
|
|
|
|
|
delete_tc1()
|
|
|
|
{
|
|
|
|
clear_one_tc() {
|
2009-04-18 18:28:25 +02:00
|
|
|
$TC qdisc del dev $1 root 2> /dev/null
|
|
|
|
$TC qdisc del dev $1 ingress 2> /dev/null
|
2009-02-22 18:30:14 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
run_tcclear_exit
|
|
|
|
|
|
|
|
run_ip link list | \
|
|
|
|
while read inx interface details; do
|
|
|
|
case $inx in
|
|
|
|
[0-9]*)
|
|
|
|
clear_one_tc ${interface%:}
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Detect a device's MTU -- echos the passed device's MTU
|
|
|
|
#
|
|
|
|
get_device_mtu() # $1 = device
|
|
|
|
{
|
|
|
|
local output
|
2009-04-18 18:28:25 +02:00
|
|
|
output="$($IP link list dev $1 2> /dev/null)" # quotes required for /bin/ash
|
2009-02-22 18:30:14 +01:00
|
|
|
|
|
|
|
if [ -n "$output" ]; then
|
|
|
|
echo $(find_mtu $output)
|
|
|
|
else
|
|
|
|
echo 1500
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Version of the above that doesn't generate any output for MTU 1500.
|
|
|
|
# Generates 'mtu <mtu+>' otherwise, where <mtu+> is the device's MTU + 100
|
|
|
|
#
|
|
|
|
get_device_mtu1() # $1 = device
|
|
|
|
{
|
|
|
|
local output
|
2009-04-18 18:28:25 +02:00
|
|
|
output="$($IP link list dev $1 2> /dev/null)" # quotes required for /bin/ash
|
2009-02-22 18:30:14 +01:00
|
|
|
local mtu
|
|
|
|
|
|
|
|
if [ -n "$output" ]; then
|
|
|
|
mtu=$(find_mtu $output)
|
|
|
|
if [ -n "$mtu" ]; then
|
|
|
|
[ $mtu = 1500 ] || echo mtu $(($mtu + 100))
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Undo changes to routing
|
|
|
|
#
|
|
|
|
undo_routing() {
|
|
|
|
|
2010-03-03 18:50:07 +01:00
|
|
|
if [ -z "$g_noroutes" ]; then
|
2009-02-22 18:30:14 +01:00
|
|
|
#
|
|
|
|
# Restore rt_tables database
|
|
|
|
#
|
|
|
|
if [ -f ${VARDIR}/rt_tables ]; then
|
|
|
|
[ -w /etc/iproute2/rt_table -a -z "$KEEP_RT_TABLES" ] && cp -f ${VARDIR}/rt_tables /etc/iproute2/ && progress_message "/etc/iproute2/rt_tables database restored"
|
|
|
|
rm -f ${VARDIR}/rt_tables
|
|
|
|
fi
|
|
|
|
#
|
|
|
|
# Restore the rest of the routing table
|
|
|
|
#
|
|
|
|
if [ -f ${VARDIR}/undo_routing ]; then
|
|
|
|
. ${VARDIR}/undo_routing
|
|
|
|
progress_message "Shorewall-generated routing tables and routing rules removed"
|
|
|
|
rm -f ${VARDIR}/undo_routing
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-03-02 04:25:16 +01:00
|
|
|
#
|
|
|
|
# Restore the default route that was in place before the initial 'shorewall start'
|
|
|
|
#
|
2009-02-22 18:30:14 +01:00
|
|
|
restore_default_route() {
|
2010-03-03 18:50:07 +01:00
|
|
|
if [ -z "$g_noroutes" -a -f ${VARDIR}/default_route ]; then
|
2009-02-22 18:30:14 +01:00
|
|
|
local default_route
|
|
|
|
default_route=
|
|
|
|
local route
|
|
|
|
local result
|
|
|
|
result=1
|
|
|
|
|
|
|
|
while read route ; do
|
|
|
|
case $route in
|
|
|
|
default)
|
|
|
|
if [ -n "$default_route" ]; then
|
|
|
|
case "$default_route" in
|
|
|
|
*metric*)
|
|
|
|
#
|
|
|
|
# Don't restore a route with a metric -- we only replace the one with metric == 0
|
|
|
|
#
|
2009-04-18 18:28:25 +02:00
|
|
|
qt $IP -6 route delete default metric 0 && \
|
2009-02-22 18:30:14 +01:00
|
|
|
progress_message "Default Route with metric 0 deleted"
|
|
|
|
;;
|
|
|
|
*)
|
2009-04-18 18:28:25 +02:00
|
|
|
qt $IP -6 route replace $default_route && \
|
2009-02-22 18:30:14 +01:00
|
|
|
result=0 && \
|
|
|
|
progress_message "Default Route (${default_route# }) restored"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
|
|
|
|
default_route="$default_route $route"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
default_route="$default_route $route"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done < ${VARDIR}/default_route
|
|
|
|
|
|
|
|
rm -f ${VARDIR}/default_route
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $result
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Determine how to do "echo -e"
|
|
|
|
#
|
|
|
|
|
|
|
|
find_echo() {
|
|
|
|
local result
|
|
|
|
|
|
|
|
result=$(echo "a\tb")
|
|
|
|
[ ${#result} -eq 3 ] && { echo echo; return; }
|
|
|
|
|
|
|
|
result=$(echo -e "a\tb")
|
|
|
|
[ ${#result} -eq 3 ] && { echo "echo -e"; return; }
|
|
|
|
|
|
|
|
result=$(which echo)
|
|
|
|
[ -n "$result" ] && { echo "$result -e"; return; }
|
|
|
|
|
|
|
|
echo echo
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2010-03-03 18:50:07 +01:00
|
|
|
# Flush the conntrack table if $g_purge is non-empty
|
2009-02-22 18:30:14 +01:00
|
|
|
#
|
|
|
|
conditionally_flush_conntrack() {
|
|
|
|
|
2010-03-03 18:50:07 +01:00
|
|
|
if [ -n "$g_purge" ]; then
|
2009-02-22 18:30:14 +01:00
|
|
|
if [ -n $(which conntrack) ]; then
|
|
|
|
conntrack -F
|
|
|
|
else
|
|
|
|
error_message "WARNING: The '-p' option requires the conntrack utility which does not appear to be installed on this system"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-10-19 16:28:30 +02:00
|
|
|
#
|
|
|
|
# Remove all Shorewall-added rules
|
|
|
|
#
|
|
|
|
clear_firewall() {
|
|
|
|
stop_firewall
|
|
|
|
|
|
|
|
setpolicy INPUT ACCEPT
|
|
|
|
setpolicy FORWARD ACCEPT
|
|
|
|
setpolicy OUTPUT ACCEPT
|
|
|
|
|
|
|
|
run_iptables -F
|
|
|
|
|
|
|
|
echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
|
|
|
|
|
|
|
|
run_clear_exit
|
|
|
|
|
|
|
|
set_state "Cleared"
|
|
|
|
|
2010-03-02 21:34:36 +01:00
|
|
|
logger -p kern.info "$g_product Cleared"
|
2009-10-19 16:28:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Issue a message and stop/restore the firewall
|
|
|
|
#
|
|
|
|
fatal_error()
|
|
|
|
{
|
|
|
|
echo " ERROR: $@" >&2
|
|
|
|
|
2010-03-01 01:58:30 +01:00
|
|
|
if [ $LOG_VERBOSITY -gt 1 ]; then
|
2009-10-19 16:28:30 +02:00
|
|
|
timestamp="$(date +'%_b %d %T') "
|
|
|
|
echo "${timestamp} ERROR: $@" >> $STARTUP_LOG
|
|
|
|
fi
|
|
|
|
|
|
|
|
stop_firewall
|
|
|
|
[ -n "$TEMPFILE" ] && rm -f $TEMPFILE
|
|
|
|
exit 2
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Issue a message and stop
|
|
|
|
#
|
|
|
|
startup_error() # $* = Error Message
|
|
|
|
{
|
|
|
|
echo " ERROR: $@: Firewall state not changed" >&2
|
|
|
|
case $COMMAND in
|
|
|
|
start)
|
2010-03-02 21:34:36 +01:00
|
|
|
logger -p kern.err "ERROR:$g_product start failed:Firewall state not changed"
|
2009-10-19 16:28:30 +02:00
|
|
|
;;
|
|
|
|
restart)
|
2010-03-02 21:34:36 +01:00
|
|
|
logger -p kern.err "ERROR:$g_product restart failed:Firewall state not changed"
|
2009-10-19 16:28:30 +02:00
|
|
|
;;
|
|
|
|
restore)
|
2010-03-02 21:34:36 +01:00
|
|
|
logger -p kern.err "ERROR:$g_product restore failed:Firewall state not changed"
|
2009-10-19 16:28:30 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2010-03-01 01:58:30 +01:00
|
|
|
if [ $LOG_VERBOSITY -gt 1 ]; then
|
2009-10-19 16:28:30 +02:00
|
|
|
timestamp="$(date +'%_b %d %T') "
|
|
|
|
|
|
|
|
case $COMMAND in
|
|
|
|
start)
|
2010-03-02 21:34:36 +01:00
|
|
|
echo "${timestamp} ERROR:$g_product start failed:Firewall state not changed" >> $STARTUP_LOG
|
2009-10-19 16:28:30 +02:00
|
|
|
;;
|
|
|
|
restart)
|
2010-03-02 21:34:36 +01:00
|
|
|
echo "${timestamp} ERROR:$g_product restart failed:Firewall state not changed" >> $STARTUP_LOG
|
2009-10-19 16:28:30 +02:00
|
|
|
;;
|
|
|
|
restore)
|
2010-03-02 21:34:36 +01:00
|
|
|
echo "${timestamp} ERROR:$g_product restore failed:Firewall state not changed" >> $STARTUP_LOG
|
2009-10-19 16:28:30 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
|
|
|
kill $$
|
|
|
|
exit 2
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Run iptables and if an error occurs, stop/restore the firewall
|
|
|
|
#
|
|
|
|
run_iptables()
|
|
|
|
{
|
|
|
|
local status
|
|
|
|
|
|
|
|
while [ 1 ]; do
|
|
|
|
$IP6TABLES $@
|
|
|
|
status=$?
|
|
|
|
[ $status -ne 4 ] && break
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ $status -ne 0 ]; then
|
|
|
|
error_message "ERROR: Command \"$IP6TABLES $@\" Failed"
|
|
|
|
stop_firewall
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Run iptables retrying exit status 4
|
|
|
|
#
|
|
|
|
do_iptables()
|
|
|
|
{
|
|
|
|
local status
|
|
|
|
|
|
|
|
while [ 1 ]; do
|
|
|
|
$IP6TABLES $@
|
|
|
|
status=$?
|
|
|
|
[ $status -ne 4 ] && return $status;
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Run iptables and if an error occurs, stop/restore the firewall
|
|
|
|
#
|
|
|
|
run_ip()
|
|
|
|
{
|
|
|
|
if ! $IP -6 $@; then
|
|
|
|
error_message "ERROR: Command \"$IP -6 $@\" Failed"
|
|
|
|
stop_firewall
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Run tc and if an error occurs, stop/restore the firewall
|
|
|
|
#
|
|
|
|
run_tc() {
|
|
|
|
if ! $TC $@ ; then
|
|
|
|
error_message "ERROR: Command \"$TC $@\" Failed"
|
|
|
|
stop_firewall
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Restore the rules generated by 'drop','reject','logdrop', etc.
|
|
|
|
#
|
|
|
|
restore_dynamic_rules() {
|
|
|
|
if [ -f ${VARDIR}/save ]; then
|
|
|
|
progress_message2 "Setting up dynamic rules..."
|
|
|
|
rangematch='source IP range'
|
|
|
|
while read target ignore1 ignore2 address ignore3 rest; do
|
|
|
|
case $target in
|
|
|
|
DROP|reject|logdrop|logreject)
|
|
|
|
case $rest in
|
|
|
|
$rangematch*)
|
|
|
|
run_iptables -A dynamic -m iprange --src-range ${rest#source IP range} -j $target
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
if [ -z "$rest" ]; then
|
|
|
|
run_iptables -A dynamic -s $address -j $target
|
|
|
|
else
|
|
|
|
error_message "WARNING: Unable to restore dynamic rule \"$target $ignore1 $ignore2 $address $ignore3 $rest\""
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done < ${VARDIR}/save
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Run the .iptables_restore_input as a set of discrete iptables commands
|
|
|
|
#
|
|
|
|
debug_restore_input() {
|
|
|
|
local first second rest table chain
|
|
|
|
#
|
|
|
|
# Clear the ruleset
|
|
|
|
#
|
|
|
|
qt1 $IP6TABLES -t mangle -F
|
|
|
|
qt1 $IP6TABLES -t mangle -X
|
|
|
|
|
|
|
|
for chain in PREROUTING INPUT FORWARD POSTROUTING; do
|
|
|
|
qt1 $IP6TABLES -t mangle -P $chain ACCEPT
|
|
|
|
done
|
|
|
|
|
|
|
|
qt1 $IP6TABLES -t raw -F
|
|
|
|
qt1 $IP6TABLES -t raw -X
|
|
|
|
|
|
|
|
for chain in PREROUTING OUTPUT; do
|
|
|
|
qt1 $IP6TABLES -t raw -P $chain ACCEPT
|
|
|
|
done
|
|
|
|
|
|
|
|
qt1 $IP6TABLES -t filter -F
|
|
|
|
qt1 $IP6TABLES -t filter -X
|
|
|
|
|
|
|
|
for chain in INPUT FORWARD OUTPUT; do
|
|
|
|
qt1 $IP6TABLES -t filter -P $chain -P ACCEPT
|
|
|
|
done
|
|
|
|
|
|
|
|
while read first second rest; do
|
|
|
|
case $first in
|
|
|
|
-*)
|
|
|
|
#
|
|
|
|
# We can't call run_iptables() here because the rules may contain quoted strings
|
|
|
|
#
|
|
|
|
eval $IP6TABLES -t $table $first $second $rest
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
error_message "ERROR: Command \"$IP6TABLES $first $second $rest\" Failed"
|
|
|
|
stop_firewall
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
:*)
|
|
|
|
chain=${first#:}
|
|
|
|
|
|
|
|
if [ "x$second" = x- ]; then
|
|
|
|
do_iptables -t $table -N $chain
|
|
|
|
else
|
|
|
|
do_iptables -t $table -P $chain $second
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
error_message "ERROR: Command \"$IP6TABLES $first $second $rest\" Failed"
|
|
|
|
stop_firewall
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
#
|
|
|
|
# This grotesque hack with the table names works around a bug/feature with ash
|
|
|
|
#
|
|
|
|
'*'raw)
|
|
|
|
table=raw
|
|
|
|
;;
|
|
|
|
'*'mangle)
|
|
|
|
table=mangle
|
|
|
|
;;
|
|
|
|
'*'nat)
|
|
|
|
table=nat
|
|
|
|
;;
|
|
|
|
'*'filter)
|
|
|
|
table=filter
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2009-02-22 18:30:14 +01:00
|
|
|
################################################################################
|
2009-03-04 00:41:30 +01:00
|
|
|
# End of functions imported from /usr/share/shorewall/prog.header6
|
2009-02-22 18:30:14 +01:00
|
|
|
################################################################################
|