xref: /netbsd/tests/net/net/t_forwarding.sh (revision b4b572e9)
1#	$NetBSD: t_forwarding.sh,v 1.20 2017/02/20 09:58:58 ozaki-r Exp $
2#
3# Copyright (c) 2015 The NetBSD Foundation, 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
28SOCKSRC=unix://commsock1
29SOCKFWD=unix://commsock2
30SOCKDST=unix://commsock3
31IP4SRC=10.0.1.2
32IP4SRCGW=10.0.1.1
33IP4DSTGW=10.0.2.1
34IP4DST=10.0.2.2
35IP4DST_BCAST=10.0.2.255
36IP6SRC=fc00:0:0:1::2
37IP6SRCGW=fc00:0:0:1::1
38IP6DSTGW=fc00:0:0:2::1
39IP6DST=fc00:0:0:2::2
40HTML_FILE=index.html
41
42DEBUG=${DEBUG:-false}
43TIMEOUT=5
44
45atf_test_case ipforwarding_v4 cleanup
46atf_test_case ipforwarding_v6 cleanup
47atf_test_case ipforwarding_fastforward_v4 cleanup
48atf_test_case ipforwarding_fastforward_v6 cleanup
49atf_test_case ipforwarding_misc cleanup
50atf_test_case ipforwarding_fragment_v4 cleanup
51
52ipforwarding_v4_head()
53{
54	atf_set "descr" "Does IPv4 forwarding tests"
55	atf_set "require.progs" "rump_server"
56}
57
58ipforwarding_v6_head()
59{
60	atf_set "descr" "Does IPv6 forwarding tests"
61	atf_set "require.progs" "rump_server"
62}
63
64ipforwarding_fastforward_v4_head()
65{
66	atf_set "descr" "Tests for IPv4 fastforward"
67	atf_set "require.progs" "rump_server"
68}
69
70ipforwarding_fastforward_v6_head()
71{
72	atf_set "descr" "Tests for IPv6 fastfoward"
73	atf_set "require.progs" "rump_server"
74}
75
76ipforwarding_misc_head()
77{
78	atf_set "descr" "Does IPv4 forwarding tests"
79	atf_set "require.progs" "rump_server"
80}
81
82ipforwarding_fragment_v4_head()
83{
84	atf_set "descr" "Tests for fragmented packet forwarding (IPv4)"
85	atf_set "require.progs" "rump_server"
86}
87
88setup_endpoint()
89{
90	sock=${1}
91	addr=${2}
92	bus=${3}
93	mode=${4}
94	gw=${5}
95
96	rump_server_add_iface $sock shmif0 $bus
97
98	export RUMP_SERVER=${sock}
99	if [ $mode = "ipv6" ]; then
100		atf_check -s exit:0 rump.ifconfig shmif0 inet6 ${addr}
101		atf_check -s exit:0 -o ignore rump.route add -inet6 default ${gw}
102	else
103		atf_check -s exit:0 rump.ifconfig shmif0 inet ${addr} netmask 0xffffff00
104		atf_check -s exit:0 -o ignore rump.route add default ${gw}
105	fi
106	atf_check -s exit:0 rump.ifconfig shmif0 up
107
108	if $DEBUG; then
109		rump.ifconfig shmif0
110		rump.netstat -nr
111	fi
112}
113
114test_endpoint()
115{
116	sock=${1}
117	addr=${2}
118	bus=${3}
119	mode=${4}
120
121	export RUMP_SERVER=${sock}
122	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
123	if [ $mode = "ipv6" ]; then
124		atf_check -s exit:0 -o ignore rump.ping6 -n -c 1 -X $TIMEOUT ${addr}
125	else
126		atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 ${addr}
127	fi
128}
129
130setup_forwarder()
131{
132	mode=${1}
133
134	rump_server_add_iface $SOCKFWD shmif0 bus1
135	rump_server_add_iface $SOCKFWD shmif1 bus2
136
137	export RUMP_SERVER=$SOCKFWD
138
139	if [ $mode = "ipv6" ]; then
140		atf_check -s exit:0 rump.ifconfig shmif0 inet6 ${IP6SRCGW}
141		atf_check -s exit:0 rump.ifconfig shmif1 inet6 ${IP6DSTGW}
142	else
143		atf_check -s exit:0 rump.ifconfig shmif0 inet ${IP4SRCGW} netmask 0xffffff00
144		atf_check -s exit:0 rump.ifconfig shmif1 inet ${IP4DSTGW} netmask 0xffffff00
145	fi
146
147	atf_check -s exit:0 rump.ifconfig shmif0 up
148	atf_check -s exit:0 rump.ifconfig shmif1 up
149
150	if $DEBUG; then
151		rump.netstat -nr
152		if [ $mode = "ipv6" ]; then
153			rump.sysctl net.inet6.ip6.forwarding
154		else
155			rump.sysctl net.inet.ip.forwarding
156		fi
157	fi
158}
159
160prepare_file()
161{
162	local file=$1
163	local data="0123456789"
164
165	touch $file
166	for i in `seq 1 512`
167	do
168		echo $data >> $file
169	done
170}
171
172setup()
173{
174	rump_server_start $SOCKSRC
175	rump_server_start $SOCKFWD
176	rump_server_start $SOCKDST
177
178	setup_endpoint $SOCKSRC $IP4SRC bus1 ipv4 $IP4SRCGW
179	setup_endpoint $SOCKDST $IP4DST bus2 ipv4 $IP4DSTGW
180	setup_forwarder ipv4
181}
182
183setup6()
184{
185	rump_server_start $SOCKSRC netinet6
186	rump_server_start $SOCKFWD netinet6
187	rump_server_start $SOCKDST netinet6
188
189	setup_endpoint $SOCKSRC $IP6SRC bus1 ipv6 $IP6SRCGW
190	setup_endpoint $SOCKDST $IP6DST bus2 ipv6 $IP6DSTGW
191	setup_forwarder ipv6
192}
193
194test_http_get()
195{
196	local ip=$1
197
198	export RUMP_SERVER=$SOCKFWD
199	atf_check -s exit:0 rump.arp -d -a
200
201	export RUMP_SERVER=$SOCKSRC
202
203	# get the webpage
204	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so 	\
205	    ftp -q $TIMEOUT -o out http://$ip/$HTML_FILE
206}
207
208test_setup()
209{
210	test_endpoint $SOCKSRC $IP4SRC bus1 ipv4
211	test_endpoint $SOCKDST $IP4DST bus2 ipv4
212
213	export RUMP_SERVER=$SOCKFWD
214	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
215	atf_check -s exit:0 -o match:shmif1 rump.ifconfig
216
217	atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 ${IP4SRCGW}
218	atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 ${IP4DSTGW}
219}
220
221test_setup6()
222{
223	test_endpoint $SOCKSRC $IP6SRC bus1 ipv6
224	test_endpoint $SOCKDST $IP6DST bus2 ipv6
225
226	export RUMP_SERVER=$SOCKFWD
227	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
228	atf_check -s exit:0 -o match:shmif1 rump.ifconfig
229
230	atf_check -s exit:0 -o ignore rump.ping6 -n -c 1 -X $TIMEOUT ${IP6SRCGW}
231	atf_check -s exit:0 -o ignore rump.ping6 -n -c 1 -X $TIMEOUT ${IP6DSTGW}
232}
233
234setup_forwarding()
235{
236	export RUMP_SERVER=$SOCKFWD
237	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.ip.forwarding=1
238}
239
240setup_forwarding6()
241{
242	export RUMP_SERVER=$SOCKFWD
243	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet6.ip6.forwarding=1
244}
245
246setup_directed_broadcast()
247{
248	export RUMP_SERVER=$SOCKFWD
249	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.ip.directed-broadcast=1
250}
251
252setup_icmp_bmcastecho()
253{
254	export RUMP_SERVER=$SOCKDST
255	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.icmp.bmcastecho=1
256}
257
258teardown_forwarding()
259{
260	export RUMP_SERVER=$SOCKFWD
261	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.ip.forwarding=0
262}
263
264teardown_forwarding6()
265{
266	export RUMP_SERVER=$SOCKFWD
267	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet6.ip6.forwarding=0
268}
269
270teardown_directed_broadcast()
271{
272	export RUMP_SERVER=$SOCKFWD
273	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.ip.directed-broadcast=0
274}
275
276teardown_icmp_bmcastecho()
277{
278	export RUMP_SERVER=$SOCKDST
279	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.icmp.bmcastecho=0
280}
281
282teardown_interfaces()
283{
284
285	rump_server_destroy_ifaces
286}
287
288test_setup_forwarding()
289{
290	export RUMP_SERVER=$SOCKFWD
291	atf_check -s exit:0 -o match:"net.inet.ip.forwarding = 1" \
292	    rump.sysctl net.inet.ip.forwarding
293}
294test_setup_forwarding6()
295{
296	export RUMP_SERVER=$SOCKFWD
297	atf_check -s exit:0 -o match:"net.inet6.ip6.forwarding = 1" \
298	    rump.sysctl net.inet6.ip6.forwarding
299}
300
301test_teardown_forwarding()
302{
303	export RUMP_SERVER=$SOCKFWD
304	atf_check -s exit:0 -o match:"net.inet.ip.forwarding = 0" \
305	    rump.sysctl net.inet.ip.forwarding
306}
307test_teardown_forwarding6()
308{
309	export RUMP_SERVER=$SOCKFWD
310	atf_check -s exit:0 -o match:"net.inet6.ip6.forwarding = 0" \
311	    rump.sysctl net.inet6.ip6.forwarding
312}
313
314test_ping_failure()
315{
316	export RUMP_SERVER=$SOCKSRC
317	atf_check -s not-exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP4DST
318	export RUMP_SERVER=$SOCKDST
319	atf_check -s not-exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP4SRC
320}
321
322test_ping_success()
323{
324	export RUMP_SERVER=$SOCKSRC
325	$DEBUG && rump.ifconfig -v shmif0
326	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP4SRCGW
327	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP4DST
328	$DEBUG && rump.ifconfig -v shmif0
329
330	export RUMP_SERVER=$SOCKDST
331	$DEBUG && rump.ifconfig -v shmif0
332	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP4DSTGW
333	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP4SRC
334	$DEBUG && rump.ifconfig -v shmif0
335}
336
337test_ping_ttl()
338{
339	export RUMP_SERVER=$SOCKSRC
340	$DEBUG && rump.ifconfig -v shmif0
341	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 -T 1 $IP4SRCGW
342	atf_check -s not-exit:0 -o match:'Time To Live exceeded' \
343	    rump.ping -v -n -w $TIMEOUT -c 1 -T 1 $IP4DST
344	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 -T 2 $IP4DST
345	$DEBUG && rump.ifconfig -v shmif0
346}
347
348test_sysctl_ttl()
349{
350	local ip=$1
351
352	export RUMP_SERVER=$SOCKSRC
353	$DEBUG && rump.ifconfig -v shmif0
354
355	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.ip.ttl=1
356	# get the webpage
357	atf_check -s not-exit:0 -e match:'timed out' \
358		env LD_PRELOAD=/usr/lib/librumphijack.so	\
359		ftp -q $TIMEOUT -o out http://$ip/$HTML_FILE
360
361
362	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.ip.ttl=2
363	# get the webpage
364	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so 	\
365		ftp -q $TIMEOUT -o out http://$ip/$HTML_FILE
366
367	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.ip.ttl=64
368	$DEBUG && rump.ifconfig -v shmif0
369}
370
371test_directed_broadcast()
372{
373	setup_icmp_bmcastecho
374
375	setup_directed_broadcast
376	export RUMP_SERVER=$SOCKSRC
377	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP4DST_BCAST
378
379	teardown_directed_broadcast
380	export RUMP_SERVER=$SOCKSRC
381	atf_check -s not-exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP4DST_BCAST
382
383	teardown_icmp_bmcastecho
384}
385
386test_ping6_failure()
387{
388	export RUMP_SERVER=$SOCKSRC
389	atf_check -s not-exit:0 -o ignore rump.ping6 -q -n -c 1 -X $TIMEOUT $IP6DST
390	export RUMP_SERVER=$SOCKDST
391	atf_check -s not-exit:0 -o ignore rump.ping6 -q -n -c 1 -X $TIMEOUT $IP6SRC
392}
393
394test_ping6_success()
395{
396	export RUMP_SERVER=$SOCKSRC
397	$DEBUG && rump.ifconfig -v shmif0
398	atf_check -s exit:0 -o ignore rump.ping6 -q -n -c 1 -X $TIMEOUT $IP6SRCGW
399	atf_check -s exit:0 -o ignore rump.ping6 -q -n -c 1 -X $TIMEOUT $IP6DST
400	$DEBUG && rump.ifconfig -v shmif0
401
402	export RUMP_SERVER=$SOCKDST
403	$DEBUG && rump.ifconfig -v shmif0
404	atf_check -s exit:0 -o ignore rump.ping6 -q -n -c 1 -X $TIMEOUT $IP6DSTGW
405	atf_check -s exit:0 -o ignore rump.ping6 -q -n -c 1 -X $TIMEOUT $IP6SRC
406	$DEBUG && rump.ifconfig -v shmif0
407}
408
409test_hoplimit()
410{
411	export RUMP_SERVER=$SOCKSRC
412	$DEBUG && rump.ifconfig -v shmif0
413	atf_check -s exit:0 -o ignore rump.ping6 -q -n -c 1 -h 1 -X $TIMEOUT $IP6SRCGW
414	atf_check -s not-exit:0 -o match:'Time to live exceeded' \
415	    rump.ping6 -v -n -c 1 -h 1 -X $TIMEOUT $IP6DST
416	atf_check -s exit:0 -o ignore rump.ping6 -q -n -c 1 -h 2 -X $TIMEOUT $IP6DST
417	$DEBUG && rump.ifconfig -v shmif0
418}
419
420setup_mtu()
421{
422	local mtu=$1
423
424	export RUMP_SERVER=$SOCKFWD
425	atf_check -s exit:0 rump.ifconfig shmif0 mtu $mtu
426}
427
428disable_mtudisc()
429{
430	local mtu=$1
431
432	export RUMP_SERVER=$SOCKDST
433	atf_check -s exit:0 -o ignore rump.sysctl -w -q net.inet.ip.mtudisc=0
434}
435
436ipforwarding_v4_body()
437{
438	setup
439	test_setup
440
441	setup_forwarding
442	test_setup_forwarding
443	test_ping_success
444
445	teardown_forwarding
446	test_teardown_forwarding
447	test_ping_failure
448
449	teardown_interfaces
450}
451
452ipforwarding_v6_body()
453{
454	setup6
455	test_setup6
456
457	setup_forwarding6
458	test_setup_forwarding6
459	test_ping6_success
460	test_hoplimit
461
462	teardown_forwarding6
463	test_teardown_forwarding6
464	test_ping6_failure
465
466	teardown_interfaces
467}
468
469ipforwarding_fastforward_v4_body()
470{
471	setup
472	test_setup
473
474	setup_forwarding
475	test_setup_forwarding
476
477	touch $HTML_FILE
478	start_httpd $SOCKDST $IP4DST
479	$DEBUG && rump.netstat -a
480
481	test_http_get $IP4DST
482
483	teardown_interfaces
484}
485
486ipforwarding_fastforward_v6_body()
487{
488	setup6
489	test_setup6
490
491	setup_forwarding6
492	test_setup_forwarding6
493
494	touch $HTML_FILE
495	start_httpd $SOCKDST $IP6DST
496	$DEBUG && rump.netstat -a
497
498	test_http_get "[$IP6DST]"
499
500	teardown_interfaces
501}
502
503ipforwarding_misc_body()
504{
505	setup
506	test_setup
507
508	setup_forwarding
509	test_setup_forwarding
510
511	test_ping_ttl
512
513	test_directed_broadcast
514
515	touch $HTML_FILE
516	start_httpd $SOCKDST $IP4DST
517	$DEBUG && rump.netstat -a
518
519	test_sysctl_ttl $IP4DST
520
521	teardown_interfaces
522	return 0
523}
524
525ipforwarding_fragment_v4_body()
526{
527	setup
528	test_setup
529
530	setup_forwarding
531	test_setup_forwarding
532
533	prepare_file $HTML_FILE
534	start_httpd $SOCKDST $IP4DST
535	$DEBUG && rump.netstat -a
536	setup_mtu 1000
537	disable_mtudisc
538
539	extract_new_packets bus1 > ./out
540	extract_new_packets bus2 > ./out
541
542	test_http_get $IP4DST
543
544	# Packets of MTU size sent from the server
545	extract_new_packets bus2 > ./out
546	atf_check -s exit:0 -o match:'length 1514' cat ./out
547
548	# The packets are fragmented down to 1000
549	extract_new_packets bus1 > ./out
550	atf_check -s exit:0 -o match:'length 1010' cat ./out
551	atf_check -s exit:0 -o match:'length 538' cat ./out
552
553	teardown_interfaces
554}
555
556ipforwarding_v4_cleanup()
557{
558	$DEBUG && dump
559	cleanup
560}
561
562ipforwarding_v6_cleanup()
563{
564	$DEBUG && dump
565	cleanup
566}
567
568ipforwarding_fastforward_v4_cleanup()
569{
570	$DEBUG && dump
571	stop_httpd
572	cleanup
573}
574
575ipforwarding_fastforward_v6_cleanup()
576{
577	$DEBUG && dump
578	stop_httpd
579	cleanup
580}
581
582ipforwarding_misc_cleanup()
583{
584	$DEBUG && dump
585	stop_httpd
586	cleanup
587}
588
589ipforwarding_fragment_v4_cleanup()
590{
591	$DEBUG && dump
592	stop_httpd
593	cleanup
594}
595
596atf_init_test_cases()
597{
598	atf_add_test_case ipforwarding_v4
599	atf_add_test_case ipforwarding_v6
600	atf_add_test_case ipforwarding_fastforward_v4
601	atf_add_test_case ipforwarding_fastforward_v6
602	atf_add_test_case ipforwarding_misc
603	atf_add_test_case ipforwarding_fragment_v4
604}
605