Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added network namespacing support #10

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contrib/highlighter/gui/highlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static QColor colormap[] = {
[HighlightDelimiter] = QColor("#7aa6da"),
#ifndef MOBILE_WGQUICK_SUBSET
[HighlightTable] = QColor("#c397d8"),
[HighlightNetNS] = QColor("#c397d8"),
[HighlightFwMark] = QColor("#c397d8"),
[HighlightSaveConfig] = QColor("#c397d8"),
[HighlightCmd] = QColor("#969896"),
Expand Down
1 change: 1 addition & 0 deletions contrib/highlighter/highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static const char *colormap[] = {
[HighlightDelimiter] = TERMINAL_FG_CYAN,
#ifndef MOBILE_WGQUICK_SUBSET
[HighlightTable] = TERMINAL_FG_BLUE,
[HighlightNetNS] = TERMINAL_FG_BLUE,
[HighlightFwMark] = TERMINAL_FG_BLUE,
[HighlightSaveConfig] = TERMINAL_FG_BLUE,
[HighlightCmd] = TERMINAL_FG_WHITE,
Expand Down
1 change: 1 addition & 0 deletions contrib/highlighter/highlighter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum highlight_type {
HighlightDelimiter,
#ifndef MOBILE_WGQUICK_SUBSET
HighlightTable,
HighlightNetNS,
HighlightFwMark,
HighlightSaveConfig,
HighlightCmd,
Expand Down
3 changes: 3 additions & 0 deletions src/man/wg-quick.8
Original file line number Diff line number Diff line change
Expand Up @@ -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
NetNS \(em Controls in which network namespace the WireGuard UDP socket is added to. The
namespace has to be created before WireGuard use.
.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
Expand Down
16 changes: 15 additions & 1 deletion src/wg-quick/linux.bash
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ MTU=""
DNS=( )
DNS_SEARCH=( )
TABLE=""
NETNS=""
PRE_UP=( )
POST_UP=( )
PRE_DOWN=( )
Expand Down Expand Up @@ -61,6 +62,7 @@ parse_options() {
[[ $v =~ (^[0-9.]+$)|(^.*:.*$) ]] && DNS+=( $v ) || DNS_SEARCH+=( $v )
done; continue ;;
Table) TABLE="$value"; continue ;;
NetNS) NETNS="$value"; continue ;;
PreUp) PRE_UP+=( "$value" ); continue ;;
PreDown) PRE_DOWN+=( "$value" ); continue ;;
PostUp) POST_UP+=( "$value" ); continue ;;
Expand All @@ -87,7 +89,18 @@ auto_su() {

add_if() {
local ret
if ! cmd ip link add "$INTERFACE" type wireguard; then
if [[ -n $NETNS ]]; then
if ! ip netns pids "${NETNS}" > /dev/null; then
ret=$?
echo "[!] Target namespace '${NETNS}' not found"
exit $ret
elif ! cmd ip -n "${NETNS}" 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."
fi
cmd ip -n "${NETNS}" link set "$INTERFACE" netns 1
elif ! cmd ip 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
Expand Down Expand Up @@ -263,6 +276,7 @@ save_config() {
done < <(resolvconf -l "$(resolvconf_iface_prefix)$INTERFACE" 2>/dev/null || cat "/etc/resolvconf/run/interface/$(resolvconf_iface_prefix)$INTERFACE" 2>/dev/null)
[[ -n $MTU && $(ip link show dev "$INTERFACE") =~ mtu\ ([0-9]+) ]] && new_config+="MTU = ${BASH_REMATCH[1]}"$'\n'
[[ -n $TABLE ]] && new_config+="Table = $TABLE"$'\n'
[[ -n $NETNS ]] && new_config+="NetNS = $NETNS"$'\n'
[[ $SAVE_CONFIG -eq 0 ]] || new_config+=$'SaveConfig = true\n'
for cmd in "${PRE_UP[@]}"; do
new_config+="PreUp = $cmd"$'\n'
Expand Down