t_icmp_redirect.sh revision 313498
1#	$NetBSD: t_icmp_redirect.sh,v 1.3 2016/08/10 22:17:44 kre 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
28# Most codes are derived from tests/net/route/t_flags.sh
29
30netserver=\
31"rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif -lrumpdev"
32SOCK_LOCAL=unix://commsock1
33SOCK_PEER=unix://commsock2
34SOCK_GW=unix://commsock3
35BUS=bus1
36BUS2=bus2
37REDIRECT_TIMEOUT=5
38
39DEBUG=false
40
41atf_test_case icmp_redirect_timeout cleanup
42
43icmp_redirect_timeout_head()
44{
45
46	atf_set "descr" "Tests for ICMP redirect timeout";
47	atf_set "require.progs" "rump_server";
48}
49
50setup_local()
51{
52
53	atf_check -s exit:0 ${netserver} ${SOCK_LOCAL}
54
55	export RUMP_SERVER=$SOCK_LOCAL
56	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
57	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
58	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 10.0.0.2/24
59	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
60
61	atf_check -s exit:0 -o ignore rump.sysctl -w \
62	    net.inet.icmp.redirtimeout=$REDIRECT_TIMEOUT
63
64	$DEBUG && rump.ifconfig
65	$DEBUG && rump.netstat -rn -f inet
66}
67
68setup_peer()
69{
70
71	atf_check -s exit:0 ${netserver} ${SOCK_PEER}
72
73	export RUMP_SERVER=$SOCK_PEER
74	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
75	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
76	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 10.0.0.1/24
77	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
78
79	$DEBUG && rump.ifconfig
80	$DEBUG && rump.netstat -rn -f inet
81}
82
83setup_gw()
84{
85
86	atf_check -s exit:0 ${netserver} ${SOCK_GW}
87
88	export RUMP_SERVER=$SOCK_GW
89	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
90	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
91	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 10.0.0.254/24
92	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
93
94	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 create
95	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 linkstr ${BUS2}
96	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 10.0.2.1/24
97	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 alias 10.0.2.2/24
98	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 up
99
100	# Wait until DAD completes (10 sec at most)
101	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
102	atf_check -s not-exit:0 -x "rump.ifconfig shmif1 |grep -q tentative"
103
104	$DEBUG && rump.ifconfig
105	$DEBUG && rump.netstat -rn -f inet
106}
107
108teardown_gw()
109{
110
111	env RUMP_SERVER=$SOCK_GW rump.halt
112}
113
114check_entry_flags()
115{
116	local ip=$(echo $1 |sed 's/\./\\./g')
117	local flags=$2
118
119	atf_check -s exit:0 -o match:" $flags " -e ignore -x \
120	    "rump.netstat -rn -f inet | grep ^'$ip'"
121}
122
123check_entry_gw()
124{
125	local ip=$(echo $1 |sed 's/\./\\./g')
126	local gw=$2
127
128	atf_check -s exit:0 -o match:" $gw " -e ignore -x \
129	    "rump.netstat -rn -f inet | grep ^'$ip'"
130}
131
132check_entry_fail()
133{
134	local ip=$(echo $1 |sed 's/\./\\./g')
135	local flags=$2  # Not used currently
136
137	atf_check -s not-exit:0 -e ignore -x \
138	    "rump.netstat -rn -f inet | grep ^'$ip'"
139}
140
141icmp_redirect_timeout_body()
142{
143
144	$DEBUG && ulimit -c unlimited
145
146	setup_local
147	setup_peer
148
149	### Testing Dynamic flag ###
150
151	#
152	# Setup a gateway 10.0.0.254. 10.0.2.1 is behind it.
153	#
154	setup_gw
155
156	#
157	# Teach the peer that 10.0.2.* is behind 10.0.0.254
158	#
159	export RUMP_SERVER=$SOCK_PEER
160	atf_check -s exit:0 -o ignore rump.route add -net 10.0.2.0/24 10.0.0.254
161	# Up, Gateway, Static
162	check_entry_flags 10.0.2/24 UGS
163
164	#
165	# Setup the default gateway to the peer, 10.0.0.1
166	#
167	export RUMP_SERVER=$SOCK_LOCAL
168	atf_check -s exit:0 -o ignore rump.route add default 10.0.0.1
169	# Up, Gateway, Static
170	check_entry_flags default UGS
171
172	# Try ping 10.0.2.1
173	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.2.1
174	$DEBUG && rump.netstat -rn -f inet
175
176	# Up, Gateway, Host, Dynamic
177	check_entry_flags 10.0.2.1 UGHD
178	check_entry_gw 10.0.2.1 10.0.0.254
179
180	atf_check -s exit:0 sleep $((REDIRECT_TIMEOUT + 2))
181
182	# The dynamic entry should be expired and removed
183	check_entry_fail 10.0.2.1
184
185	export RUMP_SERVER=$SOCK_PEER
186	$DEBUG && rump.netstat -rn -f inet
187
188	teardown_gw
189}
190
191dump()
192{
193
194	shmif_dumpbus -p - $BUS 2>/dev/null | tcpdump -n -e -r -
195	gdb -ex bt /usr/bin/rump_server rump_server.core
196}
197
198cleanup()
199{
200
201	env RUMP_SERVER=$SOCK_LOCAL rump.halt
202	env RUMP_SERVER=$SOCK_PEER rump.halt
203}
204
205icmp_redirect_timeout_cleanup()
206{
207
208	$DEBUG && dump
209	cleanup
210}
211
212atf_test_case icmp_redirect cleanup
213
214icmp_redirect_head()
215{
216
217	atf_set "descr" "Tests for icmp redirect";
218	atf_set "require.progs" "rump_server";
219}
220
221setup_redirect()
222{
223	atf_check -s exit:0 -o ignore rump.sysctl -w \
224	    net.inet.ip.redirect=1
225}
226
227teardown_redirect()
228{
229	atf_check -s exit:0 -o ignore rump.sysctl -w \
230	    net.inet.ip.redirect=0
231}
232
233icmp_redirect_body()
234{
235
236	$DEBUG && ulimit -c unlimited
237
238	setup_local
239	setup_peer
240
241	#
242	# Setup a gateway 10.0.0.254. 10.0.2.1 is behind it.
243	#
244	setup_gw
245
246	#
247	# Teach the peer that 10.0.2.* is behind 10.0.0.254
248	#
249	export RUMP_SERVER=$SOCK_PEER
250	atf_check -s exit:0 -o ignore rump.route add -net 10.0.2.0/24 10.0.0.254
251	# Up, Gateway, Static
252	check_entry_flags 10.0.2/24 UGS
253
254	#
255	# Setup the default gateway to the peer, 10.0.0.1
256	#
257	export RUMP_SERVER=$SOCK_LOCAL
258	atf_check -s exit:0 -o ignore rump.route add default 10.0.0.1
259	# Up, Gateway, Static
260	check_entry_flags default UGS
261
262
263	### ICMP redirects are NOT sent by the peer ###
264
265	#
266	# Disable net.inet.ip.redirect
267	#
268	export RUMP_SERVER=$SOCK_PEER
269	teardown_redirect
270
271	# Try ping 10.0.2.1
272	export RUMP_SERVER=$SOCK_LOCAL
273	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.2.1
274	$DEBUG && rump.netstat -rn -f inet
275
276	# A direct route shouldn't be created
277	check_entry_fail 10.0.2.1
278
279
280	### ICMP redirects are sent by the peer ###
281
282	#
283	# Enable net.inet.ip.redirect
284	#
285	export RUMP_SERVER=$SOCK_PEER
286	setup_redirect
287
288	# Try ping 10.0.2.1
289	export RUMP_SERVER=$SOCK_LOCAL
290	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.2.1
291	$DEBUG && rump.netstat -rn -f inet
292
293	# Up, Gateway, Host, Dynamic
294	check_entry_flags 10.0.2.1 UGHD
295	check_entry_gw 10.0.2.1 10.0.0.254
296
297	export RUMP_SERVER=$SOCK_PEER
298	$DEBUG && rump.netstat -rn -f inet
299
300
301	# cleanup
302	export RUMP_SERVER=$SOCK_LOCAL
303	atf_check -s exit:0 -o ignore rump.route delete 10.0.2.1
304	check_entry_fail 10.0.2.1
305
306
307	### ICMP redirects are NOT sent by the peer (again) ###
308
309	#
310	# Disable net.inet.ip.redirect
311	#
312	export RUMP_SERVER=$SOCK_PEER
313	teardown_redirect
314
315	# Try ping 10.0.2.1
316	export RUMP_SERVER=$SOCK_LOCAL
317	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.2.1
318	$DEBUG && rump.netstat -rn -f inet
319
320	# A direct route shouldn't be created
321	check_entry_fail 10.0.2.1
322
323
324	teardown_gw
325}
326
327icmp_redirect_cleanup()
328{
329
330	$DEBUG && dump
331	cleanup
332}
333
334atf_init_test_cases()
335{
336
337	atf_add_test_case icmp_redirect
338	atf_add_test_case icmp_redirect_timeout
339}
340