xref: /minix/tests/net/route/t_flags.sh (revision 0a6a1f1d)
1#	$NetBSD: t_flags.sh,v 1.3 2015/06/01 01:36:30 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
28netserver=\
29"rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif"
30SOCK_LOCAL=unix://commsock1
31SOCK_PEER=unix://commsock2
32SOCK_GW=unix://commsock3
33BUS=bus1
34BUS2=bus2
35
36DEBUG=false
37
38setup_local()
39{
40
41	atf_check -s exit:0 ${netserver} ${SOCK_LOCAL}
42
43	export RUMP_SERVER=$SOCK_LOCAL
44	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
45	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
46	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 10.0.0.2/24
47	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
48
49	$DEBUG && rump.ifconfig
50	$DEBUG && rump.netstat -rn -f inet
51}
52
53setup_peer()
54{
55
56	atf_check -s exit:0 ${netserver} ${SOCK_PEER}
57
58	export RUMP_SERVER=$SOCK_PEER
59	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
60	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
61	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 10.0.0.1/24
62	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
63
64	$DEBUG && rump.ifconfig
65	$DEBUG && rump.netstat -rn -f inet
66}
67
68setup_gw()
69{
70
71	atf_check -s exit:0 ${netserver} ${SOCK_GW}
72
73	export RUMP_SERVER=$SOCK_GW
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.254/24
77	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
78
79	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 create
80	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 linkstr ${BUS2}
81	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 10.0.2.1/24
82	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 alias 10.0.2.2/24
83	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 up
84
85	# Wait until DAD completes (10 sec at most)
86	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
87	atf_check -s not-exit:0 -x "rump.ifconfig shmif1 |grep -q tentative"
88
89	$DEBUG && rump.ifconfig
90	$DEBUG && rump.netstat -rn -f inet
91}
92
93teardown_gw()
94{
95
96	env RUMP_SERVER=$SOCK_GW rump.halt
97}
98
99check_entry_flags()
100{
101	local ip=$(echo $1 |sed 's/\./\\./g')
102	local flags=$2
103
104	atf_check -s exit:0 -o match:" $flags " -e ignore -x \
105	    "rump.netstat -rn -f inet | grep ^'$ip'"
106}
107
108check_entry_gw()
109{
110	local ip=$(echo $1 |sed 's/\./\\./g')
111	local gw=$2
112
113	atf_check -s exit:0 -o match:" $gw " -e ignore -x \
114	    "rump.netstat -rn -f inet | grep ^'$ip'"
115}
116
117check_entry_fail()
118{
119	ip=$(echo $1 |sed 's/\./\\./g')
120	flags=$2  # Not used currently
121
122	atf_check -s not-exit:0 -e ignore -x \
123	    "rump.netstat -rn -f inet | grep ^'$ip'"
124}
125
126test_lo()
127{
128
129	export RUMP_SERVER=$SOCK_LOCAL
130
131	# Up, Host, local
132	check_entry_flags 127.0.0.1 UHl
133}
134
135test_connected()
136{
137
138	export RUMP_SERVER=$SOCK_LOCAL
139
140	# Up, Host, LLINFO, local
141	check_entry_flags 10.0.0.2 UHLl
142
143	# Up, Cloning
144	check_entry_flags 10.0.0/24 UC
145}
146
147test_cloned()
148{
149
150	export RUMP_SERVER=$SOCK_LOCAL
151
152	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.0.1
153	$DEBUG && rump.netstat -rn -f inet
154
155	# Up, Host, LLINFO, cloned
156	check_entry_flags 10.0.0.1 UHLc
157}
158
159test_default_gateway()
160{
161
162	export RUMP_SERVER=$SOCK_LOCAL
163
164	atf_check -s exit:0 -o ignore rump.route add default 10.0.0.1
165	$DEBUG && rump.netstat -rn -f inet
166
167	# Up, Gateway, Static
168	check_entry_flags default UGS
169}
170
171test_static()
172{
173
174	export RUMP_SERVER=$SOCK_LOCAL
175
176	# Static route to host
177	atf_check -s exit:0 -o ignore rump.route add 10.0.1.1 10.0.0.1
178	$DEBUG && rump.netstat -rn -f inet
179
180	# Up, Gateway, Host, Static
181	check_entry_flags 10.0.1.1 UGHS
182
183	# Static route to network
184	atf_check -s exit:0 -o ignore rump.route add -net 10.0.2.0/24 10.0.0.1
185	$DEBUG && rump.netstat -rn -f inet
186
187	# Up, Gateway, Static
188	check_entry_flags 10.0.2/24 UGS
189}
190
191test_route_flush()
192{
193
194	export RUMP_SERVER=$SOCK_LOCAL
195
196	# Reusing other tests to create routes
197	test_cloned_route
198	test_default_gateway
199	test_static_route
200
201	atf_check -s exit:0 -o ignore rump.route flush
202	$DEBUG && rump.netstat -rn -f inet
203
204	# Should remain
205	check_entry_flags 127.0.0.1 UHl
206	check_entry_flags 10.0.0/24 UC
207
208	# Shouldn't remain
209	check_entry_fail default UGS
210	check_entry_fail 10.0.1.1 UGHS
211	check_entry_fail 10.0.2/24 UGS
212	# Should it remain?
213	check_entry_fail 10.0.0.2 UHLl
214}
215
216test_blackhole()
217{
218
219	export RUMP_SERVER=$SOCK_LOCAL
220
221	# Delete an existing route first
222	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
223
224	atf_check -s exit:0 -o ignore rump.route add -net 10.0.0.0/24 10.0.0.1 -blackhole
225	$DEBUG && rump.netstat -rn -f inet
226
227	# Up, Gateway, Blackhole, Static
228	check_entry_flags 10.0.0/24 UGBS
229
230	atf_check -s not-exit:0 -o ignore -e match:'No route to host' \
231	    rump.ping -n -w 1 -c 1 10.0.0.1
232	$DEBUG && rump.netstat -rn -f inet
233
234	# Shouldn't be created
235	check_entry_fail 10.0.0.1 UHLc
236}
237
238test_reject()
239{
240
241	export RUMP_SERVER=$SOCK_LOCAL
242
243	# Delete an existing route first
244	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
245
246	atf_check -s exit:0 -o ignore rump.route add -net 10.0.0.0/24 10.0.0.1 -reject
247	$DEBUG && rump.netstat -rn -f inet
248
249	# Up, Gateway, Reject, Static
250	check_entry_flags 10.0.0/24 UGRS
251
252	atf_check -s not-exit:0 -o ignore -e match:'No route to host' \
253	    rump.ping -n -w 1 -c 1 10.0.0.1
254	$DEBUG && rump.netstat -rn -f inet
255
256	# Shouldn't be created
257	check_entry_fail 10.0.0.1 UHLc
258}
259
260test_icmp_redirect()
261{
262
263	### Testing Dynamic flag ###
264
265	#
266	# Setup a gateway 10.0.0.254. 10.0.2.1 is behind it.
267	#
268	setup_gw
269
270	#
271	# Teach the peer that 10.0.2.* is behind 10.0.0.254
272	#
273	export RUMP_SERVER=$SOCK_PEER
274	atf_check -s exit:0 -o ignore rump.route add -net 10.0.2.0/24 10.0.0.254
275	# Up, Gateway, Static
276	check_entry_flags 10.0.2/24 UGS
277
278	#
279	# Setup the default gateway to the peer, 10.0.0.1
280	#
281	export RUMP_SERVER=$SOCK_LOCAL
282	atf_check -s exit:0 -o ignore rump.route add default 10.0.0.1
283	# Up, Gateway, Static
284	check_entry_flags default UGS
285
286	# Try ping 10.0.2.1
287	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.2.1
288	$DEBUG && rump.netstat -rn -f inet
289
290	# Up, Gateway, Host, Dynamic
291	check_entry_flags 10.0.2.1 UGHD
292	check_entry_gw 10.0.2.1 10.0.0.254
293
294	export RUMP_SERVER=$SOCK_PEER
295	$DEBUG && rump.netstat -rn -f inet
296
297	### Testing Modified flag ###
298
299	#
300	# Teach a wrong route to 10.0.2.2
301	#
302	export RUMP_SERVER=$SOCK_LOCAL
303	atf_check -s exit:0 -o ignore rump.route add 10.0.2.2 10.0.0.1
304	# Up, Gateway, Host, Static
305	check_entry_flags 10.0.2.2 UGHS
306	check_entry_gw 10.0.2.2 10.0.0.1
307
308	# Try ping 10.0.2.2
309	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.2.2
310	$DEBUG && rump.netstat -rn -f inet
311
312	# Up, Gateway, Host, Modified, Static
313	check_entry_flags 10.0.2.2 UGHMS
314	check_entry_gw 10.0.2.2 10.0.0.254
315
316	teardown_gw
317}
318
319test_announce()
320{
321	export RUMP_SERVER=$SOCK_LOCAL
322
323	# Delete an existing route first
324	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
325
326	atf_check -s exit:0 -o ignore rump.route add -net 10.0.0.0/24 10.0.0.1 -proxy
327	$DEBUG && rump.netstat -rn -f inet
328
329	# Up, Gateway, Static, proxy
330	check_entry_flags 10.0.0/24 UGSp
331
332	# TODO test its behavior
333}
334
335test_xresolve_rtm()
336{
337	local ip=$1
338	local rtm=$2
339	local pid=
340
341	rump.route -n monitor > ./mon.log &
342	pid=$!
343
344	# Give route monitor a chance to setup a routing socket
345	sleep 1
346
347	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 $ip
348	$DEBUG && rump.netstat -rn -f inet
349
350	# Give route monitor a chance to output a routing message
351	sleep 1
352	cat ./mon.log
353
354	atf_check -s exit:0 grep -q $rtm ./mon.log
355
356	kill $pid
357}
358
359test_xresolve()
360{
361
362	export RUMP_SERVER=$SOCK_LOCAL
363
364	# For a normal route, a RTM_ADD message is emitted on a route cloning
365	test_xresolve_rtm 10.0.0.1 RTM_ADD
366	# Up, Host, LLINFO, cloned
367	check_entry_flags 10.0.0.1 UHLc
368
369	# Delete an existing route first
370	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
371	# Create a connected route with XRESOLVE flag for the interface
372	atf_check -s exit:0 -o ignore rump.route add -net 10.0.0.0/24 10.0.0.2 \
373	    -interface -xresolve
374	$DEBUG && rump.netstat -rn -f inet
375
376	# Up, Cloning, Xresolve, Static
377	check_entry_flags 10.0.0/24 UCXS
378
379	# If XRESOLVE flag is set, a RTM_RESOLVE message is emitted
380	test_xresolve_rtm 10.0.0.1 RTM_RESOLVE
381	# Up, Host, Xresolve, LLINFO, cloned
382	check_entry_flags 10.0.0.1 UHXLc
383}
384
385cleanup()
386{
387	$DEBUG && /usr/bin/shmif_dumpbus -p - $BUS 2>/dev/null | \
388	    /usr/sbin/tcpdump -n -e -r -
389	env RUMP_SERVER=$SOCK_LOCAL rump.halt
390	env RUMP_SERVER=$SOCK_PEER rump.halt
391}
392
393add_test()
394{
395	local name=$1
396	local desc="$2"
397
398	atf_test_case "route_flags_${name}" cleanup
399	eval "route_flags_${name}_head() { \
400			atf_set \"descr\" \"${desc}\"; \
401			atf_set \"require.progs\" \"rump_server\"; \
402		}; \
403	    route_flags_${name}_body() { \
404			setup_local; \
405			setup_peer; \
406			test_${name}; \
407		}; \
408	    route_flags_${name}_cleanup() { \
409			cleanup; \
410		}"
411	atf_add_test_case "route_flags_${name}"
412}
413
414atf_init_test_cases()
415{
416
417	add_test lo              "Tests route flags: loop back interface"
418	add_test connected       "Tests route flags: connected route"
419	add_test cloned          "Tests route flags: cloned route"
420	add_test default_gateway "Tests route flags: default gateway"
421	add_test static          "Tests route flags: static route"
422	add_test blackhole       "Tests route flags: blackhole route"
423	add_test reject          "Tests route flags: reject route"
424	add_test icmp_redirect   "Tests route flags: icmp redirect"
425	add_test announce        "Tests route flags: announce flag"
426	add_test xresolve        "Tests route flags: xresolve flag"
427}
428