WireGuard Archive mirror
 help / color / mirror / Atom feed
From: aptalca <aptalca@linuxserver.io>
To: wireguard@lists.zx2c4.com
Subject: [PATCH] wg-quick: Set sysctl only if necessary
Date: Wed, 8 Feb 2023 14:33:37 -0500	[thread overview]
Message-ID: <CAAwaY-A2T3HRX2KOD7U2xTseU885+zDaZHPGPX7-W7aF-6k4sQ@mail.gmail.com> (raw)

Currently, wg-quick script on linux attempts to set the sysctl
"net.ipv4.conf.all.src_valid_mark=1" every time, no matter if it's
already set or not.

The issue is, when the script is run inside a container lacking the
privilege for setting sysctls, it fails with a warning message. In
such cases, like a docker container, the user is expected to set the
sysctl via docker arguments when creating the container so the sysctl
is already set correctly. There is no need for wg-quick to set it
inside the container as it's already set. The warning in such cases is
a false positive and is confusing to the user as it leads them to
believe the sysctl is not set correctly.

One example is the linuxserver wireguard docker image:
https://github.com/linuxserver/docker-wireguard
The container is meant to be created with the docker argument
'--sysctl="net.ipv4.conf.all.src_valid_mark=1"' so there is no need
for wg-quick to set it inside the container. It tries anyway and fails
with a warning as listed below. Since the sysctl is already set
correctly, everything works as expected.

[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.1.13.12/32 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] wg set wg0 fwmark 51820
[#] ip -4 route add 0.0.0.0/0 dev wg0 table 51820
[#] ip -4 rule add not fwmark 51820 table 51820
[#] ip -4 rule add table main suppress_prefixlength 0
[#] sysctl -q net.ipv4.conf.all.src_valid_mark=1
sysctl: setting key "net.ipv4.conf.all.src_valid_mark", ignoring:
Read-only file system
[#] iptables-restore -n
[#] iptables -t nat -A POSTROUTING -o wg+ -j MASQUERADE

Here's a patch that makes the sysctl setting attempt to be
conditional. It first checks whether it's already set correctly, and
only attempts to set it if necessary.

Signed-off-by: aptalca <aptalca@linuxserver.io>
---
 src/wg-quick/linux.bash | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/wg-quick/linux.bash b/src/wg-quick/linux.bash
index 69e5bef..5a8048f 100755
--- a/src/wg-quick/linux.bash
+++ b/src/wg-quick/linux.bash
@@ -237,7 +237,7 @@ add_default() {
  printf -v restore '%sCOMMIT\n*mangle\n-I POSTROUTING -m mark --mark
%d -p udp -j CONNMARK --save-mark %s\n-I PREROUTING -p udp -j CONNMARK
--restore-mark %s\nCOMMIT\n' "$restore" $table "$marker" "$marker"
  printf -v nftcmd '%sadd rule %s %s postmangle meta l4proto udp mark
%d ct mark set mark \n' "$nftcmd" "$pf" "$nftable" $table
  printf -v nftcmd '%sadd rule %s %s premangle meta l4proto udp meta
mark set ct mark \n' "$nftcmd" "$pf" "$nftable"
- [[ $proto == -4 ]] && cmd sysctl -q net.ipv4.conf.all.src_valid_mark=1
+ [[ $proto == -4 ]] && [[ $(sysctl -n
net.ipv4.conf.all.src_valid_mark) != 1 ]] && cmd sysctl -q
net.ipv4.conf.all.src_valid_mark=1
  if type -p nft >/dev/null; then
  cmd nft -f <(echo -n "$nftcmd")
  else
--
2.34.1

                 reply	other threads:[~2023-02-08 20:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAAwaY-A2T3HRX2KOD7U2xTseU885+zDaZHPGPX7-W7aF-6k4sQ@mail.gmail.com \
    --to=aptalca@linuxserver.io \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).