t_ipsec_gif.sh revision 1.4 1 # $NetBSD: t_ipsec_gif.sh,v 1.4 2017/05/10 04:46:13 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_gif_local
29 SOCK_TUN_LOCAL=unix://ipsec_gif_tunel_local
30 SOCK_TUN_REMOTE=unix://ipsec_gif_tunnel_remote
31 SOCK_REMOTE=unix://ipsec_gif_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_gif_pktstr()
39 {
40 local src=$1
41 local dst=$2
42 local src_inner=$3
43 local dst_inner=$4
44 local proto=$5
45 local ipproto=$6
46 local proto_cap= inner_str=
47
48 if [ $proto = esp ]; then
49 proto_cap=ESP
50 else
51 proto_cap=AH
52 if [ $ipproto = ipv4 ]; then
53 inner_str="$src_inner > $dst_inner:.+\(ipip-proto-4\)"
54 else
55 inner_str="$src_inner > $dst_inner"
56 fi
57 fi
58
59 echo "$src > $dst: $proto_cap.+$inner_str"
60 }
61
62 test_ipsec4_gif()
63 {
64 local mode=$1
65 local proto=$2
66 local algo=$3
67 local ip_local=10.0.1.2
68 local ip_gw_local=10.0.1.1
69 local ip_gwlo_tun=20.0.0.1
70 local ip_gwlo_gif=20.1.0.1
71 local ip_gwre_gif=20.1.0.2
72 local ip_gwre_tun=20.0.0.2
73 local ip_gw_remote=10.0.2.1
74 local ip_remote=10.0.2.2
75 local subnet_local=10.0.1.0
76 local subnet_remote=10.0.2.0
77 local keylen=$(get_one_valid_keylen $algo)
78 local key=$(generate_key $keylen)
79 local tmpfile=./tmp
80 local outfile=./out
81 local opt= str=
82
83 if [ $proto = esp ]; then
84 opt=-E
85 else
86 opt=-A
87 fi
88
89 rump_server_crypto_start $SOCK_LOCAL
90 rump_server_crypto_start $SOCK_TUN_LOCAL netipsec gif
91 rump_server_crypto_start $SOCK_TUN_REMOTE netipsec gif
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 atf_check -s exit:0 -o ignore \
103 rump.route -n add -net $subnet_remote $ip_gw_local
104
105 export RUMP_SERVER=$SOCK_TUN_LOCAL
106 atf_check -s exit:0 rump.ifconfig shmif0 $ip_gw_local/24
107 atf_check -s exit:0 rump.ifconfig shmif1 $ip_gwlo_tun/24
108 atf_check -s exit:0 rump.ifconfig gif0 create
109 atf_check -s exit:0 rump.ifconfig gif0 \
110 tunnel $ip_gwlo_tun $ip_gwre_tun
111 atf_check -s exit:0 rump.ifconfig gif0 \
112 inet $ip_gwlo_gif/32 $ip_gwre_gif
113 atf_check -s exit:0 rump.sysctl -q -w net.inet.ip.forwarding=1
114 atf_check -s exit:0 -o ignore \
115 rump.route -n add -net $subnet_remote $ip_gwre_gif
116
117 export RUMP_SERVER=$SOCK_TUN_REMOTE
118 atf_check -s exit:0 rump.ifconfig shmif0 $ip_gw_remote/24
119 atf_check -s exit:0 rump.ifconfig shmif1 $ip_gwre_tun/24
120 atf_check -s exit:0 rump.ifconfig gif0 create
121 atf_check -s exit:0 rump.ifconfig gif0 \
122 tunnel $ip_gwre_tun $ip_gwlo_tun
123 atf_check -s exit:0 rump.ifconfig gif0 \
124 inet $ip_gwre_gif/32 $ip_gwlo_gif
125 atf_check -s exit:0 rump.sysctl -q -w net.inet.ip.forwarding=1
126 atf_check -s exit:0 -o ignore \
127 rump.route -n add -net $subnet_local $ip_gwlo_gif
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 atf_check -s exit:0 -o ignore \
134 rump.route -n add -net $subnet_local $ip_gw_remote
135
136 extract_new_packets $BUS_TUNNEL > $outfile
137
138 export RUMP_SERVER=$SOCK_LOCAL
139 atf_check -s exit:0 -o ignore rump.ping -c 1 -n -w 3 $ip_remote
140
141 extract_new_packets $BUS_TUNNEL > $outfile
142 str="$ip_gwlo_tun > $ip_gwre_tun:"
143 str="$str $ip_local > $ip_remote: ICMP echo request,"
144 str="$str .+ \(ipip-proto-4\)"
145 atf_check -s exit:0 -o match:"$str" cat $outfile
146 str="$ip_gwre_tun > $ip_gwlo_tun:"
147 str="$str $ip_remote > $ip_local: ICMP echo reply,"
148 str="$str .+ \(ipip-proto-4\)"
149 atf_check -s exit:0 -o match:"$str" cat $outfile
150
151 if [ $mode = tunnel ]; then
152 export RUMP_SERVER=$SOCK_TUN_LOCAL
153 # from https://www.netbsd.org/docs/network/ipsec/
154 cat > $tmpfile <<-EOF
155 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
156 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
157 spdadd $subnet_local/24 $subnet_remote/24 any -P out ipsec
158 $proto/tunnel/$ip_gwlo_tun-$ip_gwre_tun/require;
159 spdadd $subnet_remote/24 $subnet_local/24 any -P in ipsec
160 $proto/tunnel/$ip_gwre_tun-$ip_gwlo_tun/require;
161 EOF
162 $DEBUG && cat $tmpfile
163 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
164
165 export RUMP_SERVER=$SOCK_TUN_REMOTE
166 cat > $tmpfile <<-EOF
167 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
168 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
169 spdadd $subnet_remote/24 $subnet_local/24 any -P out ipsec
170 $proto/tunnel/$ip_gwre_tun-$ip_gwlo_tun/require;
171 spdadd $subnet_local/24 $subnet_remote/24 any -P in ipsec
172 $proto/tunnel/$ip_gwlo_tun-$ip_gwre_tun/require;
173 EOF
174 $DEBUG && cat $tmpfile
175 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
176 else # transport mode
177 export RUMP_SERVER=$SOCK_TUN_LOCAL
178 # from https://www.netbsd.org/docs/network/ipsec/
179 cat > $tmpfile <<-EOF
180 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
181 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
182 spdadd $ip_gwlo_tun/32 $ip_gwre_tun/32 any -P out ipsec
183 $proto/transport//require;
184 spdadd $ip_gwre_tun/32 $ip_gwlo_tun/32 any -P in ipsec
185 $proto/transport//require;
186 EOF
187 $DEBUG && cat $tmpfile
188 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
189
190 export RUMP_SERVER=$SOCK_TUN_REMOTE
191 cat > $tmpfile <<-EOF
192 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
193 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
194 spdadd $ip_gwre_tun/32 $ip_gwlo_tun/32 any -P out ipsec
195 $proto/transport//require;
196 spdadd $ip_gwlo_tun/32 $ip_gwre_tun/32 any -P in ipsec
197 $proto/transport//require;
198 EOF
199 $DEBUG && cat $tmpfile
200 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
201 fi
202
203 check_sa_entries $SOCK_TUN_LOCAL $ip_gwlo_tun $ip_gwre_tun
204 check_sa_entries $SOCK_TUN_REMOTE $ip_gwlo_tun $ip_gwre_tun
205
206 export RUMP_SERVER=$SOCK_LOCAL
207 atf_check -s exit:0 -o ignore rump.ping -c 1 -n -w 3 $ip_remote
208
209 extract_new_packets $BUS_TUNNEL > $outfile
210 str=$(make_gif_pktstr $ip_gwlo_tun $ip_gwre_tun \
211 $ip_local $ip_remote $proto ipv4)
212 atf_check -s exit:0 -o match:"$str" cat $outfile
213 str=$(make_gif_pktstr $ip_gwre_tun $ip_gwlo_tun \
214 $ip_remote $ip_local $proto ipv4)
215 atf_check -s exit:0 -o match:"$str" cat $outfile
216
217 test_flush_entries $SOCK_TUN_LOCAL
218 test_flush_entries $SOCK_TUN_REMOTE
219 }
220
221 test_ipsec6_gif()
222 {
223 local mode=$1
224 local proto=$2
225 local algo=$3
226 local ip_local=fd00:1::2
227 local ip_gw_local=fd00:1::1
228 local ip_gwlo_tun=fc00::1
229 local ip_gwlo_gif=fc01::1
230 local ip_gwre_gif=fc01::2
231 local ip_gwre_tun=fc00::2
232 local ip_gw_remote=fd00:2::1
233 local ip_remote=fd00:2::2
234 local subnet_local=fd00:1::
235 local subnet_remote=fd00:2::
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 gif
250 rump_server_crypto_start $SOCK_TUN_REMOTE netipsec netinet6 gif
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 atf_check -s exit:0 -o ignore \
262 rump.route -n add -inet6 -net $subnet_remote/64 $ip_gw_local
263
264 export RUMP_SERVER=$SOCK_TUN_LOCAL
265 atf_check -s exit:0 rump.ifconfig shmif0 inet6 $ip_gw_local/64
266 atf_check -s exit:0 rump.ifconfig shmif1 inet6 $ip_gwlo_tun/64
267 atf_check -s exit:0 rump.ifconfig gif0 create
268 atf_check -s exit:0 rump.ifconfig gif0 \
269 tunnel $ip_gwlo_tun $ip_gwre_tun
270 atf_check -s exit:0 rump.ifconfig gif0 \
271 inet6 $ip_gwlo_gif/128 $ip_gwre_gif
272 atf_check -s exit:0 rump.sysctl -q -w net.inet6.ip6.forwarding=1
273 atf_check -s exit:0 -o ignore \
274 rump.route -n add -inet6 -net $subnet_remote/64 $ip_gwlo_gif
275
276 export RUMP_SERVER=$SOCK_TUN_REMOTE
277 atf_check -s exit:0 rump.ifconfig shmif0 inet6 $ip_gw_remote/64
278 atf_check -s exit:0 rump.ifconfig shmif1 inet6 $ip_gwre_tun/64
279 atf_check -s exit:0 rump.ifconfig gif0 create
280 atf_check -s exit:0 rump.ifconfig gif0 \
281 tunnel $ip_gwre_tun $ip_gwlo_tun
282 atf_check -s exit:0 rump.ifconfig gif0 \
283 inet6 $ip_gwre_gif/128 $ip_gwlo_gif
284 atf_check -s exit:0 rump.sysctl -q -w net.inet6.ip6.forwarding=1
285 atf_check -s exit:0 -o ignore \
286 rump.route -n add -inet6 -net $subnet_local/64 $ip_gwre_gif
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 atf_check -s exit:0 -o ignore \
293 rump.route -n add -inet6 -net $subnet_local/64 $ip_gw_remote
294
295 extract_new_packets $BUS_TUNNEL > $outfile
296
297 export RUMP_SERVER=$SOCK_LOCAL
298 atf_check -s exit:0 -o ignore rump.ping6 -c 1 -n -X 3 $ip_remote
299
300 extract_new_packets $BUS_TUNNEL > $outfile
301 str="$ip_gwlo_tun > $ip_gwre_tun:"
302 str="$str $ip_local > $ip_remote: ICMP6, echo request"
303 atf_check -s exit:0 -o match:"$str" cat $outfile
304 str="$ip_gwre_tun > $ip_gwlo_tun:"
305 str="$str $ip_remote > $ip_local: ICMP6, echo reply,"
306 atf_check -s exit:0 -o match:"$str" cat $outfile
307
308 if [ $mode = tunnel ]; then
309 export RUMP_SERVER=$SOCK_TUN_LOCAL
310 # from https://www.netbsd.org/docs/network/ipsec/
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_local/64 $subnet_remote/64 any -P out ipsec
315 $proto/tunnel/$ip_gwlo_tun-$ip_gwre_tun/require;
316 spdadd $subnet_remote/64 $subnet_local/64 any -P in ipsec
317 $proto/tunnel/$ip_gwre_tun-$ip_gwlo_tun/require;
318 EOF
319 $DEBUG && cat $tmpfile
320 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
321
322 export RUMP_SERVER=$SOCK_TUN_REMOTE
323 cat > $tmpfile <<-EOF
324 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
325 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
326 spdadd $subnet_remote/64 $subnet_local/64 any -P out ipsec
327 $proto/tunnel/$ip_gwre_tun-$ip_gwlo_tun/require;
328 spdadd $subnet_local/64 $subnet_remote/64 any -P in ipsec
329 $proto/tunnel/$ip_gwlo_tun-$ip_gwre_tun/require;
330 EOF
331 $DEBUG && cat $tmpfile
332 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
333 else # transport mode
334 export RUMP_SERVER=$SOCK_TUN_LOCAL
335 # from https://www.netbsd.org/docs/network/ipsec/
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_gwlo_tun/128 $ip_gwre_tun/128 any -P out ipsec
340 $proto/transport//require;
341 spdadd $ip_gwre_tun/128 $ip_gwlo_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
347 export RUMP_SERVER=$SOCK_TUN_REMOTE
348 cat > $tmpfile <<-EOF
349 add $ip_gwlo_tun $ip_gwre_tun $proto 10000 $opt $algo $key;
350 add $ip_gwre_tun $ip_gwlo_tun $proto 10001 $opt $algo $key;
351 spdadd $ip_gwre_tun/128 $ip_gwlo_tun/128 any -P out ipsec
352 $proto/transport//require;
353 spdadd $ip_gwlo_tun/128 $ip_gwre_tun/128 any -P in ipsec
354 $proto/transport//require;
355 EOF
356 $DEBUG && cat $tmpfile
357 atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
358 fi
359
360 check_sa_entries $SOCK_TUN_LOCAL $ip_gwlo_tun $ip_gwre_tun
361 check_sa_entries $SOCK_TUN_REMOTE $ip_gwlo_tun $ip_gwre_tun
362
363 export RUMP_SERVER=$SOCK_LOCAL
364 atf_check -s exit:0 -o ignore rump.ping6 -c 1 -n -X 3 $ip_remote
365
366 extract_new_packets $BUS_TUNNEL > $outfile
367 str=$(make_gif_pktstr $ip_gwlo_tun $ip_gwre_tun \
368 $ip_local $ip_remote $proto ipv6)
369 atf_check -s exit:0 -o match:"$str" cat $outfile
370 str=$(make_gif_pktstr $ip_gwre_tun $ip_gwlo_tun \
371 $ip_remote $ip_local $proto ipv6)
372 atf_check -s exit:0 -o match:"$str" cat $outfile
373
374 test_flush_entries $SOCK_TUN_LOCAL
375 test_flush_entries $SOCK_TUN_REMOTE
376 }
377
378 test_ipsec_gif_common()
379 {
380 local ipproto=$1
381 local mode=$2
382 local proto=$3
383 local algo=$4
384
385 if [ $ipproto = ipv4 ]; then
386 test_ipsec4_gif $mode $proto $algo
387 else
388 test_ipsec6_gif $mode $proto $algo
389 fi
390 }
391
392 add_test_ipsec_gif()
393 {
394 local ipproto=$1
395 local mode=$2
396 local proto=$3
397 local algo=$4
398 local _algo=$(echo $algo | sed 's/-//g')
399 local name= desc=
400
401 name="ipsec_gif_${ipproto}_${mode}_${proto}_${_algo}"
402 desc="Tests of gif/IPsec ($ipproto) ${mode} mode with $proto ($algo)"
403
404 atf_test_case ${name} cleanup
405 eval " \
406 ${name}_head() { \
407 atf_set \"descr\" \"$desc\"; \
408 atf_set \"require.progs\" \"rump_server\" \"setkey\"; \
409 }; \
410 ${name}_body() { \
411 test_ipsec_gif_common $ipproto $mode $proto $algo; \
412 rump_server_destroy_ifaces; \
413 }; \
414 ${name}_cleanup() { \
415 $DEBUG && dump; \
416 cleanup; \
417 } \
418 "
419 atf_add_test_case ${name}
420 }
421
422 atf_init_test_cases()
423 {
424 local algo=
425
426 for algo in $ESP_ENCRYPTION_ALGORITHMS_MINIMUM; do
427 add_test_ipsec_gif ipv4 tunnel esp $algo
428 add_test_ipsec_gif ipv6 tunnel esp $algo
429 add_test_ipsec_gif ipv4 transport esp $algo
430 add_test_ipsec_gif ipv6 transport esp $algo
431 done
432
433 for algo in $AH_AUTHENTICATION_ALGORITHMS_MINIMUM; do
434 add_test_ipsec_gif ipv4 tunnel ah $algo
435 add_test_ipsec_gif ipv6 tunnel ah $algo
436 add_test_ipsec_gif ipv4 transport ah $algo
437 add_test_ipsec_gif ipv6 transport ah $algo
438 done
439 }
440