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