-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwgnetns.sh
More file actions
115 lines (104 loc) · 2.71 KB
/
Copy pathwgnetns.sh
File metadata and controls
115 lines (104 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env -S --debug -- bash
set \
-e \
#
declare \
-i \
-- \
exit_code=0 \
#
function show_usage
{
echo "usage: $0 <up|down> NAMESPACE" >&2
}
if [[ $@ = ?(-)?(-)help ]]
then
show_usage
elif [[ $# -eq 2 ]]
then
declare \
-r \
-- \
subcommand="$1" \
namespace="$2" \
#
case "${subcommand}" in
down)
# Delete the network namespace
ip \
netns del \
"${namespace}" \
#
echo "info: network namespace '${namespace}' deleted" >&2
# The WireGuard interface within the namespace is deleted automatically,
# so it isn't necessary to run `ip -netns ${namespace} link delete dev wg0`
;;
up)
# Create the network namespace
ip \
netns add \
"${namespace}" \
#
echo "info: network namespace '${namespace}' created" >&2
# Network devices within the same network namespace must have unique names.
# Since the WireGuard interface is created in a shared namespace at first,
# steps must be taken to reduce the risk of a name collision.
if [[ ${BASH_VERSINFO[0]}${BASH_VERSINFO[1]} -ge 51 ]]
then
# `$SRANDOM` was introduced in Bash version 5.1
# https://www.gnu.org/software/bash/manual/bash#index-SRANDOM
random_suffix="$(( SRANDOM % 999999 ))"
else
# https://www.gnu.org/software/bash/manual/bash#index-RANDOM
random_suffix="${RANDOM}"
fi
interface="wgnetns-${random_suffix}"
# The WireGuard interface is renamed to `wg0` later, once it has been moved
# into its own namespace
# Create the WireGuard interface in the current network namespace
ip \
link add \
dev "${interface}" \
type wireguard \
#
echo "info: WireGuard interface created under temporary name '${interface}'" >&2
# Try to move the WireGuard interface into the new network namespace
if
ip \
link set \
dev "${interface}" \
netns "${namespace}" \
#
then
echo "debug: WireGuard interface '${interface}' moved into network namespace '${namespace}'" >&2
# Network devices in different network namespaces can have the same name.
# Take advantage of this to provide the WireGuard interface with a more
# reasonable name.
ip \
-netns "${namespace}" \
link set \
dev "${interface}" \
name wg0 \
#
echo "debug: WireGuard interface renamed to 'wg0' inside network namespace '${namespace}'" >&2
else
echo "error: cannot move WireGuard interface '${interface}' into network namespace '${namespace}'" >&2
exit_code=1
ip \
-netns "${namespace}" \
link delete \
dev "${interface}" \
#
echo "info: WireGuard interface '${interface}' deleted" >&2
fi
;;
*)
show_usage
exit_code=1
;;
esac
else
show_usage
exit_code=1
fi
exit "${exit_code}"