blob: e39c4d97a578a5c7e21b3d5767b0606feba62d67 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
#!/bin/sh /etc/rc.common
# Copyright (c) 2012-2015, Matthias Schiffer <mschiffer@universe-factory.net>
START=95
SERVICE_USE_PID=1
EXTRA_COMMANDS="up down show_key generate_key"
LIST_SEP="
"
TMP_FASTD=/tmp/fastd
FASTD_COMMAND=/usr/bin/fastd
section_enabled() {
config_get_bool enabled "$1" 'enabled' 0
[ $enabled -gt 0 ]
}
error() {
echo "${initscript}:" "$@" 1>&2
}
get_key_instance() {
local s="$1"
config_get secret "$s" secret
if [ "$secret" = 'generate' ]; then
secret=`"$FASTD_COMMAND" --generate-key --machine-readable`
uci -q set fastd."$s".secret="$secret" && uci -q commit fastd
fi
echo "$secret"
}
escape_string() {
local t=${1//\\/\\\\}
echo -n "\"${t//\"/\\\"}\""
}
guard_value() {
local t=${1//[^-a-z0-9\[\].:]/}
echo -n "$t"
}
guard_quotes() {
local t=${1//[^-a-zA-Z0-9\[\].:\"% ]/}
local quotes=${t//[^\"]/}
if [ "${#quotes}" = 0 -o "${#quotes}" = 2 ]; then
echo -n "$t"
fi
}
yes_no() {
case "$1" in
0|no|off|false|disabled) echo -n no;;
*) echo -n yes;;
esac
}
config_string_config='include $(escape_string "$value");'
config_string_config_peer='include peer $(escape_string "$value");'
config_string_config_peer_dir='include peers from $(escape_string "$value");'
config_string_bind='bind $(guard_quotes "$value");'
config_string_method='method $(escape_string "$value");'
config_string_syslog_level='log to syslog level $(guard_value "$value");'
config_string_mode='mode $(guard_value "$value");'
config_string_interface='interface $(escape_string "$value");'
config_string_mtu='mtu $(guard_value "$value");'
config_string_peer_limit='peer limit $(guard_value "$value");'
config_string_user='user $(escape_string "$value");'
config_string_group='group $(escape_string "$value");'
config_string_status_socket='status socket $(escape_string "$value");'
config_string_pmtu='pmtu $(yes_no "$value");'
config_string_forward='forward $(yes_no "$value");'
config_string_hide_ip_addresses='hide ip addresses $(yes_no "$value");'
config_string_hide_mac_addresses='hide mac addresses $(yes_no "$value");'
config_string_secure_handshakes='secure handshakes $(yes_no "$value");'
config_string_packet_mark='packet mark $(guard_value "$value");'
config_string_on_pre_up='on pre-up $(escape_string "$value");'
config_string_on_up='on up $(escape_string "$value");'
config_string_on_down='on down $(escape_string "$value");'
config_string_on_post_down='on post-down $(escape_string "$value");'
config_string_on_connect='on connect $(escape_string "$value");'
config_string_on_establish='on establish $(escape_string "$value");'
config_string_on_disestablish='on disestablish $(escape_string "$value");'
config_string_on_verify='on verify $(escape_string "$value");'
config_string_peer='peer $(escape_string "$value") {'
config_string_peer_group='peer group $(escape_string "$value") {'
peer_string_key='key $(escape_string "$value");'
peer_string_float='float $(yes_no "$value");'
peer_string_remote='remote $(guard_quotes "$value");'
generate_option() {
local __string=$(eval echo \"\$$2\")
local value="$1";
eval echo "\"$__string\""
}
append_option() {
local v; local len; local s="$1"; local prefix="$2"; local p="$3"
config_get len "$s" "${p}_LENGTH"
if [ -z "$len" ]; then
config_get v "$s" "$p"
[ -n "$v" ] && generate_option "$v" "${prefix}_string_${p}"
else
config_list_foreach "$s" "$p" generate_option "${prefix}_string_${p}"
fi
}
append_options() {
local p; local s="$1"; local prefix="$2"; shift; shift
for p in $*; do
append_option "$s" "$prefix" "$p"
done
}
generate_config_secret() {
echo "secret $(escape_string "$1");"
}
generate_peer_config() {
local peer="$1"
# These options are deprecated
config_get address "$peer" address
config_get hostname "$peer" hostname
config_get address_family "$peer" address_family
config_get port "$peer" port
if [ "$address" -o "$hostname" ]; then
if [ -z "$port" ]; then
error "peer $peer: address or hostname, but no port given"
return 1
fi
if [ "$hostname" ]; then
generate_option peer_string_remote "$address_family \"$hostname\" port $port"
fi
if [ "$address" ]; then
generate_option peer_string_remote "$address port $port"
fi
fi
append_options "$peer" peer \
key float remote
}
generate_single_peer_config() {
local peer="$1"; local net="$2"
config_get peer_net "$peer" net
config_get peer_group "$peer" group
[ "$net" = "$peer_net" -a "$peer_group" = '' ] || return 0
section_enabled "$peer" || return 0
generate_option "$peer" config_string_peer
generate_peer_config "$peer"
echo '}'
}
create_peer_config() {
local peer="$1"; local net="$2"; local group="$3"; local path="$4"
config_get peer_net "$peer" net
config_get peer_group "$peer" group
[ "$group" = "$peer_group" ] || return 0
if [ "$net" != "$peer_net" ]; then
[ -z "$group" ] || error "warning: the peer group of peer '$peer' doesn't match its net, the peer will be ignored"
return 0
fi
section_enabled "$peer" || return 0
generate_peer_config "$peer" >"$path/$peer"
}
update_peer_group() {
local net="$1"; local group_dir="$2"; local group="$3"; local update_only="$4"
local path="$TMP_FASTD/fastd.$net/$group_dir"
rm -rf "$path"
mkdir -p "$path"
config_foreach create_peer_config 'peer' "$net" "$group" "$path"
if [ -z "$update_only" ]; then
generate_option "$path" config_string_config_peer_dir
fi
config_foreach generate_peer_group_config 'peer_group' "$net" "$group_dir" "$update_only" "$group"
}
generate_peer_group_config() {
local group="$1"; local net="$2"; local group_dir="$3%$group"; local update_only="$4"; local parent="$5"
config_get group_net "$group" net
config_get group_parent "$group" parent
[ "$parent" = "$group_parent" ] || return 0
if [ "$net" != "$peer_net" ]; then
[ -z "$parent" ] || error "warning: the parent of peer group '$group' doesn't match its net, the peer group will be ignored"
return 0
fi
section_enabled "$group" || return 0
if [ -z "$update_only" ]; then
generate_option "$group" config_string_peer_group
append_options "$group" config \
config config_peer config_peer_dir method peer_limit
fi
update_peer_group "$net" "$group_dir" "$group" "$update_only"
if [ -z "$update_only" ]; then
echo '}'
fi
}
update_peer_groups() {
local net="$1"; local update_only="$2"
update_peer_group "$net" 'peers' '' "$update_only"
}
generate_config() {
local s="$1"
generate_option 'info' config_string_syslog_level
append_options "$s" config \
config config_peer config_peer_dir bind method syslog_level mode interface mtu peer_limit \
user group status_socket pmtu forward hide_ip_addresses hide_mac_addresses secure_handshakes packet_mark \
on_pre_up on_up on_down on_post_down on_connect on_establish on_disestablish on_verify
config_get mode "$s" mode
if [ "$mode" = "tun" ]; then
config_foreach generate_single_peer_config 'peer' "$s"
else
update_peer_groups "$s"
fi
}
generate_key_instance() {
local s="$1"
config_get secret "$s" secret
if [ -z "$secret" -o "$secret" = 'generate' ]; then
secret=`fastd --generate-key --machine-readable`
uci -q set fastd."$s".secret="$secret" && uci -q commit fastd
fi
generate_config_secret "$secret" | "$FASTD_COMMAND" --config - --show-key --machine-readable
}
show_key_instance() {
local s="$1"
local secret=`get_key_instance "$s"`
if [ -z "$secret" ]; then
error "$s: secret is not set"
return 1
fi
generate_config_secret "$secret" | "$FASTD_COMMAND" --config - --show-key --machine-readable
}
start_instance() {
local s="$1"
section_enabled "$s" || return 1
SERVICE_PID_FILE="/var/run/fastd.$s.pid"
config_get interface "$s" interface
if [ -z "$interface" ]; then
error "$s: interface is not set"
return 1
fi
if ifconfig "$interface" &>/dev/null; then
error "$s: interface '$interface' is already in use"
return 1
fi
config_get mode "$s" mode
if [ -z "$mode" ]; then
error "$s: mode is not set"
return 1
fi
local secret=`get_key_instance "$s"`
if [ -z "$secret" ]; then
error "$s: secret is not set"
return 1
fi
rm -f "$SERVICE_PID_FILE"
touch "$SERVICE_PID_FILE"
config_get user "$s" user
if [ "$user" ]; then
chown "$user" "$SERVICE_PID_FILE"
fi
(generate_config_secret "$secret"; generate_config "$s") | service_start "$FASTD_COMMAND" --config - --daemon --pid-file "$SERVICE_PID_FILE"
if ! ifconfig "$interface" >/dev/null 2>&1; then
error "$s: startup failed"
return 1
fi
config_get up "$s" up
[ -n "$up" ] && sh -c "$up" - "$interface"
}
stop_instance() {
local s="$1"
section_enabled "$s" || return 1
SERVICE_PID_FILE="/var/run/fastd.$s.pid"
config_get interface "$s" interface
if [ -z "$interface" ]; then
error "$s: interface is not set"
return 1
fi
if ! ifconfig "$interface" &>/dev/null; then
error "$s: interface '$interface' does not exist"
return 1
fi
config_get down "$s" down
[ -n "$down" ] && sh -c "$down" - "$interface"
service_stop "$FASTD_COMMAND"
rm -rf "$TMP_FASTD/fastd.$s"
}
reload_instance() {
local s="$1"
section_enabled "$s" || return 1
config_get mode "$s" mode
[ "$mode" = "tun" ] && return 1
update_peer_groups "$s" true
SERVICE_PID_FILE="/var/run/fastd.$s.pid"
service_reload "$FASTD_COMMAND"
}
start() {
config_load 'fastd'
config_foreach start_instance 'fastd'
return 0
}
stop() {
config_load 'fastd'
config_foreach stop_instance 'fastd'
return 0
}
reload() {
config_load 'fastd'
config_foreach reload_instance 'fastd'
return 0
}
up() {
local exists
local instance
config_load 'fastd'
for instance in "$@"; do
config_get exists "$instance" 'TYPE'
if [ "$exists" = 'fastd' ]; then
start_instance "$instance"
fi
done
}
down() {
local exists
local instance
config_load 'fastd'
for instance in "$@"; do
config_get exists "$instance" 'TYPE'
if [ "$exists" = 'fastd' ]; then
stop_instance "$instance"
fi
done
}
show_key() {
local exists
local instance
config_load 'fastd'
for instance in "$@"; do
config_get exists "$instance" 'TYPE'
if [ "$exists" = 'fastd' ]; then
show_key_instance "$instance"
fi
done
}
generate_key() {
local exists
local instance
config_load 'fastd'
for instance in "$@"; do
config_get exists "$instance" 'TYPE'
if [ "$exists" = 'fastd' ]; then
generate_key_instance "$instance"
fi
done
}
|