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