xref: /linux/tools/testing/selftests/net/mptcp/mptcp_join.sh (revision 07d6bf634bc8f93caf8920c9d61df761645336e2)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Double quotes to prevent globbing and word splitting is recommended in new
5# code but we accept it, especially because there were too many before having
6# address all other issues detected by shellcheck.
7#shellcheck disable=SC2086
8
9# ShellCheck incorrectly believes that most of the code here is unreachable
10# because it's invoked by variable name, see how the "tests" array is used
11#shellcheck disable=SC2317
12
13. "$(dirname "${0}")/mptcp_lib.sh"
14
15ret=0
16sin=""
17sinfail=""
18sout=""
19cin=""
20cinfail=""
21cinsent=""
22tmpfile=""
23cout=""
24err=""
25capout=""
26cappid=""
27ns1=""
28ns2=""
29iptables="iptables"
30ip6tables="ip6tables"
31timeout_poll=30
32timeout_test=$((timeout_poll * 2 + 1))
33capture=false
34checksum=false
35check_invert=0
36validate_checksum=false
37init=0
38evts_ns1=""
39evts_ns2=""
40evts_ns1_pid=0
41evts_ns2_pid=0
42last_test_failed=0
43last_test_skipped=0
44last_test_ignored=1
45
46declare -A all_tests
47declare -a only_tests_ids
48declare -a only_tests_names
49declare -A failed_tests
50MPTCP_LIB_TEST_FORMAT="%03u %s\n"
51TEST_NAME=""
52nr_blank=6
53
54# These var are used only in some tests, make sure they are not already set
55unset FAILING_LINKS
56unset test_linkfail
57unset addr_nr_ns1
58unset addr_nr_ns2
59unset cestab_ns1
60unset cestab_ns2
61unset sflags
62unset fastclose
63unset fullmesh
64unset speed
65unset join_csum_ns1
66unset join_csum_ns2
67unset join_fail_nr
68unset join_rst_nr
69unset join_infi_nr
70unset join_corrupted_pkts
71unset join_syn_tx
72unset join_create_err
73unset join_bind_err
74unset join_connect_err
75
76# generated using "nfbpf_compile '(ip && (ip[54] & 0xf0) == 0x30) ||
77#				  (ip6 && (ip6[74] & 0xf0) == 0x30)'"
78CBPF_MPTCP_SUBOPTION_ADD_ADDR="14,
79			       48 0 0 0,
80			       84 0 0 240,
81			       21 0 3 64,
82			       48 0 0 54,
83			       84 0 0 240,
84			       21 6 7 48,
85			       48 0 0 0,
86			       84 0 0 240,
87			       21 0 4 96,
88			       48 0 0 74,
89			       84 0 0 240,
90			       21 0 1 48,
91			       6 0 0 65535,
92			       6 0 0 0"
93
94init_partial()
95{
96	capout=$(mktemp)
97
98	mptcp_lib_ns_init ns1 ns2
99
100	local netns
101	for netns in "$ns1" "$ns2"; do
102		ip netns exec $netns sysctl -q net.mptcp.pm_type=0 2>/dev/null || true
103		if $checksum; then
104			ip netns exec $netns sysctl -q net.mptcp.checksum_enabled=1
105		fi
106	done
107
108	check_invert=0
109	validate_checksum=$checksum
110
111	#  ns1         ns2
112	# ns1eth1    ns2eth1
113	# ns1eth2    ns2eth2
114	# ns1eth3    ns2eth3
115	# ns1eth4    ns2eth4
116
117	local i
118	for i in $(seq 1 4); do
119		ip link add ns1eth$i netns "$ns1" type veth peer name ns2eth$i netns "$ns2"
120		ip -net "$ns1" addr add 10.0.$i.1/24 dev ns1eth$i
121		ip -net "$ns1" addr add dead:beef:$i::1/64 dev ns1eth$i nodad
122		ip -net "$ns1" link set ns1eth$i up
123
124		ip -net "$ns2" addr add 10.0.$i.2/24 dev ns2eth$i
125		ip -net "$ns2" addr add dead:beef:$i::2/64 dev ns2eth$i nodad
126		ip -net "$ns2" link set ns2eth$i up
127
128		# let $ns2 reach any $ns1 address from any interface
129		ip -net "$ns2" route add default via 10.0.$i.1 dev ns2eth$i metric 10$i
130		ip -net "$ns2" route add default via dead:beef:$i::1 dev ns2eth$i metric 10$i
131	done
132}
133
134init_shapers()
135{
136	local i
137	for i in $(seq 1 4); do
138		tc -n $ns1 qdisc add dev ns1eth$i root netem rate 20mbit delay 1ms
139		tc -n $ns2 qdisc add dev ns2eth$i root netem rate 20mbit delay 1ms
140	done
141}
142
143cleanup_partial()
144{
145	rm -f "$capout"
146
147	mptcp_lib_ns_exit "${ns1}" "${ns2}"
148}
149
150init() {
151	init=1
152
153	mptcp_lib_check_mptcp
154	mptcp_lib_check_kallsyms
155	mptcp_lib_check_tools ip tc ss "${iptables}" "${ip6tables}"
156
157	sin=$(mktemp)
158	sout=$(mktemp)
159	cin=$(mktemp)
160	cinsent=$(mktemp)
161	cout=$(mktemp)
162	err=$(mktemp)
163	evts_ns1=$(mktemp)
164	evts_ns2=$(mktemp)
165
166	trap cleanup EXIT
167
168	make_file "$cin" "client" 1 >/dev/null
169	make_file "$sin" "server" 1 >/dev/null
170}
171
172cleanup()
173{
174	rm -f "$cin" "$cout" "$sinfail"
175	rm -f "$sin" "$sout" "$cinsent" "$cinfail"
176	rm -f "$tmpfile"
177	rm -rf $evts_ns1 $evts_ns2
178	rm -f "$err"
179	cleanup_partial
180}
181
182print_check()
183{
184	printf "%-${nr_blank}s%-36s" " " "${*}"
185}
186
187print_info()
188{
189	# It can be empty, no need to print anything then
190	[ -z "${1}" ] && return
191
192	mptcp_lib_print_info "      Info: ${*}"
193}
194
195print_ok()
196{
197	mptcp_lib_pr_ok "${@}"
198}
199
200print_fail()
201{
202	mptcp_lib_pr_fail "${@}"
203}
204
205print_skip()
206{
207	mptcp_lib_pr_skip "${@}"
208}
209
210# $1: check name; $2: rc
211print_results()
212{
213	local check="${1}"
214	local rc=${2}
215
216	print_check "${check}"
217	if [ ${rc} = ${KSFT_PASS} ]; then
218		print_ok
219	elif [ ${rc} = ${KSFT_SKIP} ]; then
220		print_skip
221	else
222		fail_test "see above"
223	fi
224}
225
226# [ $1: fail msg ]
227mark_as_skipped()
228{
229	local msg="${1:-"Feature not supported"}"
230
231	mptcp_lib_fail_if_expected_feature "${msg}"
232
233	print_check "${msg}"
234	print_skip
235
236	last_test_skipped=1
237}
238
239# $@: condition
240continue_if()
241{
242	if ! "${@}"; then
243		mark_as_skipped
244		return 1
245	fi
246}
247
248skip_test()
249{
250	if [ "${#only_tests_ids[@]}" -eq 0 ] && [ "${#only_tests_names[@]}" -eq 0 ]; then
251		return 1
252	fi
253
254	local i
255	for i in "${only_tests_ids[@]}"; do
256		if [ "$((MPTCP_LIB_TEST_COUNTER+1))" -eq "${i}" ]; then
257			return 1
258		fi
259	done
260	for i in "${only_tests_names[@]}"; do
261		if [ "${TEST_NAME}" = "${i}" ]; then
262			return 1
263		fi
264	done
265
266	return 0
267}
268
269append_prev_results()
270{
271	if [ ${last_test_failed} -eq 1 ]; then
272		mptcp_lib_result_fail "${TEST_NAME}"
273	elif [ ${last_test_skipped} -eq 1 ]; then
274		mptcp_lib_result_skip "${TEST_NAME}"
275	elif [ ${last_test_ignored} -ne 1 ]; then
276		mptcp_lib_result_pass "${TEST_NAME}"
277	fi
278
279	last_test_failed=0
280	last_test_skipped=0
281	last_test_ignored=0
282}
283
284# $1: test name
285reset()
286{
287	append_prev_results
288
289	TEST_NAME="${1}"
290
291	MPTCP_LIB_SUBTEST_FLAKY=0 # reset if modified
292
293	if skip_test; then
294		MPTCP_LIB_TEST_COUNTER=$((MPTCP_LIB_TEST_COUNTER+1))
295		last_test_ignored=1
296		return 1
297	fi
298
299	mptcp_lib_print_title "${TEST_NAME}"
300
301	if [ "${init}" != "1" ]; then
302		init
303	else
304		cleanup_partial
305	fi
306
307	init_partial
308
309	return 0
310}
311
312# $1: test name ; $2: counter to check
313reset_check_counter()
314{
315	reset "${1}" || return 1
316
317	local counter="${2}"
318
319	if ! nstat -asz "${counter}" | grep -wq "${counter}"; then
320		mark_as_skipped "counter '${counter}' is not available"
321		return 1
322	fi
323}
324
325# $1: test name
326reset_with_cookies()
327{
328	reset "${1}" || return 1
329
330	local netns
331	for netns in "$ns1" "$ns2"; do
332		ip netns exec $netns sysctl -q net.ipv4.tcp_syncookies=2
333	done
334}
335
336# $1: test name
337reset_with_add_addr_timeout()
338{
339	local ip="${2:-4}"
340	local tables
341
342	reset "${1}" || return 1
343
344	tables="${iptables}"
345	if [ $ip -eq 6 ]; then
346		tables="${ip6tables}"
347	fi
348
349	ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
350
351	if ! ip netns exec $ns2 $tables -A OUTPUT -p tcp \
352			-m tcp --tcp-option 30 \
353			-m bpf --bytecode \
354			"$CBPF_MPTCP_SUBOPTION_ADD_ADDR" \
355			-j DROP; then
356		mark_as_skipped "unable to set the 'add addr' rule"
357		return 1
358	fi
359}
360
361# $1: test name
362reset_with_checksum()
363{
364	local ns1_enable=$1
365	local ns2_enable=$2
366
367	reset "checksum test ${ns1_enable} ${ns2_enable}" || return 1
368
369	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=$ns1_enable
370	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=$ns2_enable
371
372	validate_checksum=true
373}
374
375reset_with_allow_join_id0()
376{
377	local ns1_enable=$2
378	local ns2_enable=$3
379
380	reset "${1}" || return 1
381
382	ip netns exec $ns1 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns1_enable
383	ip netns exec $ns2 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns2_enable
384}
385
386# Modify TCP payload without corrupting the TCP packet
387#
388# This rule inverts a 8-bit word at byte offset 148 for the 2nd TCP ACK packets
389# carrying enough data.
390# Once it is done, the TCP Checksum field is updated so the packet is still
391# considered as valid at the TCP level.
392# Because the MPTCP checksum, covering the TCP options and data, has not been
393# updated, the modification will be detected and an MP_FAIL will be emitted:
394# what we want to validate here without corrupting "random" MPTCP options.
395#
396# To avoid having tc producing this pr_info() message for each TCP ACK packets
397# not carrying enough data:
398#
399#     tc action pedit offset 162 out of bounds
400#
401# Netfilter is used to mark packets with enough data.
402setup_fail_rules()
403{
404	check_invert=1
405	validate_checksum=true
406	local i="$1"
407	local ip="${2:-4}"
408	local tables
409
410	tables="${iptables}"
411	if [ $ip -eq 6 ]; then
412		tables="${ip6tables}"
413	fi
414
415	ip netns exec $ns2 $tables \
416		-t mangle \
417		-A OUTPUT \
418		-o ns2eth$i \
419		-p tcp \
420		-m length --length 150:9999 \
421		-m statistic --mode nth --packet 1 --every 99999 \
422		-j MARK --set-mark 42 || return ${KSFT_SKIP}
423
424	tc -n $ns2 qdisc add dev ns2eth$i clsact || return ${KSFT_SKIP}
425	tc -n $ns2 filter add dev ns2eth$i egress \
426		protocol ip prio 1000 \
427		handle 42 fw \
428		action pedit munge offset 148 u8 invert \
429		pipe csum tcp \
430		index 100 || return ${KSFT_SKIP}
431}
432
433reset_with_fail()
434{
435	reset_check_counter "${1}" "MPTcpExtInfiniteMapTx" || return 1
436	shift
437
438	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=1
439	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=1
440
441	local rc=0
442	setup_fail_rules "${@}" || rc=$?
443
444	if [ ${rc} -eq ${KSFT_SKIP} ]; then
445		mark_as_skipped "unable to set the 'fail' rules"
446		return 1
447	fi
448}
449
450start_events()
451{
452	mptcp_lib_events "${ns1}" "${evts_ns1}" evts_ns1_pid
453	mptcp_lib_events "${ns2}" "${evts_ns2}" evts_ns2_pid
454}
455
456reset_with_events()
457{
458	reset "${1}" || return 1
459
460	start_events
461}
462
463reset_with_tcp_filter()
464{
465	reset "${1}" || return 1
466	shift
467
468	local ns="${!1}"
469	local src="${2}"
470	local target="${3}"
471	local chain="${4:-INPUT}"
472
473	if ! ip netns exec "${ns}" ${iptables} \
474			-A "${chain}" \
475			-s "${src}" \
476			-p tcp \
477			-j "${target}"; then
478		mark_as_skipped "unable to set the filter rules"
479		return 1
480	fi
481}
482
483# $1: err msg
484fail_test()
485{
486	if ! mptcp_lib_subtest_is_flaky; then
487		ret=${KSFT_FAIL}
488	fi
489
490	if [ ${#} -gt 0 ]; then
491		print_fail "${@}"
492	fi
493
494	# just in case a test is marked twice as failed
495	if [ ${last_test_failed} -eq 0 ]; then
496		failed_tests[${MPTCP_LIB_TEST_COUNTER}]="${TEST_NAME}"
497		dump_stats
498		last_test_failed=1
499	fi
500}
501
502get_failed_tests_ids()
503{
504	# sorted
505	local i
506	for i in "${!failed_tests[@]}"; do
507		echo "${i}"
508	done | sort -n
509}
510
511check_transfer()
512{
513	local in=$1
514	local out=$2
515	local what=$3
516	local bytes=$4
517	local i a b
518
519	local line
520	if [ -n "$bytes" ]; then
521		local out_size
522		# when truncating we must check the size explicitly
523		out_size=$(wc -c $out | awk '{print $1}')
524		if [ $out_size -ne $bytes ]; then
525			fail_test "$what output file has wrong size ($out_size, $bytes)"
526			return 1
527		fi
528
529		# note: BusyBox's "cmp" command doesn't support --bytes
530		tmpfile=$(mktemp)
531		head --bytes="$bytes" "$in" > "$tmpfile"
532		mv "$tmpfile" "$in"
533		head --bytes="$bytes" "$out" > "$tmpfile"
534		mv "$tmpfile" "$out"
535		tmpfile=""
536	fi
537	cmp -l "$in" "$out" | while read -r i a b; do
538		local sum=$((0${a} + 0${b}))
539		if [ $check_invert -eq 0 ] || [ $sum -ne $((0xff)) ]; then
540			fail_test "$what does not match (in, out):"
541			mptcp_lib_print_file_err "$in"
542			mptcp_lib_print_file_err "$out"
543
544			return 1
545		else
546			print_info "$what has inverted byte at ${i}"
547		fi
548	done
549
550	return 0
551}
552
553do_ping()
554{
555	local listener_ns="$1"
556	local connector_ns="$2"
557	local connect_addr="$3"
558
559	if ! ip netns exec ${connector_ns} ping -q -c 1 $connect_addr >/dev/null; then
560		fail_test "$listener_ns -> $connect_addr connectivity"
561	fi
562}
563
564link_failure()
565{
566	local ns="$1"
567
568	if [ -z "$FAILING_LINKS" ]; then
569		l=$((RANDOM%4))
570		FAILING_LINKS=$((l+1))
571	fi
572
573	local l
574	for l in $FAILING_LINKS; do
575		local veth="ns1eth$l"
576		ip -net "$ns" link set "$veth" down
577	done
578}
579
580rm_addr_count()
581{
582	mptcp_lib_get_counter "${1}" "MPTcpExtRmAddr"
583}
584
585# $1: ns, $2: old rm_addr counter in $ns
586wait_rm_addr()
587{
588	local ns="${1}"
589	local old_cnt="${2}"
590	local cnt
591
592	local i
593	for i in $(seq 10); do
594		cnt=$(rm_addr_count ${ns})
595		[ "$cnt" = "${old_cnt}" ] || break
596		sleep 0.1
597	done
598}
599
600rm_sf_count()
601{
602	mptcp_lib_get_counter "${1}" "MPTcpExtRmSubflow"
603}
604
605# $1: ns, $2: old rm_sf counter in $ns
606wait_rm_sf()
607{
608	local ns="${1}"
609	local old_cnt="${2}"
610	local cnt
611
612	local i
613	for i in $(seq 10); do
614		cnt=$(rm_sf_count ${ns})
615		[ "$cnt" = "${old_cnt}" ] || break
616		sleep 0.1
617	done
618}
619
620wait_mpj()
621{
622	local ns="${1}"
623	local cnt old_cnt
624
625	old_cnt=$(mptcp_lib_get_counter ${ns} "MPTcpExtMPJoinAckRx")
626
627	local i
628	for i in $(seq 10); do
629		cnt=$(mptcp_lib_get_counter ${ns} "MPTcpExtMPJoinAckRx")
630		[ "$cnt" = "${old_cnt}" ] || break
631		sleep 0.1
632	done
633}
634
635kill_events_pids()
636{
637	mptcp_lib_kill_wait $evts_ns1_pid
638	evts_ns1_pid=0
639	mptcp_lib_kill_wait $evts_ns2_pid
640	evts_ns2_pid=0
641}
642
643pm_nl_set_limits()
644{
645	mptcp_lib_pm_nl_set_limits "${@}"
646}
647
648pm_nl_add_endpoint()
649{
650	mptcp_lib_pm_nl_add_endpoint "${@}"
651}
652
653pm_nl_del_endpoint()
654{
655	mptcp_lib_pm_nl_del_endpoint "${@}"
656}
657
658pm_nl_flush_endpoint()
659{
660	mptcp_lib_pm_nl_flush_endpoint "${@}"
661}
662
663pm_nl_show_endpoints()
664{
665	mptcp_lib_pm_nl_show_endpoints "${@}"
666}
667
668pm_nl_change_endpoint()
669{
670	mptcp_lib_pm_nl_change_endpoint "${@}"
671}
672
673pm_nl_check_endpoint()
674{
675	local msg="$1"
676	local ns=$2
677	local addr=$3
678	local flags dev id port
679
680	print_check "${msg}"
681
682	shift 3
683	while [ -n "$1" ]; do
684		case "${1}" in
685		"flags" | "dev" | "id" | "port")
686			eval "${1}"="${2}"
687			shift
688			;;
689		*)
690			;;
691		esac
692
693		shift
694	done
695
696	if [ -z "${id}" ]; then
697		fail_test "bad test - missing endpoint id"
698		return
699	fi
700
701	check_output "mptcp_lib_pm_nl_get_endpoint ${ns} ${id}" \
702		"$(mptcp_lib_pm_nl_format_endpoints \
703			"${id},${addr},${flags//","/" "},${dev},${port}")"
704}
705
706pm_nl_set_endpoint()
707{
708	local listener_ns="$1"
709	local connector_ns="$2"
710	local connect_addr="$3"
711
712	local addr_nr_ns1=${addr_nr_ns1:-0}
713	local addr_nr_ns2=${addr_nr_ns2:-0}
714	local sflags=${sflags:-""}
715	local fullmesh=${fullmesh:-""}
716
717	local flags="subflow"
718	if [ -n "${fullmesh}" ]; then
719		flags="${flags},fullmesh"
720		addr_nr_ns2=${fullmesh}
721	fi
722
723	# let the mptcp subflow be established in background before
724	# do endpoint manipulation
725	if [ $addr_nr_ns1 != "0" ] || [ $addr_nr_ns2 != "0" ]; then
726		sleep 1
727	fi
728
729	if [ $addr_nr_ns1 -gt 0 ]; then
730		local counter=2
731		local add_nr_ns1=${addr_nr_ns1}
732		local id=10
733		while [ $add_nr_ns1 -gt 0 ]; do
734			local addr
735			if mptcp_lib_is_v6 "${connect_addr}"; then
736				addr="dead:beef:$counter::1"
737			else
738				addr="10.0.$counter.1"
739			fi
740			pm_nl_add_endpoint $ns1 $addr flags signal
741			counter=$((counter + 1))
742			add_nr_ns1=$((add_nr_ns1 - 1))
743			id=$((id + 1))
744		done
745	elif [ $addr_nr_ns1 -lt 0 ]; then
746		local rm_nr_ns1=$((-addr_nr_ns1))
747		if [ $rm_nr_ns1 -lt 8 ]; then
748			local counter=0
749			local line
750			pm_nl_show_endpoints ${listener_ns} | while read -r line; do
751				# shellcheck disable=SC2206 # we do want to split per word
752				local arr=($line)
753				local nr=0
754
755				local i
756				for i in "${arr[@]}"; do
757					if [ $i = "id" ]; then
758						if [ $counter -eq $rm_nr_ns1 ]; then
759							break
760						fi
761						id=${arr[$nr+1]}
762						rm_addr=$(rm_addr_count ${connector_ns})
763						pm_nl_del_endpoint ${listener_ns} $id
764						wait_rm_addr ${connector_ns} ${rm_addr}
765						counter=$((counter + 1))
766					fi
767					nr=$((nr + 1))
768				done
769			done
770		elif [ $rm_nr_ns1 -eq 8 ]; then
771			pm_nl_flush_endpoint ${listener_ns}
772		elif [ $rm_nr_ns1 -eq 9 ]; then
773			pm_nl_del_endpoint ${listener_ns} 0 ${connect_addr}
774		fi
775	fi
776
777	# if newly added endpoints must be deleted, give the background msk
778	# some time to created them
779	[ $addr_nr_ns1 -gt 0 ] && [ $addr_nr_ns2 -lt 0 ] && sleep 1
780
781	if [ $addr_nr_ns2 -gt 0 ]; then
782		local add_nr_ns2=${addr_nr_ns2}
783		local counter=3
784		local id=20
785		while [ $add_nr_ns2 -gt 0 ]; do
786			local addr
787			if mptcp_lib_is_v6 "${connect_addr}"; then
788				addr="dead:beef:$counter::2"
789			else
790				addr="10.0.$counter.2"
791			fi
792			pm_nl_add_endpoint $ns2 $addr flags $flags
793			counter=$((counter + 1))
794			add_nr_ns2=$((add_nr_ns2 - 1))
795			id=$((id + 1))
796		done
797	elif [ $addr_nr_ns2 -lt 0 ]; then
798		local rm_nr_ns2=$((-addr_nr_ns2))
799		if [ $rm_nr_ns2 -lt 8 ]; then
800			local counter=0
801			local line
802			pm_nl_show_endpoints ${connector_ns} | while read -r line; do
803				# shellcheck disable=SC2206 # we do want to split per word
804				local arr=($line)
805				local nr=0
806
807				local i
808				for i in "${arr[@]}"; do
809					if [ $i = "id" ]; then
810						if [ $counter -eq $rm_nr_ns2 ]; then
811							break
812						fi
813						local id rm_addr
814						# rm_addr are serialized, allow the previous one to
815						# complete
816						id=${arr[$nr+1]}
817						rm_addr=$(rm_addr_count ${listener_ns})
818						pm_nl_del_endpoint ${connector_ns} $id
819						wait_rm_addr ${listener_ns} ${rm_addr}
820						counter=$((counter + 1))
821					fi
822					nr=$((nr + 1))
823				done
824			done
825		elif [ $rm_nr_ns2 -eq 8 ]; then
826			pm_nl_flush_endpoint ${connector_ns}
827		elif [ $rm_nr_ns2 -eq 9 ]; then
828			local addr
829			if mptcp_lib_is_v6 "${connect_addr}"; then
830				addr="dead:beef:1::2"
831			else
832				addr="10.0.1.2"
833			fi
834			pm_nl_del_endpoint ${connector_ns} 0 $addr
835		fi
836	fi
837
838	if [ -n "${sflags}" ]; then
839		sleep 1
840
841		local netns
842		for netns in "$ns1" "$ns2"; do
843			local line
844			pm_nl_show_endpoints $netns | while read -r line; do
845				# shellcheck disable=SC2206 # we do want to split per word
846				local arr=($line)
847				local nr=0
848				local id
849
850				local i
851				for i in "${arr[@]}"; do
852					if [ $i = "id" ]; then
853						id=${arr[$nr+1]}
854					fi
855					nr=$((nr + 1))
856				done
857				pm_nl_change_endpoint $netns $id $sflags
858			done
859		done
860	fi
861}
862
863chk_cestab_nr()
864{
865	local ns=$1
866	local cestab=$2
867	local count
868
869	print_check "currently established: $cestab"
870	count=$(mptcp_lib_get_counter ${ns} "MPTcpExtMPCurrEstab")
871	if [ -z "$count" ]; then
872		print_skip
873	elif [ "$count" != "$cestab" ]; then
874		fail_test "got $count current establish[s] expected $cestab"
875	else
876		print_ok
877	fi
878}
879
880# $1 namespace 1, $2 namespace 2
881check_cestab()
882{
883	if [ -n "${cestab_ns1}" ]; then
884		chk_cestab_nr ${1} ${cestab_ns1}
885	fi
886	if [ -n "${cestab_ns2}" ]; then
887		chk_cestab_nr ${2} ${cestab_ns2}
888	fi
889}
890
891cond_start_capture()
892{
893	local ns="$1"
894
895	:> "$capout"
896
897	if $capture; then
898		local capuser capfile
899		if [ -z $SUDO_USER ]; then
900			capuser=""
901		else
902			capuser="-Z $SUDO_USER"
903		fi
904
905		capfile=$(printf "mp_join-%02u-%s.pcap" "$MPTCP_LIB_TEST_COUNTER" "$ns")
906
907		echo "Capturing traffic for test $MPTCP_LIB_TEST_COUNTER into $capfile"
908		ip netns exec "$ns" tcpdump -i any -s 65535 -B 32768 $capuser -w "$capfile" > "$capout" 2>&1 &
909		cappid=$!
910
911		sleep 1
912	fi
913}
914
915cond_stop_capture()
916{
917	if $capture; then
918		sleep 1
919		kill $cappid
920		cat "$capout"
921	fi
922}
923
924get_port()
925{
926	echo "$((10000 + MPTCP_LIB_TEST_COUNTER - 1))"
927}
928
929do_transfer()
930{
931	local listener_ns="$1"
932	local connector_ns="$2"
933	local cl_proto="$3"
934	local srv_proto="$4"
935	local connect_addr="$5"
936	local port
937
938	local FAILING_LINKS=${FAILING_LINKS:-""}
939	local fastclose=${fastclose:-""}
940	local speed=${speed:-"fast"}
941	port=$(get_port)
942
943	:> "$cout"
944	:> "$sout"
945
946	cond_start_capture ${listener_ns}
947
948	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
949		nstat -n
950	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
951		nstat -n
952
953	local extra_args
954	if [ $speed = "fast" ]; then
955		extra_args="-j"
956	elif [ $speed = "slow" ]; then
957		extra_args="-r 50"
958	elif [ $speed -gt 0 ]; then
959		extra_args="-r ${speed}"
960	fi
961
962	local extra_cl_args=""
963	local extra_srv_args=""
964	local trunc_size=""
965	if [ -n "${fastclose}" ]; then
966		if [ ${test_linkfail} -le 1 ]; then
967			fail_test "fastclose tests need test_linkfail argument"
968			return 1
969		fi
970
971		# disconnect
972		trunc_size=${test_linkfail}
973		local side=${fastclose}
974
975		if [ ${side} = "client" ]; then
976			extra_cl_args="-f ${test_linkfail}"
977			extra_srv_args="-f -1"
978		elif [ ${side} = "server" ]; then
979			extra_srv_args="-f ${test_linkfail}"
980			extra_cl_args="-f -1"
981		else
982			fail_test "wrong/unknown fastclose spec ${side}"
983			return 1
984		fi
985	fi
986
987	extra_srv_args="$extra_args $extra_srv_args"
988	if [ "$test_linkfail" -gt 1 ];then
989		timeout ${timeout_test} \
990			ip netns exec ${listener_ns} \
991				./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
992					$extra_srv_args "::" < "$sinfail" > "$sout" &
993	else
994		timeout ${timeout_test} \
995			ip netns exec ${listener_ns} \
996				./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
997					$extra_srv_args "::" < "$sin" > "$sout" &
998	fi
999	local spid=$!
1000
1001	mptcp_lib_wait_local_port_listen "${listener_ns}" "${port}"
1002
1003	extra_cl_args="$extra_args $extra_cl_args"
1004	if [ "$test_linkfail" -eq 0 ];then
1005		timeout ${timeout_test} \
1006			ip netns exec ${connector_ns} \
1007				./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
1008					$extra_cl_args $connect_addr < "$cin" > "$cout" &
1009	elif [ "$test_linkfail" -eq 1 ] || [ "$test_linkfail" -eq 2 ];then
1010		( cat "$cinfail" ; sleep 2; link_failure $listener_ns ; cat "$cinfail" ) | \
1011			tee "$cinsent" | \
1012			timeout ${timeout_test} \
1013				ip netns exec ${connector_ns} \
1014					./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
1015						$extra_cl_args $connect_addr > "$cout" &
1016	else
1017		tee "$cinsent" < "$cinfail" | \
1018			timeout ${timeout_test} \
1019				ip netns exec ${connector_ns} \
1020					./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
1021						$extra_cl_args $connect_addr > "$cout" &
1022	fi
1023	local cpid=$!
1024
1025	pm_nl_set_endpoint $listener_ns $connector_ns $connect_addr
1026	check_cestab $listener_ns $connector_ns
1027
1028	wait $cpid
1029	local retc=$?
1030	wait $spid
1031	local rets=$?
1032
1033	cond_stop_capture
1034
1035	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
1036		nstat | grep Tcp > /tmp/${listener_ns}.out
1037	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
1038		nstat | grep Tcp > /tmp/${connector_ns}.out
1039
1040	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
1041		fail_test "client exit code $retc, server $rets"
1042		echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
1043		ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
1044		cat /tmp/${listener_ns}.out
1045		echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
1046		ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
1047		cat /tmp/${connector_ns}.out
1048
1049		return 1
1050	fi
1051
1052	if [ "$test_linkfail" -gt 1 ];then
1053		check_transfer $sinfail $cout "file received by client" $trunc_size
1054	else
1055		check_transfer $sin $cout "file received by client" $trunc_size
1056	fi
1057	retc=$?
1058	if [ "$test_linkfail" -eq 0 ];then
1059		check_transfer $cin $sout "file received by server" $trunc_size
1060	else
1061		check_transfer $cinsent $sout "file received by server" $trunc_size
1062	fi
1063	rets=$?
1064
1065	[ $retc -eq 0 ] && [ $rets -eq 0 ]
1066}
1067
1068make_file()
1069{
1070	local name=$1
1071	local who=$2
1072	local size=$3
1073
1074	mptcp_lib_make_file $name 1024 $size
1075
1076	print_info "Test file (size $size KB) for $who"
1077}
1078
1079run_tests()
1080{
1081	local listener_ns="$1"
1082	local connector_ns="$2"
1083	local connect_addr="$3"
1084
1085	local size
1086	local test_linkfail=${test_linkfail:-0}
1087
1088	# The values above 2 are reused to make test files
1089	# with the given sizes (KB)
1090	if [ "$test_linkfail" -gt 2 ]; then
1091		size=$test_linkfail
1092
1093		if [ -z "$cinfail" ]; then
1094			cinfail=$(mktemp)
1095		fi
1096		make_file "$cinfail" "client" $size
1097	# create the input file for the failure test when
1098	# the first failure test run
1099	elif [ "$test_linkfail" -ne 0 ] && [ -z "$cinfail" ]; then
1100		# the client file must be considerably larger
1101		# of the maximum expected cwin value, or the
1102		# link utilization will be not predicable
1103		size=$((RANDOM%2))
1104		size=$((size+1))
1105		size=$((size*8192))
1106		size=$((size + ( RANDOM % 8192) ))
1107
1108		cinfail=$(mktemp)
1109		make_file "$cinfail" "client" $size
1110	fi
1111
1112	if [ "$test_linkfail" -gt 2 ]; then
1113		size=$test_linkfail
1114
1115		if [ -z "$sinfail" ]; then
1116			sinfail=$(mktemp)
1117		fi
1118		make_file "$sinfail" "server" $size
1119	elif [ "$test_linkfail" -eq 2 ] && [ -z "$sinfail" ]; then
1120		size=$((RANDOM%16))
1121		size=$((size+1))
1122		size=$((size*2048))
1123
1124		sinfail=$(mktemp)
1125		make_file "$sinfail" "server" $size
1126	fi
1127
1128	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr}
1129}
1130
1131dump_stats()
1132{
1133	echo Server ns stats
1134	ip netns exec $ns1 nstat -as | grep Tcp
1135	echo Client ns stats
1136	ip netns exec $ns2 nstat -as | grep Tcp
1137}
1138
1139chk_csum_nr()
1140{
1141	local csum_ns1=${1:-0}
1142	local csum_ns2=${2:-0}
1143	local count
1144	local extra_msg=""
1145	local allow_multi_errors_ns1=0
1146	local allow_multi_errors_ns2=0
1147
1148	if [[ "${csum_ns1}" = "+"* ]]; then
1149		allow_multi_errors_ns1=1
1150		csum_ns1=${csum_ns1:1}
1151	fi
1152	if [[ "${csum_ns2}" = "+"* ]]; then
1153		allow_multi_errors_ns2=1
1154		csum_ns2=${csum_ns2:1}
1155	fi
1156
1157	print_check "checksum server"
1158	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtDataCsumErr")
1159	if [ -n "$count" ] && [ "$count" != "$csum_ns1" ]; then
1160		extra_msg+=" ns1=$count"
1161	fi
1162	if [ -z "$count" ]; then
1163		print_skip
1164	elif { [ "$count" != $csum_ns1 ] && [ $allow_multi_errors_ns1 -eq 0 ]; } ||
1165	     { [ "$count" -lt $csum_ns1 ] && [ $allow_multi_errors_ns1 -eq 1 ]; }; then
1166		fail_test "got $count data checksum error[s] expected $csum_ns1"
1167	else
1168		print_ok
1169	fi
1170
1171	print_check "checksum client"
1172	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtDataCsumErr")
1173	if [ -n "$count" ] && [ "$count" != "$csum_ns2" ]; then
1174		extra_msg+=" ns2=$count"
1175	fi
1176	if [ -z "$count" ]; then
1177		print_skip
1178	elif { [ "$count" != $csum_ns2 ] && [ $allow_multi_errors_ns2 -eq 0 ]; } ||
1179	     { [ "$count" -lt $csum_ns2 ] && [ $allow_multi_errors_ns2 -eq 1 ]; }; then
1180		fail_test "got $count data checksum error[s] expected $csum_ns2"
1181	else
1182		print_ok
1183	fi
1184
1185	print_info "$extra_msg"
1186}
1187
1188chk_fail_nr()
1189{
1190	local fail_tx=$1
1191	local fail_rx=$2
1192	local ns_invert=${3:-""}
1193	local count
1194	local ns_tx=$ns1
1195	local ns_rx=$ns2
1196	local tx="server"
1197	local rx="client"
1198	local extra_msg=""
1199	local allow_tx_lost=0
1200	local allow_rx_lost=0
1201
1202	if [[ $ns_invert = "invert" ]]; then
1203		ns_tx=$ns2
1204		ns_rx=$ns1
1205		tx="client"
1206		rx="server"
1207	fi
1208
1209	if [[ "${fail_tx}" = "-"* ]]; then
1210		allow_tx_lost=1
1211		fail_tx=${fail_tx:1}
1212	fi
1213	if [[ "${fail_rx}" = "-"* ]]; then
1214		allow_rx_lost=1
1215		fail_rx=${fail_rx:1}
1216	fi
1217
1218	print_check "fail tx ${tx}"
1219	count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMPFailTx")
1220	if [ -n "$count" ] && [ "$count" != "$fail_tx" ]; then
1221		extra_msg+=" tx=$count"
1222	fi
1223	if [ -z "$count" ]; then
1224		print_skip
1225	elif { [ "$count" != "$fail_tx" ] && [ $allow_tx_lost -eq 0 ]; } ||
1226	     { [ "$count" -gt "$fail_tx" ] && [ $allow_tx_lost -eq 1 ]; }; then
1227		fail_test "got $count MP_FAIL[s] TX expected $fail_tx"
1228	else
1229		print_ok
1230	fi
1231
1232	print_check "fail rx ${rx}"
1233	count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtMPFailRx")
1234	if [ -n "$count" ] && [ "$count" != "$fail_rx" ]; then
1235		extra_msg+=" rx=$count"
1236	fi
1237	if [ -z "$count" ]; then
1238		print_skip
1239	elif { [ "$count" != "$fail_rx" ] && [ $allow_rx_lost -eq 0 ]; } ||
1240	     { [ "$count" -gt "$fail_rx" ] && [ $allow_rx_lost -eq 1 ]; }; then
1241		fail_test "got $count MP_FAIL[s] RX expected $fail_rx"
1242	else
1243		print_ok
1244	fi
1245
1246	print_info "$extra_msg"
1247}
1248
1249chk_fclose_nr()
1250{
1251	local fclose_tx=$1
1252	local fclose_rx=$2
1253	local ns_invert=$3
1254	local count
1255	local ns_tx=$ns2
1256	local ns_rx=$ns1
1257	local tx="client"
1258	local rx="server"
1259
1260	if [[ $ns_invert = "invert" ]]; then
1261		ns_tx=$ns1
1262		ns_rx=$ns2
1263		tx="server"
1264		rx="client"
1265	fi
1266
1267	print_check "fast close tx ${tx}"
1268	count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMPFastcloseTx")
1269	if [ -z "$count" ]; then
1270		print_skip
1271	elif [ "$count" != "$fclose_tx" ]; then
1272		fail_test "got $count MP_FASTCLOSE[s] TX expected $fclose_tx"
1273	else
1274		print_ok
1275	fi
1276
1277	print_check "fast close rx ${rx}"
1278	count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtMPFastcloseRx")
1279	if [ -z "$count" ]; then
1280		print_skip
1281	elif [ "$count" != "$fclose_rx" ]; then
1282		fail_test "got $count MP_FASTCLOSE[s] RX expected $fclose_rx"
1283	else
1284		print_ok
1285	fi
1286}
1287
1288chk_rst_nr()
1289{
1290	local rst_tx=$1
1291	local rst_rx=$2
1292	local ns_invert=${3:-""}
1293	local count
1294	local ns_tx=$ns1
1295	local ns_rx=$ns2
1296	local tx="server"
1297	local rx="client"
1298
1299	if [[ $ns_invert = "invert" ]]; then
1300		ns_tx=$ns2
1301		ns_rx=$ns1
1302		tx="client"
1303		rx="server"
1304	fi
1305
1306	print_check "reset tx ${tx}"
1307	count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMPRstTx")
1308	if [ -z "$count" ]; then
1309		print_skip
1310	# accept more rst than expected except if we don't expect any
1311	elif { [ $rst_tx -ne 0 ] && [ $count -lt $rst_tx ]; } ||
1312	     { [ $rst_tx -eq 0 ] && [ $count -ne 0 ]; }; then
1313		fail_test "got $count MP_RST[s] TX expected $rst_tx"
1314	else
1315		print_ok
1316	fi
1317
1318	print_check "reset rx ${rx}"
1319	count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtMPRstRx")
1320	if [ -z "$count" ]; then
1321		print_skip
1322	# accept more rst than expected except if we don't expect any
1323	elif { [ $rst_rx -ne 0 ] && [ $count -lt $rst_rx ]; } ||
1324	     { [ $rst_rx -eq 0 ] && [ $count -ne 0 ]; }; then
1325		fail_test "got $count MP_RST[s] RX expected $rst_rx"
1326	else
1327		print_ok
1328	fi
1329}
1330
1331chk_infi_nr()
1332{
1333	local infi_tx=$1
1334	local infi_rx=$2
1335	local count
1336
1337	print_check "infi tx client"
1338	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtInfiniteMapTx")
1339	if [ -z "$count" ]; then
1340		print_skip
1341	elif [ "$count" != "$infi_tx" ]; then
1342		fail_test "got $count infinite map[s] TX expected $infi_tx"
1343	else
1344		print_ok
1345	fi
1346
1347	print_check "infi rx server"
1348	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtInfiniteMapRx")
1349	if [ -z "$count" ]; then
1350		print_skip
1351	elif [ "$count" != "$infi_rx" ]; then
1352		fail_test "got $count infinite map[s] RX expected $infi_rx"
1353	else
1354		print_ok
1355	fi
1356}
1357
1358chk_join_tx_nr()
1359{
1360	local syn_tx=${join_syn_tx:-0}
1361	local create=${join_create_err:-0}
1362	local bind=${join_bind_err:-0}
1363	local connect=${join_connect_err:-0}
1364	local rc=${KSFT_PASS}
1365	local count
1366
1367	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtMPJoinSynTx")
1368	if [ -z "$count" ]; then
1369		rc=${KSFT_SKIP}
1370	elif [ "$count" != "$syn_tx" ]; then
1371		rc=${KSFT_FAIL}
1372		print_check "syn tx"
1373		fail_test "got $count JOIN[s] syn tx expected $syn_tx"
1374	fi
1375
1376	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtMPJoinSynTxCreatSkErr")
1377	if [ -z "$count" ]; then
1378		rc=${KSFT_SKIP}
1379	elif [ "$count" != "$create" ]; then
1380		rc=${KSFT_FAIL}
1381		print_check "syn tx create socket error"
1382		fail_test "got $count JOIN[s] syn tx create socket error expected $create"
1383	fi
1384
1385	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtMPJoinSynTxBindErr")
1386	if [ -z "$count" ]; then
1387		rc=${KSFT_SKIP}
1388	elif [ "$count" != "$bind" ]; then
1389		rc=${KSFT_FAIL}
1390		print_check "syn tx bind error"
1391		fail_test "got $count JOIN[s] syn tx bind error expected $bind"
1392	fi
1393
1394	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtMPJoinSynTxConnectErr")
1395	if [ -z "$count" ]; then
1396		rc=${KSFT_SKIP}
1397	elif [ "$count" != "$connect" ]; then
1398		rc=${KSFT_FAIL}
1399		print_check "syn tx connect error"
1400		fail_test "got $count JOIN[s] syn tx connect error expected $connect"
1401	fi
1402
1403	print_results "join Tx" ${rc}
1404}
1405
1406chk_join_nr()
1407{
1408	local syn_nr=$1
1409	local syn_ack_nr=$2
1410	local ack_nr=$3
1411	local csum_ns1=${join_csum_ns1:-0}
1412	local csum_ns2=${join_csum_ns2:-0}
1413	local fail_nr=${join_fail_nr:-0}
1414	local rst_nr=${join_rst_nr:-0}
1415	local infi_nr=${join_infi_nr:-0}
1416	local corrupted_pkts=${join_corrupted_pkts:-0}
1417	local rc=${KSFT_PASS}
1418	local count
1419	local with_cookie
1420
1421	if [ "${corrupted_pkts}" -gt 0 ]; then
1422		print_info "${corrupted_pkts} corrupted pkts"
1423	fi
1424
1425	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtMPJoinSynRx")
1426	if [ -z "$count" ]; then
1427		rc=${KSFT_SKIP}
1428	elif [ "$count" != "$syn_nr" ]; then
1429		rc=${KSFT_FAIL}
1430		print_check "syn rx"
1431		fail_test "got $count JOIN[s] syn rx expected $syn_nr"
1432	fi
1433
1434	with_cookie=$(ip netns exec $ns2 sysctl -n net.ipv4.tcp_syncookies)
1435	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtMPJoinSynAckRx")
1436	if [ -z "$count" ]; then
1437		rc=${KSFT_SKIP}
1438	elif [ "$count" != "$syn_ack_nr" ]; then
1439		# simult connections exceeding the limit with cookie enabled could go up to
1440		# synack validation as the conn limit can be enforced reliably only after
1441		# the subflow creation
1442		if [ "$with_cookie" != 2 ] || [ "$count" -le "$syn_ack_nr" ] || [ "$count" -gt "$syn_nr" ]; then
1443			rc=${KSFT_FAIL}
1444			print_check "synack rx"
1445			fail_test "got $count JOIN[s] synack rx expected $syn_ack_nr"
1446		fi
1447	fi
1448
1449	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtMPJoinAckRx")
1450	if [ -z "$count" ]; then
1451		rc=${KSFT_SKIP}
1452	elif [ "$count" != "$ack_nr" ]; then
1453		rc=${KSFT_FAIL}
1454		print_check "ack rx"
1455		fail_test "got $count JOIN[s] ack rx expected $ack_nr"
1456	fi
1457
1458	print_results "join Rx" ${rc}
1459
1460	join_syn_tx="${join_syn_tx:-${syn_nr}}" \
1461		chk_join_tx_nr
1462
1463	if $validate_checksum; then
1464		chk_csum_nr $csum_ns1 $csum_ns2
1465		chk_fail_nr $fail_nr $fail_nr
1466		chk_rst_nr $rst_nr $rst_nr
1467		chk_infi_nr $infi_nr $infi_nr
1468	fi
1469}
1470
1471# a negative value for 'stale_max' means no upper bound:
1472# for bidirectional transfer, if one peer sleep for a while
1473# - as these tests do - we can have a quite high number of
1474# stale/recover conversions, proportional to
1475# sleep duration/ MPTCP-level RTX interval.
1476chk_stale_nr()
1477{
1478	local ns=$1
1479	local stale_min=$2
1480	local stale_max=$3
1481	local stale_delta=$4
1482	local dump_stats
1483	local stale_nr
1484	local recover_nr
1485
1486	print_check "stale"
1487
1488	stale_nr=$(mptcp_lib_get_counter ${ns} "MPTcpExtSubflowStale")
1489	recover_nr=$(mptcp_lib_get_counter ${ns} "MPTcpExtSubflowRecover")
1490	if [ -z "$stale_nr" ] || [ -z "$recover_nr" ]; then
1491		print_skip
1492	elif [ $stale_nr -lt $stale_min ] ||
1493	   { [ $stale_max -gt 0 ] && [ $stale_nr -gt $stale_max ]; } ||
1494	   [ $((stale_nr - recover_nr)) -ne $stale_delta ]; then
1495		fail_test "got $stale_nr stale[s] $recover_nr recover[s], " \
1496		     " expected stale in range [$stale_min..$stale_max]," \
1497		     " stale-recover delta $stale_delta"
1498		dump_stats=1
1499	else
1500		print_ok
1501	fi
1502
1503	if [ "${dump_stats}" = 1 ]; then
1504		echo $ns stats
1505		ip netns exec $ns ip -s link show
1506		ip netns exec $ns nstat -as | grep MPTcp
1507	fi
1508}
1509
1510chk_add_nr()
1511{
1512	local add_nr=$1
1513	local echo_nr=$2
1514	local port_nr=${3:-0}
1515	local ns_invert=${4:-""}
1516	local syn_nr=$port_nr
1517	local syn_ack_nr=$port_nr
1518	local ack_nr=$port_nr
1519	local mis_syn_nr=0
1520	local mis_ack_nr=0
1521	local ns_tx=$ns1
1522	local ns_rx=$ns2
1523	local tx=""
1524	local rx=""
1525	local count
1526	local timeout
1527
1528	if [[ $ns_invert = "invert" ]]; then
1529		ns_tx=$ns2
1530		ns_rx=$ns1
1531		tx=" client"
1532		rx=" server"
1533	fi
1534
1535	timeout=$(ip netns exec ${ns_tx} sysctl -n net.mptcp.add_addr_timeout)
1536
1537	print_check "add addr rx${rx}"
1538	count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtAddAddr")
1539	if [ -z "$count" ]; then
1540		print_skip
1541	# if the test configured a short timeout tolerate greater then expected
1542	# add addrs options, due to retransmissions
1543	elif [ "$count" != "$add_nr" ] && { [ "$timeout" -gt 1 ] || [ "$count" -lt "$add_nr" ]; }; then
1544		fail_test "got $count ADD_ADDR[s] expected $add_nr"
1545	else
1546		print_ok
1547	fi
1548
1549	print_check "add addr echo rx${tx}"
1550	count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtEchoAdd")
1551	if [ -z "$count" ]; then
1552		print_skip
1553	elif [ "$count" != "$echo_nr" ]; then
1554		fail_test "got $count ADD_ADDR echo[s] expected $echo_nr"
1555	else
1556		print_ok
1557	fi
1558
1559	if [ $port_nr -gt 0 ]; then
1560		print_check "add addr rx with port${rx}"
1561		count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtPortAdd")
1562		if [ -z "$count" ]; then
1563			print_skip
1564		elif [ "$count" != "$port_nr" ]; then
1565			fail_test "got $count ADD_ADDR[s] with a port-number expected $port_nr"
1566		else
1567			print_ok
1568		fi
1569
1570		print_check "syn rx port${tx}"
1571		count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMPJoinPortSynRx")
1572		if [ -z "$count" ]; then
1573			print_skip
1574		elif [ "$count" != "$syn_nr" ]; then
1575			fail_test "got $count JOIN[s] syn with a different \
1576				   port-number expected $syn_nr"
1577		else
1578			print_ok
1579		fi
1580
1581		print_check "synack rx port${rx}"
1582		count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtMPJoinPortSynAckRx")
1583		if [ -z "$count" ]; then
1584			print_skip
1585		elif [ "$count" != "$syn_ack_nr" ]; then
1586			fail_test "got $count JOIN[s] synack with a different \
1587				   port-number expected $syn_ack_nr"
1588		else
1589			print_ok
1590		fi
1591
1592		print_check "ack rx port${tx}"
1593		count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMPJoinPortAckRx")
1594		if [ -z "$count" ]; then
1595			print_skip
1596		elif [ "$count" != "$ack_nr" ]; then
1597			fail_test "got $count JOIN[s] ack with a different \
1598				   port-number expected $ack_nr"
1599		else
1600			print_ok
1601		fi
1602
1603		print_check "syn rx port mismatch${tx}"
1604		count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMismatchPortSynRx")
1605		if [ -z "$count" ]; then
1606			print_skip
1607		elif [ "$count" != "$mis_syn_nr" ]; then
1608			fail_test "got $count JOIN[s] syn with a mismatched \
1609				   port-number expected $mis_syn_nr"
1610		else
1611			print_ok
1612		fi
1613
1614		print_check "ack rx port mismatch${tx}"
1615		count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMismatchPortAckRx")
1616		if [ -z "$count" ]; then
1617			print_skip
1618		elif [ "$count" != "$mis_ack_nr" ]; then
1619			fail_test "got $count JOIN[s] ack with a mismatched \
1620				   port-number expected $mis_ack_nr"
1621		else
1622			print_ok
1623		fi
1624	fi
1625}
1626
1627chk_add_tx_nr()
1628{
1629	local add_tx_nr=$1
1630	local echo_tx_nr=$2
1631	local timeout
1632	local count
1633
1634	timeout=$(ip netns exec $ns1 sysctl -n net.mptcp.add_addr_timeout)
1635
1636	print_check "add addr tx"
1637	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtAddAddrTx")
1638	if [ -z "$count" ]; then
1639		print_skip
1640	# if the test configured a short timeout tolerate greater then expected
1641	# add addrs options, due to retransmissions
1642	elif [ "$count" != "$add_tx_nr" ] && { [ "$timeout" -gt 1 ] || [ "$count" -lt "$add_tx_nr" ]; }; then
1643		fail_test "got $count ADD_ADDR[s] TX, expected $add_tx_nr"
1644	else
1645		print_ok
1646	fi
1647
1648	print_check "add addr echo tx"
1649	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtEchoAddTx")
1650	if [ -z "$count" ]; then
1651		print_skip
1652	elif [ "$count" != "$echo_tx_nr" ]; then
1653		fail_test "got $count ADD_ADDR echo[s] TX, expected $echo_tx_nr"
1654	else
1655		print_ok
1656	fi
1657}
1658
1659chk_rm_nr()
1660{
1661	local rm_addr_nr=$1
1662	local rm_subflow_nr=$2
1663	local invert
1664	local simult
1665	local count
1666	local addr_ns=$ns1
1667	local subflow_ns=$ns2
1668	local addr="server"
1669	local subflow="client"
1670	local extra_msg=""
1671
1672	shift 2
1673	while [ -n "$1" ]; do
1674		[ "$1" = "invert" ] && invert=true
1675		[ "$1" = "simult" ] && simult=true
1676		shift
1677	done
1678
1679	if [ "$invert" = "true" ]; then
1680		addr_ns=$ns2
1681		subflow_ns=$ns1
1682		addr="client"
1683		subflow="server"
1684	fi
1685
1686	print_check "rm addr rx ${addr}"
1687	count=$(mptcp_lib_get_counter ${addr_ns} "MPTcpExtRmAddr")
1688	if [ -z "$count" ]; then
1689		print_skip
1690	elif [ "$count" != "$rm_addr_nr" ]; then
1691		fail_test "got $count RM_ADDR[s] expected $rm_addr_nr"
1692	else
1693		print_ok
1694	fi
1695
1696	print_check "rm subflow ${subflow}"
1697	count=$(mptcp_lib_get_counter ${subflow_ns} "MPTcpExtRmSubflow")
1698	if [ -z "$count" ]; then
1699		print_skip
1700	elif [ -n "$simult" ]; then
1701		local cnt suffix
1702
1703		cnt=$(mptcp_lib_get_counter ${addr_ns} "MPTcpExtRmSubflow")
1704
1705		# in case of simult flush, the subflow removal count on each side is
1706		# unreliable
1707		count=$((count + cnt))
1708		if [ "$count" != "$rm_subflow_nr" ]; then
1709			suffix="$count in [$rm_subflow_nr:$((rm_subflow_nr*2))]"
1710			extra_msg="simult"
1711		fi
1712		if [ $count -ge "$rm_subflow_nr" ] && \
1713		   [ "$count" -le "$((rm_subflow_nr *2 ))" ]; then
1714			print_ok "$suffix"
1715		else
1716			fail_test "got $count RM_SUBFLOW[s] expected in range [$rm_subflow_nr:$((rm_subflow_nr*2))]"
1717		fi
1718	elif [ "$count" != "$rm_subflow_nr" ]; then
1719		fail_test "got $count RM_SUBFLOW[s] expected $rm_subflow_nr"
1720	else
1721		print_ok
1722	fi
1723
1724	print_info "$extra_msg"
1725}
1726
1727chk_rm_tx_nr()
1728{
1729	local rm_addr_tx_nr=$1
1730
1731	print_check "rm addr tx client"
1732	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtRmAddrTx")
1733	if [ -z "$count" ]; then
1734		print_skip
1735	elif [ "$count" != "$rm_addr_tx_nr" ]; then
1736		fail_test "got $count RM_ADDR[s] expected $rm_addr_tx_nr"
1737	else
1738		print_ok
1739	fi
1740}
1741
1742chk_prio_nr()
1743{
1744	local mp_prio_nr_tx=$1
1745	local mp_prio_nr_rx=$2
1746	local mpj_syn=$3
1747	local mpj_syn_ack=$4
1748	local count
1749
1750	print_check "mp_prio tx server"
1751	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtMPPrioTx")
1752	if [ -z "$count" ]; then
1753		print_skip
1754	elif [ "$count" != "$mp_prio_nr_tx" ]; then
1755		fail_test "got $count MP_PRIO[s] TX expected $mp_prio_nr_tx"
1756	else
1757		print_ok
1758	fi
1759
1760	print_check "mp_prio rx client"
1761	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtMPPrioRx")
1762	if [ -z "$count" ]; then
1763		print_skip
1764	elif [ "$count" != "$mp_prio_nr_rx" ]; then
1765		fail_test "got $count MP_PRIO[s] RX expected $mp_prio_nr_rx"
1766	else
1767		print_ok
1768	fi
1769
1770	print_check "syn backup"
1771	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtMPJoinSynBackupRx")
1772	if [ -z "$count" ]; then
1773		print_skip
1774	elif [ "$count" != "$mpj_syn" ]; then
1775		fail_test "got $count JOIN[s] syn with Backup expected $mpj_syn"
1776	else
1777		print_ok
1778	fi
1779
1780	print_check "synack backup"
1781	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtMPJoinSynAckBackupRx")
1782	if [ -z "$count" ]; then
1783		print_skip
1784	elif [ "$count" != "$mpj_syn_ack" ]; then
1785		fail_test "got $count JOIN[s] synack with Backup expected $mpj_syn_ack"
1786	else
1787		print_ok
1788	fi
1789}
1790
1791chk_subflow_nr()
1792{
1793	local msg="$1"
1794	local subflow_nr=$2
1795	local cnt1
1796	local cnt2
1797	local dump_stats
1798
1799	print_check "${msg}"
1800
1801	cnt1=$(ss -N $ns1 -tOni | grep -c token)
1802	cnt2=$(ss -N $ns2 -tOni | grep -c token)
1803	if [ "$cnt1" != "$subflow_nr" ] || [ "$cnt2" != "$subflow_nr" ]; then
1804		fail_test "got $cnt1:$cnt2 subflows expected $subflow_nr"
1805		dump_stats=1
1806	else
1807		print_ok
1808	fi
1809
1810	if [ "${dump_stats}" = 1 ]; then
1811		ss -N $ns1 -tOni
1812		ss -N $ns1 -tOni | grep token
1813		ip -n $ns1 mptcp endpoint
1814	fi
1815}
1816
1817chk_mptcp_info()
1818{
1819	local info1=$1
1820	local exp1=$2
1821	local info2=$3
1822	local exp2=$4
1823	local cnt1
1824	local cnt2
1825	local dump_stats
1826
1827	print_check "mptcp_info ${info1:0:15}=$exp1:$exp2"
1828
1829	cnt1=$(ss -N $ns1 -inmHM | mptcp_lib_get_info_value "$info1" "$info1")
1830	cnt2=$(ss -N $ns2 -inmHM | mptcp_lib_get_info_value "$info2" "$info2")
1831	# 'ss' only display active connections and counters that are not 0.
1832	[ -z "$cnt1" ] && cnt1=0
1833	[ -z "$cnt2" ] && cnt2=0
1834
1835	if [ "$cnt1" != "$exp1" ] || [ "$cnt2" != "$exp2" ]; then
1836		fail_test "got $cnt1:$cnt2 $info1:$info2 expected $exp1:$exp2"
1837		dump_stats=1
1838	else
1839		print_ok
1840	fi
1841
1842	if [ "$dump_stats" = 1 ]; then
1843		ss -N $ns1 -inmHM
1844		ss -N $ns2 -inmHM
1845	fi
1846}
1847
1848# $1: subflows in ns1 ; $2: subflows in ns2
1849# number of all subflows, including the initial subflow.
1850chk_subflows_total()
1851{
1852	local cnt1
1853	local cnt2
1854	local info="subflows_total"
1855	local dump_stats
1856
1857	# if subflows_total counter is supported, use it:
1858	if [ -n "$(ss -N $ns1 -inmHM | mptcp_lib_get_info_value $info $info)" ]; then
1859		chk_mptcp_info $info $1 $info $2
1860		return
1861	fi
1862
1863	print_check "$info $1:$2"
1864
1865	# if not, count the TCP connections that are in fact MPTCP subflows
1866	cnt1=$(ss -N $ns1 -ti state established state syn-sent state syn-recv |
1867	       grep -c tcp-ulp-mptcp)
1868	cnt2=$(ss -N $ns2 -ti state established state syn-sent state syn-recv |
1869	       grep -c tcp-ulp-mptcp)
1870
1871	if [ "$1" != "$cnt1" ] || [ "$2" != "$cnt2" ]; then
1872		fail_test "got subflows $cnt1:$cnt2 expected $1:$2"
1873		dump_stats=1
1874	else
1875		print_ok
1876	fi
1877
1878	if [ "$dump_stats" = 1 ]; then
1879		ss -N $ns1 -ti
1880		ss -N $ns2 -ti
1881	fi
1882}
1883
1884chk_link_usage()
1885{
1886	local ns=$1
1887	local link=$2
1888	local out=$3
1889	local expected_rate=$4
1890
1891	local tx_link tx_total
1892	tx_link=$(ip netns exec $ns cat /sys/class/net/$link/statistics/tx_bytes)
1893	tx_total=$(stat --format=%s $out)
1894	local tx_rate=$((tx_link * 100 / tx_total))
1895	local tolerance=5
1896
1897	print_check "link usage"
1898	if [ $tx_rate -lt $((expected_rate - tolerance)) ] || \
1899	   [ $tx_rate -gt $((expected_rate + tolerance)) ]; then
1900		fail_test "got $tx_rate% usage, expected $expected_rate%"
1901	else
1902		print_ok
1903	fi
1904}
1905
1906wait_attempt_fail()
1907{
1908	local timeout_ms=$((timeout_poll * 1000))
1909	local time=0
1910	local ns=$1
1911
1912	while [ $time -lt $timeout_ms ]; do
1913		local cnt
1914
1915		cnt=$(mptcp_lib_get_counter ${ns} "TcpAttemptFails")
1916
1917		[ "$cnt" = 1 ] && return 1
1918		time=$((time + 100))
1919		sleep 0.1
1920	done
1921	return 1
1922}
1923
1924set_userspace_pm()
1925{
1926	local ns=$1
1927
1928	ip netns exec $ns sysctl -q net.mptcp.pm_type=1
1929}
1930
1931subflows_tests()
1932{
1933	if reset "no JOIN"; then
1934		run_tests $ns1 $ns2 10.0.1.1
1935		chk_join_nr 0 0 0
1936	fi
1937
1938	# subflow limited by client
1939	if reset "single subflow, limited by client"; then
1940		pm_nl_set_limits $ns1 0 0
1941		pm_nl_set_limits $ns2 0 0
1942		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1943		run_tests $ns1 $ns2 10.0.1.1
1944		chk_join_nr 0 0 0
1945	fi
1946
1947	# subflow limited by server
1948	if reset "single subflow, limited by server"; then
1949		pm_nl_set_limits $ns1 0 0
1950		pm_nl_set_limits $ns2 0 1
1951		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1952		run_tests $ns1 $ns2 10.0.1.1
1953		chk_join_nr 1 1 0
1954	fi
1955
1956	# subflow
1957	if reset "single subflow"; then
1958		pm_nl_set_limits $ns1 0 1
1959		pm_nl_set_limits $ns2 0 1
1960		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1961		run_tests $ns1 $ns2 10.0.1.1
1962		chk_join_nr 1 1 1
1963	fi
1964
1965	# multiple subflows
1966	if reset "multiple subflows"; then
1967		pm_nl_set_limits $ns1 0 2
1968		pm_nl_set_limits $ns2 0 2
1969		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1970		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1971		run_tests $ns1 $ns2 10.0.1.1
1972		chk_join_nr 2 2 2
1973	fi
1974
1975	# multiple subflows limited by server
1976	if reset "multiple subflows, limited by server"; then
1977		pm_nl_set_limits $ns1 0 1
1978		pm_nl_set_limits $ns2 0 2
1979		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1980		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1981		run_tests $ns1 $ns2 10.0.1.1
1982		chk_join_nr 2 2 1
1983	fi
1984
1985	# single subflow, dev
1986	if reset "single subflow, dev"; then
1987		pm_nl_set_limits $ns1 0 1
1988		pm_nl_set_limits $ns2 0 1
1989		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow dev ns2eth3
1990		run_tests $ns1 $ns2 10.0.1.1
1991		chk_join_nr 1 1 1
1992	fi
1993}
1994
1995subflows_error_tests()
1996{
1997	# If a single subflow is configured, and matches the MPC src
1998	# address, no additional subflow should be created
1999	if reset "no MPC reuse with single endpoint"; then
2000		pm_nl_set_limits $ns1 0 1
2001		pm_nl_set_limits $ns2 0 1
2002		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2003		pm_nl_add_endpoint $ns2 10.0.12.2 flags subflow
2004		speed=slow \
2005			run_tests $ns1 $ns2 10.0.1.1
2006		join_bind_err=1 \
2007			chk_join_nr 0 0 0
2008	fi
2009
2010	# multiple subflows, with subflow creation error
2011	if reset_with_tcp_filter "multi subflows, with failing subflow" ns1 10.0.3.2 REJECT &&
2012	   continue_if mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
2013		pm_nl_set_limits $ns1 0 2
2014		pm_nl_set_limits $ns2 0 2
2015		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2016		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2017		speed=slow \
2018			run_tests $ns1 $ns2 10.0.1.1
2019		join_syn_tx=2 \
2020			chk_join_nr 1 1 1
2021	fi
2022
2023	# multiple subflows, with subflow timeout on MPJ
2024	if reset_with_tcp_filter "multi subflows, with subflow timeout" ns1 10.0.3.2 DROP &&
2025	   continue_if mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
2026		pm_nl_set_limits $ns1 0 2
2027		pm_nl_set_limits $ns2 0 2
2028		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2029		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2030		speed=slow \
2031			run_tests $ns1 $ns2 10.0.1.1
2032		join_syn_tx=2 \
2033			chk_join_nr 1 1 1
2034	fi
2035
2036	# multiple subflows, check that the endpoint corresponding to
2037	# closed subflow (due to reset) is not reused if additional
2038	# subflows are added later
2039	if reset_with_tcp_filter "multi subflows, fair usage on close" ns1 10.0.3.2 REJECT &&
2040	   continue_if mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
2041		pm_nl_set_limits $ns1 0 1
2042		pm_nl_set_limits $ns2 0 1
2043		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2044		speed=slow \
2045			run_tests $ns1 $ns2 10.0.1.1 &
2046
2047		# mpj subflow will be in TW after the reset
2048		wait_attempt_fail $ns2
2049		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2050		wait
2051
2052		# additional subflow could be created only if the PM select
2053		# the later endpoint, skipping the already used one
2054		join_syn_tx=2 \
2055			chk_join_nr 1 1 1
2056	fi
2057}
2058
2059signal_address_tests()
2060{
2061	# add_address, unused
2062	if reset "unused signal address"; then
2063		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2064		run_tests $ns1 $ns2 10.0.1.1
2065		chk_join_nr 0 0 0
2066		chk_add_tx_nr 1 1
2067		chk_add_nr 1 1
2068	fi
2069
2070	# accept and use add_addr
2071	if reset "signal address"; then
2072		pm_nl_set_limits $ns1 0 1
2073		pm_nl_set_limits $ns2 1 1
2074		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2075		run_tests $ns1 $ns2 10.0.1.1
2076		chk_join_nr 1 1 1
2077		chk_add_nr 1 1
2078	fi
2079
2080	# accept and use add_addr with an additional subflow
2081	# note: signal address in server ns and local addresses in client ns must
2082	# belong to different subnets or one of the listed local address could be
2083	# used for 'add_addr' subflow
2084	if reset "subflow and signal"; then
2085		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2086		pm_nl_set_limits $ns1 0 2
2087		pm_nl_set_limits $ns2 1 2
2088		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2089		run_tests $ns1 $ns2 10.0.1.1
2090		chk_join_nr 2 2 2
2091		chk_add_nr 1 1
2092	fi
2093
2094	# uncommon: subflow and signal flags on the same endpoint
2095	# or because the user wrongly picked both, but still expects the client
2096	# to create additional subflows
2097	if reset "subflow and signal together"; then
2098		pm_nl_set_limits $ns1 0 2
2099		pm_nl_set_limits $ns2 0 2
2100		pm_nl_add_endpoint $ns2 10.0.3.2 flags signal,subflow
2101		run_tests $ns1 $ns2 10.0.1.1
2102		chk_join_nr 1 1 1
2103		chk_add_nr 1 1 0 invert  # only initiated by ns2
2104		chk_add_nr 0 0 0         # none initiated by ns1
2105		chk_rst_nr 0 0 invert    # no RST sent by the client
2106		chk_rst_nr 0 0           # no RST sent by the server
2107	fi
2108
2109	# accept and use add_addr with additional subflows
2110	if reset "multiple subflows and signal"; then
2111		pm_nl_set_limits $ns1 0 3
2112		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2113		pm_nl_set_limits $ns2 1 3
2114		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2115		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2116		run_tests $ns1 $ns2 10.0.1.1
2117		chk_join_nr 3 3 3
2118		chk_add_nr 1 1
2119	fi
2120
2121	# signal addresses
2122	if reset "signal addresses"; then
2123		pm_nl_set_limits $ns1 3 3
2124		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2125		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2126		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
2127		pm_nl_set_limits $ns2 3 3
2128		run_tests $ns1 $ns2 10.0.1.1
2129		chk_join_nr 3 3 3
2130		chk_add_nr 3 3
2131	fi
2132
2133	# signal invalid addresses
2134	if reset "signal invalid addresses"; then
2135		pm_nl_set_limits $ns1 3 3
2136		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
2137		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2138		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
2139		pm_nl_set_limits $ns2 3 3
2140		run_tests $ns1 $ns2 10.0.1.1
2141		join_syn_tx=3 \
2142			chk_join_nr 1 1 1
2143		chk_add_nr 3 3
2144	fi
2145
2146	# signal addresses race test
2147	if reset "signal addresses race test"; then
2148		pm_nl_set_limits $ns1 4 4
2149		pm_nl_set_limits $ns2 4 4
2150		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
2151		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2152		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2153		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
2154		pm_nl_add_endpoint $ns2 10.0.1.2 flags signal
2155		pm_nl_add_endpoint $ns2 10.0.2.2 flags signal
2156		pm_nl_add_endpoint $ns2 10.0.3.2 flags signal
2157		pm_nl_add_endpoint $ns2 10.0.4.2 flags signal
2158
2159		# the peer could possibly miss some addr notification, allow retransmission
2160		ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
2161		speed=slow \
2162			run_tests $ns1 $ns2 10.0.1.1
2163
2164		# It is not directly linked to the commit introducing this
2165		# symbol but for the parent one which is linked anyway.
2166		if ! mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
2167			chk_join_nr 3 3 2
2168			chk_add_nr 4 4
2169		else
2170			chk_join_nr 3 3 3
2171			# the server will not signal the address terminating
2172			# the MPC subflow
2173			chk_add_nr 3 3
2174		fi
2175	fi
2176}
2177
2178link_failure_tests()
2179{
2180	# accept and use add_addr with additional subflows and link loss
2181	if reset "multiple flows, signal, link failure"; then
2182		# without any b/w limit each veth could spool the packets and get
2183		# them acked at xmit time, so that the corresponding subflow will
2184		# have almost always no outstanding pkts, the scheduler will pick
2185		# always the first subflow and we will have hard time testing
2186		# active backup and link switch-over.
2187		# Let's set some arbitrary (low) virtual link limits.
2188		init_shapers
2189		pm_nl_set_limits $ns1 0 3
2190		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2191		pm_nl_set_limits $ns2 1 3
2192		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow
2193		pm_nl_add_endpoint $ns2 10.0.4.2 dev ns2eth4 flags subflow
2194		test_linkfail=1 \
2195			run_tests $ns1 $ns2 10.0.1.1
2196		chk_join_nr 3 3 3
2197		chk_add_nr 1 1
2198		chk_stale_nr $ns2 1 5 1
2199	fi
2200
2201	# accept and use add_addr with additional subflows and link loss
2202	# for bidirectional transfer
2203	if reset "multi flows, signal, bidi, link fail"; then
2204		init_shapers
2205		pm_nl_set_limits $ns1 0 3
2206		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2207		pm_nl_set_limits $ns2 1 3
2208		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow
2209		pm_nl_add_endpoint $ns2 10.0.4.2 dev ns2eth4 flags subflow
2210		test_linkfail=2 \
2211			run_tests $ns1 $ns2 10.0.1.1
2212		chk_join_nr 3 3 3
2213		chk_add_nr 1 1
2214		chk_stale_nr $ns2 1 -1 1
2215	fi
2216
2217	# 2 subflows plus 1 backup subflow with a lossy link, backup
2218	# will never be used
2219	if reset "backup subflow unused, link failure"; then
2220		init_shapers
2221		pm_nl_set_limits $ns1 0 2
2222		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2223		pm_nl_set_limits $ns2 1 2
2224		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
2225		FAILING_LINKS="1" test_linkfail=1 \
2226			run_tests $ns1 $ns2 10.0.1.1
2227		chk_join_nr 2 2 2
2228		chk_add_nr 1 1
2229		chk_link_usage $ns2 ns2eth3 $cinsent 0
2230	fi
2231
2232	# 2 lossy links after half transfer, backup will get half of
2233	# the traffic
2234	if reset "backup flow used, multi links fail"; then
2235		init_shapers
2236		pm_nl_set_limits $ns1 0 2
2237		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2238		pm_nl_set_limits $ns2 1 2
2239		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
2240		FAILING_LINKS="1 2" test_linkfail=1 \
2241			run_tests $ns1 $ns2 10.0.1.1
2242		chk_join_nr 2 2 2
2243		chk_add_nr 1 1
2244		chk_stale_nr $ns2 2 4 2
2245		chk_link_usage $ns2 ns2eth3 $cinsent 50
2246	fi
2247
2248	# use a backup subflow with the first subflow on a lossy link
2249	# for bidirectional transfer
2250	if reset "backup flow used, bidi, link failure"; then
2251		init_shapers
2252		pm_nl_set_limits $ns1 0 2
2253		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2254		pm_nl_set_limits $ns2 1 3
2255		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
2256		FAILING_LINKS="1 2" test_linkfail=2 \
2257			run_tests $ns1 $ns2 10.0.1.1
2258		chk_join_nr 2 2 2
2259		chk_add_nr 1 1
2260		chk_stale_nr $ns2 1 -1 2
2261		chk_link_usage $ns2 ns2eth3 $cinsent 50
2262	fi
2263}
2264
2265add_addr_timeout_tests()
2266{
2267	# add_addr timeout
2268	if reset_with_add_addr_timeout "signal address, ADD_ADDR timeout"; then
2269		pm_nl_set_limits $ns1 0 1
2270		pm_nl_set_limits $ns2 1 1
2271		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2272		speed=slow \
2273			run_tests $ns1 $ns2 10.0.1.1
2274		chk_join_nr 1 1 1
2275		chk_add_tx_nr 4 4
2276		chk_add_nr 4 0
2277	fi
2278
2279	# add_addr timeout IPv6
2280	if reset_with_add_addr_timeout "signal address, ADD_ADDR6 timeout" 6; then
2281		pm_nl_set_limits $ns1 0 1
2282		pm_nl_set_limits $ns2 1 1
2283		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2284		speed=slow \
2285			run_tests $ns1 $ns2 dead:beef:1::1
2286		chk_join_nr 1 1 1
2287		chk_add_nr 4 0
2288	fi
2289
2290	# signal addresses timeout
2291	if reset_with_add_addr_timeout "signal addresses, ADD_ADDR timeout"; then
2292		pm_nl_set_limits $ns1 2 2
2293		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2294		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2295		pm_nl_set_limits $ns2 2 2
2296		speed=10 \
2297			run_tests $ns1 $ns2 10.0.1.1
2298		chk_join_nr 2 2 2
2299		chk_add_nr 8 0
2300	fi
2301
2302	# signal invalid addresses timeout
2303	if reset_with_add_addr_timeout "invalid address, ADD_ADDR timeout"; then
2304		pm_nl_set_limits $ns1 2 2
2305		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
2306		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2307		pm_nl_set_limits $ns2 2 2
2308		speed=10 \
2309			run_tests $ns1 $ns2 10.0.1.1
2310		join_syn_tx=2 \
2311			chk_join_nr 1 1 1
2312		chk_add_nr 8 0
2313	fi
2314}
2315
2316remove_tests()
2317{
2318	# single subflow, remove
2319	if reset "remove single subflow"; then
2320		pm_nl_set_limits $ns1 0 1
2321		pm_nl_set_limits $ns2 0 1
2322		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2323		addr_nr_ns2=-1 speed=slow \
2324			run_tests $ns1 $ns2 10.0.1.1
2325		chk_join_nr 1 1 1
2326		chk_rm_tx_nr 1
2327		chk_rm_nr 1 1
2328		chk_rst_nr 0 0
2329	fi
2330
2331	# multiple subflows, remove
2332	if reset "remove multiple subflows"; then
2333		pm_nl_set_limits $ns1 0 2
2334		pm_nl_set_limits $ns2 0 2
2335		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2336		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2337		addr_nr_ns2=-2 speed=slow \
2338			run_tests $ns1 $ns2 10.0.1.1
2339		chk_join_nr 2 2 2
2340		chk_rm_nr 2 2
2341		chk_rst_nr 0 0
2342	fi
2343
2344	# single address, remove
2345	if reset "remove single address"; then
2346		pm_nl_set_limits $ns1 0 1
2347		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2348		pm_nl_set_limits $ns2 1 1
2349		addr_nr_ns1=-1 speed=slow \
2350			run_tests $ns1 $ns2 10.0.1.1
2351		chk_join_nr 1 1 1
2352		chk_add_nr 1 1
2353		chk_rm_nr 1 1 invert
2354		chk_rst_nr 0 0
2355	fi
2356
2357	# subflow and signal, remove
2358	if reset "remove subflow and signal"; then
2359		pm_nl_set_limits $ns1 0 2
2360		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2361		pm_nl_set_limits $ns2 1 2
2362		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2363		addr_nr_ns1=-1 addr_nr_ns2=-1 speed=slow \
2364			run_tests $ns1 $ns2 10.0.1.1
2365		chk_join_nr 2 2 2
2366		chk_add_nr 1 1
2367		chk_rm_nr 1 1
2368		chk_rst_nr 0 0
2369	fi
2370
2371	# subflows and signal, remove
2372	if reset "remove subflows and signal"; then
2373		pm_nl_set_limits $ns1 0 3
2374		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2375		pm_nl_set_limits $ns2 1 3
2376		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2377		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2378		addr_nr_ns1=-1 addr_nr_ns2=-2 speed=10 \
2379			run_tests $ns1 $ns2 10.0.1.1
2380		chk_join_nr 3 3 3
2381		chk_add_nr 1 1
2382		chk_rm_nr 2 2
2383		chk_rst_nr 0 0
2384	fi
2385
2386	# addresses remove
2387	if reset "remove addresses"; then
2388		pm_nl_set_limits $ns1 3 3
2389		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal id 250
2390		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2391		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
2392		pm_nl_set_limits $ns2 3 3
2393		addr_nr_ns1=-3 speed=10 \
2394			run_tests $ns1 $ns2 10.0.1.1
2395		chk_join_nr 3 3 3
2396		chk_add_nr 3 3
2397		chk_rm_nr 3 3 invert
2398		chk_rst_nr 0 0
2399	fi
2400
2401	# invalid addresses remove
2402	if reset "remove invalid addresses"; then
2403		pm_nl_set_limits $ns1 3 3
2404		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
2405		# broadcast IP: no packet for this address will be received on ns1
2406		pm_nl_add_endpoint $ns1 224.0.0.1 flags signal
2407		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2408		pm_nl_set_limits $ns2 2 2
2409		addr_nr_ns1=-3 speed=10 \
2410			run_tests $ns1 $ns2 10.0.1.1
2411		join_syn_tx=2 join_connect_err=1 \
2412			chk_join_nr 1 1 1
2413		chk_add_nr 3 3
2414		chk_rm_nr 3 1 invert
2415		chk_rst_nr 0 0
2416	fi
2417
2418	# subflows and signal, flush
2419	if reset "flush subflows and signal"; then
2420		pm_nl_set_limits $ns1 0 3
2421		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2422		pm_nl_set_limits $ns2 1 3
2423		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2424		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2425		addr_nr_ns1=-8 addr_nr_ns2=-8 speed=slow \
2426			run_tests $ns1 $ns2 10.0.1.1
2427		chk_join_nr 3 3 3
2428		chk_add_nr 1 1
2429		chk_rm_nr 1 3 invert simult
2430		chk_rst_nr 0 0
2431	fi
2432
2433	# subflows flush
2434	if reset "flush subflows"; then
2435		pm_nl_set_limits $ns1 3 3
2436		pm_nl_set_limits $ns2 3 3
2437		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow id 150
2438		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2439		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2440		addr_nr_ns1=-8 addr_nr_ns2=-8 speed=slow \
2441			run_tests $ns1 $ns2 10.0.1.1
2442		chk_join_nr 3 3 3
2443
2444		if mptcp_lib_kversion_ge 5.18; then
2445			chk_rm_tx_nr 0
2446			chk_rm_nr 0 3 simult
2447		else
2448			chk_rm_nr 3 3
2449		fi
2450		chk_rst_nr 0 0
2451	fi
2452
2453	# addresses flush
2454	if reset "flush addresses"; then
2455		pm_nl_set_limits $ns1 3 3
2456		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal id 250
2457		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2458		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
2459		pm_nl_set_limits $ns2 3 3
2460		addr_nr_ns1=-8 addr_nr_ns2=-8 speed=slow \
2461			run_tests $ns1 $ns2 10.0.1.1
2462		chk_join_nr 3 3 3
2463		chk_add_nr 3 3
2464		chk_rm_nr 3 3 invert simult
2465		chk_rst_nr 0 0
2466	fi
2467
2468	# invalid addresses flush
2469	if reset "flush invalid addresses"; then
2470		pm_nl_set_limits $ns1 3 3
2471		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
2472		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2473		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
2474		pm_nl_set_limits $ns2 3 3
2475		addr_nr_ns1=-8 speed=slow \
2476			run_tests $ns1 $ns2 10.0.1.1
2477		join_syn_tx=3 \
2478			chk_join_nr 1 1 1
2479		chk_add_nr 3 3
2480		chk_rm_nr 3 1 invert
2481		chk_rst_nr 0 0
2482	fi
2483
2484	# remove id 0 subflow
2485	if reset "remove id 0 subflow"; then
2486		pm_nl_set_limits $ns1 0 1
2487		pm_nl_set_limits $ns2 0 1
2488		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2489		addr_nr_ns2=-9 speed=slow \
2490			run_tests $ns1 $ns2 10.0.1.1
2491		chk_join_nr 1 1 1
2492		chk_rm_nr 1 1
2493		chk_rst_nr 0 0
2494	fi
2495
2496	# remove id 0 address
2497	if reset "remove id 0 address"; then
2498		pm_nl_set_limits $ns1 0 1
2499		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2500		pm_nl_set_limits $ns2 1 1
2501		addr_nr_ns1=-9 speed=slow \
2502			run_tests $ns1 $ns2 10.0.1.1
2503		chk_join_nr 1 1 1
2504		chk_add_nr 1 1
2505		chk_rm_nr 1 1 invert
2506		chk_rst_nr 0 0 invert
2507	fi
2508}
2509
2510add_tests()
2511{
2512	# add single subflow
2513	if reset "add single subflow"; then
2514		pm_nl_set_limits $ns1 0 1
2515		pm_nl_set_limits $ns2 0 1
2516		addr_nr_ns2=1 speed=slow cestab_ns2=1 \
2517			run_tests $ns1 $ns2 10.0.1.1
2518		chk_join_nr 1 1 1
2519		chk_cestab_nr $ns2 0
2520	fi
2521
2522	# add signal address
2523	if reset "add signal address"; then
2524		pm_nl_set_limits $ns1 0 1
2525		pm_nl_set_limits $ns2 1 1
2526		addr_nr_ns1=1 speed=slow cestab_ns1=1 \
2527			run_tests $ns1 $ns2 10.0.1.1
2528		chk_join_nr 1 1 1
2529		chk_add_nr 1 1
2530		chk_cestab_nr $ns1 0
2531	fi
2532
2533	# add multiple subflows
2534	if reset "add multiple subflows"; then
2535		pm_nl_set_limits $ns1 0 2
2536		pm_nl_set_limits $ns2 0 2
2537		addr_nr_ns2=2 speed=slow cestab_ns2=1 \
2538			run_tests $ns1 $ns2 10.0.1.1
2539		chk_join_nr 2 2 2
2540		chk_cestab_nr $ns2 0
2541	fi
2542
2543	# add multiple subflows IPv6
2544	if reset "add multiple subflows IPv6"; then
2545		pm_nl_set_limits $ns1 0 2
2546		pm_nl_set_limits $ns2 0 2
2547		addr_nr_ns2=2 speed=slow cestab_ns2=1 \
2548			run_tests $ns1 $ns2 dead:beef:1::1
2549		chk_join_nr 2 2 2
2550		chk_cestab_nr $ns2 0
2551	fi
2552
2553	# add multiple addresses IPv6
2554	if reset "add multiple addresses IPv6"; then
2555		pm_nl_set_limits $ns1 0 2
2556		pm_nl_set_limits $ns2 2 2
2557		addr_nr_ns1=2 speed=slow cestab_ns1=1 \
2558			run_tests $ns1 $ns2 dead:beef:1::1
2559		chk_join_nr 2 2 2
2560		chk_add_nr 2 2
2561		chk_cestab_nr $ns1 0
2562	fi
2563}
2564
2565ipv6_tests()
2566{
2567	# subflow IPv6
2568	if reset "single subflow IPv6"; then
2569		pm_nl_set_limits $ns1 0 1
2570		pm_nl_set_limits $ns2 0 1
2571		pm_nl_add_endpoint $ns2 dead:beef:3::2 dev ns2eth3 flags subflow
2572		speed=slow \
2573			run_tests $ns1 $ns2 dead:beef:1::1
2574		chk_join_nr 1 1 1
2575	fi
2576
2577	# add_address, unused IPv6
2578	if reset "unused signal address IPv6"; then
2579		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2580		speed=slow \
2581			run_tests $ns1 $ns2 dead:beef:1::1
2582		chk_join_nr 0 0 0
2583		chk_add_nr 1 1
2584	fi
2585
2586	# signal address IPv6
2587	if reset "single address IPv6"; then
2588		pm_nl_set_limits $ns1 0 1
2589		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2590		pm_nl_set_limits $ns2 1 1
2591		speed=slow \
2592			run_tests $ns1 $ns2 dead:beef:1::1
2593		chk_join_nr 1 1 1
2594		chk_add_nr 1 1
2595	fi
2596
2597	# single address IPv6, remove
2598	if reset "remove single address IPv6"; then
2599		pm_nl_set_limits $ns1 0 1
2600		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2601		pm_nl_set_limits $ns2 1 1
2602		addr_nr_ns1=-1 speed=slow \
2603			run_tests $ns1 $ns2 dead:beef:1::1
2604		chk_join_nr 1 1 1
2605		chk_add_nr 1 1
2606		chk_rm_nr 1 1 invert
2607	fi
2608
2609	# subflow and signal IPv6, remove
2610	if reset "remove subflow and signal IPv6"; then
2611		pm_nl_set_limits $ns1 0 2
2612		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2613		pm_nl_set_limits $ns2 1 2
2614		pm_nl_add_endpoint $ns2 dead:beef:3::2 dev ns2eth3 flags subflow
2615		addr_nr_ns1=-1 addr_nr_ns2=-1 speed=slow \
2616			run_tests $ns1 $ns2 dead:beef:1::1
2617		chk_join_nr 2 2 2
2618		chk_add_nr 1 1
2619		chk_rm_nr 1 1
2620	fi
2621}
2622
2623v4mapped_tests()
2624{
2625	# subflow IPv4-mapped to IPv4-mapped
2626	if reset "single subflow IPv4-mapped"; then
2627		pm_nl_set_limits $ns1 0 1
2628		pm_nl_set_limits $ns2 0 1
2629		pm_nl_add_endpoint $ns2 "::ffff:10.0.3.2" flags subflow
2630		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2631		chk_join_nr 1 1 1
2632	fi
2633
2634	# signal address IPv4-mapped with IPv4-mapped sk
2635	if reset "signal address IPv4-mapped"; then
2636		pm_nl_set_limits $ns1 0 1
2637		pm_nl_set_limits $ns2 1 1
2638		pm_nl_add_endpoint $ns1 "::ffff:10.0.2.1" flags signal
2639		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2640		chk_join_nr 1 1 1
2641		chk_add_nr 1 1
2642	fi
2643
2644	# subflow v4-map-v6
2645	if reset "single subflow v4-map-v6"; then
2646		pm_nl_set_limits $ns1 0 1
2647		pm_nl_set_limits $ns2 0 1
2648		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2649		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2650		chk_join_nr 1 1 1
2651	fi
2652
2653	# signal address v4-map-v6
2654	if reset "signal address v4-map-v6"; then
2655		pm_nl_set_limits $ns1 0 1
2656		pm_nl_set_limits $ns2 1 1
2657		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2658		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2659		chk_join_nr 1 1 1
2660		chk_add_nr 1 1
2661	fi
2662
2663	# subflow v6-map-v4
2664	if reset "single subflow v6-map-v4"; then
2665		pm_nl_set_limits $ns1 0 1
2666		pm_nl_set_limits $ns2 0 1
2667		pm_nl_add_endpoint $ns2 "::ffff:10.0.3.2" flags subflow
2668		run_tests $ns1 $ns2 10.0.1.1
2669		chk_join_nr 1 1 1
2670	fi
2671
2672	# signal address v6-map-v4
2673	if reset "signal address v6-map-v4"; then
2674		pm_nl_set_limits $ns1 0 1
2675		pm_nl_set_limits $ns2 1 1
2676		pm_nl_add_endpoint $ns1 "::ffff:10.0.2.1" flags signal
2677		run_tests $ns1 $ns2 10.0.1.1
2678		chk_join_nr 1 1 1
2679		chk_add_nr 1 1
2680	fi
2681
2682	# no subflow IPv6 to v4 address
2683	if reset "no JOIN with diff families v4-v6"; then
2684		pm_nl_set_limits $ns1 0 1
2685		pm_nl_set_limits $ns2 0 1
2686		pm_nl_add_endpoint $ns2 dead:beef:2::2 flags subflow
2687		run_tests $ns1 $ns2 10.0.1.1
2688		chk_join_nr 0 0 0
2689	fi
2690
2691	# no subflow IPv6 to v4 address even if v6 has a valid v4 at the end
2692	if reset "no JOIN with diff families v4-v6-2"; then
2693		pm_nl_set_limits $ns1 0 1
2694		pm_nl_set_limits $ns2 0 1
2695		pm_nl_add_endpoint $ns2 dead:beef:2::10.0.3.2 flags subflow
2696		run_tests $ns1 $ns2 10.0.1.1
2697		chk_join_nr 0 0 0
2698	fi
2699
2700	# no subflow IPv4 to v6 address, no need to slow down too then
2701	if reset "no JOIN with diff families v6-v4"; then
2702		pm_nl_set_limits $ns1 0 1
2703		pm_nl_set_limits $ns2 0 1
2704		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2705		run_tests $ns1 $ns2 dead:beef:1::1
2706		chk_join_nr 0 0 0
2707	fi
2708}
2709
2710mixed_tests()
2711{
2712	if reset "IPv4 sockets do not use IPv6 addresses" &&
2713	   continue_if mptcp_lib_kversion_ge 6.3; then
2714		pm_nl_set_limits $ns1 0 1
2715		pm_nl_set_limits $ns2 1 1
2716		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2717		speed=slow \
2718			run_tests $ns1 $ns2 10.0.1.1
2719		chk_join_nr 0 0 0
2720	fi
2721
2722	# Need an IPv6 mptcp socket to allow subflows of both families
2723	if reset "simult IPv4 and IPv6 subflows" &&
2724	   continue_if mptcp_lib_kversion_ge 6.3; then
2725		pm_nl_set_limits $ns1 0 1
2726		pm_nl_set_limits $ns2 1 1
2727		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
2728		speed=slow \
2729			run_tests $ns1 $ns2 dead:beef:2::1
2730		chk_join_nr 1 1 1
2731	fi
2732
2733	# cross families subflows will not be created even in fullmesh mode
2734	if reset "simult IPv4 and IPv6 subflows, fullmesh 1x1" &&
2735	   continue_if mptcp_lib_kversion_ge 6.3; then
2736		pm_nl_set_limits $ns1 0 4
2737		pm_nl_set_limits $ns2 1 4
2738		pm_nl_add_endpoint $ns2 dead:beef:2::2 flags subflow,fullmesh
2739		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
2740		speed=slow \
2741			run_tests $ns1 $ns2 dead:beef:2::1
2742		chk_join_nr 1 1 1
2743	fi
2744
2745	# fullmesh still tries to create all the possibly subflows with
2746	# matching family
2747	if reset "simult IPv4 and IPv6 subflows, fullmesh 2x2" &&
2748	   continue_if mptcp_lib_kversion_ge 6.3; then
2749		pm_nl_set_limits $ns1 0 4
2750		pm_nl_set_limits $ns2 2 4
2751		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2752		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2753		fullmesh=1 speed=slow \
2754			run_tests $ns1 $ns2 dead:beef:1::1
2755		chk_join_nr 4 4 4
2756	fi
2757}
2758
2759backup_tests()
2760{
2761	# single subflow, backup
2762	if reset "single subflow, backup" &&
2763	   continue_if mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
2764		pm_nl_set_limits $ns1 0 1
2765		pm_nl_set_limits $ns2 0 1
2766		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow,backup
2767		sflags=nobackup speed=slow \
2768			run_tests $ns1 $ns2 10.0.1.1
2769		chk_join_nr 1 1 1
2770		chk_prio_nr 0 1 1 0
2771	fi
2772
2773	# single address, backup
2774	if reset "single address, backup" &&
2775	   continue_if mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
2776		pm_nl_set_limits $ns1 0 1
2777		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal,backup
2778		pm_nl_set_limits $ns2 1 1
2779		sflags=nobackup speed=slow \
2780			run_tests $ns1 $ns2 10.0.1.1
2781		chk_join_nr 1 1 1
2782		chk_add_nr 1 1
2783		chk_prio_nr 1 0 0 1
2784	fi
2785
2786	# single address, switch to backup
2787	if reset "single address, switch to backup" &&
2788	   continue_if mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
2789		pm_nl_set_limits $ns1 0 1
2790		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2791		pm_nl_set_limits $ns2 1 1
2792		sflags=backup speed=slow \
2793			run_tests $ns1 $ns2 10.0.1.1
2794		chk_join_nr 1 1 1
2795		chk_add_nr 1 1
2796		chk_prio_nr 1 1 0 0
2797	fi
2798
2799	# single address with port, backup
2800	if reset "single address with port, backup" &&
2801	   continue_if mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
2802		pm_nl_set_limits $ns1 0 1
2803		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal,backup port 10100
2804		pm_nl_set_limits $ns2 1 1
2805		sflags=nobackup speed=slow \
2806			run_tests $ns1 $ns2 10.0.1.1
2807		chk_join_nr 1 1 1
2808		chk_add_nr 1 1
2809		chk_prio_nr 1 0 0 1
2810	fi
2811
2812	if reset "mpc backup" &&
2813	   continue_if mptcp_lib_kallsyms_doesnt_have "T mptcp_subflow_send_ack$"; then
2814		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,backup
2815		speed=slow \
2816			run_tests $ns1 $ns2 10.0.1.1
2817		chk_join_nr 0 0 0
2818		chk_prio_nr 0 1 0 0
2819	fi
2820
2821	if reset "mpc backup both sides" &&
2822	   continue_if mptcp_lib_kallsyms_doesnt_have "T mptcp_subflow_send_ack$"; then
2823		pm_nl_set_limits $ns1 0 2
2824		pm_nl_set_limits $ns2 1 2
2825		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal,backup
2826		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,backup
2827
2828		# 10.0.2.2 (non-backup) -> 10.0.1.1 (backup)
2829		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2830		# 10.0.1.2 (backup) -> 10.0.2.1 (non-backup)
2831		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2832		ip -net "$ns2" route add 10.0.2.1 via 10.0.1.1 dev ns2eth1 # force this path
2833
2834		speed=slow \
2835			run_tests $ns1 $ns2 10.0.1.1
2836		chk_join_nr 2 2 2
2837		chk_prio_nr 1 1 1 1
2838	fi
2839
2840	if reset "mpc switch to backup" &&
2841	   continue_if mptcp_lib_kallsyms_doesnt_have "T mptcp_subflow_send_ack$"; then
2842		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2843		sflags=backup speed=slow \
2844			run_tests $ns1 $ns2 10.0.1.1
2845		chk_join_nr 0 0 0
2846		chk_prio_nr 0 1 0 0
2847	fi
2848
2849	if reset "mpc switch to backup both sides" &&
2850	   continue_if mptcp_lib_kallsyms_doesnt_have "T mptcp_subflow_send_ack$"; then
2851		pm_nl_add_endpoint $ns1 10.0.1.1 flags subflow
2852		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2853		sflags=backup speed=slow \
2854			run_tests $ns1 $ns2 10.0.1.1
2855		chk_join_nr 0 0 0
2856		chk_prio_nr 1 1 0 0
2857	fi
2858}
2859
2860verify_listener_events()
2861{
2862	local e_type=$2
2863	local e_saddr=$4
2864	local e_sport=$5
2865	local name
2866
2867	if [ $e_type = $MPTCP_LIB_EVENT_LISTENER_CREATED ]; then
2868		name="LISTENER_CREATED"
2869	elif [ $e_type = $MPTCP_LIB_EVENT_LISTENER_CLOSED ]; then
2870		name="LISTENER_CLOSED "
2871	else
2872		name="$e_type"
2873	fi
2874
2875	print_check "$name $e_saddr:$e_sport"
2876
2877	if ! mptcp_lib_kallsyms_has "mptcp_event_pm_listener$"; then
2878		print_skip "event not supported"
2879		return
2880	fi
2881
2882	if mptcp_lib_verify_listener_events "${@}"; then
2883		print_ok
2884		return 0
2885	fi
2886	fail_test
2887}
2888
2889chk_mpc_endp_attempt()
2890{
2891	local retl=$1
2892	local attempts=$2
2893
2894	print_check "Connect"
2895
2896	if [ ${retl} = 124 ]; then
2897		fail_test "timeout on connect"
2898	elif [ ${retl} = 0 ]; then
2899		fail_test "unexpected successful connect"
2900	else
2901		print_ok
2902
2903		print_check "Attempts"
2904		count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtMPCapableEndpAttempt")
2905		if [ -z "$count" ]; then
2906			print_skip
2907		elif [ "$count" != "$attempts" ]; then
2908			fail_test "got ${count} MPC attempt[s] on port-based endpoint, expected ${attempts}"
2909		else
2910			print_ok
2911		fi
2912	fi
2913}
2914
2915add_addr_ports_tests()
2916{
2917	# signal address with port
2918	if reset "signal address with port"; then
2919		pm_nl_set_limits $ns1 0 1
2920		pm_nl_set_limits $ns2 1 1
2921		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2922		run_tests $ns1 $ns2 10.0.1.1
2923		chk_join_nr 1 1 1
2924		chk_add_nr 1 1 1
2925	fi
2926
2927	# subflow and signal with port
2928	if reset "subflow and signal with port"; then
2929		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2930		pm_nl_set_limits $ns1 0 2
2931		pm_nl_set_limits $ns2 1 2
2932		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2933		run_tests $ns1 $ns2 10.0.1.1
2934		chk_join_nr 2 2 2
2935		chk_add_nr 1 1 1
2936	fi
2937
2938	# single address with port, remove
2939	# pm listener events
2940	if reset_with_events "remove single address with port"; then
2941		pm_nl_set_limits $ns1 0 1
2942		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2943		pm_nl_set_limits $ns2 1 1
2944		addr_nr_ns1=-1 speed=slow \
2945			run_tests $ns1 $ns2 10.0.1.1
2946		chk_join_nr 1 1 1
2947		chk_add_nr 1 1 1
2948		chk_rm_nr 1 1 invert
2949
2950		verify_listener_events $evts_ns1 $MPTCP_LIB_EVENT_LISTENER_CREATED \
2951				       $MPTCP_LIB_AF_INET 10.0.2.1 10100
2952		verify_listener_events $evts_ns1 $MPTCP_LIB_EVENT_LISTENER_CLOSED \
2953				       $MPTCP_LIB_AF_INET 10.0.2.1 10100
2954		kill_events_pids
2955	fi
2956
2957	# subflow and signal with port, remove
2958	if reset "remove subflow and signal with port"; then
2959		pm_nl_set_limits $ns1 0 2
2960		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2961		pm_nl_set_limits $ns2 1 2
2962		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2963		addr_nr_ns1=-1 addr_nr_ns2=-1 speed=slow \
2964			run_tests $ns1 $ns2 10.0.1.1
2965		chk_join_nr 2 2 2
2966		chk_add_nr 1 1 1
2967		chk_rm_nr 1 1
2968	fi
2969
2970	# subflows and signal with port, flush
2971	if reset "flush subflows and signal with port"; then
2972		pm_nl_set_limits $ns1 0 3
2973		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2974		pm_nl_set_limits $ns2 1 3
2975		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2976		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2977		addr_nr_ns1=-8 addr_nr_ns2=-2 speed=slow \
2978			run_tests $ns1 $ns2 10.0.1.1
2979		chk_join_nr 3 3 3
2980		chk_add_nr 1 1
2981		chk_rm_nr 1 3 invert simult
2982	fi
2983
2984	# multiple addresses with port
2985	if reset "multiple addresses with port"; then
2986		pm_nl_set_limits $ns1 2 2
2987		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2988		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal port 10100
2989		pm_nl_set_limits $ns2 2 2
2990		run_tests $ns1 $ns2 10.0.1.1
2991		chk_join_nr 2 2 2
2992		chk_add_nr 2 2 2
2993	fi
2994
2995	# multiple addresses with ports
2996	if reset "multiple addresses with ports"; then
2997		pm_nl_set_limits $ns1 2 2
2998		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2999		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal port 10101
3000		pm_nl_set_limits $ns2 2 2
3001		run_tests $ns1 $ns2 10.0.1.1
3002		chk_join_nr 2 2 2
3003		chk_add_nr 2 2 2
3004	fi
3005
3006	if reset "port-based signal endpoint must not accept mpc"; then
3007		local port retl count
3008		port=$(get_port)
3009
3010		cond_start_capture ${ns1}
3011		pm_nl_add_endpoint ${ns1} 10.0.2.1 flags signal port ${port}
3012		mptcp_lib_wait_local_port_listen ${ns1} ${port}
3013
3014		timeout 1 ip netns exec ${ns2} \
3015			./mptcp_connect -t ${timeout_poll} -p $port -s MPTCP 10.0.2.1 >/dev/null 2>&1
3016		retl=$?
3017		cond_stop_capture
3018
3019		chk_mpc_endp_attempt ${retl} 1
3020	fi
3021}
3022
3023syncookies_tests()
3024{
3025	# single subflow, syncookies
3026	if reset_with_cookies "single subflow with syn cookies"; then
3027		pm_nl_set_limits $ns1 0 1
3028		pm_nl_set_limits $ns2 0 1
3029		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3030		run_tests $ns1 $ns2 10.0.1.1
3031		chk_join_nr 1 1 1
3032	fi
3033
3034	# multiple subflows with syn cookies
3035	if reset_with_cookies "multiple subflows with syn cookies"; then
3036		pm_nl_set_limits $ns1 0 2
3037		pm_nl_set_limits $ns2 0 2
3038		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3039		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
3040		run_tests $ns1 $ns2 10.0.1.1
3041		chk_join_nr 2 2 2
3042	fi
3043
3044	# multiple subflows limited by server
3045	if reset_with_cookies "subflows limited by server w cookies"; then
3046		pm_nl_set_limits $ns1 0 1
3047		pm_nl_set_limits $ns2 0 2
3048		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3049		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
3050		run_tests $ns1 $ns2 10.0.1.1
3051		chk_join_nr 2 1 1
3052	fi
3053
3054	# test signal address with cookies
3055	if reset_with_cookies "signal address with syn cookies"; then
3056		pm_nl_set_limits $ns1 0 1
3057		pm_nl_set_limits $ns2 1 1
3058		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3059		run_tests $ns1 $ns2 10.0.1.1
3060		chk_join_nr 1 1 1
3061		chk_add_nr 1 1
3062	fi
3063
3064	# test cookie with subflow and signal
3065	if reset_with_cookies "subflow and signal w cookies"; then
3066		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3067		pm_nl_set_limits $ns1 0 2
3068		pm_nl_set_limits $ns2 1 2
3069		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3070		run_tests $ns1 $ns2 10.0.1.1
3071		chk_join_nr 2 2 2
3072		chk_add_nr 1 1
3073	fi
3074
3075	# accept and use add_addr with additional subflows
3076	if reset_with_cookies "subflows and signal w. cookies"; then
3077		pm_nl_set_limits $ns1 0 3
3078		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3079		pm_nl_set_limits $ns2 1 3
3080		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3081		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
3082		run_tests $ns1 $ns2 10.0.1.1
3083		chk_join_nr 3 3 3
3084		chk_add_nr 1 1
3085	fi
3086}
3087
3088checksum_tests()
3089{
3090	local checksum_enable
3091	for checksum_enable in "0 0" "1 1" "0 1" "1 0"; do
3092		# checksum test 0 0, 1 1, 0 1, 1 0
3093		if reset_with_checksum ${checksum_enable}; then
3094			pm_nl_set_limits $ns1 0 1
3095			pm_nl_set_limits $ns2 0 1
3096			run_tests $ns1 $ns2 10.0.1.1
3097			chk_join_nr 0 0 0
3098		fi
3099	done
3100}
3101
3102deny_join_id0_tests()
3103{
3104	# subflow allow join id0 ns1
3105	if reset_with_allow_join_id0 "single subflow allow join id0 ns1" 1 0; then
3106		pm_nl_set_limits $ns1 1 1
3107		pm_nl_set_limits $ns2 1 1
3108		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3109		run_tests $ns1 $ns2 10.0.1.1
3110		chk_join_nr 1 1 1
3111	fi
3112
3113	# subflow allow join id0 ns2
3114	if reset_with_allow_join_id0 "single subflow allow join id0 ns2" 0 1; then
3115		pm_nl_set_limits $ns1 1 1
3116		pm_nl_set_limits $ns2 1 1
3117		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3118		run_tests $ns1 $ns2 10.0.1.1
3119		chk_join_nr 0 0 0
3120	fi
3121
3122	# signal address allow join id0 ns1
3123	# ADD_ADDRs are not affected by allow_join_id0 value.
3124	if reset_with_allow_join_id0 "signal address allow join id0 ns1" 1 0; then
3125		pm_nl_set_limits $ns1 1 1
3126		pm_nl_set_limits $ns2 1 1
3127		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3128		run_tests $ns1 $ns2 10.0.1.1
3129		chk_join_nr 1 1 1
3130		chk_add_nr 1 1
3131	fi
3132
3133	# signal address allow join id0 ns2
3134	# ADD_ADDRs are not affected by allow_join_id0 value.
3135	if reset_with_allow_join_id0 "signal address allow join id0 ns2" 0 1; then
3136		pm_nl_set_limits $ns1 1 1
3137		pm_nl_set_limits $ns2 1 1
3138		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3139		run_tests $ns1 $ns2 10.0.1.1
3140		chk_join_nr 1 1 1
3141		chk_add_nr 1 1
3142	fi
3143
3144	# subflow and address allow join id0 ns1
3145	if reset_with_allow_join_id0 "subflow and address allow join id0 1" 1 0; then
3146		pm_nl_set_limits $ns1 2 2
3147		pm_nl_set_limits $ns2 2 2
3148		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3149		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3150		run_tests $ns1 $ns2 10.0.1.1
3151		chk_join_nr 2 2 2
3152	fi
3153
3154	# subflow and address allow join id0 ns2
3155	if reset_with_allow_join_id0 "subflow and address allow join id0 2" 0 1; then
3156		pm_nl_set_limits $ns1 2 2
3157		pm_nl_set_limits $ns2 2 2
3158		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3159		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3160		run_tests $ns1 $ns2 10.0.1.1
3161		chk_join_nr 1 1 1
3162	fi
3163}
3164
3165fullmesh_tests()
3166{
3167	# fullmesh 1
3168	# 2 fullmesh addrs in ns2, added before the connection,
3169	# 1 non-fullmesh addr in ns1, added during the connection.
3170	if reset "fullmesh test 2x1"; then
3171		pm_nl_set_limits $ns1 0 4
3172		pm_nl_set_limits $ns2 1 4
3173		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow,fullmesh
3174		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow,fullmesh
3175		addr_nr_ns1=1 speed=slow \
3176			run_tests $ns1 $ns2 10.0.1.1
3177		chk_join_nr 4 4 4
3178		chk_add_nr 1 1
3179	fi
3180
3181	# fullmesh 2
3182	# 1 non-fullmesh addr in ns1, added before the connection,
3183	# 1 fullmesh addr in ns2, added during the connection.
3184	if reset "fullmesh test 1x1"; then
3185		pm_nl_set_limits $ns1 1 3
3186		pm_nl_set_limits $ns2 1 3
3187		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3188		if mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
3189			pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,fullmesh
3190		fi
3191		fullmesh=1 speed=slow \
3192			run_tests $ns1 $ns2 10.0.1.1
3193		chk_join_nr 3 3 3
3194		chk_add_nr 1 1
3195	fi
3196
3197	# fullmesh 3
3198	# 1 non-fullmesh addr in ns1, added before the connection,
3199	# 2 fullmesh addrs in ns2, added during the connection.
3200	if reset "fullmesh test 1x2"; then
3201		pm_nl_set_limits $ns1 2 5
3202		pm_nl_set_limits $ns2 1 5
3203		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3204		fullmesh=2 speed=slow \
3205			run_tests $ns1 $ns2 10.0.1.1
3206		chk_join_nr 5 5 5
3207		chk_add_nr 1 1
3208	fi
3209
3210	# fullmesh 4
3211	# 1 non-fullmesh addr in ns1, added before the connection,
3212	# 2 fullmesh addrs in ns2, added during the connection,
3213	# limit max_subflows to 4.
3214	if reset "fullmesh test 1x2, limited"; then
3215		pm_nl_set_limits $ns1 2 4
3216		pm_nl_set_limits $ns2 1 4
3217		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3218		fullmesh=2 speed=slow \
3219			run_tests $ns1 $ns2 10.0.1.1
3220		chk_join_nr 4 4 4
3221		chk_add_nr 1 1
3222	fi
3223
3224	# set fullmesh flag
3225	if reset "set fullmesh flag test" &&
3226	   continue_if mptcp_lib_kversion_ge 5.18; then
3227		pm_nl_set_limits $ns1 4 4
3228		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow
3229		pm_nl_set_limits $ns2 4 4
3230		addr_nr_ns2=1 sflags=fullmesh speed=slow \
3231			run_tests $ns1 $ns2 10.0.1.1
3232		chk_join_nr 2 2 2
3233		chk_rm_nr 0 1
3234	fi
3235
3236	# set nofullmesh flag
3237	if reset "set nofullmesh flag test" &&
3238	   continue_if mptcp_lib_kversion_ge 5.18; then
3239		pm_nl_set_limits $ns1 4 4
3240		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow,fullmesh
3241		pm_nl_set_limits $ns2 4 4
3242		fullmesh=1 sflags=nofullmesh speed=slow \
3243			run_tests $ns1 $ns2 10.0.1.1
3244		chk_join_nr 2 2 2
3245		chk_rm_nr 0 1
3246	fi
3247
3248	# set backup,fullmesh flags
3249	if reset "set backup,fullmesh flags test" &&
3250	   continue_if mptcp_lib_kversion_ge 5.18; then
3251		pm_nl_set_limits $ns1 4 4
3252		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow
3253		pm_nl_set_limits $ns2 4 4
3254		addr_nr_ns2=1 sflags=backup,fullmesh speed=slow \
3255			run_tests $ns1 $ns2 10.0.1.1
3256		chk_join_nr 2 2 2
3257		chk_prio_nr 0 1 1 0
3258		chk_rm_nr 0 1
3259	fi
3260
3261	# set nobackup,nofullmesh flags
3262	if reset "set nobackup,nofullmesh flags test" &&
3263	   continue_if mptcp_lib_kversion_ge 5.18; then
3264		pm_nl_set_limits $ns1 4 4
3265		pm_nl_set_limits $ns2 4 4
3266		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow,backup,fullmesh
3267		sflags=nobackup,nofullmesh speed=slow \
3268			run_tests $ns1 $ns2 10.0.1.1
3269		chk_join_nr 2 2 2
3270		chk_prio_nr 0 1 1 0
3271		chk_rm_nr 0 1
3272	fi
3273}
3274
3275fastclose_tests()
3276{
3277	if reset_check_counter "fastclose test" "MPTcpExtMPFastcloseTx"; then
3278		MPTCP_LIB_SUBTEST_FLAKY=1
3279		test_linkfail=1024 fastclose=client \
3280			run_tests $ns1 $ns2 10.0.1.1
3281		chk_join_nr 0 0 0
3282		chk_fclose_nr 1 1
3283		chk_rst_nr 1 1 invert
3284	fi
3285
3286	if reset_check_counter "fastclose server test" "MPTcpExtMPFastcloseRx"; then
3287		MPTCP_LIB_SUBTEST_FLAKY=1
3288		test_linkfail=1024 fastclose=server \
3289			run_tests $ns1 $ns2 10.0.1.1
3290		join_rst_nr=1 \
3291			chk_join_nr 0 0 0
3292		chk_fclose_nr 1 1 invert
3293		chk_rst_nr 1 1
3294	fi
3295}
3296
3297pedit_action_pkts()
3298{
3299	tc -n $ns2 -j -s action show action pedit index 100 | \
3300		mptcp_lib_get_info_value \"packets\" packets
3301}
3302
3303fail_tests()
3304{
3305	# single subflow
3306	if reset_with_fail "Infinite map" 1; then
3307		MPTCP_LIB_SUBTEST_FLAKY=1
3308		test_linkfail=128 \
3309			run_tests $ns1 $ns2 10.0.1.1
3310		join_csum_ns1=+1 join_csum_ns2=+0 \
3311			join_fail_nr=1 join_rst_nr=0 join_infi_nr=1 \
3312			join_corrupted_pkts="$(pedit_action_pkts)" \
3313			chk_join_nr 0 0 0
3314		chk_fail_nr 1 -1 invert
3315	fi
3316
3317	# multiple subflows
3318	if reset_with_fail "MP_FAIL MP_RST" 2; then
3319		MPTCP_LIB_SUBTEST_FLAKY=1
3320		tc -n $ns2 qdisc add dev ns2eth1 root netem rate 1mbit delay 5ms
3321		pm_nl_set_limits $ns1 0 1
3322		pm_nl_set_limits $ns2 0 1
3323		pm_nl_add_endpoint $ns2 10.0.2.2 dev ns2eth2 flags subflow
3324		test_linkfail=1024 \
3325			run_tests $ns1 $ns2 10.0.1.1
3326		join_csum_ns1=1 join_csum_ns2=0 \
3327			join_fail_nr=1 join_rst_nr=1 join_infi_nr=0 \
3328			join_corrupted_pkts="$(pedit_action_pkts)" \
3329			chk_join_nr 1 1 1
3330	fi
3331}
3332
3333# $1: ns ; $2: addr ; $3: id
3334userspace_pm_add_addr()
3335{
3336	local evts=$evts_ns1
3337	local tk
3338
3339	[ "$1" == "$ns2" ] && evts=$evts_ns2
3340	tk=$(mptcp_lib_evts_get_info token "$evts")
3341
3342	ip netns exec $1 ./pm_nl_ctl ann $2 token $tk id $3
3343	sleep 1
3344}
3345
3346# $1: ns ; $2: id
3347userspace_pm_rm_addr()
3348{
3349	local evts=$evts_ns1
3350	local tk
3351	local cnt
3352
3353	[ "$1" == "$ns2" ] && evts=$evts_ns2
3354	tk=$(mptcp_lib_evts_get_info token "$evts")
3355
3356	cnt=$(rm_addr_count ${1})
3357	ip netns exec $1 ./pm_nl_ctl rem token $tk id $2
3358	wait_rm_addr $1 "${cnt}"
3359}
3360
3361# $1: ns ; $2: addr ; $3: id
3362userspace_pm_add_sf()
3363{
3364	local evts=$evts_ns1
3365	local tk da dp
3366
3367	[ "$1" == "$ns2" ] && evts=$evts_ns2
3368	tk=$(mptcp_lib_evts_get_info token "$evts")
3369	da=$(mptcp_lib_evts_get_info daddr4 "$evts")
3370	dp=$(mptcp_lib_evts_get_info dport "$evts")
3371
3372	ip netns exec $1 ./pm_nl_ctl csf lip $2 lid $3 \
3373				rip $da rport $dp token $tk
3374	sleep 1
3375}
3376
3377# $1: ns ; $2: addr $3: event type
3378userspace_pm_rm_sf()
3379{
3380	local evts=$evts_ns1
3381	local t=${3:-1}
3382	local ip
3383	local tk da dp sp
3384	local cnt
3385
3386	[ "$1" == "$ns2" ] && evts=$evts_ns2
3387	[ -n "$(mptcp_lib_evts_get_info "saddr4" "$evts" $t)" ] && ip=4
3388	[ -n "$(mptcp_lib_evts_get_info "saddr6" "$evts" $t)" ] && ip=6
3389	tk=$(mptcp_lib_evts_get_info token "$evts")
3390	da=$(mptcp_lib_evts_get_info "daddr$ip" "$evts" $t $2)
3391	dp=$(mptcp_lib_evts_get_info dport "$evts" $t $2)
3392	sp=$(mptcp_lib_evts_get_info sport "$evts" $t $2)
3393
3394	cnt=$(rm_sf_count ${1})
3395	ip netns exec $1 ./pm_nl_ctl dsf lip $2 lport $sp \
3396				rip $da rport $dp token $tk
3397	wait_rm_sf $1 "${cnt}"
3398}
3399
3400check_output()
3401{
3402	local cmd="$1"
3403	local expected="$2"
3404	local msg="$3"
3405	local rc=0
3406
3407	mptcp_lib_check_output "${err}" "${cmd}" "${expected}" || rc=${?}
3408	if [ ${rc} -eq 2 ]; then
3409		fail_test "fail to check output # error ${rc}"
3410	elif [ ${rc} -eq 0 ]; then
3411		print_ok
3412	elif [ ${rc} -eq 1 ]; then
3413		fail_test "fail to check output # different output"
3414	fi
3415}
3416
3417# $1: ns
3418userspace_pm_dump()
3419{
3420	local evts=$evts_ns1
3421	local tk
3422
3423	[ "$1" == "$ns2" ] && evts=$evts_ns2
3424	tk=$(mptcp_lib_evts_get_info token "$evts")
3425
3426	ip netns exec $1 ./pm_nl_ctl dump token $tk
3427}
3428
3429# $1: ns ; $2: id
3430userspace_pm_get_addr()
3431{
3432	local evts=$evts_ns1
3433	local tk
3434
3435	[ "$1" == "$ns2" ] && evts=$evts_ns2
3436	tk=$(mptcp_lib_evts_get_info token "$evts")
3437
3438	ip netns exec $1 ./pm_nl_ctl get $2 token $tk
3439}
3440
3441userspace_pm_chk_dump_addr()
3442{
3443	local ns="${1}"
3444	local exp="${2}"
3445	local check="${3}"
3446
3447	print_check "dump addrs ${check}"
3448
3449	if mptcp_lib_kallsyms_has "mptcp_userspace_pm_dump_addr$"; then
3450		check_output "userspace_pm_dump ${ns}" "${exp}"
3451	else
3452		print_skip
3453	fi
3454}
3455
3456userspace_pm_chk_get_addr()
3457{
3458	local ns="${1}"
3459	local id="${2}"
3460	local exp="${3}"
3461
3462	print_check "get id ${id} addr"
3463
3464	if mptcp_lib_kallsyms_has "mptcp_userspace_pm_get_addr$"; then
3465		check_output "userspace_pm_get_addr ${ns} ${id}" "${exp}"
3466	else
3467		print_skip
3468	fi
3469}
3470
3471# $1: ns ; $2: event type ; $3: count
3472chk_evt_nr()
3473{
3474	local ns=${1}
3475	local evt_name="${2}"
3476	local exp="${3}"
3477
3478	local evts="${evts_ns1}"
3479	local evt="${!evt_name}"
3480	local count
3481
3482	evt_name="${evt_name:16}" # without MPTCP_LIB_EVENT_
3483	[ "${ns}" == "ns2" ] && evts="${evts_ns2}"
3484
3485	print_check "event ${ns} ${evt_name} (${exp})"
3486
3487	if [[ "${evt_name}" = "LISTENER_"* ]] &&
3488	   ! mptcp_lib_kallsyms_has "mptcp_event_pm_listener$"; then
3489		print_skip "event not supported"
3490		return
3491	fi
3492
3493	count=$(grep -cw "type:${evt}" "${evts}")
3494	if [ "${count}" != "${exp}" ]; then
3495		fail_test "got ${count} events, expected ${exp}"
3496	else
3497		print_ok
3498	fi
3499}
3500
3501userspace_tests()
3502{
3503	# userspace pm type prevents add_addr
3504	if reset "userspace pm type prevents add_addr" &&
3505	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3506		set_userspace_pm $ns1
3507		pm_nl_set_limits $ns1 0 2
3508		pm_nl_set_limits $ns2 0 2
3509		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3510		run_tests $ns1 $ns2 10.0.1.1
3511		chk_join_nr 0 0 0
3512		chk_add_nr 0 0
3513	fi
3514
3515	# userspace pm type does not echo add_addr without daemon
3516	if reset "userspace pm no echo w/o daemon" &&
3517	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3518		set_userspace_pm $ns2
3519		pm_nl_set_limits $ns1 0 2
3520		pm_nl_set_limits $ns2 0 2
3521		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3522		run_tests $ns1 $ns2 10.0.1.1
3523		chk_join_nr 0 0 0
3524		chk_add_nr 1 0
3525	fi
3526
3527	# userspace pm type rejects join
3528	if reset "userspace pm type rejects join" &&
3529	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3530		set_userspace_pm $ns1
3531		pm_nl_set_limits $ns1 1 1
3532		pm_nl_set_limits $ns2 1 1
3533		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3534		run_tests $ns1 $ns2 10.0.1.1
3535		chk_join_nr 1 1 0
3536	fi
3537
3538	# userspace pm type does not send join
3539	if reset "userspace pm type does not send join" &&
3540	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3541		set_userspace_pm $ns2
3542		pm_nl_set_limits $ns1 1 1
3543		pm_nl_set_limits $ns2 1 1
3544		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3545		run_tests $ns1 $ns2 10.0.1.1
3546		chk_join_nr 0 0 0
3547	fi
3548
3549	# userspace pm type prevents mp_prio
3550	if reset "userspace pm type prevents mp_prio" &&
3551	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3552		set_userspace_pm $ns1
3553		pm_nl_set_limits $ns1 1 1
3554		pm_nl_set_limits $ns2 1 1
3555		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3556		sflags=backup speed=slow \
3557			run_tests $ns1 $ns2 10.0.1.1
3558		chk_join_nr 1 1 0
3559		chk_prio_nr 0 0 0 0
3560	fi
3561
3562	# userspace pm type prevents rm_addr
3563	if reset "userspace pm type prevents rm_addr" &&
3564	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3565		set_userspace_pm $ns1
3566		set_userspace_pm $ns2
3567		pm_nl_set_limits $ns1 0 1
3568		pm_nl_set_limits $ns2 0 1
3569		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3570		addr_nr_ns2=-1 speed=slow \
3571			run_tests $ns1 $ns2 10.0.1.1
3572		chk_join_nr 0 0 0
3573		chk_rm_nr 0 0
3574	fi
3575
3576	# userspace pm add & remove address
3577	if reset_with_events "userspace pm add & remove address" &&
3578	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3579		set_userspace_pm $ns1
3580		pm_nl_set_limits $ns2 2 2
3581		{ speed=5 \
3582			run_tests $ns1 $ns2 10.0.1.1 & } 2>/dev/null
3583		local tests_pid=$!
3584		wait_mpj $ns1
3585		userspace_pm_add_addr $ns1 10.0.2.1 10
3586		userspace_pm_add_addr $ns1 10.0.3.1 20
3587		chk_join_nr 2 2 2
3588		chk_add_nr 2 2
3589		chk_mptcp_info subflows 2 subflows 2
3590		chk_subflows_total 3 3
3591		chk_mptcp_info add_addr_signal 2 add_addr_accepted 2
3592		userspace_pm_chk_dump_addr "${ns1}" \
3593			$'id 10 flags signal 10.0.2.1\nid 20 flags signal 10.0.3.1' \
3594			"signal"
3595		userspace_pm_chk_get_addr "${ns1}" "10" "id 10 flags signal 10.0.2.1"
3596		userspace_pm_chk_get_addr "${ns1}" "20" "id 20 flags signal 10.0.3.1"
3597		userspace_pm_rm_sf $ns1 "::ffff:10.0.2.1" $MPTCP_LIB_EVENT_SUB_ESTABLISHED
3598		userspace_pm_chk_dump_addr "${ns1}" \
3599			"id 20 flags signal 10.0.3.1" "after rm_sf 10"
3600		userspace_pm_rm_addr $ns1 20
3601		userspace_pm_chk_dump_addr "${ns1}" "" "after rm_addr 20"
3602		chk_rm_nr 1 1 invert
3603		chk_mptcp_info subflows 0 subflows 0
3604		chk_subflows_total 1 1
3605		kill_events_pids
3606		mptcp_lib_kill_wait $tests_pid
3607	fi
3608
3609	# userspace pm create destroy subflow
3610	if reset_with_events "userspace pm create destroy subflow" &&
3611	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3612		set_userspace_pm $ns2
3613		pm_nl_set_limits $ns1 0 1
3614		{ speed=5 \
3615			run_tests $ns1 $ns2 10.0.1.1 & } 2>/dev/null
3616		local tests_pid=$!
3617		wait_mpj $ns2
3618		userspace_pm_add_sf $ns2 10.0.3.2 20
3619		chk_join_nr 1 1 1
3620		chk_mptcp_info subflows 1 subflows 1
3621		chk_subflows_total 2 2
3622		userspace_pm_chk_dump_addr "${ns2}" \
3623			"id 20 flags subflow 10.0.3.2" \
3624			"subflow"
3625		userspace_pm_chk_get_addr "${ns2}" "20" "id 20 flags subflow 10.0.3.2"
3626		userspace_pm_rm_sf $ns2 10.0.3.2 $MPTCP_LIB_EVENT_SUB_ESTABLISHED
3627		userspace_pm_chk_dump_addr "${ns2}" \
3628			"" \
3629			"after rm_sf 20"
3630		chk_rm_nr 0 1
3631		chk_mptcp_info subflows 0 subflows 0
3632		chk_subflows_total 1 1
3633		kill_events_pids
3634		mptcp_lib_kill_wait $tests_pid
3635	fi
3636
3637	# userspace pm create id 0 subflow
3638	if reset_with_events "userspace pm create id 0 subflow" &&
3639	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3640		set_userspace_pm $ns2
3641		pm_nl_set_limits $ns1 0 1
3642		{ speed=5 \
3643			run_tests $ns1 $ns2 10.0.1.1 & } 2>/dev/null
3644		local tests_pid=$!
3645		wait_mpj $ns2
3646		chk_mptcp_info subflows 0 subflows 0
3647		chk_subflows_total 1 1
3648		userspace_pm_add_sf $ns2 10.0.3.2 0
3649		userspace_pm_chk_dump_addr "${ns2}" \
3650			"id 0 flags subflow 10.0.3.2" "id 0 subflow"
3651		chk_join_nr 1 1 1
3652		chk_mptcp_info subflows 1 subflows 1
3653		chk_subflows_total 2 2
3654		kill_events_pids
3655		mptcp_lib_kill_wait $tests_pid
3656	fi
3657
3658	# userspace pm remove initial subflow
3659	if reset_with_events "userspace pm remove initial subflow" &&
3660	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3661		set_userspace_pm $ns2
3662		pm_nl_set_limits $ns1 0 1
3663		{ speed=5 \
3664			run_tests $ns1 $ns2 10.0.1.1 & } 2>/dev/null
3665		local tests_pid=$!
3666		wait_mpj $ns2
3667		userspace_pm_add_sf $ns2 10.0.3.2 20
3668		chk_join_nr 1 1 1
3669		chk_mptcp_info subflows 1 subflows 1
3670		chk_subflows_total 2 2
3671		userspace_pm_rm_sf $ns2 10.0.1.2
3672		# we don't look at the counter linked to the RM_ADDR but
3673		# to the one linked to the subflows that have been removed
3674		chk_rm_nr 0 1
3675		chk_rst_nr 0 0 invert
3676		chk_mptcp_info subflows 1 subflows 1
3677		chk_subflows_total 1 1
3678		kill_events_pids
3679		mptcp_lib_kill_wait $tests_pid
3680	fi
3681
3682	# userspace pm send RM_ADDR for ID 0
3683	if reset_with_events "userspace pm send RM_ADDR for ID 0" &&
3684	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3685		set_userspace_pm $ns1
3686		pm_nl_set_limits $ns2 1 1
3687		{ speed=5 \
3688			run_tests $ns1 $ns2 10.0.1.1 & } 2>/dev/null
3689		local tests_pid=$!
3690		wait_mpj $ns1
3691		userspace_pm_add_addr $ns1 10.0.2.1 10
3692		chk_join_nr 1 1 1
3693		chk_add_nr 1 1
3694		chk_mptcp_info subflows 1 subflows 1
3695		chk_subflows_total 2 2
3696		chk_mptcp_info add_addr_signal 1 add_addr_accepted 1
3697		userspace_pm_rm_addr $ns1 0
3698		# we don't look at the counter linked to the subflows that
3699		# have been removed but to the one linked to the RM_ADDR
3700		chk_rm_nr 1 0 invert
3701		chk_rst_nr 0 0 invert
3702		chk_mptcp_info subflows 1 subflows 1
3703		chk_subflows_total 1 1
3704		kill_events_pids
3705		mptcp_lib_kill_wait $tests_pid
3706	fi
3707}
3708
3709endpoint_tests()
3710{
3711	# subflow_rebuild_header is needed to support the implicit flag
3712	# userspace pm type prevents add_addr
3713	if reset "implicit EP" &&
3714	   mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
3715		pm_nl_set_limits $ns1 2 2
3716		pm_nl_set_limits $ns2 2 2
3717		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3718		{ speed=slow \
3719			run_tests $ns1 $ns2 10.0.1.1 & } 2>/dev/null
3720		local tests_pid=$!
3721
3722		wait_mpj $ns1
3723		pm_nl_check_endpoint "creation" \
3724			$ns2 10.0.2.2 id 1 flags implicit
3725		chk_mptcp_info subflows 1 subflows 1
3726		chk_mptcp_info add_addr_signal 1 add_addr_accepted 1
3727
3728		pm_nl_add_endpoint $ns2 10.0.2.2 id 33 2>/dev/null
3729		pm_nl_check_endpoint "ID change is prevented" \
3730			$ns2 10.0.2.2 id 1 flags implicit
3731
3732		pm_nl_add_endpoint $ns2 10.0.2.2 flags signal
3733		pm_nl_check_endpoint "modif is allowed" \
3734			$ns2 10.0.2.2 id 1 flags signal
3735		mptcp_lib_kill_wait $tests_pid
3736	fi
3737
3738	if reset_with_tcp_filter "delete and re-add" ns2 10.0.3.2 REJECT OUTPUT &&
3739	   mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
3740		start_events
3741		pm_nl_set_limits $ns1 0 3
3742		pm_nl_set_limits $ns2 0 3
3743		pm_nl_add_endpoint $ns2 10.0.1.2 id 1 dev ns2eth1 flags subflow
3744		pm_nl_add_endpoint $ns2 10.0.2.2 id 2 dev ns2eth2 flags subflow
3745		{ test_linkfail=4 speed=5 \
3746			run_tests $ns1 $ns2 10.0.1.1 & } 2>/dev/null
3747		local tests_pid=$!
3748
3749		wait_mpj $ns2
3750		pm_nl_check_endpoint "creation" \
3751			$ns2 10.0.2.2 id 2 flags subflow dev ns2eth2
3752		chk_subflow_nr "before delete id 2" 2
3753		chk_mptcp_info subflows 1 subflows 1
3754
3755		pm_nl_del_endpoint $ns2 2 10.0.2.2
3756		sleep 0.5
3757		chk_subflow_nr "after delete id 2" 1
3758		chk_mptcp_info subflows 0 subflows 0
3759
3760		pm_nl_add_endpoint $ns2 10.0.2.2 id 2 dev ns2eth2 flags subflow
3761		wait_mpj $ns2
3762		chk_subflow_nr "after re-add id 2" 2
3763		chk_mptcp_info subflows 1 subflows 1
3764
3765		pm_nl_add_endpoint $ns2 10.0.3.2 id 3 flags subflow
3766		wait_attempt_fail $ns2
3767		chk_subflow_nr "after new reject" 2
3768		chk_mptcp_info subflows 1 subflows 1
3769
3770		ip netns exec "${ns2}" ${iptables} -D OUTPUT -s "10.0.3.2" -p tcp -j REJECT
3771		pm_nl_del_endpoint $ns2 3 10.0.3.2
3772		pm_nl_add_endpoint $ns2 10.0.3.2 id 3 flags subflow
3773		wait_mpj $ns2
3774		chk_subflow_nr "after no reject" 3
3775		chk_mptcp_info subflows 2 subflows 2
3776
3777		local i
3778		for i in $(seq 3); do
3779			pm_nl_del_endpoint $ns2 1 10.0.1.2
3780			sleep 0.5
3781			chk_subflow_nr "after delete id 0 ($i)" 2
3782			chk_mptcp_info subflows 2 subflows 2 # only decr for additional sf
3783
3784			pm_nl_add_endpoint $ns2 10.0.1.2 id 1 dev ns2eth1 flags subflow
3785			wait_mpj $ns2
3786			chk_subflow_nr "after re-add id 0 ($i)" 3
3787			chk_mptcp_info subflows 3 subflows 3
3788		done
3789
3790		mptcp_lib_kill_wait $tests_pid
3791
3792		kill_events_pids
3793		chk_evt_nr ns1 MPTCP_LIB_EVENT_LISTENER_CREATED 1
3794		chk_evt_nr ns1 MPTCP_LIB_EVENT_CREATED 1
3795		chk_evt_nr ns1 MPTCP_LIB_EVENT_ESTABLISHED 1
3796		chk_evt_nr ns1 MPTCP_LIB_EVENT_ANNOUNCED 0
3797		chk_evt_nr ns1 MPTCP_LIB_EVENT_REMOVED 4
3798		chk_evt_nr ns1 MPTCP_LIB_EVENT_SUB_ESTABLISHED 6
3799		chk_evt_nr ns1 MPTCP_LIB_EVENT_SUB_CLOSED 4
3800
3801		chk_evt_nr ns2 MPTCP_LIB_EVENT_CREATED 1
3802		chk_evt_nr ns2 MPTCP_LIB_EVENT_ESTABLISHED 1
3803		chk_evt_nr ns2 MPTCP_LIB_EVENT_ANNOUNCED 0
3804		chk_evt_nr ns2 MPTCP_LIB_EVENT_REMOVED 0
3805		chk_evt_nr ns2 MPTCP_LIB_EVENT_SUB_ESTABLISHED 6
3806		chk_evt_nr ns2 MPTCP_LIB_EVENT_SUB_CLOSED 5 # one has been closed before estab
3807
3808		join_syn_tx=7 \
3809			chk_join_nr 6 6 6
3810		chk_rm_nr 4 4
3811	fi
3812
3813	# remove and re-add
3814	if reset_with_events "delete re-add signal" &&
3815	   mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
3816		pm_nl_set_limits $ns1 0 3
3817		pm_nl_set_limits $ns2 3 3
3818		pm_nl_add_endpoint $ns1 10.0.2.1 id 1 flags signal
3819		# broadcast IP: no packet for this address will be received on ns1
3820		pm_nl_add_endpoint $ns1 224.0.0.1 id 2 flags signal
3821		pm_nl_add_endpoint $ns1 10.0.1.1 id 42 flags signal
3822		{ test_linkfail=4 speed=5 \
3823			run_tests $ns1 $ns2 10.0.1.1 & } 2>/dev/null
3824		local tests_pid=$!
3825
3826		wait_mpj $ns2
3827		pm_nl_check_endpoint "creation" \
3828			$ns1 10.0.2.1 id 1 flags signal
3829		chk_subflow_nr "before delete" 2
3830		chk_mptcp_info subflows 1 subflows 1
3831
3832		pm_nl_del_endpoint $ns1 1 10.0.2.1
3833		pm_nl_del_endpoint $ns1 2 224.0.0.1
3834		sleep 0.5
3835		chk_subflow_nr "after delete" 1
3836		chk_mptcp_info subflows 0 subflows 0
3837
3838		pm_nl_add_endpoint $ns1 10.0.2.1 id 1 flags signal
3839		pm_nl_add_endpoint $ns1 10.0.3.1 id 2 flags signal
3840		wait_mpj $ns2
3841		chk_subflow_nr "after re-add" 3
3842		chk_mptcp_info subflows 2 subflows 2
3843
3844		pm_nl_del_endpoint $ns1 42 10.0.1.1
3845		sleep 0.5
3846		chk_subflow_nr "after delete ID 0" 2
3847		chk_mptcp_info subflows 2 subflows 2
3848
3849		pm_nl_add_endpoint $ns1 10.0.1.1 id 99 flags signal
3850		wait_mpj $ns2
3851		chk_subflow_nr "after re-add ID 0" 3
3852		chk_mptcp_info subflows 3 subflows 3
3853
3854		pm_nl_del_endpoint $ns1 99 10.0.1.1
3855		sleep 0.5
3856		chk_subflow_nr "after re-delete ID 0" 2
3857		chk_mptcp_info subflows 2 subflows 2
3858
3859		pm_nl_add_endpoint $ns1 10.0.1.1 id 88 flags signal
3860		wait_mpj $ns2
3861		chk_subflow_nr "after re-re-add ID 0" 3
3862		chk_mptcp_info subflows 3 subflows 3
3863		mptcp_lib_kill_wait $tests_pid
3864
3865		kill_events_pids
3866		chk_evt_nr ns1 MPTCP_LIB_EVENT_LISTENER_CREATED 1
3867		chk_evt_nr ns1 MPTCP_LIB_EVENT_CREATED 1
3868		chk_evt_nr ns1 MPTCP_LIB_EVENT_ESTABLISHED 1
3869		chk_evt_nr ns1 MPTCP_LIB_EVENT_ANNOUNCED 0
3870		chk_evt_nr ns1 MPTCP_LIB_EVENT_REMOVED 0
3871		chk_evt_nr ns1 MPTCP_LIB_EVENT_SUB_ESTABLISHED 5
3872		chk_evt_nr ns1 MPTCP_LIB_EVENT_SUB_CLOSED 3
3873
3874		chk_evt_nr ns2 MPTCP_LIB_EVENT_CREATED 1
3875		chk_evt_nr ns2 MPTCP_LIB_EVENT_ESTABLISHED 1
3876		chk_evt_nr ns2 MPTCP_LIB_EVENT_ANNOUNCED 6
3877		chk_evt_nr ns2 MPTCP_LIB_EVENT_REMOVED 4
3878		chk_evt_nr ns2 MPTCP_LIB_EVENT_SUB_ESTABLISHED 5
3879		chk_evt_nr ns2 MPTCP_LIB_EVENT_SUB_CLOSED 3
3880
3881		join_connect_err=1 \
3882			chk_join_nr 5 5 5
3883		chk_add_nr 6 6
3884		chk_rm_nr 4 3 invert
3885	fi
3886
3887	# flush and re-add
3888	if reset_with_tcp_filter "flush re-add" ns2 10.0.3.2 REJECT OUTPUT &&
3889	   mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
3890		pm_nl_set_limits $ns1 0 2
3891		pm_nl_set_limits $ns2 1 2
3892		# broadcast IP: no packet for this address will be received on ns1
3893		pm_nl_add_endpoint $ns1 224.0.0.1 id 2 flags signal
3894		pm_nl_add_endpoint $ns2 10.0.3.2 id 3 flags subflow
3895		{ test_linkfail=4 speed=20 \
3896			run_tests $ns1 $ns2 10.0.1.1 & } 2>/dev/null
3897		local tests_pid=$!
3898
3899		wait_attempt_fail $ns2
3900		chk_subflow_nr "before flush" 1
3901		chk_mptcp_info subflows 0 subflows 0
3902
3903		pm_nl_flush_endpoint $ns2
3904		pm_nl_flush_endpoint $ns1
3905		wait_rm_addr $ns2 0
3906		ip netns exec "${ns2}" ${iptables} -D OUTPUT -s "10.0.3.2" -p tcp -j REJECT
3907		pm_nl_add_endpoint $ns2 10.0.3.2 id 3 flags subflow
3908		wait_mpj $ns2
3909		pm_nl_add_endpoint $ns1 10.0.3.1 id 2 flags signal
3910		wait_mpj $ns2
3911		mptcp_lib_kill_wait $tests_pid
3912
3913		join_syn_tx=3 join_connect_err=1 \
3914			chk_join_nr 2 2 2
3915		chk_add_nr 2 2
3916		chk_rm_nr 1 0 invert
3917	fi
3918}
3919
3920# [$1: error message]
3921usage()
3922{
3923	if [ -n "${1}" ]; then
3924		echo "${1}"
3925		ret=${KSFT_FAIL}
3926	fi
3927
3928	echo "mptcp_join usage:"
3929
3930	local key
3931	for key in "${!all_tests[@]}"; do
3932		echo "  -${key} ${all_tests[${key}]}"
3933	done
3934
3935	echo "  -c capture pcap files"
3936	echo "  -C enable data checksum"
3937	echo "  -i use ip mptcp"
3938	echo "  -h help"
3939
3940	echo "[test ids|names]"
3941
3942	exit ${ret}
3943}
3944
3945
3946# Use a "simple" array to force an specific order we cannot have with an associative one
3947all_tests_sorted=(
3948	f@subflows_tests
3949	e@subflows_error_tests
3950	s@signal_address_tests
3951	l@link_failure_tests
3952	t@add_addr_timeout_tests
3953	r@remove_tests
3954	a@add_tests
3955	6@ipv6_tests
3956	4@v4mapped_tests
3957	M@mixed_tests
3958	b@backup_tests
3959	p@add_addr_ports_tests
3960	k@syncookies_tests
3961	S@checksum_tests
3962	d@deny_join_id0_tests
3963	m@fullmesh_tests
3964	z@fastclose_tests
3965	F@fail_tests
3966	u@userspace_tests
3967	I@endpoint_tests
3968)
3969
3970all_tests_args=""
3971all_tests_names=()
3972for subtests in "${all_tests_sorted[@]}"; do
3973	key="${subtests%@*}"
3974	value="${subtests#*@}"
3975
3976	all_tests_args+="${key}"
3977	all_tests_names+=("${value}")
3978	all_tests[${key}]="${value}"
3979done
3980
3981tests=()
3982while getopts "${all_tests_args}cCih" opt; do
3983	case $opt in
3984		["${all_tests_args}"])
3985			tests+=("${all_tests[${opt}]}")
3986			;;
3987		c)
3988			capture=true
3989			;;
3990		C)
3991			checksum=true
3992			;;
3993		i)
3994			mptcp_lib_set_ip_mptcp
3995			;;
3996		h)
3997			usage
3998			;;
3999		*)
4000			usage "Unknown option: -${opt}"
4001			;;
4002	esac
4003done
4004
4005shift $((OPTIND - 1))
4006
4007for arg in "${@}"; do
4008	if [[ "${arg}" =~ ^[0-9]+$ ]]; then
4009		only_tests_ids+=("${arg}")
4010	else
4011		only_tests_names+=("${arg}")
4012	fi
4013done
4014
4015if [ ${#tests[@]} -eq 0 ]; then
4016	tests=("${all_tests_names[@]}")
4017fi
4018
4019mptcp_lib_subtests_last_ts_reset
4020for subtests in "${tests[@]}"; do
4021	"${subtests}"
4022done
4023append_prev_results
4024
4025if [ ${ret} -ne 0 ]; then
4026	echo
4027	echo "${#failed_tests[@]} failure(s) has(ve) been detected:"
4028	for i in $(get_failed_tests_ids); do
4029		echo -e "\t- ${i}: ${failed_tests[${i}]}"
4030	done
4031	echo
4032fi
4033
4034mptcp_lib_result_print_all_tap
4035
4036exit $ret
4037