aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/netpfil/pf/sctp.sh
blob: 78055f5a9dd2f825c657a947c4e5867c030a083a (plain) (blame)
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
#
# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
#
# Copyright © 2023 Orange Business Services
#
# 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.

. $(atf_get_srcdir)/utils.subr

sctp_init()
{
	pft_init
	if ! kldstat -q -m sctp; then
		atf_skip "This test requires SCTP"
	fi
}

atf_test_case "basic_v4" "cleanup"
basic_v4_head()
{
	atf_set descr 'Basic SCTP connection over IPv4 passthrough'
	atf_set require.user root
}

basic_v4_body()
{
	sctp_init

	j="sctp:basic_v4"
	epair=$(vnet_mkepair)

	vnet_mkjail ${j}a ${epair}a
	vnet_mkjail ${j}b ${epair}b

	jexec ${j}a ifconfig ${epair}a 192.0.2.1/24 up
	jexec ${j}b ifconfig ${epair}b 192.0.2.2/24 up
	# Sanity check
	atf_check -s exit:0 -o ignore \
	    jexec ${j}a ping -c 1 192.0.2.2

	jexec ${j}a pfctl -e
	pft_set_rules ${j}a \
		"block" \
		"pass in proto sctp to port 1234"

	echo "foo" | jexec ${j}a nc --sctp -N -l 1234 &

	# Wait for the server to start
	sleep 1

	out=$(jexec ${j}b nc --sctp -N -w 3 192.0.2.1 1234)
	if [ "$out" != "foo" ]; then
		atf_fail "SCTP connection failed"
	fi

	# Now with scrub rules present, so normalization is done
	pft_set_rules ${j}a \
		"scrub on ${j}a" \
		"block" \
		"pass in proto sctp to port 1234"

	echo "foo" | jexec ${j}a nc --sctp -N -l 1234 &
	sleep 1

	out=$(jexec ${j}b nc --sctp -N -w 3 192.0.2.1 1234)
	if [ "$out" != "foo" ]; then
		atf_fail "SCTP connection failed"
	fi

	# Now fail with a blocked port
	echo "foo" | jexec ${j}a nc --sctp -N -l 1235 &
	sleep 1

	out=$(jexec ${j}b nc --sctp -N -w 3 192.0.2.1 1235)
	if [ "$out" == "foo" ]; then
		atf_fail "SCTP port block failed"
	fi

	# Now fail with a blocked port but passing source port
	out=$(jexec ${j}b nc --sctp -N -w 3 -p 1234 192.0.2.1 1235)
	if [ "$out" == "foo" ]; then
		atf_fail "SCTP port block failed"
	fi
}

basic_v4_cleanup()
{
	pft_cleanup
}

atf_test_case "basic_v6" "cleanup"
basic_v6_head()
{
	atf_set descr 'Basic SCTP connection over IPv6'
	atf_set require.user root
}

basic_v6_body()
{
	sctp_init

	j="sctp:basic_v6"
	epair=$(vnet_mkepair)

	vnet_mkjail ${j}a ${epair}a
	vnet_mkjail ${j}b ${epair}b

	jexec ${j}a ifconfig ${epair}a inet6 2001:db8::a/64 up no_dad
	jexec ${j}b ifconfig ${epair}b inet6 2001:db8::b/64 up no_dad

	# Sanity check
	atf_check -s exit:0 -o ignore \
	    jexec ${j}a ping -6 -c 1 2001:db8::b

	jexec ${j}a pfctl -e
	pft_set_rules ${j}a \
		"block proto sctp" \
		"pass in proto sctp to port 1234"

	echo "foo" | jexec ${j}a nc -6 --sctp -N -l 1234 &

	# Wait for the server to start
	sleep 1

	out=$(jexec ${j}b nc --sctp -N -w 3 2001:db8::a 1234)
	if [ "$out" != "foo" ]; then
		atf_fail "SCTP connection failed"
	fi

	# Now with scrub rules present, so normalization is done
	pft_set_rules ${j}a \
		"scrub on ${j}a" \
		"block proto sctp" \
		"pass in proto sctp to port 1234"

	echo "foo" | jexec ${j}a nc -6 --sctp -N -l 1234 &
	sleep 1

	out=$(jexec ${j}b nc --sctp -N -w 3 2001:db8::a 1234)
	if [ "$out" != "foo" ]; then
		atf_fail "SCTP connection failed"
	fi

	# Now fail with a blocked port
	echo "foo" | jexec ${j}a nc -6 --sctp -N -l 1235 &
	sleep 1

	out=$(jexec ${j}b nc --sctp -N -w 3 2001:db8::a 1235)
	if [ "$out" == "foo" ]; then
		atf_fail "SCTP port block failed"
	fi

	# Now fail with a blocked port but passing source port
	out=$(jexec ${j}b nc --sctp -N -w 3 -p 1234 2001:db8::a 1235)
	if [ "$out" == "foo" ]; then
		atf_fail "SCTP port block failed"
	fi
}

basic_v6_cleanup()
{
	pft_cleanup
}

atf_test_case "reuse" "cleanup"
reuse_head()
{
	atf_set descr 'Test handling dumb clients that reuse source ports'
	atf_set require.user root
}

reuse_body()
{
	sctp_init

	j="sctp:reuse"
	epair=$(vnet_mkepair)

	vnet_mkjail ${j}a ${epair}a
	vnet_mkjail ${j}b ${epair}b

	jexec ${j}a ifconfig ${epair}a 192.0.2.1/24 up
	jexec ${j}b ifconfig ${epair}b 192.0.2.2/24 up
	# Sanity check
	atf_check -s exit:0 -o ignore \
	    jexec ${j}a ping -c 1 192.0.2.2

	jexec ${j}a pfctl -e
	pft_set_rules ${j}a \
		"block" \
		"pass in proto sctp to port 1234"

	echo "foo" | jexec ${j}a nc --sctp -N -l 1234 &

	# Wait for the server to start
	sleep 1

	out=$(jexec ${j}b nc --sctp -N -w 3 -p 1234 192.0.2.1 1234)
	if [ "$out" != "foo" ]; then
		atf_fail "SCTP connection failed"
	fi

	# Now do the same thing again, with the same port numbers
	jexec ${j}a pfctl -ss -v

	echo "foo" | jexec ${j}a nc --sctp -N -l 1234 &

	# Wait for the server to start
	sleep 1

	out=$(jexec ${j}b nc --sctp -N -w 3 -p 1234 192.0.2.1 1234)
	if [ "$out" != "foo" ]; then
		atf_fail "SCTP connection failed"
	fi
	jexec ${j}a pfctl -ss -v
}

reuse_cleanup()
{
	pft_cleanup
}

atf_test_case "abort_v4" "cleanup"
abort_v4_head()
{
	atf_set descr 'Test sending ABORT messages'
	atf_set require.user root
}

abort_v4_body()
{
	sctp_init

	j="sctp:abort_v4"
	epair=$(vnet_mkepair)

	vnet_mkjail ${j}a ${epair}a
	vnet_mkjail ${j}b ${epair}b

	jexec ${j}a ifconfig ${epair}a 192.0.2.1/24 up
	jexec ${j}b ifconfig ${epair}b 192.0.2.2/24 up

	# Sanity check
	atf_check -s exit:0 -o ignore \
	    jexec ${j}a ping -c 1 192.0.2.2

	jexec ${j}a pfctl -e
	pft_set_rules ${j}a \
		"block return in proto sctp to port 1234"

	echo "foo" | jexec ${j}a nc --sctp -N -l 1234 &

	# Wait for the server to start
	sleep 1

	# If we get the abort we'll exit immediately, if we don't timeout will
	# stop nc.
	out=$(jexec ${j}b timeout 3 nc --sctp -N 192.0.2.1 1234)
	if [ $? -eq 124 ]; then
		atf_fail 'Abort not received'
	fi
	if [ "$out" == "foo" ]; then
		atf_fail "block failed entirely"
	fi

	# Without 'return' we will time out.
	pft_set_rules ${j}a \
		"block in proto sctp to port 1234"

	out=$(jexec ${j}b timeout 3 nc --sctp -N 192.0.2.1 1234)
	if [ $? -ne 124 ]; then
		atf_fail 'Abort sent anyway?'
	fi
}

abort_v4_cleanup()
{
	pft_cleanup
}

atf_test_case "abort_v6" "cleanup"
abort_v6_head()
{
	atf_set descr 'Test sending ABORT messages over IPv6'
	atf_set require.user root
}

abort_v6_body()
{
	sctp_init

	j="sctp:abort_v6"
	epair=$(vnet_mkepair)

	vnet_mkjail ${j}a ${epair}a
	vnet_mkjail ${j}b ${epair}b

	jexec ${j}a ifconfig ${epair}a inet6 2001:db8::a/64 no_dad
	jexec ${j}b ifconfig ${epair}b inet6 2001:db8::b/64 no_dad

	# Sanity check
	atf_check -s exit:0 -o ignore \
	    jexec ${j}a ping -6 -c 1 2001:db8::b

	jexec ${j}a pfctl -e
	pft_set_rules ${j}a \
		"block return in proto sctp to port 1234"

	echo "foo" | jexec ${j}a nc -6 --sctp -N -l 1234 &

	# Wait for the server to start
	sleep 1

	# If we get the abort we'll exit immediately, if we don't timeout will
	# stop nc.
	out=$(jexec ${j}b timeout 3 nc --sctp -N 2001:db8::a 1234)
	if [ $? -eq 124 ]; then
		atf_fail 'Abort not received'
	fi
	if [ "$out" == "foo" ]; then
		atf_fail "block failed entirely"
	fi

	# Without 'return' we will time out.
	pft_set_rules ${j}a \
		"block in proto sctp to port 1234"

	out=$(jexec ${j}b timeout 3 nc --sctp -N 2001:db8::a 1234)
	if [ $? -ne 124 ]; then
		atf_fail 'Abort sent anyway?'
	fi
}

abort_v6_cleanup()
{
	pft_cleanup
}

atf_test_case "nat_v4" "cleanup"
nat_v4_head()
{
	atf_set descr 'Test NAT-ing SCTP over IPv4'
	atf_set require.user root
}

nat_v4_body()
{
	sctp_init

	j="sctp:nat_v4"
	epair_c=$(vnet_mkepair)
	epair_srv=$(vnet_mkepair)

	vnet_mkjail ${j}srv ${epair_srv}a
	vnet_mkjail ${j}gw ${epair_srv}b ${epair_c}a
	vnet_mkjail ${j}c ${epair_c}b

	jexec ${j}srv ifconfig ${epair_srv}a 198.51.100.1/24 up
	# No default route in srv jail, to ensure we're NAT-ing
	jexec ${j}gw ifconfig ${epair_srv}b 198.51.100.2/24 up
	jexec ${j}gw ifconfig ${epair_c}a 192.0.2.1/24 up
	jexec ${j}gw sysctl net.inet.ip.forwarding=1
	jexec ${j}c ifconfig ${epair_c}b 192.0.2.2/24 up
	jexec ${j}c route add default 192.0.2.1

	jexec ${j}gw pfctl -e
	pft_set_rules ${j}gw \
		"nat on ${epair_srv}b from 192.0.2.0/24 -> (${epair_srv}b)" \
		"pass"

	# Sanity check
	atf_check -s exit:0 -o ignore \
	    jexec ${j}c ping -c 1 198.51.100.1

	echo "foo" | jexec ${j}srv nc --sctp -N -l 1234 &

	# Wait for the server to start
	sleep 1

	out=$(jexec ${j}c nc --sctp -N -w 3 198.51.100.1 1234)
	if [ "$out" != "foo" ]; then
		atf_fail "SCTP connection failed"
	fi
}

nat_v4_cleanup()
{
	pft_cleanup
}

atf_test_case "nat_v6" "cleanup"
nat_v6_head()
{
	atf_set descr 'Test NAT-ing SCTP over IPv6'
	atf_set require.user root
}

nat_v6_body()
{
	sctp_init

	j="sctp:nat_v6"
	epair_c=$(vnet_mkepair)
	epair_srv=$(vnet_mkepair)

	vnet_mkjail ${j}srv ${epair_srv}a
	vnet_mkjail ${j}gw ${epair_srv}b ${epair_c}a
	vnet_mkjail ${j}c ${epair_c}b

	jexec ${j}srv ifconfig ${epair_srv}a inet6 2001:db8::1/64 up no_dad
	# No default route in srv jail, to ensure we're NAT-ing
	jexec ${j}gw ifconfig ${epair_srv}b inet6 2001:db8::2/64 up no_dad
	jexec ${j}gw ifconfig ${epair_c}a inet6 2001:db8:1::1/64 up no_dad
	jexec ${j}gw sysctl net.inet6.ip6.forwarding=1
	jexec ${j}c ifconfig ${epair_c}b inet6 2001:db8:1::2/64 up no_dad
	jexec ${j}c route add -6 default 2001:db8:1::1

	jexec ${j}gw pfctl -e
	pft_set_rules ${j}gw \
		"nat on ${epair_srv}b from 2001:db8:1::/64 -> (${epair_srv}b)" \
		"pass"

	# Sanity check
	atf_check -s exit:0 -o ignore \
	    jexec ${j}c ping -6 -c 1 2001:db8::1

	echo "foo" | jexec ${j}srv nc -6 --sctp -N -l 1234 &

	# Wait for the server to start
	sleep 1

	out=$(jexec ${j}c nc --sctp -N -w 3 2001:db8::1 1234)
	if [ "$out" != "foo" ]; then
		atf_fail "SCTP connection failed"
	fi
}

nat_v6_cleanup()
{
	pft_cleanup
}

atf_test_case "rdr_v4" "cleanup"
rdr_v4_head()
{
	atf_set descr 'Test rdr SCTP over IPv4'
	atf_set require.user root
}

rdr_v4_body()
{
	sctp_init

	j="sctp:rdr_v4"
	epair_c=$(vnet_mkepair)
	epair_srv=$(vnet_mkepair)

	vnet_mkjail ${j}srv ${epair_srv}a
	vnet_mkjail ${j}gw ${epair_srv}b ${epair_c}a
	vnet_mkjail ${j}c ${epair_c}b

	jexec ${j}srv ifconfig ${epair_srv}a 198.51.100.1/24 up
	# No default route in srv jail, to ensure we're NAT-ing
	jexec ${j}gw ifconfig ${epair_srv}b 198.51.100.2/24 up
	jexec ${j}gw ifconfig ${epair_c}a 192.0.2.1/24 up
	jexec ${j}gw sysctl net.inet.ip.forwarding=1
	jexec ${j}c ifconfig ${epair_c}b 192.0.2.2/24 up
	jexec ${j}c route add default 192.0.2.1

	jexec ${j}gw pfctl -e
	pft_set_rules ${j}gw \
		"rdr pass on ${epair_srv}b proto sctp from 198.51.100.0/24 to any port 1234 -> 192.0.2.2 port 1234" \
		"pass"

	echo "foo" | jexec ${j}c nc --sctp -N -l 1234 &

	# Wait for the server to start
	sleep 1

	out=$(jexec ${j}srv nc --sctp -N -w 3 198.51.100.2 1234)
	if [ "$out" != "foo" ]; then
		atf_fail "SCTP connection failed"
	fi

	# Despite configuring port changes pf will not do so.
	echo "bar" | jexec ${j}c nc --sctp -N -l 1234 &

	pft_set_rules ${j}gw \
		"rdr pass on ${epair_srv}b proto sctp from 198.51.100.0/24 to any port 1234 -> 192.0.2.2 port 4321" \
		"pass"

	# This will fail
	out=$(jexec ${j}srv nc --sctp -N -w 3 198.51.100.2 4321)
	if [ "$out" == "bar" ]; then
		atf_fail "Port was unexpectedly changed."
	fi

	# This succeeds
	out=$(jexec ${j}srv nc --sctp -N -w 3 198.51.100.2 1234)
	if [ "$out" != "bar" ]; then
		atf_fail "Port was unexpectedly changed."
	fi
}

rdr_v4_cleanup()
{
	pft_cleanup
}

atf_test_case "pfsync" "cleanup"
pfsync_head()
{
	atf_set descr 'Test pfsync-ing SCTP connections'
	atf_set require.user root
}

pfsync_body()
{
	# + Builds bellow topology and initiate an SCTP connection
	#   from client to server.
	# + Tests that the connection remains open when we fail over from
	#   router one to router two.
	#
	#                   ┌──────┐
	#                   │client│
	#                   └───┬──┘
	#                       │
	#                   ┌───┴───┐
	#                   │bridge0│
	#                   └┬─────┬┘
	#                    │     │
	#   ┌────────────────┴─┐ ┌─┴────────────────┐
	#   │        one       ├─┤       two        │
	#   └────────────────┬─┘ └─┬────────────────┘
	#                    │     │
	#                   ┌┴─────┴┐
	#                   │bridge1│
	#                   └───┬───┘
	#                       │
	#                   ┌───┴──┐
	#                   │server│
	#                   └──────┘

	sctp_init
	pfsynct_init
	vnet_init_bridge
	if ! kldstat -q -m carp
	then
		atf_skip "This test requires carp"
	fi

	j="sctp:pfsync"

	tmp=`pwd`

	bridge0=$(vnet_mkbridge)
	bridge1=$(vnet_mkbridge)

	epair_c=$(vnet_mkepair)
	epair_one0=$(vnet_mkepair)
	epair_two0=$(vnet_mkepair)
	epair_sync=$(vnet_mkepair)
	epair_one1=$(vnet_mkepair)
	epair_two1=$(vnet_mkepair)
	epair_srv=$(vnet_mkepair)

	ifconfig ${bridge0} addm ${epair_c}a addm ${epair_one0}a addm ${epair_two0}a
	ifconfig ${epair_one0}a up
	ifconfig ${epair_two0}a up
	ifconfig ${epair_c}a up
	ifconfig ${bridge0} up

	ifconfig ${bridge1} addm ${epair_srv}a addm ${epair_one1}a addm ${epair_two1}a
	ifconfig ${epair_one1}a up
	ifconfig ${epair_two1}a up
	ifconfig ${epair_srv}a up
	ifconfig ${bridge1} up

	vnet_mkjail ${j}c ${epair_c}b
	jexec ${j}c ifconfig ${epair_c}b 192.0.2.2/24 up
	jexec ${j}c route add default 192.0.2.1

	vnet_mkjail ${j}one ${epair_one0}b ${epair_one1}b ${epair_sync}a
	jexec ${j}one ifconfig ${epair_one0}b 192.0.2.3/24 up
	jexec ${j}one ifconfig ${epair_one0}b \
	    alias 192.0.2.1/32 vhid 1 pass 1234
	jexec ${j}one ifconfig ${epair_one1}b 198.51.100.3/24 up
	jexec ${j}one ifconfig ${epair_one1}b \
	    alias 198.51.100.2/32 vhid 2 pass 4321
	jexec ${j}one ifconfig ${epair_sync}a 203.0.113.1/24 up
	jexec ${j}one ifconfig pfsync0 \
		syncdev ${epair_sync}a \
		maxupd 1 \
		up
	jexec ${j}one sysctl net.inet.ip.forwarding=1

	vnet_mkjail ${j}two ${epair_two0}b ${epair_two1}b ${epair_sync}b
	jexec ${j}two ifconfig ${epair_two0}b 192.0.2.4/24 up
	jexec ${j}two ifconfig ${epair_two0}b \
	    alias 192.0.2.1/32 vhid 1 pass 1234
	jexec ${j}two ifconfig ${epair_two1}b 198.51.100.4/24 up
	jexec ${j}two ifconfig ${epair_two1}b \
	    alias 198.51.100.2/32 vhid 2 pass 4321
	jexec ${j}two ifconfig ${epair_sync}b 203.0.113.2/24 up
	jexec ${j}two ifconfig pfsync0 \
		syncdev ${epair_sync}b \
		maxupd 1 \
		up
	jexec ${j}two sysctl net.inet.ip.forwarding=1

	vnet_mkjail ${j}srv ${epair_srv}b
	jexec ${j}srv ifconfig ${epair_srv}b 198.51.100.1/24 up
	jexec ${j}srv route add default 198.51.100.2

	# Demote two, to avoid dealing with asymmetric routing
	jexec ${j}two sysctl net.inet.carp.demotion=50

	jexec ${j}one pfctl -e
	pft_set_rules ${j}one \
		"block all" \
		"pass proto { icmp, pfsync, carp }" \
		"pass proto sctp to port 1234" \
		"pass proto tcp to port 1234"

	jexec ${j}two pfctl -e
	pft_set_rules ${j}two \
		"block all" \
		"pass proto { icmp, pfsync, carp }" \
		"pass proto sctp to port 1234" \
		"pass proto tcp to port 1234"

	# Give carp time to get set up
	sleep 2

	# Sanity check
	atf_check -s exit:0 -o ignore \
	    jexec ${j}c ping -c 1 198.51.100.1

	# Now start up an SCTP connection
	touch ${tmp}/input
	tail -F ${tmp}/input | jexec ${j}srv nc --sctp -l 1234 &
	sleep 1

	jexec ${j}c nc --sctp 198.51.100.1 1234 > ${tmp}/output &
	echo "1" >> ${tmp}/input

	# Give time for the traffic to arrive
	sleep 1
	line=$(tail -n -1 ${tmp}/output)
	if [ "${line}" != "1" ];
	then
		echo "Found ${line}"
		cat ${tmp}/output
		atf_fail "Initial SCTP connection failed"
	fi

	# Give pfsync some time to do its thing
	sleep 1

	# Verify that two has the connection too
	state=$(jexec ${j}two pfctl -ss | grep sctp)
	if [ -z "${state}" ];
	then
		jexec ${j}two pfctl -ss
		atf_fail "Failed to find SCTP state on secondary pfsync host"
	fi

	# Now fail over (both carp IPs should switch here)
	jexec ${j}one sysctl net.inet.carp.demotion=100

	while ! jexec ${j}one ifconfig ${epair_one0}b | grep MASTER;
	do
		sleep 1
	done
	while ! jexec ${j}one ifconfig ${epair_one1}b | grep MASTER;
	do
		sleep 1
	done

	# Sanity check
	atf_check -s exit:0 -o ignore \
	    jexec ${j}c ping -c 1 198.51.100.1

	# And check that the connection is still live
	echo "2" >> ${tmp}/input
	sleep 1
	line=$(tail -n -1 ${tmp}/output)
	if [ "${line}" != "2" ];
	then
		echo "Found ${line}"
		cat ${tmp}/output
		atf_fail "SCTP failover failed"
	fi
}

pfsync_cleanup()
{
	pfsynct_cleanup
}

atf_test_case "timeout" "cleanup"
timeout_head()
{
	atf_set descr 'Test setting and retrieving timeout values'
	atf_set require.user root
}

timeout_body()
{
	sctp_init

	vnet_mkjail timeout

	pft_set_rules timeout \
		"set timeout sctp.first 13" \
		"set timeout sctp.opening 14"

	atf_check -s exit:0 -o match:"sctp.first.*13" \
	    jexec timeout pfctl -st
	atf_check -s exit:0 -o match:"sctp.opening.*14" \
	    jexec timeout pfctl -st
	# We've not changed other timeouts
	atf_check -s exit:0 -o match:"sctp.established.*86400" \
	    jexec timeout pfctl -st
}

timeout_cleanup()
{
	pft_cleanup
}

atf_test_case "related_icmp" "cleanup"
related_icmp_head()
{
	atf_set descr 'Verify that ICMP messages related to an SCTP connection are allowed'
	atf_set require.user root
}

related_icmp_body()
{
	sctp_init

	epair_cl=$(vnet_mkepair)
	epair_rtr=$(vnet_mkepair)
	epair_srv=$(vnet_mkepair)

	ifconfig ${epair_cl}a 192.0.2.1/24 up
	route add default 192.0.2.2

	vnet_mkjail rtr ${epair_cl}b ${epair_rtr}a
	jexec rtr ifconfig ${epair_cl}b 192.0.2.2/24 up
	jexec rtr ifconfig ${epair_rtr}a 198.51.100.1/24 up
	jexec rtr sysctl net.inet.ip.forwarding=1
	jexec rtr route add default 198.51.100.2

	vnet_mkjail rtr2 ${epair_rtr}b ${epair_srv}a
	jexec rtr2 ifconfig ${epair_rtr}b 198.51.100.2/24 up
	jexec rtr2 ifconfig ${epair_srv}a 203.0.113.1/24 up
	jexec rtr2 ifconfig ${epair_srv}a mtu 1300
	jexec rtr2 sysctl net.inet.ip.forwarding=1
	jexec rtr2 route add default 198.51.100.1

	vnet_mkjail srv ${epair_srv}b
	jexec srv ifconfig ${epair_srv}b 203.0.113.2/24 up
	jexec srv ifconfig ${epair_srv}b mtu 1300
	jexec srv route add default 203.0.113.1

	# Sanity checks
	atf_check -s exit:0 -o ignore \
	    ping -c 1 192.0.2.2
	atf_check -s exit:0 -o ignore \
	    ping -c 1 198.51.100.1
	atf_check -s exit:0 -o ignore \
	    ping -c 1 198.51.100.2
	atf_check -s exit:0 -o ignore \
	    ping -c 1 203.0.113.1
	atf_check -s exit:0 -o ignore \
	    ping -c 1 203.0.113.2

	jexec rtr pfctl -e
	pft_set_rules rtr \
	    "block proto icmp" \
	    "pass proto sctp"

	# Make sure SCTP traffic passes
	echo "foo" | jexec srv nc --sctp -N -l 1234 &
	sleep 1

	out=$(nc --sctp -N -w 3 203.0.113.2 1234)
	if [ "$out" != "foo" ]; then
		jexec rtr pfctl -ss -vv
		jexec rtr pfctl -sr -vv
		atf_fail "SCTP connection failed"
	fi

	# Do we see ICMP traffic if we send overly large traffic?
	echo "foo" | jexec srv nc --sctp -l 1234 >/dev/null &
	sleep 1

	atf_check -s exit:0 -o not-match:".*destination unreachable:.*" \
	    netstat -s -p icmp

	# Generate traffic that will be fragmented by rtr2, and will provoke an
	# ICMP unreachable - need to frag (mtu 1300) message
	dd if=/dev/random bs=10000 count=1 | nc --sctp -N -w 3 203.0.113.2 1234

	# We'd expect to see an ICMP message
	atf_check -s exit:0 -o match:".*destination unreachable: [1-9]" \
	    netstat -s -p icmp
}

related_icmp_cleanup()
{
	pft_cleanup
}

atf_init_test_cases()
{
	atf_add_test_case "basic_v4"
	atf_add_test_case "basic_v6"
	atf_add_test_case "reuse"
	atf_add_test_case "abort_v4"
	atf_add_test_case "abort_v6"
	atf_add_test_case "nat_v4"
	atf_add_test_case "nat_v6"
	atf_add_test_case "rdr_v4"
	atf_add_test_case "pfsync"
	atf_add_test_case "timeout"
	atf_add_test_case "related_icmp"
}