2020-12-07 15:46:31 +00:00
|
|
|
#!/sbin/openrc-run
|
|
|
|
# Copyright 1999-2011 Gentoo Foundation
|
|
|
|
# Distributed under the terms of the GNU General Public License v2
|
|
|
|
# $Header: /var/cvsroot/gentoo-x86/net-firewall/iptables/files/iptables-1.4.11.init,v 1.2 2011/12/04 10:15:59 swegener Exp $
|
2021-06-04 17:17:43 +00:00
|
|
|
# Modified: caskd <caskd@redxen.eu>
|
2020-12-07 15:46:31 +00:00
|
|
|
|
|
|
|
description="IPv4/IPv6 packet filtering and NAT"
|
|
|
|
description_save="Save firewall state"
|
|
|
|
description_panic="Drop all packets"
|
|
|
|
description_reload="Reload configuration"
|
|
|
|
|
|
|
|
extra_commands="save panic"
|
|
|
|
extra_started_commands="reload"
|
|
|
|
|
2021-06-01 23:17:41 +00:00
|
|
|
iptables_name=${SVCNAME##*.}
|
|
|
|
if [ "${iptables_name}" != "iptables" -a "${iptables_name}" != "ip6tables" ] ; then
|
|
|
|
iptables_name="iptables"
|
|
|
|
fi
|
2020-12-07 15:46:31 +00:00
|
|
|
|
|
|
|
iptables_bin="/sbin/${iptables_name}"
|
2021-06-04 17:17:43 +00:00
|
|
|
iptables_dir="${IPTABLES_DIR}"
|
2020-12-07 15:46:31 +00:00
|
|
|
case ${iptables_name} in
|
2021-06-04 17:17:43 +00:00
|
|
|
iptables) iptables_proc="/proc/net/ip_tables_names";;
|
|
|
|
ip6tables) iptables_proc="/proc/net/ip6_tables_names";;
|
2020-12-07 15:46:31 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
depend() {
|
|
|
|
before net
|
2021-06-01 23:17:41 +00:00
|
|
|
after sysctl
|
2020-12-07 15:46:31 +00:00
|
|
|
use logger
|
|
|
|
provide firewall
|
|
|
|
}
|
|
|
|
|
|
|
|
set_table_policy() {
|
|
|
|
local chains table=$1 policy=$2
|
|
|
|
case ${table} in
|
2021-06-04 17:17:43 +00:00
|
|
|
nat) chains="PREROUTING POSTROUTING OUTPUT";;
|
|
|
|
mangle) chains="PREROUTING INPUT FORWARD OUTPUT POSTROUTING";;
|
|
|
|
filter) chains="INPUT FORWARD OUTPUT";;
|
|
|
|
*) chains="";;
|
2020-12-07 15:46:31 +00:00
|
|
|
esac
|
|
|
|
local chain
|
|
|
|
for chain in ${chains} ; do
|
2021-06-04 17:17:43 +00:00
|
|
|
${iptables_bin} -w 5 -t ${table} -P ${chain} ${policy}
|
2020-12-07 15:46:31 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
checkkernel() {
|
2021-06-04 17:17:43 +00:00
|
|
|
if [ ! -e ${iptables_proc} ]; then
|
2020-12-07 15:46:31 +00:00
|
|
|
eerror "Your kernel lacks ${iptables_name} support, please load"
|
|
|
|
eerror "appropriate modules and try again."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
2021-06-04 17:17:43 +00:00
|
|
|
|
2020-12-07 15:46:31 +00:00
|
|
|
checkconfig() {
|
2021-06-04 17:17:43 +00:00
|
|
|
if [ ! -d "${iptables_dir}" ]; then
|
|
|
|
eerror "The iptables directory ${iptables_dir} doesn't exist"
|
2020-12-07 15:46:31 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
checkconfig || return 1
|
2021-06-04 17:17:43 +00:00
|
|
|
restore_file="$(mktemp)"
|
2021-06-04 18:32:14 +00:00
|
|
|
einfo "Merging ${iptables_name} rules into ${restore_file}"
|
2021-06-04 17:17:43 +00:00
|
|
|
for i in "filter" "mangle" "raw" "nat" "security"; do
|
|
|
|
[ -f "${iptables_dir}/$i" ] && cat "${iptables_dir}/$i" "${iptables_dir}/$i-rules"/* "${iptables_dir}/commit" >> "${restore_file}"
|
|
|
|
done
|
2020-12-07 15:46:31 +00:00
|
|
|
ebegin "Loading ${iptables_name} state and starting firewall"
|
2021-06-04 17:17:43 +00:00
|
|
|
${iptables_bin}-restore ${SAVE_RESTORE_OPTIONS} < "${restore_file}"
|
2020-12-07 15:46:31 +00:00
|
|
|
eend $?
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
checkkernel || return 1
|
|
|
|
ebegin "Stopping firewall"
|
|
|
|
local a
|
2021-06-04 17:17:43 +00:00
|
|
|
for a in $(cat ${iptables_proc}); do
|
2020-12-07 15:46:31 +00:00
|
|
|
set_table_policy $a ACCEPT
|
|
|
|
|
2021-06-01 23:17:41 +00:00
|
|
|
${iptables_bin} -w 5 -F -t $a
|
|
|
|
${iptables_bin} -w 5 -X -t $a
|
2020-12-07 15:46:31 +00:00
|
|
|
done
|
|
|
|
eend $?
|
|
|
|
}
|
|
|
|
|
|
|
|
reload() {
|
|
|
|
checkkernel || return 1
|
|
|
|
ebegin "Flushing firewall"
|
|
|
|
local a
|
2021-06-04 17:17:43 +00:00
|
|
|
for a in $(cat ${iptables_proc}); do
|
2021-06-01 23:17:41 +00:00
|
|
|
${iptables_bin} -w 5 -F -t $a
|
|
|
|
${iptables_bin} -w 5 -X -t $a
|
2020-12-07 15:46:31 +00:00
|
|
|
done
|
|
|
|
eend $?
|
|
|
|
|
|
|
|
start
|
|
|
|
}
|
|
|
|
|
|
|
|
panic() {
|
|
|
|
checkkernel || return 1
|
|
|
|
if service_started ${iptables_name}; then
|
|
|
|
rc-service ${iptables_name} stop
|
|
|
|
fi
|
|
|
|
|
|
|
|
local a
|
|
|
|
ebegin "Dropping all packets"
|
2021-06-04 17:17:43 +00:00
|
|
|
for a in $(cat ${iptables_proc}); do
|
2021-06-01 23:17:41 +00:00
|
|
|
${iptables_bin} -w 5 -F -t $a
|
|
|
|
${iptables_bin} -w 5 -X -t $a
|
2020-12-07 15:46:31 +00:00
|
|
|
|
|
|
|
set_table_policy $a DROP
|
|
|
|
done
|
|
|
|
eend $?
|
|
|
|
}
|