2016-05-18 10:09:34 +02:00
|
|
|
#!/sbin/openrc-run
|
2022-07-15 16:44:57 +02:00
|
|
|
# Copyright 1999-2022 Gentoo Authors
|
2014-11-01 11:33:56 +01:00
|
|
|
# Distributed under the terms of the GNU General Public License v2
|
|
|
|
|
|
|
|
extra_commands="check save panic"
|
|
|
|
extra_started_commands="reload"
|
|
|
|
|
2019-07-18 19:03:04 +02:00
|
|
|
iptables_lock_wait_time=${IPTABLES_LOCK_WAIT_TIME:-"60"}
|
|
|
|
iptables_lock_wait_interval=${IPTABLES_LOCK_WAIT_INTERVAL:-"1000"}
|
|
|
|
|
2014-11-01 11:33:56 +01:00
|
|
|
iptables_name=${SVCNAME}
|
|
|
|
case ${iptables_name} in
|
2019-07-18 19:03:04 +02:00
|
|
|
iptables|ip6tables) ;;
|
|
|
|
*) iptables_name="iptables" ;;
|
2014-11-01 11:33:56 +01:00
|
|
|
esac
|
|
|
|
|
|
|
|
iptables_bin="/sbin/${iptables_name}"
|
|
|
|
case ${iptables_name} in
|
|
|
|
iptables) iptables_proc="/proc/net/ip_tables_names"
|
|
|
|
iptables_save=${IPTABLES_SAVE};;
|
|
|
|
ip6tables) iptables_proc="/proc/net/ip6_tables_names"
|
|
|
|
iptables_save=${IP6TABLES_SAVE};;
|
|
|
|
esac
|
|
|
|
|
|
|
|
depend() {
|
|
|
|
need localmount #434774
|
|
|
|
before net
|
|
|
|
}
|
|
|
|
|
|
|
|
set_table_policy() {
|
2019-07-18 19:03:04 +02:00
|
|
|
local has_errors=0 chains table=$1 policy=$2
|
2014-11-01 11:33:56 +01:00
|
|
|
case ${table} in
|
|
|
|
nat) chains="PREROUTING POSTROUTING OUTPUT";;
|
|
|
|
mangle) chains="PREROUTING INPUT FORWARD OUTPUT POSTROUTING";;
|
|
|
|
filter) chains="INPUT FORWARD OUTPUT";;
|
|
|
|
*) chains="";;
|
|
|
|
esac
|
2019-07-18 19:03:04 +02:00
|
|
|
|
2014-11-01 11:33:56 +01:00
|
|
|
local chain
|
|
|
|
for chain in ${chains} ; do
|
2022-07-15 16:44:57 +02:00
|
|
|
${iptables_bin} --wait ${iptables_lock_wait_time} -t ${table} -P ${chain} ${policy}
|
2019-07-18 19:03:04 +02:00
|
|
|
[ $? -ne 0 ] && has_errors=1
|
2014-11-01 11:33:56 +01:00
|
|
|
done
|
2019-07-18 19:03:04 +02:00
|
|
|
|
|
|
|
return ${has_errors}
|
2014-11-01 11:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
checkkernel() {
|
|
|
|
if [ ! -e ${iptables_proc} ] ; then
|
|
|
|
eerror "Your kernel lacks ${iptables_name} support, please load"
|
|
|
|
eerror "appropriate modules and try again."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
2019-07-18 19:03:04 +02:00
|
|
|
|
2014-11-01 11:33:56 +01:00
|
|
|
checkconfig() {
|
2019-07-18 19:03:04 +02:00
|
|
|
if [ -z "${iptables_save}" -o ! -f "${iptables_save}" ] ; then
|
2014-11-01 11:33:56 +01:00
|
|
|
eerror "Not starting ${iptables_name}. First create some rules then run:"
|
|
|
|
eerror "/etc/init.d/${iptables_name} save"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-07-18 19:03:04 +02:00
|
|
|
start_pre() {
|
2014-11-01 11:33:56 +01:00
|
|
|
checkconfig || return 1
|
2019-07-18 19:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2014-11-01 11:33:56 +01:00
|
|
|
ebegin "Loading ${iptables_name} state and starting firewall"
|
2022-07-15 16:44:57 +02:00
|
|
|
${iptables_bin}-restore --wait ${iptables_lock_wait_time} ${SAVE_RESTORE_OPTIONS} < "${iptables_save}"
|
2014-11-01 11:33:56 +01:00
|
|
|
eend $?
|
|
|
|
}
|
|
|
|
|
2019-07-18 19:03:04 +02:00
|
|
|
stop_pre() {
|
|
|
|
checkkernel || return 1
|
|
|
|
}
|
|
|
|
|
2014-11-01 11:33:56 +01:00
|
|
|
stop() {
|
|
|
|
if [ "${SAVE_ON_STOP}" = "yes" ] ; then
|
|
|
|
save || return 1
|
|
|
|
fi
|
2019-07-18 19:03:04 +02:00
|
|
|
|
2014-11-01 11:33:56 +01:00
|
|
|
ebegin "Stopping firewall"
|
2019-07-18 19:03:04 +02:00
|
|
|
local has_errors=0 a
|
2014-11-01 11:33:56 +01:00
|
|
|
for a in $(cat ${iptables_proc}) ; do
|
|
|
|
set_table_policy $a ACCEPT
|
2019-07-18 19:03:04 +02:00
|
|
|
[ $? -ne 0 ] && has_errors=1
|
|
|
|
|
2022-07-15 16:44:57 +02:00
|
|
|
${iptables_bin} --wait ${iptables_lock_wait_time} -F -t $a
|
2019-07-18 19:03:04 +02:00
|
|
|
[ $? -ne 0 ] && has_errors=1
|
2014-11-01 11:33:56 +01:00
|
|
|
|
2022-07-15 16:44:57 +02:00
|
|
|
${iptables_bin} --wait ${iptables_lock_wait_time} -X -t $a
|
2019-07-18 19:03:04 +02:00
|
|
|
[ $? -ne 0 ] && has_errors=1
|
2014-11-01 11:33:56 +01:00
|
|
|
done
|
2019-07-18 19:03:04 +02:00
|
|
|
eend ${has_errors}
|
2014-11-01 11:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
reload() {
|
|
|
|
checkkernel || return 1
|
|
|
|
checkrules || return 1
|
|
|
|
ebegin "Flushing firewall"
|
2019-07-18 19:03:04 +02:00
|
|
|
local has_errors=0 a
|
2014-11-01 11:33:56 +01:00
|
|
|
for a in $(cat ${iptables_proc}) ; do
|
2022-07-15 16:44:57 +02:00
|
|
|
${iptables_bin} --wait ${iptables_lock_wait_time} -F -t $a
|
2019-07-18 19:03:04 +02:00
|
|
|
[ $? -ne 0 ] && has_errors=1
|
|
|
|
|
2022-07-15 16:44:57 +02:00
|
|
|
${iptables_bin} --wait ${iptables_lock_wait_time} -X -t $a
|
2019-07-18 19:03:04 +02:00
|
|
|
[ $? -ne 0 ] && has_errors=1
|
2014-11-01 11:33:56 +01:00
|
|
|
done
|
2019-07-18 19:03:04 +02:00
|
|
|
eend ${has_errors}
|
2014-11-01 11:33:56 +01:00
|
|
|
|
|
|
|
start
|
|
|
|
}
|
|
|
|
|
|
|
|
checkrules() {
|
|
|
|
ebegin "Checking rules"
|
|
|
|
${iptables_bin}-restore --test ${SAVE_RESTORE_OPTIONS} < "${iptables_save}"
|
|
|
|
eend $?
|
|
|
|
}
|
|
|
|
|
|
|
|
check() {
|
|
|
|
# Short name for users of init.d script.
|
|
|
|
checkrules
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
ebegin "Saving ${iptables_name} state"
|
|
|
|
checkpath -q -d "$(dirname "${iptables_save}")"
|
|
|
|
checkpath -q -m 0600 -f "${iptables_save}"
|
|
|
|
${iptables_bin}-save ${SAVE_RESTORE_OPTIONS} > "${iptables_save}"
|
|
|
|
eend $?
|
|
|
|
}
|
|
|
|
|
|
|
|
panic() {
|
2019-07-18 19:03:04 +02:00
|
|
|
# use iptables autoload capability to load at least all required
|
|
|
|
# modules and filter table
|
2022-07-15 16:44:57 +02:00
|
|
|
${iptables_bin} --wait ${iptables_lock_wait_time} -S >/dev/null
|
2019-07-18 19:03:04 +02:00
|
|
|
if [ $? -ne 0 ] ; then
|
|
|
|
eerror "${iptables_bin} failed to load"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2014-11-01 11:33:56 +01:00
|
|
|
if service_started ${iptables_name}; then
|
|
|
|
rc-service ${iptables_name} stop
|
|
|
|
fi
|
|
|
|
|
2019-07-18 19:03:04 +02:00
|
|
|
local has_errors=0 a
|
2014-11-01 11:33:56 +01:00
|
|
|
ebegin "Dropping all packets"
|
|
|
|
for a in $(cat ${iptables_proc}) ; do
|
2022-07-15 16:44:57 +02:00
|
|
|
${iptables_bin} --wait ${iptables_lock_wait_time} -F -t $a
|
2019-07-18 19:03:04 +02:00
|
|
|
[ $? -ne 0 ] && has_errors=1
|
2014-11-01 11:33:56 +01:00
|
|
|
|
2022-07-15 16:44:57 +02:00
|
|
|
${iptables_bin} --wait ${iptables_lock_wait_time} -X -t $a
|
2019-07-18 19:03:04 +02:00
|
|
|
[ $? -ne 0 ] && has_errors=1
|
|
|
|
|
|
|
|
if [ "${a}" != "nat" ]; then
|
|
|
|
# The "nat" table is not intended for filtering, the use of DROP is therefore inhibited.
|
|
|
|
set_table_policy $a DROP
|
|
|
|
[ $? -ne 0 ] && has_errors=1
|
|
|
|
fi
|
2014-11-01 11:33:56 +01:00
|
|
|
done
|
2019-07-18 19:03:04 +02:00
|
|
|
eend ${has_errors}
|
2014-11-01 11:33:56 +01:00
|
|
|
}
|