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