2010-03-04 21:38:02 +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]
|
|
|
|
#
|
2010-03-18 19:13:51 +01:00
|
|
|
# (c) 1999-2010 - Tom Eastep (teastep@shorewall.net)
|
2009-02-22 18:30:14 +01:00
|
|
|
#
|
|
|
|
# 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-02 04:25:16 +01:00
|
|
|
# Functions imported from /usr/share/shorewall/prog.header
|
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
|
|
|
|
}
|
|
|
|
|
2010-09-14 22:50:22 +02:00
|
|
|
#
|
|
|
|
# Set a standard chain's policy
|
|
|
|
#
|
|
|
|
setpolicy() # $1 = name of chain, $2 = policy
|
|
|
|
{
|
|
|
|
run_iptables -P $1 $2
|
|
|
|
}
|
|
|
|
|
2010-06-04 19:35:45 +02:00
|
|
|
#
|
|
|
|
# Generate a list of all network interfaces on the system
|
|
|
|
#
|
|
|
|
find_all_interfaces() {
|
2010-08-30 05:13:56 +02:00
|
|
|
${IP:-ip} link list | egrep '^[[:digit:]]+:' | cut -d ' ' -f2 | sed -r 's/(@.*)?:$//'
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Generate a list of all network interfaces on the system that have an ipv4 address
|
|
|
|
#
|
|
|
|
find_all_interfaces1() {
|
|
|
|
${IP:-ip} -4 addr list | egrep '^[[:digit:]]+:' | cut -d ' ' -f2 | sed -r 's/(@.*)?:$//'
|
2010-06-04 19:35:45 +02:00
|
|
|
}
|
|
|
|
|
2009-02-22 18:30:14 +01:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Find the interfaces that have a route to the passed address - the default
|
|
|
|
# route is not used.
|
|
|
|
#
|
|
|
|
|
|
|
|
find_rt_interface() {
|
2009-04-18 18:28:25 +02:00
|
|
|
$IP -4 route list | while read addr rest; do
|
2009-02-22 18:30:14 +01:00
|
|
|
case $addr in
|
|
|
|
*/*)
|
|
|
|
in_network ${1%/*} $addr && echo $(find_device $rest)
|
|
|
|
;;
|
|
|
|
default)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
if [ "$addr" = "$1" -o "$addr/32" = "$1" ]; then
|
|
|
|
echo $(find_device $rest)
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
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 -4 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 -4 route list | while read first rest; do
|
2009-02-22 18:30:14 +01:00
|
|
|
[ "$first" = default ] && echo $(find_device $rest) && return
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Echo the name of the interface(s) that will be used to send to the
|
|
|
|
# passed address
|
|
|
|
#
|
|
|
|
|
|
|
|
find_interface_by_address() {
|
|
|
|
local dev
|
|
|
|
dev="$(find_rt_interface $1)"
|
|
|
|
local first
|
|
|
|
local rest
|
|
|
|
|
|
|
|
[ -z "$dev" ] && dev=$(find_default_interface)
|
|
|
|
|
|
|
|
[ -n "$dev" ] && echo $dev
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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)" != 0.0.0.0 ] && 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 inet addr show $1 2> /dev/null | grep inet\ | sed 's/\s*inet //;s/\/.*//;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 -4 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)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
[ "$address" = "${address%/*}" ] && address="${address}/32"
|
|
|
|
echo $address
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2009-03-02 04:25:16 +01:00
|
|
|
#
|
|
|
|
# Get the broadcast addresses associated with an interface
|
|
|
|
#
|
2009-02-22 18:30:14 +01:00
|
|
|
get_interface_bcasts() # $1 = interface
|
|
|
|
{
|
|
|
|
local addresses
|
|
|
|
addresses=
|
|
|
|
|
2009-04-18 18:28:25 +02:00
|
|
|
$IP -f inet addr show dev $1 2> /dev/null | grep 'inet.*brd' | sed 's/inet.*brd //; s/scope.*//;' | sort -u
|
2009-02-22 18:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Delete IP address
|
|
|
|
#
|
|
|
|
del_ip_addr() # $1 = address, $2 = interface
|
|
|
|
{
|
2009-04-18 18:28:25 +02:00
|
|
|
[ $(find_first_interface_address_if_any $2) = $1 ] || qt $IP addr del $1 dev $2
|
2009-02-22 18:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Add IP Aliases
|
|
|
|
#
|
|
|
|
add_ip_aliases() # $* = List of addresses
|
|
|
|
{
|
|
|
|
local local
|
|
|
|
local addresses
|
|
|
|
local external
|
|
|
|
local interface
|
|
|
|
local inet
|
|
|
|
local cidr
|
|
|
|
local rest
|
|
|
|
local val
|
|
|
|
local arping
|
|
|
|
arping=$(mywhich arping)
|
|
|
|
|
|
|
|
address_details()
|
|
|
|
{
|
|
|
|
#
|
|
|
|
# Folks feel uneasy if they don't see all of the same
|
|
|
|
# decoration on these IP addresses that they see when their
|
|
|
|
# distro's net config tool adds them. In an attempt to reduce
|
|
|
|
# the anxiety level, we have the following code which sets
|
|
|
|
# the VLSM and BRD from an existing address in the same networks
|
|
|
|
#
|
|
|
|
# Get all of the lines that contain inet addresses with broadcast
|
|
|
|
#
|
2009-04-18 18:28:25 +02:00
|
|
|
$IP -f inet addr show $interface 2> /dev/null | grep 'inet.*brd' | while read inet cidr rest ; do
|
2009-02-22 18:30:14 +01:00
|
|
|
case $cidr in
|
|
|
|
*/*)
|
|
|
|
if in_network $external $cidr; then
|
|
|
|
echo "/${cidr#*/} brd $(broadcastaddress $cidr)"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
do_one()
|
|
|
|
{
|
|
|
|
val=$(address_details)
|
|
|
|
|
2009-04-18 18:28:25 +02:00
|
|
|
$IP addr add ${external}${val} dev $interface $label
|
2009-02-22 18:30:14 +01:00
|
|
|
[ -n "$arping" ] && qt $arping -U -c 2 -I $interface $external
|
|
|
|
echo "$external $interface" >> $VARDIR/nat
|
|
|
|
[ -n "$label" ] && label="with $label"
|
|
|
|
progress_message " IP Address $external added to interface $interface $label"
|
|
|
|
}
|
|
|
|
|
|
|
|
progress_message "Adding IP Addresses..."
|
|
|
|
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
external=$1
|
|
|
|
interface=$2
|
|
|
|
label=
|
|
|
|
|
|
|
|
if [ "$interface" != "${interface%:*}" ]; then
|
|
|
|
label="${interface#*:}"
|
|
|
|
interface="${interface%:*}"
|
|
|
|
label="label $interface:$label"
|
|
|
|
fi
|
|
|
|
|
|
|
|
shift 2
|
|
|
|
|
|
|
|
list_search $external $(find_interface_addresses $interface) || do_one
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2009-03-02 04:25:16 +01:00
|
|
|
#
|
|
|
|
# Detect the gateway through a PPP or DHCP-configured interface
|
|
|
|
#
|
2009-02-22 18:30:14 +01:00
|
|
|
detect_dynamic_gateway() { # $1 = interface
|
|
|
|
local interface
|
|
|
|
interface=$1
|
|
|
|
local GATEWAYS
|
|
|
|
GATEWAYS=
|
|
|
|
local gateway
|
2009-05-11 23:35:20 +02:00
|
|
|
|
|
|
|
gateway=$(run_findgw_exit $1);
|
|
|
|
|
2009-06-04 22:03:56 +02:00
|
|
|
if [ -z "$gateway" ]; then
|
2009-05-11 23:35:20 +02:00
|
|
|
gateway=$( find_peer $($IP addr list $interface ) )
|
|
|
|
fi
|
|
|
|
|
2009-02-22 18:30:14 +01:00
|
|
|
if [ -z "$gateway" -a -f /var/lib/dhcpcd/dhcpcd-${1}.info ]; then
|
|
|
|
eval $(grep ^GATEWAYS= /var/lib/dhcpcd/dhcpcd-${1}.info 2> /dev/null)
|
|
|
|
[ -n "$GATEWAYS" ] && GATEWAYS=${GATEWAYS%,*} && gateway=$GATEWAYS
|
|
|
|
fi
|
|
|
|
|
2009-03-24 21:58:04 +01:00
|
|
|
if [ -z "$gateway" -a -f /var/lib/dhcp/dhclient-${1}.lease ]; then
|
2009-06-15 22:34:35 +02:00
|
|
|
gateway=$(grep 'option routers' /var/lib/dhcp/dhclient-${1}.lease | tail -n 1 | while read j1 j2 gateway; do echo $gateway ; return 0; done)
|
2009-03-24 21:58:04 +01:00
|
|
|
fi
|
|
|
|
|
2009-02-22 18:30:14 +01:00
|
|
|
[ -n "$gateway" ] && echo $gateway
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
local gateway
|
|
|
|
#
|
|
|
|
# First assume that this is some sort of dynamic interface
|
|
|
|
#
|
|
|
|
gateway=$( detect_dynamic_gateway $interface )
|
|
|
|
#
|
|
|
|
# Maybe there's a default route through this gateway already
|
|
|
|
#
|
2009-04-18 18:28:25 +02:00
|
|
|
[ -n "$gateway" ] || gateway=$(find_gateway $($IP -4 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
|
|
|
|
}
|
|
|
|
|
2009-07-07 03:23:23 +02:00
|
|
|
#
|
|
|
|
# Disable IPV6
|
|
|
|
#
|
|
|
|
disable_ipv6() {
|
|
|
|
local foo
|
|
|
|
foo="$($IP -f inet6 addr list 2> /dev/null)"
|
|
|
|
|
|
|
|
if [ -n "$foo" ]; then
|
2009-07-09 19:29:56 +02:00
|
|
|
if [ -x "$IP6TABLES" ]; then
|
|
|
|
$IP6TABLES -P FORWARD DROP
|
|
|
|
$IP6TABLES -P INPUT DROP
|
|
|
|
$IP6TABLES -P OUTPUT DROP
|
|
|
|
$IP6TABLES -F
|
|
|
|
$IP6TABLES -X
|
|
|
|
$IP6TABLES -A OUTPUT -o lo -j ACCEPT
|
|
|
|
$IP6TABLES -A INPUT -i lo -j ACCEPT
|
2009-07-07 03:23:23 +02:00
|
|
|
else
|
|
|
|
error_message "WARNING: DISABLE_IPV6=Yes in shorewall.conf but this system does not appear to have ip6tables"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-03-02 04:25:16 +01:00
|
|
|
#
|
|
|
|
# Clear the current traffic shaping configuration
|
|
|
|
#
|
|
|
|
|
2009-02-22 18:30:14 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-13 22:53:18 +02:00
|
|
|
#
|
|
|
|
# Save the default route
|
|
|
|
#
|
|
|
|
save_default_route() {
|
|
|
|
awk \
|
2011-05-07 18:21:37 +02:00
|
|
|
'BEGIN {defroute=0;};
|
|
|
|
/^default / {deroute=1; print; next};
|
|
|
|
/nexthop/ {if (defroute == 1 ) {print ; next} };
|
|
|
|
{ defroute=0; };'
|
2011-04-13 22:53:18 +02:00
|
|
|
}
|
|
|
|
|
2009-03-02 04:25:16 +01:00
|
|
|
#
|
|
|
|
# Restore the default route that was in place before the initial 'shorewall start'
|
|
|
|
#
|
2011-04-14 21:25:33 +02:00
|
|
|
replace_default_route() # $1 = USE_DEFAULT_RT
|
|
|
|
{
|
|
|
|
#
|
|
|
|
# default_route and result are inherited from the caller
|
|
|
|
#
|
|
|
|
if [ -n "$default_route" ]; then
|
|
|
|
case "$default_route" in
|
|
|
|
*metric*)
|
|
|
|
#
|
|
|
|
# Don't restore a default route with a metric unless USE_DEFAULT_RT=Yes. Otherwise, we only replace the one with metric 0
|
|
|
|
#
|
|
|
|
[ -n "$1" ] && qt $IP -4 route replace $default_route && progress_message "Default Route (${default_route# }) restored"
|
|
|
|
default_route=
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
qt $IP -4 route replace $default_route && progress_message "Default Route (${default_route# }) restored"
|
|
|
|
result=0
|
|
|
|
default_route=
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
restore_default_route() # $1 = USE_DEFAULT_RT
|
|
|
|
{
|
2010-09-12 19:07:26 +02:00
|
|
|
local result
|
2011-04-14 21:25:33 +02:00
|
|
|
result=1
|
2010-09-27 20:16:18 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
while read route ; do
|
|
|
|
case $route in
|
|
|
|
default*)
|
2011-04-14 21:25:33 +02:00
|
|
|
replace_default_route $1
|
2009-02-22 18:30:14 +01:00
|
|
|
default_route="$default_route $route"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
default_route="$default_route $route"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done < ${VARDIR}/default_route
|
|
|
|
|
2011-04-14 21:25:33 +02:00
|
|
|
replace_default_route $1
|
|
|
|
|
|
|
|
if [ $result = 1 ]; then
|
|
|
|
#
|
2011-04-15 16:09:34 +02:00
|
|
|
# We didn't restore a default route with metric 0
|
2011-04-14 21:25:33 +02:00
|
|
|
#
|
2011-04-15 20:15:32 +02:00
|
|
|
if $IP -4 -o route list 2> /dev/null | fgrep default | fgrep -qv metric; then
|
2011-04-15 16:09:34 +02:00
|
|
|
#
|
|
|
|
# But we added a default route with metric 0
|
|
|
|
#
|
|
|
|
qt $IP -4 route del default metric 0 && progress_message "Default route with metric 0 deleted"
|
|
|
|
fi
|
2011-04-14 21:25:33 +02:00
|
|
|
fi
|
|
|
|
|
2009-02-22 18:30:14 +01:00
|
|
|
rm -f ${VARDIR}/default_route
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $result
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Determine the MAC address of the passed IP through the passed interface
|
|
|
|
#
|
|
|
|
find_mac() # $1 = IP address, $2 = interface
|
|
|
|
{
|
|
|
|
if interface_is_usable $2 ; then
|
|
|
|
qt ping -nc 1 -t 2 -I $2 $1
|
|
|
|
|
|
|
|
local result
|
2009-04-18 18:28:25 +02:00
|
|
|
result=$($IP neigh list | awk "/^$1 / {print \$5}")
|
2009-02-22 18:30:14 +01:00
|
|
|
|
|
|
|
case $result in
|
|
|
|
\<*\>)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
[ -n "$result" ] && echo $result
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
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-06-17 00:39:04 +02:00
|
|
|
if [ -n $(mywhich conntrack) ]; then
|
2009-02-22 18:30:14 +01:00
|
|
|
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-18 17:47:20 +02:00
|
|
|
#
|
|
|
|
# Clear Proxy Arp
|
|
|
|
#
|
|
|
|
delete_proxyarp() {
|
|
|
|
if [ -f ${VARDIR}/proxyarp ]; then
|
|
|
|
while read address interface external haveroute; do
|
2010-12-11 04:06:44 +01:00
|
|
|
qt $IP -4 neigh del proxy $address dev $external
|
|
|
|
[ -z "${haveroute}${g_noroutes}" ] && qt $IP -4 route del $address/32 dev $interface
|
2009-10-18 17:47:20 +02:00
|
|
|
f=/proc/sys/net/ipv4/conf/$interface/proxy_arp
|
|
|
|
[ -f $f ] && echo 0 > $f
|
|
|
|
done < ${VARDIR}/proxyarp
|
|
|
|
|
2010-09-12 19:07:26 +02:00
|
|
|
rm -f ${VARDIR}/proxyarp
|
|
|
|
fi
|
2009-10-18 17:47:20 +02:00
|
|
|
}
|
|
|
|
|
2010-12-16 18:55:31 +01:00
|
|
|
#
|
2009-10-18 17:47:20 +02:00
|
|
|
# Remove all Shorewall-added rules
|
|
|
|
#
|
|
|
|
clear_firewall() {
|
|
|
|
stop_firewall
|
|
|
|
|
|
|
|
setpolicy INPUT ACCEPT
|
|
|
|
setpolicy FORWARD ACCEPT
|
|
|
|
setpolicy OUTPUT ACCEPT
|
|
|
|
|
|
|
|
run_iptables -F
|
2010-09-18 02:12:34 +02:00
|
|
|
qt $IPTABLES -t raw -F
|
2009-10-18 17:47:20 +02:00
|
|
|
|
|
|
|
echo 1 > /proc/sys/net/ipv4/ip_forward
|
|
|
|
|
|
|
|
if [ -n "$DISABLE_IPV6" ]; then
|
2010-01-09 16:28:57 +01:00
|
|
|
if [ -x $IP6TABLES ]; then
|
2009-10-18 17:47:20 +02:00
|
|
|
$IP6TABLES -P INPUT ACCEPT 2> /dev/null
|
|
|
|
$IP6TABLES -P OUTPUT ACCEPT 2> /dev/null
|
|
|
|
$IP6TABLES -P FORWARD ACCEPT 2> /dev/null
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
run_clear_exit
|
|
|
|
|
|
|
|
set_state "Cleared"
|
|
|
|
|
2010-03-02 21:34:36 +01:00
|
|
|
logger -p kern.info "$g_product Cleared"
|
2009-10-18 17:47:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Issue a message and stop/restore the firewall
|
|
|
|
#
|
|
|
|
fatal_error()
|
|
|
|
{
|
|
|
|
echo " ERROR: $@" >&2
|
|
|
|
|
2010-05-17 16:02:30 +02:00
|
|
|
if [ $LOG_VERBOSITY -ge 0 ]; then
|
2009-10-18 17:47:20 +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
|
2010-05-17 16:02:30 +02:00
|
|
|
|
|
|
|
if [ $LOG_VERBOSITY -ge 0 ]; then
|
|
|
|
timestamp="$(date +'%_b %d %T') "
|
|
|
|
echo "${timestamp} ERROR: $@" >> $STARTUP_LOG
|
|
|
|
fi
|
|
|
|
|
2009-10-18 17:47:20 +02:00
|
|
|
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-18 17:47:20 +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-18 17:47:20 +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-18 17:47:20 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2010-07-22 21:53:00 +02:00
|
|
|
if [ $LOG_VERBOSITY -ge 0 ]; then
|
2009-10-18 17:47:20 +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-18 17:47:20 +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-18 17:47:20 +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-18 17:47:20 +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
|
|
|
|
$IPTABLES $@
|
|
|
|
status=$?
|
|
|
|
[ $status -ne 4 ] && break
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ $status -ne 0 ]; then
|
|
|
|
error_message "ERROR: Command \"$IPTABLES $@\" Failed"
|
|
|
|
stop_firewall
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Run iptables retrying exit status 4
|
|
|
|
#
|
|
|
|
do_iptables()
|
|
|
|
{
|
|
|
|
local status
|
|
|
|
|
|
|
|
while [ 1 ]; do
|
|
|
|
$IPTABLES $@
|
|
|
|
status=$?
|
|
|
|
[ $status -ne 4 ] && return $status;
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Run iptables and if an error occurs, stop/restore the firewall
|
|
|
|
#
|
|
|
|
run_ip()
|
|
|
|
{
|
|
|
|
if ! $IP -4 $@; then
|
|
|
|
error_message "ERROR: Command \"$IP -4 $@\" 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
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Get a list of all configured broadcast addresses on the system
|
|
|
|
#
|
|
|
|
get_all_bcasts()
|
|
|
|
{
|
|
|
|
$IP -f inet addr show 2> /dev/null | grep 'inet.*brd' | grep -v '/32 ' | sed 's/inet.*brd //; s/scope.*//;' | sort -u
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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 $IPTABLES -t mangle -F
|
|
|
|
qt1 $IPTABLES -t mangle -X
|
|
|
|
|
|
|
|
for chain in PREROUTING INPUT FORWARD POSTROUTING; do
|
|
|
|
qt1 $IPTABLES -t mangle -P $chain ACCEPT
|
|
|
|
done
|
|
|
|
|
|
|
|
qt1 $IPTABLES -t raw -F
|
|
|
|
qt1 $IPTABLES -t raw -X
|
|
|
|
|
|
|
|
for chain in PREROUTING OUTPUT; do
|
|
|
|
qt1 $IPTABLES -t raw -P $chain ACCEPT
|
|
|
|
done
|
|
|
|
|
|
|
|
run_iptables -t nat -F
|
|
|
|
run_iptables -t nat -X
|
|
|
|
|
|
|
|
for chain in PREROUTING POSTROUTING OUTPUT; do
|
|
|
|
qt1 $IPTABLES -t nat -P $chain ACCEPT
|
|
|
|
done
|
|
|
|
|
|
|
|
qt1 $IPTABLES -t filter -F
|
|
|
|
qt1 $IPTABLES -t filter -X
|
|
|
|
|
|
|
|
for chain in INPUT FORWARD OUTPUT; do
|
|
|
|
qt1 $IPTABLES -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 $IPTABLES -t $table $first $second $rest
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
error_message "ERROR: Command \"$IPTABLES $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 \"$IPTABLES $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-02 04:25:16 +01:00
|
|
|
# End of functions in /usr/share/shorewall/prog.header
|
2009-02-22 18:30:14 +01:00
|
|
|
################################################################################
|