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