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