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