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

This commit is contained in:
Robert Förster 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,5 +1,13 @@
# /etc/conf.d/iptables
# 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
${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

View File

@ -1,21 +1,21 @@
# Copyright 1999-2018 Gentoo Foundation
# Copyright 1999-2019 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
EAPI=6
EAPI=7
# Force users doing their own patches to install their own tools
AUTOTOOLS_AUTO_DEPEND=no
inherit ltprune multilib systemd toolchain-funcs autotools flag-o-matic git-r3
inherit multilib systemd toolchain-funcs autotools flag-o-matic usr-ldscript git-r3
DESCRIPTION="Linux kernel (2.4+) firewall, NAT and packet mangling tools"
HOMEPAGE="http://www.netfilter.org/projects/iptables/"
HOMEPAGE="https://www.netfilter.org/projects/iptables/"
EGIT_REPO_URI="git://git.netfilter.org/iptables.git"
LICENSE="GPL-2"
# Subslot tracks libxtables as that's the one other packages generally link
# against and iptables changes. Will have to revisit if other sonames change.
SLOT="0/12"
# Subslot reflects PV when libxtables and/or libip*tc was changed
# the last time.
SLOT="0/1.8.3"
KEYWORDS=""
IUSE="conntrack ipv6 netlink nftables pcap static-libs"
@ -24,13 +24,15 @@ COMMON_DEPEND="
netlink? ( net-libs/libnfnetlink )
nftables? (
>=net-libs/libmnl-1.0:0=
>=net-libs/libnftnl-1.0.5:0=
>=net-libs/libnftnl-1.1.3:0=
)
pcap? ( net-libs/libpcap )
"
DEPEND="${COMMON_DEPEND}
virtual/os-headers
>=sys-kernel/linux-headers-4.4:0
"
BDEPEND="
virtual/pkgconfig
nftables? (
sys-devel/flex
@ -43,7 +45,7 @@ RDEPEND="${COMMON_DEPEND}
src_prepare() {
# use the saner headers from the kernel
rm -f include/linux/{kernel,types}.h
rm include/linux/{kernel,types}.h || die
eautoreconf
}
@ -97,17 +99,23 @@ src_install() {
doins include/iptables/internal.h
keepdir /var/lib/iptables
newinitd "${FILESDIR}"/${PN}.init iptables
newconfd "${FILESDIR}"/${PN}-1.4.13.confd iptables
newinitd "${FILESDIR}"/${PN}-r2.init iptables
newconfd "${FILESDIR}"/${PN}-r1.confd iptables
if use ipv6 ; then
keepdir /var/lib/ip6tables
newinitd "${FILESDIR}"/iptables.init ip6tables
newconfd "${FILESDIR}"/ip6tables-1.4.13.confd ip6tables
dosym iptables /etc/init.d/ip6tables
newconfd "${FILESDIR}"/ip6tables-r1.confd ip6tables
fi
if use nftables; then
# Bug 647458
rm "${ED%/}"/etc/ethertypes || die
rm "${ED}"/etc/ethertypes || die
# Bug 660886
rm "${ED}"/sbin/{arptables,ebtables} || die
# Bug 669894
rm "${ED}"/sbin/ebtables-{save,restore} || die
fi
systemd_dounit "${FILESDIR}"/systemd/iptables-{re,}store.service
@ -118,5 +126,5 @@ src_install() {
# Move important libs to /lib #332175
gen_usr_ldscript -a ip{4,6}tc iptc xtables
prune_libtool_files
find "${ED}" -type f -name "*.la" -delete || die
}