wg-installer: fix multiple namespaces
authorNick Hainke <vincent@systemli.org>
Sat, 22 Jan 2022 22:27:02 +0000 (23:27 +0100)
committerNick Hainke <vincent@systemli.org>
Sun, 23 Jan 2022 17:09:51 +0000 (18:09 +0100)
Add flag "--lookup-default-namespace" to signal that wg-installer should
look already established wireguard sessions in the default namespace.

Signed-off-by: Nick Hainke <vincent@systemli.org>
(cherry picked from commit 94efdcf02a723fbcdcc6a192e026e0c2f766a158)

net/wg-installer/common/wg.sh
net/wg-installer/wg-client/wg-client-installer.sh

index 0f1666b31ee5bc2b6a9c34b01da6835af2a8ef19..003ce139857e0d0ed69cba0257300448c2513227 100644 (file)
@@ -43,6 +43,7 @@ check_wg_neighbors() {
 }
 
 case $1 in
+next_port|\
 cleanup_wginterfaces)
     "$@"
     exit
index 072e30c7737a9350c09737aa8e5fefd0c2285c85..884f8fd655212703923307c7709484a5e41eb198 100644 (file)
@@ -3,6 +3,8 @@
 . /usr/share/wginstaller/rpcd_ubus.sh
 . /usr/share/wginstaller/wg.sh
 
+DEFAULT_NAMESPACE=0
+
 CMD=$1
 shift
 
@@ -32,6 +34,10 @@ while true; do
                WG_KEY_FILE=$2
                shift 2
                ;;
+       --lookup-default-namespace)
+               DEFAULT_NAMESPACE=1
+               shift 1
+               ;;
        '')
                break
                ;;
@@ -47,11 +53,18 @@ register_client_interface () {
        local privkey=$4
        local pubkey=$5
        local gw_port=$6
+       local def_namespace=$7
 
        port_start=$(uci get wgclient.@client[0].port_start)
        port_end=$(uci get wgclient.@client[0].port_end)
 
-       port=$(next_port "$port_start" "$port_end")
+       if [ "$def_namespace" -eq "1" ]; then
+               [ -f /var/run/netns/default ] || ln -s /proc/1/ns/net /var/run/netns/default
+               port=$(ip netns exec default /usr/share/wginstaller/wg.sh next_port "$port_start" "$port_end")
+       else
+               port=$(next_port "$port_start" "$port_end")
+       fi
+
        ifname="wg_$port"
 
        ip link add dev "$ifname" type wireguard
@@ -88,7 +101,7 @@ case $CMD in
                exit 1
        fi
 
-       register_client_interface __interface "$ENDPOINT" "$WG_MTU" "$wg_priv_key_file" "$__gw_pubkey" "$__gw_port"
+       register_client_interface __interface "$ENDPOINT" "$WG_MTU" "$wg_priv_key_file" "$__gw_pubkey" "$__gw_port" "$DEFAULT_NAMESPACE"
        logger -t "wg-client-installer" "Registered: $__interface"
        echo $__interface
        ;;