xref: /freebsd/contrib/netbsd-tests/net/route/t_route.sh (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1#	$NetBSD: t_route.sh,v 1.10 2016/12/21 02:46:08 ozaki-r Exp $
2#
3# Copyright (c) 2016 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# non_subnet_gateway
29SOCK_CLIENT=unix://commsock1
30SOCK_GW=unix://commsock2
31BUS=bus1
32
33# command_get
34SOCKSRC=unix://commsock1
35SOCKFWD=unix://commsock2
36SOCKDST=unix://commsock3
37IP4SRC=10.0.1.2
38IP4SRCGW=10.0.1.1
39IP4DSTGW=10.0.2.1
40IP4DST=10.0.2.2
41IP4DST_BCAST=10.0.2.255
42IP6SRC=fc00:0:0:1::2
43IP6SRCGW=fc00:0:0:1::1
44IP6DSTGW=fc00:0:0:2::1
45IP6DST=fc00:0:0:2::2
46BUS_SRCGW=bus1
47BUS_DSTGW=bus2
48
49DEBUG=${DEBUG:-false}
50TIMEOUT=1
51PING_OPTS="-n -c 1 -w $TIMEOUT"
52
53atf_test_case route_non_subnet_gateway cleanup
54route_non_subnet_gateway_head()
55{
56
57	atf_set "descr" "tests of a gateway not on the local subnet"
58	atf_set "require.progs" "rump_server"
59}
60
61route_non_subnet_gateway_body()
62{
63
64	rump_server_start $SOCK_CLIENT
65	rump_server_start $SOCK_GW
66
67	export RUMP_SERVER=${SOCK_GW}
68	rump_server_add_iface $SOCK_GW shmif0 $BUS
69	atf_check -s exit:0 rump.ifconfig shmif0 192.168.0.1
70	atf_check -s exit:0 rump.ifconfig shmif0 up
71
72	# The gateway knows the client
73	atf_check -s exit:0 -o match:'add net 10.0.0.1: gateway shmif0' \
74	    rump.route add -net 10.0.0.1/32 -link -cloning -iface shmif0
75
76	$DEBUG && rump.netstat -nr -f inet
77
78	export RUMP_SERVER=${SOCK_CLIENT}
79	rump_server_add_iface $SOCK_CLIENT shmif0 $BUS
80	atf_check -s exit:0 rump.ifconfig shmif0 10.0.0.1/32
81	atf_check -s exit:0 rump.ifconfig shmif0 up
82	atf_check -s exit:0 rump.ifconfig -w 10
83
84	$DEBUG && rump.netstat -nr -f inet
85
86	# Don't know a route to the gateway yet
87	atf_check -s not-exit:0 -o match:'100.0% packet loss' \
88	    -e match:'No route to host' rump.ping $PING_OPTS 192.168.0.1
89
90	# Teach a route to the gateway
91	atf_check -s exit:0 -o match:'add net 192.168.0.1: gateway shmif0' \
92	    rump.route add -net 192.168.0.1/32 -link -cloning -iface shmif0
93	atf_check -s exit:0 -o match:'add net default: gateway 192.168.0.1' \
94	    rump.route add default -ifa 10.0.0.1 192.168.0.1
95
96	$DEBUG && rump.netstat -nr -f inet
97
98	# Be reachable to the gateway
99	atf_check -s exit:0 -o ignore rump.ping $PING_OPTS 192.168.0.1
100
101	rump_server_destroy_ifaces
102}
103
104route_non_subnet_gateway_cleanup()
105{
106
107	$DEBUG && dump
108	cleanup
109}
110
111atf_test_case route_command_get cleanup
112atf_test_case route_command_get6 cleanup
113route_command_get_head()
114{
115
116	atf_set "descr" "tests of route get command"
117	atf_set "require.progs" "rump_server"
118}
119
120route_command_get6_head()
121{
122
123	atf_set "descr" "tests of route get command (IPv6)"
124	atf_set "require.progs" "rump_server"
125}
126
127setup_endpoint()
128{
129	local sock=${1}
130	local addr=${2}
131	local bus=${3}
132	local mode=${4}
133	local gw=${5}
134
135	export RUMP_SERVER=${sock}
136	rump_server_add_iface $sock shmif0 $bus
137	if [ $mode = "ipv6" ]; then
138		atf_check -s exit:0 rump.ifconfig shmif0 inet6 ${addr}
139		atf_check -s exit:0 -o ignore rump.route add -inet6 default ${gw}
140	else
141		atf_check -s exit:0 rump.ifconfig shmif0 inet ${addr} netmask 0xffffff00
142		atf_check -s exit:0 -o ignore rump.route add default ${gw}
143	fi
144	atf_check -s exit:0 rump.ifconfig shmif0 up
145	atf_check -s exit:0 rump.ifconfig -w 10
146
147	if $DEBUG; then
148		rump.ifconfig shmif0
149		rump.netstat -nr
150	fi
151}
152
153setup_forwarder()
154{
155	mode=${1}
156
157	rump_server_add_iface $SOCKFWD shmif0 $BUS_SRCGW
158	rump_server_add_iface $SOCKFWD shmif1 $BUS_DSTGW
159
160	export RUMP_SERVER=$SOCKFWD
161	if [ $mode = "ipv6" ]; then
162		atf_check -s exit:0 rump.ifconfig shmif0 inet6 ${IP6SRCGW}
163		atf_check -s exit:0 rump.ifconfig shmif1 inet6 ${IP6DSTGW}
164	else
165		atf_check -s exit:0 rump.ifconfig shmif0 inet ${IP4SRCGW} netmask 0xffffff00
166		atf_check -s exit:0 rump.ifconfig shmif1 inet ${IP4DSTGW} netmask 0xffffff00
167	fi
168
169	atf_check -s exit:0 rump.ifconfig shmif0 up
170	atf_check -s exit:0 rump.ifconfig shmif1 up
171	atf_check -s exit:0 rump.ifconfig -w 10
172
173	if $DEBUG; then
174		rump.netstat -nr
175		if [ $mode = "ipv6" ]; then
176			rump.sysctl net.inet6.ip6.forwarding
177		else
178			rump.sysctl net.inet.ip.forwarding
179		fi
180	fi
181}
182
183setup_forwarding()
184{
185	export RUMP_SERVER=$SOCKFWD
186	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.ip.forwarding=1
187}
188
189setup_forwarding6()
190{
191	export RUMP_SERVER=$SOCKFWD
192	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet6.ip6.forwarding=1
193}
194
195setup()
196{
197
198	rump_server_start $SOCKSRC
199	rump_server_start $SOCKFWD
200	rump_server_start $SOCKDST
201
202	setup_endpoint $SOCKSRC $IP4SRC $BUS_SRCGW ipv4 $IP4SRCGW
203	setup_endpoint $SOCKDST $IP4DST $BUS_DSTGW ipv4 $IP4DSTGW
204	setup_forwarder ipv4
205}
206
207setup6()
208{
209
210	rump_server_start $SOCKSRC netinet6
211	rump_server_start $SOCKFWD netinet6
212	rump_server_start $SOCKDST netinet6
213
214	setup_endpoint $SOCKSRC $IP6SRC $BUS_SRCGW ipv6 $IP6SRCGW
215	setup_endpoint $SOCKDST $IP6DST $BUS_DSTGW ipv6 $IP6DSTGW
216	setup_forwarder ipv6
217}
218
219test_route_get()
220{
221
222	export RUMP_SERVER=$SOCKSRC
223	$DEBUG && rump.netstat -nr -f inet
224	$DEBUG && rump.arp -n -a
225
226	# Make sure an ARP cache to the gateway doesn't exist
227	rump.arp -d $IP4SRCGW
228
229	# Local
230	cat >./expect <<-EOF
231   route to: 10.0.1.2
232destination: 10.0.1.2
233 local addr: 10.0.1.2
234  interface: lo0
235      flags: <UP,HOST,DONE,LLINFO,LOCAL>
236 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
237	EOF
238	rump.route -n get $IP4SRC > ./output
239	$DEBUG && cat ./expect ./output
240	# XXX: omit the last line because expire is unstable on rump kernel.
241	sed -i '$d' ./output
242	atf_check -s exit:0 diff ./expect ./output
243
244	# Neighbor
245	cat >./expect <<-EOF
246   route to: 10.0.1.1
247destination: 10.0.1.0
248       mask: 255.255.255.0
249 local addr: 10.0.1.2
250  interface: shmif0
251      flags: <UP,DONE,CONNECTED>
252 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
253	EOF
254	rump.route -n get $IP4SRCGW > ./output
255	$DEBUG && cat ./expect ./output
256	sed -i '$d' ./output
257	atf_check -s exit:0 diff ./expect ./output
258
259	# Remote host
260	cat >./expect <<-EOF
261   route to: 10.0.2.2
262destination: default
263       mask: default
264    gateway: 10.0.1.1
265 local addr: 10.0.1.2
266  interface: shmif0
267      flags: <UP,GATEWAY,DONE,STATIC>
268 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
269	EOF
270	rump.route -n get $IP4DST > ./output
271	$DEBUG && cat ./expect ./output
272	sed -i '$d' ./output
273	atf_check -s exit:0 diff ./expect ./output
274
275	# Create a ARP cache
276	atf_check -s exit:0 -o ignore rump.ping -q -n -w $TIMEOUT -c 1 $IP4SRCGW
277
278	# Neighbor with a cache (no different from w/o cache)
279	cat >./expect <<-EOF
280   route to: 10.0.1.1
281destination: 10.0.1.0
282       mask: 255.255.255.0
283 local addr: 10.0.1.2
284  interface: shmif0
285      flags: <UP,DONE,CONNECTED>
286 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
287	EOF
288	rump.route -n get $IP4SRCGW > ./output
289	$DEBUG && cat ./expect ./output
290	sed -i '$d' ./output
291	atf_check -s exit:0 diff ./expect ./output
292}
293
294test_route_get6()
295{
296
297	export RUMP_SERVER=$SOCKSRC
298	$DEBUG && rump.netstat -nr -f inet
299	$DEBUG && rump.ndp -n -a
300
301	# Make sure an ARP cache to the gateway doesn't exist
302	rump.ndp -d $IP6SRCGW
303
304	# Local
305	cat >./expect <<-EOF
306   route to: fc00:0:0:1::2
307destination: fc00:0:0:1::2
308 local addr: fc00:0:0:1::2
309  interface: lo0
310      flags: <UP,HOST,DONE,LLINFO,LOCAL>
311 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
312	EOF
313	rump.route -n get -inet6 $IP6SRC > ./output
314	$DEBUG && cat ./expect ./output
315	sed -i '$d' ./output
316	atf_check -s exit:0 diff ./expect ./output
317
318	# Neighbor
319	cat >./expect <<-EOF
320   route to: fc00:0:0:1::1
321destination: fc00:0:0:1::
322       mask: ffff:ffff:ffff:ffff::
323 local addr: fc00:0:0:1::2
324  interface: shmif0
325      flags: <UP,DONE,CONNECTED>
326 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
327	EOF
328	rump.route -n get -inet6 $IP6SRCGW > ./output
329	$DEBUG && cat ./expect ./output
330	sed -i '$d' ./output
331	atf_check -s exit:0 diff ./expect ./output
332
333	# Remote host
334	cat >./expect <<-EOF
335   route to: fc00:0:0:2::2
336destination: ::
337       mask: default
338    gateway: fc00:0:0:1::1
339 local addr: fc00:0:0:1::2
340  interface: shmif0
341      flags: <UP,GATEWAY,DONE,STATIC>
342 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
343	EOF
344	rump.route -n get -inet6 $IP6DST > ./output
345	$DEBUG && cat ./expect ./output
346	sed -i '$d' ./output
347	atf_check -s exit:0 diff ./expect ./output
348
349	# Create a NDP cache
350	atf_check -s exit:0 -o ignore rump.ping6 -n -c 1 -X $TIMEOUT $IP6SRCGW
351
352	# Neighbor with a cache (no different from w/o cache)
353	cat >./expect <<-EOF
354   route to: fc00:0:0:1::1
355destination: fc00:0:0:1::
356       mask: ffff:ffff:ffff:ffff::
357 local addr: fc00:0:0:1::2
358  interface: shmif0
359      flags: <UP,DONE,CONNECTED>
360 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
361	EOF
362	rump.route -n get -inet6 $IP6SRCGW > ./output
363	$DEBUG && cat ./expect ./output
364	sed -i '$d' ./output
365	atf_check -s exit:0 diff ./expect ./output
366}
367
368route_command_get_body()
369{
370
371	setup
372	setup_forwarding
373	test_route_get
374	rump_server_destroy_ifaces
375}
376
377route_command_get6_body()
378{
379
380	setup6
381	setup_forwarding6
382	test_route_get6
383	rump_server_destroy_ifaces
384}
385
386route_command_get_cleanup()
387{
388
389	$DEBUG && dump
390	cleanup
391}
392
393route_command_get6_cleanup()
394{
395
396	$DEBUG && dump
397	cleanup
398}
399
400atf_init_test_cases()
401{
402
403	atf_add_test_case route_non_subnet_gateway
404	atf_add_test_case route_command_get
405	atf_add_test_case route_command_get6
406}
407