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