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