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