t_ipsec_l2tp.sh revision 1.2 1 # $NetBSD: t_ipsec_l2tp.sh,v 1.2 2017/04/27 10:17:12 ozaki-r 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 export RUMP_SERVER=$SOCK_TUN_LOCAL
200 $DEBUG && $HIJACKING setkey -D
201 atf_check -s exit:0 -o match:"$ip_gwlo_tun $ip_gwre_tun" \
202 $HIJACKING setkey -D
203 atf_check -s exit:0 -o match:"$ip_gwre_tun $ip_gwlo_tun" \
204 $HIJACKING setkey -D
205 # TODO: more detail checks
206
207 export RUMP_SERVER=$SOCK_TUN_REMOTE
208 $DEBUG && $HIJACKING setkey -D
209 atf_check -s exit:0 -o match:"$ip_gwlo_tun $ip_gwre_tun" \
210 $HIJACKING setkey -D
211 atf_check -s exit:0 -o match:"$ip_gwre_tun $ip_gwlo_tun" \
212 $HIJACKING setkey -D
213 # TODO: more detail checks
214
215 export RUMP_SERVER=$SOCK_LOCAL
216 atf_check -s exit:0 -o ignore rump.ping -c 1 -n -w 3 $ip_remote
217
218 extract_new_packets $BUS_TUNNEL > $outfile
219 str=$(make_l2tp_pktstr $ip_gwlo_tun $ip_gwre_tun $proto ipv4 $mode)
220 atf_check -s exit:0 -o match:"$str" cat $outfile
221 str=$(make_l2tp_pktstr $ip_gwre_tun $ip_gwlo_tun $proto ipv4 $mode)
222 atf_check -s exit:0 -o match:"$str" cat $outfile
223 }
224
225 test_ipsec6_l2tp()
226 {
227 local mode=$1
228 local proto=$2
229 local algo=$3
230 local ip_local=fd00::1
231 local ip_gwlo_tun=fc00::1
232 local ip_gwre_tun=fc00::2
233 local ip_remote=fd00::2
234 local subnet_local=fc00::
235 local subnet_remote=fc00::
236 local keylen=$(get_one_valid_keylen $algo)
237 local key=$(generate_key $keylen)
238 local tmpfile=./tmp
239 local outfile=./out
240 local opt= str=
241
242 if [ $proto = esp ]; then
243 opt=-E
244 else
245 opt=-A
246 fi
247
248 rump_server_crypto_start $SOCK_LOCAL netinet6
249 rump_server_crypto_start $SOCK_TUN_LOCAL netipsec netinet6 l2tp bridge
250 rump_server_crypto_start $SOCK_TUN_REMOTE netipsec netinet6 l2tp bridge
251 rump_server_crypto_start $SOCK_REMOTE netinet6
252 rump_server_add_iface $SOCK_LOCAL shmif0 $BUS_LOCAL
253 rump_server_add_iface $SOCK_TUN_LOCAL shmif0 $BUS_LOCAL
254 rump_server_add_iface $SOCK_TUN_LOCAL shmif1 $BUS_TUNNEL
255 rump_server_add_iface $SOCK_TUN_REMOTE shmif0 $BUS_REMOTE
256 rump_server_add_iface $SOCK_TUN_REMOTE shmif1 $BUS_TUNNEL
257 rump_server_add_iface $SOCK_REMOTE shmif0 $BUS_REMOTE
258
259 export RUMP_SERVER=$SOCK_LOCAL
260 atf_check -s exit:0 rump.ifconfig shmif0 inet6 $ip_local/64
261
262 export RUMP_SERVER=$SOCK_TUN_LOCAL
263 atf_check -s exit:0 rump.ifconfig shmif0 up
264 atf_check -s exit:0 rump.ifconfig shmif1 inet6 $ip_gwlo_tun/64
265 atf_check -s exit:0 rump.ifconfig l2tp0 create
266 atf_check -s exit:0 rump.ifconfig l2tp0 \
267 tunnel $ip_gwlo_tun $ip_gwre_tun
268 atf_check -s exit:0 rump.ifconfig l2tp0 session 1234 4321
269 atf_check -s exit:0 rump.ifconfig l2tp0 up
270 atf_check -s exit:0 rump.ifconfig bridge0 create
271 atf_check -s exit:0 rump.ifconfig bridge0 up
272 atf_check -s exit:0 $HIJACKING brconfig bridge0 add l2tp0
273 atf_check -s exit:0 $HIJACKING brconfig bridge0 add shmif0
274
275 export RUMP_SERVER=$SOCK_TUN_REMOTE
276 atf_check -s exit:0 rump.ifconfig shmif0 up
277 atf_check -s exit:0 rump.ifconfig shmif1 inet6 $ip_gwre_tun/64
278 atf_check -s exit:0 rump.ifconfig l2tp0 create
279 atf_check -s exit:0 rump.ifconfig l2tp0 \
280 tunnel $ip_gwre_tun $ip_gwlo_tun
281 atf_check -s exit:0 rump.ifconfig l2tp0 session 4321 1234
282 atf_check -s exit:0 rump.ifconfig l2tp0 up
283 atf_check -s exit:0 rump.ifconfig bridge0 create
284 atf_check -s exit:0 rump.ifconfig bridge0 up
285 atf_check -s exit:0 $HIJACKING brconfig bridge0 add l2tp0
286 atf_check -s exit:0 $HIJACKING brconfig bridge0 add shmif0
287
288 export RUMP_SERVER=$SOCK_REMOTE
289 atf_check -s exit:0 rump.ifconfig shmif0 inet6 $ip_remote
290 # Run ifconfig -w 10 just once for optimization
291 atf_check -s exit:0 rump.ifconfig -w 10
292
293 extract_new_packets $BUS_TUNNEL > $outfile
294
295 export RUMP_SERVER=$SOCK_LOCAL
296 atf_check -s exit:0 -o ignore rump.ping6 -c 1 -n -X 3 $ip_remote
297
298 extract_new_packets $BUS_TUNNEL > $outfile
299 atf_check -s exit:0 \
300 -o match:"$ip_gwlo_tun > $ip_gwre_tun: +ip-proto-115" \
301 cat $outfile
302 atf_check -s exit:0 \
303 -o match:"$ip_gwre_tun > $ip_gwlo_tun: +ip-proto-115" \
304 cat $outfile
305
306 if [ $mode = tunnel ]; then
307 export RUMP_SERVER=$SOCK_TUN_LOCAL
308 # from https://www.netbsd.org/docs/network/ipsec/
309 cat > $tmpfile <<-EOF
310 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
311 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
312 spdadd $subnet_local/64 $subnet_remote/64 any -P out ipsec
313 $proto/tunnel/$ip_gwlo_tun-$ip_gwre_tun/require;
314 spdadd $subnet_remote/64 $subnet_local/64 any -P in ipsec
315 $proto/tunnel/$ip_gwre_tun-$ip_gwlo_tun/require;
316 EOF
317 $DEBUG && cat $tmpfile
318 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
319 $DEBUG && $HIJACKING setkey -D
320 atf_check -s exit:0 -o match:"$ip_gwlo_tun $ip_gwre_tun" \
321 $HIJACKING setkey -D
322 atf_check -s exit:0 -o match:"$ip_gwre_tun $ip_gwlo_tun" \
323 $HIJACKING setkey -D
324 # TODO: more detail checks
325
326 export RUMP_SERVER=$SOCK_TUN_REMOTE
327 cat > $tmpfile <<-EOF
328 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
329 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
330 spdadd $subnet_remote/64 $subnet_local/64 any -P out ipsec
331 $proto/tunnel/$ip_gwre_tun-$ip_gwlo_tun/require;
332 spdadd $subnet_local/64 $subnet_remote/64 any -P in ipsec
333 $proto/tunnel/$ip_gwlo_tun-$ip_gwre_tun/require;
334 EOF
335 $DEBUG && cat $tmpfile
336 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
337 $DEBUG && $HIJACKING setkey -D
338 atf_check -s exit:0 -o match:"$ip_gwlo_tun $ip_gwre_tun" \
339 $HIJACKING setkey -D
340 atf_check -s exit:0 -o match:"$ip_gwre_tun $ip_gwlo_tun" \
341 $HIJACKING setkey -D
342 # TODO: more detail checks
343 else # transport mode
344 export RUMP_SERVER=$SOCK_TUN_LOCAL
345 # from https://www.netbsd.org/docs/network/ipsec/
346 cat > $tmpfile <<-EOF
347 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
348 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
349 spdadd $ip_gwlo_tun/128 $ip_gwre_tun/128 any -P out ipsec
350 $proto/transport//require;
351 spdadd $ip_gwre_tun/128 $ip_gwlo_tun/128 any -P in ipsec
352 $proto/transport//require;
353 EOF
354 $DEBUG && cat $tmpfile
355 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
356 $DEBUG && $HIJACKING setkey -D
357 atf_check -s exit:0 -o match:"$ip_gwlo_tun $ip_gwre_tun" \
358 $HIJACKING setkey -D
359 atf_check -s exit:0 -o match:"$ip_gwre_tun $ip_gwlo_tun" \
360 $HIJACKING setkey -D
361 # TODO: more detail checks
362
363 export RUMP_SERVER=$SOCK_TUN_REMOTE
364 cat > $tmpfile <<-EOF
365 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
366 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
367 spdadd $ip_gwre_tun/128 $ip_gwlo_tun/128 any -P out ipsec
368 $proto/transport//require;
369 spdadd $ip_gwlo_tun/128 $ip_gwre_tun/128 any -P in ipsec
370 $proto/transport//require;
371 EOF
372 $DEBUG && cat $tmpfile
373 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
374 $DEBUG && $HIJACKING setkey -D
375 atf_check -s exit:0 -o match:"$ip_gwlo_tun $ip_gwre_tun" \
376 $HIJACKING setkey -D
377 atf_check -s exit:0 -o match:"$ip_gwre_tun $ip_gwlo_tun" \
378 $HIJACKING setkey -D
379 # TODO: more detail checks
380 fi
381
382 export RUMP_SERVER=$SOCK_LOCAL
383 atf_check -s exit:0 -o ignore rump.ping6 -c 1 -n -X 3 $ip_remote
384
385 extract_new_packets $BUS_TUNNEL > $outfile
386 str=$(make_l2tp_pktstr $ip_gwlo_tun $ip_gwre_tun $proto ipv6 $mode)
387 atf_check -s exit:0 -o match:"$str" cat $outfile
388 str=$(make_l2tp_pktstr $ip_gwre_tun $ip_gwlo_tun $proto ipv6 $mode)
389 atf_check -s exit:0 -o match:"$str" cat $outfile
390 }
391
392 test_ipsec_l2tp_common()
393 {
394 local ipproto=$1
395 local mode=$2
396 local proto=$3
397 local algo=$4
398
399 if [ $ipproto = ipv4 ]; then
400 test_ipsec4_l2tp $mode $proto $algo
401 else
402 test_ipsec6_l2tp $mode $proto $algo
403 fi
404 }
405
406 add_test_ipsec_l2tp()
407 {
408 local ipproto=$1
409 local mode=$2
410 local proto=$3
411 local algo=$4
412 local _algo=$(echo $algo | sed 's/-//g')
413 local name= desc=
414
415 name="ipsec_l2tp_${ipproto}_${mode}_${proto}_${_algo}"
416 desc="Tests of l2tp/IPsec ($ipproto) ${mode} mode with $proto ($algo)"
417
418 atf_test_case ${name} cleanup
419 eval " \
420 ${name}_head() { \
421 atf_set \"descr\" \"$desc\"; \
422 atf_set \"require.progs\" \"rump_server\" \"setkey\"; \
423 }; \
424 ${name}_body() { \
425 test_ipsec_l2tp_common $ipproto $mode $proto $algo; \
426 rump_server_destroy_ifaces; \
427 }; \
428 ${name}_cleanup() { \
429 $DEBUG && dump; \
430 cleanup; \
431 } \
432 "
433 atf_add_test_case ${name}
434 }
435
436 atf_init_test_cases()
437 {
438 local algo=
439
440 for algo in $ESP_ENCRYPTION_ALGORITHMS_MINIMUM; do
441 add_test_ipsec_l2tp ipv4 tunnel esp $algo
442 add_test_ipsec_l2tp ipv6 tunnel esp $algo
443 add_test_ipsec_l2tp ipv4 transport esp $algo
444 add_test_ipsec_l2tp ipv6 transport esp $algo
445 done
446
447 for algo in $AH_AUTHENTICATION_ALGORITHMS_MINIMUM; do
448 add_test_ipsec_l2tp ipv4 tunnel ah $algo
449 add_test_ipsec_l2tp ipv6 tunnel ah $algo
450 add_test_ipsec_l2tp ipv4 transport ah $algo
451 add_test_ipsec_l2tp ipv6 transport ah $algo
452 done
453 }
454