2017-09-19 18:03:00 +02:00
|
|
|
# (c) 1999-2017 - Tom Eastep (teastep@shorewall.net)
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2015-05-05 19:40:17 +02:00
|
|
|
# This program is part of Shorewall.
|
2014-01-04 18:48:27 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by the
|
2015-05-05 19:40:17 +02:00
|
|
|
# Free Software Foundation, either version 2 of the license or, at your
|
|
|
|
# option, any later version.
|
2014-01-04 18:48:27 +01:00
|
|
|
#
|
|
|
|
# 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, see <http://www.gnu.org/licenses/>.
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 23:56:02 +01:00
|
|
|
# Options are:
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2015-05-05 19:40:17 +02:00
|
|
|
# -n Do not alter Routing
|
|
|
|
# -v and -q Standard Shorewall Verbosity control
|
|
|
|
# -t Timestamp progress messages
|
|
|
|
# -p Purge conntrack table
|
2015-07-26 18:59:49 +02:00
|
|
|
# -r Recover from failed start/reload
|
2015-05-05 19:40:17 +02:00
|
|
|
# -V <verbosity> Set verbosity level explicitly
|
|
|
|
# -R <restore> Overrides RESTOREFILE setting
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 23:56:02 +01:00
|
|
|
# Commands are:
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2015-05-05 19:40:17 +02:00
|
|
|
# clear Removes all firewall rules
|
|
|
|
# disable Disable an optional interface
|
|
|
|
# down Stop an optional interface
|
|
|
|
# enable Enable an optional interface
|
|
|
|
# help Show command syntax
|
2017-04-19 01:31:51 +02:00
|
|
|
# reenable Disable then enable an optional
|
2015-05-05 19:40:17 +02:00
|
|
|
# interface
|
|
|
|
# refresh Refresh the firewall
|
|
|
|
# reload Reload the firewall
|
|
|
|
# restart Restarts the firewall
|
|
|
|
# restore Restore a saved configuration
|
|
|
|
# reset Reset byte and packet counters
|
|
|
|
# run Call a function in this program
|
|
|
|
# savesets Save the ipset contents
|
|
|
|
# status Displays firewall status
|
|
|
|
# start Starts the firewall
|
|
|
|
# stop Stops the firewall
|
|
|
|
# up Start an optional interface
|
|
|
|
# version Displays the version of Shorewall that
|
|
|
|
# generated this program
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 23:56:02 +01:00
|
|
|
################################################################################
|
2016-04-21 17:12:41 +02:00
|
|
|
# Functions imported from /usr/share/shorewall/lib.runtime
|
2012-03-19 23:56:02 +01:00
|
|
|
################################################################################
|
2015-05-05 19:40:17 +02:00
|
|
|
# Address family-neutral Functions
|
2012-03-20 15:17:19 +01:00
|
|
|
################################################################################
|
2012-03-18 16:48:26 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Conditionally produce message
|
2012-03-18 16:48:26 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
progress_message() # $* = Message
|
2012-01-03 22:06:00 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
local timestamp
|
|
|
|
timestamp=
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ $VERBOSITY -gt 1 ]; then
|
|
|
|
[ -n "$g_timestamp" ] && timestamp="$(date +%H:%M:%S) "
|
|
|
|
echo "${timestamp}$@"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $LOG_VERBOSITY -gt 1 ]; then
|
2015-10-08 22:16:32 +02:00
|
|
|
timestamp="$(date +'%b %e %T') "
|
2012-03-19 15:20:31 +01:00
|
|
|
echo "${timestamp}$@" >> $STARTUP_LOG
|
|
|
|
fi
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
progress_message2() # $* = Message
|
2012-01-03 22:06:00 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
local timestamp
|
|
|
|
timestamp=
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ $VERBOSITY -gt 0 ]; then
|
|
|
|
[ -n "$g_timestamp" ] && timestamp="$(date +%H:%M:%S) "
|
|
|
|
echo "${timestamp}$@"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $LOG_VERBOSITY -gt 0 ]; then
|
2015-10-08 22:16:32 +02:00
|
|
|
timestamp="$(date +'%b %e %T') "
|
2012-03-19 15:20:31 +01:00
|
|
|
echo "${timestamp}$@" >> $STARTUP_LOG
|
|
|
|
fi
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
progress_message3() # $* = Message
|
2012-03-18 16:48:26 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
local timestamp
|
|
|
|
timestamp=
|
|
|
|
|
|
|
|
if [ $VERBOSITY -ge 0 ]; then
|
|
|
|
[ -n "$g_timestamp" ] && timestamp="$(date +%H:%M:%S) "
|
|
|
|
echo "${timestamp}$@"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $LOG_VERBOSITY -ge 0 ]; then
|
2015-10-08 22:16:32 +02:00
|
|
|
timestamp="$(date +'%b %e %T') "
|
2012-03-19 15:20:31 +01:00
|
|
|
echo "${timestamp}$@" >> $STARTUP_LOG
|
|
|
|
fi
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Set a standard chain's policy
|
|
|
|
#
|
|
|
|
setpolicy() # $1 = name of chain, $2 = policy
|
2012-03-18 16:48:26 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
run_iptables -P $1 $2
|
|
|
|
}
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
# Generate a list of all network interfaces on the system
|
|
|
|
#
|
|
|
|
find_all_interfaces() {
|
|
|
|
${IP:-ip} link list | egrep '^[[:digit:]]+:' | cut -d ' ' -f2 | sed -r 's/(@.*)?:$//'
|
|
|
|
}
|
2012-03-18 16:48:26 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
# Generate a list of all network interfaces on the system that have an ipvX address
|
|
|
|
#
|
|
|
|
find_all_interfaces1() {
|
|
|
|
${IP:-ip} -$g_family addr list | egrep '^[[:digit:]]+:' | cut -d ' ' -f2 | sed -r 's/(@.*)?:$//'
|
|
|
|
}
|
2012-03-18 16:48:26 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
# Find the value 'dev' in the passed arguments then echo the next value
|
|
|
|
#
|
2012-03-18 16:48:26 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
find_device() {
|
|
|
|
while [ $# -gt 1 ]; do
|
|
|
|
[ "x$1" = xdev ] && echo $2 && return
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
# Find the value 'via' in the passed arguments then echo the next value
|
|
|
|
#
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
find_gateway() {
|
|
|
|
while [ $# -gt 1 ]; do
|
|
|
|
[ "x$1" = xvia ] && echo $2 && return
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
# 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
|
2012-01-03 22:06:00 +01:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Find the value 'peer' in the passed arguments then echo the next value up to
|
|
|
|
# "/"
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
find_peer() {
|
|
|
|
while [ $# -gt 1 ]; do
|
|
|
|
[ "x$1" = xpeer ] && echo ${2%/*} && return
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
# Try to find the gateway through an interface looking for 'nexthop'
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
find_nexthop() # $1 = interface
|
|
|
|
{
|
|
|
|
echo $(find_gateway `$IP -$g_family route list | grep "[[:space:]]nexthop.* $1"`)
|
|
|
|
}
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
# Find the default route's interface
|
|
|
|
#
|
|
|
|
find_default_interface() {
|
|
|
|
$IP -$g_family route list | while read first rest; do
|
|
|
|
[ "$first" = default ] && echo $(find_device $rest) && return
|
|
|
|
done
|
|
|
|
}
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
# Determine if Interface is up
|
|
|
|
#
|
|
|
|
interface_is_up() {
|
|
|
|
[ -n "$($IP -$g_family link list dev $1 2> /dev/null | grep -e '[<,]UP[,>]')" ]
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# echo the list of networks routed out of a given interface
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
get_routed_networks() # $1 = interface name, $2-n = Fatal error message
|
2012-01-03 22:06:00 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
local address
|
|
|
|
local rest
|
|
|
|
local mask
|
2012-03-18 16:48:26 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
[ $g_family -eq 4 ] && mask=32 || mask=128
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
$IP -$g_family route show dev $1 2> /dev/null |
|
|
|
|
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-3]*)
|
|
|
|
[ "$address" = "${address%/*}" ] && address="${address}/${mask}"
|
|
|
|
echo $address
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
if [ $g_family -eq 4 ]; then
|
|
|
|
[ "$address" = "${address%/*}" ] && address="${address}/${mask}"
|
|
|
|
echo $address
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Clear the current traffic shaping configuration
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
|
|
|
|
delete_tc1()
|
2012-01-03 22:06:00 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
clear_one_tc() {
|
2013-01-09 23:09:07 +01:00
|
|
|
$TC qdisc del dev ${1%@*} root 2> /dev/null
|
|
|
|
$TC qdisc del dev ${1%@*} ingress 2> /dev/null
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +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
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Detect a device's MTU -- echos the passed device's MTU
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
get_device_mtu() # $1 = device
|
2012-01-03 22:06:00 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
local output
|
|
|
|
output="$($IP link list dev $1 2> /dev/null)" # quotes required for /bin/ash
|
2012-03-18 16:48:26 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ -n "$output" ]; then
|
|
|
|
echo $(find_mtu $output)
|
|
|
|
else
|
|
|
|
echo 1500
|
2012-01-03 22:06:00 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# 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
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
get_device_mtu1() # $1 = device
|
|
|
|
{
|
|
|
|
local output
|
|
|
|
output="$($IP link list dev $1 2> /dev/null)" # quotes required for /bin/ash
|
|
|
|
local mtu
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ -n "$output" ]; then
|
|
|
|
mtu=$(find_mtu $output)
|
|
|
|
if [ -n "$mtu" ]; then
|
|
|
|
[ $mtu = 1500 ] || echo mtu $(($mtu + 100))
|
|
|
|
fi
|
2012-03-18 16:48:26 +01:00
|
|
|
fi
|
2012-03-19 15:20:31 +01:00
|
|
|
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Undo changes to routing
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
undo_routing() {
|
|
|
|
local undofiles
|
|
|
|
local f
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ -z "$g_noroutes" ]; then
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
undofiles="$(ls ${VARDIR}/undo_*routing 2> /dev/null)"
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ -n "$undofiles" ]; then
|
|
|
|
for f in $undofiles; do
|
|
|
|
. $f
|
|
|
|
done
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
rm -f $undofiles
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
progress_message "Shorewall-generated routing tables and routing rules removed"
|
2012-01-03 22:06:00 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Save the default route
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
save_default_route() {
|
|
|
|
awk \
|
|
|
|
'BEGIN {defroute=0;};
|
|
|
|
/^default / {defroute=1; print; next};
|
|
|
|
/nexthop/ {if (defroute == 1 ) {print ; next} };
|
|
|
|
{ defroute=0; };'
|
2012-03-18 16:48:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Restore the default route that was in place before the initial 'shorewall start'
|
2012-03-18 16:48:26 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
replace_default_route() # $1 = USE_DEFAULT_RT
|
2012-03-18 16:48:26 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
# default_route and result are inherited from the caller
|
|
|
|
#
|
|
|
|
if [ -n "$default_route" ]; then
|
|
|
|
case "$default_route" in
|
|
|
|
*metric*)
|
|
|
|
#
|
2017-01-14 21:40:39 +01:00
|
|
|
# Don't restore a default route with a metric unless USE_DEFAULT_RT=Yes or =Exact. Otherwise, we only replace the one with metric 0
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
[ -n "$1" ] && qt $IP -$g_family route replace $default_route && progress_message "Default Route (${default_route# }) restored"
|
|
|
|
default_route=
|
2012-01-03 22:06:00 +01:00
|
|
|
;;
|
|
|
|
*)
|
2012-03-19 15:20:31 +01:00
|
|
|
qt $IP -$g_family route replace $default_route && progress_message "Default Route (${default_route# }) restored"
|
|
|
|
result=0
|
|
|
|
default_route=
|
2012-01-03 22:06:00 +01:00
|
|
|
;;
|
|
|
|
esac
|
2012-03-19 15:20:31 +01:00
|
|
|
fi
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
2012-06-12 01:57:49 +02:00
|
|
|
#
|
|
|
|
# Delete default routes with metric 0 from the passed routing table
|
|
|
|
#
|
|
|
|
delete_default_routes() # $1 = table number
|
|
|
|
{
|
2013-10-14 16:15:08 +02:00
|
|
|
$IP -$g_family route ls table $1 | grep -F default | grep -vF metric | while read route; do
|
2017-10-11 19:58:35 +02:00
|
|
|
qt $IP -$g_family route del $route table $1
|
2012-06-12 01:57:49 +02:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
restore_default_route() # $1 = USE_DEFAULT_RT
|
|
|
|
{
|
|
|
|
local result
|
|
|
|
result=1
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ -z "$g_noroutes" -a -f ${VARDIR}/default_route ]; then
|
|
|
|
local default_route
|
|
|
|
default_route=
|
|
|
|
local route
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
while read route ; do
|
|
|
|
case $route in
|
|
|
|
default*)
|
|
|
|
replace_default_route $1
|
|
|
|
default_route="$default_route $route"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
default_route="$default_route $route"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done < ${VARDIR}/default_route
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
replace_default_route $1
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ $result = 1 ]; then
|
|
|
|
#
|
|
|
|
# We didn't restore a default route with metric 0
|
|
|
|
#
|
2013-10-14 16:15:08 +02:00
|
|
|
if $IP -$g_family -o route list 2> /dev/null | grep -F default | grep -qvF metric; then
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
# But we added a default route with metric 0
|
|
|
|
#
|
|
|
|
qt $IP -$g_family route del default metric 0 && progress_message "Default route with metric 0 deleted"
|
|
|
|
fi
|
|
|
|
fi
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
rm -f ${VARDIR}/default_route
|
|
|
|
fi
|
2012-03-18 16:48:26 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
return $result
|
|
|
|
}
|
2012-03-18 16:48:26 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
#
|
|
|
|
# Flush the conntrack table if $g_purge is non-empty
|
|
|
|
#
|
|
|
|
conditionally_flush_conntrack() {
|
2012-03-18 16:48:26 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ -n "$g_purge" ]; then
|
2017-08-24 00:04:47 +02:00
|
|
|
if [ -n "$(mywhich conntrack)" ]; then
|
2012-03-19 15:20:31 +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
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Issue a message and stop/restore the firewall.
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
fatal_error()
|
2012-03-18 16:48:26 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
echo " ERROR: $@" >&2
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ $LOG_VERBOSITY -ge 0 ]; then
|
2015-10-08 22:16:32 +02:00
|
|
|
timestamp="$(date +'%b %e %T') "
|
2012-03-19 15:20:31 +01:00
|
|
|
echo "${timestamp} ERROR: $@" >> $STARTUP_LOG
|
|
|
|
fi
|
|
|
|
|
|
|
|
stop_firewall
|
|
|
|
[ -n "$TEMPFILE" ] && rm -f $TEMPFILE
|
2013-05-09 18:57:09 +02:00
|
|
|
mutex_off
|
2012-03-19 15:20:31 +01:00
|
|
|
exit 2
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Run iptables/ip6tables and if an error occurs, stop/restore the firewall
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
run_iptables()
|
2012-01-03 22:06:00 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
local status
|
|
|
|
|
|
|
|
while [ 1 ]; do
|
2012-10-14 22:26:08 +02:00
|
|
|
eval $g_tool $@
|
2012-03-19 15:20:31 +01:00
|
|
|
status=$?
|
|
|
|
[ $status -ne 4 ] && break
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ $status -ne 0 ]; then
|
|
|
|
error_message "ERROR: Command \"$g_tool $@\" Failed"
|
|
|
|
stop_firewall
|
|
|
|
exit 2
|
|
|
|
fi
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Run iptables/ip6tables retrying exit status 4
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
do_iptables()
|
2012-01-03 22:06:00 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
local status
|
|
|
|
|
|
|
|
while [ 1 ]; do
|
|
|
|
$g_tool $@
|
|
|
|
status=$?
|
|
|
|
[ $status -ne 4 ] && return $status;
|
|
|
|
done
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Run ip and if an error occurs, stop/restore the firewall
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
run_ip()
|
2012-03-18 16:48:26 +01:00
|
|
|
{
|
2012-03-19 15:20:31 +01:00
|
|
|
if ! $IP -$g_family $@; then
|
|
|
|
error_message "ERROR: Command \"$IP -$g_family $@\" Failed"
|
|
|
|
stop_firewall
|
|
|
|
exit 2
|
|
|
|
fi
|
2012-01-03 22:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Run tc and if an error occurs, stop/restore the firewall
|
2012-01-03 22:06:00 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
run_tc() {
|
|
|
|
if ! $TC $@ ; then
|
|
|
|
error_message "ERROR: Command \"$TC $@\" Failed"
|
|
|
|
stop_firewall
|
|
|
|
exit 2
|
|
|
|
fi
|
2012-03-18 16:48:26 +01:00
|
|
|
}
|
2012-03-06 00:21:10 +01:00
|
|
|
|
2012-03-18 16:48:26 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
# Run the .iptables_restore_input as a set of discrete iptables commands
|
2012-03-18 16:48:26 +01:00
|
|
|
#
|
2012-03-19 15:20:31 +01:00
|
|
|
debug_restore_input() {
|
|
|
|
local first second rest table chain
|
|
|
|
#
|
|
|
|
# Clear the ruleset
|
|
|
|
#
|
|
|
|
qt1 $g_tool -t mangle -F
|
|
|
|
qt1 $g_tool -t mangle -X
|
2012-03-06 00:21:10 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
for chain in PREROUTING INPUT FORWARD POSTROUTING; do
|
|
|
|
qt1 $g_tool -t mangle -P $chain ACCEPT
|
|
|
|
done
|
2012-03-06 00:21:10 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
qt1 $g_tool -t raw -F
|
|
|
|
qt1 $g_tool -t raw -X
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
for chain in PREROUTING OUTPUT; do
|
|
|
|
qt1 $g_tool -t raw -P $chain ACCEPT
|
|
|
|
done
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
qt1 $g_tool -t nat -F
|
|
|
|
qt1 $g_tool -t nat -X
|
2012-01-03 22:06:00 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
for chain in PREROUTING POSTROUTING; do
|
|
|
|
qt1 $g_tool -t nat -P $chain ACCEPT
|
|
|
|
done
|
2012-01-16 04:23:44 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
qt1 $g_tool -t filter -F
|
|
|
|
qt1 $g_tool -t filter -X
|
2012-01-16 04:23:44 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
for chain in INPUT FORWARD OUTPUT; do
|
|
|
|
qt1 $g_tool -t filter -P $chain -P ACCEPT
|
|
|
|
done
|
2012-01-16 04:23:44 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
while read first second rest; do
|
|
|
|
case $first in
|
|
|
|
-*)
|
|
|
|
#
|
|
|
|
# We can't call run_iptables() here because the rules may contain quoted strings
|
|
|
|
#
|
|
|
|
eval $g_tool -t $table $first $second $rest
|
2012-01-16 04:23:44 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ $? -ne 0 ]; then
|
2014-03-18 15:28:14 +01:00
|
|
|
error_message "ERROR: Command \"$g_tool -t $table $first $second $rest\" Failed"
|
2012-03-19 15:20:31 +01:00
|
|
|
stop_firewall
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
:*)
|
|
|
|
chain=${first#:}
|
2012-01-16 04:23:44 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ "x$second" = x- ]; then
|
|
|
|
do_iptables -t $table -N $chain
|
|
|
|
else
|
|
|
|
do_iptables -t $table -P $chain $second
|
|
|
|
fi
|
2012-01-16 04:23:44 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
error_message "ERROR: Command \"$g_tool $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
|
|
|
|
}
|
2012-03-18 16:48:26 +01:00
|
|
|
|
2014-10-28 02:38:22 +01:00
|
|
|
interface_enabled() {
|
2016-09-30 23:01:16 +02:00
|
|
|
status=0
|
|
|
|
|
2016-10-01 01:01:04 +02:00
|
|
|
if [ -f ${VARDIR}/${1}_disabled ]; then
|
|
|
|
status=1
|
|
|
|
elif [ -f ${VARDIR}/${1}.status ]; then
|
|
|
|
status=$(cat ${VARDIR}/${1}.status)
|
|
|
|
fi
|
2016-09-30 23:01:16 +02:00
|
|
|
|
2016-10-09 19:58:29 +02:00
|
|
|
return $status
|
2012-01-16 04:23:44 +01:00
|
|
|
}
|
2012-03-18 16:48:26 +01:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
distribute_load() {
|
|
|
|
local interface
|
2015-05-27 15:58:10 +02:00
|
|
|
local currentload # Total load of enabled interfaces
|
|
|
|
local load # Specified load of an enabled interface
|
|
|
|
local mark # Mark of an enabled interface
|
|
|
|
local totalload # Total load of all interfaces - usually 1.000000
|
|
|
|
local nload # Normalized load of an enabled interface
|
|
|
|
local var # Interface name to embed in a variable name
|
2012-03-19 15:20:31 +01:00
|
|
|
|
2015-05-26 23:59:20 +02:00
|
|
|
totalload=$1
|
2012-03-19 15:20:31 +01:00
|
|
|
shift
|
|
|
|
|
2015-05-26 23:59:20 +02:00
|
|
|
currentload=0
|
2012-03-19 15:20:31 +01:00
|
|
|
|
|
|
|
for interface in $@; do
|
2014-10-28 02:38:22 +01:00
|
|
|
if interface_enabled $interface; then
|
2015-05-27 15:58:10 +02:00
|
|
|
var=$(echo $interface | sed 's/[.-]/_/g')
|
2012-03-19 15:20:31 +01:00
|
|
|
load=$(cat ${VARDIR}/${interface}_load)
|
2015-05-27 15:58:10 +02:00
|
|
|
eval ${var}_load=$load
|
2012-05-08 22:55:13 +02:00
|
|
|
mark=$(cat ${VARDIR}/${interface}_mark)
|
2015-05-27 15:58:10 +02:00
|
|
|
eval ${var}_mark=$mark
|
2015-05-26 23:59:20 +02:00
|
|
|
currentload=$( bc <<EOF
|
2012-03-19 15:20:31 +01:00
|
|
|
scale=8
|
2015-05-26 23:59:20 +02:00
|
|
|
$currentload + $load
|
2012-03-19 15:20:31 +01:00
|
|
|
EOF
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2015-05-26 23:59:20 +02:00
|
|
|
if [ $currentload ]; then
|
2012-03-19 15:20:31 +01:00
|
|
|
for interface in $@; do
|
|
|
|
qt $g_tool -t mangle -F ~$interface
|
2015-05-26 23:59:20 +02:00
|
|
|
|
2015-05-27 15:58:10 +02:00
|
|
|
var=$(echo $interface | sed 's/[.-]/_/g')
|
|
|
|
eval load=\$${var}_load
|
|
|
|
eval mark=\$${var}_mark
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2012-03-19 15:20:31 +01:00
|
|
|
if [ -n "$load" ]; then
|
2015-05-27 15:58:10 +02:00
|
|
|
nload=$(bc <<EOF
|
2012-03-19 15:20:31 +01:00
|
|
|
scale=8
|
2015-05-26 23:59:20 +02:00
|
|
|
( $load / $currentload ) * $totalload
|
2012-03-19 15:20:31 +01:00
|
|
|
EOF
|
|
|
|
)
|
2015-05-26 23:59:20 +02:00
|
|
|
currentload=$(bc <<EOF
|
2012-03-19 15:20:31 +01:00
|
|
|
scale=8
|
2015-05-27 15:58:10 +02:00
|
|
|
$currentload - $load
|
2012-03-19 15:20:31 +01:00
|
|
|
EOF
|
|
|
|
)
|
2015-05-23 16:06:31 +02:00
|
|
|
|
2015-05-27 15:58:10 +02:00
|
|
|
case $nload in
|
2015-05-23 16:06:31 +02:00
|
|
|
.*|0.*)
|
2015-05-27 15:58:10 +02:00
|
|
|
run_iptables -t mangle -A ~$interface -m statistic --mode random --probability $nload -j MARK --set-mark $mark
|
2015-05-23 16:06:31 +02:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
run_iptables -t mangle -A ~$interface -j MARK --set-mark $mark
|
|
|
|
;;
|
|
|
|
esac
|
2012-03-19 15:20:31 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
2012-03-19 23:56:02 +01:00
|
|
|
|
2012-10-13 16:14:51 +02:00
|
|
|
?if __IPV4
|
2012-03-20 15:17:19 +01:00
|
|
|
#################################################################################
|
|
|
|
# IPv4-specific Functions
|
|
|
|
#################################################################################
|
2012-03-19 23:56:02 +01:00
|
|
|
#
|
2012-05-14 00:53:02 +02:00
|
|
|
# Determine if interface is usable from a Netfilter perspective
|
|
|
|
#
|
|
|
|
interface_is_usable() # $1 = interface
|
|
|
|
{
|
|
|
|
local status;
|
|
|
|
status=0
|
|
|
|
|
2015-01-04 17:35:33 +01:00
|
|
|
if ! loopback_interface $1; then
|
2012-05-14 00:53:02 +02:00
|
|
|
if interface_is_up $1 && [ "$(find_first_interface_address_if_any $1)" != 0.0.0.0 ]; then
|
2016-10-01 01:01:04 +02:00
|
|
|
if [ "$COMMAND" != enable ]; then
|
|
|
|
[ ! -f ${VARDIR}/${1}_disabled ] && run_isusable_exit $1
|
|
|
|
status=$?
|
|
|
|
fi
|
2012-05-15 15:58:03 +02:00
|
|
|
else
|
|
|
|
status=1
|
2012-05-14 00:53:02 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $status
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Find interface addresses--returns the set of addresses assigned to the passed device
|
|
|
|
#
|
|
|
|
find_interface_addresses() # $1 = interface
|
|
|
|
{
|
|
|
|
$IP -f inet addr show $1 2> /dev/null | grep inet\ | sed 's/\s*inet //;s/\/.*//;s/ peer.*//'
|
|
|
|
}
|
2012-03-19 23:56:02 +01:00
|
|
|
|
2012-05-14 00:53:02 +02:00
|
|
|
#
|
|
|
|
# Find the value 'weight' in the passed arguments then echo the next value
|
|
|
|
#
|
2012-03-19 23:56:02 +01:00
|
|
|
find_weight() {
|
|
|
|
while [ $# -gt 1 ]; do
|
|
|
|
[ "x$1" = xweight ] && 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() {
|
|
|
|
$IP -4 route list | while read addr rest; do
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
|
|
|
$IP -4 route show dev $1 2> /dev/null |
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Get the broadcast addresses associated with an interface
|
|
|
|
#
|
|
|
|
get_interface_bcasts() # $1 = interface
|
|
|
|
{
|
|
|
|
local addresses
|
|
|
|
addresses=
|
|
|
|
|
|
|
|
$IP -f inet addr show dev $1 2> /dev/null | grep 'inet.*brd' | sed 's/inet.*brd //; s/scope.*//;' | sort -u
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Delete IP address
|
|
|
|
#
|
|
|
|
del_ip_addr() # $1 = address, $2 = interface
|
|
|
|
{
|
|
|
|
[ $(find_first_interface_address_if_any $2) = $1 ] || qtnoin $IP addr del $1 dev $2
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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
|
2012-04-06 17:01:07 +02:00
|
|
|
# the VLSM and BRD from an existing address in the same network
|
2012-03-19 23:56:02 +01:00
|
|
|
#
|
|
|
|
# Get all of the lines that contain inet addresses with broadcast
|
|
|
|
#
|
|
|
|
$IP -f inet addr show $interface 2> /dev/null | grep 'inet.*brd' | while read inet cidr rest ; do
|
|
|
|
case $cidr in
|
|
|
|
*/*)
|
|
|
|
if in_network $external $cidr; then
|
|
|
|
echo "/${cidr#*/} brd $(broadcastaddress $cidr)"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
do_one()
|
|
|
|
{
|
|
|
|
val=$(address_details)
|
|
|
|
|
|
|
|
$IP addr add ${external}${val} dev $interface $label
|
|
|
|
[ -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
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Detect the gateway through a PPP or DHCP-configured interface
|
|
|
|
#
|
|
|
|
detect_dynamic_gateway() { # $1 = interface
|
|
|
|
local interface
|
|
|
|
interface=$1
|
|
|
|
local GATEWAYS
|
|
|
|
GATEWAYS=
|
|
|
|
local gateway
|
2014-11-14 22:39:23 +01:00
|
|
|
local file
|
2012-03-19 23:56:02 +01:00
|
|
|
|
|
|
|
gateway=$(run_findgw_exit $1);
|
|
|
|
|
|
|
|
if [ -z "$gateway" ]; then
|
|
|
|
gateway=$( find_peer $($IP addr list $interface ) )
|
|
|
|
fi
|
|
|
|
|
2014-11-14 22:39:23 +01:00
|
|
|
file="${VARLIB}/dhcpcd/dhcpcd-${1}.info"
|
|
|
|
if [ -z "$gateway" -a -f "${file}" ]; then
|
|
|
|
eval $(grep ^GATEWAYS= "${file}" 2> /dev/null)
|
2012-03-19 23:56:02 +01:00
|
|
|
[ -n "$GATEWAYS" ] && GATEWAYS=${GATEWAYS%,*} && gateway=$GATEWAYS
|
|
|
|
fi
|
|
|
|
|
2014-11-14 22:39:23 +01:00
|
|
|
for file in \
|
|
|
|
"${VARLIB}/dhcp/dhclient-${1}.lease" \
|
|
|
|
"${VARLIB}/dhcp/dhclient.${1}.leases"
|
|
|
|
do
|
|
|
|
[ -n "$gateway" ] && break
|
|
|
|
if [ -f "${file}" ]; then
|
|
|
|
gateway=$(grep 'option routers' "${file}" | tail -n 1 | while read j1 j2 gateway; do echo "${gateway%\;}" ; return 0; done)
|
|
|
|
fi
|
|
|
|
done
|
2012-03-19 23:56:02 +01:00
|
|
|
|
|
|
|
[ -n "$gateway" ] && echo $gateway
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Detect the gateway through an interface
|
|
|
|
#
|
2017-08-24 00:04:47 +02:00
|
|
|
detect_gateway() # $1 = interface $2 = table number
|
2012-03-19 23:56:02 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
#
|
|
|
|
[ -n "$gateway" ] || gateway=$(find_gateway $($IP -4 route list dev $interface | grep ^default))
|
2017-08-24 00:04:47 +02:00
|
|
|
|
|
|
|
[ -z "$gateway" -a -n "$2" ] && gateway=$(find_gateway $($IP -4 route list dev $interface table $2 | grep ^default))
|
2012-03-19 23:56:02 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Disable IPV6
|
|
|
|
#
|
|
|
|
disable_ipv6() {
|
2015-01-02 17:49:38 +01:00
|
|
|
local temp
|
|
|
|
temp="$($IP -f inet6 addr list 2> /dev/null)"
|
2012-03-19 23:56:02 +01:00
|
|
|
|
2015-01-02 17:49:38 +01:00
|
|
|
if [ -n "$temp" ]; then
|
2012-03-19 23:56:02 +01:00
|
|
|
if [ -x "$IP6TABLES" ]; then
|
|
|
|
$IP6TABLES -P FORWARD DROP
|
|
|
|
$IP6TABLES -P INPUT DROP
|
|
|
|
$IP6TABLES -P OUTPUT DROP
|
|
|
|
$IP6TABLES -F
|
|
|
|
$IP6TABLES -X
|
2015-01-02 17:49:38 +01:00
|
|
|
|
|
|
|
for temp in $(find_loopback_interfaces); do
|
|
|
|
$IP6TABLES -A OUTPUT -o $temp -j ACCEPT
|
|
|
|
$IP6TABLES -A INPUT -i $temp -j ACCEPT
|
|
|
|
done
|
2012-03-19 23:56:02 +01:00
|
|
|
else
|
|
|
|
error_message "WARNING: DISABLE_IPV6=Yes in shorewall.conf but this system does not appear to have ip6tables"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Add an additional gateway to the default route
|
|
|
|
#
|
|
|
|
add_gateway() # $1 = Delta $2 = Table Number
|
|
|
|
{
|
|
|
|
local route
|
|
|
|
local weight
|
|
|
|
local delta
|
|
|
|
local dev
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2012-03-19 23:56:02 +01:00
|
|
|
route=`$IP -4 -o route ls table $2 | grep ^default | sed 's/default //; s/[\]//g'`
|
|
|
|
|
|
|
|
if [ -z "$route" ]; then
|
|
|
|
run_ip route add default scope global table $2 $1
|
|
|
|
else
|
|
|
|
delta=$1
|
|
|
|
|
2013-10-14 16:15:08 +02:00
|
|
|
if ! echo $route | grep -qF ' nexthop '; then
|
|
|
|
if echo $route | grep -qF via; then
|
2012-08-16 15:59:35 +02:00
|
|
|
route=`echo $route | sed 's/via/nexthop via/'`
|
|
|
|
else
|
|
|
|
route="nexthop $route"
|
|
|
|
fi
|
|
|
|
|
2012-03-19 23:56:02 +01:00
|
|
|
dev=$(find_device $route)
|
|
|
|
if [ -f ${VARDIR}/${dev}_weight ]; then
|
|
|
|
weight=`cat ${VARDIR}/${dev}_weight`
|
|
|
|
route="$route weight $weight"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
run_ip route replace default scope global table $2 $route $delta
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Remove a gateway from the default route
|
|
|
|
#
|
|
|
|
delete_gateway() # $! = Description of the Gateway $2 = table number $3 = device
|
|
|
|
{
|
|
|
|
local route
|
|
|
|
local gateway
|
|
|
|
local dev
|
|
|
|
|
|
|
|
route=`$IP -4 -o route ls table $2 | grep ^default | sed 's/[\]//g'`
|
|
|
|
gateway=$1
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2012-03-19 23:56:02 +01:00
|
|
|
if [ -n "$route" ]; then
|
2013-10-14 16:15:08 +02:00
|
|
|
if echo $route | grep -qF ' nexthop '; then
|
2016-09-27 20:43:26 +02:00
|
|
|
if interface_is_up $3; then
|
|
|
|
gateway="nexthop $gateway"
|
|
|
|
else
|
|
|
|
gateway="nexthop $gateway dead"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if eval echo $route \| fgrep -q \'$gateway\'; then
|
|
|
|
eval route=\`echo $route \| sed \'s/$gateway/ /\'\`
|
|
|
|
run_ip route replace table $2 $route
|
|
|
|
fi
|
2012-03-19 23:56:02 +01:00
|
|
|
else
|
|
|
|
dev=$(find_device $route)
|
|
|
|
[ "$dev" = "$3" ] && run_ip route delete default table $2
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
result=$($IP neigh list | awk "/^$1 / {print \$5}")
|
|
|
|
|
|
|
|
case $result in
|
|
|
|
\<*\>)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
[ -n "$result" ] && echo $result
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Clear Proxy Arp
|
|
|
|
#
|
|
|
|
delete_proxyarp() {
|
|
|
|
if [ -f ${VARDIR}/proxyarp ]; then
|
|
|
|
while read address interface external haveroute; do
|
|
|
|
qtnoin $IP -4 neigh del proxy $address dev $external
|
|
|
|
[ -z "${haveroute}${g_noroutes}" ] && qtnoin $IP -4 route del $address/32 dev $interface
|
|
|
|
f=/proc/sys/net/ipv4/conf/$interface/proxy_arp
|
|
|
|
[ -f $f ] && echo 0 > $f
|
|
|
|
done < ${VARDIR}/proxyarp
|
|
|
|
|
|
|
|
rm -f ${VARDIR}/proxyarp
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Remove all Shorewall-added rules
|
|
|
|
#
|
|
|
|
clear_firewall() {
|
|
|
|
stop_firewall
|
|
|
|
|
|
|
|
setpolicy INPUT ACCEPT
|
|
|
|
setpolicy FORWARD ACCEPT
|
|
|
|
setpolicy OUTPUT ACCEPT
|
|
|
|
|
|
|
|
run_iptables -F
|
|
|
|
qt $IPTABLES -t raw -F
|
|
|
|
|
|
|
|
if [ -n "$DISABLE_IPV6" ]; then
|
|
|
|
if [ -x $IP6TABLES ]; then
|
|
|
|
$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"
|
|
|
|
|
|
|
|
logger -p kern.info "$g_product Cleared"
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
2012-10-13 16:14:51 +02:00
|
|
|
?else
|
2012-03-20 15:17:19 +01:00
|
|
|
#################################################################################
|
2012-04-06 17:01:07 +02:00
|
|
|
# IPv6-specific Functions
|
2012-03-20 15:17:19 +01:00
|
|
|
#################################################################################
|
2012-05-14 00:53:02 +02:00
|
|
|
#
|
|
|
|
# Determine if interface is usable from a Netfilter perspective
|
|
|
|
#
|
|
|
|
interface_is_usable() # $1 = interface
|
|
|
|
{
|
|
|
|
local status;
|
|
|
|
status=0
|
|
|
|
|
2012-05-15 15:58:03 +02:00
|
|
|
if [ "$1" != lo ]; then
|
2012-05-14 00:53:02 +02:00
|
|
|
if interface_is_up $1 && [ "$(find_first_interface_address_if_any $1)" != :: ]; then
|
2016-10-01 01:01:04 +02:00
|
|
|
if [ "$COMMAND" != enable ]; then
|
|
|
|
[ ! -f ${VARDIR}/${1}_disabled ] && run_isusable_exit $1
|
|
|
|
status=$?
|
|
|
|
fi
|
2012-05-15 15:58:03 +02:00
|
|
|
else
|
|
|
|
status=1
|
2012-05-14 00:53:02 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $status
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Find interface addresses--returns the set of addresses assigned to the passed device
|
|
|
|
#
|
|
|
|
find_interface_addresses() # $1 = interface
|
|
|
|
{
|
2016-05-02 16:25:37 +02:00
|
|
|
$IP -f inet6 addr show $1 2> /dev/null | grep 'inet6 2' | sed 's/\s*inet6 //;s/\/.*//;s/ peer [0-9a-f:]*//'
|
2012-05-14 00:53:02 +02:00
|
|
|
}
|
|
|
|
|
2012-03-19 23:56:02 +01:00
|
|
|
#
|
|
|
|
# Get all interface addresses with VLSMs
|
|
|
|
#
|
|
|
|
|
|
|
|
find_interface_full_addresses() # $1 = interface
|
|
|
|
{
|
2016-05-02 16:25:37 +02:00
|
|
|
$IP -f inet6 addr show $1 2> /dev/null | grep 'inet6 ' | sed 's/\s*inet6 //;s/ scope.*//;s/ peer [0-9a-f:]*//'
|
2012-03-19 23:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Normalize an IPv6 Address by compressing out consecutive zero elements
|
|
|
|
#
|
|
|
|
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
|
|
|
|
case $address in
|
|
|
|
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
|
|
|
|
#
|
|
|
|
# Note: at this point $address and $badress are the same except possibly for
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Detect the gateway through an interface
|
|
|
|
#
|
|
|
|
detect_gateway() # $1 = interface
|
|
|
|
{
|
|
|
|
local interface
|
|
|
|
interface=$1
|
|
|
|
#
|
|
|
|
# First assume that this is some sort of point-to-point interface
|
|
|
|
#
|
|
|
|
gateway=$( find_peer $($IP -6 addr list $interface ) )
|
|
|
|
#
|
|
|
|
# Maybe there's a default route through this gateway already
|
|
|
|
#
|
|
|
|
[ -n "$gateway" ] || gateway=$(find_gateway $($IP -6 route list dev $interface | grep '^default'))
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Add an additional gateway to the default route
|
|
|
|
#
|
|
|
|
add_gateway() # $1 = Delta $2 = Table Number
|
|
|
|
{
|
|
|
|
local route
|
|
|
|
local weight
|
|
|
|
local delta
|
|
|
|
local dev
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2012-03-19 23:56:02 +01:00
|
|
|
run_ip route add default scope global table $2 $1
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Remove a gateway from the default route
|
|
|
|
#
|
|
|
|
delete_gateway() # $! = Description of the Gateway $2 = table number $3 = device
|
|
|
|
{
|
|
|
|
local route
|
|
|
|
local gateway
|
|
|
|
local dev
|
|
|
|
|
|
|
|
route=`$IP -6 -o route ls table $2 | grep ^default | sed 's/[\]//g'`
|
|
|
|
gateway=$1
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2012-03-19 23:56:02 +01:00
|
|
|
dev=$(find_device $route)
|
|
|
|
[ "$dev" = "$3" ] && run_ip route delete default table $2
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Clear Proxy NDP
|
|
|
|
#
|
|
|
|
delete_proxyndp() {
|
|
|
|
if [ -f ${VARDIR}/proxyndp ]; then
|
|
|
|
while read address interface external haveroute; do
|
|
|
|
qt $IP -6 neigh del proxy $address dev $external
|
|
|
|
[ -z "${haveroute}${g_noroutes}" ] && qt $IP -6 route del $address/128 dev $interface
|
|
|
|
f=/proc/sys/net/ipv6/conf/$interface/proxy_ndp
|
|
|
|
[ -f $f ] && echo 0 > $f
|
|
|
|
done < ${VARDIR}/proxyndp
|
|
|
|
|
|
|
|
rm -f ${VARDIR}/proxyndp
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Remove all Shorewall-added rules
|
|
|
|
#
|
|
|
|
clear_firewall() {
|
|
|
|
stop_firewall
|
|
|
|
|
|
|
|
setpolicy INPUT ACCEPT
|
|
|
|
setpolicy FORWARD ACCEPT
|
|
|
|
setpolicy OUTPUT ACCEPT
|
|
|
|
|
|
|
|
run_iptables -F
|
|
|
|
qt $IP6TABLES -t raw -F
|
|
|
|
|
|
|
|
run_clear_exit
|
|
|
|
|
|
|
|
set_state "Cleared"
|
|
|
|
|
|
|
|
logger -p kern.info "$g_product Cleared"
|
|
|
|
}
|
|
|
|
|
2013-01-12 00:50:23 +01:00
|
|
|
?endif # IPv6-specific functions.
|