1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ret=0
5sin=""
6sout=""
7cin=""
8cinsent=""
9cout=""
10ksft_skip=4
11timeout_poll=30
12timeout_test=$((timeout_poll * 2 + 1))
13mptcp_connect=""
14capture=0
15checksum=0
16do_all_tests=1
17
18TEST_COUNT=0
19
20# generated using "nfbpf_compile '(ip && (ip[54] & 0xf0) == 0x30) ||
21#				  (ip6 && (ip6[74] & 0xf0) == 0x30)'"
22CBPF_MPTCP_SUBOPTION_ADD_ADDR="14,
23			       48 0 0 0,
24			       84 0 0 240,
25			       21 0 3 64,
26			       48 0 0 54,
27			       84 0 0 240,
28			       21 6 7 48,
29			       48 0 0 0,
30			       84 0 0 240,
31			       21 0 4 96,
32			       48 0 0 74,
33			       84 0 0 240,
34			       21 0 1 48,
35			       6 0 0 65535,
36			       6 0 0 0"
37
38init()
39{
40	capout=$(mktemp)
41
42	rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
43
44	ns1="ns1-$rndh"
45	ns2="ns2-$rndh"
46
47	for netns in "$ns1" "$ns2";do
48		ip netns add $netns || exit $ksft_skip
49		ip -net $netns link set lo up
50		ip netns exec $netns sysctl -q net.mptcp.enabled=1
51		ip netns exec $netns sysctl -q net.ipv4.conf.all.rp_filter=0
52		ip netns exec $netns sysctl -q net.ipv4.conf.default.rp_filter=0
53		if [ $checksum -eq 1 ]; then
54			ip netns exec $netns sysctl -q net.mptcp.checksum_enabled=1
55		fi
56	done
57
58	#  ns1              ns2
59	# ns1eth1    ns2eth1
60	# ns1eth2    ns2eth2
61	# ns1eth3    ns2eth3
62	# ns1eth4    ns2eth4
63
64	for i in `seq 1 4`; do
65		ip link add ns1eth$i netns "$ns1" type veth peer name ns2eth$i netns "$ns2"
66		ip -net "$ns1" addr add 10.0.$i.1/24 dev ns1eth$i
67		ip -net "$ns1" addr add dead:beef:$i::1/64 dev ns1eth$i nodad
68		ip -net "$ns1" link set ns1eth$i up
69
70		ip -net "$ns2" addr add 10.0.$i.2/24 dev ns2eth$i
71		ip -net "$ns2" addr add dead:beef:$i::2/64 dev ns2eth$i nodad
72		ip -net "$ns2" link set ns2eth$i up
73
74		# let $ns2 reach any $ns1 address from any interface
75		ip -net "$ns2" route add default via 10.0.$i.1 dev ns2eth$i metric 10$i
76	done
77}
78
79cleanup_partial()
80{
81	rm -f "$capout"
82
83	for netns in "$ns1" "$ns2"; do
84		ip netns del $netns
85		rm -f /tmp/$netns.{nstat,out}
86	done
87}
88
89cleanup()
90{
91	rm -f "$cin" "$cout"
92	rm -f "$sin" "$sout" "$cinsent"
93	cleanup_partial
94}
95
96reset()
97{
98	cleanup_partial
99	init
100}
101
102reset_with_cookies()
103{
104	reset
105
106	for netns in "$ns1" "$ns2";do
107		ip netns exec $netns sysctl -q net.ipv4.tcp_syncookies=2
108	done
109}
110
111reset_with_add_addr_timeout()
112{
113	local ip="${1:-4}"
114	local tables
115
116	tables="iptables"
117	if [ $ip -eq 6 ]; then
118		tables="ip6tables"
119	fi
120
121	reset
122
123	ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
124	ip netns exec $ns2 $tables -A OUTPUT -p tcp \
125		-m tcp --tcp-option 30 \
126		-m bpf --bytecode \
127		"$CBPF_MPTCP_SUBOPTION_ADD_ADDR" \
128		-j DROP
129}
130
131reset_with_checksum()
132{
133	local ns1_enable=$1
134	local ns2_enable=$2
135
136	reset
137
138	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=$ns1_enable
139	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=$ns2_enable
140}
141
142reset_with_allow_join_id0()
143{
144	local ns1_enable=$1
145	local ns2_enable=$2
146
147	reset
148
149	ip netns exec $ns1 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns1_enable
150	ip netns exec $ns2 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns2_enable
151}
152
153ip -Version > /dev/null 2>&1
154if [ $? -ne 0 ];then
155	echo "SKIP: Could not run test without ip tool"
156	exit $ksft_skip
157fi
158
159iptables -V > /dev/null 2>&1
160if [ $? -ne 0 ];then
161	echo "SKIP: Could not run all tests without iptables tool"
162	exit $ksft_skip
163fi
164
165ip6tables -V > /dev/null 2>&1
166if [ $? -ne 0 ];then
167	echo "SKIP: Could not run all tests without ip6tables tool"
168	exit $ksft_skip
169fi
170
171print_file_err()
172{
173	ls -l "$1" 1>&2
174	echo "Trailing bytes are: "
175	tail -c 27 "$1"
176}
177
178check_transfer()
179{
180	in=$1
181	out=$2
182	what=$3
183
184	cmp "$in" "$out" > /dev/null 2>&1
185	if [ $? -ne 0 ] ;then
186		echo "[ FAIL ] $what does not match (in, out):"
187		print_file_err "$in"
188		print_file_err "$out"
189		ret=1
190
191		return 1
192	fi
193
194	return 0
195}
196
197do_ping()
198{
199	listener_ns="$1"
200	connector_ns="$2"
201	connect_addr="$3"
202
203	ip netns exec ${connector_ns} ping -q -c 1 $connect_addr >/dev/null
204	if [ $? -ne 0 ] ; then
205		echo "$listener_ns -> $connect_addr connectivity [ FAIL ]" 1>&2
206		ret=1
207	fi
208}
209
210link_failure()
211{
212	ns="$1"
213
214	l=$((RANDOM%4))
215	l=$((l+1))
216
217	veth="ns1eth$l"
218	ip -net "$ns" link set "$veth" down
219}
220
221# $1: IP address
222is_v6()
223{
224	[ -z "${1##*:*}" ]
225}
226
227do_transfer()
228{
229	listener_ns="$1"
230	connector_ns="$2"
231	cl_proto="$3"
232	srv_proto="$4"
233	connect_addr="$5"
234	test_link_fail="$6"
235	addr_nr_ns1="$7"
236	addr_nr_ns2="$8"
237	speed="$9"
238	bkup="${10}"
239
240	port=$((10000+$TEST_COUNT))
241	TEST_COUNT=$((TEST_COUNT+1))
242
243	:> "$cout"
244	:> "$sout"
245	:> "$capout"
246
247	if [ $capture -eq 1 ]; then
248		if [ -z $SUDO_USER ] ; then
249			capuser=""
250		else
251			capuser="-Z $SUDO_USER"
252		fi
253
254		capfile=$(printf "mp_join-%02u-%s.pcap" "$TEST_COUNT" "${listener_ns}")
255
256		echo "Capturing traffic for test $TEST_COUNT into $capfile"
257		ip netns exec ${listener_ns} tcpdump -i any -s 65535 -B 32768 $capuser -w $capfile > "$capout" 2>&1 &
258		cappid=$!
259
260		sleep 1
261	fi
262
263	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
264		nstat -n
265	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
266		nstat -n
267
268	if [ $speed = "fast" ]; then
269		mptcp_connect="./mptcp_connect -j"
270	elif [ $speed = "slow" ]; then
271		mptcp_connect="./mptcp_connect -r 50"
272	elif [ $speed = "least" ]; then
273		mptcp_connect="./mptcp_connect -r 10"
274	fi
275
276	local local_addr
277	if is_v6 "${connect_addr}"; then
278		local_addr="::"
279	else
280		local_addr="0.0.0.0"
281	fi
282
283	timeout ${timeout_test} \
284		ip netns exec ${listener_ns} \
285			$mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
286				${local_addr} < "$sin" > "$sout" &
287	spid=$!
288
289	sleep 1
290
291	if [ "$test_link_fail" -eq 0 ];then
292		timeout ${timeout_test} \
293			ip netns exec ${connector_ns} \
294				$mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
295					$connect_addr < "$cin" > "$cout" &
296	else
297		( cat "$cin" ; sleep 2; link_failure $listener_ns ; cat "$cin" ) | \
298			tee "$cinsent" | \
299			timeout ${timeout_test} \
300				ip netns exec ${connector_ns} \
301					$mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
302						$connect_addr > "$cout" &
303	fi
304	cpid=$!
305
306	if [ $addr_nr_ns1 -gt 0 ]; then
307		let add_nr_ns1=addr_nr_ns1
308		counter=2
309		sleep 1
310		while [ $add_nr_ns1 -gt 0 ]; do
311			local addr
312			if is_v6 "${connect_addr}"; then
313				addr="dead:beef:$counter::1"
314			else
315				addr="10.0.$counter.1"
316			fi
317			ip netns exec $ns1 ./pm_nl_ctl add $addr flags signal
318			let counter+=1
319			let add_nr_ns1-=1
320		done
321		sleep 1
322	elif [ $addr_nr_ns1 -lt 0 ]; then
323		let rm_nr_ns1=-addr_nr_ns1
324		if [ $rm_nr_ns1 -lt 8 ]; then
325			counter=1
326			dump=(`ip netns exec ${listener_ns} ./pm_nl_ctl dump`)
327			if [ ${#dump[@]} -gt 0 ]; then
328				id=${dump[1]}
329				sleep 1
330
331				while [ $counter -le $rm_nr_ns1 ]
332				do
333					ip netns exec ${listener_ns} ./pm_nl_ctl del $id
334					sleep 1
335					let counter+=1
336					let id+=1
337				done
338			fi
339		elif [ $rm_nr_ns1 -eq 8 ]; then
340			sleep 1
341			ip netns exec ${listener_ns} ./pm_nl_ctl flush
342		elif [ $rm_nr_ns1 -eq 9 ]; then
343			sleep 1
344			ip netns exec ${listener_ns} ./pm_nl_ctl del 0 ${connect_addr}
345		fi
346	fi
347
348	if [ $addr_nr_ns2 -gt 0 ]; then
349		let add_nr_ns2=addr_nr_ns2
350		counter=3
351		sleep 1
352		while [ $add_nr_ns2 -gt 0 ]; do
353			local addr
354			if is_v6 "${connect_addr}"; then
355				addr="dead:beef:$counter::2"
356			else
357				addr="10.0.$counter.2"
358			fi
359			ip netns exec $ns2 ./pm_nl_ctl add $addr flags subflow
360			let counter+=1
361			let add_nr_ns2-=1
362		done
363		sleep 1
364	elif [ $addr_nr_ns2 -lt 0 ]; then
365		let rm_nr_ns2=-addr_nr_ns2
366		if [ $rm_nr_ns2 -lt 8 ]; then
367			counter=1
368			dump=(`ip netns exec ${connector_ns} ./pm_nl_ctl dump`)
369			if [ ${#dump[@]} -gt 0 ]; then
370				id=${dump[1]}
371				sleep 1
372
373				while [ $counter -le $rm_nr_ns2 ]
374				do
375					ip netns exec ${connector_ns} ./pm_nl_ctl del $id
376					sleep 1
377					let counter+=1
378					let id+=1
379				done
380			fi
381		elif [ $rm_nr_ns2 -eq 8 ]; then
382			sleep 1
383			ip netns exec ${connector_ns} ./pm_nl_ctl flush
384		elif [ $rm_nr_ns2 -eq 9 ]; then
385			local addr
386			if is_v6 "${connect_addr}"; then
387				addr="dead:beef:1::2"
388			else
389				addr="10.0.1.2"
390			fi
391			sleep 1
392			ip netns exec ${connector_ns} ./pm_nl_ctl del 0 $addr
393		fi
394	fi
395
396	if [ ! -z $bkup ]; then
397		sleep 1
398		for netns in "$ns1" "$ns2"; do
399			dump=(`ip netns exec $netns ./pm_nl_ctl dump`)
400			if [ ${#dump[@]} -gt 0 ]; then
401				addr=${dump[${#dump[@]} - 1]}
402				backup="ip netns exec $netns ./pm_nl_ctl set $addr flags $bkup"
403				$backup
404			fi
405		done
406	fi
407
408	wait $cpid
409	retc=$?
410	wait $spid
411	rets=$?
412
413	if [ $capture -eq 1 ]; then
414	    sleep 1
415	    kill $cappid
416	fi
417
418	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
419		nstat | grep Tcp > /tmp/${listener_ns}.out
420	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
421		nstat | grep Tcp > /tmp/${connector_ns}.out
422
423	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
424		echo " client exit code $retc, server $rets" 1>&2
425		echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
426		ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
427		cat /tmp/${listener_ns}.out
428		echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
429		ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
430		cat /tmp/${connector_ns}.out
431
432		cat "$capout"
433		ret=1
434		return 1
435	fi
436
437	check_transfer $sin $cout "file received by client"
438	retc=$?
439	if [ "$test_link_fail" -eq 0 ];then
440		check_transfer $cin $sout "file received by server"
441	else
442		check_transfer $cinsent $sout "file received by server"
443	fi
444	rets=$?
445
446	if [ $retc -eq 0 ] && [ $rets -eq 0 ];then
447		cat "$capout"
448		return 0
449	fi
450
451	cat "$capout"
452	return 1
453}
454
455make_file()
456{
457	name=$1
458	who=$2
459	size=$3
460
461	dd if=/dev/urandom of="$name" bs=1024 count=$size 2> /dev/null
462	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
463
464	echo "Created $name (size $size KB) containing data sent by $who"
465}
466
467run_tests()
468{
469	listener_ns="$1"
470	connector_ns="$2"
471	connect_addr="$3"
472	test_linkfail="${4:-0}"
473	addr_nr_ns1="${5:-0}"
474	addr_nr_ns2="${6:-0}"
475	speed="${7:-fast}"
476	bkup="${8:-""}"
477	lret=0
478	oldin=""
479
480	if [ "$test_linkfail" -eq 1 ];then
481		size=$((RANDOM%1024))
482		size=$((size+1))
483		size=$((size*128))
484
485		oldin=$(mktemp)
486		cp "$cin" "$oldin"
487		make_file "$cin" "client" $size
488	fi
489
490	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr} \
491		${test_linkfail} ${addr_nr_ns1} ${addr_nr_ns2} ${speed} ${bkup}
492	lret=$?
493
494	if [ "$test_linkfail" -eq 1 ];then
495		cp "$oldin" "$cin"
496		rm -f "$oldin"
497	fi
498
499	if [ $lret -ne 0 ]; then
500		ret=$lret
501		return
502	fi
503}
504
505chk_csum_nr()
506{
507	local msg=${1:-""}
508	local count
509	local dump_stats
510
511	if [ ! -z "$msg" ]; then
512		printf "%02u" "$TEST_COUNT"
513	else
514		echo -n "  "
515	fi
516	printf " %-36s %s" "$msg" "sum"
517	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtDataCsumErr | awk '{print $2}'`
518	[ -z "$count" ] && count=0
519	if [ "$count" != 0 ]; then
520		echo "[fail] got $count data checksum error[s] expected 0"
521		ret=1
522		dump_stats=1
523	else
524		echo -n "[ ok ]"
525	fi
526	echo -n " - csum  "
527	count=`ip netns exec $ns2 nstat -as | grep MPTcpExtDataCsumErr | awk '{print $2}'`
528	[ -z "$count" ] && count=0
529	if [ "$count" != 0 ]; then
530		echo "[fail] got $count data checksum error[s] expected 0"
531		ret=1
532		dump_stats=1
533	else
534		echo "[ ok ]"
535	fi
536	if [ "${dump_stats}" = 1 ]; then
537		echo Server ns stats
538		ip netns exec $ns1 nstat -as | grep MPTcp
539		echo Client ns stats
540		ip netns exec $ns2 nstat -as | grep MPTcp
541	fi
542}
543
544chk_join_nr()
545{
546	local msg="$1"
547	local syn_nr=$2
548	local syn_ack_nr=$3
549	local ack_nr=$4
550	local count
551	local dump_stats
552
553	printf "%02u %-36s %s" "$TEST_COUNT" "$msg" "syn"
554	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinSynRx | awk '{print $2}'`
555	[ -z "$count" ] && count=0
556	if [ "$count" != "$syn_nr" ]; then
557		echo "[fail] got $count JOIN[s] syn expected $syn_nr"
558		ret=1
559		dump_stats=1
560	else
561		echo -n "[ ok ]"
562	fi
563
564	echo -n " - synack"
565	count=`ip netns exec $ns2 nstat -as | grep MPTcpExtMPJoinSynAckRx | awk '{print $2}'`
566	[ -z "$count" ] && count=0
567	if [ "$count" != "$syn_ack_nr" ]; then
568		echo "[fail] got $count JOIN[s] synack expected $syn_ack_nr"
569		ret=1
570		dump_stats=1
571	else
572		echo -n "[ ok ]"
573	fi
574
575	echo -n " - ack"
576	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinAckRx | awk '{print $2}'`
577	[ -z "$count" ] && count=0
578	if [ "$count" != "$ack_nr" ]; then
579		echo "[fail] got $count JOIN[s] ack expected $ack_nr"
580		ret=1
581		dump_stats=1
582	else
583		echo "[ ok ]"
584	fi
585	if [ "${dump_stats}" = 1 ]; then
586		echo Server ns stats
587		ip netns exec $ns1 nstat -as | grep MPTcp
588		echo Client ns stats
589		ip netns exec $ns2 nstat -as | grep MPTcp
590	fi
591	if [ $checksum -eq 1 ]; then
592		chk_csum_nr
593	fi
594}
595
596chk_add_nr()
597{
598	local add_nr=$1
599	local echo_nr=$2
600	local port_nr=${3:-0}
601	local syn_nr=${4:-$port_nr}
602	local syn_ack_nr=${5:-$port_nr}
603	local ack_nr=${6:-$port_nr}
604	local mis_syn_nr=${7:-0}
605	local mis_ack_nr=${8:-0}
606	local count
607	local dump_stats
608
609	printf "%-39s %s" " " "add"
610	count=`ip netns exec $ns2 nstat -as | grep MPTcpExtAddAddr | awk '{print $2}'`
611	[ -z "$count" ] && count=0
612	if [ "$count" != "$add_nr" ]; then
613		echo "[fail] got $count ADD_ADDR[s] expected $add_nr"
614		ret=1
615		dump_stats=1
616	else
617		echo -n "[ ok ]"
618	fi
619
620	echo -n " - echo  "
621	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtEchoAdd | awk '{print $2}'`
622	[ -z "$count" ] && count=0
623	if [ "$count" != "$echo_nr" ]; then
624		echo "[fail] got $count ADD_ADDR echo[s] expected $echo_nr"
625		ret=1
626		dump_stats=1
627	else
628		echo -n "[ ok ]"
629	fi
630
631	if [ $port_nr -gt 0 ]; then
632		echo -n " - pt "
633		count=`ip netns exec $ns2 nstat -as | grep MPTcpExtPortAdd | awk '{print $2}'`
634		[ -z "$count" ] && count=0
635		if [ "$count" != "$port_nr" ]; then
636			echo "[fail] got $count ADD_ADDR[s] with a port-number expected $port_nr"
637			ret=1
638			dump_stats=1
639		else
640			echo "[ ok ]"
641		fi
642
643		printf "%-39s %s" " " "syn"
644		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinPortSynRx |
645			awk '{print $2}'`
646		[ -z "$count" ] && count=0
647		if [ "$count" != "$syn_nr" ]; then
648			echo "[fail] got $count JOIN[s] syn with a different \
649				port-number expected $syn_nr"
650			ret=1
651			dump_stats=1
652		else
653			echo -n "[ ok ]"
654		fi
655
656		echo -n " - synack"
657		count=`ip netns exec $ns2 nstat -as | grep MPTcpExtMPJoinPortSynAckRx |
658			awk '{print $2}'`
659		[ -z "$count" ] && count=0
660		if [ "$count" != "$syn_ack_nr" ]; then
661			echo "[fail] got $count JOIN[s] synack with a different \
662				port-number expected $syn_ack_nr"
663			ret=1
664			dump_stats=1
665		else
666			echo -n "[ ok ]"
667		fi
668
669		echo -n " - ack"
670		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinPortAckRx |
671			awk '{print $2}'`
672		[ -z "$count" ] && count=0
673		if [ "$count" != "$ack_nr" ]; then
674			echo "[fail] got $count JOIN[s] ack with a different \
675				port-number expected $ack_nr"
676			ret=1
677			dump_stats=1
678		else
679			echo "[ ok ]"
680		fi
681
682		printf "%-39s %s" " " "syn"
683		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortSynRx |
684			awk '{print $2}'`
685		[ -z "$count" ] && count=0
686		if [ "$count" != "$mis_syn_nr" ]; then
687			echo "[fail] got $count JOIN[s] syn with a mismatched \
688				port-number expected $mis_syn_nr"
689			ret=1
690			dump_stats=1
691		else
692			echo -n "[ ok ]"
693		fi
694
695		echo -n " - ack   "
696		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortAckRx |
697			awk '{print $2}'`
698		[ -z "$count" ] && count=0
699		if [ "$count" != "$mis_ack_nr" ]; then
700			echo "[fail] got $count JOIN[s] ack with a mismatched \
701				port-number expected $mis_ack_nr"
702			ret=1
703			dump_stats=1
704		else
705			echo "[ ok ]"
706		fi
707	else
708		echo ""
709	fi
710
711	if [ "${dump_stats}" = 1 ]; then
712		echo Server ns stats
713		ip netns exec $ns1 nstat -as | grep MPTcp
714		echo Client ns stats
715		ip netns exec $ns2 nstat -as | grep MPTcp
716	fi
717}
718
719chk_rm_nr()
720{
721	local rm_addr_nr=$1
722	local rm_subflow_nr=$2
723	local invert=${3:-""}
724	local count
725	local dump_stats
726	local addr_ns
727	local subflow_ns
728
729	if [ -z $invert ]; then
730		addr_ns=$ns1
731		subflow_ns=$ns2
732	elif [ $invert = "invert" ]; then
733		addr_ns=$ns2
734		subflow_ns=$ns1
735	fi
736
737	printf "%-39s %s" " " "rm "
738	count=`ip netns exec $addr_ns nstat -as | grep MPTcpExtRmAddr | awk '{print $2}'`
739	[ -z "$count" ] && count=0
740	if [ "$count" != "$rm_addr_nr" ]; then
741		echo "[fail] got $count RM_ADDR[s] expected $rm_addr_nr"
742		ret=1
743		dump_stats=1
744	else
745		echo -n "[ ok ]"
746	fi
747
748	echo -n " - sf    "
749	count=`ip netns exec $subflow_ns nstat -as | grep MPTcpExtRmSubflow | awk '{print $2}'`
750	[ -z "$count" ] && count=0
751	if [ "$count" != "$rm_subflow_nr" ]; then
752		echo "[fail] got $count RM_SUBFLOW[s] expected $rm_subflow_nr"
753		ret=1
754		dump_stats=1
755	else
756		echo "[ ok ]"
757	fi
758
759	if [ "${dump_stats}" = 1 ]; then
760		echo Server ns stats
761		ip netns exec $ns1 nstat -as | grep MPTcp
762		echo Client ns stats
763		ip netns exec $ns2 nstat -as | grep MPTcp
764	fi
765}
766
767chk_prio_nr()
768{
769	local mp_prio_nr_tx=$1
770	local mp_prio_nr_rx=$2
771	local count
772	local dump_stats
773
774	printf "%-39s %s" " " "ptx"
775	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPPrioTx | awk '{print $2}'`
776	[ -z "$count" ] && count=0
777	if [ "$count" != "$mp_prio_nr_tx" ]; then
778		echo "[fail] got $count MP_PRIO[s] TX expected $mp_prio_nr_tx"
779		ret=1
780		dump_stats=1
781	else
782		echo -n "[ ok ]"
783	fi
784
785	echo -n " - prx   "
786	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPPrioRx | awk '{print $2}'`
787	[ -z "$count" ] && count=0
788	if [ "$count" != "$mp_prio_nr_rx" ]; then
789		echo "[fail] got $count MP_PRIO[s] RX expected $mp_prio_nr_rx"
790		ret=1
791		dump_stats=1
792	else
793		echo "[ ok ]"
794	fi
795
796	if [ "${dump_stats}" = 1 ]; then
797		echo Server ns stats
798		ip netns exec $ns1 nstat -as | grep MPTcp
799		echo Client ns stats
800		ip netns exec $ns2 nstat -as | grep MPTcp
801	fi
802}
803
804subflows_tests()
805{
806	reset
807	run_tests $ns1 $ns2 10.0.1.1
808	chk_join_nr "no JOIN" "0" "0" "0"
809
810	# subflow limited by client
811	reset
812	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
813	run_tests $ns1 $ns2 10.0.1.1
814	chk_join_nr "single subflow, limited by client" 0 0 0
815
816	# subflow limited by server
817	reset
818	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
819	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
820	run_tests $ns1 $ns2 10.0.1.1
821	chk_join_nr "single subflow, limited by server" 1 1 0
822
823	# subflow
824	reset
825	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
826	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
827	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
828	run_tests $ns1 $ns2 10.0.1.1
829	chk_join_nr "single subflow" 1 1 1
830
831	# multiple subflows
832	reset
833	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
834	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
835	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
836	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
837	run_tests $ns1 $ns2 10.0.1.1
838	chk_join_nr "multiple subflows" 2 2 2
839
840	# multiple subflows limited by serverf
841	reset
842	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
843	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
844	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
845	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
846	run_tests $ns1 $ns2 10.0.1.1
847	chk_join_nr "multiple subflows, limited by server" 2 2 1
848
849	# single subflow, dev
850	reset
851	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
852	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
853	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow dev ns2eth3
854	run_tests $ns1 $ns2 10.0.1.1
855	chk_join_nr "single subflow, dev" 1 1 1
856}
857
858signal_address_tests()
859{
860	# add_address, unused
861	reset
862	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
863	run_tests $ns1 $ns2 10.0.1.1
864	chk_join_nr "unused signal address" 0 0 0
865	chk_add_nr 1 1
866
867	# accept and use add_addr
868	reset
869	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
870	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
871	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
872	run_tests $ns1 $ns2 10.0.1.1
873	chk_join_nr "signal address" 1 1 1
874	chk_add_nr 1 1
875
876	# accept and use add_addr with an additional subflow
877	# note: signal address in server ns and local addresses in client ns must
878	# belong to different subnets or one of the listed local address could be
879	# used for 'add_addr' subflow
880	reset
881	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
882	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
883	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
884	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
885	run_tests $ns1 $ns2 10.0.1.1
886	chk_join_nr "subflow and signal" 2 2 2
887	chk_add_nr 1 1
888
889	# accept and use add_addr with additional subflows
890	reset
891	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
892	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
893	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
894	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
895	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
896	run_tests $ns1 $ns2 10.0.1.1
897	chk_join_nr "multiple subflows and signal" 3 3 3
898	chk_add_nr 1 1
899
900	# signal addresses
901	reset
902	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
903	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
904	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
905	ip netns exec $ns1 ./pm_nl_ctl add 10.0.4.1 flags signal
906	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
907	run_tests $ns1 $ns2 10.0.1.1
908	chk_join_nr "signal addresses" 3 3 3
909	chk_add_nr 3 3
910
911	# signal invalid addresses
912	reset
913	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
914	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
915	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
916	ip netns exec $ns1 ./pm_nl_ctl add 10.0.14.1 flags signal
917	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
918	run_tests $ns1 $ns2 10.0.1.1
919	chk_join_nr "signal invalid addresses" 1 1 1
920	chk_add_nr 3 3
921}
922
923link_failure_tests()
924{
925	# accept and use add_addr with additional subflows and link loss
926	reset
927	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
928	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
929	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
930	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
931	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
932	run_tests $ns1 $ns2 10.0.1.1 1
933	chk_join_nr "multiple flows, signal, link failure" 3 3 3
934	chk_add_nr 1 1
935}
936
937add_addr_timeout_tests()
938{
939	# add_addr timeout
940	reset_with_add_addr_timeout
941	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
942	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
943	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
944	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
945	chk_join_nr "signal address, ADD_ADDR timeout" 1 1 1
946	chk_add_nr 4 0
947
948	# add_addr timeout IPv6
949	reset_with_add_addr_timeout 6
950	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
951	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
952	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
953	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
954	chk_join_nr "signal address, ADD_ADDR6 timeout" 1 1 1
955	chk_add_nr 4 0
956
957	# signal addresses timeout
958	reset_with_add_addr_timeout
959	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
960	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
961	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
962	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
963	run_tests $ns1 $ns2 10.0.1.1 0 0 0 least
964	chk_join_nr "signal addresses, ADD_ADDR timeout" 2 2 2
965	chk_add_nr 8 0
966
967	# signal invalid addresses timeout
968	reset_with_add_addr_timeout
969	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
970	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
971	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
972	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
973	run_tests $ns1 $ns2 10.0.1.1 0 0 0 least
974	chk_join_nr "invalid address, ADD_ADDR timeout" 1 1 1
975	chk_add_nr 8 0
976}
977
978remove_tests()
979{
980	# single subflow, remove
981	reset
982	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
983	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
984	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
985	run_tests $ns1 $ns2 10.0.1.1 0 0 -1 slow
986	chk_join_nr "remove single subflow" 1 1 1
987	chk_rm_nr 1 1
988
989	# multiple subflows, remove
990	reset
991	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
992	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
993	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
994	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
995	run_tests $ns1 $ns2 10.0.1.1 0 0 -2 slow
996	chk_join_nr "remove multiple subflows" 2 2 2
997	chk_rm_nr 2 2
998
999	# single address, remove
1000	reset
1001	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1002	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1003	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1004	run_tests $ns1 $ns2 10.0.1.1 0 -1 0 slow
1005	chk_join_nr "remove single address" 1 1 1
1006	chk_add_nr 1 1
1007	chk_rm_nr 1 1 invert
1008
1009	# subflow and signal, remove
1010	reset
1011	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1012	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1013	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1014	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1015	run_tests $ns1 $ns2 10.0.1.1 0 -1 -1 slow
1016	chk_join_nr "remove subflow and signal" 2 2 2
1017	chk_add_nr 1 1
1018	chk_rm_nr 1 1
1019
1020	# subflows and signal, remove
1021	reset
1022	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1023	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1024	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1025	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1026	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1027	run_tests $ns1 $ns2 10.0.1.1 0 -1 -2 slow
1028	chk_join_nr "remove subflows and signal" 3 3 3
1029	chk_add_nr 1 1
1030	chk_rm_nr 2 2
1031
1032	# addresses remove
1033	reset
1034	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1035	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal id 250
1036	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1037	ip netns exec $ns1 ./pm_nl_ctl add 10.0.4.1 flags signal
1038	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1039	run_tests $ns1 $ns2 10.0.1.1 0 -3 0 slow
1040	chk_join_nr "remove addresses" 3 3 3
1041	chk_add_nr 3 3
1042	chk_rm_nr 3 3 invert
1043
1044	# invalid addresses remove
1045	reset
1046	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1047	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
1048	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1049	ip netns exec $ns1 ./pm_nl_ctl add 10.0.14.1 flags signal
1050	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1051	run_tests $ns1 $ns2 10.0.1.1 0 -3 0 slow
1052	chk_join_nr "remove invalid addresses" 1 1 1
1053	chk_add_nr 3 3
1054	chk_rm_nr 3 1 invert
1055
1056	# subflows and signal, flush
1057	reset
1058	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1059	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1060	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1061	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1062	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1063	run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
1064	chk_join_nr "flush subflows and signal" 3 3 3
1065	chk_add_nr 1 1
1066	chk_rm_nr 2 2
1067
1068	# subflows flush
1069	reset
1070	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1071	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1072	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow id 150
1073	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1074	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1075	run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
1076	chk_join_nr "flush subflows" 3 3 3
1077	chk_rm_nr 3 3
1078
1079	# addresses flush
1080	reset
1081	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1082	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal id 250
1083	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1084	ip netns exec $ns1 ./pm_nl_ctl add 10.0.4.1 flags signal
1085	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1086	run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
1087	chk_join_nr "flush addresses" 3 3 3
1088	chk_add_nr 3 3
1089	chk_rm_nr 3 3 invert
1090
1091	# invalid addresses flush
1092	reset
1093	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1094	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
1095	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1096	ip netns exec $ns1 ./pm_nl_ctl add 10.0.14.1 flags signal
1097	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1098	run_tests $ns1 $ns2 10.0.1.1 0 -8 0 slow
1099	chk_join_nr "flush invalid addresses" 1 1 1
1100	chk_add_nr 3 3
1101	chk_rm_nr 3 1 invert
1102
1103	# remove id 0 subflow
1104	reset
1105	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1106	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1107	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1108	run_tests $ns1 $ns2 10.0.1.1 0 0 -9 slow
1109	chk_join_nr "remove id 0 subflow" 1 1 1
1110	chk_rm_nr 1 1
1111
1112	# remove id 0 address
1113	reset
1114	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1115	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1116	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1117	run_tests $ns1 $ns2 10.0.1.1 0 -9 0 slow
1118	chk_join_nr "remove id 0 address" 1 1 1
1119	chk_add_nr 1 1
1120	chk_rm_nr 1 1 invert
1121}
1122
1123add_tests()
1124{
1125	# add single subflow
1126	reset
1127	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1128	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1129	run_tests $ns1 $ns2 10.0.1.1 0 0 1 slow
1130	chk_join_nr "add single subflow" 1 1 1
1131
1132	# add signal address
1133	reset
1134	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1135	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1136	run_tests $ns1 $ns2 10.0.1.1 0 1 0 slow
1137	chk_join_nr "add signal address" 1 1 1
1138	chk_add_nr 1 1
1139
1140	# add multiple subflows
1141	reset
1142	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1143	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1144	run_tests $ns1 $ns2 10.0.1.1 0 0 2 slow
1145	chk_join_nr "add multiple subflows" 2 2 2
1146
1147	# add multiple subflows IPv6
1148	reset
1149	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1150	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1151	run_tests $ns1 $ns2 dead:beef:1::1 0 0 2 slow
1152	chk_join_nr "add multiple subflows IPv6" 2 2 2
1153
1154	# add multiple addresses IPv6
1155	reset
1156	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1157	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1158	run_tests $ns1 $ns2 dead:beef:1::1 0 2 0 slow
1159	chk_join_nr "add multiple addresses IPv6" 2 2 2
1160	chk_add_nr 2 2
1161}
1162
1163ipv6_tests()
1164{
1165	# subflow IPv6
1166	reset
1167	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1168	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1169	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:3::2 flags subflow
1170	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
1171	chk_join_nr "single subflow IPv6" 1 1 1
1172
1173	# add_address, unused IPv6
1174	reset
1175	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1176	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
1177	chk_join_nr "unused signal address IPv6" 0 0 0
1178	chk_add_nr 1 1
1179
1180	# signal address IPv6
1181	reset
1182	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1183	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1184	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1185	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
1186	chk_join_nr "single address IPv6" 1 1 1
1187	chk_add_nr 1 1
1188
1189	# single address IPv6, remove
1190	reset
1191	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1192	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1193	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1194	run_tests $ns1 $ns2 dead:beef:1::1 0 -1 0 slow
1195	chk_join_nr "remove single address IPv6" 1 1 1
1196	chk_add_nr 1 1
1197	chk_rm_nr 1 1 invert
1198
1199	# subflow and signal IPv6, remove
1200	reset
1201	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1202	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1203	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1204	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:3::2 flags subflow
1205	run_tests $ns1 $ns2 dead:beef:1::1 0 -1 -1 slow
1206	chk_join_nr "remove subflow and signal IPv6" 2 2 2
1207	chk_add_nr 1 1
1208	chk_rm_nr 1 1
1209}
1210
1211v4mapped_tests()
1212{
1213	# subflow IPv4-mapped to IPv4-mapped
1214	reset
1215	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1216	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1217	ip netns exec $ns2 ./pm_nl_ctl add "::ffff:10.0.3.2" flags subflow
1218	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1219	chk_join_nr "single subflow IPv4-mapped" 1 1 1
1220
1221	# signal address IPv4-mapped with IPv4-mapped sk
1222	reset
1223	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1224	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1225	ip netns exec $ns1 ./pm_nl_ctl add "::ffff:10.0.2.1" flags signal
1226	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1227	chk_join_nr "signal address IPv4-mapped" 1 1 1
1228	chk_add_nr 1 1
1229
1230	# subflow v4-map-v6
1231	reset
1232	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1233	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1234	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1235	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1236	chk_join_nr "single subflow v4-map-v6" 1 1 1
1237
1238	# signal address v4-map-v6
1239	reset
1240	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1241	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1242	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1243	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1244	chk_join_nr "signal address v4-map-v6" 1 1 1
1245	chk_add_nr 1 1
1246
1247	# subflow v6-map-v4
1248	reset
1249	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1250	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1251	ip netns exec $ns2 ./pm_nl_ctl add "::ffff:10.0.3.2" flags subflow
1252	run_tests $ns1 $ns2 10.0.1.1
1253	chk_join_nr "single subflow v6-map-v4" 1 1 1
1254
1255	# signal address v6-map-v4
1256	reset
1257	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1258	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1259	ip netns exec $ns1 ./pm_nl_ctl add "::ffff:10.0.2.1" flags signal
1260	run_tests $ns1 $ns2 10.0.1.1
1261	chk_join_nr "signal address v6-map-v4" 1 1 1
1262	chk_add_nr 1 1
1263
1264	# no subflow IPv6 to v4 address
1265	reset
1266	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1267	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1268	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:2::2 flags subflow
1269	run_tests $ns1 $ns2 10.0.1.1
1270	chk_join_nr "no JOIN with diff families v4-v6" 0 0 0
1271
1272	# no subflow IPv6 to v4 address even if v6 has a valid v4 at the end
1273	reset
1274	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1275	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1276	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:2::10.0.3.2 flags subflow
1277	run_tests $ns1 $ns2 10.0.1.1
1278	chk_join_nr "no JOIN with diff families v4-v6-2" 0 0 0
1279
1280	# no subflow IPv4 to v6 address, no need to slow down too then
1281	reset
1282	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1283	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1284	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1285	run_tests $ns1 $ns2 dead:beef:1::1
1286	chk_join_nr "no JOIN with diff families v6-v4" 0 0 0
1287}
1288
1289backup_tests()
1290{
1291	# single subflow, backup
1292	reset
1293	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1294	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1295	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow,backup
1296	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow nobackup
1297	chk_join_nr "single subflow, backup" 1 1 1
1298	chk_prio_nr 0 1
1299
1300	# single address, backup
1301	reset
1302	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1303	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1304	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1305	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
1306	chk_join_nr "single address, backup" 1 1 1
1307	chk_add_nr 1 1
1308	chk_prio_nr 1 0
1309}
1310
1311add_addr_ports_tests()
1312{
1313	# signal address with port
1314	reset
1315	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1316	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1317	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1318	run_tests $ns1 $ns2 10.0.1.1
1319	chk_join_nr "signal address with port" 1 1 1
1320	chk_add_nr 1 1 1
1321
1322	# subflow and signal with port
1323	reset
1324	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1325	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1326	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1327	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1328	run_tests $ns1 $ns2 10.0.1.1
1329	chk_join_nr "subflow and signal with port" 2 2 2
1330	chk_add_nr 1 1 1
1331
1332	# single address with port, remove
1333	reset
1334	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1335	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1336	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1337	run_tests $ns1 $ns2 10.0.1.1 0 -1 0 slow
1338	chk_join_nr "remove single address with port" 1 1 1
1339	chk_add_nr 1 1 1
1340	chk_rm_nr 1 1 invert
1341
1342	# subflow and signal with port, remove
1343	reset
1344	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1345	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1346	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1347	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1348	run_tests $ns1 $ns2 10.0.1.1 0 -1 -1 slow
1349	chk_join_nr "remove subflow and signal with port" 2 2 2
1350	chk_add_nr 1 1 1
1351	chk_rm_nr 1 1
1352
1353	# subflows and signal with port, flush
1354	reset
1355	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1356	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1357	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1358	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1359	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1360	run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
1361	chk_join_nr "flush subflows and signal with port" 3 3 3
1362	chk_add_nr 1 1
1363	chk_rm_nr 2 2
1364
1365	# multiple addresses with port
1366	reset
1367	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1368	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1369	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal port 10100
1370	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1371	run_tests $ns1 $ns2 10.0.1.1
1372	chk_join_nr "multiple addresses with port" 2 2 2
1373	chk_add_nr 2 2 2
1374
1375	# multiple addresses with ports
1376	reset
1377	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1378	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1379	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal port 10101
1380	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1381	run_tests $ns1 $ns2 10.0.1.1
1382	chk_join_nr "multiple addresses with ports" 2 2 2
1383	chk_add_nr 2 2 2
1384}
1385
1386syncookies_tests()
1387{
1388	# single subflow, syncookies
1389	reset_with_cookies
1390	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1391	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1392	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1393	run_tests $ns1 $ns2 10.0.1.1
1394	chk_join_nr "single subflow with syn cookies" 1 1 1
1395
1396	# multiple subflows with syn cookies
1397	reset_with_cookies
1398	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1399	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1400	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1401	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1402	run_tests $ns1 $ns2 10.0.1.1
1403	chk_join_nr "multiple subflows with syn cookies" 2 2 2
1404
1405	# multiple subflows limited by server
1406	reset_with_cookies
1407	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1408	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1409	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1410	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1411	run_tests $ns1 $ns2 10.0.1.1
1412	chk_join_nr "subflows limited by server w cookies" 2 2 1
1413
1414	# test signal address with cookies
1415	reset_with_cookies
1416	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1417	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1418	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1419	run_tests $ns1 $ns2 10.0.1.1
1420	chk_join_nr "signal address with syn cookies" 1 1 1
1421	chk_add_nr 1 1
1422
1423	# test cookie with subflow and signal
1424	reset_with_cookies
1425	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1426	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1427	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1428	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1429	run_tests $ns1 $ns2 10.0.1.1
1430	chk_join_nr "subflow and signal w cookies" 2 2 2
1431	chk_add_nr 1 1
1432
1433	# accept and use add_addr with additional subflows
1434	reset_with_cookies
1435	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1436	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1437	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1438	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1439	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1440	run_tests $ns1 $ns2 10.0.1.1
1441	chk_join_nr "subflows and signal w. cookies" 3 3 3
1442	chk_add_nr 1 1
1443}
1444
1445checksum_tests()
1446{
1447	# checksum test 0 0
1448	reset_with_checksum 0 0
1449	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1450	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1451	run_tests $ns1 $ns2 10.0.1.1
1452	chk_csum_nr "checksum test 0 0"
1453
1454	# checksum test 1 1
1455	reset_with_checksum 1 1
1456	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1457	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1458	run_tests $ns1 $ns2 10.0.1.1
1459	chk_csum_nr "checksum test 1 1"
1460
1461	# checksum test 0 1
1462	reset_with_checksum 0 1
1463	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1464	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1465	run_tests $ns1 $ns2 10.0.1.1
1466	chk_csum_nr "checksum test 0 1"
1467
1468	# checksum test 1 0
1469	reset_with_checksum 1 0
1470	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1471	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1472	run_tests $ns1 $ns2 10.0.1.1
1473	chk_csum_nr "checksum test 1 0"
1474}
1475
1476deny_join_id0_tests()
1477{
1478	# subflow allow join id0 ns1
1479	reset_with_allow_join_id0 1 0
1480	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1481	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1482	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1483	run_tests $ns1 $ns2 10.0.1.1
1484	chk_join_nr "single subflow allow join id0 ns1" 1 1 1
1485
1486	# subflow allow join id0 ns2
1487	reset_with_allow_join_id0 0 1
1488	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1489	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1490	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1491	run_tests $ns1 $ns2 10.0.1.1
1492	chk_join_nr "single subflow allow join id0 ns2" 0 0 0
1493
1494	# signal address allow join id0 ns1
1495	# ADD_ADDRs are not affected by allow_join_id0 value.
1496	reset_with_allow_join_id0 1 0
1497	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1498	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1499	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1500	run_tests $ns1 $ns2 10.0.1.1
1501	chk_join_nr "signal address allow join id0 ns1" 1 1 1
1502	chk_add_nr 1 1
1503
1504	# signal address allow join id0 ns2
1505	# ADD_ADDRs are not affected by allow_join_id0 value.
1506	reset_with_allow_join_id0 0 1
1507	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1508	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1509	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1510	run_tests $ns1 $ns2 10.0.1.1
1511	chk_join_nr "signal address allow join id0 ns2" 1 1 1
1512	chk_add_nr 1 1
1513
1514	# subflow and address allow join id0 ns1
1515	reset_with_allow_join_id0 1 0
1516	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1517	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1518	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1519	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1520	run_tests $ns1 $ns2 10.0.1.1
1521	chk_join_nr "subflow and address allow join id0 1" 2 2 2
1522
1523	# subflow and address allow join id0 ns2
1524	reset_with_allow_join_id0 0 1
1525	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1526	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1527	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1528	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1529	run_tests $ns1 $ns2 10.0.1.1
1530	chk_join_nr "subflow and address allow join id0 2" 1 1 1
1531}
1532
1533all_tests()
1534{
1535	subflows_tests
1536	signal_address_tests
1537	link_failure_tests
1538	add_addr_timeout_tests
1539	remove_tests
1540	add_tests
1541	ipv6_tests
1542	v4mapped_tests
1543	backup_tests
1544	add_addr_ports_tests
1545	syncookies_tests
1546	checksum_tests
1547	deny_join_id0_tests
1548}
1549
1550usage()
1551{
1552	echo "mptcp_join usage:"
1553	echo "  -f subflows_tests"
1554	echo "  -s signal_address_tests"
1555	echo "  -l link_failure_tests"
1556	echo "  -t add_addr_timeout_tests"
1557	echo "  -r remove_tests"
1558	echo "  -a add_tests"
1559	echo "  -6 ipv6_tests"
1560	echo "  -4 v4mapped_tests"
1561	echo "  -b backup_tests"
1562	echo "  -p add_addr_ports_tests"
1563	echo "  -k syncookies_tests"
1564	echo "  -S checksum_tests"
1565	echo "  -d deny_join_id0_tests"
1566	echo "  -c capture pcap files"
1567	echo "  -C enable data checksum"
1568	echo "  -h help"
1569}
1570
1571sin=$(mktemp)
1572sout=$(mktemp)
1573cin=$(mktemp)
1574cinsent=$(mktemp)
1575cout=$(mktemp)
1576init
1577make_file "$cin" "client" 1
1578make_file "$sin" "server" 1
1579trap cleanup EXIT
1580
1581for arg in "$@"; do
1582	# check for "capture/checksum" args before launching tests
1583	if [[ "${arg}" =~ ^"-"[0-9a-zA-Z]*"c"[0-9a-zA-Z]*$ ]]; then
1584		capture=1
1585	fi
1586	if [[ "${arg}" =~ ^"-"[0-9a-zA-Z]*"C"[0-9a-zA-Z]*$ ]]; then
1587		checksum=1
1588	fi
1589
1590	# exception for the capture/checksum options, the rest means: a part of the tests
1591	if [ "${arg}" != "-c" ] && [ "${arg}" != "-C" ]; then
1592		do_all_tests=0
1593	fi
1594done
1595
1596if [ $do_all_tests -eq 1 ]; then
1597	all_tests
1598	exit $ret
1599fi
1600
1601while getopts 'fsltra64bpkdchCS' opt; do
1602	case $opt in
1603		f)
1604			subflows_tests
1605			;;
1606		s)
1607			signal_address_tests
1608			;;
1609		l)
1610			link_failure_tests
1611			;;
1612		t)
1613			add_addr_timeout_tests
1614			;;
1615		r)
1616			remove_tests
1617			;;
1618		a)
1619			add_tests
1620			;;
1621		6)
1622			ipv6_tests
1623			;;
1624		4)
1625			v4mapped_tests
1626			;;
1627		b)
1628			backup_tests
1629			;;
1630		p)
1631			add_addr_ports_tests
1632			;;
1633		k)
1634			syncookies_tests
1635			;;
1636		S)
1637			checksum_tests
1638			;;
1639		d)
1640			deny_join_id0_tests
1641			;;
1642		c)
1643			;;
1644		C)
1645			;;
1646		h | *)
1647			usage
1648			;;
1649	esac
1650done
1651
1652exit $ret
1653