Development discussion of WireGuard
 help / color / mirror / Atom feed
From: "Björn Fiedler" <bjoern@reldeif.de>
To: wireguard@lists.zx2c4.com
Subject: [PATCH] wg-quick: linux: introduce BirthNamespace parameter
Date: Tue, 27 Feb 2024 13:52:37 +0100	[thread overview]
Message-ID: <a34c95ff-c053-49fd-8ffc-d9cca49c11b2@reldeif.de> (raw)

This allows the interface to be created in another network namespace
and be moved into the current one afterwards. As the interface
remembers it's birth namespace, encrypted packets are sent and
received using the birth namespace. Use case is to route all the
traffic via the "New Namespace Solution" as described on
https://www.wireguard.com/netns/

Signed-off-by Björn Fiedler <bjoern@reldeif.de>
---
  src/man/wg-quick.8      | 3 +++
  src/wg-quick/linux.bash | 7 ++++++-
  2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/man/wg-quick.8 b/src/man/wg-quick.8
index bc9e145..b141041 100644
--- a/src/man/wg-quick.8
+++ b/src/man/wg-quick.8
@@ -92,6 +92,9 @@ special values: `off' disables the creation of routes altogether, and `auto'
  (the default) adds routes to the default table and enables special handling of
  default routes.
  .IP \(bu
+BirthNamespace \(em on Linux, if specified, the interface is created inside that namespace and
+afterwards moved into the current network namespace.
+.IP \(bu
  PreUp, PostUp, PreDown, PostDown \(em script snippets which will be executed by
  .BR bash (1)
  before/after setting up/tearing down the interface, most commonly used
diff --git a/src/wg-quick/linux.bash b/src/wg-quick/linux.bash
index 4193ce5..b0d1b70 100755
--- a/src/wg-quick/linux.bash
+++ b/src/wg-quick/linux.bash
@@ -18,6 +18,7 @@ MTU=""
  DNS=( )
  DNS_SEARCH=( )
  TABLE=""
+BIRTH_NAMESPACE=""
  PRE_UP=( )
  POST_UP=( )
  PRE_DOWN=( )
@@ -61,6 +62,7 @@ parse_options() {
                                 [[ $v =~ (^[0-9.]+$)|(^.*:.*$) ]] && DNS+=( $v ) || DNS_SEARCH+=( $v )
                         done; continue ;;
                         Table) TABLE="$value"; continue ;;
+                       BirthNamespace) BIRTH_NAMESPACE="$value"; continue ;;
                         PreUp) PRE_UP+=( "$value" ); continue ;;
                         PreDown) PRE_DOWN+=( "$value" ); continue ;;
                         PostUp) POST_UP+=( "$value" ); continue ;;
@@ -87,12 +89,15 @@ auto_su() {

  add_if() {
         local ret
-       if ! cmd ip link add "$INTERFACE" type wireguard; then
+       local namespace_arg=""
+       [[ -z $BIRTH_NAMESPACE ]] || namespace_arg="-n ${BIRTH_NAMESPACE}"
+       if ! cmd ip $namespace_arg link add "$INTERFACE" type wireguard; then
                 ret=$?
                 [[ -e /sys/module/wireguard ]] || ! command -v "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" >/dev/null && exit $ret
                 echo "[!] Missing WireGuard kernel module. Falling back to slow userspace implementation." >&2
                 cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" "$INTERFACE"
         fi
+    [[ -z $BIRTH_NAMESPACE ]] || cmd ip $namespace_arg link set "$INTERFACE" netns $BASHPID
  }

  del_if() {
-- 
2.43.0


                 reply	other threads:[~2024-02-27 12:52 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=a34c95ff-c053-49fd-8ffc-d9cca49c11b2@reldeif.de \
    --to=bjoern@reldeif.de \
    --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 NNTP newsgroup(s).