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