diff mbox series

[1/1] package/nftables: add init script

Message ID 20240613155931.3986107-1-fiona.klute@gmx.de
State New
Headers show
Series [1/1] package/nftables: add init script | expand

Commit Message

Fiona Klute June 13, 2024, 3:59 p.m. UTC
The init script handles a ruleset file with support for atomic
reloading. By default the ruleset is expected in /etc/nftables.conf,
the location can be changed in /etc/default/nftables. If the ruleset
file does not exist the script does nothing but echos a warning about
that fact.

Signed-off-by: Fiona Klute <fiona.klute+wiwa@gmx.de>
---
 package/nftables/S35nftables | 53 ++++++++++++++++++++++++++++++++++++
 package/nftables/nftables.mk |  5 ++++
 2 files changed, 58 insertions(+)
 create mode 100644 package/nftables/S35nftables

--
2.45.1
diff mbox series

Patch

diff --git a/package/nftables/S35nftables b/package/nftables/S35nftables
new file mode 100644
index 0000000000..03f7821a48
--- /dev/null
+++ b/package/nftables/S35nftables
@@ -0,0 +1,53 @@ 
+#!/bin/sh
+
+DAEMON="nftables"
+
+# Main ruleset file, override in /etc/default/nftables if you want a
+# different location. The file should include a "flush ruleset"
+# command to atomically replace any previous rules.
+NFTABLES_CONFIG="/etc/nftables.conf"
+
+# shellcheck source=/dev/null
+[ -r "/etc/default/$DAEMON" ] && . "/etc/default/$DAEMON"
+
+# Run only if the ruleset file exists.
+if [ ! -f "${NFTABLES_CONFIG}" ]; then
+	echo "No nftables config file, nothing to do."
+	exit 0
+fi
+
+start() {
+	printf "Loading nftables rules: "
+	if /usr/sbin/nft -f "${NFTABLES_CONFIG}"; then
+		echo "OK"
+	else
+		echo "FAIL"
+	fi
+}
+
+stop() {
+	printf "Clearing nftables rules: "
+	if /usr/sbin/nft flush ruleset; then
+		echo "OK"
+	else
+		echo "FAIL"
+	fi
+}
+
+case "$1" in
+	start|reload)
+		start
+		;;
+	stop)
+		stop
+		;;
+	restart)
+		stop
+		start
+		;;
+	*)
+		echo "Usage: $0 {start|stop|restart|reload}"
+		exit 1
+esac
+
+exit $?
diff --git a/package/nftables/nftables.mk b/package/nftables/nftables.mk
index 9cba243372..d74ca2da64 100644
--- a/package/nftables/nftables.mk
+++ b/package/nftables/nftables.mk
@@ -57,6 +57,11 @@  define NFTABLES_LINUX_CONFIG_FIXUPS
 	$(call KCONFIG_ENABLE_OPT,CONFIG_NF_TABLES_INET)
 endef

+define NFTABLES_INSTALL_INIT_SYSV
+	$(INSTALL) -m 0755 -D package/nftables/S35nftables \
+		$(TARGET_DIR)/etc/init.d/S35nftables
+endef
+
 $(eval $(autotools-package))

 # Legacy: we used to handle it in this .mk