wireguard-tools: add uci option to disable wireguard peers
[openwrt/openwrt.git] / package / network / utils / wireguard-tools / files / wireguard.sh
1 #!/bin/sh
2 # Copyright 2016-2017 Dan Luedtke <mail@danrl.com>
3 # Licensed to the public under the Apache License 2.0.
4
5 WG=/usr/bin/wg
6 if [ ! -x $WG ]; then
7 logger -t "wireguard" "error: missing wireguard-tools (${WG})"
8 exit 0
9 fi
10
11 [ -n "$INCLUDE_ONLY" ] || {
12 . /lib/functions.sh
13 . ../netifd-proto.sh
14 init_proto "$@"
15 }
16
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"
22 available=1
23 no_proto_task=1
24 }
25
26 proto_wireguard_setup_peer() {
27 local peer_config="$1"
28
29 local disabled
30 local public_key
31 local preshared_key
32 local allowed_ips
33 local route_allowed_ips
34 local endpoint_host
35 local endpoint_port
36 local persistent_keepalive
37
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"
46
47 if [ "${disabled}" -eq 1 ]; then
48 # skip disabled peers
49 return 0
50 fi
51
52 if [ -z "$public_key" ]; then
53 echo "Skipping peer config $peer_config because public key is not defined."
54 return 0
55 fi
56
57 echo "[Peer]" >> "${wg_cfg}"
58 echo "PublicKey=${public_key}" >> "${wg_cfg}"
59 if [ "${preshared_key}" ]; then
60 echo "PresharedKey=${preshared_key}" >> "${wg_cfg}"
61 fi
62 for allowed_ip in $allowed_ips; do
63 echo "AllowedIPs=${allowed_ip}" >> "${wg_cfg}"
64 done
65 if [ "${endpoint_host}" ]; then
66 case "${endpoint_host}" in
67 *:*)
68 endpoint="[${endpoint_host}]"
69 ;;
70 *)
71 endpoint="${endpoint_host}"
72 ;;
73 esac
74 if [ "${endpoint_port}" ]; then
75 endpoint="${endpoint}:${endpoint_port}"
76 else
77 endpoint="${endpoint}:51820"
78 fi
79 echo "Endpoint=${endpoint}" >> "${wg_cfg}"
80 fi
81 if [ "${persistent_keepalive}" ]; then
82 echo "PersistentKeepalive=${persistent_keepalive}" >> "${wg_cfg}"
83 fi
84
85 if [ ${route_allowed_ips} -ne 0 ]; then
86 for allowed_ip in ${allowed_ips}; do
87 case "${allowed_ip}" in
88 *:*/*)
89 proto_add_ipv6_route "${allowed_ip%%/*}" "${allowed_ip##*/}"
90 ;;
91 *.*/*)
92 proto_add_ipv4_route "${allowed_ip%%/*}" "${allowed_ip##*/}"
93 ;;
94 *:*)
95 proto_add_ipv6_route "${allowed_ip%%/*}" "128"
96 ;;
97 *.*)
98 proto_add_ipv4_route "${allowed_ip%%/*}" "32"
99 ;;
100 esac
101 done
102 fi
103 }
104
105 proto_wireguard_setup() {
106 local config="$1"
107 local wg_dir="/tmp/wireguard"
108 local wg_cfg="${wg_dir}/${config}"
109
110 local private_key
111 local listen_port
112 local mtu
113
114 config_load network
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"
123
124 ip link del dev "${config}" 2>/dev/null
125 ip link add dev "${config}" type wireguard
126
127 if [ "${mtu}" ]; then
128 ip link set mtu "${mtu}" dev "${config}"
129 fi
130
131 proto_init_update "${config}" 1
132
133 umask 077
134 mkdir -p "${wg_dir}"
135 echo "[Interface]" > "${wg_cfg}"
136 echo "PrivateKey=${private_key}" >> "${wg_cfg}"
137 if [ "${listen_port}" ]; then
138 echo "ListenPort=${listen_port}" >> "${wg_cfg}"
139 fi
140 if [ "${fwmark}" ]; then
141 echo "FwMark=${fwmark}" >> "${wg_cfg}"
142 fi
143 config_foreach proto_wireguard_setup_peer "wireguard_${config}"
144
145 # apply configuration file
146 ${WG} setconf ${config} "${wg_cfg}"
147 WG_RETURN=$?
148
149 rm -f "${wg_cfg}"
150
151 if [ ${WG_RETURN} -ne 0 ]; then
152 sleep 5
153 proto_setup_failed "${config}"
154 exit 1
155 fi
156
157 for address in ${addresses}; do
158 case "${address}" in
159 *:*/*)
160 proto_add_ipv6_address "${address%%/*}" "${address##*/}"
161 ;;
162 *.*/*)
163 proto_add_ipv4_address "${address%%/*}" "${address##*/}"
164 ;;
165 *:*)
166 proto_add_ipv6_address "${address%%/*}" "128"
167 ;;
168 *.*)
169 proto_add_ipv4_address "${address%%/*}" "32"
170 ;;
171 esac
172 done
173
174 for prefix in ${ip6prefix}; do
175 proto_add_ipv6_prefix "$prefix"
176 done
177
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}"
185 done
186 fi
187
188 proto_send_update "${config}"
189 }
190
191 proto_wireguard_teardown() {
192 local config="$1"
193 ip link del dev "${config}" >/dev/null 2>&1
194 }
195
196 [ -n "$INCLUDE_ONLY" ] || {
197 add_protocol wireguard
198 }