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