wireguard: converted whitespaces from space to tab
[openwrt/openwrt.git] / package / network / services / wireguard / 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 public_key
30 local preshared_key
31 local allowed_ips
32 local route_allowed_ips
33 local endpoint_host
34 local endpoint_port
35 local persistent_keepalive
36
37 config_get public_key "${peer_config}" "public_key"
38 config_get preshared_key "${peer_config}" "preshared_key"
39 config_get allowed_ips "${peer_config}" "allowed_ips"
40 config_get_bool route_allowed_ips "${peer_config}" "route_allowed_ips" 0
41 config_get endpoint_host "${peer_config}" "endpoint_host"
42 config_get endpoint_port "${peer_config}" "endpoint_port"
43 config_get persistent_keepalive "${peer_config}" "persistent_keepalive"
44
45 # peer configuration
46 echo "[Peer]" >> "${wg_cfg}"
47 echo "PublicKey=${public_key}" >> "${wg_cfg}"
48 if [ "${preshared_key}" ]; then
49 echo "PresharedKey=${preshared_key}" >> "${wg_cfg}"
50 fi
51 for allowed_ip in $allowed_ips; do
52 echo "AllowedIPs=${allowed_ip}" >> "${wg_cfg}"
53 done
54 if [ "${endpoint_host}" ]; then
55 case "${endpoint_host}" in
56 *:*)
57 endpoint="[${endpoint_host}]"
58 ;;
59 *)
60 endpoint="${endpoint_host}"
61 ;;
62 esac
63 if [ "${endpoint_port}" ]; then
64 endpoint="${endpoint}:${endpoint_port}"
65 else
66 endpoint="${endpoint}:51820"
67 fi
68 echo "Endpoint=${endpoint}" >> "${wg_cfg}"
69 fi
70 if [ "${persistent_keepalive}" ]; then
71 echo "PersistentKeepalive=${persistent_keepalive}" >> "${wg_cfg}"
72 fi
73
74 # add routes for allowed ips
75 if [ ${route_allowed_ips} -ne 0 ]; then
76 for allowed_ip in ${allowed_ips}; do
77 case "${allowed_ip}" in
78 *:*/*)
79 proto_add_ipv6_route "${allowed_ip%%/*}" "${allowed_ip##*/}"
80 ;;
81 *.*/*)
82 proto_add_ipv4_route "${allowed_ip%%/*}" "${allowed_ip##*/}"
83 ;;
84 *:*)
85 proto_add_ipv6_route "${allowed_ip%%/*}" "128"
86 ;;
87 *.*)
88 proto_add_ipv4_route "${allowed_ip%%/*}" "32"
89 ;;
90 esac
91 done
92 fi
93 }
94
95 proto_wireguard_setup() {
96 local config="$1"
97 local wg_dir="/tmp/wireguard"
98 local wg_cfg="${wg_dir}/${config}"
99
100 local private_key
101 local listen_port
102 local mtu
103
104 # load configuration
105 config_load network
106 config_get private_key "${config}" "private_key"
107 config_get listen_port "${config}" "listen_port"
108 config_get addresses "${config}" "addresses"
109 config_get mtu "${config}" "mtu"
110 config_get fwmark "${config}" "fwmark"
111 config_get ip6prefix "${config}" "ip6prefix"
112 config_get nohostroute "${config}" "nohostroute"
113
114 # create interface
115 ip link del dev "${config}" 2>/dev/null
116 ip link add dev "${config}" type wireguard
117
118 if [ "${mtu}" ]; then
119 ip link set mtu "${mtu}" dev "${config}"
120 fi
121
122 proto_init_update "${config}" 1
123
124 # generate configuration file
125 umask 077
126 mkdir -p "${wg_dir}"
127 echo "[Interface]" > "${wg_cfg}"
128 echo "PrivateKey=${private_key}" >> "${wg_cfg}"
129 if [ "${listen_port}" ]; then
130 echo "ListenPort=${listen_port}" >> "${wg_cfg}"
131 fi
132 if [ "${fwmark}" ]; then
133 echo "FwMark=${fwmark}" >> "${wg_cfg}"
134 fi
135 config_foreach proto_wireguard_setup_peer "wireguard_${config}"
136
137 # apply configuration file
138 ${WG} setconf ${config} "${wg_cfg}"
139 WG_RETURN=$?
140
141 # delete configuration file
142 rm -f "${wg_cfg}"
143
144 # check status
145 if [ ${WG_RETURN} -ne 0 ]; then
146 sleep 5
147 proto_setup_failed "${config}"
148 exit 1
149 fi
150
151 # add ip addresses
152 for address in ${addresses}; do
153 case "${address}" in
154 *:*/*)
155 proto_add_ipv6_address "${address%%/*}" "${address##*/}"
156 ;;
157 *.*/*)
158 proto_add_ipv4_address "${address%%/*}" "${address##*/}"
159 ;;
160 *:*)
161 proto_add_ipv6_address "${address%%/*}" "128"
162 ;;
163 *.*)
164 proto_add_ipv4_address "${address%%/*}" "32"
165 ;;
166 esac
167 done
168
169 # support ip6 prefixes
170 for prefix in ${ip6prefix}; do
171 proto_add_ipv6_prefix "$prefix"
172 done
173
174 # endpoint dependency
175 if [ "${nohostroute}" != "1" ]; then
176 wg show "${config}" endpoints | \
177 sed -E 's/\[?([0-9.:a-f]+)\]?:([0-9]+)/\1 \2/' | \
178 while IFS=$'\t ' read -r key address port; do
179 [ -n "${port}" ] || continue
180 proto_add_host_dependency "${config}" "${address}"
181 done
182 fi
183
184 proto_send_update "${config}"
185 }
186
187 proto_wireguard_teardown() {
188 local config="$1"
189 ip link del dev "${config}" >/dev/null 2>&1
190 }
191
192 [ -n "$INCLUDE_ONLY" ] || {
193 add_protocol wireguard
194 }