summaryrefslogtreecommitdiffstats
path: root/tools/test/netfibs/reflector.sh
blob: 3f8c43ae4f26f6a49059611b921172868d434939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
#!/bin/sh
#-
# Copyright (c) 2012 Cisco Systems, Inc.
# All rights reserved.
#
# This software was developed by Bjoern Zeeb under contract to
# Cisco Systems, Inc..
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#

# We will use the RFC5180 (and Errata) benchmarking working group prefix
# 2001:0002::/48 for testing.
PREFIX="2001:2:"

# Set IFACE to the real interface you want to run the test on.
: ${IFACE:=lo0}

# Control port we use to exchange messages between nodes to sync. tests, etc.
: ${CTRLPORT:=6666}

# Get the number of FIBs from the kernel.
RT_NUMFIBS=`sysctl -n net.fibs`

PEERADDR="2001:2:ff00::1"
OURADDR="2001:2:ff00::2"

OURLINKLOCAL=""
PEERLINKLOCAL=""

# By default all commands must succeed.  Individual tests may disable this
# temporary.
set -e

# Debug magic.
case "${DEBUG}" in
42)	set -x ;;
esac



#
# Helper functions.
#

# Function to avoid prelist races adding and deleting prefixes too quickly.
delay()
{

	# sleep 1 is too long.
	touch /tmp/foo || true
	stat /tmp/foo > /dev/null 2>&1 || true
}

check_rc()
{
	local _rc _exp _testno _testname _msg _r
	_rc=$1
	_exp=$2
	_testno=$3
	_testname="$4"
	_msg="$5"

	_r="not ok"
	if test ${_rc} -eq ${_exp}; then
		_r="ok"
	fi
	echo "${_r} ${_testno} ${_testname} # ${_msg} ${_rc}"
}

print_debug()
{
	local _msg
	_msg="$*"

	case ${DEBUG} in
	''|0)	;;
	*)	echo "DEBUG: ${_msg}" >&2 ;;
	esac
}

die()
{
	local _msg
	_msg="$*"

	echo "ERROR: ${_msg}" >&2
	exit 1
}

#
# Test functions.
#

# Setup our side and wait for the peer to tell us that it is ready.
wait_remote_ready()
{
	local _greeting _keyword _fibs _linklocal i

	print_debug "Setting up interface ${IFACE}"
	ifconfig ${IFACE} inet6 ${OURADDR}/64 -alias > /dev/null 2>&1 || true
	delay
	ifconfig ${IFACE} inet6 ${OURADDR}/64 alias up
	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do
		ifconfig ${IFACE} inet6 2001:2:${i}::2/64 -alias \
		    > /dev/null 2>&1 || true
		delay
		i=$((i + 1))
	done
	OURLINKLOCAL=`ifconfig ${IFACE} | awk '/inet6 fe80:/ { print $2 }'`

	# Let things settle.
	print_debug "Waiting 4 seconds for things to settle"
	sleep 4

	# Wait for the remote to connect and start things.
	# We tell it the magic keyword, our number of FIBs and our link-local.
	# It already knows our global address.
	_greeting=`echo "SETUP ${RT_NUMFIBS} ${OURLINKLOCAL}" | \
	    nc -6 -l ${CTRLPORT}`

	read _keyword _fibs _linklocal <<EOI
${_greeting}
EOI
	print_debug "_keyword=${_keyword}"
	print_debug "_fibs=${_fibs}"
	print_debug "_linklocal=${_linklocal}"
	case ${_keyword} in
	SETUP)	;;
	*)	die "Got invalid keyword in greeting: ${_greeting}"
		;;
	esac
	if test ${_fibs} -ne ${RT_NUMFIBS}; then
		die "Number of FIBs not matching ours (${RT_NUMFIBS}) in" \
		    "greeting: ${_greeting}"
	fi
	PEERLINKLOCAL=${_linklocal}

	print_debug "Successfully received greeting. Peer at ${PEERLINKLOCAL}"
}

cleanup()
{

	print_debug "Removing address from interface ${IFACE}"
	ifconfig ${IFACE} inet6 ${OURADDR}/64 -alias
	delay
}

################################################################################
#
testtx_icmp6_connected()
{
	local _opts

	_opts=""
	case ${DEBUG} in
	''|0)	;;
	42)	_opts="-d -d" ;;
	*)	_opts="-d" ;;
	esac
	print_debug "./reflect -p ${CTRLPORT} -T TCP6 " \
	    "-t testtx_icmp6_connected ${_opts}"
	./reflect -p ${CTRLPORT} -T TCP6 -t testtx_icmp6_connected ${_opts}
	print_debug "reflect terminated without error."
}

testtx_tcp6_connected()
{
	local _opts

	_opts=""
	case ${DEBUG} in
	''|0)	;;
	42)	_opts="-d -d" ;;
	*)	_opts="-d" ;;
	esac
	print_debug "./reflect -p ${CTRLPORT} -T TCP6 " \
	    "-t testtx_tcp6_connected ${_opts}"
	./reflect -p ${CTRLPORT} -T TCP6 -t testtx_tcp6_connected ${_opts}
	print_debug "reflect terminated without error."
}

testtx_udp6_connected()
{
	local _opts

	_opts=""
	case ${DEBUG} in
	''|0)	;;
	42)	_opts="-d -d" ;;
	*)	_opts="-d" ;;
	esac
	print_debug "./reflect -p ${CTRLPORT} -T UDP6 " \
	    "-t testtx_udp6_connected ${_opts}"
	./reflect -p ${CTRLPORT} -T UDP6 -t testtx_udp6_connected ${_opts}
	print_debug "reflect terminated without error."
}

################################################################################
#
testtx_icmp6_connected_blackhole()
{
	local _opts fib

	_opts=""
	case ${DEBUG} in
	''|0)	;;
	42)	_opts="-d -d" ;;
	*)	_opts="-d" ;;
	esac

	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		print_debug "./reflect -p ${CTRLPORT} -T TCP6 " \
		    "-t testtx_icmp6_connected_blackhole${fib} ${_opts}"
		./reflect -p ${CTRLPORT} -T TCP6 \
		    -t testtx_icmp6_connected_blackhole${fib} ${_opts}
		print_debug "reflect terminated without error."
		fib=$((fib + 1))
	done
}

testtx_tcp6_connected_blackhole()
{
	local _opts fib

	_opts=""
	case ${DEBUG} in
	''|0)	;;
	42)	_opts="-d -d" ;;
	*)	_opts="-d" ;;
	esac

	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		print_debug "./reflect -p ${CTRLPORT} -T TCP6 " \
		    "-t testtx_tcp6_connected_blackhole${fib} ${_opts}"
		./reflect -p ${CTRLPORT} -T TCP6 \
		    -t testtx_tcp6_connected_blackhole${fib} ${_opts}
		print_debug "reflect terminated without error."
		fib=$((fib + 1))
	done
}

testtx_udp6_connected_blackhole()
{
	local _opts fib

	_opts=""
	case ${DEBUG} in
	''|0)	;;
	42)	_opts="-d -d" ;;
	*)	_opts="-d" ;;
	esac

	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		print_debug "./reflect -p ${CTRLPORT} -T UDP6 " \
		    "-t testtx_udp6_connected_blackhole${fib} ${_opts}"
		./reflect -p ${CTRLPORT} -T UDP6 \
		    -t testtx_udp6_connected_blackhole${fib} ${_opts}
		print_debug "reflect terminated without error."
		fib=$((fib + 1))
	done
}

################################################################################
#
testtx_ulp6_connected_transfernets()
{
	local _opts fib _n _o
	_n="$1"
	_o="$2"

	_opts=""
	case ${DEBUG} in
	''|0)	;;
	42)	_opts="-d -d" ;;
	*)	_opts="-d" ;;
	esac

	# Setup transfer networks.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		setfib -F${fib} \
		    ifconfig ${IFACE} inet6 2001:2:${fib}::2/64 alias
		fib=$((fib + 1))
	done

	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		print_debug "./reflect -p ${CTRLPORT} -T ${_o} -t ${_n}${fib} ${_opts}"
		./reflect -p ${CTRLPORT} -T ${_o} -t ${_n}${fib} ${_opts}
		print_debug "reflect terminated without error."
		fib=$((fib + 1))
	done

	# Cleanup transfer networks.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		setfib -F${fib} \
		    ifconfig ${IFACE} inet6 2001:2:${fib}::2/64 -alias
		delay
		fib=$((fib + 1))
	done
}

testtx_icmp6_connected_transfernets()
{

	testtx_ulp6_connected_transfernets \
	    "testtx_icmp6_connected_transfernets" "TCP6"
}

testtx_tcp6_connected_transfernets()
{

	testtx_ulp6_connected_transfernets \
	    "testtx_tcp6_connected_transfernets" "TCP6"
}

testtx_udp6_connected_transfernets()
{

	testtx_ulp6_connected_transfernets \
	    "testtx_udp6_connected_transfernets" "UDP6"
}

testtx_icmp6_connected_ifconfig_transfernets()
{

	testtx_ulp6_connected_transfernets \
	    "testtx_icmp6_connected_ifconfig_transfernets" "TCP6"
}

testtx_tcp6_connected_ifconfig_transfernets()
{

	testtx_ulp6_connected_transfernets \
	    "testtx_tcp6_connected_ifconfig_transfernets" "TCP6"
}

testtx_udp6_connected_ifconfig_transfernets()
{

	testtx_ulp6_connected_transfernets \
	    "testtx_udp6_connected_ifconfig_transfernets" "UDP6"
}

################################################################################
#
testtx_ulp6_gateway()
{
	local _opts _n _o
	_n="$1"
	_o="$2"

	ifconfig lo0 inet6 2001:2:ff01::2 -alias > /dev/null 2>&1 || true
	delay
	ifconfig lo0 inet6 2001:2:ff01::2 alias

	_opts=""
	case ${DEBUG} in
	''|0)	;;
	42)	_opts="-d -d" ;;
	*)	_opts="-d" ;;
	esac

	print_debug "./reflect -p ${CTRLPORT} -T ${_o} " \
	    "-t ${_n} ${_opts} -A 2001:2:ff01::2"
	./reflect -p ${CTRLPORT} -T ${_o} \
	    -t ${_n} ${_opts} -A 2001:2:ff01::2
	print_debug "reflect terminated without error."

	ifconfig lo0 inet6 2001:2:ff01::2 -alias
	delay
}

testtx_icmp6_gateway()
{

	testtx_ulp6_gateway "testtx_icmp6_gateway" "TCP6"
}

testtx_tcp6_gateway()
{

	testtx_ulp6_gateway "testtx_tcp6_gateway" "TCP6"
}

testtx_udp6_gateway()
{

	testtx_ulp6_gateway "testtx_udp6_gateway" "UDP6"
}

################################################################################
#
testtx_ulp6_transfernets_gateways()
{
	local _opts fib _n _o
	_n="$1"
	_o="$2"

	_opts=""
	case ${DEBUG} in
	''|0)	;;
	42)	_opts="-d -d" ;;
	*)	_opts="-d" ;;
	esac

	# Setup transfer networks.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		setfib -F${fib} \
		    ifconfig ${IFACE} inet6 2001:2:${fib}::2/64 alias
		fib=$((fib + 1))
	done

	# Setup out listener IP.
	ifconfig lo0 inet6 2001:2:ff01::2 -alias > /dev/null 2>&1 || true
	delay
	ifconfig lo0 inet6 2001:2:ff01::2 alias

	# Reflect requests.
	print_debug "./reflect -p ${CTRLPORT} -T ${_o} " \
	    "-t ${_n} ${_opts} -A 2001:2:ff01::2"
	./reflect -p ${CTRLPORT} -T ${_o} \
	    -t ${_n} ${_opts} -A 2001:2:ff01::2
	print_debug "reflect terminated without error."

	# Cleanup transfer networks and listener IP.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		setfib -F${fib} \
		    ifconfig ${IFACE} inet6 2001:2:${fib}::2/64 -alias
		delay
		fib=$((fib + 1))
	done
	ifconfig lo0 inet6 2001:2:ff01::2 -alias
}

testtx_icmp6_transfernets_gateways()
{

	testtx_ulp6_transfernets_gateways \
	    "testtx_icmp6_transfernets_gateways" "TCP6"
}

testtx_tcp6_transfernets_gateways()
{

	testtx_ulp6_transfernets_gateways \
	    "testtx_tcp6_transfernets_gateways" "TCP6"
}

testtx_udp6_transfernets_gateways()
{

	testtx_ulp6_transfernets_gateways \
	    "testtx_udp6_transfernets_gateways" "UDP6"
}


################################################################################
#
testtx_ulp6_transfernets_gateway()
{
	local _opts fib _n _o
	_n="$1"
	_o="$2"

	_opts=""
	case ${DEBUG} in
	''|0)	;;
	42)	_opts="-d -d" ;;
	*)	_opts="-d" ;;
	esac

	# Setup transfer networks.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		setfib -F${fib} \
		    ifconfig ${IFACE} inet6 2001:2:${fib}::2/64 alias
		fib=$((fib + 1))
	done

	# Setup out listener IP.
	ifconfig lo0 inet6 2001:2:ff01::2 -alias > /dev/null 2>&1 || true
	delay
	ifconfig lo0 inet6 2001:2:ff01::2 alias

	# Reflect requests.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		print_debug "./reflect -p ${CTRLPORT} -T ${_o} " \
		    "-t ${_n}${fib} ${_opts} -A 2001:2:ff01::2"
		./reflect -p ${CTRLPORT} -T ${_o} \
		    -t ${_n}${fib} ${_opts} -A 2001:2:ff01::2
		print_debug "reflect terminated without error."
		fib=$((fib + 1))
	done

	# Cleanup transfer networks and listener IP.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		setfib -F${fib} \
		    ifconfig ${IFACE} inet6 2001:2:${fib}::2/64 -alias
		delay
		fib=$((fib + 1))
	done
	ifconfig lo0 inet6 2001:2:ff01::2 -alias
}

testtx_icmp6_transfernets_gateway()
{

	testtx_ulp6_transfernets_gateway \
	    "testtx_icmp6_transfernets_gateway" "TCP6"
}

testtx_tcp6_transfernets_gateway()
{

	testtx_ulp6_transfernets_gateway \
	    "testtx_tcp6_transfernets_gateway" "TCP6"
}

testtx_udp6_transfernets_gateway()
{

	testtx_ulp6_transfernets_gateway \
	    "testtx_udp6_transfernets_gateway" "UDP6"
}

################################################################################
#
# We are receiver, but the FIBs are with us this time.
#
#

#       # For IPFW, IFCONFIG
#       #   For each FIB
#       #     Send OOB well known to work START, wait for reflect
#       #     Send probe, wait for reply with FIB# or RST/ICMP6 unreach
#       #       (in case of ICMP6 use magic like ipfw count and OOB reply)
#       #     Send OOB well known to work DONE, wait for reflect   
#       #     Compare real with expected results.
#

textrx_ipfw_setup()
{
	local _fib _transfer i _p _o
	_fib=$1
	_transfer=$2

	# ICMP6 would need content inspection to distinguish FIB, we can
	# only differentiate by address.
	# For the default single-address cases always set to current FIB.
	ipfw add 100 setfib ${_fib} ipv6-icmp \
	    from ${PEERADDR} to ${OURADDR} \
	    via ${IFACE} in > /dev/null 2>&1
	ipfw add 100 setfib ${_fib} ipv6-icmp \
	    from ${PEERLINKLOCAL%\%*} to ${OURLINKLOCAL%\%*} \
	    via ${IFACE} in > /dev/null 2>&1

	# Always also do a setfib for the control port so that OOB
	# signaling workes even if we remove connected subnets.
	ipfw add 200 setfib ${_fib} ip6 from ${PEERADDR} to ${OURADDR} \
	    dst-port ${CTRLPORT} via ${IFACE} in > /dev/null 2>&1

	# Save addresses
	_p="${PEERADDR}"
	_o="${OURADDR}"

	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do

		# If doing multiple transfer networks, replace PEERADDR.
		case ${_transfer} in
		1)	PEERADDR=2001:2:${i}::1
			OURADDR=2001:2:${i}::2
			;;
		esac

		if test ${_instances} -gt 1 -o ${_transfer} -eq 1; then
			ipfw add 400 setfib ${_fib} ipv6-icmp \
			    from ${PEERADDR} to ${OURADDR} \
			    icmp6types 128 \
			    via ${IFACE} in > /dev/null 2>&1
		fi
		
		case ${i} in
		${_fib})
			ipfw add 400 setfib ${_fib} ip6 \
			    from ${PEERADDR} to ${OURADDR} \
			    dst-port $((CTRLPORT + 1000 + i)) \
			    via ${IFACE} in > /dev/null 2>&1
			ipfw add 400 setfib ${_fib} ip6 \
			    from ${PEERLINKLOCAL%\%*} to ${OURLINKLOCAL%\%*} \
			    dst-port $((CTRLPORT + 1000 + i)) \
			    via ${IFACE} in > /dev/null 2>&1
			if test ${_instances} -le 1 -o ${_transfer} -ne 1; then
				ipfw add 400 setfib ${_fib} ipv6-icmp \
				    from ${PEERADDR} to ${OURADDR} \
				    icmp6types 128 \
				    via ${IFACE} in > /dev/null 2>&1
			fi
			;;
		esac

		i=$((i + 1))
	done

	# Restore addresses.
	PEERADDR="${_p}"
	OURADDR="${_o}"

	case ${DEBUG} in
	''|0)	;;
	*)	ipfw show ;;
	esac
}

textrx_ifconfig_setup()
{
	local _fib
	_fib=$1

	ifconfig ${IFACE} fib ${_fib} > /dev/null 2>&1
}

textrx_ipfw_cleanup()
{
	local i

	case ${DEBUG} in
	''|0)	;;
	*)	ipfw show ;;
	esac

	ipfw delete 100 > /dev/null 2>&1 || true
	ipfw delete 200 > /dev/null 2>&1 || true
	ipfw delete 400 > /dev/null 2>&1 || true

	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do

		ipfw delete $((1000 + i)) > /dev/null 2>&1 || true
		i=$((i + 1))
	done
}

textrx_ifconfig_cleanup()
{

	ifconfig ${IFACE} fib 0 > /dev/null 2>&1
}

textrx_count_setup()
{
	local i

	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do

		# Count ICMP6 echo replies.
		ipfw add $((500 + i)) count ipv6-icmp from any to any \
		    icmp6types 129 fib ${i} via ${IFACE} out > /dev/null 2>&1
		ipfw add $((500 + i)) count tcp from any to any \
		    fib ${i} via ${IFACE} out > /dev/null 2>&1
		ipfw add $((500 + i)) count udp from any to any \
		    fib ${i} via ${IFACE} out > /dev/null 2>&1
		i=$((i + 1))
	done
}

textrx_count_results()
{
	local _fib _o i _rstr _c _req _p _opts
	_fib=$1
	_o="$2"

	case ${DEBUG} in
	''|0)	;;
	*)	ipfw show ;;
	esac

	_rstr=""
	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do

		case "${_o}" in
		"-i")	_c=`ipfw show $((500 + i)) | awk '/ ipv6-icmp / { print $2 }'` ;;
		"-u")	_c=`ipfw show $((500 + i)) | awk '/ udp / { print $2 }'` ;;
		*)	_c=`ipfw show $((500 + i)) | awk '/ tcp / { print $2 }'` ;;
		esac
		_rstr="${_rstr}${i} ${_c},"

		ipfw delete $((500 + i)) > /dev/null 2>&1 || true
		i=$((i + 1))
	done

	# We do not care about the request.
	_req=`echo "RESULT ${_rstr}" | nc -V ${_fib} -6 -l ${CTRLPORT}`
	print_debug "$? -- ${_req} -- RESULT ${_rstr}"
}

testrx_remove_connected()
{
	local _fib _transfer i j _prefix
	_fib=$1
	_transfer=$2

	if test ${_transfer} -eq 1; then
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			j=0
			while test ${j} -lt ${RT_NUMFIBS}; do
				_prefix="2001:2:${j}::"

				case ${j} in
				${_fib});;
				*)	print_debug "setfib -F${i} route delete" \
					    "-inet6 -net ${_prefix}"
					setfib -F${i} route delete -inet6 -net \
					    ${_prefix} > /dev/null 2>&1
					;;
				esac
				j=$((j + 1))
			done
			i=$((i + 1))
		done

	else
		_prefix=${OURADDR%2}	# Luckily we know the details.
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do

			case ${i} in
			${_fib});;
			*)	print_debug "setfib -F${i} route delete" \
				    "-inet6 -net ${_prefix}"
				setfib -F${i} route delete -inet6 -net \
				    ${_prefix} > /dev/null 2>&1
				;;
			esac

			i=$((i + 1))
		done
	fi
}

testrx_cleanup_connected()
{
	local _fib _transfer i _prefix
	_fib=$1
	_transfer=$2

	if test ${_transfer} -eq 1; then

		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			setfib -F${i} \
			   ifconfig ${IFACE} inet6 2001:2:${i}::2/64 -alias \
			    > /dev/null 2>&1
			delay
			i=$((i + 1))
		done

	else
		# Use the hammer removing the address and adding it again to get
		# the connected subnet back to all FIBs.  Hard to do otherwise.
		ifconfig ${IFACE} inet6 ${OURADDR}/64 -alias || true
		delay
		ifconfig ${IFACE} inet6 ${OURADDR}/64 alias up
	fi
}

testrx_setup_transfer_networks()
{
	local i

	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do
		ifconfig ${IFACE} inet6 2001:2:${i}::2/64 -alias \
		    > /dev/null 2>&1 || true
		delay
		ifconfig ${IFACE} inet6 2001:2:${i}::2/64 alias
		i=$((i + 1))
	done
}

testrx_run_one()
{
	local _fib _txt _opts
	_fib=$1
	_txt="$2"
	_opts="$3"

	case ${USE_SOSETFIB} in
	0)	print_debug "setfib -F${_fib} ./reflect -p ${CTRLPORT}" \
		    "-t ${_txt} ${_opts}"
		setfib -F${_fib} ./reflect -p ${CTRLPORT} -t ${_txt} ${_opts}
		;;
	1)	print_debug "./reflect -F${_fib} -p ${CTRLPORT} -t ${_txt}" \
		    "${_opts}"
		./reflect -F${_fib} -p ${CTRLPORT} -t ${_txt} ${_opts}
		;;
	*)	die "Invalid value for USE_SOSETFIB: ${USE_SOSETFIB}" ;;
	esac
	print_debug "reflect '${_txt}' terminated without error."
}

testrx_run_multiple()
{
	local _fib _txt _opts i _jobs _p _w
	_fib=$1
	_txt="$2"
	_opts="$3"

	i=0
	_jobs=""
	while test ${i} -lt ${RT_NUMFIBS}; do
		case ${USE_SOSETFIB} in
		0)	print_debug "setfib -F${i} ./reflect" \
			    "-p $((CTRLPORT + 1000 + i))" \
			    "-t ${_txt} ${_opts} -N -f ${i} &"
			setfib -F${i} ./reflect -p $((CTRLPORT + 1000 + i)) \
			    -t ${_txt} ${_opts} -N -f ${i} &
			;;
		1)	print_debug "./reflect -F ${i}" \
			    "-p $((CTRLPORT + 1000 + i))" \
			    "-t ${_txt} ${_opts} -N -f ${i} &"
			./reflect -F ${i} -p $((CTRLPORT + 1000 + i)) \
			    -t ${_txt} ${_opts} -N -f ${i} &
			;;
		*)	die "Invalid value for USE_SOSETFIB: ${USE_SOSETFIB}" ;;
		esac
		_p=$!
		_jobs="${_jobs}${_p} "
		case ${i} in
		${_fib}) _w=${_p} ;;
		esac
		i=$((i + 1))
	done

	# Start OOB control connection for START/DONE.
	testrx_run_one ${_fib} "${_txt}" "${_opts}"
	print_debug "KILL ${_jobs}"
	for i in ${_jobs}; do
		kill ${i} || true
	done
	#killall reflect || true
	print_debug "reflects for '${_txt}' terminated without error."
}

testrx_run_test()
{
	local _n _t _fib _o _txt i _f _instance _destructive _transfer
	_n="$1"
	_t="$2"
	_fib=$3
	_o="$4"
	_instances=$5
	_destructive=$6
	_transfer=$7

	: ${_destructive:=0}

	_opts=""
	case ${DEBUG} in
	''|0)	;;
	42)	_opts="-d -d" ;;
	*)	_opts="-d" ;;
	esac

	# Convert netcat options to reflect aguments.
	case "${_o}" in
	-i)	_opts="${_opts} -T TCP6" ;;	# Use TCP for START/DONE.
	-u)	_opts="${_opts} -T UDP6" ;;
	*)	_opts="${_opts} -T TCP6" ;;
	esac

	# Combined test case base name.
	case ${USE_SOSETFIB} in
	0)	_f="setfib" ;;
	1)	_f="so_setfib" ;;
	*)	die "Unexpected value for SO_SETFIB: ${SO_SETFIB}" ;;
	esac

        _txt="${_n}_${_f}_${_t}_${_fib}_${_instances}_${_destructive}_${_transfer}"

	case ${_transfer} in
	1)	testrx_setup_transfer_networks ;;
	esac

	case "${_t}" in
	ipfw)		textrx_ipfw_setup ${_fib} ${_transfer} ${_instances} ;;
	ifconfig)	textrx_ifconfig_setup ${_fib} ;;
	*)		die "Invalid type in ${_txt}" ;;
	esac

	# Setup unresponsive FIBs.
	case ${_destructive} in
	1)	testrx_remove_connected ${_fib} ${_transfer} ;;
	esac

	# Setup to get result counts.
	textrx_count_setup

	# Run just one / one per FIB (with incremental ports).
	#case ${_instances} in
	#1)	testrx_run_one ${_fib} "${_txt}" "${_opts}" ;;
	#*)	testrx_run_multiple ${_fib} "${_txt}" "${_opts}" ;;
	#esac
	testrx_run_multiple ${_fib} "${_txt}" "${_opts}" ${_transfer}

	# Export result counts.
	textrx_count_results ${_fib} "${_o}"

	# Cleanup unresponsive  FIBs or multiple prefixes.
	if test ${_destructive} -eq 1 -o ${_transfer} -eq 1; then
		testrx_cleanup_connected ${_fib} ${_transfer}
	fi

	case "${_t}" in
	ipfw)		textrx_ipfw_cleanup ;;
	ifconfig)	textrx_ifconfig_cleanup ;;
	*)		die "Invalid type in ${_txt}" ;;
	esac
}

testrx_main()
{
	local _n _o s t fib _instances _destructive
	_n="$1"
	_o="$2"
	_instances=$3

	: ${_instances:=1}

	print_debug "${_n}"
	for _transfer in 1 0; do
		for _destructive in 0 1; do
			for t in ipfw ifconfig; do

				print_debug "${_n}_${t}"
				fib=0
				while test ${fib} -lt ${RT_NUMFIBS}; do

					print_debug "${_n}_${t}_${fib}" \
					    "${_instances} ${_destructive}" \
					    "${_transfer}"
					testrx_run_test "${_n}" "${t}" ${fib} \
					   "${_o}" ${_instances} \
					   ${_destructive} ${_transfer}

					fib=$((fib + 1))
				done
			done
		done
	done
}

################################################################################
#
# Probe all FIBs with one "active" one a time.
#
testrx_icmp6_same_addr_one_fib_a_time()
{

	testrx_main "testrx_icmp6_same_addr_one_fib_a_time" "-i"
}

testrx_tcp6_same_addr_one_fib_a_time()
{

	testrx_main "testrx_tcp6_same_addr_one_fib_a_time" ""
}

testrx_udp6_same_addr_one_fib_a_time()
{

	testrx_main "testrx_udp6_same_addr_one_fib_a_time" "-u"
}

################################################################################
#
# Probe all FIBs with all "active" all time.
#
testrx_tcp6_same_addr_all_fibs_a_time()
{

	testrx_main "testrx_tcp6_same_addr_all_fibs_a_time" "" ${RT_NUMFIBS}
}

testrx_udp6_same_addr_all_fibs_a_time()
{

	testrx_main "testrx_udp6_same_addr_all_fibs_a_time" "-u" ${RT_NUMFIBS}
}


################################################################################
#
# Prereqs.
#
if test `sysctl -n security.jail.jailed` -eq 0; then
	kldload ipfw > /dev/null 2>&1 || kldstat -v | grep -q ipfw 
fi
ipfw -f flush > /dev/null 2>&1 || die "please load ipfw in base system"
ipfw add 65000 permit ip from any to any > /dev/null 2>&1
killall reflect || true

################################################################################
#
# Run tests.
#
wait_remote_ready

# We are receiver reflecting the input back.
for uso in 0 1; do

	# Only run ICMP6 tests for the first loop.
	test ${uso} -ne 0 || testtx_icmp6_connected
	testtx_tcp6_connected
	testtx_udp6_connected

	test ${uso} -ne 0 || testtx_icmp6_connected_blackhole
	testtx_tcp6_connected_blackhole
	testtx_udp6_connected_blackhole

	test ${uso} -ne 0 || testtx_icmp6_connected_transfernets
	testtx_tcp6_connected_transfernets
	testtx_udp6_connected_transfernets

	test ${uso} -ne 0 || testtx_icmp6_connected_ifconfig_transfernets
	testtx_tcp6_connected_ifconfig_transfernets
	testtx_udp6_connected_ifconfig_transfernets

	test ${uso} -ne 0 || testtx_icmp6_gateway
	testtx_tcp6_gateway
	testtx_udp6_gateway

	test ${uso} -ne 0 || testtx_icmp6_transfernets_gateways
	testtx_tcp6_transfernets_gateways
	testtx_udp6_transfernets_gateways

	test ${uso} -ne 0 || testtx_icmp6_transfernets_gateway
	testtx_tcp6_transfernets_gateway
	testtx_udp6_transfernets_gateway
done

ipfw -f flush > /dev/null 2>&1
ipfw add 65000 permit ip from any to any > /dev/null 2>&1

# We are receiver, but the FIBs are with us this time.
for uso in 0 1; do

	USE_SOSETFIB=${uso}
	
	# Only expect ICMP6 tests for the first loop.
	test ${uso} -ne 0 || testrx_icmp6_same_addr_one_fib_a_time
	testrx_tcp6_same_addr_one_fib_a_time
	testrx_udp6_same_addr_one_fib_a_time

	testrx_tcp6_same_addr_all_fibs_a_time
	testrx_udp6_same_addr_all_fibs_a_time

done

cleanup

# end
OpenPOWER on IntegriCloud