2 # Copyright 2016-2017 Dan Luedtke <mail@danrl.com>
3 # Licensed to the public under the Apache License 2.0.
7 logger
-t "wireguard" "error: missing wireguard-tools (${WG})"
11 [ -n "$INCLUDE_ONLY" ] ||
{
17 proto_wireguard_init_config
() {
18 proto_config_add_string
"private_key"
19 proto_config_add_int
"listen_port"
20 proto_config_add_int
"mtu"
21 proto_config_add_string
"fwmark"
26 proto_wireguard_setup_peer
() {
27 local peer_config
="$1"
33 local route_allowed_ips
36 local persistent_keepalive
38 config_get_bool disabled
"${peer_config}" "disabled" 0
39 config_get public_key
"${peer_config}" "public_key"
40 config_get preshared_key
"${peer_config}" "preshared_key"
41 config_get allowed_ips
"${peer_config}" "allowed_ips"
42 config_get_bool route_allowed_ips
"${peer_config}" "route_allowed_ips" 0
43 config_get endpoint_host
"${peer_config}" "endpoint_host"
44 config_get endpoint_port
"${peer_config}" "endpoint_port"
45 config_get persistent_keepalive
"${peer_config}" "persistent_keepalive"
47 if [ "${disabled}" -eq 1 ]; then
52 if [ -z "$public_key" ]; then
53 echo "Skipping peer config $peer_config because public key is not defined."
57 echo "[Peer]" >> "${wg_cfg}"
58 echo "PublicKey=${public_key}" >> "${wg_cfg}"
59 if [ "${preshared_key}" ]; then
60 echo "PresharedKey=${preshared_key}" >> "${wg_cfg}"
62 for allowed_ip
in $allowed_ips; do
63 echo "AllowedIPs=${allowed_ip}" >> "${wg_cfg}"
65 if [ "${endpoint_host}" ]; then
66 case "${endpoint_host}" in
68 endpoint
="[${endpoint_host}]"
71 endpoint
="${endpoint_host}"
74 if [ "${endpoint_port}" ]; then
75 endpoint
="${endpoint}:${endpoint_port}"
77 endpoint
="${endpoint}:51820"
79 echo "Endpoint=${endpoint}" >> "${wg_cfg}"
81 if [ "${persistent_keepalive}" ]; then
82 echo "PersistentKeepalive=${persistent_keepalive}" >> "${wg_cfg}"
85 if [ ${route_allowed_ips} -ne 0 ]; then
86 for allowed_ip
in ${allowed_ips}; do
87 case "${allowed_ip}" in
89 proto_add_ipv6_route
"${allowed_ip%%/*}" "${allowed_ip##*/}"
92 proto_add_ipv4_route
"${allowed_ip%%/*}" "${allowed_ip##*/}"
95 proto_add_ipv6_route
"${allowed_ip%%/*}" "128"
98 proto_add_ipv4_route
"${allowed_ip%%/*}" "32"
105 ensure_key_is_generated
() {
107 private_key
="$(uci get network."$1".private_key)"
109 if [ "$private_key" == "generate" ]; then
113 ucitmp
="$(mktemp -d)"
114 private_key
="$("${WG}" genkey)"
115 uci
-q -t "$ucitmp" set network.
"$1".private_key
="$private_key" && \
116 uci
-q -t "$ucitmp" commit network
122 proto_wireguard_setup
() {
124 local wg_dir
="/tmp/wireguard"
125 local wg_cfg
="${wg_dir}/${config}"
131 ensure_key_is_generated
"${config}"
134 config_get private_key
"${config}" "private_key"
135 config_get listen_port
"${config}" "listen_port"
136 config_get addresses
"${config}" "addresses"
137 config_get mtu
"${config}" "mtu"
138 config_get fwmark
"${config}" "fwmark"
139 config_get ip6prefix
"${config}" "ip6prefix"
140 config_get nohostroute
"${config}" "nohostroute"
141 config_get tunlink
"${config}" "tunlink"
143 ip link del dev
"${config}" 2>/dev
/null
144 ip link add dev
"${config}" type wireguard
146 if [ "${mtu}" ]; then
147 ip link
set mtu
"${mtu}" dev
"${config}"
150 proto_init_update
"${config}" 1
154 echo "[Interface]" > "${wg_cfg}"
155 echo "PrivateKey=${private_key}" >> "${wg_cfg}"
156 if [ "${listen_port}" ]; then
157 echo "ListenPort=${listen_port}" >> "${wg_cfg}"
159 if [ "${fwmark}" ]; then
160 echo "FwMark=${fwmark}" >> "${wg_cfg}"
162 config_foreach proto_wireguard_setup_peer
"wireguard_${config}"
164 # apply configuration file
165 ${WG} setconf ${config} "${wg_cfg}"
170 if [ ${WG_RETURN} -ne 0 ]; then
172 proto_setup_failed "${config}"
176 for address in ${addresses}; do
179 proto_add_ipv6_address "${address%%/*}" "${address##*/}"
182 proto_add_ipv4_address "${address%%/*}" "${address##*/}"
185 proto_add_ipv6_address "${address%%/*}" "128"
188 proto_add_ipv4_address "${address%%/*}" "32"
193 for prefix in ${ip6prefix}; do
194 proto_add_ipv6_prefix "$prefix"
197 # endpoint dependency
198 if [ "${nohostroute}" != "1" ]; then
199 wg show "${config}" endpoints | \
200 sed -E 's/\[?([0-9.:a-f]+)\]?:([0-9]+)/\1 \2/' | \
201 while IFS=$'\t ' read -r key address port; do
202 [ -n "${port}" ] || continue
203 proto_add_host_dependency "${config}" "${address}" "${tunlink}"
207 proto_send_update "${config}"
210 proto_wireguard_teardown() {
212 ip link del dev "${config}" >/dev/null 2>&1
215 [ -n "$INCLUDE_ONLY" ] || {
216 add_protocol wireguard