mailarchive of the ptxdist mailing list
 help / color / mirror / Atom feed
From: Gavin Schenk <g.schenk@eckelmann.de>
To: ptxdist@pengutronix.de
Cc: Gavin Schenk <g.schenk@eckelmann.de>
Subject: [ptxdist] [PATCH] Add persistant iptable-rules via systemd
Date: Thu, 7 Apr 2016 09:24:26 +0200	[thread overview]
Message-ID: <1460013866-29763-1-git-send-email-g.schenk@eckelmann.de> (raw)

Supports ipv4 and ipv6 and both options can be selected in menuconfig IPTABLES_IPV6_SYSTEMD_UNIT and IPTABLES_IPV4_SYSTEMD_UNIT

If you select IPTABLES_IPV4_SYSTEMD_UNIT a systemd unit is started on multiuser.target that set the iptable rules from file /etc/iptables/rules.v4.
If you select IPTABLES_IPV6_SYSTEMD_UNIT a systemd unit is started on multiuser.target that set the iptable rules from the file /etc/iptables/rules.v6.
You have to provide this files. Both files can easily be generated with the utils iptables-save ip6tables-save from the iptables package.

e.g: Generating a rulefile, that drops port 5000 on interface eth0 ipv4
1.) iptables -A INPUT -i eth0 -p TCP --dport 5000 -j DROP
2.) iptables-save > /etc/iptables/rules.v4
---
 .../0001-Added-files-for-systemd-support.patch     | 93 ++++++++++++++++++++++
 patches/iptables-1.4.21/series                     |  4 +
 rules/iptables.in                                  | 10 +++
 rules/iptables.make                                | 15 ++++
 4 files changed, 122 insertions(+)
 create mode 100644 patches/iptables-1.4.21/0001-Added-files-for-systemd-support.patch
 create mode 100644 patches/iptables-1.4.21/series

diff --git a/patches/iptables-1.4.21/0001-Added-files-for-systemd-support.patch b/patches/iptables-1.4.21/0001-Added-files-for-systemd-support.patch
new file mode 100644
index 0000000..6b055f4
--- /dev/null
+++ b/patches/iptables-1.4.21/0001-Added-files-for-systemd-support.patch
@@ -0,0 +1,93 @@
+From: Gavin Schenk <g.schenk@eckelmann.de>
+Date: Wed, 6 Apr 2016 10:19:52 +0200
+Subject: [PATCH] Added files for systemd support
+
+---
+ scripts/ip6tables-flush          | 13 +++++++++++++
+ scripts/iptables-flush           | 13 +++++++++++++
+ systemd/system/ip6tables.service | 14 ++++++++++++++
+ systemd/system/iptables.service  | 14 ++++++++++++++
+ 4 files changed, 54 insertions(+)
+ create mode 100755 scripts/ip6tables-flush
+ create mode 100755 scripts/iptables-flush
+ create mode 100755 systemd/system/ip6tables.service
+ create mode 100755 systemd/system/iptables.service
+
+diff --git a/scripts/ip6tables-flush b/scripts/ip6tables-flush
+new file mode 100755
+index 000000000000..95086b0470d3
+--- /dev/null
++++ b/scripts/ip6tables-flush
+@@ -0,0 +1,13 @@
++#!/bin/sh
++ip6tables -F
++ip6tables -X
++ip6tables -Z
++for table in $(</proc/net/ip6_tables_names)
++do
++        ip6tables -t $table -F
++        ip6tables -t $table -X
++        ip6tables -t $table -Z
++done
++ip6tables -P INPUT ACCEPT
++ip6tables -P OUTPUT ACCEPT
++ip6tables -P FORWARD ACCEPT
+diff --git a/scripts/iptables-flush b/scripts/iptables-flush
+new file mode 100755
+index 000000000000..a47e82c0f875
+--- /dev/null
++++ b/scripts/iptables-flush
+@@ -0,0 +1,13 @@
++#!/bin/sh
++iptables -F
++iptables -X
++iptables -Z
++for table in $(</proc/net/ip_tables_names)
++do
++        iptables -t $table -F
++        iptables -t $table -X
++        iptables -t $table -Z
++done
++iptables -P INPUT ACCEPT
++iptables -P FORWARD ACCEPT
++iptables -P OUTPUT ACCEPT
+diff --git a/systemd/system/ip6tables.service b/systemd/system/ip6tables.service
+new file mode 100755
+index 000000000000..e842cc1973a1
+--- /dev/null
++++ b/systemd/system/ip6tables.service
+@@ -0,0 +1,14 @@
++[Unit]
++Description=Packet Filtering Framework
++DefaultDependencies=no
++After=systemd-sysctl.service
++Before=sysinit.target
++ConditionFileNotEmpty=/etc/iptables/rules.v6
++[Service]
++Type=oneshot
++ExecStart=/usr/sbin/ip6tables-restore /etc/iptables/rules.v6
++ExecReload=/usr/sbin/ip6tables-restore /etc/iptables/rules.v6
++ExecStop=/usr/sbin/iptables/ip6tables-flush
++RemainAfterExit=yes
++[Install]
++WantedBy=multi-user.target
+diff --git a/systemd/system/iptables.service b/systemd/system/iptables.service
+new file mode 100755
+index 000000000000..fa4a8b367ca0
+--- /dev/null
++++ b/systemd/system/iptables.service
+@@ -0,0 +1,14 @@
++[Unit]
++Description=Packet Filtering Framework
++DefaultDependencies=no
++After=systemd-sysctl.service
++Before=sysinit.target
++ConditionFileNotEmpty=/etc/iptables/rules.v4
++[Service]
++Type=oneshot
++ExecStart=/usr/sbin/iptables-restore /etc/iptables/rules.v4
++ExecReload=/usr/sbin/iptables-restore /etc/iptables/rules.v4
++ExecStop=/usr/sbin/iptables-flush
++RemainAfterExit=yes
++[Install]
++WantedBy=multi-user.target
diff --git a/patches/iptables-1.4.21/series b/patches/iptables-1.4.21/series
new file mode 100644
index 0000000..b8f388f
--- /dev/null
+++ b/patches/iptables-1.4.21/series
@@ -0,0 +1,4 @@
+# generated by git-ptx-patches
+#tag:base --start-number 1
+0001-Added-files-for-systemd-support.patch
+# 366b7fd90bb4fe7e229f0ba777703fb5  - git-ptx-patches magic
diff --git a/rules/iptables.in b/rules/iptables.in
index e6f3699..8354060 100644
--- a/rules/iptables.in
+++ b/rules/iptables.in
@@ -25,6 +25,16 @@ config IPTABLES_IPV4
 	bool
 	prompt "IPv4 support"
 
+config IPTABLES_IPV6_SYSTEMD_UNIT
+	bool
+	prompt "Activate IPv6 systemd service unit"
+	select IPTABLES_IPV6
+
+config IPTABLES_IPV4_SYSTEMD_UNIT
+	bool
+	prompt "Activate IPv4 systemd service unit"
+	select IPTABLES_IPV4
+
 config IPTABLES_LIBIPQ
 	bool
 	prompt "Enable libipq"
diff --git a/rules/iptables.make b/rules/iptables.make
index 8a1ea66..dd283a6 100644
--- a/rules/iptables.make
+++ b/rules/iptables.make
@@ -126,6 +126,21 @@ ifdef PTXCONF_IPTABLES_IPV4
 	@$(call install_link, iptables, xtables-multi, /usr/sbin/iptables-restore)
 	@$(call install_link, iptables, xtables-multi, /usr/sbin/iptables-save)
 endif
+
+ifdef PTXCONF_IPTABLES_IPV6_SYSTEMD_UNIT
+# 	# IPv6 systemd service unit part
+	@$(call install_copy, iptables, 0, 0, 0755,  $(IPTABLES_DIR)/scripts/ip6tables-flush, /usr/sbin/ip6tables-flush)
+	@$(call install_copy, iptables, 0, 0, 0644,  $(IPTABLES_DIR)/systemd/system/ip6tables.service, /lib/systemd/system/ip6tables.service)
+	@$(call install_link, iptables, ../ip6tables.service, /lib/systemd/system/multi-user.target.wants/ip6tables.service)
+endif
+
+ifdef PTXCONF_IPTABLES_IPV4_SYSTEMD_UNIT
+# 	# IPv4 systemd service unit part
+	@$(call install_copy, iptables, 0, 0, 0755,  $(IPTABLES_DIR)/scripts/iptables-flush, /usr/sbin/iptables-flush)
+	@$(call install_copy, iptables, 0, 0, 0644,  $(IPTABLES_DIR)/systemd/system/iptables.service, /lib/systemd/system/iptables.service)
+	@$(call install_link, iptables, ../iptables.service, /lib/systemd/system/multi-user.target.wants/iptables.service)
+endif
+
 endif
 
 ifdef PTXCONF_IPTABLES_INSTALL_IPTABLES_APPLY
-- 
1.9.1


_______________________________________________
ptxdist mailing list
ptxdist@pengutronix.de

             reply	other threads:[~2016-04-07  7:24 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-07  7:24 Gavin Schenk [this message]
2016-04-07  8:11 ` Uwe Kleine-König
2016-04-07  9:14   ` Schenk, Gavin
2016-04-07  9:20     ` Uwe Kleine-König
2016-04-07  9:25       ` Schenk, Gavin
2016-04-07 10:10 Gavin Schenk
2016-04-07 11:59 ` Uwe Kleine-König
2016-04-07 12:24 ` Michael Olbrich
2016-04-07 12:21 Gavin Schenk
2016-04-08 13:04 Gavin Schenk
2016-04-11 10:00 ` Michael Olbrich
2016-04-11 12:08   ` Schenk, Gavin
2016-04-11 12:44     ` Michael Olbrich
2016-04-11 12:19 Gavin Schenk
2016-04-11 17:46 ` Uwe Kleine-König
2016-04-12  8:35   ` Michael Olbrich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1460013866-29763-1-git-send-email-g.schenk@eckelmann.de \
    --to=g.schenk@eckelmann.de \
    --cc=ptxdist@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox