t_ipsec_l2tp.sh revision 1.2.2.3 1 # $NetBSD: t_ipsec_l2tp.sh,v 1.2.2.3 2017/05/11 02:58:42 pgoyette Exp $
2 #
3 # Copyright (c) 2017 Internet Initiative Japan Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
26 #
27
28 SOCK_LOCAL=unix://ipsec_l2tp_local
29 SOCK_TUN_LOCAL=unix://ipsec_l2tp_tunel_local
30 SOCK_TUN_REMOTE=unix://ipsec_l2tp_tunnel_remote
31 SOCK_REMOTE=unix://ipsec_l2tp_remote
32 BUS_LOCAL=./bus_ipsec_local
33 BUS_TUNNEL=./bus_ipsec_tunnel
34 BUS_REMOTE=./bus_ipsec_remote
35
36 DEBUG=${DEBUG:-false}
37
38 make_l2tp_pktstr()
39 {
40 local src=$1
41 local dst=$2
42 local proto=$3
43 local ipproto=$4
44 local mode=$5
45 local proto_cap= proto_str=
46
47 if [ $proto = esp ]; then
48 proto_cap=ESP
49 else
50 proto_cap=AH
51 if [ $ipproto = ipv4 ]; then
52 if [ $mode = tunnel ]; then
53 proto_str="ip-proto-115 102 \(ipip-proto-4\)"
54 else
55 proto_str="ip-proto-115 102"
56 fi
57 else
58 proto_str="ip-proto-115"
59 fi
60 fi
61
62 echo "$src > $dst: $proto_cap.+$proto_str"
63 }
64
65 test_ipsec4_l2tp()
66 {
67 local mode=$1
68 local proto=$2
69 local algo=$3
70 local ip_local=10.0.0.1
71 local ip_gwlo_tun=20.0.0.1
72 local ip_gwre_tun=20.0.0.2
73 local ip_remote=10.0.0.2
74 local subnet_local=20.0.0.0
75 local subnet_remote=20.0.0.0
76 local keylen=$(get_one_valid_keylen $algo)
77 local key=$(generate_key $keylen)
78 local tmpfile=./tmp
79 local outfile=./out
80 local opt= str=
81
82 if [ $proto = esp ]; then
83 opt=-E
84 else
85 opt=-A
86 fi
87
88 # See https://www.netbsd.org/docs/network/ipsec/#sample_vpn
89 rump_server_crypto_start $SOCK_LOCAL
90 rump_server_crypto_start $SOCK_TUN_LOCAL netipsec l2tp bridge
91 rump_server_crypto_start $SOCK_TUN_REMOTE netipsec l2tp bridge
92 rump_server_crypto_start $SOCK_REMOTE
93 rump_server_add_iface $SOCK_LOCAL shmif0 $BUS_LOCAL
94 rump_server_add_iface $SOCK_TUN_LOCAL shmif0 $BUS_LOCAL
95 rump_server_add_iface $SOCK_TUN_LOCAL shmif1 $BUS_TUNNEL
96 rump_server_add_iface $SOCK_TUN_REMOTE shmif0 $BUS_REMOTE
97 rump_server_add_iface $SOCK_TUN_REMOTE shmif1 $BUS_TUNNEL
98 rump_server_add_iface $SOCK_REMOTE shmif0 $BUS_REMOTE
99
100 export RUMP_SERVER=$SOCK_LOCAL
101 atf_check -s exit:0 rump.ifconfig shmif0 $ip_local/24
102
103 export RUMP_SERVER=$SOCK_TUN_LOCAL
104 atf_check -s exit:0 rump.ifconfig shmif0 up
105 atf_check -s exit:0 rump.ifconfig shmif1 $ip_gwlo_tun/24
106 atf_check -s exit:0 rump.ifconfig l2tp0 create
107 atf_check -s exit:0 rump.ifconfig l2tp0 \
108 tunnel $ip_gwlo_tun $ip_gwre_tun
109 atf_check -s exit:0 rump.ifconfig l2tp0 session 1234 4321
110 atf_check -s exit:0 rump.ifconfig l2tp0 up
111 atf_check -s exit:0 rump.ifconfig bridge0 create
112 atf_check -s exit:0 rump.ifconfig bridge0 up
113 atf_check -s exit:0 $HIJACKING brconfig bridge0 add l2tp0
114 atf_check -s exit:0 $HIJACKING brconfig bridge0 add shmif0
115
116 export RUMP_SERVER=$SOCK_TUN_REMOTE
117 atf_check -s exit:0 rump.ifconfig shmif0 up
118 atf_check -s exit:0 rump.ifconfig shmif1 $ip_gwre_tun/24
119 atf_check -s exit:0 rump.ifconfig l2tp0 create
120 atf_check -s exit:0 rump.ifconfig l2tp0 \
121 tunnel $ip_gwre_tun $ip_gwlo_tun
122 atf_check -s exit:0 rump.ifconfig l2tp0 session 4321 1234
123 atf_check -s exit:0 rump.ifconfig l2tp0 up
124 atf_check -s exit:0 rump.ifconfig bridge0 create
125 atf_check -s exit:0 rump.ifconfig bridge0 up
126 atf_check -s exit:0 $HIJACKING brconfig bridge0 add l2tp0
127 atf_check -s exit:0 $HIJACKING brconfig bridge0 add shmif0
128
129 export RUMP_SERVER=$SOCK_REMOTE
130 atf_check -s exit:0 rump.ifconfig shmif0 $ip_remote/24
131 # Run ifconfig -w 10 just once for optimization
132 atf_check -s exit:0 rump.ifconfig -w 10
133
134 extract_new_packets $BUS_TUNNEL > $outfile
135
136 export RUMP_SERVER=$SOCK_LOCAL
137 atf_check -s exit:0 -o ignore rump.ping -c 1 -n -w 3 $ip_remote
138
139 extract_new_packets $BUS_TUNNEL > $outfile
140 atf_check -s exit:0 \
141 -o match:"$ip_gwlo_tun > $ip_gwre_tun: +ip-proto-115" \
142 cat $outfile
143 atf_check -s exit:0 \
144 -o match:"$ip_gwre_tun > $ip_gwlo_tun: +ip-proto-115" \
145 cat $outfile
146
147 if [ $mode = tunnel ]; then
148 export RUMP_SERVER=$SOCK_TUN_LOCAL
149 # from https://www.netbsd.org/docs/network/ipsec/
150 cat > $tmpfile <<-EOF
151 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
152 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
153 spdadd $subnet_local/24 $subnet_remote/24 any -P out ipsec
154 $proto/tunnel/$ip_gwlo_tun-$ip_gwre_tun/require;
155 spdadd $subnet_remote/24 $subnet_local/24 any -P in ipsec
156 $proto/tunnel/$ip_gwre_tun-$ip_gwlo_tun/require;
157 EOF
158 $DEBUG && cat $tmpfile
159 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
160
161 export RUMP_SERVER=$SOCK_TUN_REMOTE
162 cat > $tmpfile <<-EOF
163 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
164 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
165 spdadd $subnet_remote/24 $subnet_local/24 any -P out ipsec
166 $proto/tunnel/$ip_gwre_tun-$ip_gwlo_tun/require;
167 spdadd $subnet_local/24 $subnet_remote/24 any -P in ipsec
168 $proto/tunnel/$ip_gwlo_tun-$ip_gwre_tun/require;
169 EOF
170 $DEBUG && cat $tmpfile
171 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
172 else # transport mode
173 export RUMP_SERVER=$SOCK_TUN_LOCAL
174 # from https://www.netbsd.org/docs/network/ipsec/
175 cat > $tmpfile <<-EOF
176 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
177 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
178 spdadd $ip_gwlo_tun/32 $ip_gwre_tun/32 any -P out ipsec
179 $proto/transport//require;
180 spdadd $ip_gwre_tun/32 $ip_gwlo_tun/32 any -P in ipsec
181 $proto/transport//require;
182 EOF
183 $DEBUG && cat $tmpfile
184 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
185
186 export RUMP_SERVER=$SOCK_TUN_REMOTE
187 cat > $tmpfile <<-EOF
188 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
189 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
190 spdadd $ip_gwre_tun/32 $ip_gwlo_tun/32 any -P out ipsec
191 $proto/transport//require;
192 spdadd $ip_gwlo_tun/32 $ip_gwre_tun/32 any -P in ipsec
193 $proto/transport//require;
194 EOF
195 $DEBUG && cat $tmpfile
196 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
197 fi
198
199 check_sa_entries $SOCK_TUN_LOCAL $ip_gwlo_tun $ip_gwre_tun
200 check_sa_entries $SOCK_TUN_REMOTE $ip_gwlo_tun $ip_gwre_tun
201
202 export RUMP_SERVER=$SOCK_LOCAL
203 atf_check -s exit:0 -o ignore rump.ping -c 1 -n -w 3 $ip_remote
204
205 extract_new_packets $BUS_TUNNEL > $outfile
206 str=$(make_l2tp_pktstr $ip_gwlo_tun $ip_gwre_tun $proto ipv4 $mode)
207 atf_check -s exit:0 -o match:"$str" cat $outfile
208 str=$(make_l2tp_pktstr $ip_gwre_tun $ip_gwlo_tun $proto ipv4 $mode)
209 atf_check -s exit:0 -o match:"$str" cat $outfile
210
211 test_flush_entries $SOCK_TUN_LOCAL
212 test_flush_entries $SOCK_TUN_REMOTE
213 }
214
215 test_ipsec6_l2tp()
216 {
217 local mode=$1
218 local proto=$2
219 local algo=$3
220 local ip_local=fd00::1
221 local ip_gwlo_tun=fc00::1
222 local ip_gwre_tun=fc00::2
223 local ip_remote=fd00::2
224 local subnet_local=fc00::
225 local subnet_remote=fc00::
226 local keylen=$(get_one_valid_keylen $algo)
227 local key=$(generate_key $keylen)
228 local tmpfile=./tmp
229 local outfile=./out
230 local opt= str=
231
232 if [ $proto = esp ]; then
233 opt=-E
234 else
235 opt=-A
236 fi
237
238 rump_server_crypto_start $SOCK_LOCAL netinet6
239 rump_server_crypto_start $SOCK_TUN_LOCAL netipsec netinet6 l2tp bridge
240 rump_server_crypto_start $SOCK_TUN_REMOTE netipsec netinet6 l2tp bridge
241 rump_server_crypto_start $SOCK_REMOTE netinet6
242 rump_server_add_iface $SOCK_LOCAL shmif0 $BUS_LOCAL
243 rump_server_add_iface $SOCK_TUN_LOCAL shmif0 $BUS_LOCAL
244 rump_server_add_iface $SOCK_TUN_LOCAL shmif1 $BUS_TUNNEL
245 rump_server_add_iface $SOCK_TUN_REMOTE shmif0 $BUS_REMOTE
246 rump_server_add_iface $SOCK_TUN_REMOTE shmif1 $BUS_TUNNEL
247 rump_server_add_iface $SOCK_REMOTE shmif0 $BUS_REMOTE
248
249 export RUMP_SERVER=$SOCK_LOCAL
250 atf_check -s exit:0 rump.ifconfig shmif0 inet6 $ip_local/64
251
252 export RUMP_SERVER=$SOCK_TUN_LOCAL
253 atf_check -s exit:0 rump.ifconfig shmif0 up
254 atf_check -s exit:0 rump.ifconfig shmif1 inet6 $ip_gwlo_tun/64
255 atf_check -s exit:0 rump.ifconfig l2tp0 create
256 atf_check -s exit:0 rump.ifconfig l2tp0 \
257 tunnel $ip_gwlo_tun $ip_gwre_tun
258 atf_check -s exit:0 rump.ifconfig l2tp0 session 1234 4321
259 atf_check -s exit:0 rump.ifconfig l2tp0 up
260 atf_check -s exit:0 rump.ifconfig bridge0 create
261 atf_check -s exit:0 rump.ifconfig bridge0 up
262 atf_check -s exit:0 $HIJACKING brconfig bridge0 add l2tp0
263 atf_check -s exit:0 $HIJACKING brconfig bridge0 add shmif0
264
265 export RUMP_SERVER=$SOCK_TUN_REMOTE
266 atf_check -s exit:0 rump.ifconfig shmif0 up
267 atf_check -s exit:0 rump.ifconfig shmif1 inet6 $ip_gwre_tun/64
268 atf_check -s exit:0 rump.ifconfig l2tp0 create
269 atf_check -s exit:0 rump.ifconfig l2tp0 \
270 tunnel $ip_gwre_tun $ip_gwlo_tun
271 atf_check -s exit:0 rump.ifconfig l2tp0 session 4321 1234
272 atf_check -s exit:0 rump.ifconfig l2tp0 up
273 atf_check -s exit:0 rump.ifconfig bridge0 create
274 atf_check -s exit:0 rump.ifconfig bridge0 up
275 atf_check -s exit:0 $HIJACKING brconfig bridge0 add l2tp0
276 atf_check -s exit:0 $HIJACKING brconfig bridge0 add shmif0
277
278 export RUMP_SERVER=$SOCK_REMOTE
279 atf_check -s exit:0 rump.ifconfig shmif0 inet6 $ip_remote
280 # Run ifconfig -w 10 just once for optimization
281 atf_check -s exit:0 rump.ifconfig -w 10
282
283 extract_new_packets $BUS_TUNNEL > $outfile
284
285 export RUMP_SERVER=$SOCK_LOCAL
286 atf_check -s exit:0 -o ignore rump.ping6 -c 1 -n -X 3 $ip_remote
287
288 extract_new_packets $BUS_TUNNEL > $outfile
289 atf_check -s exit:0 \
290 -o match:"$ip_gwlo_tun > $ip_gwre_tun: +ip-proto-115" \
291 cat $outfile
292 atf_check -s exit:0 \
293 -o match:"$ip_gwre_tun > $ip_gwlo_tun: +ip-proto-115" \
294 cat $outfile
295
296 if [ $mode = tunnel ]; then
297 export RUMP_SERVER=$SOCK_TUN_LOCAL
298 # from https://www.netbsd.org/docs/network/ipsec/
299 cat > $tmpfile <<-EOF
300 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
301 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
302 spdadd $subnet_local/64 $subnet_remote/64 any -P out ipsec
303 $proto/tunnel/$ip_gwlo_tun-$ip_gwre_tun/require;
304 spdadd $subnet_remote/64 $subnet_local/64 any -P in ipsec
305 $proto/tunnel/$ip_gwre_tun-$ip_gwlo_tun/require;
306 EOF
307 $DEBUG && cat $tmpfile
308 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
309
310 export RUMP_SERVER=$SOCK_TUN_REMOTE
311 cat > $tmpfile <<-EOF
312 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
313 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
314 spdadd $subnet_remote/64 $subnet_local/64 any -P out ipsec
315 $proto/tunnel/$ip_gwre_tun-$ip_gwlo_tun/require;
316 spdadd $subnet_local/64 $subnet_remote/64 any -P in ipsec
317 $proto/tunnel/$ip_gwlo_tun-$ip_gwre_tun/require;
318 EOF
319 $DEBUG && cat $tmpfile
320 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
321 else # transport mode
322 export RUMP_SERVER=$SOCK_TUN_LOCAL
323 # from https://www.netbsd.org/docs/network/ipsec/
324 cat > $tmpfile <<-EOF
325 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
326 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
327 spdadd $ip_gwlo_tun/128 $ip_gwre_tun/128 any -P out ipsec
328 $proto/transport//require;
329 spdadd $ip_gwre_tun/128 $ip_gwlo_tun/128 any -P in ipsec
330 $proto/transport//require;
331 EOF
332 $DEBUG && cat $tmpfile
333 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
334
335 export RUMP_SERVER=$SOCK_TUN_REMOTE
336 cat > $tmpfile <<-EOF
337 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
338 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
339 spdadd $ip_gwre_tun/128 $ip_gwlo_tun/128 any -P out ipsec
340 $proto/transport//require;
341 spdadd $ip_gwlo_tun/128 $ip_gwre_tun/128 any -P in ipsec
342 $proto/transport//require;
343 EOF
344 $DEBUG && cat $tmpfile
345 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
346 fi
347
348 check_sa_entries $SOCK_TUN_LOCAL $ip_gwlo_tun $ip_gwre_tun
349 check_sa_entries $SOCK_TUN_REMOTE $ip_gwlo_tun $ip_gwre_tun
350
351 export RUMP_SERVER=$SOCK_LOCAL
352 atf_check -s exit:0 -o ignore rump.ping6 -c 1 -n -X 3 $ip_remote
353
354 extract_new_packets $BUS_TUNNEL > $outfile
355 str=$(make_l2tp_pktstr $ip_gwlo_tun $ip_gwre_tun $proto ipv6 $mode)
356 atf_check -s exit:0 -o match:"$str" cat $outfile
357 str=$(make_l2tp_pktstr $ip_gwre_tun $ip_gwlo_tun $proto ipv6 $mode)
358 atf_check -s exit:0 -o match:"$str" cat $outfile
359
360 test_flush_entries $SOCK_TUN_LOCAL
361 test_flush_entries $SOCK_TUN_REMOTE
362 }
363
364 test_ipsec_l2tp_common()
365 {
366 local ipproto=$1
367 local mode=$2
368 local proto=$3
369 local algo=$4
370
371 if [ $ipproto = ipv4 ]; then
372 test_ipsec4_l2tp $mode $proto $algo
373 else
374 test_ipsec6_l2tp $mode $proto $algo
375 fi
376 }
377
378 add_test_ipsec_l2tp()
379 {
380 local ipproto=$1
381 local mode=$2
382 local proto=$3
383 local algo=$4
384 local _algo=$(echo $algo | sed 's/-//g')
385 local name= desc=
386
387 name="ipsec_l2tp_${ipproto}_${mode}_${proto}_${_algo}"
388 desc="Tests of l2tp/IPsec ($ipproto) ${mode} mode with $proto ($algo)"
389
390 atf_test_case ${name} cleanup
391 eval " \
392 ${name}_head() { \
393 atf_set \"descr\" \"$desc\"; \
394 atf_set \"require.progs\" \"rump_server\" \"setkey\"; \
395 }; \
396 ${name}_body() { \
397 test_ipsec_l2tp_common $ipproto $mode $proto $algo; \
398 rump_server_destroy_ifaces; \
399 }; \
400 ${name}_cleanup() { \
401 $DEBUG && dump; \
402 cleanup; \
403 } \
404 "
405 atf_add_test_case ${name}
406 }
407
408 atf_init_test_cases()
409 {
410 local algo=
411
412 for algo in $ESP_ENCRYPTION_ALGORITHMS_MINIMUM; do
413 add_test_ipsec_l2tp ipv4 tunnel esp $algo
414 add_test_ipsec_l2tp ipv6 tunnel esp $algo
415 add_test_ipsec_l2tp ipv4 transport esp $algo
416 add_test_ipsec_l2tp ipv6 transport esp $algo
417 done
418
419 for algo in $AH_AUTHENTICATION_ALGORITHMS_MINIMUM; do
420 add_test_ipsec_l2tp ipv4 tunnel ah $algo
421 add_test_ipsec_l2tp ipv6 tunnel ah $algo
422 add_test_ipsec_l2tp ipv4 transport ah $algo
423 add_test_ipsec_l2tp ipv6 transport ah $algo
424 done
425 }
426