Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
#!/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}

# Number of seconds to wait for peer node to synchronize for test.
: ${WAITS:=120}

# 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`

OURADDR="2001:2:ff00::1"
PEERADDR="2001:2:ff00::2"
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.
#
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} ${_exp}"
}

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.
#

# Make sure the local link-local and global addresses are reachable
# from all FIBs.
check_local_addr()
{
	local _l i testno

	print_debug "Setting up interface ${IFACE}"
	ifconfig ${IFACE} inet6 ${OURADDR}/64 alias up
	_l=`ifconfig ${IFACE} | awk '/inet6 fe80:/ { print $2 }'`

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

	printf "1..%d\n" `expr 2 \* ${RT_NUMFIBS}`
	testno=1
	i=0
	set +e
	while test ${i} -lt ${RT_NUMFIBS}; do
		print_debug "Testing FIB ${i}"

		setfib -F${i} ping6 -n -c1 ${_l} > /dev/null 2>&1
		check_rc $? 0 ${testno} "check_local_addr_${i}_l" \
		    "FIB ${i} ${_l}"
		testno=$((testno + 1))

		setfib -F${i} ping6 -n -c1 ${OURADDR} > /dev/null 2>&1
		check_rc $? 0 ${testno} "check_local_addr_${i}_a" \
		    "FIB ${i} ${OURADDR}"
		testno=$((testno + 1))

		i=$((i + 1))
	done
	set -e
	ifconfig ${IFACE} inet6 ${OURADDR}/64 -alias
}


# Cloned tun(4) devices behave differently on FIB 0 vs. FIB 1..n after creation
# (they also do in IPv4).
check_local_tun()
{
	local _l i testno IFACE _msg

	IFACE=tun42
	print_debug "Setting up interface ${IFACE}"
	ifconfig ${IFACE} create
	ifconfig ${IFACE} inet6 ${OURADDR}/64 alias up
	_l=`ifconfig ${IFACE} | awk '/inet6 fe80:/ { print $2 }'`

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

	printf "1..%d\n" `expr 2 \* ${RT_NUMFIBS}`
	testno=1
	_msg=""
	i=0
	set +e
	while test ${i} -lt ${RT_NUMFIBS}; do
		print_debug "Testing FIB ${i}"
		if test ${i} -gt 0; then
			# Flag the well known behaviour as such.
			_msg="TODO "
		fi

		setfib -F${i} ping6 -n -c1 ${_l} > /dev/null 2>&1
		check_rc $? 0 ${testno} "check_local_addr_${i}_l" \
		    "${_msg}FIB ${i} ${_l}"
		testno=$((testno + 1))

		setfib -F${i} ping6 -n -c1 ${OURADDR} > /dev/null 2>&1
		check_rc $? 0 ${testno} "check_local_addr_${i}_a" \
		    "${_msg}FIB ${i} ${OURADDR}"
		testno=$((testno + 1))

		i=$((i + 1))
	done
	set -e
	ifconfig ${IFACE} inet6 ${OURADDR}/64 -alias
	ifconfig ${IFACE} destroy
}

check_remote_up()
{
	local _l i testno

	print_debug "Setting up interface ${IFACE}"
	ifconfig ${IFACE} inet6 ${OURADDR}/64 alias up
	_l=`ifconfig ${IFACE} | awk '/inet6 fe80:/ { print $2 }'`

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



}

send_greeting()
{
        local _l _greeting _keyword _fib _fibs _linklocal

	print_debug "Setting up interface ${IFACE}"
	ifconfig ${IFACE} inet6 ${OURADDR}/64 alias up
	_l=`ifconfig ${IFACE} | awk '/inet6 fe80:/ { print $2 }'`

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

	# Cleanup firewall and install rules to always allow NS/NA to succeed.
	# The latter is needed to allow indvidiual less specific later rules
	# from test cases to just disallow any IPv6 traffic on a matching FIB.
	ipfw -f flush > /dev/null 2>&1
	ipfw add 65000 permit ip from any to any > /dev/null 2>&1
	ipfw add 5 permit ipv6-icmp from any to any icmp6types 135,136 fib 0 \
	    via ${IFACE} out > /dev/null 2>&1

	set +e
	i=0
	rc=-1
	while test ${i} -lt ${WAITS} -a ${rc} -ne 0; do
		print_debug "Sending greeting #${i} to peer"
		_greeting=`echo "SETUP ${RT_NUMFIBS} ${_l}" | \
		    nc -6 -w 1 ${PEERADDR} ${CTRLPORT}`
		rc=$?
		i=$((i + 1))
		# Might sleep longer in total but better than to DoS
		# and not get anywhere.
		sleep 1
	done
	set -e

	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}

	# Swap the zoneid to the local interface scope.
	PEERLINKLOCAL=${PEERLINKLOCAL%%\%*}"%${IFACE}"

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

cleanup()
{

	# Cleanup ipfw.
	ipfw delete 5 > /dev/null 2>&1

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

testtx_icmp6()
{
	local _n _transfer i testno _txt _fibtxt _rc _ec _p
	_n="$1"
	_transfer=$2

	printf "1..%d\n" `expr 2 \* ${RT_NUMFIBS}`
	testno=1
	set +e
	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do
		_txt="${_n}${i}"
		print_debug "Testing ${_txt}"
		_fibtxt=`echo "${_txt}" | hd -v | cut -b11-60 | tr -d ' \r\n'`

		eval _rc="\${rc_${i}_l}"
		setfib -F${i} ping6 -n -c1 -p ${_fibtxt} \
		    ${PEERLINKLOCAL} > /dev/null 2>&1
		_ec=$?
		# We need to normalize the exit code of ping6.
		case ${_ec} in
		0)	;;
		*)	_ec=1 ;;
		esac
		check_rc ${_ec} ${_rc} ${testno} "${_txt}_l" \
		    "FIB ${i} ${PEERLINKLOCAL}"
		testno=$((testno + 1))

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

		eval _rc="\${rc_${i}_a}"
		setfib -F${i} ping6 -n -c1 -p ${_fibtxt} ${PEERADDR} \
		    > /dev/null 2>&1
		_ec=$?
		# We need to normalize the exit code of ping6.
		case ${_ec} in
		0)	;;
		*)	_ec=1 ;;
		esac
		check_rc ${_ec} ${_rc} ${testno} "${_txt}_a" \
		    "FIB ${i} ${PEERADDR}"
		testno=$((testno + 1))

		# Restore PEERADDR.
		PEERADDR="${_p}"

		i=$((i + 1))
	done
	set -e
}

nc_send_recv()
{
	local _fib _loops _msg _expreply _addr _port _opts i
	_fib=$1
	_loops=$2
	_msg="$3"
	_expreply="$4"
	_addr=$5
	_port=$6
	_opts="$7"

	i=0
	while test ${i} -lt ${_loops}; do
		i=$((i + 1))
		case "${USE_SOSETFIB}" in
		1)
			_reply=`echo "${_msg}" | \
			    nc -V ${_fib} ${_opts} ${_addr} ${_port}`
			;;
		*)
			_reply=`echo "${_msg}" | \
			    setfib -F${_fib} nc ${_opts} ${_addr} ${_port}`
			;;
		esac
		if test "${_reply}" != "${_expreply}"; then
			if test ${i} -lt ${_loops}; then
				sleep 1
			else
			# Must let caller decide how to handle the error.
			#	die "Got invalid reply from peer." \
			#	    "Expected '${_expreply}', got '${_reply}'"
				return 1
			fi
		else
			break
		fi
	done
	return 0
}

testtx_tcp_udp()
{
	local _n _o _f testno i _fibtxt
	_n="$1"
	_o="$2"
	_f="$3"

	printf "1..%d\n" `expr 2 \* ${RT_NUMFIBS}`
	testno=1
	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do
		print_debug "Testing ${_f} ${i}"

		eval _rc="\${rc_${i}_l}"
		_fibtxt="${_n}_${i}_l ${_f} ${i} ${PEERLINKLOCAL}"
		nc_send_recv ${i} 1 "${_fibtxt}" "${_fibtxt}" ${PEERLINKLOCAL} \
		    ${CTRLPORT} "-6 ${_o} -w1"
		check_rc $? ${_rc} ${testno} "${_fibtxt}"
		testno=$((testno + 1))

		eval _rc="\${rc_${i}_a}"
		_fibtxt="${_n}_${i}_a ${_f} ${i} ${PEERADDR}"
		nc_send_recv ${i} 1 "${_fibtxt}" "${_fibtxt}" ${PEERADDR} \
		    ${CTRLPORT} "-6 ${_o} -w1"
		check_rc $? ${_rc} ${testno} "${_fibtxt}"
		testno=$((testno + 1))

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

# setfib TCP|UDP/IPv6 test on link-local and global address of peer from all FIBs.
testtx_ulp6_connected()
{
	local _fibtxt _reply _n _o _rc _fib _f _opts
	_n=$1
	_o="$2"
	_fib=$3

	case "${USE_SOSETFIB}" in
	1) _f="SO_SETFIB" ;;
	*) _f="SETFIB" ;;
	esac

	if test "${_o}" = "-i" -a "${_f}" = "SO_SETFIB"; then
		print_debug "Skipping icmp6 tests for SO_SETFIB."
		return 0
	fi

	# Clear the neighbor table to get deterministic runs.
	ndp -cn > /dev/null 2>&1

	case "${_o}" in
	-i)	_opts="" ;;		# Use TCP for START/DONE.
	*)	_opts="${_o}" ;;
	esac

	set +e
	# Let peer know that we are about to start.
	_msg="START ${_n}"
	nc_send_recv ${_fib} ${WAITS} "${_msg}" "${_msg}" ${PEERADDR} \
	    ${CTRLPORT} "-6 ${_opts} -w1"
	case $? in
	0)	;;
	*)	die "Got invalid reply from peer." \
		    "Expected '${_msg}', got '${_reply}'" ;;
	esac

	case "${_o}" in
	-i)	testtx_icmp6 "${_n}" ;;
	*)	testtx_tcp_udp "${_n}" "${_o}" "${_f}" ;;
	esac

	# Let peer know that we are done with this test to move to next.
	# This must immediately succeed.
	_msg="DONE ${_n}"
	nc_send_recv ${_fib} ${WAITS} "${_msg}" "${_msg}" ${PEERADDR} \
	    ${CTRLPORT} "-6 ${_opts} -w1"
	case $? in
	0)	;;
	*)	die "Got invalid reply from peer." \
		    "Expected '${_msg}', got '${_reply}'" ;;
	esac
	set -e

	print_debug "Successfully received status update '${_reply}'."
}

################################################################################
#
# ping6|TCP/UDP connect link-local and global address of peer from all FIBs.
# Default reachability test.
#
testtx_icmp6_connected()
{
	local i

	# Setup expected return values.
	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do
		eval rc_${i}_l=0
		eval rc_${i}_a=0
		i=$((i + 1))
	done

	testtx_ulp6_connected "testtx_icmp6_connected" "-i" 0
}

testtx_tcp6_connected()
{
	local i

	# Setup expected return values.
	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do
		eval rc_${i}_l=0
		eval rc_${i}_a=0
		i=$((i + 1))
	done

	testtx_ulp6_connected testtx_tcp6_connected "" 0
}

testtx_udp6_connected()
{
	local i

	# Setup expected return values.
	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do
		eval rc_${i}_l=0
		eval rc_${i}_a=0
		i=$((i + 1))
	done

	testtx_ulp6_connected testtx_udp6_connected "-u" 0
}

################################################################################
#
# Use ipfw to return unreach messages for all but one FIB.  Rotate over all.
# Making sure error messages are properly returned.
#
testtx_ulp6_connected_blackhole()
{
	local fib i _n _o
	_n="$1"
	_o="$2"

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

		print_debug "${_n} ${fib}"

		# Setup expected return values.
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			ipfw delete $((100 + i)) > /dev/null 2>&1 || true
			case ${i} in
			${fib})
				eval rc_${i}_l=0
				eval rc_${i}_a=0
				;;
			*)	eval rc_${i}_l=1
				eval rc_${i}_a=1
				ipfw add $((100 + i)) unreach6 admin-prohib \
				    ip6 from any to any fib ${i} via ${IFACE} \
				    out > /dev/null 2>&1
				;;
			esac
			i=$((i + 1))
		done

		testtx_ulp6_connected "${_n}${fib}" "${_o}" ${fib}
		case ${DEBUG} in
		''|0)	;;
		*)	ipfw show ;;
		esac
		fib=$((fib + 1))
	done
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		ipfw delete $((100 + fib)) > /dev/null 2>&1 || true
		fib=$((fib + 1))
	done
}

testtx_icmp6_connected_blackhole()
{

	testtx_ulp6_connected_blackhole \
	    "testtx_icmp6_connected_blackhole" "-i"
}

testtx_tcp6_connected_blackhole()
{

	testtx_ulp6_connected_blackhole \
	    "testtx_tcp6_connected_blackhole" ""
}

testtx_udp6_connected_blackhole()
{

	testtx_ulp6_connected_blackhole \
	    "testtx_udp6_connected_blackhole" "-u"
}

################################################################################
#
# Setup a different transfer net on each FIB.  Delete all but one connected
# route in all FIBs (e.g. FIB 0 uses prefix 0, FIB 1 uses prefix 1 , ...).
#
# Need to tag NS/NA incoming to the right FIB given the default FIB does not
# know about the prefix and thus cannot do proper source address lookups for
# replying otherwise.   Use ipfw.
#
testtx_ulp6_connected_transfernets()
{
	local fib i _n _o _p
	_n="$1"
	_o="$2"

	# Setup transfer networks and firewall.
	ipfw delete 10 > /dev/null 2>&1 || true
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		ifconfig ${IFACE} inet6 2001:2:${fib}::1/64 -alias \
		    > /dev/null 2>&1 || true
		ifconfig ${IFACE} inet6 2001:2:${fib}::1/64 alias
		ipfw add 10 setfib ${fib} ipv6-icmp from 2001:2:${fib}::/64 \
		    to any ip6 icmp6types 135,136 via ${IFACE} in \
		    > /dev/null 2>&1
		# Remove connected routes from all but matching FIB.
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			case ${i} in
			${fib});;
			*)	setfib -F${i} route delete -inet6 \
				    -net 2001:2:${fib}:: > /dev/null 2>&1
				;;
			esac
			i=$((i + 1))
		done
		fib=$((fib + 1))
	done

	# Save PEERADDR
	_p=${PEERADDR}

	# Run tests.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		PEERADDR=2001:2:${fib}::2

		print_debug "${_n} ${fib}"

		# Setup expected return values.
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			eval rc_${i}_l=0
			case ${i} in
			${fib})
				eval rc_${i}_a=0
				;;
			*)	eval rc_${i}_a=1
				;;
			esac
			i=$((i + 1))
		done

		testtx_ulp6_connected "${_n}${fib}" "${_o}" ${fib}
		fib=$((fib + 1))
	done

	# Restore PEERADDR
	PEERADDR=${_p}

	# Cleanup transfer networks and firewall.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		ifconfig ${IFACE} inet6 2001:2:${fib}::1/64 -alias
		fib=$((fib + 1))
	done
	ipfw delete 10 > /dev/null 2>&1
}

testtx_icmp6_connected_transfernets()
{

	testtx_ulp6_connected_transfernets \
	    "testtx_icmp6_connected_transfernets" "-i"
}

testtx_tcp6_connected_transfernets()
{

	testtx_ulp6_connected_transfernets \
	    "testtx_tcp6_connected_transfernets" ""
}

testtx_udp6_connected_transfernets()
{

	testtx_ulp6_connected_transfernets \
	    "testtx_udp6_connected_transfernets" "-u"
}

################################################################################
#
# Setup a different transfernet on each FIB.  Delete all but one connected
# route in all FIBs (e.g. FIB 0 uses prefix 0, FIB 1 uses prefix 1 , ...).
#
# Need to tag NS/NA incoming to the right FIB given the default FIB does not
# know about the prefix and thus cannot do proper source address lookups for
# replying otherwise.   Use ifconfig IFACE fib.
#
testtx_ulp6_connected_ifconfig_transfernets()
{
	local fib i _n _o _p
	_n="$1"
	_o="$2"

	# Setup transfer networks.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		ifconfig ${IFACE} inet6 2001:2:${fib}::1/64 -alias \
		    > /dev/null 2>&1 || true
		ifconfig ${IFACE} inet6 2001:2:${fib}::1/64 alias
		# Remove connected routes from all but matching FIB.
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			case ${i} in
			${fib});;
			*)	setfib -F${i} route delete -inet6 \
				    -net 2001:2:${fib}:: > /dev/null 2>&1
				;;
			esac
			i=$((i + 1))
		done
		fib=$((fib + 1))
	done

	# Save PEERADDR
	_p=${PEERADDR}

	# Run tests.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		PEERADDR=2001:2:${fib}::2

		print_debug "${_n} ${fib}"

		# Setup expected return values.
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			eval rc_${i}_l=0
			case ${i} in
			${fib})
				eval rc_${i}_a=0
				;;
			*)	eval rc_${i}_a=1
				;;
			esac
			i=$((i + 1))
		done

		ifconfig ${IFACE} fib ${fib}

		testtx_ulp6_connected "${_n}${fib}" "${_o}" ${fib}
		fib=$((fib + 1))
	done

	# Restore PEERADDR
	PEERADDR=${_p}

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

testtx_icmp6_connected_ifconfig_transfernets()
{

	testtx_ulp6_connected_ifconfig_transfernets \
	    "testtx_icmp6_connected_ifconfig_transfernets" "-i"
}


testtx_tcp6_connected_ifconfig_transfernets()
{

	testtx_ulp6_connected_ifconfig_transfernets \
	    "testtx_tcp6_connected_ifconfig_transfernets" ""
}

testtx_udp6_connected_ifconfig_transfernets()
{

	testtx_ulp6_connected_ifconfig_transfernets \
	    "testtx_udp6_connected_ifconfig_transfernets" "-u"
}

################################################################################
#
# Make destination reachable through the same default route in each FIB only.
# Run standard reachability test.
#
testtx_ulp6_gateway()
{
	local fib i _n _o _p
	_n="$1"
	_o="$2"

	# Setup default gateway and expected error codes.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		setfib -F${fib} route delete -inet6 -net default \
		    > /dev/null 2>&1 || true
		setfib -F${fib} route add -inet6 -net default ${PEERADDR} \
		    > /dev/null 2>&1
		case "${_o}" in
		-i) eval rc_${fib}_l=0 ;;	# ICMPv6 will succeed
		*)  eval rc_${fib}_l=1 ;;
		esac
		eval rc_${fib}_a=0
		fib=$((fib + 1))
	done

	# Save PEERADDR
	_p=${PEERADDR}
	PEERADDR="2001:2:ff01::2"

	# Run tests.
	print_debug "${_n}"
	testtx_ulp6_connected "${_n}" "${_o}" 0

	# Restore PEERADDR
	PEERADDR=${_p}

	# Cleanup transfer networks.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		setfib -F${fib} route delete -inet6 -net default \
		    > /dev/null 2>&1
		fib=$((fib + 1))
	done
}

testtx_icmp6_gateway()
{

	testtx_ulp6_gateway "testtx_icmp6_gateway" "-i"
}

testtx_tcp6_gateway()
{

	testtx_ulp6_gateway "testtx_tcp6_gateway" ""
}

testtx_udp6_gateway()
{

	testtx_ulp6_gateway "testtx_udp6_gateway" "-u"
}

################################################################################
#
# Make destination reachable through a different default route in each FIB.
# Generate a dedicated transfer network for that in each FIB.  Delete all but
# one connected route in all FIBs (e.g. FIB 0 uses prefix 0, ...).
#
# Have a default route present in each FIB all time.
#
# Need to tag NS/NA incoming to the right FIB given the default FIB does not
# know about the prefix and thus cannot do proper source address lookups for
# replying otherwise.   Use ipfw.
#
#
testtx_ulp6_transfernets_gateways()
{
	local fib i _n _o _p
	_n="$1"
	_o="$2"

	# Setup transfer networks, default routes, and firewall.
	fib=0
	ipfw delete 10 > /dev/null 2>&1 || true
	while test ${fib} -lt ${RT_NUMFIBS}; do
		ifconfig ${IFACE} inet6 2001:2:${fib}::1/64 -alias \
		    > /dev/null 2>&1 || true
		ifconfig ${IFACE} inet6 2001:2:${fib}::1/64 alias \
		    > /dev/null 2>&1
		ipfw add 10 setfib ${fib} ipv6-icmp \
		    from 2001:2:${fib}::/64 to any ip6 icmp6types 135,136 \
		    via ${IFACE} in > /dev/null 2>&1
		# Remove connected routes from all but matching FIB.
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			case ${i} in
			${fib});;
			*)	setfib -F${i} route delete -inet6 \
				    -net 2001:2:${fib}:: > /dev/null 2>&1
				;;
			esac
			i=$((i + 1))
		done
		# Add default route.
		setfib -F${fib} route delete -inet6 -net default \
		    > /dev/null 2>&1 || true
		setfib -F${fib} route add -inet6 -net default \
		    2001:2:${fib}::2 > /dev/null 2>&1
		fib=$((fib + 1))
	done

	# Save PEERADDR
	_p=${PEERADDR}
	PEERADDR="2001:2:ff01::2"

	# Setup expected return values.
	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do
		case "${_o}" in
		-i) eval rc_${i}_l=0 ;;	# ICMPv6 will succeed
		*)  eval rc_${i}_l=1 ;;
		esac
		eval rc_${i}_a=0
		i=$((i + 1))
	done

	# Run tests.
	print_debug "${_n}"
	testtx_ulp6_connected "${_n}" "${_o}" 0

	# Restore PEERADDR
	PEERADDR=${_p}

	# Cleanup default routes, transfer networks, and firewall.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		setfib -F${fib} route delete -inet6 -net default \
		    > /dev/null 2>&1
		ifconfig ${IFACE} inet6 2001:2:${fib}::1/64 -alias \
		    > /dev/null 2>&1
		fib=$((fib + 1))
	done
	ipfw delete 10 > /dev/null 2>&1
}

testtx_icmp6_transfernets_gateways()
{

	testtx_ulp6_transfernets_gateways \
	    "testtx_icmp6_transfernets_gateways" "-i"
}

testtx_tcp6_transfernets_gateways()
{

	testtx_ulp6_transfernets_gateways \
	    "testtx_tcp6_transfernets_gateways" ""
}

testtx_udp6_transfernets_gateways()
{

	testtx_ulp6_transfernets_gateways \
	    "testtx_udp6_transfernets_gateways" "-u"
}

################################################################################
#
# Make destination reachable through a different default route in each FIB.
# Generate a dedicated transfer network for that in each FIB.  Delete all but
# one connected route in all FIBs (e.g. FIB 0 uses prefix 0, ...).
#
# Only have a default route present in 1 FIB at a time.
#
# Need to tag NS/NA incoming to the right FIB given the default FIB does not
# know about the prefix and thus cannot do proper source address lookups for
# replying otherwise.   Use ipfw.
#
testtx_ulp6_transfernets_gateway()
{
	local fib i _n _o _p
	_n="$1"
	_o="$2"

	# Setup transfer networks, default routes, and firewall.
	fib=0
	ipfw delete 10 > /dev/null 2>&1 || true
	while test ${fib} -lt ${RT_NUMFIBS}; do
		ifconfig ${IFACE} inet6 2001:2:${fib}::1/64 -alias \
		    > /dev/null 2>&1 || true
		ifconfig ${IFACE} inet6 2001:2:${fib}::1/64 alias \
		    > /dev/null 2>&1
		ipfw add 10 setfib ${fib} ipv6-icmp \
		    from 2001:2:${fib}::/64 to any ip6 icmp6types 135,136 \
		    via ${IFACE} in > /dev/null 2>&1
		# Remove connected routes from all but matching FIB.
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			case ${i} in
			${fib});;
			*)	setfib -F${i} route delete -inet6 \
				    -net 2001:2:${fib}:: > /dev/null 2>&1
				;;
			esac
			i=$((i + 1))
		done
		fib=$((fib + 1))
	done

	# Save PEERADDR
	_p=${PEERADDR}
	PEERADDR="2001:2:ff01::2"

	# Run tests.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do

		print_debug "${_n} ${fib}"

		# Setup expected return values.
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			case "${_o}" in
			-i) eval rc_${i}_l=0 ;;	# ICMPv6 will succeed
			*)  eval rc_${i}_l=1 ;;
			esac
			case ${i} in
			${fib})
				eval rc_${i}_a=0
				;;
			*)	eval rc_${i}_a=1
				;;
			esac
			i=$((i + 1))
		done

		# Add default route.
		setfib -F${fib} route delete -inet6 -net default \
		    > /dev/null 2>&1 || true
		setfib -F${fib} route add -inet6 -net default \
		    2001:2:${fib}::2 > /dev/null 2>&1

		testtx_ulp6_connected "${_n}${fib}" "${_o}" ${fib}

		# Delete default route again.
		setfib -F${fib} route delete -inet6 -net default \
		    > /dev/null 2>&1
		fib=$((fib + 1))
	done

	# Restore PEERADDR
	PEERADDR=${_p}

	# Cleanup default routes, transfer networks, and firewall.
	fib=0
	while test ${fib} -lt ${RT_NUMFIBS}; do
		setfib -F${fib} route delete -inet6 -net default \
		    > /dev/null 2>&1
		ifconfig ${IFACE} inet6 2001:2:${fib}::1/64 -alias \
		    > /dev/null 2>&1
		fib=$((fib + 1))
	done
	ipfw delete 10 > /dev/null 2>&1
}

testtx_icmp6_transfernets_gateway()
{

	testtx_ulp6_transfernets_gateway \
	    "testtx_icmp6_transfernets_gateway" "-i"
}


testtx_tcp6_transfernets_gateway()
{

	testtx_ulp6_transfernets_gateway \
	    "testtx_tcp6_transfernets_gateway" ""
}

testtx_udp6_transfernets_gateway()
{

	testtx_ulp6_transfernets_gateway \
	    "testtx_udp6_transfernets_gateway" "-u"
}


################################################################################
#
# RX tests (Remotely originated connections).  The FIB tests happens on peer.
#
#	# 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.
#
testrx_results()
{
	local _r _n _fib i count _instances _transfer _o
	_fib="$1"
	_n="$2"
	_r="$3"
	_instances=$4
	_transfer=$5
	_o="$6"

	print_debug "testrx_results ${_fib} ${_n} ${_r} ${_instances}"

	# Trim "RESULT "
	_r=${_r#* }

	echo "1..${RT_NUMFIBS}"
	while read i count; do
		if test ${_instances} -gt 1; then
			if test ${count} -gt 0; then
				echo "ok ${i} ${_n}result_${i} #" \
				     "FIB ${i} ${count} (tested)"
			else
				echo "not ok ${i} ${_n}result_${i} #" \
				     "FIB ${i} ${count} (tested)"
			fi
		else
			case ${i} in
			${_fib})
				if test ${count} -gt 0; then
					echo "ok ${i} ${_n}result_${i} #" \
					     "FIB ${i} ${count} (tested)"
				else
					echo "not ok ${i} ${_n}result_${i} #" \
					     "FIB ${i} ${count} (tested)"
				fi
				;;
			*)	if test ${count} -eq 0; then
					echo "ok ${i} ${_n}result_${i} #" \
					    "FIB ${i} ${count}"
				else
					echo "not ok ${i} ${_n}result_${i} #" \
					    "FIB ${i} ${count}"
				fi
				;;
			esac
		fi
		i=$((i + 1))
	done <<EOI
`echo "${_r}" | tr ',' '\n'`
EOI

}

testrx_tcp_udp()
{
	local _n _o _f testno i _fibtxt _instances _res _port _transfer _p
	_n="$1"
	_o="$2"
	_f="$3"
	_instances=$4
	_transfer=$5

	# Unused so far.
	: ${_instances:=1}

	printf "1..%d\n" `expr 2 \* ${RT_NUMFIBS}`
	testno=1
	i=0
	while test ${i} -lt ${RT_NUMFIBS}; do
		print_debug "Testing ${_f} ${i}"

		# We are expecting the FIB number (only) to be returned.
		eval _rc="\${rc_${i}_l}"
		_fibtxt="${_n}_${i}_l ${_f} ${i} ${PEERLINKLOCAL}"
		if test ${_instances} -gt 1; then
			_res="FIB ${i}"
			_port=$((CTRLPORT + 1000 + i))
		else
			_res="${_fibtxt}"
			_port=${CTRLPORT}
		fi
		nc_send_recv ${i} 1 "${_fibtxt}" "${_res}" ${PEERLINKLOCAL} \
		    ${_port} "-6 ${_o} -w1"
		check_rc $? ${_rc} ${testno} "${_fibtxt}" "${_reply}"
		testno=$((testno + 1))

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

		eval _rc="\${rc_${i}_a}"
		_fibtxt="${_n}_${i}_a ${_f} ${i} ${PEERADDR}"
		if test ${_instances} -gt 1; then
			_res="FIB ${i}"
			_port=$((CTRLPORT + 1000 + i))
		else
			_res="${_fibtxt}"
			_port=${CTRLPORT}
		fi
		nc_send_recv ${i} 1 "${_fibtxt}" "${_res}" ${PEERADDR} \
		    ${_port} "-6 ${_o} -w1"
		check_rc $? ${_rc} ${testno} "${_fibtxt}" "${_reply}"
		testno=$((testno + 1))

		# Restore PEERADDR.
		PEERADDR="${_p}"

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


testrx_setup_transfer_networks()
{
	local i

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

testrx_cleanup_transfer_networks()
{
	local i

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


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

	# Netcat options (for UDP basically).
	case "${_o}" in
	-i)	_opts="" ;;		# Use TCP for START/DONE.
	*)	_opts="${_o}" ;;
	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}_${_detsructive}_${_transfer}"

	print_debug "Starting test '${_txt}' (for ${_instances} instances)."

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

	# Let the other side a chance to get ready as well.
	sleep 1

	set +e
	# Let peer know that we are about to start.
	_msg="START ${_txt}"
	nc_send_recv ${_fib} ${WAITS} "${_msg}" "${_msg}" ${PEERADDR} \
	    ${CTRLPORT} "-6 ${_opts} -w1"
	case $? in
	0)	;;
	*)	die "Got invalid reply from peer." \
		    "Expected '${_msg}', got '${_reply}'" ;;
	esac

	# Let the other side a chance to get ready as well.
	sleep 1

	# Send probe.
	case "${_o}" in
	-i)	testtx_icmp6 "${_txt}_" ${_transfer} ;;
	*)	testrx_tcp_udp "${_txt}" "${_o}" "${_fib}" ${_instances} \
		    ${_transfer} ;;
	esac

	# Let peer know that we are done with this test to move to next.
	# This must immediately succeed.
	_msg="DONE ${_txt}"
	nc_send_recv ${_fib} ${WAITS} "${_msg}" "${_msg}" ${PEERADDR} \
	    ${CTRLPORT} "-6 ${_opts} -w1"
	case $? in
	0)	;;
	*)	die "Got invalid reply from peer." \
		    "Expected '${_msg}', got '${_reply}'" ;;
	esac

	# Collect and validate the results.   Always use TCP.
	sleep 1
	set +e
	nc_send_recv ${_fib} 1 "RESULT REQUEST" "" ${PEERADDR} \
	    ${CTRLPORT} "-6 -w1"
	case "${_reply}" in
	RESULT\ *) testrx_results ${_fib} "${_txt}_" "${_reply}" ${_instances} \
			${_transfer} "${_o}"
		;;
	*)	die "Got invalid reply from peer." \
		    "Expected 'RESULT ...', got '${_reply}'" ;;
	esac
	set -e

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

	print_debug "Successfully received status update '${_reply}'."
}

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

	# Setup expected return values.
	if test ${_destructive} -eq 0; then
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			eval rc_${i}_l=0
			eval rc_${i}_a=0
			i=$((i + 1))
		done
	else
		i=0
		while test ${i} -lt ${RT_NUMFIBS}; do
			eval rc_${i}_l=0
			case ${i} in
			${_fib})	eval rc_${i}_a=0 ;;
			*)	# ICMP6 cannot be distinguished and will
				# always work in single transfer network.
				case "${_o}" in
				-i)	case ${_transfer} in
					0) eval rc_${i}_a=0 ;;
					1) eval rc_${i}_a=1 ;;
					esac
					;;
				*)	if test ${_instances} -eq 1 -a \
					    ${_transfer} -eq 0; then
						eval rc_${i}_a=0
					else
						eval rc_${i}_a=1
					fi
					;;
				esac
				;;
			esac
			i=$((i + 1))
		done
	fi

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

testrx_main()
{
	local _n _o s t fib _instances _destructive _transfer
	_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

					testrx_main_setup_rc "${_n}" "${t}" \
					    ${fib} "${_o}" ${_instances} \
					    ${_destructive} ${_transfer}

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

################################################################################
#
#
#

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"
}


################################################################################

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 

	# Reduce the time we wait in case of no reply to 2s.
	sysctl net.inet.tcp.keepinit=2000 > /dev/null 2>&1
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

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

# 64 cases at 16 FIBs.
check_local_addr
check_local_tun

send_greeting

# Initiator testing.
for uso in 0 1; do

	USE_SOSETFIB=${uso}

	# Only run ICMP6 tests for the first loop.
	# 160 cases at 16 FIBs.
	test ${uso} -ne 0 || testtx_icmp6_connected && sleep 1
	testtx_tcp6_connected && sleep 1
	testtx_udp6_connected && sleep 1

	# 2560 cases at 16 FIBs.
	test ${uso} -ne 0 || testtx_icmp6_connected_blackhole && sleep 1
	testtx_tcp6_connected_blackhole && sleep 1
	testtx_udp6_connected_blackhole && sleep 1

	# 2560 cases at 16 FIBs.
	test ${uso} -ne 0 || testtx_icmp6_connected_transfernets && sleep 1
	testtx_tcp6_connected_transfernets && sleep 1
	testtx_udp6_connected_transfernets && sleep 1

	# 2560 cases at 16 FIBs.
	test ${uso} -ne 0 || \
	    testtx_icmp6_connected_ifconfig_transfernets && sleep 1
	testtx_tcp6_connected_ifconfig_transfernets && sleep 1
	testtx_udp6_connected_ifconfig_transfernets && sleep 1

	# 160 cases at 16 FIBs.
	test ${uso} -ne 0 || testtx_icmp6_gateway && sleep 1
	testtx_tcp6_gateway && sleep 1
	testtx_udp6_gateway && sleep 1

	# 160 cases at 16 FIBs.
	test ${uso} -ne 0 || testtx_icmp6_transfernets_gateways && sleep 1
	testtx_tcp6_transfernets_gateways && sleep 1
	testtx_udp6_transfernets_gateways && sleep 1

	# 2560 cases at 16 FIBs.
	test ${uso} -ne 0 || testtx_icmp6_transfernets_gateway && sleep 1
	testtx_tcp6_transfernets_gateway && sleep 1
	testtx_udp6_transfernets_gateway && sleep 1
done

# Receiver testing.
for uso in 0 1; do

	USE_SOSETFIB=${uso}

	# Only expect ICMP6 tests for the first loop.
	# 6144 cases at 16 FIBs.
	test ${uso} -ne 0 || testrx_icmp6_same_addr_one_fib_a_time && sleep 1
	# 12288 cases at 16 FIBs.
	testrx_tcp6_same_addr_one_fib_a_time && sleep 1
	# 12288 cases at 16 FIBs.
	testrx_udp6_same_addr_one_fib_a_time && sleep 1

	# 12288 cases at 16 FIBs.
	testrx_tcp6_same_addr_all_fibs_a_time && sleep 1
	# 12288 cases at 16 FIBs.
	testrx_udp6_same_addr_all_fibs_a_time && sleep 1

done

cleanup

# end