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