1#	$NetBSD: t_ipv6address.sh,v 1.12 2016/12/14 02:50:42 ozaki-r Exp $
2#
3# Copyright (c) 2015 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
27SERVER="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet"
28SERVER="${SERVER} -lrumpnet_shmif -lrumpdev"
29SERVER6="${SERVER} -lrumpnet_netinet6"
30
31SOCKSRC=unix://commsock1
32SOCKFWD=unix://commsock2
33SOCKDST=unix://commsock3
34IP6SRCNW=fc00:1::0/64
35IP6SRC=fc00:1::1
36IP6DSTNW=fc00:2::0/64
37IP6DST=fc00:2::1
38IP6FWD0=fc00:3::1
39BUS1=bus1
40BUS2=bus2
41BUSSRC=bus_src
42BUSDST=bus_dst
43
44DEBUG=${DEBUG:-true}
45TIMEOUT=3
46
47atf_test_case linklocal cleanup
48atf_test_case linklocal_ops cleanup
49
50setup()
51{
52	atf_check -s exit:0 ${SERVER6} ${SOCKSRC}
53	atf_check -s exit:0 ${SERVER6} ${SOCKFWD}
54	atf_check -s exit:0 ${SERVER6} ${SOCKDST}
55
56	export RUMP_SERVER=${SOCKSRC}
57	atf_check -s exit:0 rump.ifconfig shmif0 create
58	atf_check -s exit:0 rump.ifconfig shmif1 create
59	unset RUMP_SERVER
60
61	export RUMP_SERVER=${SOCKDST}
62	atf_check -s exit:0 rump.ifconfig shmif0 create
63	atf_check -s exit:0 rump.ifconfig shmif1 create
64	unset RUMP_SERVER
65
66	export RUMP_SERVER=${SOCKFWD}
67	atf_check -s exit:0 rump.ifconfig shmif0 create
68	atf_check -s exit:0 rump.ifconfig shmif1 create
69	atf_check -s exit:0 -o match:"0 -> 1" rump.sysctl \
70	    -w net.inet6.ip6.forwarding=1
71	unset RUMP_SERVER
72
73	setup_ifcfg
74
75	export RUMP_SERVER=${SOCKSRC}
76	$DEBUG && rump.ifconfig
77	$DEBUG && rump.netstat -rn -f inet6
78	unset RUMP_SERVER
79
80	export RUMP_SERVER=${SOCKDST}
81	$DEBUG && rump.ifconfig
82	$DEBUG && rump.netstat -rn -f inet6
83	unset RUMP_SERVER
84
85	export RUMP_SERVER=${SOCKFWD}
86	$DEBUG && rump.ifconfig
87	$DEBUG && rump.netstat -rn -f inet6
88	unset RUMP_SERVER
89}
90setup_ifcfg()
91{
92	export RUMP_SERVER=${SOCKSRC}
93	atf_check -s exit:0 rump.ifconfig shmif0 linkstr ${BUS1}
94	atf_check -s exit:0 rump.ifconfig shmif0 up
95	atf_check -s exit:0 rump.ifconfig shmif1 linkstr ${BUSSRC}
96	atf_check -s exit:0 rump.ifconfig shmif1 up
97	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
98	unset RUMP_SERVER
99
100	export RUMP_SERVER=${SOCKDST}
101	atf_check -s exit:0 rump.ifconfig shmif0 linkstr ${BUS2}
102	atf_check -s exit:0 rump.ifconfig shmif0 up
103	atf_check -s exit:0 rump.ifconfig shmif1 linkstr ${BUSDST}
104	atf_check -s exit:0 rump.ifconfig shmif1 up
105	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
106	unset RUMP_SERVER
107
108	export RUMP_SERVER=${SOCKFWD}
109	atf_check -s exit:0 rump.ifconfig shmif0 linkstr ${BUS1}
110	atf_check -s exit:0 rump.ifconfig shmif0 up
111
112	atf_check -s exit:0 rump.ifconfig shmif1 linkstr ${BUS2}
113	atf_check -s exit:0 rump.ifconfig shmif1 up
114	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
115	unset RUMP_SERVER
116}
117
118setup_route()
119{
120	local tmp_rump_server=$RUMP_SERVER
121
122	local src_if0_lladdr=`get_linklocal_addr ${SOCKSRC} shmif0`
123	local dst_if0_lladdr=`get_linklocal_addr ${SOCKDST} shmif0`
124	local fwd_if0_lladdr=`get_linklocal_addr ${SOCKFWD} shmif0`
125	local fwd_if1_lladdr=`get_linklocal_addr ${SOCKFWD} shmif1`
126
127	export RUMP_SERVER=${SOCKSRC}
128	atf_check -s ignore -o ignore -e ignore \
129	    rump.route delete -inet6 default ${fwd_if0_lladdr}%shmif0
130	atf_check -s exit:0 -o match:"add net default:" \
131	    rump.route add    -inet6 default ${fwd_if0_lladdr}%shmif0
132	atf_check -s exit:0 rump.ifconfig shmif1 inet6 ${IP6SRC}
133	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
134	$DEBUG && rump.netstat -rn -f inet6
135	unset RUMP_SERVER
136
137	export RUMP_SERVER=${SOCKDST}
138	atf_check -s ignore -o ignore -e ignore \
139	    rump.route delete -inet6 default ${fwd_if1_lladdr}%shmif0
140	atf_check -s exit:0 -o match:"add net default:" \
141	    rump.route add    -inet6 default ${fwd_if1_lladdr}%shmif0
142	atf_check -s exit:0 rump.ifconfig shmif1 inet6 ${IP6DST}
143	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
144	$DEBUG && rump.netstat -rn -f inet6
145	unset RUMP_SERVER
146
147	export RUMP_SERVER=${SOCKFWD}
148	atf_check -s ignore -o ignore -e ignore \
149	    rump.route delete -inet6 ${IP6SRCNW} ${src_if0_lladdr}%shmif0
150	atf_check -s exit:0 -o match:"add net" \
151	    rump.route add    -inet6 ${IP6SRCNW} ${src_if0_lladdr}%shmif0
152
153	atf_check -s ignore -o ignore -e ignore \
154	    rump.route delete -inet6 ${IP6DSTNW} ${dst_if0_lladdr}%shmif1
155	atf_check -s exit:0 -o match:"add net" \
156	    rump.route add    -inet6 ${IP6DSTNW} ${dst_if0_lladdr}%shmif1
157	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
158	$DEBUG && rump.netstat -rn -f inet6
159	unset RUMP_SERVER
160
161	export RUMP_SERVER=$tmp_rump_server
162}
163
164cleanup_bus()
165{
166	local tmp_rump_server=$RUMP_SERVER
167
168	$DEBUG && dump_bus
169
170	export RUMP_SERVER=${SOCKSRC}
171	atf_check -s exit:0 rump.ifconfig shmif0 down
172	atf_check -s exit:0 rump.ifconfig shmif0 -linkstr
173	atf_check -s exit:0 rump.ifconfig shmif1 down
174	atf_check -s exit:0 rump.ifconfig shmif1 -linkstr
175	unset RUMP_SERVER
176
177	export RUMP_SERVER=${SOCKDST}
178	atf_check -s exit:0 rump.ifconfig shmif0 down
179	atf_check -s exit:0 rump.ifconfig shmif0 -linkstr
180	atf_check -s exit:0 rump.ifconfig shmif1 down
181	atf_check -s exit:0 rump.ifconfig shmif1 -linkstr
182	unset RUMP_SERVER
183
184	export RUMP_SERVER=${SOCKFWD}
185	atf_check -s exit:0 rump.ifconfig shmif0 down
186	atf_check -s exit:0 rump.ifconfig shmif0 -linkstr
187	atf_check -s exit:0 rump.ifconfig shmif1 down
188	atf_check -s exit:0 rump.ifconfig shmif1 -linkstr
189	unset RUMP_SERVER
190
191	atf_check -s exit:0 rm ${BUSSRC}
192	atf_check -s exit:0 rm ${BUSDST}
193	atf_check -s exit:0 rm ${BUS1}
194	atf_check -s exit:0 rm ${BUS2}
195
196	setup_ifcfg
197
198	export RUMP_SERVER=$tmp_rump_server
199}
200
201cleanup_rump_servers()
202{
203
204	env RUMP_SERVER=${SOCKSRC} rump.halt
205	env RUMP_SERVER=${SOCKDST} rump.halt
206	env RUMP_SERVER=${SOCKFWD} rump.halt
207}
208
209dump_bus()
210{
211
212	shmif_dumpbus -p - ${BUSSRC} 2>/dev/null| tcpdump -n -e -r -
213	shmif_dumpbus -p - ${BUSDST} 2>/dev/null| tcpdump -n -e -r -
214	shmif_dumpbus -p - ${BUS1}   2>/dev/null| tcpdump -n -e -r -
215	shmif_dumpbus -p - ${BUS2}   2>/dev/null| tcpdump -n -e -r -
216}
217
218_dump()
219{
220
221	export RUMP_SERVER=${SOCKSRC}
222	rump.ndp -n -a
223	rump.netstat -nr -f inet6
224	export RUMP_SERVER=${SOCKDST}
225	rump.ndp -n -a
226	rump.netstat -nr -f inet6
227	export RUMP_SERVER=${SOCKFWD}
228	rump.ndp -n -a
229	rump.netstat -nr -f inet6
230	unset RUMP_SERVER
231}
232
233linklocal_head()
234{
235	atf_set "descr" \
236	    "Test for bassically function of the IPv6 linklocal address"
237	atf_set "require.progs" \
238	    "rump_server rump.route rump.ifconfig rump.ping6"
239}
240
241linklocal_body()
242{
243	setup
244
245	local src_if0_lladdr=`get_linklocal_addr ${SOCKSRC} shmif0`
246	local src_if1_lladdr=`get_linklocal_addr ${SOCKSRC} shmif1`
247	local dst_if0_lladdr=`get_linklocal_addr ${SOCKDST} shmif0`
248	local fwd_if0_lladdr=`get_linklocal_addr ${SOCKFWD} shmif0`
249	local fwd_if1_lladdr=`get_linklocal_addr ${SOCKFWD} shmif1`
250
251	export RUMP_SERVER=${SOCKSRC}
252	$DEBUG && rump.ifconfig
253	$DEBUG && rump.netstat -rn -f inet6
254
255	# link local address to link local address
256
257	atf_check -s not-exit:0 -e match:"No route to host" \
258	    rump.ping6 -c 1 -X $TIMEOUT -n ${fwd_if0_lladdr}
259
260	atf_check -s exit:0 -o match:"0.0% packet loss" \
261	    rump.ping6 -c 1 -X $TIMEOUT -n ${fwd_if0_lladdr}%shmif0
262
263	atf_check -s ignore -o empty -e ignore \
264	    -x "shmif_dumpbus -p - ${BUSSRC} | tcpdump -r - -n -p icmp6"
265	atf_check -s ignore -o not-empty -e ignore \
266	    -x "shmif_dumpbus -p - ${BUS1}   | tcpdump -r - -n -p icmp6"
267
268	cleanup_bus
269
270	atf_check -s not-exit:0 -o ignore -e ignore \
271	    rump.ping6 -c 1 -X $TIMEOUT -n -S ${src_if1_lladdr}%shmif1 \
272	    ${fwd_if0_lladdr}%shmif0
273	atf_check -s ignore -o not-match:"${src_if1_lladdr}" -e ignore \
274	    -x   "shmif_dumpbus -p - ${BUS1} | tcpdump -r - -n -p icmp6"
275	$DEBUG && shmif_dumpbus -p - ${BUS1} | tcpdump -r - -n -p icmp6
276	unset RUMP_SERVER
277
278	# link local address to host address
279	export RUMP_SERVER=${SOCKFWD}
280	atf_check -s exit:0 rump.ifconfig shmif0 inet6 ${IP6FWD0}
281	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
282	unset RUMP_SERVER
283
284	export RUMP_SERVER=${SOCKSRC}
285	atf_check -s exit:0 -o match:"add net default:" \
286	    rump.route add -inet6 default ${fwd_if0_lladdr}%shmif0
287	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
288
289	$DEBUG && rump.ifconfig shmif0
290	$DEBUG && _dump
291
292	export RUMP_SERVER=${SOCKSRC}
293	atf_check -s exit:0 -o match:"0.0% packet loss" \
294	  rump.ping6 -c 1 -X $TIMEOUT -n -S ${src_if0_lladdr}%shmif0 ${IP6FWD0}
295	unset RUMP_SERVER
296
297	export RUMP_SERVER=${SOCKFWD}
298	# host address to link local address
299	atf_check -s exit:0 -o match:"0.0% packet loss" \
300	    rump.ping6 -c 1 -X $TIMEOUT -n ${src_if0_lladdr}%shmif0
301	atf_check -s not-exit:0 -o match:"100.0% packet loss" \
302	    rump.ping6 -c 1 -X $TIMEOUT -n ${src_if1_lladdr}%shmif0
303
304	atf_check -s exit:0 rump.ifconfig shmif0 inet6 ${IP6FWD0} delete
305
306	unset RUMP_SERVER
307
308	# forwarding with link local address
309	setup_route
310
311	export RUMP_SERVER=${SOCKSRC}
312	atf_check -s exit:0 -o match:"0.0% packet loss" rump.ping6 -c 1 \
313	    -X $TIMEOUT -n -S ${IP6SRC} ${IP6DST}
314
315	cleanup_bus
316	$DEBUG && rump.ifconfig shmif0
317	atf_check -s not-exit:0 -o match:"100.0% packet loss" rump.ping6 -c 1 \
318	    -X $TIMEOUT -n -S ${src_if0_lladdr}%shmif0 ${IP6DST}
319	atf_check -s ignore -o not-match:"${src_if0_lladdr}" -e ignore \
320	    -x "shmif_dumpbus -p - ${BUS2} | tcpdump -r - -n -p icmp6"
321
322	cleanup_bus
323	atf_check -s not-exit:0 -o match:"100.0% packet loss" rump.ping6 -c 1 \
324	    -X $TIMEOUT -n -S ${IP6SRC} ${dst_if0_lladdr}%shmif0
325	atf_check -s ignore -o not-empty -e ignore \
326	    -x "shmif_dumpbus -p - ${BUS2} | tcpdump -r - -n -p icmp6"
327
328	unset RUMP_SERVER
329
330}
331
332linklocal_cleanup()
333{
334
335	$DEBUG && _dump
336	$DEBUG && dump_bus
337	cleanup_rump_servers
338}
339
340linklocal_ops_head()
341{
342
343	atf_set "descr" \
344	    "Test for various operations to IPv6 linklocal addresses"
345	atf_set "require.progs" "rump_server rump.route rump.ndp"
346}
347
348linklocal_ops_body()
349{
350	local src_if0_lladdr=
351
352	setup
353
354	src_if0_lladdr=`get_linklocal_addr ${SOCKSRC} shmif0`
355
356	export RUMP_SERVER=${SOCKSRC}
357
358	# route get
359	atf_check -s exit:0 -o match:"${src_if0_lladdr}" \
360	    rump.route get -inet6 ${src_if0_lladdr}%shmif0
361
362	# route get without an interface name (zone index)
363	atf_check -s not-exit:0 -e match:"not in table" \
364	    rump.route get -inet6 ${src_if0_lladdr}
365
366	# ndp
367	atf_check -s exit:0 -o match:"${src_if0_lladdr}" \
368	    rump.ndp -n ${src_if0_lladdr}%shmif0
369
370	# ndp without an interface name (zone index)
371	atf_check -s not-exit:0 -o ignore -e match:"no entry" \
372	    rump.ndp -n ${src_if0_lladdr}
373}
374
375
376linklocal_ops_cleanup()
377{
378
379	cleanup_rump_servers
380}
381
382atf_init_test_cases()
383{
384
385	atf_add_test_case linklocal
386	atf_add_test_case linklocal_ops
387}
388