Development discussion of WireGuard
 help / color / mirror / Atom feed
From: aptalca <>
Subject: [PATCH] wg-quick: Set sysctl only if necessary
Date: Wed, 8 Feb 2023 14:33:37 -0500	[thread overview]
Message-ID: <> (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:
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 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] wg set wg0 fwmark 51820
[#] ip -4 route add 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 <>
 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
  if type -p nft >/dev/null; then
  cmd nft -f <(echo -n "$nftcmd")

                 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:

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

  git send-email \ \ \ \

* 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 NNTP newsgroup(s).