From patchwork Thu May 18 17:48:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Maxim Cournoyer X-Patchwork-Id: 50130 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id CA61A27BBEA; Thu, 18 May 2023 18:49:44 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id A903627BBE2 for ; Thu, 18 May 2023 18:49:42 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pzhkV-0000yf-FY; Thu, 18 May 2023 13:49:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzhkM-0000xM-Hj for guix-patches@gnu.org; Thu, 18 May 2023 13:49:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pzhkM-0000zy-7y for guix-patches@gnu.org; Thu, 18 May 2023 13:49:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pzhkM-0001fY-3L for guix-patches@gnu.org; Thu, 18 May 2023 13:49:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63402] [PATCH v4 1/4] services: wireguard: Implement a dynamic IP monitoring feature. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 18 May 2023 17:49:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63402 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63402@debbugs.gnu.org Cc: Maxim Cournoyer Received: via spool by 63402-submit@debbugs.gnu.org id=B63402.16844321406391 (code B ref 63402); Thu, 18 May 2023 17:49:02 +0000 Received: (at 63402) by debbugs.gnu.org; 18 May 2023 17:49:00 +0000 Received: from localhost ([127.0.0.1]:54394 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzhkJ-0001ey-G0 for submit@debbugs.gnu.org; Thu, 18 May 2023 13:49:00 -0400 Received: from mail-qv1-f46.google.com ([209.85.219.46]:57712) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzhkG-0001eV-UV for 63402@debbugs.gnu.org; Thu, 18 May 2023 13:48:57 -0400 Received: by mail-qv1-f46.google.com with SMTP id 6a1803df08f44-623914a4bf0so5058896d6.3 for <63402@debbugs.gnu.org>; Thu, 18 May 2023 10:48:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684432131; x=1687024131; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Fw4OeDUUSA23vGlOSbp2JVBYyPOhBNJ4PLtJ1rPJa/E=; b=d8D/ZDtSvGWyT67KgsXOkw4+ZC8y7lM78DYshQxETFTbz/7Y9wrbNo8cEGIOJTp16i glrm/4Ywr8TVztUrHpJoVdUlXNPXJ3xi0rN2ylDcZGcgEr9KLum9IPgaszr4q0iQ2rFx 45O9jLNxvjwa4FxEta56jaADW04utq9sjO8Lp/7a3btoTGkzVnP+5mTjozaLQ5NeUFRt nmfDn1IyZvclVtZSK84+gODvGf8nEbYArLY+PYlmPMp3Od+hHRHYFEleofkQKQcvHY0S AI9SkGMJ3yc0ceOk9lKAcw0nXHPl9nfhAv4UKLDO3qkccRFE+Zum7r/FVuiYSXJTLSjT Jqxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684432131; x=1687024131; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Fw4OeDUUSA23vGlOSbp2JVBYyPOhBNJ4PLtJ1rPJa/E=; b=aXWSAal0RpVcyYe1TfRhxkqeSF/s8xfaGnc/HLuOZZW47q+OHbLK/3N+r7GHVTmQSs 8MGLt2jI9Yn2Fdmfs7IZxxqaCTQAxAqJ7IsFZHbCRQsymeQl742G4J+RUKDsK2Upi/f5 fbAemSjG5vn3+IoIRBwi3GI9+SeljcNOyGf+PSUvrNdQ47a0ZRrkP3Ebtw1UUYi/I5ny +DEm2OYyuYTqUNmanCOAMx7UoTUypFi/E8dZsLh/kOuMNSDd5FyzWKNaN6gOxByVjt03 BQ80mdlPl0awlhXwJrGFJXXh+gucXlz4W6116FdDcImE9HqCU2va8NMKzpex+6liXswO jqjw== X-Gm-Message-State: AC+VfDz+SmSABN2UIoobU/fxecE9DD6HRbJsYTgsC3D3z6J3BSYD2d8m oA49uzCIDaNodRw7stSPhwCtsVhcuZUTVQ== X-Google-Smtp-Source: ACHHUZ4et5zCNAsCW7ZMuxPKXDKmHE8aCbaT+ofSyY4+gJ6RQ0HV4Ifq5nIMlZSU0wT2Dl3raTPOXQ== X-Received: by 2002:a05:6214:401d:b0:5f1:5ed3:dd82 with SMTP id kd29-20020a056214401d00b005f15ed3dd82mr543888qvb.48.1684432131095; Thu, 18 May 2023 10:48:51 -0700 (PDT) Received: from localhost.localdomain (dsl-150-33.b2b2c.ca. [66.158.150.33]) by smtp.gmail.com with ESMTPSA id ml7-20020a056214584700b0061a0f7fb340sm689006qvb.6.2023.05.18.10.48.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 10:48:50 -0700 (PDT) From: Maxim Cournoyer Date: Thu, 18 May 2023 13:48:39 -0400 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * gnu/services/vpn.scm () [monitor-ips?, monitor-ips-internal]: New fields. * gnu/services/vpn.scm (define-with-source): New syntax. (wireguard-service-name, strip-port/maybe) (ipv4-address?, ipv6-address?, host-name?) (endpoint-host-names): New procedure. (wireguard-monitoring-jobs): Likewise. (wireguard-service-type): Register it. * tests/services/vpn.scm: New file. * Makefile.am (SCM_TESTS): Register it. * doc/guix.texi (VPN Services): Update doc. --- Makefile.am | 1 + doc/guix.texi | 17 ++++- gnu/services/vpn.scm | 147 +++++++++++++++++++++++++++++++++++++++-- tests/services/vpn.scm | 83 +++++++++++++++++++++++ 4 files changed, 242 insertions(+), 6 deletions(-) create mode 100644 tests/services/vpn.scm diff --git a/Makefile.am b/Makefile.am index 8b7bb4772d..e1cb1083fc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -557,6 +557,7 @@ SCM_TESTS = \ tests/services/lightdm.scm \ tests/services/linux.scm \ tests/services/telephony.scm \ + tests/services/vpn.scm \ tests/sets.scm \ tests/size.scm \ tests/status.scm \ diff --git a/doc/guix.texi b/doc/guix.texi index 60972f408d..ef96d064ed 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -32591,9 +32591,22 @@ VPN Services @item @code{dns} (default: @code{#f}) The DNS server(s) to announce to VPN clients via DHCP. +@item @code{monitor-ips?} (default: @code{#f}) +@cindex Dynamic IP, with Wireguard +@cindex dyndns, usage with Wireguard +Whether to monitor the resolved Internet addresses (IPs) of the +endpoints of the configured peers, resetting the peer endpoints using an +IP address that no longer correspond to their freshly resolved host +name. Set this to @code{#t} if one or more endpoints use host names +provided by a dynamic DNS service to keep the sessions alive. + +@item @code{monitor-ips-internal} (default: @code{'(next-minute (range 0 60 5))}) +The time interval at which the IP monitoring job should run, provided as +an mcron time specification (@pxref{Guile Syntax,,,mcron}). + @item @code{private-key} (default: @code{"/etc/wireguard/private.key"}) -The private key file for the interface. It is automatically generated if -the file does not exist. +The private key file for the interface. It is automatically generated +if the file does not exist. @item @code{peers} (default: @code{'()}) The authorized peers on this interface. This is a list of diff --git a/gnu/services/vpn.scm b/gnu/services/vpn.scm index a884d71eb2..c11faed879 100644 --- a/gnu/services/vpn.scm +++ b/gnu/services/vpn.scm @@ -11,6 +11,7 @@ ;;; Copyright © 2021 Nathan Dehnel ;;; Copyright © 2022 Cameron V Chaparro ;;; Copyright © 2022 Timo Wilken +;;; Copyright © 2023 Maxim Cournoyer ;;; ;;; This file is part of GNU Guix. ;;; @@ -31,10 +32,12 @@ (define-module (gnu services vpn) #:use-module (gnu services) #:use-module (gnu services configuration) #:use-module (gnu services dbus) + #:use-module (gnu services mcron) #:use-module (gnu services shepherd) #:use-module (gnu system shadow) #:use-module (gnu packages admin) #:use-module (gnu packages vpn) + #:use-module (guix modules) #:use-module (guix packages) #:use-module (guix records) #:use-module (guix gexp) @@ -73,6 +76,8 @@ (define-module (gnu services vpn) wireguard-configuration-addresses wireguard-configuration-port wireguard-configuration-dns + wireguard-configuration-monitor-ips? + wireguard-configuration-monitor-ips-interval wireguard-configuration-private-key wireguard-configuration-peers wireguard-configuration-pre-up @@ -741,6 +746,10 @@ (define-record-type* (default '())) (dns wireguard-configuration-dns ;list of strings (default #f)) + (monitor-ips? wireguard-configuration-monitor-ips? ;boolean + (default #f)) + (monitor-ips-interval wireguard-configuration-monitor-ips-interval + (default '(next-minute (range 0 60 5)))) ;string | list (pre-up wireguard-configuration-pre-up ;list of strings (default '())) (post-up wireguard-configuration-post-up ;list of strings @@ -871,6 +880,56 @@ (define (wireguard-activation config) (chmod #$private-key #o400) (close-pipe pipe)))))) +;;; XXX: Copied from (guix scripts pack), changing define to define*. +(define-syntax-rule (define-with-source (variable args ...) body body* ...) + "Bind VARIABLE to a procedure accepting ARGS defined as BODY, also setting +its source property." + (begin + (define* (variable args ...) + body body* ...) + (eval-when (load eval) + (set-procedure-property! variable 'source + '(define* (variable args ...) body body* ...))))) + +(define (wireguard-service-name interface) + "Return the WireGuard service name (a symbol) configured to use INTERFACE." + (symbol-append 'wireguard- (string->symbol interface))) + +(define-with-source (strip-port/maybe endpoint #:key ipv6?) + "Strip the colon and port, if present in ENDPOINT, a string." + (if ipv6? + (if (string-prefix? "[" endpoint) + (first (string-split (string-drop endpoint 1) #\])) ;ipv6 + endpoint) + (first (string-split endpoint #\:)))) ;ipv4 + +(define (ipv4-address? str) + "Return true if STR denotes an IPv4 address." + (false-if-exception + (->bool (inet-pton AF_INET (strip-port/maybe str))))) + +(define (ipv6-address? str) + "Return true if STR denotes an IPv6 address." + (false-if-exception + (->bool (inet-pton AF_INET6 (strip-port/maybe str #:ipv6? #t))))) + +(define (host-name? name) + "Predicate to check whether NAME is a host name, i.e. not an IP address." + (not (or (ipv6-address? name) (ipv4-address? name)))) + +(define (endpoint-host-names peers) + "Return an association list of endpoint host names keyed by their peer +public key, if any." + (reverse + (fold (lambda (peer host-names) + (let ((public-key (wireguard-peer-public-key peer)) + (endpoint (wireguard-peer-endpoint peer))) + (if (and endpoint (host-name? endpoint)) + (cons (cons public-key endpoint) host-names) + host-names))) + '() + peers))) + (define (wireguard-shepherd-service config) (match-record config (wireguard interface) @@ -878,9 +937,7 @@ (define (wireguard-shepherd-service config) (config (wireguard-configuration-file config))) (list (shepherd-service (requirement '(networking)) - (provision (list - (symbol-append 'wireguard- - (string->symbol interface)))) + (provision (list (wireguard-service-name interface))) (start #~(lambda _ (invoke #$wg-quick "up" #$config))) (stop #~(lambda _ @@ -888,6 +945,86 @@ (define (wireguard-shepherd-service config) #f)) ;stopped! (documentation "Run the Wireguard VPN tunnel")))))) +(define (wireguard-monitoring-jobs config) + ;; Loosely based on WireGuard's own 'reresolve-dns.sh' shell script (see: + ;; https://raw.githubusercontent.com/WireGuard/wireguard-tools/ + ;; master/contrib/reresolve-dns/reresolve-dns.sh). + (match-record config + (interface monitor-ips? monitor-ips-interval peers) + (let ((host-names (endpoint-host-names peers))) + (if monitor-ips? + (if (null? host-names) + (begin + (warn "monitor-ips? is #t but no host name to monitor") + '()) + ;; The mcron monitor job may be a string or a list; ungexp strips + ;; one quote level, which must be added back when a list is + ;; provided. + (list + #~(job + (if (string? #$monitor-ips-interval) + #$monitor-ips-interval + '#$monitor-ips-interval) + #$(program-file + (format #f "wireguard-~a-monitoring" interface) + (with-imported-modules (source-module-closure + '((gnu services herd) + (guix build utils))) + #~(begin + (use-modules (gnu services herd) + (guix build utils) + (ice-9 popen) + (ice-9 match) + (ice-9 textual-ports) + (srfi srfi-1) + (srfi srfi-26)) + + (define (resolve-host name) + "Return the IP address resolved from NAME." + (let* ((ai (car (getaddrinfo name))) + (sa (addrinfo:addr ai))) + (inet-ntop (sockaddr:fam sa) + (sockaddr:addr sa)))) + + (define wg #$(file-append wireguard-tools "/bin/wg")) + + #$(procedure-source strip-port/maybe) + + (define service-name '#$(wireguard-service-name + interface)) + + (when (start-service service-name) + (let* ((pipe (open-pipe* OPEN_READ wg "show" + #$interface "endpoints")) + (lines (string-split (get-string-all pipe) + #\newline)) + ;; IPS is an association list mapping + ;; public keys to IP addresses. + (ips (map (match-lambda + ((public-key ip) + (cons public-key + (strip-port/maybe ip)))) + (map (cut string-split <> #\tab) + (remove string-null? + lines))))) + (close-pipe pipe) + (for-each + (match-lambda + ((key . host-name) + (let ((resolved-ip (resolve-host + (strip-port/maybe + host-name))) + (current-ip (assoc-ref ips key))) + (unless (string=? resolved-ip current-ip) + (format #t "resetting `~a' peer \ +endpoint to `~a' due to stale IP (`~a' instead of `~a')~%" + key host-name + current-ip resolved-ip) + (invoke wg "set" #$interface "peer" key + "endpoint" host-name))))) + '#$host-names))))))))) + '())))) ;monitor-ips? is #f + (define wireguard-service-type (service-type (name 'wireguard) @@ -898,6 +1035,8 @@ (define wireguard-service-type wireguard-activation) (service-extension profile-service-type (compose list - wireguard-configuration-wireguard)))) + wireguard-configuration-wireguard)) + (service-extension mcron-service-type + wireguard-monitoring-jobs))) (description "Set up Wireguard @acronym{VPN, Virtual Private Network} tunnels."))) diff --git a/tests/services/vpn.scm b/tests/services/vpn.scm new file mode 100644 index 0000000000..a7f4bec26b --- /dev/null +++ b/tests/services/vpn.scm @@ -0,0 +1,83 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2023 Maxim Cournoyer +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (tests services vpn) + #:use-module (gnu packages vpn) + #:use-module (gnu services vpn) + #:use-module (guix gexp) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-64)) + +;;; Commentary: +;;; +;;; Unit tests for the (gnu services vpn) module. +;;; +;;; Code: + +;;; Access some internals for whitebox testing. +(define ipv4-address? (@@ (gnu services vpn) ipv4-address?)) +(define ipv6-address? (@@ (gnu services vpn) ipv6-address?)) +(define host-name? (@@ (gnu services vpn) host-name?)) +(define endpoint-host-names + (@@ (gnu services vpn) endpoint-host-names)) + +(test-begin "vpn-services") + +(test-assert "ipv4-address?" + (every ipv4-address? + (list "192.95.5.67:1234" + "10.0.0.1"))) + +(test-assert "ipv6-address?" + (every ipv6-address? + (list "[2607:5300:60:6b0::c05f:543]:2468" + "2607:5300:60:6b0::c05f:543" + "2345:0425:2CA1:0000:0000:0567:5673:23b5" + "2345:0425:2CA1::0567:5673:23b5"))) + +(define %wireguard-peers + (list (wireguard-peer + (name "dummy1") + (public-key "VlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XjoalC8=") + (endpoint "some.dynamic-dns.service:53281") + (allowed-ips '())) + (wireguard-peer + (name "dummy2") + (public-key "AlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XgoalC9=") + (endpoint "example.org") + (allowed-ips '())) + (wireguard-peer + (name "dummy3") + (public-key "BlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XgoalC7=") + (endpoint "10.0.0.7:7777") + (allowed-ips '())) + (wireguard-peer + (name "dummy4") + (public-key "ClesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XgoalC6=") + (endpoint "[2345:0425:2CA1::0567:5673:23b5]:44444") + (allowed-ips '())))) + +(test-equal "endpoint-host-names" + '(("VlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XjoalC8=" . + "some.dynamic-dns.service:53281") + ("AlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XgoalC9=" . + "example.org")) + (endpoint-host-names %wireguard-peers)) + +(test-end "vpn-services") From patchwork Thu May 18 17:48:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Cournoyer X-Patchwork-Id: 50131 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 4B9FD27BBE9; Thu, 18 May 2023 18:50:29 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 2A32F27BBE2 for ; Thu, 18 May 2023 18:50:27 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pzhlO-0001IN-1E; Thu, 18 May 2023 13:50:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzhlL-0001GW-Dg for guix-patches@gnu.org; Thu, 18 May 2023 13:50:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pzhlL-0001DL-4Z for guix-patches@gnu.org; Thu, 18 May 2023 13:50:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pzhlL-0001hs-17 for guix-patches@gnu.org; Thu, 18 May 2023 13:50:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63402] [PATCH v4 2/4] services: wireguard: Clean-up configuration file serializer. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 18 May 2023 17:50:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63402 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63402@debbugs.gnu.org Cc: Maxim Cournoyer Received: via spool by 63402-submit@debbugs.gnu.org id=B63402.16844321446453 (code B ref 63402); Thu, 18 May 2023 17:50:02 +0000 Received: (at 63402) by debbugs.gnu.org; 18 May 2023 17:49:04 +0000 Received: from localhost ([127.0.0.1]:54403 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzhkN-0001fu-LX for submit@debbugs.gnu.org; Thu, 18 May 2023 13:49:04 -0400 Received: from mail-qk1-f178.google.com ([209.85.222.178]:54517) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzhkK-0001eg-37 for 63402@debbugs.gnu.org; Thu, 18 May 2023 13:49:00 -0400 Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-75783ac48e7so212798685a.0 for <63402@debbugs.gnu.org>; Thu, 18 May 2023 10:49:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684432134; x=1687024134; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mweOIvBXYiHwoWCpNrNTXmRvqevL4WPkYv5E8K/bjTo=; b=nmqyDtFDiF7BUkFBXe3h2XYONBsNAK7tZW94F+vU4UGhuwRR1bC0DcLjd+/GfFZD3f rrgDAPWgiD/yeKijugJ9fJQdem81N9kNeBSkVI2NTXDECOyX8Bqzel3mtXAO6TAmvaDk OSCz/UO+ppjPZrz0d5PggwLxiYskFOFCm7s5kI6G49nW1dEEax8ulMLfFHAlA7pzqdv5 WF3FKNVlWC8OqIULxMqJi27xLrzu9mG67nKvsW1YsTUGeNmQ+Jj7Z85DWvLbH8CA0N/Q ZKAZD+P6FW9CT/ibQAh4eUwBOFqvJ982wwtxGLpG0kJtZ1BO/mqqJ6zLt5N/7bikYkDs 40Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684432134; x=1687024134; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mweOIvBXYiHwoWCpNrNTXmRvqevL4WPkYv5E8K/bjTo=; b=YjvFmmsTyawBj7yky7u7WIJUu5B7SFQK3V8o4GjiXxiXCBff41nX/hzQFgjJ03Zncx MxKLNnRv+R6YarKzMI9OzycsOTcpx9hFW6WCeDvxGye893z/45U4j124XvACrl4Rk1YV bdxw6wHGvEdZTcs6LxdJPumKhBVlCsvF/FH5aUktvaNaVdlMpvHVQ8AvgrIlRvAJTXr6 e1VDr2bdWmeQMnBRkHOVzxioKpdPVnC6rCdE4BY+CEw3Taw+voLgCa9jtp85cD3WSrSB VoQJPOESVRBag1Z9ZHhaCe3bxKpHV/rEiCiV41rWnOQnd49P6r4EOy0/jUHC9tjf42N6 eL5A== X-Gm-Message-State: AC+VfDx4q/J3X5FcS2D8mFLbJ3A0oW3QJKp3LK5t9Xq3FYP9EVAe30Q8 S8VtlxILJJdxzW+Jqex5ejPPOsmD0TLnpg== X-Google-Smtp-Source: ACHHUZ4sSHZLxA0GdN9ug6gGZoZcmZ3iihgBTby8hL80RIifc1X+Xk6sqAvVzQSfxqc2gxt7Jm8y3Q== X-Received: by 2002:a05:6214:411c:b0:5ef:67b9:8d37 with SMTP id kc28-20020a056214411c00b005ef67b98d37mr619645qvb.13.1684432134308; Thu, 18 May 2023 10:48:54 -0700 (PDT) Received: from localhost.localdomain (dsl-150-33.b2b2c.ca. [66.158.150.33]) by smtp.gmail.com with ESMTPSA id ml7-20020a056214584700b0061a0f7fb340sm689006qvb.6.2023.05.18.10.48.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 10:48:54 -0700 (PDT) From: Maxim Cournoyer Date: Thu, 18 May 2023 13:48:40 -0400 Message-Id: <235e307060c61947158361f4ef4eb40df72c79de.1684431342.git.maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches Previously, the generated config file would contain arbitrary whitespace that made it look ugly. * gnu/services/vpn.scm () [dns]: Change default value from #f to '(). (wireguard-configuration-file): Use match-record. Format each line individually, assembling the lines at the end to avoid extraneous white space. * doc/guix.texi (VPN Services): Update doc. --- doc/guix.texi | 2 +- gnu/services/vpn.scm | 119 ++++++++++++++++--------------------------- 2 files changed, 46 insertions(+), 75 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index ef96d064ed..b61a2ceb5b 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -32588,7 +32588,7 @@ VPN Services @item @code{port} (default: @code{51820}) The port on which to listen for incoming connections. -@item @code{dns} (default: @code{#f}) +@item @code{dns} (default: @code{'())}) The DNS server(s) to announce to VPN clients via DHCP. @item @code{monitor-ips?} (default: @code{#f}) diff --git a/gnu/services/vpn.scm b/gnu/services/vpn.scm index c11faed879..a34889a6cc 100644 --- a/gnu/services/vpn.scm +++ b/gnu/services/vpn.scm @@ -44,6 +44,7 @@ (define-module (gnu services vpn) #:use-module (guix i18n) #:use-module (guix deprecation) #:use-module (srfi srfi-1) + #:use-module (ice-9 format) #:use-module (ice-9 match) #:use-module (ice-9 regex) #:export (openvpn-client-service ; deprecated @@ -745,7 +746,7 @@ (define-record-type* (peers wireguard-configuration-peers ;list of (default '())) (dns wireguard-configuration-dns ;list of strings - (default #f)) + (default '())) (monitor-ips? wireguard-configuration-monitor-ips? ;boolean (default #f)) (monitor-ips-interval wireguard-configuration-monitor-ips-interval @@ -763,24 +764,15 @@ (define-record-type* (define (wireguard-configuration-file config) (define (peer->config peer) - (let ((name (wireguard-peer-name peer)) - (public-key (wireguard-peer-public-key peer)) - (endpoint (wireguard-peer-endpoint peer)) - (allowed-ips (wireguard-peer-allowed-ips peer)) - (keep-alive (wireguard-peer-keep-alive peer))) - (format #f "[Peer] #~a -PublicKey = ~a -AllowedIPs = ~a -~a~a" - name - public-key - (string-join allowed-ips ",") - (if endpoint - (format #f "Endpoint = ~a\n" endpoint) - "") - (if keep-alive - (format #f "PersistentKeepalive = ~a\n" keep-alive) - "\n")))) + (match-record peer + (name public-key endpoint allowed-ips keep-alive) + (let ((lines (list + (format #f "[Peer] #~a" name) + (format #f "PublicKey = ~a" public-key) + (format #f "AllowedIPs = ~{~a~^, ~}" allowed-ips) + (format #f "~@[Endpoint = ~a~]" endpoint) + (format #f "~@[PersistentKeepalive = ~a~]" keep-alive)))) + (string-join (remove string-null? lines) "\n")))) (define (peers->preshared-keys peer keys) (let ((public-key (wireguard-peer-public-key peer)) @@ -799,65 +791,44 @@ (define (wireguard-configuration-file config) (computed-file "wireguard-config" #~(begin + (use-modules (ice-9 format) + (srfi srfi-1)) + + (define lines + (list + "[Interface]" + #$@(if (null? addresses) + '() + (list (format #f "Address = ~{~a~^, ~}" + addresses))) + (format #f "~@[Table = ~a~]" #$table) + #$@(if (null? pre-up) + '() + (list (format #f "~{PreUp = ~a~%~}" pre-up))) + (format #f "PostUp = ~a set %i private-key ~a\ +~{ peer ~a preshared-key ~a~}" #$(file-append wireguard "/bin/wg") +#$private-key '#$peer-keys) + #$@(if (null? post-up) + '() + (list (format #f "~{PostUp = ~a~%~}" post-up))) + #$@(if (null? pre-down) + '() + (list (format #f "~{PreDown = ~a~%~}" pre-down))) + #$@(if (null? post-down) + '() + (list (format #f "~{PostDown = ~a~%~}" post-down))) + (format #f "~@[ListenPort = ~a~]" #$port) + #$@(if (null? dns) + '() + (list (format #f "~{DNS = ~{~a~^, ~}" dns))))) + (mkdir #$output) (chdir #$output) (call-with-output-file #$config-file (lambda (port) - (let ((format (@ (ice-9 format) format))) - (format port "[Interface] -Address = ~a -~a -~a -PostUp = ~a set %i private-key ~a~{ peer ~a preshared-key ~a~} -~a -~a -~a -~a -~a -~{~a~^~%~}" - #$(string-join addresses ",") - #$(if table - (format #f "Table = ~a" table) - "") - #$(if (null? pre-up) - "" - (string-join - (map (lambda (command) - (format #f "PreUp = ~a" command)) - pre-up) - "\n")) - #$(file-append wireguard "/bin/wg") - #$private-key - '#$peer-keys - #$(if (null? post-up) - "" - (string-join - (map (lambda (command) - (format #f "PostUp = ~a" command)) - post-up) - "\n")) - #$(if (null? pre-down) - "" - (string-join - (map (lambda (command) - (format #f "PreDown = ~a" command)) - pre-down) - "\n")) - #$(if (null? post-down) - "" - (string-join - (map (lambda (command) - (format #f "PostDown = ~a" command)) - post-down) - "\n")) - #$(if port - (format #f "ListenPort = ~a" port) - "") - #$(if dns - (format #f "DNS = ~a" - (string-join dns ",")) - "") - (list #$@peers))))))))) + (format port "~a~%~%~{~a~%~^~%~}" + (string-join (remove string-null? lines) "\n") + '#$peers))))))) (file-append config "/" config-file)))) (define (wireguard-activation config) From patchwork Thu May 18 17:48:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Cournoyer X-Patchwork-Id: 50132 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 2528A27BBE9; Thu, 18 May 2023 18:50:32 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 5F1CD27BBE2 for ; Thu, 18 May 2023 18:50:30 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pzhlP-0001Iz-Lk; Thu, 18 May 2023 13:50:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzhlL-0001H0-Ro for guix-patches@gnu.org; Thu, 18 May 2023 13:50:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pzhlL-0001H5-Id for guix-patches@gnu.org; Thu, 18 May 2023 13:50:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pzhlL-0001i0-Er for guix-patches@gnu.org; Thu, 18 May 2023 13:50:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63402] [PATCH v4 3/4] services: wireguard: Add a 'configuration' action. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 18 May 2023 17:50:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63402 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63402@debbugs.gnu.org Cc: Maxim Cournoyer Received: via spool by 63402-submit@debbugs.gnu.org id=B63402.16844321446459 (code B ref 63402); Thu, 18 May 2023 17:50:03 +0000 Received: (at 63402) by debbugs.gnu.org; 18 May 2023 17:49:04 +0000 Received: from localhost ([127.0.0.1]:54405 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzhkO-0001g1-7L for submit@debbugs.gnu.org; Thu, 18 May 2023 13:49:04 -0400 Received: from mail-qv1-f53.google.com ([209.85.219.53]:48276) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzhkL-0001ep-Ek for 63402@debbugs.gnu.org; Thu, 18 May 2023 13:49:01 -0400 Received: by mail-qv1-f53.google.com with SMTP id 6a1803df08f44-62383441211so10346236d6.0 for <63402@debbugs.gnu.org>; Thu, 18 May 2023 10:49:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684432136; x=1687024136; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=M2Pu/CLLi9flH476knewJVsZPAJxxvAeW8qlmtN6Gyc=; b=DsZaeVfZMeEA5j278Z37YWMC8xPb3VtXfR+MPxGBZh/ZZCBHDrDJOANZJUulehdMZC BRnsA9gqQNLh/n4GhR1l6fQlAtmiITxQdWAleUSYvGphRSnTKynVtNoRK94EMYiIgXql Y3Qy2/UAgiX404X6RKlVGJMMZtBWVik44zIOaCPIB37sb5Lh4D+6yeWlfYdfu9CaO15H 8pHVApZBY7JID9moOGHr9wquoEClqEQ5MR/OGruq4FuByFKxtIowUEWSB07nSlFiWn1X iiDmYJTC+l2LOSG6NZNm/Lv+G/BocxHdjJRKDtNYtStVnNaVZvxMj+jmIFf9h5+PnPU/ Mz0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684432136; x=1687024136; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M2Pu/CLLi9flH476knewJVsZPAJxxvAeW8qlmtN6Gyc=; b=ada8B0CEghU/OqAR3KwBF20K89hIyi1s/6OD+UotWHLzsL3ID2HKC/c9lfwSd0yv33 juDZ0gY6YOw42o7GGkSe6w8IZCuBQ+iTVjvYZyPkgrngDLC0A4PiVj6I3qKDikd/tTA3 R2hiIYl9VSjWZBjP75rksouRzud76o4aMvZaNqjQcd/8U6GDRUFJCWySorPzjN8WCLuc bj3gJ4pHrRgaZJJ58rnDo6QarwBJQGN+Lm0DBOHYSosctHqKnnjqtQSmt+Is5jO2MI8j yjVBEa+HMM2DJBJe9P9OCaEofPhstcJajFIoftYoUwX99miGitY6pHxqHAKDKkIrznSA 5GCg== X-Gm-Message-State: AC+VfDxbXjfGrEGcb2J/CodQTMMy3SJfY7Zw0p9hWejXFW38NzyYwGNs tmJoIjgfZeFcjuTkhNZIPTY7Xy1B/+ZevA== X-Google-Smtp-Source: ACHHUZ6AD8jCKk9mfxzHc3XR7VzS9GcXtlYrsLLK6DJ0BSAWOruRL1VSbN1hlGVXpucGJHra15fmqg== X-Received: by 2002:a05:6214:5182:b0:623:557d:91c7 with SMTP id kl2-20020a056214518200b00623557d91c7mr712081qvb.25.1684432135832; Thu, 18 May 2023 10:48:55 -0700 (PDT) Received: from localhost.localdomain (dsl-150-33.b2b2c.ca. [66.158.150.33]) by smtp.gmail.com with ESMTPSA id ml7-20020a056214584700b0061a0f7fb340sm689006qvb.6.2023.05.18.10.48.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 10:48:55 -0700 (PDT) From: Maxim Cournoyer Date: Thu, 18 May 2023 13:48:41 -0400 Message-Id: <9644e77d72a5fb88c5e788e10d43d152078d9419.1684431342.git.maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * gnu/services/vpn.scm (wireguard-shepherd-service) [actions]: New field. --- gnu/services/vpn.scm | 1 + 1 file changed, 1 insertion(+) diff --git a/gnu/services/vpn.scm b/gnu/services/vpn.scm index a34889a6cc..c3fe82a063 100644 --- a/gnu/services/vpn.scm +++ b/gnu/services/vpn.scm @@ -914,6 +914,7 @@ (define (wireguard-shepherd-service config) (stop #~(lambda _ (invoke #$wg-quick "down" #$config) #f)) ;stopped! + (actions (list (shepherd-configuration-action config))) (documentation "Run the Wireguard VPN tunnel")))))) (define (wireguard-monitoring-jobs config) From patchwork Thu May 18 17:48:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Maxim Cournoyer X-Patchwork-Id: 50133 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 1F8F927BBE9; Thu, 18 May 2023 18:50:43 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 1395D27BBE2 for ; Thu, 18 May 2023 18:50:41 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pzhlU-0001JQ-5Z; Thu, 18 May 2023 13:50:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzhlM-0001HD-Lq for guix-patches@gnu.org; Thu, 18 May 2023 13:50:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pzhlM-0001Kg-0I for guix-patches@gnu.org; Thu, 18 May 2023 13:50:04 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pzhlL-0001i7-SG; Thu, 18 May 2023 13:50:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63402] [PATCH v4 4/4] gnu: linux-libre: Apply wireguard patch fixing keep-alive bug. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: leo@famulari.name, me@tobias.gr, guix-patches@gnu.org Resent-Date: Thu, 18 May 2023 17:50:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63402 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63402@debbugs.gnu.org Cc: Maxim Cournoyer , Leo Famulari , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Leo Famulari , Tobias Geerinckx-Rice Received: via spool by 63402-submit@debbugs.gnu.org id=B63402.16844321496482 (code B ref 63402); Thu, 18 May 2023 17:50:03 +0000 Received: (at 63402) by debbugs.gnu.org; 18 May 2023 17:49:09 +0000 Received: from localhost ([127.0.0.1]:54410 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzhkS-0001gP-TM for submit@debbugs.gnu.org; Thu, 18 May 2023 13:49:09 -0400 Received: from mail-qv1-f45.google.com ([209.85.219.45]:42231) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzhkP-0001f0-0j for 63402@debbugs.gnu.org; Thu, 18 May 2023 13:49:05 -0400 Received: by mail-qv1-f45.google.com with SMTP id 6a1803df08f44-62385de2d40so7466936d6.0 for <63402@debbugs.gnu.org>; Thu, 18 May 2023 10:49:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684432139; x=1687024139; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Qg03Fbiq3xmc+Z90K1Ru5CLtjdVMJPdv1lfOn+t0kwQ=; b=BL3fr38G5vJEqi4sHFmwXgjuPDceHUX76p4Rcd3UgZ2fYdnOEwr+IMp995QXCLBQbv t88gZJE6SIvd+gegN8UXeiDzJLPsEHNohWuoXj2uVsf77zE5yf8pytkihGrSAwK+scEJ UjGSJtTgSelcJHkaEEZxhuQa+AAylkRNk+T8sU6+0TpSvcO+x/qBp8pi3qUuml7Z7+UK mWgNGWYpejVdU6DMyBdtYE1m/TDAdthp4sNrlHMNiNvK7RBkRLv+giTSSutYypDH8vfP eGJL+Uz0KHKfhtGRf4Ee9Pgkfrtf1uDAU69Vy1UXgoOwqun81+1iyeIqYjvuP9YNcL/B B+cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684432139; x=1687024139; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Qg03Fbiq3xmc+Z90K1Ru5CLtjdVMJPdv1lfOn+t0kwQ=; b=l/HVUGuBHfreqAViRaKLpwt0gsBICKrxdJo2RZ1lhKNdfkUQw387dsnbBuVuI4FbC7 msw9eNYyLjMpcRglJM0RcoxFHQwONmQhCit9rzACakXoj6jxNMwbcgIisQGW00ibxOc/ RRNTzDownJ/eigYQih4PZp/CR1nE+KnIn67OcmISGQM27YKAOwuAGzetMIEkqUs1N7NU //KKShYl/78GHOrtthuN2dbfWf0QtQjZw+2YMFrE+nMg6bkS6EIVGJ6mbepI2bAIxzTA lK1SnjJ96iJNRj99QQRtHSm8Dagccdl3rm60Xct0qEjx1DY2QGi2GCEPS10QU0NNQylE qZjg== X-Gm-Message-State: AC+VfDzIi0MFaV9JYpuvzmdZqqBF9RbTbhlAYfT6o26WVFWRMTt8jJ68 5pZTDNNP8AbuFXyeSBXUtoN0EBMPCKigxA== X-Google-Smtp-Source: ACHHUZ4DmCwCGx0GArh8fzl2Y6BJUIum03TxHFkvHhFFudo3PW6QJ5Cas4TIAazKht4ISDU7pwXMVA== X-Received: by 2002:ad4:5d4e:0:b0:616:49fe:f150 with SMTP id jk14-20020ad45d4e000000b0061649fef150mr928699qvb.18.1684432139174; Thu, 18 May 2023 10:48:59 -0700 (PDT) Received: from localhost.localdomain (dsl-150-33.b2b2c.ca. [66.158.150.33]) by smtp.gmail.com with ESMTPSA id ml7-20020a056214584700b0061a0f7fb340sm689006qvb.6.2023.05.18.10.48.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 10:48:58 -0700 (PDT) From: Maxim Cournoyer Date: Thu, 18 May 2023 13:48:42 -0400 Message-Id: <4f49bf6c8952680bd5e017f90b3e7478fe338111.1684431342.git.maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * gnu/packages/patches/linux-libre-wireguard-postup-privkey.patch: New patch. * gnu/local.mk (dist_patch_DATA): Register it. * gnu/packages/linux.scm (linux-libre-6.3-source, linux-libre-6.2-source) (linux-libre-6.1-source, linux-libre-5.15-source) (linux-libre-5.10-source): Apply it. --- gnu/local.mk | 1 + gnu/packages/linux.scm | 27 ++-- ...linux-libre-wireguard-postup-privkey.patch | 119 ++++++++++++++++++ 3 files changed, 139 insertions(+), 8 deletions(-) create mode 100644 gnu/packages/patches/linux-libre-wireguard-postup-privkey.patch diff --git a/gnu/local.mk b/gnu/local.mk index 42514ded8e..0b0aafa016 100644 --- a/gnu/local.mk +++ b/gnu/local.mk @@ -1515,6 +1515,7 @@ dist_patch_DATA = \ %D%/packages/patches/linphone-desktop-without-sdk.patch \ %D%/packages/patches/linux-libre-infodocs-target.patch \ %D%/packages/patches/linux-libre-support-for-Pinebook-Pro.patch \ + %D%/packages/patches/linux-libre-wireguard-postup-privkey.patch \ %D%/packages/patches/linux-pam-no-setfsuid.patch \ %D%/packages/patches/linux-pam-unix_chkpwd.patch \ %D%/packages/patches/linuxdcpp-openssl-1.1.patch \ diff --git a/gnu/packages/linux.scm b/gnu/packages/linux.scm index c38287e16b..6440e358c0 100644 --- a/gnu/packages/linux.scm +++ b/gnu/packages/linux.scm @@ -34,7 +34,7 @@ ;;; Copyright © 2018 Vasile Dumitrascu ;;; Copyright © 2019 Tim Gesthuizen ;;; Copyright © 2019 mikadoZero -;;; Copyright © 2019, 2020, 2021, 2022 Maxim Cournoyer +;;; Copyright © 2019, 2020, 2021, 2022, 2023 Maxim Cournoyer ;;; Copyright © 2019 Stefan Stefanović ;;; Copyright © 2019-2022 Brice Waegeneire ;;; Copyright © 2019 Kei Kebreau @@ -639,28 +639,39 @@ (define (source-with-patches source patches) (define-public linux-libre-6.3-source (source-with-patches linux-libre-6.3-pristine-source (list %boot-logo-patch - %linux-libre-arm-export-__sync_icache_dcache-patch))) + %linux-libre-arm-export-__sync_icache_dcache-patch + (search-patch + "linux-libre-wireguard-postup-privkey.patch")))) (define-public linux-libre-6.2-source (source-with-patches linux-libre-6.2-pristine-source (list %boot-logo-patch - %linux-libre-arm-export-__sync_icache_dcache-patch))) + %linux-libre-arm-export-__sync_icache_dcache-patch + (search-patch + "linux-libre-wireguard-postup-privkey.patch")))) (define-public linux-libre-6.1-source (source-with-patches linux-libre-6.1-pristine-source - (list %boot-logo-patch - %linux-libre-arm-export-__sync_icache_dcache-patch - (search-patch "linux-libre-infodocs-target.patch")))) + (append + (list %boot-logo-patch + %linux-libre-arm-export-__sync_icache_dcache-patch) + (search-patches + "linux-libre-infodocs-target.patch" + "linux-libre-wireguard-postup-privkey.patch")))) (define-public linux-libre-5.15-source (source-with-patches linux-libre-5.15-pristine-source (list %boot-logo-patch - %linux-libre-arm-export-__sync_icache_dcache-patch))) + %linux-libre-arm-export-__sync_icache_dcache-patch + (search-patch + "linux-libre-wireguard-postup-privkey.patch")))) (define-public linux-libre-5.10-source (source-with-patches linux-libre-5.10-pristine-source (list %boot-logo-patch - %linux-libre-arm-export-__sync_icache_dcache-patch))) + %linux-libre-arm-export-__sync_icache_dcache-patch + (search-patch + "linux-libre-wireguard-postup-privkey.patch")))) (define-public linux-libre-5.4-source (source-with-patches linux-libre-5.4-pristine-source diff --git a/gnu/packages/patches/linux-libre-wireguard-postup-privkey.patch b/gnu/packages/patches/linux-libre-wireguard-postup-privkey.patch new file mode 100644 index 0000000000..a6050499e1 --- /dev/null +++ b/gnu/packages/patches/linux-libre-wireguard-postup-privkey.patch @@ -0,0 +1,119 @@ +From 3ac1bf099766f1e9735883d5127148054cd5b30a Mon Sep 17 00:00:00 2001 +From: "Jason A. Donenfeld" +Date: Thu, 18 May 2023 03:08:44 +0200 +Subject: wireguard: netlink: send staged packets when setting initial private + key + +Packets bound for peers can queue up prior to the device private key +being set. For example, if persistent keepalive is set, a packet is +queued up to be sent as soon as the device comes up. However, if the +private key hasn't been set yet, the handshake message never sends, and +no timer is armed to retry, since that would be pointless. + +But, if a user later sets a private key, the expectation is that those +queued packets, such as a persistent keepalive, are actually sent. So +adjust the configuration logic to account for this edge case, and add a +test case to make sure this works. + +Maxim noticed this with a wg-quick(8) config to the tune of: + + [Interface] + PostUp = wg set %i private-key somefile + + [Peer] + PublicKey = ... + Endpoint = ... + PersistentKeepalive = 25 + +Here, the private key gets set after the device comes up using a PostUp +script, triggering the bug. + +Fixes: e7096c131e51 ("net: WireGuard secure network tunnel") +Cc: stable@vger.kernel.org +Reported-by: Maxim Cournoyer +Link: https://lore.kernel.org/wireguard/87fs7xtqrv.fsf@gmail.com/ +Signed-off-by: Jason A. Donenfeld +--- + drivers/net/wireguard/netlink.c | 14 +++++++++----- + tools/testing/selftests/wireguard/netns.sh | 30 ++++++++++++++++++++++++++---- + 2 files changed, 35 insertions(+), 9 deletions(-) + +diff --git a/drivers/net/wireguard/netlink.c b/drivers/net/wireguard/netlink.c +index 43c8c84e7ea8..6d1bd9f52d02 100644 +--- a/drivers/net/wireguard/netlink.c ++++ b/drivers/net/wireguard/netlink.c +@@ -546,6 +546,7 @@ static int wg_set_device(struct sk_buff *skb, struct genl_info *info) + u8 *private_key = nla_data(info->attrs[WGDEVICE_A_PRIVATE_KEY]); + u8 public_key[NOISE_PUBLIC_KEY_LEN]; + struct wg_peer *peer, *temp; ++ bool send_staged_packets; + + if (!crypto_memneq(wg->static_identity.static_private, + private_key, NOISE_PUBLIC_KEY_LEN)) +@@ -564,14 +565,17 @@ static int wg_set_device(struct sk_buff *skb, struct genl_info *info) + } + + down_write(&wg->static_identity.lock); +- wg_noise_set_static_identity_private_key(&wg->static_identity, +- private_key); +- list_for_each_entry_safe(peer, temp, &wg->peer_list, +- peer_list) { ++ send_staged_packets = !wg->static_identity.has_identity && netif_running(wg->dev); ++ wg_noise_set_static_identity_private_key(&wg->static_identity, private_key); ++ send_staged_packets = send_staged_packets && wg->static_identity.has_identity; ++ ++ wg_cookie_checker_precompute_device_keys(&wg->cookie_checker); ++ list_for_each_entry_safe(peer, temp, &wg->peer_list, peer_list) { + wg_noise_precompute_static_static(peer); + wg_noise_expire_current_peer_keypairs(peer); ++ if (send_staged_packets) ++ wg_packet_send_staged_packets(peer); + } +- wg_cookie_checker_precompute_device_keys(&wg->cookie_checker); + up_write(&wg->static_identity.lock); + } + skip_set_private_key: +diff --git a/tools/testing/selftests/wireguard/netns.sh b/tools/testing/selftests/wireguard/netns.sh +index 69c7796c7ca9..405ff262ca93 100755 +--- a/tools/testing/selftests/wireguard/netns.sh ++++ b/tools/testing/selftests/wireguard/netns.sh +@@ -514,10 +514,32 @@ n2 bash -c 'printf 0 > /proc/sys/net/ipv4/conf/all/rp_filter' + n1 ping -W 1 -c 1 192.168.241.2 + [[ $(n2 wg show wg0 endpoints) == "$pub1 10.0.0.3:1" ]] + +-ip1 link del veth1 +-ip1 link del veth3 +-ip1 link del wg0 +-ip2 link del wg0 ++ip1 link del dev veth3 ++ip1 link del dev wg0 ++ip2 link del dev wg0 ++ ++# Make sure persistent keep alives are sent when an adapter comes up ++ip1 link add dev wg0 type wireguard ++n1 wg set wg0 private-key <(echo "$key1") peer "$pub2" endpoint 10.0.0.1:1 persistent-keepalive 1 ++read _ _ tx_bytes < <(n1 wg show wg0 transfer) ++[[ $tx_bytes -eq 0 ]] ++ip1 link set dev wg0 up ++read _ _ tx_bytes < <(n1 wg show wg0 transfer) ++[[ $tx_bytes -gt 0 ]] ++ip1 link del dev wg0 ++# This should also happen even if the private key is set later ++ip1 link add dev wg0 type wireguard ++n1 wg set wg0 peer "$pub2" endpoint 10.0.0.1:1 persistent-keepalive 1 ++read _ _ tx_bytes < <(n1 wg show wg0 transfer) ++[[ $tx_bytes -eq 0 ]] ++ip1 link set dev wg0 up ++read _ _ tx_bytes < <(n1 wg show wg0 transfer) ++[[ $tx_bytes -eq 0 ]] ++n1 wg set wg0 private-key <(echo "$key1") ++read _ _ tx_bytes < <(n1 wg show wg0 transfer) ++[[ $tx_bytes -gt 0 ]] ++ip1 link del dev veth1 ++ip1 link del dev wg0 + + # We test that Netlink/IPC is working properly by doing things that usually cause split responses + ip0 link add dev wg0 type wireguard +-- +cgit v1.2.3-59-g8ed1b +