aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Android.mk41
-rw-r--r--README.md22
-rwxr-xr-xfetch-wireguard.sh10
-rw-r--r--wg-quick.bash187
4 files changed, 260 insertions, 0 deletions
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..f89c42c
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,41 @@
+WIREGUARD_PATH := $(call my-dir)
+
+fetch-wireguard:
+ cd "$(WIREGUARD_PATH)" && ./fetch-wireguard.sh
+
+TARGET_KERNEL_BINARIES: patch-wireguard
+patch-wireguard: fetch-wireguard
+ ln -vsfT "$$(realpath --relative-to="$(TARGET_KERNEL_SOURCE)/net)" "$(WIREGUARD_PATH)/wireguard-src/src")" "$(TARGET_KERNEL_SOURCE)/net/wireguard"
+ grep -q wireguard "$(TARGET_KERNEL_SOURCE)/net/Makefile" || sed -i "/^obj-\\\$$(CONFIG_NETFILTER).*+=/a obj-\$$(CONFIG_WIREGUARD) += wireguard/" "$(TARGET_KERNEL_SOURCE)/net/Makefile"
+ grep -q wireguard "$(TARGET_KERNEL_SOURCE)/net/Kconfig" || sed -i "/^if INET\$$/a source \"net/wireguard/Kconfig\"" "$(TARGET_KERNEL_SOURCE)/net/Kconfig"
+
+fetch-libmnl:
+ rm -rf "$(WIREGUARD_PATH)/libmnl-src" && mkdir "$(WIREGUARD_PATH)/libmnl-src"
+ curl -L "https://www.netfilter.org/projects/libmnl/files/libmnl-1.0.4.tar.bz2" | tar -C "$(WIREGUARD_PATH)/libmnl-src" --strip-components=1 -xjf -
+
+.PHONY: fetch-wireguard fetch-libmnl patch-wireguard
+
+LOCAL_PATH := $(WIREGUARD_PATH)
+include $(CLEAR_VARS)
+MNL := libmnl-src/src
+WG := wireguard-src/src/tools
+WG_FILES := $(WG)/config.c $(WG)/curve25519.c $(WG)/encoding.c $(WG)/genkey.c $(WG)/ipc.c $(WG)/mnlg.c $(WG)/pubkey.c $(WG)/set.c $(WG)/setconf.c $(WG)/show.c $(WG)/showconf.c $(WG)/terminal.c $(WG)/wg.c
+MNL_FILES := $(MNL)/attr.c $(MNL)/callback.c $(MNL)/nlmsg.c $(MNL)/socket.c
+$(foreach F,$(WG_FILES) $(MNL_FILES),$(WIREGUARD_PATH)/$(F)): fetch-wireguard fetch-libmnl
+LOCAL_SRC_FILES := $(WG_FILES) $(MNL_FILES)
+LOCAL_C_INCLUDES := $(WIREGUARD_PATH)/$(MNL)/ $(WIREGUARD_PATH)/$(WG)/ $(WIREGUARD_PATH)/$(MNL)/../include/
+LOCAL_CFLAGS := -O3 -std=gnu11 -D_GNU_SOURCE -DHAVE_VISIBILITY_HIDDEN -DRUNSTATEDIR="\"/data/local/run\"" -Wno-pointer-arith -Wno-unused-parameter
+LOCAL_MODULE := wg
+LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+LOCAL_MODULE_CLASS := EXECUTABLES
+ALL_DEFAULT_INSTALLED_MODULES += wg
+include $(BUILD_EXECUTABLE)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := wg-quick
+LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+LOCAL_SRC_FILES := wg-quick.bash
+LOCAL_REQUIRED_MODULES := bash wg
+LOCAL_MODULE_CLASS := EXECUTABLES
+ALL_DEFAULT_INSTALLED_MODULES += wg-quick
+include $(BUILD_PREBUILT)
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ccfb1ae
--- /dev/null
+++ b/README.md
@@ -0,0 +1,22 @@
+WireGuard for Android ROMs
+==========================
+
+This is a repository meant to be included via a `local_manifest.xml`, so that [WireGuard](https://www.wireguard.com/) is built into the kernel and userland of an Android ROM. This is currently tested on Android 7 and kernels ≥3.10.
+
+Usage
+-----
+
+Add the following local manifest to your project, or include the `<remote>` and `<project>` lines in an existing manifest:
+
+```
+<?xml version="1.0" encoding="UTF-8"?>
+<manifest>
+ <remote name="zx2c4" fetch="https://git.zx2c4.com/" />
+ <project remote="zx2c4" name="android_kernel_wireguard" path="kernel/wireguard" />
+</manifest>
+```
+
+Tools
+-----
+
+In addition to the kernel module, this repository also contains a version of [`wg-quick(8)`](https://git.zx2c4.com/WireGuard/about/src/tools/wg-quick.8) that works with Android 7's `ndc` command. Compared to the ordinary wg-quick, this one does not support `SaveConfig` and `{Pre,Post}{Up,Down}`. Put your configuration files into `/data/misc/wireguard/`. After that, the normal `wg-quick up|down` commands will work as usual.
diff --git a/fetch-wireguard.sh b/fetch-wireguard.sh
new file mode 100755
index 0000000..73a2200
--- /dev/null
+++ b/fetch-wireguard.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+set -e
+
+[[ $(( $(date +%s) - $(stat -c %Y .last-wireguard-fetch 2>/dev/null || echo 0) )) -gt 21600 ]] || exit 0
+
+[[ $(curl -L https://git.zx2c4.com/WireGuard/refs/) =~ snapshot/(WireGuard-[0-9.]+\.tar\.xz) ]]
+rm -rf wireguard-src
+mkdir wireguard-src
+curl -L "https://git.zx2c4.com/WireGuard/snapshot/${BASH_REMATCH[1]}" | tar -C "wireguard-src" --strip-components=1 -xJf -
+touch .last-wireguard-fetch
diff --git a/wg-quick.bash b/wg-quick.bash
new file mode 100644
index 0000000..4a1bce2
--- /dev/null
+++ b/wg-quick.bash
@@ -0,0 +1,187 @@
+#!/system/xbin/bash
+#
+# Copyright (C) 2016-2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
+#
+
+set -e -o pipefail
+shopt -s extglob
+export LC_ALL=C
+
+SELF="$(readlink -f "${BASH_SOURCE[0]}")"
+export PATH="${SELF%/*}:$PATH"
+
+WG_CONFIG=""
+INTERFACE=""
+NETID=0
+ADDRESSES=( )
+MTU=""
+DNS=( )
+CONFIG_FILE=""
+PROGRAM="${0##*/}"
+ARGS=( "$@" )
+
+parse_options() {
+ local interface_section=0 line key value
+ CONFIG_FILE="$1"
+ [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,16}$ ]] && CONFIG_FILE="/data/misc/wireguard/$CONFIG_FILE.conf"
+ [[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist"
+ [[ $CONFIG_FILE =~ /?([a-zA-Z0-9_=+.-]{1,16})\.conf$ ]] || die "The config file must be a valid interface name, followed by .conf"
+ ((($(stat -c '%#a' "$CONFIG_FILE") & 0007) == 0)) || echo "Warning: \`$CONFIG_FILE' is world accessible" >&2
+ INTERFACE="${BASH_REMATCH[1]}"
+ shopt -s nocasematch
+ while read -r line || [[ -n $line ]]; do
+ key="${line%%=*}"; key="${key##*( )}"; key="${key%%*( )}"
+ value="${line#*=}"; value="${value##*( )}"; value="${value%%*( )}"
+ [[ $key == "["* ]] && interface_section=0
+ [[ $key == "[Interface]" ]] && interface_section=1
+ if [[ $interface_section -eq 1 ]]; then
+ case "$key" in
+ Address) ADDRESSES+=( ${value//,/ } ); continue ;;
+ MTU) MTU="$value"; continue ;;
+ DNS) DNS+=( ${value//,/ } ); continue ;;
+ esac
+ fi
+ WG_CONFIG+="$line"$'\n'
+ done < "$CONFIG_FILE"
+ shopt -u nocasematch
+}
+
+cmd() {
+ echo "[#] $*" >&2
+ "$@"
+}
+
+cndc() {
+ local out="$(cmd ndc "$@")"
+ [[ $out == *200\ 0* ]] || { echo "$out"; return 1; }
+}
+
+die() {
+ echo "$PROGRAM: $*" >&2
+ exit 1
+}
+
+auto_su() {
+ [[ $UID == 0 ]] || exec su -p -c "'$SELF' ${ARGS[*]}"
+}
+
+add_if() {
+ cmd ip link add "$INTERFACE" type wireguard
+}
+
+del_if() {
+ cmd ip link del "$INTERFACE"
+ [[ $(ip rule show) =~ 0xc([0-9a-f]+)/0xcffff\ lookup\ $INTERFACE ]] && cndc network destroy $(( 0x${BASH_REMATCH[1]} ));
+}
+
+up_if() {
+ while [[ $NETID -lt 4096 ]]; do
+ NETID="$RANDOM"
+ done
+ cmd wg set "$INTERFACE" fwmark 0x20000
+ cndc interface setcfg "$INTERFACE" up
+ cndc network create "$NETID" vpn 1 1
+ cndc network interface add "$NETID" "$INTERFACE"
+ cndc network users add "$NETID" 0-99999
+}
+
+set_dns() {
+ [[ ${#DNS[@]} -eq 0 ]] || cndc resolver setnetdns "$NETID" "" "${DNS[@]}"
+}
+
+add_addr() {
+ if [[ $1 == *:* ]]; then
+ cndc interface ipv6 "$INTERFACE" enable
+ cmd ip -6 addr add "$1" dev "$INTERFACE"
+ else
+ local ip="${1%%/*}" mask=32
+ [[ $1 == */* ]] && mask="${1##*/}"
+ cndc interface setcfg "$INTERFACE" "$ip" "$mask"
+ fi
+}
+
+set_mtu() {
+ local mtu=0 endpoint output
+ if [[ -n $MTU ]]; then
+ cndc interface setmtu "$INTERFACE" "$MTU"
+ return
+ fi
+ while read -r _ endpoint; do
+ [[ $endpoint =~ ^\[?([a-z0-9:.]+)\]?:[0-9]+$ ]] || continue
+ output="$(ip route get "${BASH_REMATCH[1]}" || true)"
+ [[ ( $output =~ mtu\ ([0-9]+) || ( $output =~ dev\ ([^ ]+) && $(ip link show dev "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) ) ) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}"
+ done < <(wg show "$INTERFACE" endpoints)
+ if [[ $mtu -eq 0 ]]; then
+ read -r output < <(ip route show default || true) || true
+ [[ ( $output =~ mtu\ ([0-9]+) || ( $output =~ dev\ ([^ ]+) && $(ip link show dev "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) ) ) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}"
+ fi
+ [[ $mtu -gt 0 ]] || mtu=1500
+ cndc interface setmtu "$INTERFACE" $(( mtu - 80 ))
+}
+
+add_route() {
+ cndc network route add "$NETID" "$INTERFACE" "$1"
+}
+
+set_config() {
+ cmd wg setconf "$INTERFACE" <(echo "$WG_CONFIG")
+}
+
+cmd_usage() {
+ cat >&2 <<-_EOF
+ Usage: $PROGRAM [ up | down ] [ CONFIG_FILE | INTERFACE ]
+
+ CONFIG_FILE is a configuration file, whose filename is the interface name
+ followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
+ configuration found at /data/misc/wireguard/INTERFACE.conf. It is to be readable
+ by wg(8)'s \`setconf' sub-command, with the exception of the following additions
+ to the [Interface] section, which are handled by $PROGRAM:
+
+ - Address: may be specified one or more times and contains one or more
+ IP addresses (with an optional CIDR mask) to be set for the interface.
+ - MTU: an optional MTU for the interface; if unspecified, auto-calculated.
+ - DNS: an optional DNS server to use while the device is up.
+
+ See wg-quick(8) for more info and examples.
+ _EOF
+}
+
+cmd_up() {
+ local i
+ [[ -z $(ip link show dev "$INTERFACE" 2>/dev/null) ]] || die "\`$INTERFACE' already exists"
+ trap 'del_if; exit' INT TERM EXIT
+ add_if
+ set_config
+ set_mtu
+ for i in "${ADDRESSES[@]}"; do
+ add_addr "$i"
+ done
+ up_if
+ set_dns
+ for i in $(while read -r _ i; do for i in $i; do [[ $i =~ ^[0-9a-z:.]+/[0-9]+$ ]] && echo "$i"; done; done < <(wg show "$INTERFACE" allowed-ips) | sort -nr -k 2 -t /); do
+ [[ $(ip route get "$i" 2>/dev/null) == *dev\ $INTERFACE\ * ]] || add_route "$i"
+ done
+ trap - INT TERM EXIT
+}
+
+cmd_down() {
+ [[ -n $(ip link show dev "$INTERFACE" type wireguard 2>/dev/null) ]] || die "\`$INTERFACE' is not a WireGuard interface"
+ del_if
+}
+
+if [[ $# -eq 1 && ( $1 == --help || $1 == -h || $1 == help ) ]]; then
+ cmd_usage
+elif [[ $# -eq 2 && $1 == up ]]; then
+ auto_su
+ parse_options "$2"
+ cmd_up
+elif [[ $# -eq 2 && $1 == down ]]; then
+ auto_su
+ parse_options "$2"
+ cmd_down
+else
+ cmd_usage
+ exit 1
+fi
+
+exit 0