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 proto_wireguard_setup
() {
107 local wg_dir
="/tmp/wireguard"
108 local wg_cfg
="${wg_dir}/${config}"
115 config_get private_key
"${config}" "private_key"
116 config_get listen_port
"${config}" "listen_port"
117 config_get addresses
"${config}" "addresses"
118 config_get mtu
"${config}" "mtu"
119 config_get fwmark
"${config}" "fwmark"
120 config_get ip6prefix
"${config}" "ip6prefix"
121 config_get nohostroute
"${config}" "nohostroute"
122 config_get tunlink
"${config}" "tunlink"
124 ip link del dev
"${config}" 2>/dev
/null
125 ip link add dev
"${config}" type wireguard
127 if [ "${mtu}" ]; then
128 ip link
set mtu
"${mtu}" dev
"${config}"
131 proto_init_update
"${config}" 1
135 echo "[Interface]" > "${wg_cfg}"
136 echo "PrivateKey=${private_key}" >> "${wg_cfg}"
137 if [ "${listen_port}" ]; then
138 echo "ListenPort=${listen_port}" >> "${wg_cfg}"
140 if [ "${fwmark}" ]; then
141 echo "FwMark=${fwmark}" >> "${wg_cfg}"
143 config_foreach proto_wireguard_setup_peer
"wireguard_${config}"
145 # apply configuration file
146 ${WG} setconf ${config} "${wg_cfg}"
151 if [ ${WG_RETURN} -ne 0 ]; then
153 proto_setup_failed "${config}"
157 for address in ${addresses}; do
160 proto_add_ipv6_address "${address%%/*}" "${address##*/}"
163 proto_add_ipv4_address "${address%%/*}" "${address##*/}"
166 proto_add_ipv6_address "${address%%/*}" "128"
169 proto_add_ipv4_address "${address%%/*}" "32"
174 for prefix in ${ip6prefix}; do
175 proto_add_ipv6_prefix "$prefix"
178 # endpoint dependency
179 if [ "${nohostroute}" != "1" ]; then
180 wg show "${config}" endpoints | \
181 sed -E 's/\[?([0-9.:a-f]+)\]?:([0-9]+)/\1 \2/' | \
182 while IFS=$'\t ' read -r key address port; do
183 [ -n "${port}" ] || continue
184 proto_add_host_dependency "${config}" "${address}" "${tunlink}"
188 proto_send_update "${config}"
191 proto_wireguard_teardown() {
193 ip link del dev "${config}" >/dev/null 2>&1
196 [ -n "$INCLUDE_ONLY" ] || {
197 add_protocol wireguard