summaryrefslogtreecommitdiff
path: root/gnu/services
diff options
context:
space:
mode:
authorMaxim Cournoyer <maxim.cournoyer@gmail.com>2025-05-10 22:54:19 +0900
committerMaxim Cournoyer <maxim.cournoyer@gmail.com>2025-05-26 13:43:28 +0900
commitcfa2de2a77df3876061c8d26c104d2ebbae2631b (patch)
tree85f0a654fe923bd39d1705939b00bc023a0236c8 /gnu/services
parent8c5be5f31c6181eb71212f055b6dad216b5f60f4 (diff)
services: Modernize and test nftables service.
* doc/guix.texi (Networking Services) <nftables>: Update doc. * gnu/services/networking.scm (list-of-debug-levels?): (debug-level?, maybe-list-of-debug-levels?): (nftables-configuration): Rewrite using `define-configuration'. [debug-levels]: New field. (nftables-shepherd-service): Honor it. * gnu/tests/networking.scm (%inetd-echo-port): Extract to top level. (run-iptables-test): Adjust accordingly. (make-nftables-os): New procedure. (%default-nftables-ruleset-for-tests): New variable. (%nftables-os): Likewise. (%test-nftables): New test. Change-Id: I2889603342ff6d2be6261c3de6e4fddd9a9bbe2d
Diffstat (limited to 'gnu/services')
-rw-r--r--gnu/services/networking.scm49
1 files changed, 35 insertions, 14 deletions
diff --git a/gnu/services/networking.scm b/gnu/services/networking.scm
index 67653e2cbf5..8b7bf668927 100644
--- a/gnu/services/networking.scm
+++ b/gnu/services/networking.scm
@@ -10,7 +10,7 @@
10;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com> 10;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
11;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net> 11;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
12;;; Copyright © 2019 Florian Pelz <pelzflorian@pelzflorian.de> 12;;; Copyright © 2019 Florian Pelz <pelzflorian@pelzflorian.de>
13;;; Copyright © 2019, 2021, 2024 Maxim Cournoyer <maxim.cournoyer@gmail.com> 13;;; Copyright © 2019, 2021, 2024, 2025 Maxim Cournoyer <maxim.cournoyer@gmail.com>
14;;; Copyright © 2019 Sou Bunnbu <iyzsong@member.fsf.org> 14;;; Copyright © 2019 Sou Bunnbu <iyzsong@member.fsf.org>
15;;; Copyright © 2019 Alex Griffin <a@ajgrf.com> 15;;; Copyright © 2019 Alex Griffin <a@ajgrf.com>
16;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re> 16;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
@@ -80,6 +80,7 @@
80 #:use-module (srfi srfi-9) 80 #:use-module (srfi srfi-9)
81 #:use-module (srfi srfi-26) 81 #:use-module (srfi srfi-26)
82 #:use-module (srfi srfi-43) 82 #:use-module (srfi srfi-43)
83 #:use-module (ice-9 format)
83 #:use-module (ice-9 match) 84 #:use-module (ice-9 match)
84 #:use-module (ice-9 string-fun) 85 #:use-module (ice-9 string-fun)
85 #:use-module (json) 86 #:use-module (json)
@@ -258,6 +259,7 @@
258 nftables-configuration 259 nftables-configuration
259 nftables-configuration? 260 nftables-configuration?
260 nftables-configuration-package 261 nftables-configuration-package
262 nftables-configuration-debug-levels
261 nftables-configuration-ruleset 263 nftables-configuration-ruleset
262 %default-nftables-ruleset 264 %default-nftables-ruleset
263 265
@@ -2279,12 +2281,12 @@ COMMIT
2279 (compose list iptables-shepherd-service)))))) 2281 (compose list iptables-shepherd-service))))))
2280 2282
2281;;; 2283;;;
2282;;; nftables 2284;;; nftables.
2283;;; 2285;;;
2284 2286
2285(define %default-nftables-ruleset 2287(define %default-nftables-ruleset
2286 (plain-file "nftables.conf" 2288 (plain-file "nftables.conf" "\
2287 "# A simple and safe firewall 2289# A simple and safe firewall
2288table inet filter { 2290table inet filter {
2289 chain input { 2291 chain input {
2290 type filter hook input priority 0; policy drop; 2292 type filter hook input priority 0; policy drop;
@@ -2320,25 +2322,44 @@ table inet filter {
2320} 2322}
2321")) 2323"))
2322 2324
2323(define-record-type* <nftables-configuration> 2325(define (debug-level? x)
2324 nftables-configuration 2326 (member x '(scanner parser eval netlink mnl proto-ctx segtree all)))
2325 make-nftables-configuration 2327
2326 nftables-configuration? 2328(define list-of-debug-levels?
2327 (package nftables-configuration-package 2329 (list-of debug-level?))
2328 (default nftables)) 2330
2329 (ruleset nftables-configuration-ruleset ; file-like object 2331(define-maybe/no-serialization list-of-debug-levels)
2330 (default %default-nftables-ruleset))) 2332
2333(define-configuration/no-serialization nftables-configuration
2334 (package
2335 (file-like nftables)
2336 "The @code{nftables} package to use.")
2337 (debug-levels
2338 maybe-list-of-debug-levels
2339 "A list of debug levels, for enabling debugging output. Valid debug level values
2340are the @samp{scanner}, @samp{parser}, @samp{eval}, @samp{netlink},
2341@samp{mnl}, @samp{proto-ctx}, @samp{segtree} or @samp{all} symbols.")
2342 (ruleset
2343 (file-like %default-nftables-ruleset)
2344 "A file-like object containing the complete nftables ruleset. The default
2345ruleset rejects all incoming connections except those to TCP port 22, with
2346connections from the loopback interface are allowed."))
2331 2347
2332(define (nftables-shepherd-service config) 2348(define (nftables-shepherd-service config)
2333 (match-record config <nftables-configuration> 2349 (match-record config <nftables-configuration>
2334 (package ruleset) 2350 (package debug-levels ruleset)
2335 (let ((nft (file-append package "/sbin/nft"))) 2351 (let ((nft (file-append package "/sbin/nft")))
2336 (shepherd-service 2352 (shepherd-service
2337 (documentation "Packet filtering and classification") 2353 (documentation "Packet filtering and classification")
2338 (actions (list (shepherd-configuration-action ruleset))) 2354 (actions (list (shepherd-configuration-action ruleset)))
2339 (provision '(nftables)) 2355 (provision '(nftables))
2340 (start #~(lambda _ 2356 (start #~(lambda _
2341 (invoke #$nft "--file" #$ruleset))) 2357 (invoke #$nft
2358 #$@(if (maybe-value-set? debug-levels)
2359 (list (format #f "--debug=~{~a~^,~}"
2360 debug-levels))
2361 #~())
2362 "--file" #$ruleset)))
2342 (stop #~(lambda _ 2363 (stop #~(lambda _
2343 (invoke #$nft "flush" "ruleset"))))))) 2364 (invoke #$nft "flush" "ruleset")))))))
2344 2365