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.
6#shellcheck disable=SC2086
7
8# Some variables are used below but indirectly, see check_expected_one()
9#shellcheck disable=SC2034
10
11. "$(dirname "${0}")/mptcp_lib.sh"
12
13mptcp_lib_check_mptcp
14mptcp_lib_check_kallsyms
15
16if ! mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
17	echo "userspace pm tests are not supported by the kernel: SKIP"
18	exit ${KSFT_SKIP}
19fi
20
21if ! ip -Version &> /dev/null; then
22	echo "SKIP: Cannot not run test without ip tool"
23	exit ${KSFT_SKIP}
24fi
25
26ANNOUNCED=${MPTCP_LIB_EVENT_ANNOUNCED}
27REMOVED=${MPTCP_LIB_EVENT_REMOVED}
28SUB_ESTABLISHED=${MPTCP_LIB_EVENT_SUB_ESTABLISHED}
29SUB_CLOSED=${MPTCP_LIB_EVENT_SUB_CLOSED}
30LISTENER_CREATED=${MPTCP_LIB_EVENT_LISTENER_CREATED}
31LISTENER_CLOSED=${MPTCP_LIB_EVENT_LISTENER_CLOSED}
32
33AF_INET=${MPTCP_LIB_AF_INET}
34AF_INET6=${MPTCP_LIB_AF_INET6}
35
36file=""
37server_evts=""
38client_evts=""
39server_evts_pid=0
40client_evts_pid=0
41client4_pid=0
42server4_pid=0
43client6_pid=0
44server6_pid=0
45client4_token=""
46server4_token=""
47client6_token=""
48server6_token=""
49client4_port=0;
50client6_port=0;
51app4_port=50002
52new4_port=50003
53app6_port=50004
54client_addr_id=${RANDOM:0:2}
55server_addr_id=${RANDOM:0:2}
56
57sec=$(date +%s)
58rndh=$(printf %x "$sec")-$(mktemp -u XXXXXX)
59ns1="ns1-$rndh"
60ns2="ns2-$rndh"
61ret=0
62test_name=""
63
64_printf() {
65	stdbuf -o0 -e0 printf "${@}"
66}
67
68print_title()
69{
70	_printf "INFO: %s\n" "${1}"
71}
72
73# $1: test name
74print_test()
75{
76	test_name="${1}"
77
78	_printf "%-68s" "${test_name}"
79}
80
81print_results()
82{
83	_printf "[%s]\n" "${1}"
84}
85
86test_pass()
87{
88	print_results " OK "
89	mptcp_lib_result_pass "${test_name}"
90}
91
92test_skip()
93{
94	print_results "SKIP"
95	mptcp_lib_result_skip "${test_name}"
96}
97
98# $1: msg
99test_fail()
100{
101	print_results "FAIL"
102	ret=1
103
104	if [ -n "${1}" ]; then
105		_printf "\t%s\n" "${1}"
106	fi
107
108	mptcp_lib_result_fail "${test_name}"
109}
110
111# This function is used in the cleanup trap
112#shellcheck disable=SC2317
113cleanup()
114{
115	print_title "Cleanup"
116
117	# Terminate the MPTCP connection and related processes
118	local pid
119	for pid in $client4_pid $server4_pid $client6_pid $server6_pid\
120		   $server_evts_pid $client_evts_pid
121	do
122		mptcp_lib_kill_wait $pid
123	done
124
125	local netns
126	for netns in "$ns1" "$ns2" ;do
127		ip netns del "$netns"
128	done
129
130	rm -rf $file $client_evts $server_evts
131
132	_printf "Done\n"
133}
134
135trap cleanup EXIT
136
137# Create and configure network namespaces for testing
138for i in "$ns1" "$ns2" ;do
139	ip netns add "$i" || exit 1
140	ip -net "$i" link set lo up
141	ip netns exec "$i" sysctl -q net.mptcp.enabled=1
142	ip netns exec "$i" sysctl -q net.mptcp.pm_type=1
143done
144
145#  "$ns1"              ns2
146#     ns1eth2    ns2eth1
147
148ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
149
150# Add IPv4/v6 addresses to the namespaces
151ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth2
152ip -net "$ns1" addr add 10.0.2.1/24 dev ns1eth2
153ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth2 nodad
154ip -net "$ns1" addr add dead:beef:2::1/64 dev ns1eth2 nodad
155ip -net "$ns1" link set ns1eth2 up
156
157ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
158ip -net "$ns2" addr add 10.0.2.2/24 dev ns2eth1
159ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1 nodad
160ip -net "$ns2" addr add dead:beef:2::2/64 dev ns2eth1 nodad
161ip -net "$ns2" link set ns2eth1 up
162
163print_title "Init"
164print_test "Created network namespaces ns1, ns2"
165test_pass
166
167make_file()
168{
169	# Store a chunk of data in a file to transmit over an MPTCP connection
170	local name=$1
171	local ksize=1
172
173	dd if=/dev/urandom of="$name" bs=2 count=$ksize 2> /dev/null
174	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
175}
176
177make_connection()
178{
179	if [ -z "$file" ]; then
180		file=$(mktemp)
181	fi
182	make_file "$file" "client"
183
184	local is_v6=$1
185	local app_port=$app4_port
186	local connect_addr="10.0.1.1"
187	local client_addr="10.0.1.2"
188	local listen_addr="0.0.0.0"
189	if [ "$is_v6" = "v6" ]
190	then
191		connect_addr="dead:beef:1::1"
192		client_addr="dead:beef:1::2"
193		listen_addr="::"
194		app_port=$app6_port
195	else
196		is_v6="v4"
197	fi
198
199	# Capture netlink events over the two network namespaces running
200	# the MPTCP client and server
201	if [ -z "$client_evts" ]; then
202		client_evts=$(mktemp)
203	fi
204	mptcp_lib_events "${ns2}" "${client_evts}" client_evts_pid
205	if [ -z "$server_evts" ]; then
206		server_evts=$(mktemp)
207	fi
208	mptcp_lib_events "${ns1}" "${server_evts}" server_evts_pid
209	sleep 0.5
210
211	# Run the server
212	ip netns exec "$ns1" \
213	   ./mptcp_connect -s MPTCP -w 300 -p $app_port -l $listen_addr > /dev/null 2>&1 &
214	local server_pid=$!
215	sleep 0.5
216
217	# Run the client, transfer $file and stay connected to the server
218	# to conduct tests
219	ip netns exec "$ns2" \
220	   ./mptcp_connect -s MPTCP -w 300 -m sendfile -p $app_port $connect_addr\
221	   2>&1 > /dev/null < "$file" &
222	local client_pid=$!
223	sleep 1
224
225	# Capture client/server attributes from MPTCP connection netlink events
226
227	local client_token
228	local client_port
229	local client_serverside
230	local server_token
231	local server_serverside
232
233	client_token=$(mptcp_lib_evts_get_info token "$client_evts")
234	client_port=$(mptcp_lib_evts_get_info sport "$client_evts")
235	client_serverside=$(mptcp_lib_evts_get_info server_side "$client_evts")
236	server_token=$(mptcp_lib_evts_get_info token "$server_evts")
237	server_serverside=$(mptcp_lib_evts_get_info server_side "$server_evts")
238
239	print_test "Established IP${is_v6} MPTCP Connection ns2 => ns1"
240	if [ "$client_token" != "" ] && [ "$server_token" != "" ] && [ "$client_serverside" = 0 ] &&
241		   [ "$server_serverside" = 1 ]
242	then
243		test_pass
244		print_title "Connection info: ${client_addr}:${client_port} -> ${connect_addr}:${app_port}"
245	else
246		test_fail "Expected tokens (c:${client_token} - s:${server_token}) and server (c:${client_serverside} - s:${server_serverside})"
247		mptcp_lib_result_print_all_tap
248		exit 1
249	fi
250
251	if [ "$is_v6" = "v6" ]
252	then
253		client6_token=$client_token
254		server6_token=$server_token
255		client6_port=$client_port
256		client6_pid=$client_pid
257		server6_pid=$server_pid
258	else
259		client4_token=$client_token
260		server4_token=$server_token
261		client4_port=$client_port
262		client4_pid=$client_pid
263		server4_pid=$server_pid
264	fi
265}
266
267# $1: var name ; $2: prev ret
268check_expected_one()
269{
270	local var="${1}"
271	local exp="e_${var}"
272	local prev_ret="${2}"
273
274	if [ "${!var}" = "${!exp}" ]
275	then
276		return 0
277	fi
278
279	if [ "${prev_ret}" = "0" ]
280	then
281		test_fail
282	fi
283
284	_printf "\tExpected value for '%s': '%s', got '%s'.\n" \
285		"${var}" "${!exp}" "${!var}"
286	return 1
287}
288
289# $@: all var names to check
290check_expected()
291{
292	local rc=0
293	local var
294
295	for var in "${@}"
296	do
297		check_expected_one "${var}" "${rc}" || rc=1
298	done
299
300	if [ ${rc} -eq 0 ]
301	then
302		test_pass
303		return 0
304	fi
305
306	return 1
307}
308
309verify_announce_event()
310{
311	local evt=$1
312	local e_type=$2
313	local e_token=$3
314	local e_addr=$4
315	local e_id=$5
316	local e_dport=$6
317	local e_af=$7
318	local type
319	local token
320	local addr
321	local dport
322	local id
323
324	type=$(mptcp_lib_evts_get_info type "$evt" $e_type)
325	token=$(mptcp_lib_evts_get_info token "$evt" $e_type)
326	if [ "$e_af" = "v6" ]
327	then
328		addr=$(mptcp_lib_evts_get_info daddr6 "$evt" $e_type)
329	else
330		addr=$(mptcp_lib_evts_get_info daddr4 "$evt" $e_type)
331	fi
332	dport=$(mptcp_lib_evts_get_info dport "$evt" $e_type)
333	id=$(mptcp_lib_evts_get_info rem_id "$evt" $e_type)
334
335	check_expected "type" "token" "addr" "dport" "id"
336}
337
338test_announce()
339{
340	print_title "Announce tests"
341
342	# Capture events on the network namespace running the server
343	:>"$server_evts"
344
345	# ADD_ADDR using an invalid token should result in no action
346	local invalid_token=$(( client4_token - 1))
347	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token $invalid_token id\
348	   $client_addr_id dev ns2eth1 > /dev/null 2>&1
349
350	local type
351	type=$(mptcp_lib_evts_get_info type "$server_evts")
352	print_test "ADD_ADDR 10.0.2.2 (ns2) => ns1, invalid token"
353	if [ "$type" = "" ]
354	then
355		test_pass
356	else
357		test_fail "type defined: ${type}"
358	fi
359
360	# ADD_ADDR from the client to server machine reusing the subflow port
361	:>"$server_evts"
362	ip netns exec "$ns2"\
363	   ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id $client_addr_id dev\
364	   ns2eth1
365	print_test "ADD_ADDR id:client 10.0.2.2 (ns2) => ns1, reuse port"
366	sleep 0.5
367	verify_announce_event $server_evts $ANNOUNCED $server4_token "10.0.2.2" $client_addr_id \
368			      "$client4_port"
369
370	# ADD_ADDR6 from the client to server machine reusing the subflow port
371	:>"$server_evts"
372	ip netns exec "$ns2" ./pm_nl_ctl ann\
373	   dead:beef:2::2 token "$client6_token" id $client_addr_id dev ns2eth1
374	print_test "ADD_ADDR6 id:client dead:beef:2::2 (ns2) => ns1, reuse port"
375	sleep 0.5
376	verify_announce_event "$server_evts" "$ANNOUNCED" "$server6_token" "dead:beef:2::2"\
377			      "$client_addr_id" "$client6_port" "v6"
378
379	# ADD_ADDR from the client to server machine using a new port
380	:>"$server_evts"
381	client_addr_id=$((client_addr_id+1))
382	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
383	   $client_addr_id dev ns2eth1 port $new4_port
384	print_test "ADD_ADDR id:client+1 10.0.2.2 (ns2) => ns1, new port"
385	sleep 0.5
386	verify_announce_event "$server_evts" "$ANNOUNCED" "$server4_token" "10.0.2.2"\
387			      "$client_addr_id" "$new4_port"
388
389	# Capture events on the network namespace running the client
390	:>"$client_evts"
391
392	# ADD_ADDR from the server to client machine reusing the subflow port
393	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
394	   $server_addr_id dev ns1eth2
395	print_test "ADD_ADDR id:server 10.0.2.1 (ns1) => ns2, reuse port"
396	sleep 0.5
397	verify_announce_event "$client_evts" "$ANNOUNCED" "$client4_token" "10.0.2.1"\
398			      "$server_addr_id" "$app4_port"
399
400	# ADD_ADDR6 from the server to client machine reusing the subflow port
401	:>"$client_evts"
402	ip netns exec "$ns1" ./pm_nl_ctl ann dead:beef:2::1 token "$server6_token" id\
403	   $server_addr_id dev ns1eth2
404	print_test "ADD_ADDR6 id:server dead:beef:2::1 (ns1) => ns2, reuse port"
405	sleep 0.5
406	verify_announce_event "$client_evts" "$ANNOUNCED" "$client6_token" "dead:beef:2::1"\
407			      "$server_addr_id" "$app6_port" "v6"
408
409	# ADD_ADDR from the server to client machine using a new port
410	:>"$client_evts"
411	server_addr_id=$((server_addr_id+1))
412	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
413	   $server_addr_id dev ns1eth2 port $new4_port
414	print_test "ADD_ADDR id:server+1 10.0.2.1 (ns1) => ns2, new port"
415	sleep 0.5
416	verify_announce_event "$client_evts" "$ANNOUNCED" "$client4_token" "10.0.2.1"\
417			      "$server_addr_id" "$new4_port"
418}
419
420verify_remove_event()
421{
422	local evt=$1
423	local e_type=$2
424	local e_token=$3
425	local e_id=$4
426	local type
427	local token
428	local id
429
430	type=$(mptcp_lib_evts_get_info type "$evt" $e_type)
431	token=$(mptcp_lib_evts_get_info token "$evt" $e_type)
432	id=$(mptcp_lib_evts_get_info rem_id "$evt" $e_type)
433
434	check_expected "type" "token" "id"
435}
436
437test_remove()
438{
439	print_title "Remove tests"
440
441	# Capture events on the network namespace running the server
442	:>"$server_evts"
443
444	# RM_ADDR using an invalid token should result in no action
445	local invalid_token=$(( client4_token - 1 ))
446	ip netns exec "$ns2" ./pm_nl_ctl rem token $invalid_token id\
447	   $client_addr_id > /dev/null 2>&1
448	print_test "RM_ADDR id:client ns2 => ns1, invalid token"
449	local type
450	type=$(mptcp_lib_evts_get_info type "$server_evts")
451	if [ "$type" = "" ]
452	then
453		test_pass
454	else
455		test_fail
456	fi
457
458	# RM_ADDR using an invalid addr id should result in no action
459	local invalid_id=$(( client_addr_id + 1 ))
460	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
461	   $invalid_id > /dev/null 2>&1
462	print_test "RM_ADDR id:client+1 ns2 => ns1, invalid id"
463	type=$(mptcp_lib_evts_get_info type "$server_evts")
464	if [ "$type" = "" ]
465	then
466		test_pass
467	else
468		test_fail
469	fi
470
471	# RM_ADDR from the client to server machine
472	:>"$server_evts"
473	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
474	   $client_addr_id
475	print_test "RM_ADDR id:client ns2 => ns1"
476	sleep 0.5
477	verify_remove_event "$server_evts" "$REMOVED" "$server4_token" "$client_addr_id"
478
479	# RM_ADDR from the client to server machine
480	:>"$server_evts"
481	client_addr_id=$(( client_addr_id - 1 ))
482	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
483	   $client_addr_id
484	print_test "RM_ADDR id:client-1 ns2 => ns1"
485	sleep 0.5
486	verify_remove_event "$server_evts" "$REMOVED" "$server4_token" "$client_addr_id"
487
488	# RM_ADDR6 from the client to server machine
489	:>"$server_evts"
490	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client6_token" id\
491	   $client_addr_id
492	print_test "RM_ADDR6 id:client-1 ns2 => ns1"
493	sleep 0.5
494	verify_remove_event "$server_evts" "$REMOVED" "$server6_token" "$client_addr_id"
495
496	# Capture events on the network namespace running the client
497	:>"$client_evts"
498
499	# RM_ADDR from the server to client machine
500	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server4_token" id\
501	   $server_addr_id
502	print_test "RM_ADDR id:server ns1 => ns2"
503	sleep 0.5
504	verify_remove_event "$client_evts" "$REMOVED" "$client4_token" "$server_addr_id"
505
506	# RM_ADDR from the server to client machine
507	:>"$client_evts"
508	server_addr_id=$(( server_addr_id - 1 ))
509	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server4_token" id\
510	   $server_addr_id
511	print_test "RM_ADDR id:server-1 ns1 => ns2"
512	sleep 0.5
513	verify_remove_event "$client_evts" "$REMOVED" "$client4_token" "$server_addr_id"
514
515	# RM_ADDR6 from the server to client machine
516	:>"$client_evts"
517	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server6_token" id\
518	   $server_addr_id
519	print_test "RM_ADDR6 id:server-1 ns1 => ns2"
520	sleep 0.5
521	verify_remove_event "$client_evts" "$REMOVED" "$client6_token" "$server_addr_id"
522}
523
524verify_subflow_events()
525{
526	local evt=$1
527	local e_type=$2
528	local e_token=$3
529	local e_family=$4
530	local e_saddr=$5
531	local e_daddr=$6
532	local e_dport=$7
533	local e_locid=$8
534	local e_remid=$9
535	shift 2
536	local e_from=$8
537	local e_to=$9
538	local type
539	local token
540	local family
541	local saddr
542	local daddr
543	local dport
544	local locid
545	local remid
546	local info
547	local e_dport_txt
548
549	# only display the fixed ports
550	if [ "${e_dport}" -ge "${app4_port}" ] && [ "${e_dport}" -le "${app6_port}" ]; then
551		e_dport_txt=":${e_dport}"
552	fi
553
554	info="${e_saddr} (${e_from}) => ${e_daddr}${e_dport_txt} (${e_to})"
555
556	if [ "$e_type" = "$SUB_ESTABLISHED" ]
557	then
558		if [ "$e_family" = "$AF_INET6" ]
559		then
560			print_test "CREATE_SUBFLOW6 ${info}"
561		else
562			print_test "CREATE_SUBFLOW ${info}"
563		fi
564	else
565		if [ "$e_family" = "$AF_INET6" ]
566		then
567			print_test "DESTROY_SUBFLOW6 ${info}"
568		else
569			print_test "DESTROY_SUBFLOW ${info}"
570		fi
571	fi
572
573	type=$(mptcp_lib_evts_get_info type "$evt" $e_type)
574	token=$(mptcp_lib_evts_get_info token "$evt" $e_type)
575	family=$(mptcp_lib_evts_get_info family "$evt" $e_type)
576	dport=$(mptcp_lib_evts_get_info dport "$evt" $e_type)
577	locid=$(mptcp_lib_evts_get_info loc_id "$evt" $e_type)
578	remid=$(mptcp_lib_evts_get_info rem_id "$evt" $e_type)
579	if [ "$family" = "$AF_INET6" ]
580	then
581		saddr=$(mptcp_lib_evts_get_info saddr6 "$evt" $e_type)
582		daddr=$(mptcp_lib_evts_get_info daddr6 "$evt" $e_type)
583	else
584		saddr=$(mptcp_lib_evts_get_info saddr4 "$evt" $e_type)
585		daddr=$(mptcp_lib_evts_get_info daddr4 "$evt" $e_type)
586	fi
587
588	check_expected "type" "token" "daddr" "dport" "family" "saddr" "locid" "remid"
589}
590
591test_subflows()
592{
593	print_title "Subflows v4 or v6 only tests"
594
595	# Capture events on the network namespace running the server
596	:>"$server_evts"
597
598	# Attempt to add a listener at 10.0.2.2:<subflow-port>
599	ip netns exec "$ns2" ./pm_nl_ctl listen 10.0.2.2\
600	   "$client4_port" &
601	local listener_pid=$!
602
603	# ADD_ADDR from client to server machine reusing the subflow port
604	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
605	   $client_addr_id
606	sleep 0.5
607
608	# CREATE_SUBFLOW from server to client machine
609	:>"$server_evts"
610	ip netns exec "$ns1" ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2\
611	   rport "$client4_port" token "$server4_token"
612	sleep 0.5
613	verify_subflow_events $server_evts $SUB_ESTABLISHED $server4_token $AF_INET "10.0.2.1" \
614			      "10.0.2.2" "$client4_port" "23" "$client_addr_id" "ns1" "ns2"
615
616	# Delete the listener from the client ns, if one was created
617	mptcp_lib_kill_wait $listener_pid
618
619	local sport
620	sport=$(mptcp_lib_evts_get_info sport "$server_evts" $SUB_ESTABLISHED)
621
622	# DESTROY_SUBFLOW from server to client machine
623	:>"$server_evts"
624	ip netns exec "$ns1" ./pm_nl_ctl dsf lip 10.0.2.1 lport "$sport" rip 10.0.2.2 rport\
625	   "$client4_port" token "$server4_token"
626	sleep 0.5
627	verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server4_token" "$AF_INET" "10.0.2.1"\
628			      "10.0.2.2" "$client4_port" "23" "$client_addr_id" "ns1" "ns2"
629
630	# RM_ADDR from client to server machine
631	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
632	   "$client4_token"
633	sleep 0.5
634
635	# Attempt to add a listener at dead:beef:2::2:<subflow-port>
636	ip netns exec "$ns2" ./pm_nl_ctl listen dead:beef:2::2\
637	   "$client6_port" &
638	listener_pid=$!
639
640	# ADD_ADDR6 from client to server machine reusing the subflow port
641	:>"$server_evts"
642	ip netns exec "$ns2" ./pm_nl_ctl ann dead:beef:2::2 token "$client6_token" id\
643	   $client_addr_id
644	sleep 0.5
645
646	# CREATE_SUBFLOW6 from server to client machine
647	:>"$server_evts"
648	ip netns exec "$ns1" ./pm_nl_ctl csf lip dead:beef:2::1 lid 23 rip\
649	   dead:beef:2::2 rport "$client6_port" token "$server6_token"
650	sleep 0.5
651	verify_subflow_events "$server_evts" "$SUB_ESTABLISHED" "$server6_token" "$AF_INET6"\
652			      "dead:beef:2::1" "dead:beef:2::2" "$client6_port" "23"\
653			      "$client_addr_id" "ns1" "ns2"
654
655	# Delete the listener from the client ns, if one was created
656	mptcp_lib_kill_wait $listener_pid
657
658	sport=$(mptcp_lib_evts_get_info sport "$server_evts" $SUB_ESTABLISHED)
659
660	# DESTROY_SUBFLOW6 from server to client machine
661	:>"$server_evts"
662	ip netns exec "$ns1" ./pm_nl_ctl dsf lip dead:beef:2::1 lport "$sport" rip\
663	   dead:beef:2::2 rport "$client6_port" token "$server6_token"
664	sleep 0.5
665	verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server6_token" "$AF_INET6"\
666			      "dead:beef:2::1" "dead:beef:2::2" "$client6_port" "23"\
667			      "$client_addr_id" "ns1" "ns2"
668
669	# RM_ADDR from client to server machine
670	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
671	   "$client6_token"
672	sleep 0.5
673
674	# Attempt to add a listener at 10.0.2.2:<new-port>
675	ip netns exec "$ns2" ./pm_nl_ctl listen 10.0.2.2\
676	   $new4_port &
677	listener_pid=$!
678
679	# ADD_ADDR from client to server machine using a new port
680	:>"$server_evts"
681	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
682	   $client_addr_id port $new4_port
683	sleep 0.5
684
685	# CREATE_SUBFLOW from server to client machine
686	:>"$server_evts"
687	ip netns exec "$ns1" ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2 rport\
688	   $new4_port token "$server4_token"
689	sleep 0.5
690	verify_subflow_events "$server_evts" "$SUB_ESTABLISHED" "$server4_token" "$AF_INET"\
691			      "10.0.2.1" "10.0.2.2" "$new4_port" "23"\
692			      "$client_addr_id" "ns1" "ns2"
693
694	# Delete the listener from the client ns, if one was created
695	mptcp_lib_kill_wait $listener_pid
696
697	sport=$(mptcp_lib_evts_get_info sport "$server_evts" $SUB_ESTABLISHED)
698
699	# DESTROY_SUBFLOW from server to client machine
700	:>"$server_evts"
701	ip netns exec "$ns1" ./pm_nl_ctl dsf lip 10.0.2.1 lport "$sport" rip 10.0.2.2 rport\
702	   $new4_port token "$server4_token"
703	sleep 0.5
704	verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server4_token" "$AF_INET" "10.0.2.1"\
705			      "10.0.2.2" "$new4_port" "23" "$client_addr_id" "ns1" "ns2"
706
707	# RM_ADDR from client to server machine
708	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
709	   "$client4_token"
710
711	# Capture events on the network namespace running the client
712	:>"$client_evts"
713
714	# Attempt to add a listener at 10.0.2.1:<subflow-port>
715	ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
716	   $app4_port &
717	listener_pid=$!
718
719	# ADD_ADDR from server to client machine reusing the subflow port
720	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
721	   $server_addr_id
722	sleep 0.5
723
724	# CREATE_SUBFLOW from client to server machine
725	:>"$client_evts"
726	ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
727	   $app4_port token "$client4_token"
728	sleep 0.5
729	verify_subflow_events $client_evts $SUB_ESTABLISHED $client4_token $AF_INET "10.0.2.2"\
730			      "10.0.2.1" "$app4_port" "23" "$server_addr_id" "ns2" "ns1"
731
732	# Delete the listener from the server ns, if one was created
733	mptcp_lib_kill_wait $listener_pid
734
735	sport=$(mptcp_lib_evts_get_info sport "$client_evts" $SUB_ESTABLISHED)
736
737	# DESTROY_SUBFLOW from client to server machine
738	:>"$client_evts"
739	ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
740	   $app4_port token "$client4_token"
741	sleep 0.5
742	verify_subflow_events "$client_evts" "$SUB_CLOSED" "$client4_token" "$AF_INET" "10.0.2.2"\
743			      "10.0.2.1" "$app4_port" "23" "$server_addr_id" "ns2" "ns1"
744
745	# RM_ADDR from server to client machine
746	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
747	   "$server4_token"
748	sleep 0.5
749
750	# Attempt to add a listener at dead:beef:2::1:<subflow-port>
751	ip netns exec "$ns1" ./pm_nl_ctl listen dead:beef:2::1\
752	   $app6_port &
753	listener_pid=$!
754
755	# ADD_ADDR6 from server to client machine reusing the subflow port
756	:>"$client_evts"
757	ip netns exec "$ns1" ./pm_nl_ctl ann dead:beef:2::1 token "$server6_token" id\
758	   $server_addr_id
759	sleep 0.5
760
761	# CREATE_SUBFLOW6 from client to server machine
762	:>"$client_evts"
763	ip netns exec "$ns2" ./pm_nl_ctl csf lip dead:beef:2::2 lid 23 rip\
764	   dead:beef:2::1 rport $app6_port token "$client6_token"
765	sleep 0.5
766	verify_subflow_events "$client_evts" "$SUB_ESTABLISHED" "$client6_token"\
767			      "$AF_INET6" "dead:beef:2::2"\
768			      "dead:beef:2::1" "$app6_port" "23"\
769			      "$server_addr_id" "ns2" "ns1"
770
771	# Delete the listener from the server ns, if one was created
772	mptcp_lib_kill_wait $listener_pid
773
774	sport=$(mptcp_lib_evts_get_info sport "$client_evts" $SUB_ESTABLISHED)
775
776	# DESTROY_SUBFLOW6 from client to server machine
777	:>"$client_evts"
778	ip netns exec "$ns2" ./pm_nl_ctl dsf lip dead:beef:2::2 lport "$sport" rip\
779	   dead:beef:2::1 rport $app6_port token "$client6_token"
780	sleep 0.5
781	verify_subflow_events $client_evts $SUB_CLOSED $client6_token $AF_INET6 "dead:beef:2::2"\
782			      "dead:beef:2::1" "$app6_port" "23" "$server_addr_id" "ns2" "ns1"
783
784	# RM_ADDR6 from server to client machine
785	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
786	   "$server6_token"
787	sleep 0.5
788
789	# Attempt to add a listener at 10.0.2.1:<new-port>
790	ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
791	   $new4_port &
792	listener_pid=$!
793
794	# ADD_ADDR from server to client machine using a new port
795	:>"$client_evts"
796	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
797	   $server_addr_id port $new4_port
798	sleep 0.5
799
800	# CREATE_SUBFLOW from client to server machine
801	:>"$client_evts"
802	ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
803	   $new4_port token "$client4_token"
804	sleep 0.5
805	verify_subflow_events "$client_evts" "$SUB_ESTABLISHED" "$client4_token" "$AF_INET"\
806			      "10.0.2.2" "10.0.2.1" "$new4_port" "23" "$server_addr_id" "ns2" "ns1"
807
808	# Delete the listener from the server ns, if one was created
809	mptcp_lib_kill_wait $listener_pid
810
811	sport=$(mptcp_lib_evts_get_info sport "$client_evts" $SUB_ESTABLISHED)
812
813	# DESTROY_SUBFLOW from client to server machine
814	:>"$client_evts"
815	ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
816	   $new4_port token "$client4_token"
817	sleep 0.5
818	verify_subflow_events "$client_evts" "$SUB_CLOSED" "$client4_token" "$AF_INET" "10.0.2.2"\
819			      "10.0.2.1" "$new4_port" "23" "$server_addr_id" "ns2" "ns1"
820
821	# RM_ADDR from server to client machine
822	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
823	   "$server4_token"
824}
825
826test_subflows_v4_v6_mix()
827{
828	print_title "Subflows v4 and v6 mix tests"
829
830	# Attempt to add a listener at 10.0.2.1:<subflow-port>
831	ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
832	   $app6_port &
833	local listener_pid=$!
834
835	# ADD_ADDR4 from server to client machine reusing the subflow port on
836	# the established v6 connection
837	:>"$client_evts"
838	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server6_token" id\
839	   $server_addr_id dev ns1eth2
840	print_test "ADD_ADDR4 id:server 10.0.2.1 (ns1) => ns2, reuse port"
841	sleep 0.5
842	verify_announce_event "$client_evts" "$ANNOUNCED" "$client6_token" "10.0.2.1"\
843			      "$server_addr_id" "$app6_port"
844
845	# CREATE_SUBFLOW from client to server machine
846	:>"$client_evts"
847	ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
848	   $app6_port token "$client6_token"
849	sleep 0.5
850	verify_subflow_events "$client_evts" "$SUB_ESTABLISHED" "$client6_token"\
851			      "$AF_INET" "10.0.2.2" "10.0.2.1" "$app6_port" "23"\
852			      "$server_addr_id" "ns2" "ns1"
853
854	# Delete the listener from the server ns, if one was created
855	mptcp_lib_kill_wait $listener_pid
856
857	sport=$(mptcp_lib_evts_get_info sport "$client_evts" $SUB_ESTABLISHED)
858
859	# DESTROY_SUBFLOW from client to server machine
860	:>"$client_evts"
861	ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
862	   $app6_port token "$client6_token"
863	sleep 0.5
864	verify_subflow_events "$client_evts" "$SUB_CLOSED" "$client6_token" \
865			      "$AF_INET" "10.0.2.2" "10.0.2.1" "$app6_port" "23"\
866			      "$server_addr_id" "ns2" "ns1"
867
868	# RM_ADDR from server to client machine
869	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
870	   "$server6_token"
871	sleep 0.5
872}
873
874test_prio()
875{
876	print_title "Prio tests"
877
878	local count
879
880	# Send MP_PRIO signal from client to server machine
881	ip netns exec "$ns2" ./pm_nl_ctl set 10.0.1.2 port "$client4_port" flags backup token "$client4_token" rip 10.0.1.1 rport "$app4_port"
882	sleep 0.5
883
884	# Check TX
885	print_test "MP_PRIO TX"
886	count=$(mptcp_lib_get_counter "$ns2" "MPTcpExtMPPrioTx")
887	if [ -z "$count" ]; then
888		test_skip
889	elif [ $count != 1 ]; then
890		test_fail "Count != 1: ${count}"
891	else
892		test_pass
893	fi
894
895	# Check RX
896	print_test "MP_PRIO RX"
897	count=$(mptcp_lib_get_counter "$ns1" "MPTcpExtMPPrioRx")
898	if [ -z "$count" ]; then
899		test_skip
900	elif [ $count != 1 ]; then
901		test_fail "Count != 1: ${count}"
902	else
903		test_pass
904	fi
905}
906
907verify_listener_events()
908{
909	local evt=$1
910	local e_type=$2
911	local e_family=$3
912	local e_saddr=$4
913	local e_sport=$5
914	local type
915	local family
916	local saddr
917	local sport
918
919	type=$(mptcp_lib_evts_get_info type $evt $e_type)
920	family=$(mptcp_lib_evts_get_info family $evt $e_type)
921	sport=$(mptcp_lib_evts_get_info sport $evt $e_type)
922	if [ $family ] && [ $family = $AF_INET6 ]; then
923		saddr=$(mptcp_lib_evts_get_info saddr6 $evt $e_type)
924	else
925		saddr=$(mptcp_lib_evts_get_info saddr4 $evt $e_type)
926	fi
927
928	check_expected "type" "family" "saddr" "sport"
929}
930
931test_listener()
932{
933	print_title "Listener tests"
934
935	if ! mptcp_lib_kallsyms_has "mptcp_event_pm_listener$"; then
936		print_test "LISTENER events"
937		test_skip
938		return
939	fi
940
941	# Capture events on the network namespace running the client
942	:>$client_evts
943
944	# Attempt to add a listener at 10.0.2.2:<subflow-port>
945	ip netns exec $ns2 ./pm_nl_ctl listen 10.0.2.2\
946		$client4_port &
947	local listener_pid=$!
948
949	sleep 0.5
950	print_test "CREATE_LISTENER 10.0.2.2 (client port)"
951	verify_listener_events $client_evts $LISTENER_CREATED $AF_INET 10.0.2.2 $client4_port
952
953	# ADD_ADDR from client to server machine reusing the subflow port
954	ip netns exec $ns2 ./pm_nl_ctl ann 10.0.2.2 token $client4_token id\
955		$client_addr_id
956	sleep 0.5
957
958	# CREATE_SUBFLOW from server to client machine
959	ip netns exec $ns1 ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2\
960		rport $client4_port token $server4_token
961	sleep 0.5
962
963	# Delete the listener from the client ns, if one was created
964	mptcp_lib_kill_wait $listener_pid
965
966	sleep 0.5
967	print_test "CLOSE_LISTENER 10.0.2.2 (client port)"
968	verify_listener_events $client_evts $LISTENER_CLOSED $AF_INET 10.0.2.2 $client4_port
969}
970
971print_title "Make connections"
972make_connection
973make_connection "v6"
974print_title "Will be using address IDs ${client_addr_id} (client) and ${server_addr_id} (server)"
975
976test_announce
977test_remove
978test_subflows
979test_subflows_v4_v6_mix
980test_prio
981test_listener
982
983mptcp_lib_result_print_all_tap
984exit ${ret}
985