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