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