From debbugs-submit-bounces@debbugs.gnu.org Mon Jul 12 17:05:56 2021 Received: (at submit) by debbugs.gnu.org; 12 Jul 2021 21:05:57 +0000 Received: from localhost ([127.0.0.1]:38959 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m337k-0001wT-MA for submit@debbugs.gnu.org; Mon, 12 Jul 2021 17:05:56 -0400 Received: from lists.gnu.org ([209.51.188.17]:50750) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m337i-0001wJ-Je for submit@debbugs.gnu.org; Mon, 12 Jul 2021 17:05:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42992) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m337i-0005qm-Dz for guix-patches@gnu.org; Mon, 12 Jul 2021 17:05:54 -0400 Received: from relay4-d.mail.gandi.net ([217.70.183.196]:59109) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m337g-0004Dz-2e for guix-patches@gnu.org; Mon, 12 Jul 2021 17:05:53 -0400 Received: (Authenticated sender: brice@waegenei.re) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id A05F6E000A; Mon, 12 Jul 2021 21:05:47 +0000 (UTC) From: Brice Waegeneire To: guix-patches@gnu.org Subject: [PATCH 0/2] services: nftables: Make it extandable Date: Mon, 12 Jul 2021 23:05:43 +0200 Message-Id: <20210712210543.16598-1-brice@waegenei.re> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=217.70.183.196; envelope-from=brice@waegenei.re; helo=relay4-d.mail.gandi.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.6 (-) X-Debbugs-Envelope-To: submit Cc: iyzsong@member.fsf.org, solene@perso.pw X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.6 (--) This patchset make "nftables-service-type" extendable, so other services could open port. I wrote this to be able to use libvirt with nftables (another patch is comming about that) like this: --8<---------------cut here---------------start------------->8--- (simple-service 'nftables-libvirt nftables-service-type (list "# Libvirt? add rule inet guix forward ct state established,related accept add rule inet guix forward iifname \"virbr*\" accept add chain inet guix libvirt insert rule inet guix input iifname \"virbr*\" jump libvirt insert rule inet guix libvirt udp dport 53 accept insert rule inet guix libvirt tcp dport 53 accept insert rule inet guix libvirt udp dport 67 accept ")) --8<---------------cut here---------------end--------------->8--- So this should make it possible to implement Solene's "simple-firewall-service"¹ by simply extending "nftables-service-type". Also, now, stopping nftables only remove the "guix" table so other software can use their own namespaces without being purged when that service is stopped. WDYT? ¹ Brice Waegeneire (2): services: nftables: Only manage delete our own table. services: nftables: Make it extendable. gnu/services/networking.scm | 51 +++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 10 deletions(-) -- 2.31.1 From debbugs-submit-bounces@debbugs.gnu.org Mon Jul 12 17:08:38 2021 Received: (at submit) by debbugs.gnu.org; 12 Jul 2021 21:08:38 +0000 Received: from localhost ([127.0.0.1]:38965 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m33AI-00020w-3J for submit@debbugs.gnu.org; Mon, 12 Jul 2021 17:08:38 -0400 Received: from lists.gnu.org ([209.51.188.17]:53668) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m33AF-00020i-Fp for submit@debbugs.gnu.org; Mon, 12 Jul 2021 17:08:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43450) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m33AF-00083S-As for guix-patches@gnu.org; Mon, 12 Jul 2021 17:08:31 -0400 Received: from relay10.mail.gandi.net ([217.70.178.230]:33731) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m33AD-0005xc-I2 for guix-patches@gnu.org; Mon, 12 Jul 2021 17:08:31 -0400 Received: (Authenticated sender: brice@waegenei.re) by relay10.mail.gandi.net (Postfix) with ESMTPSA id C841F240003; Mon, 12 Jul 2021 21:08:26 +0000 (UTC) From: Brice Waegeneire To: guix-patches@gnu.org Subject: [PATCH 1/2] services: nftables: Only manage delete our own table. Date: Mon, 12 Jul 2021 23:08:22 +0200 Message-Id: <20210712210823.16987-1-brice@waegenei.re> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210712210543.16598-1-brice@waegenei.re> References: <20210712210543.16598-1-brice@waegenei.re> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=217.70.178.230; envelope-from=brice@waegenei.re; helo=relay10.mail.gandi.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.6 (-) X-Debbugs-Envelope-To: submit Cc: iyzsong@member.fsf.org, solene@perso.pw X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.3 (/) * gnu/services/networking.scm (%default-nftables-ruleset): Rename table from "forward" to "guix". Clear table before applying before setting it up. (nftables-shepherd-service): Don't flush all the table, just delete our own. --- gnu/services/networking.scm | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/gnu/services/networking.scm b/gnu/services/networking.scm index 1ae58041d3..3058c14caf 100644 --- a/gnu/services/networking.scm +++ b/gnu/services/networking.scm @@ -13,7 +13,7 @@ ;;; Copyright © 2019, 2021 Maxim Cournoyer ;;; Copyright © 2019 Sou Bunnbu ;;; Copyright © 2019 Alex Griffin -;;; Copyright © 2020 Brice Waegeneire +;;; Copyright © 2020, 2021 Brice Waegeneire ;;; Copyright © 2021 Oleg Pykhalov ;;; Copyright © 2021 Christopher Lemmer Webber ;;; Copyright © 2021 Maxime Devos @@ -1717,7 +1717,12 @@ COMMIT (define %default-nftables-ruleset (plain-file "nftables.conf" "# A simple and safe firewall -table inet filter { + +# Start with our table clean of previous state +add table inet guix +delete table inet guix + +table inet guix { chain input { type filter hook input priority 0; policy drop; @@ -1768,7 +1773,7 @@ table inet filter { (start #~(lambda _ (invoke #$nft "--file" #$ruleset))) (stop #~(lambda _ - (invoke #$nft "flush" "ruleset")))))))) + (invoke #$nft "delete" "table" "inet" "guix")))))))) (define nftables-service-type (service-type -- 2.31.1 From debbugs-submit-bounces@debbugs.gnu.org Mon Jul 12 17:08:38 2021 Received: (at submit) by debbugs.gnu.org; 12 Jul 2021 21:08:38 +0000 Received: from localhost ([127.0.0.1]:38967 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m33AM-000213-8K for submit@debbugs.gnu.org; Mon, 12 Jul 2021 17:08:38 -0400 Received: from lists.gnu.org ([209.51.188.17]:53672) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m33AG-00020k-H8 for submit@debbugs.gnu.org; Mon, 12 Jul 2021 17:08:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43460) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m33AG-00083X-7Z for guix-patches@gnu.org; Mon, 12 Jul 2021 17:08:32 -0400 Received: from relay4-d.mail.gandi.net ([217.70.183.196]:48859) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m33AD-0005yB-ND for guix-patches@gnu.org; Mon, 12 Jul 2021 17:08:31 -0400 Received: (Authenticated sender: brice@waegenei.re) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id CF855E0003; Mon, 12 Jul 2021 21:08:27 +0000 (UTC) From: Brice Waegeneire To: guix-patches@gnu.org Subject: [PATCH 2/2] services: nftables: Make it extendable. Date: Mon, 12 Jul 2021 23:08:23 +0200 Message-Id: <20210712210823.16987-2-brice@waegenei.re> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210712210543.16598-1-brice@waegenei.re> References: <20210712210543.16598-1-brice@waegenei.re> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=217.70.183.196; envelope-from=brice@waegenei.re; helo=relay4-d.mail.gandi.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.6 (-) X-Debbugs-Envelope-To: submit Cc: iyzsong@member.fsf.org, solene@perso.pw X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.6 (--) * gnu/services/networking.scm (%default-nftables-rules): New variable… (define-record-type): …replace %default-nftables-ruleset with it. (%default-nftables-ruleset): Deprecate it. (nftables-ruleset): New procedure… (nftables-shepherd-service): …use it. (nftables-service-type): Make it extendable. --- gnu/services/networking.scm | 40 ++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/gnu/services/networking.scm b/gnu/services/networking.scm index 3058c14caf..53c06dcfed 100644 --- a/gnu/services/networking.scm +++ b/gnu/services/networking.scm @@ -1714,9 +1714,8 @@ COMMIT ;;; nftables ;;; -(define %default-nftables-ruleset - (plain-file "nftables.conf" - "# A simple and safe firewall +(define %default-nftables-rules + "# A simple and safe firewall # Start with our table clean of previous state add table inet guix @@ -1752,7 +1751,11 @@ table inet guix { type filter hook output priority 0; policy accept; } } -")) +") + +(define-deprecated %default-nftables-ruleset + %default-nftables-rules + (plain-file "nftables.conf" %default-nftables-rules)) (define-record-type* nftables-configuration @@ -1760,13 +1763,28 @@ table inet guix { nftables-configuration? (package nftables-configuration-package (default nftables)) - (ruleset nftables-configuration-ruleset ; file-like object - (default %default-nftables-ruleset))) + ; file-like object | list of strings and file-like objects + (ruleset nftables-configuration-ruleset + (default (list %default-nftables-rules)))) + +(define (nftables-ruleset ruleset) + (if (file-like? ruleset) + ruleset + (apply mixed-text-file + `("nftables.conf" + ,@(fold-right + (lambda (rule result) + (if (file-like? rule) + (append (list "include \"" rule "\"\n") result) + (append (list rule "\n") result))) + '() + ruleset))))) (define nftables-shepherd-service (match-lambda (($ package ruleset) - (let ((nft (file-append package "/sbin/nft"))) + (let ((nft (file-append package "/sbin/nft")) + (ruleset (nftables-ruleset ruleset))) (shepherd-service (documentation "Packet filtering and classification") (provision '(nftables)) @@ -1785,6 +1803,14 @@ table inet guix { (compose list nftables-shepherd-service)) (service-extension profile-service-type (compose list nftables-configuration-package)))) + (compose concatenate) + (extend (lambda (config additional-rules) + (let ((ruleset (nftables-configuration-ruleset config))) + (nftables-configuration + (inherit config) + (ruleset (if (list? ruleset) + (append ruleset additional-rules) + (cons* ruleset additional-rules))))))) (default-value (nftables-configuration)))) -- 2.31.1