[net-firewall/iptables] long overdue sync with tree

This commit is contained in:
2019-07-18 19:03:04 +02:00
parent 782f9c4872
commit 629024c08f
8 changed files with 123 additions and 52 deletions

View File

@@ -1,16 +1,24 @@
# /etc/conf.d/ip6tables
# Location in which iptables initscript will save set rules on
# Set wait option for xtables lock in seconds
# DEFAULT: 60
#IPTABLES_LOCK_WAIT_TIME="60"
# Set wait interval option for xtables lock in microseconds
# DEFAULT: 1000
#IPTABLES_LOCK_WAIT_INTERVAL="1000"
# Location in which ip6tables initscript will save set rules on
# service shutdown
IP6TABLES_SAVE="/var/lib/ip6tables/rules-save"
# Options to pass to iptables-save and iptables-restore
# Options to pass to ip6tables-save and ip6tables-restore
SAVE_RESTORE_OPTIONS="-c"
# Save state on stopping iptables
# Save state on stopping ip6tables
SAVE_ON_STOP="yes"
# If you need to log iptables messages as soon as iptables starts,
# If you need to log ip6tables messages as soon as ip6tables starts,
# AND your logger does NOT depend on the network, then you may wish
# to uncomment the next line.
# If your logger depends on the network, and you uncomment this line

View File

@@ -1,6 +1,14 @@
# /etc/conf.d/iptables
# Location in which iptables initscript will save set rules on
# Set wait option for xtables lock in seconds
# DEFAULT: 60
#IPTABLES_LOCK_WAIT_TIME="60"
# Set wait interval option for xtables lock in microseconds
# DEFAULT: 1000
#IPTABLES_LOCK_WAIT_INTERVAL="1000"
# Location in which iptables initscript will save set rules on
# service shutdown
IPTABLES_SAVE="/var/lib/iptables/rules-save"

View File

@@ -1,15 +1,17 @@
#!/sbin/openrc-run
# Copyright 1999-2013 Gentoo Foundation
# Copyright 1999-2018 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# $Id$
extra_commands="check save panic"
extra_started_commands="reload"
iptables_lock_wait_time=${IPTABLES_LOCK_WAIT_TIME:-"60"}
iptables_lock_wait_interval=${IPTABLES_LOCK_WAIT_INTERVAL:-"1000"}
iptables_name=${SVCNAME}
case ${iptables_name} in
iptables|ip6tables) ;;
*) iptables_name="iptables" ;;
iptables|ip6tables) ;;
*) iptables_name="iptables" ;;
esac
iptables_bin="/sbin/${iptables_name}"
@@ -26,17 +28,21 @@ depend() {
}
set_table_policy() {
local chains table=$1 policy=$2
local has_errors=0 chains table=$1 policy=$2
case ${table} in
nat) chains="PREROUTING POSTROUTING OUTPUT";;
mangle) chains="PREROUTING INPUT FORWARD OUTPUT POSTROUTING";;
filter) chains="INPUT FORWARD OUTPUT";;
*) chains="";;
esac
local chain
for chain in ${chains} ; do
${iptables_bin} -w -t ${table} -P ${chain} ${policy}
${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -t ${table} -P ${chain} ${policy}
[ $? -ne 0 ] && has_errors=1
done
return ${has_errors}
}
checkkernel() {
@@ -47,8 +53,9 @@ checkkernel() {
fi
return 0
}
checkconfig() {
if [ ! -f ${iptables_save} ] ; then
if [ -z "${iptables_save}" -o ! -f "${iptables_save}" ] ; then
eerror "Not starting ${iptables_name}. First create some rules then run:"
eerror "/etc/init.d/${iptables_name} save"
return 1
@@ -56,39 +63,53 @@ checkconfig() {
return 0
}
start() {
start_pre() {
checkconfig || return 1
}
start() {
ebegin "Loading ${iptables_name} state and starting firewall"
${iptables_bin}-restore ${SAVE_RESTORE_OPTIONS} < "${iptables_save}"
${iptables_bin}-restore --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} ${SAVE_RESTORE_OPTIONS} < "${iptables_save}"
eend $?
}
stop_pre() {
checkkernel || return 1
}
stop() {
if [ "${SAVE_ON_STOP}" = "yes" ] ; then
save || return 1
fi
checkkernel || return 1
ebegin "Stopping firewall"
local a
local has_errors=0 a
for a in $(cat ${iptables_proc}) ; do
set_table_policy $a ACCEPT
[ $? -ne 0 ] && has_errors=1
${iptables_bin} -w -F -t $a
${iptables_bin} -w -X -t $a
${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -F -t $a
[ $? -ne 0 ] && has_errors=1
${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -X -t $a
[ $? -ne 0 ] && has_errors=1
done
eend $?
eend ${has_errors}
}
reload() {
checkkernel || return 1
checkrules || return 1
ebegin "Flushing firewall"
local a
local has_errors=0 a
for a in $(cat ${iptables_proc}) ; do
${iptables_bin} -w -F -t $a
${iptables_bin} -w -X -t $a
${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -F -t $a
[ $? -ne 0 ] && has_errors=1
${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -X -t $a
[ $? -ne 0 ] && has_errors=1
done
eend $?
eend ${has_errors}
start
}
@@ -113,18 +134,32 @@ save() {
}
panic() {
checkkernel || return 1
# use iptables autoload capability to load at least all required
# modules and filter table
${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -S >/dev/null
if [ $? -ne 0 ] ; then
eerror "${iptables_bin} failed to load"
return 1
fi
if service_started ${iptables_name}; then
rc-service ${iptables_name} stop
fi
local a
local has_errors=0 a
ebegin "Dropping all packets"
for a in $(cat ${iptables_proc}) ; do
${iptables_bin} -w -F -t $a
${iptables_bin} -w -X -t $a
${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -F -t $a
[ $? -ne 0 ] && has_errors=1
set_table_policy $a DROP
${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -X -t $a
[ $? -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
done
eend $?
eend ${has_errors}
}

View File

@@ -3,12 +3,12 @@ Description=Restore ip6tables firewall rules
# if both are queued for some reason, don't store before restoring :)
Before=ip6tables-store.service
# sounds reasonable to have firewall up before any of the services go up
Before=network.target
Conflicts=shutdown.target
Before=network-pre.target
Wants=network-pre.target
[Service]
Type=oneshot
ExecStart=/sbin/ip6tables-restore /var/lib/ip6tables/rules-save
ExecStart=/sbin/ip6tables-restore -w -- /var/lib/ip6tables/rules-save
[Install]
WantedBy=basic.target

View File

@@ -0,0 +1,6 @@
[Unit]
Description=Store and restore ip6tables firewall rules
[Install]
Also=ip6tables-store.service
Also=ip6tables-restore.service

View File

@@ -3,12 +3,12 @@ Description=Restore iptables firewall rules
# if both are queued for some reason, don't store before restoring :)
Before=iptables-store.service
# sounds reasonable to have firewall up before any of the services go up
Before=network.target
Conflicts=shutdown.target
Before=network-pre.target
Wants=network-pre.target
[Service]
Type=oneshot
ExecStart=/sbin/iptables-restore /var/lib/iptables/rules-save
ExecStart=/sbin/iptables-restore -w -- /var/lib/iptables/rules-save
[Install]
WantedBy=basic.target

View File

@@ -0,0 +1,6 @@
[Unit]
Description=Store and restore iptables firewall rules
[Install]
Also=iptables-store.service
Also=iptables-restore.service