Skip to contentSkip to conversation

iptables: add systemd helper unit to load/restore rules

RFC In review Jack Mitchell 3 participants 4 messages Sep 8, 2016 – Oct 3, 2016

lore
Share & tools
lore
#0 lore raw

RFC 1/1 iptables: add systemd helper unit to load/restore rules

From: Jack Mitchell <[email protected]>

there is currently no way to automatically load iptable rules in OE. Add a systemd unit file to automatically load rules on network connection. This is cribbed from the way ArchLinux handles iptables with some minor modifications for OE. New rules can be generated using 'iptables-save > iptables.rules'

---
 .../iptables/iptables/iptables.rules                 |  0
 .../iptables/iptables/iptables.service               | 13 +++++++++++++
 meta/recipes-extended/iptables/iptables_1.6.0.bb     | 20 ++++++++++++++++++--
 3 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 meta/recipes-extended/iptables/iptables/iptables.rules
 create mode 100644 meta/recipes-extended/iptables/iptables/iptables.service
meta/recipes-extended/iptables/iptables/iptables.rules new file +0 -0
new file mode 100644
index 0000000..e69de29
meta/recipes-extended/iptables/iptables/iptables.service new file +13 -0
new file mode 100644
index 0000000..041316e
@@ -0,0 +1,13 @@
+[Unit]
+Description=Packet Filtering Framework
+Before=network-pre.target
+Wants=network-pre.target
+
+[Service]
+Type=oneshot
+ExecStart=@SBINDIR@/iptables-restore /etc/iptables/iptables.rules
+ExecReload=@SBINDIR@/iptables-restore /etc/iptables/iptables.rules
+RemainAfterExit=yes
+
+[Install]
+WantedBy=multi-user.target
meta/recipes-extended/iptables/iptables_1.6.0.bb +18 -2
index fbbe418..65430a1 100644
@@ -22,13 +22,16 @@ SRC_URI = "http://netfilter.org/projects/iptables/files/iptables-${PV}.tar.bz2 \
            file://types.h-add-defines-that-are-required-for-if_packet.patch \
            file://0001-configure-Add-option-to-enable-disable-libnfnetlink.patch \
            file://0002-configure.ac-only-check-conntrack-when-libnfnetlink-enabled.patch \
-          "
+           file://iptables.service \
+           file://iptables.rules \
+"
+
 SRC_URI_append_libc-musl = " file://0001-fix-build-with-musl.patch"
 
 SRC_URI[md5sum] = "27ba3451cb622467fc9267a176f19a31"
 SRC_URI[sha256sum] = "4bb72a0a0b18b5a9e79e87631ddc4084528e5df236bc7624472dcaa8480f1c60"
 
-inherit autotools pkgconfig
+inherit autotools pkgconfig systemd
 
 EXTRA_OECONF = "--with-kernel=${STAGING_INCDIR} \
                "
@@ -48,3 +51,16 @@ do_configure_prepend() {
 	# Keep ax_check_linker_flags.m4 which belongs to autoconf-archive.
 	rm -f libtool.m4 lt~obsolete.m4 ltoptions.m4 ltsugar.m4 ltversion.m4
 }
+
+do_install_append() {
+
+        install -d ${D}${sysconfdir}/iptables
+        install -m 0644 ${WORKDIR}/iptables.rules ${D}${sysconfdir}/iptables
+
+        install -d ${D}${systemd_unitdir}/system
+        install -m 0644 ${WORKDIR}/iptables.service ${D}${systemd_unitdir}/system
+
+	sed -i -e 's,@SBINDIR@,${sbindir},g' ${D}${systemd_unitdir}/system/iptables.service
+}
+
+SYSTEMD_SERVICE_${PN} = "iptables.service"
Signature · 1 line
2.9.3
#1 lore raw
Quoted 68 lines · Jack Mitchell

From: Jack Mitchell <[email protected]>

there is currently no way to automatically load iptable rules in OE. Add a systemd unit file to automatically load rules on network connection. This is cribbed from the way ArchLinux handles iptables with some minor modifications for OE. New rules can be generated using 'iptables-save > iptables.rules'

---
 .../iptables/iptables/iptables.rules                 |  0
 .../iptables/iptables/iptables.service               | 13 +++++++++++++
 meta/recipes-extended/iptables/iptables_1.6.0.bb     | 20 ++++++++++++++++++--
 3 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 meta/recipes-extended/iptables/iptables/iptables.rules
 create mode 100644 meta/recipes-extended/iptables/iptables/iptables.service
meta/recipes-extended/iptables/iptables/iptables.rules new file +0 -0
new file mode 100644
index 0000000..e69de29
meta/recipes-extended/iptables/iptables/iptables.service new file +13 -0
new file mode 100644
index 0000000..041316e
@@ -0,0 +1,13 @@
+[Unit]
+Description=Packet Filtering Framework
+Before=network-pre.target
+Wants=network-pre.target
+
+[Service]
+Type=oneshot
+ExecStart=@SBINDIR@/iptables-restore /etc/iptables/iptables.rules
+ExecReload=@SBINDIR@/iptables-restore /etc/iptables/iptables.rules
+RemainAfterExit=yes
+
+[Install]
+WantedBy=multi-user.target
meta/recipes-extended/iptables/iptables_1.6.0.bb +8 -2
index fbbe418..65430a1 100644
@@ -22,13 +22,16 @@ SRC_URI = "http://netfilter.org/projects/iptables/files/iptables-${PV}.tar.bz2 \
            file://types.h-add-defines-that-are-required-for-if_packet.patch \
            file://0001-configure-Add-option-to-enable-disable-libnfnetlink.patch \
            file://0002-configure.ac-only-check-conntrack-when-libnfnetlink-enabled.patch \
-          "
+           file://iptables.service \
+           file://iptables.rules \
+"
+
 SRC_URI_append_libc-musl = " file://0001-fix-build-with-musl.patch"
 
 SRC_URI[md5sum] = "27ba3451cb622467fc9267a176f19a31"
 SRC_URI[sha256sum] = "4bb72a0a0b18b5a9e79e87631ddc4084528e5df236bc7624472dcaa8480f1c60"
 
-inherit autotools pkgconfig
+inherit autotools pkgconfig systemd
 
 EXTRA_OECONF = "--with-kernel=${STAGING_INCDIR} \
                "
@@ -48,3 +51,16 @@ do_configure_prepend() {
 	# Keep ax_check_linker_flags.m4 which belongs to autoconf-archive.
 	rm -f libtool.m4 lt~obsolete.m4 ltoptions.m4 ltsugar.m4 ltversion.m4
 }
+
+do_install_append() {
+

meta/recipes-extended/iptables/iptables_1.6.0.bb do_configure_prepend() { · line 66

Quoted 10 lines
meta/recipes-extended/iptables/iptables_1.6.0.bb +10 -0
+        install -d ${D}${sysconfdir}/iptables
+        install -m 0644 ${WORKDIR}/iptables.rules ${D}${sysconfdir}/iptables
+
+        install -d ${D}${systemd_unitdir}/system
+        install -m 0644 ${WORKDIR}/iptables.service ${D}${systemd_unitdir}/system
+
+	sed -i -e 's,@SBINDIR@,${sbindir},g' ${D}${systemd_unitdir}/system/iptables.service
+}
+
+SYSTEMD_SERVICE_${PN} = "iptables.service"

If there are no comments on this then I suggest we take it as it is. Not having a way to auto load iptables rules is just asking for layer/application specific hacks.

#2 lore raw
In reply to Jack Mitchell
Quoted 10 lines · Jack Mitchell

From: Jack Mitchell <[email protected]>

there is currently no way to automatically load iptable rules in OE. Add a systemd unit file to automatically load rules on network connection. This is cribbed from the way ArchLinux handles iptables with some minor modifications for OE. New rules can be generated using 'iptables-save > iptables.rules’

Patch is fine but
Can you add commentary on how one would go about doing this offline during cross compiling ?

Quoted 78 lines

---

.../iptables/iptables/iptables.rules                 |  0
.../iptables/iptables/iptables.service               | 13 +++++++++++++
meta/recipes-extended/iptables/iptables_1.6.0.bb     | 20 ++++++++++++++++++--
3 files changed, 31 insertions(+), 2 deletions(-)

create mode 100644 meta/recipes-extended/iptables/iptables/iptables.rules create mode 100644 meta/recipes-extended/iptables/iptables/iptables.service

meta/recipes-extended/iptables/iptables/iptables.rules new file +0 -0
new file mode 100644
index 0000000..e69de29
meta/recipes-extended/iptables/iptables/iptables.service new file +13 -0
new file mode 100644
index 0000000..041316e
@@ -0,0 +1,13 @@
+[Unit]
+Description=Packet Filtering Framework
+Before=network-pre.target
+Wants=network-pre.target
+
+[Service]
+Type=oneshot
+ExecStart=@SBINDIR@/iptables-restore /etc/iptables/iptables.rules
+ExecReload=@SBINDIR@/iptables-restore /etc/iptables/iptables.rules
+RemainAfterExit=yes
+
+[Install]
+WantedBy=multi-user.target
meta/recipes-extended/iptables/iptables_1.6.0.bb +4 -1
index fbbe418..65430a1 100644
@@ -22,13 +22,16 @@ SRC_URI = "http://netfilter.org/projects/iptables/files/iptables-${PV}.tar.bz2 \
           file://types.h-add-defines-that-are-required-for-if_packet.patch \
           file://0001-configure-Add-option-to-enable-disable-libnfnetlink.patch \
           file://0002-configure.ac-only-check-conntrack-when-libnfnetlink-enabled.patch \
-          "
+           file://iptables.service \
+           file://iptables.rules \
+"
+

SRC_URI_append_libc-musl = " file://0001-fix-build-with-musl.patch"

SRC_URI[md5sum] = "27ba3451cb622467fc9267a176f19a31"
SRC_URI[sha256sum] = "4bb72a0a0b18b5a9e79e87631ddc4084528e5df236bc7624472dcaa8480f1c60"

meta/recipes-extended/iptables/iptables_1.6.0.bb +1 -1
-inherit autotools pkgconfig
+inherit autotools pkgconfig systemd
EXTRA_OECONF = "--with-kernel=${STAGING_INCDIR} \
               "
meta/recipes-extended/iptables/iptables_1.6.0.bb +13 -1
@@ -48,3 +51,16 @@ do_configure_prepend() {
 	# Keep ax_check_linker_flags.m4 which belongs to autoconf-archive.
 	rm -f libtool.m4 lt~obsolete.m4 ltoptions.m4 ltsugar.m4 ltversion.m4
 }
+
+do_install_append() {
+
+        install -d ${D}${sysconfdir}/iptables
+        install -m 0644 ${WORKDIR}/iptables.rules ${D}${sysconfdir}/iptables
+
+        install -d ${D}${systemd_unitdir}/system
+        install -m 0644 ${WORKDIR}/iptables.service ${D}${systemd_unitdir}/system
+
+	sed -i -e 's,@SBINDIR@,${sbindir},g' ${D}${systemd_unitdir}/system/iptables.service
+}
+
+SYSTEMD_SERVICE_${PN} = "iptables.service"
--

2.9.3

--
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core

#3 lore raw
In reply to Jack Mitchell

Hi,

can you please use ${systemd_system_unitdir} instead of ${systemd_unitdir}/system?
Pau Espin Pedrol

2016-09-08 13:29 GMT+02:00 Jack Mitchell <[email protected]>:

Quoted 85 lines

From: Jack Mitchell <[email protected]>

there is currently no way to automatically load iptable rules in OE. Add a systemd unit file to automatically load rules on network connection. This is cribbed from the way ArchLinux handles iptables with some minor modifications for OE. New rules can be generated using 'iptables-save > iptables.rules'

---
 .../iptables/iptables/iptables.rules                 |  0
 .../iptables/iptables/iptables.service               | 13 +++++++++++++
 meta/recipes-extended/iptables/iptables_1.6.0.bb     | 20 ++++++++++++++++++--
 3 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 meta/recipes-extended/iptables/iptables/iptables.rules
 create mode 100644 meta/recipes-extended/iptables/iptables/iptables.service
meta/recipes-extended/iptables/iptables/iptables.rules new file +0 -0
new file mode 100644
index 0000000..e69de29
meta/recipes-extended/iptables/iptables/iptables.service new file +13 -0
new file mode 100644
index 0000000..041316e
@@ -0,0 +1,13 @@
+[Unit]
+Description=Packet Filtering Framework
+Before=network-pre.target
+Wants=network-pre.target
+
+[Service]
+Type=oneshot
+ExecStart=@SBINDIR@/iptables-restore /etc/iptables/iptables.rules
+ExecReload=@SBINDIR@/iptables-restore /etc/iptables/iptables.rules
+RemainAfterExit=yes
+
+[Install]
+WantedBy=multi-user.target
meta/recipes-extended/iptables/iptables_1.6.0.bb +18 -3
index fbbe418..65430a1 100644
@@ -22,13 +22,16 @@ SRC_URI = "http://netfilter.org/projects/iptables/files/iptables-${PV}.tar.bz2 \
            file://types.h-add-defines-that-are-required-for-if_packet.patch \
            file://0001-configure-Add-option-to-enable-disable-libnfnetlink.patch \
            file://0002-configure.ac-only-check-conntrack-when-libnfnetlink-enabled.patch \
-          "
+           file://iptables.service \
+           file://iptables.rules \
+"
+
 SRC_URI_append_libc-musl = " file://0001-fix-build-with-musl.patch"
 
 SRC_URI[md5sum] = "27ba3451cb622467fc9267a176f19a31"
 SRC_URI[sha256sum] = "4bb72a0a0b18b5a9e79e87631ddc4084528e5df236bc7624472dcaa8480f1c60"
 
-inherit autotools pkgconfig
+inherit autotools pkgconfig systemd
 
 EXTRA_OECONF = "--with-kernel=${STAGING_INCDIR} \
                "
@@ -48,3 +51,16 @@ do_configure_prepend() {
        # Keep ax_check_linker_flags.m4 which belongs to autoconf-archive.
        rm -f libtool.m4 lt~obsolete.m4 ltoptions.m4 ltsugar.m4 ltversion.m4
 }
+
+do_install_append() {
+
+        install -d ${D}${sysconfdir}/iptables
+        install -m 0644 ${WORKDIR}/iptables.rules ${D}${sysconfdir}/iptables
+
+        install -d ${D}${systemd_unitdir}/system
+        install -m 0644 ${WORKDIR}/iptables.service ${D}${systemd_unitdir}/system
+
+       sed -i -e 's,@SBINDIR@,${sbindir},g' ${D}${systemd_unitdir}/system/iptables.service
+}
+
+SYSTEMD_SERVICE_${PN} = "iptables.service"
--

2.9.3

--
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core