summaryrefslogtreecommitdiffstats
path: root/net/libreswan/files/etc/init.d/ipsec
blob: 8a94785283b2dfb41c6e6ea8ea2d55b64d34fb2d (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
#!/bin/sh /etc/rc.common

. "${IPKG_INSTROOT}/lib/functions/network.sh"

START=90
STOP=10

USE_PROCD=1

PROG="/usr/libexec/ipsec/pluto"
IPSEC_BIN="/usr/sbin/ipsec"

IPSEC_DIR="/var/run/ipsec"
IPSEC_CONF="$IPSEC_DIR/setup.conf"
IPSEC_CONF_DIR="$IPSEC_DIR/conf.d"

IPSEC_AUTO="${IPSEC_BIN} auto"

extra_command "start_tunnel" "Start ipsec tunnel"
extra_command "stop_tunnel" "Stop ipsec tunnel"
extra_command "reload_tunnel" "Reload/restart ipsec tunnel"

set_var() {
	export "$1=$2"
}

get_var() {
	local var

	var=$(eval echo "\"\${${1}}\"")
	[ "$var" = "1" ] && return 0

	return 1
}

set_restart_flag() {
	set_var "RESTART_IPSEC" 1
}

restart_flag() {
	get_var RESTART_IPSEC
}

set_replace_flag() {
	set_var "REPLACE_${1}" 1
}

replace_flag() {
	get_var "REPLACE_${1}"
}

checkconfig() {
	${IPSEC_BIN} addconn --checkconfig || return 1
	mkdir -p /var/run/pluto
}

expand_ike() {
	local id="$1"
	local encryption_algorithm hash_algorithm dh_group proposal

	config_get encryption_algorithm "${id}" encryption_algorithm
	config_get hash_algorithm "${id}" hash_algorithm
	config_get dh_group "${id}" dh_group

	encryption_algorithm="${encryption_algorithm% *}"
	proposal="${encryption_algorithm:+${encryption_algorithm}${hash_algorithm:+-${hash_algorithm}${dh_group:+;${dh_group%% *}}}}"
	append ike_proposal "$proposal" ","
}

expand_phase2alg() {
	local id="$1"
	local encryption_algorithm hash_algorithm dh_group

	config_get encryption_algorithm "${id}" encryption_algorithm
	config_get hash_algorithm "${id}" hash_algorithm
	config_get dh_group "${id}" dh_group

	phase2alg_proposal="${encryption_algorithm:+${encryption_algorithm// /+}${hash_algorithm:+-${hash_algorithm// /+}${dh_group:+-${dh_group// /+}}}}"
}

generate_tunnel_config() {
	local id=$1
	local config_file="$IPSEC_CONF_DIR/$id.conf"
	local secret_file="$IPSEC_CONF_DIR/$id.secret"
	local tmp_config_file="/tmp/$id.conf"
	local tmp_secret_file="/tmp/$id.secret"
	local ikey mark_in okey mark_out ifid

	config_get auto "$id" auto
	config_get left "$id" left
	config_get left_interface "$id" left_interface
	[ -n "$left_interface" ] && network_get_ipaddr left "$left_interface"
	config_get right "$id" right
	config_get leftid "$id" leftid "$left"
	config_get rightid "$id" rightid "$right"
	config_get leftsourceip "$id" leftsourceip
	config_get rightsourceip "$id" rightsourceip
	config_get leftsubnets "$id" leftsubnets
	config_get rightsubnets "$id" rightsubnets
	config_get_bool ikev2 "$id" ikev2
	[ "$ikev2" = "1" ] && ikev2=yes || ikev2=no
	config_get_bool rekey "$id" rekey
	[ "$rekey" = "1" ] && rekey=yes || rekey=no
	config_get ikelifetime "$id" ikelifetime
	config_get rekeymargin "$id" rekeymargin
	config_get dpdaction "$id" dpdaction
	config_get dpdtimeout "$id" dpdtimeout
	config_get dpddelay "$id" dpddelay
	config_get phase2 "$id" phase2
	config_get phase2alg "$id" phase2alg
	config_get nflog "$id" nflog 0
	[ "$nflog" = "0" ] && unset nflog

	config_list_foreach "$id" ike expand_ike
	config_list_foreach "$id" phase2alg expand_phase2alg

	config_get authby "$id" authby
	config_get psk "$id" psk

	if [ -n "$leftsubnets" ]; then
		[[ "$leftsubnets" =~ 0.0.0.0* ]] && leftsubnets="0.0.0.0/0"
		leftsubnets="{${leftsubnets// /,}}"
	fi

	if [ -n "$rightsubnets" ]; then
		[[ "$rightsubnets" =~ 0.0.0.0* ]] && rightsubnets="0.0.0.0/0"
		rightsubnets="{${rightsubnets// /,}}"
	fi

	config_get interface "$id" interface

	cat << EOF > "$tmp_secret_file"
$leftid $rightid : PSK "$psk"
EOF

	cat << EOF > "$tmp_config_file"
conn $id
	auto=${auto}
	authby=${authby}
	ikev2=${ikev2}
	left=${left%% *}
	${leftid:+leftid=${leftid}}
	${leftsourceip:+leftsourceip=${leftsourceip}}
	${leftsubnets:+leftsubnets=${leftsubnets}}
	right=${right%% *}
	${rightid:+rightid=${rightid}}
	${rightsourceip:+rightsourceip=${rightsourceip}}
	${rightsubnets:+rightsubnets=${rightsubnets}}
	${dpdaction:+dpdaction=${dpdaction}}
	${dpdtimeout:+dpdtimeout=${dpdtimeout}}
	${dpddelay:+dpddelay=${dpddelay}}
	${ikelifetime:+ikelifetime=${ikelifetime}}
	${rekey:+rekey=${rekey}}
	${rekeymargin:+rekeymargin=${rekeymargin}}
	${rekeyfuzz:+rekeyfuzz=${rekeyfuzz}}
	${phase2:+phase2=${phase2}}
	${ike_proposal:+ike=${ike_proposal}}
	${phase2alg_proposal:+phase2alg=${phase2alg_proposal}}
	${nflog:+nflog=${nflog}}
EOF

	if [ -n "$interface" ]; then
		proto=$(uci_get network "$interface" proto)
		case "$proto" in
			vti)
				ikey=$(uci_get network "$interface" ikey)
				okey=$(uci_get network "$interface" okey)
				mark_in=$(printf "0x%x" $ikey)
				mark_out=$(printf "0x%x" $okey)
				echo -e "${mark_in:+\tmark-in=${mark_in}}" >> "$tmp_config_file"
				echo -e "${mark_out:+\tmark-out=${mark_out}}" >> "$tmp_config_file"
				echo -e "${interface:+\tvti-interface=${interface}}" >> "$tmp_config_file"
				;;
			xfrm)
				ifid=$(uci_get network "$interface" ifid)
				echo -e "${ifid:+\tipsec-interface=${ifid}}" >> "$tmp_config_file"
				;;
		esac
	fi


	[ -f "$config_file" ] && {
		cmp "$config_file" "$tmp_config_file" 2>/dev/null && rm -f "$tmp_config_file"
	}

	[ -f "$secret_file" ] && {
		cmp "$secret_file" "$tmp_secret_file" 2>/dev/null && rm -f "$tmp_secret_file"
	}

	[ -f "$tmp_config_file" ] && mv "$tmp_config_file" "$config_file" && set_replace_flag "$id"
	[ -f "$tmp_secret_file" ] && mv "$tmp_secret_file" "$secret_file" && set_replace_flag "$id"

	unset ike_proposal phase2alg_proposal
}

generate_daemon_config() {
	local tmp_config_file="/tmp/setup.conf"

	config_get_bool debug globals debug 0
	[ "$debug" = "0" ] && debug=none || debug=all
	config_get_bool uniqueids globals uniqueids 0
	[ "$uniqueids" = "0" ] && uniqueids=no || uniqueids=yes
	config_get listen globals listen
	config_get listen_interface globals listen_interface
	[ -n "$listen_interface" ] && network_get_ipaddr listen "$listen_interface"
	config_get virtual_private globals virtual_private
	[ -z "$virtual_private" ] && virtual_private='10.0.0.0/8 192.168.0.0/16 172.16.0.0/12 25.0.0.0/8 100.64.0.0/10 !100.64.0.0/24'
	config_get nflog_all globals nflog_all 0
	[ "$nflog_all" = "0" ] && unset nflog_all

	[ ! -d $IPSEC_DIR ] && mkdir -p $IPSEC_DIR
	[ ! -d $IPSEC_CONF_DIR ] && mkdir -p $IPSEC_CONF_DIR

	cat << EOF > "$tmp_config_file"
config setup
	${debug:+plutodebug=${debug}}
	${uniqueids:+uniqueids=${uniqueids}}
	${listen:+listen=${listen}}
	${virtual_private:+virtual-private=%v4:${virtual_private// /,%v4:}}
	${nflog_all:+nflog-all=${nflog_all}}
EOF

	if ! cmp "$IPSEC_CONF" "$tmp_config_file" 2>/dev/null; then
		mv "$tmp_config_file" "$IPSEC_CONF"
		set_restart_flag 1
	else
		rm -f "$tmp_config_file"
	fi

	return 0
}

clean_config() {
	rm -f $IPSEC_CONF_DIR/*.conf $IPSEC_CONF_DIR/*.secret
}

config_cb() {
	local var="CONFIG_${1}_SECTIONS"
	export $var
	append "$var" "$2"
}

generate_config() {
	config_load libreswan
	generate_daemon_config
	config_foreach generate_tunnel_config tunnel
}

regenerate_config() {
	clean_config
	generate_config
}

active_conns() {
	local active_conns file _file

	active_conns=$(${IPSEC_BIN} --trafficstatus | awk -F'[":/]' '{print $3}' | sort -u)

	for file in $IPSEC_CONF_DIR/*.conf; do
		_file="${file##*/}"
		list_contains active_conns "${_file%%.*}" || append active_conns "${_file%%.*}"
	done

	echo "$active_conns"
}

start_service() {
	generate_config
	checkconfig || return 1

	${IPSEC_BIN} _stackmanager start

	procd_open_instance
	procd_set_param command $PROG --nofork
	procd_set_param respawn
	procd_close_instance
}

stop_service() {
	${IPSEC_BIN} whack --shutdown
	${IPSEC_BIN} _stackmanager stop
}

stop_tunnel() {
	${IPSEC_AUTO} --delete "$1" > /dev/null 2>&1
	rm -f ${IPSEC_CONF_DIR}/$1.*
}

start_tunnel() {
	generate_tunnel_config "$1"
	${IPSEC_AUTO} --add "$1" > /dev/null 2>&1
	${IPSEC_AUTO} --rereadsecrets
	${IPSEC_AUTO} --up "$1" > /dev/null 2>&1 &
}

reload_tunnel() {
	generate_tunnel_config "$1"

	replace_flag "$1" || return 0

	${IPSEC_AUTO} --rereadsecrets
	${IPSEC_AUTO} --replace "$1" > /dev/null 2>&1
	${IPSEC_AUTO} --up "$1" > /dev/null 2>&1 &
}

reload_service() {
	local active_tunnels uci_tunnels
	uci_tunnels="$@"

	config_load libreswan
	generate_daemon_config

	if restart_flag; then
		restart
		return 0
	fi

	[ -z "$uci_tunnels" ] && config_get uci_tunnels tunnel SECTIONS

	active_tunnels="$(active_conns)"

	for tunnel in $active_tunnels; do
		list_contains uci_tunnels "$tunnel" || stop_tunnel "$tunnel"
	done

	for tunnel in $uci_tunnels; do
		if list_contains active_tunnels "$tunnel"; then
			reload_tunnel "$tunnel"
		else
			start_tunnel "$tunnel"
		fi
	done
}

service_triggers() {
	procd_add_reload_trigger 'libreswan'
}