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=6        # MPTCP_EVENT_ANNOUNCED
27REMOVED=7          # MPTCP_EVENT_REMOVED
28SUB_ESTABLISHED=10 # MPTCP_EVENT_SUB_ESTABLISHED
29SUB_CLOSED=11      # MPTCP_EVENT_SUB_CLOSED
30LISTENER_CREATED=15 #MPTCP_EVENT_LISTENER_CREATED
31LISTENER_CLOSED=16  #MPTCP_EVENT_LISTENER_CLOSED
32
33AF_INET=2
34AF_INET6=10
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	:>"$client_evts"
205	if [ $client_evts_pid -ne 0 ]; then
206		mptcp_lib_kill_wait $client_evts_pid
207	fi
208	ip netns exec "$ns2" ./pm_nl_ctl events >> "$client_evts" 2>&1 &
209	client_evts_pid=$!
210	if [ -z "$server_evts" ]; then
211		server_evts=$(mktemp)
212	fi
213	:>"$server_evts"
214	if [ $server_evts_pid -ne 0 ]; then
215		mptcp_lib_kill_wait $server_evts_pid
216	fi
217	ip netns exec "$ns1" ./pm_nl_ctl events >> "$server_evts" 2>&1 &
218	server_evts_pid=$!
219	sleep 0.5
220
221	# Run the server
222	ip netns exec "$ns1" \
223	   ./mptcp_connect -s MPTCP -w 300 -p $app_port -l $listen_addr > /dev/null 2>&1 &
224	local server_pid=$!
225	sleep 0.5
226
227	# Run the client, transfer $file and stay connected to the server
228	# to conduct tests
229	ip netns exec "$ns2" \
230	   ./mptcp_connect -s MPTCP -w 300 -m sendfile -p $app_port $connect_addr\
231	   2>&1 > /dev/null < "$file" &
232	local client_pid=$!
233	sleep 1
234
235	# Capture client/server attributes from MPTCP connection netlink events
236
237	local client_token
238	local client_port
239	local client_serverside
240	local server_token
241	local server_serverside
242
243	client_token=$(mptcp_lib_evts_get_info token "$client_evts")
244	client_port=$(mptcp_lib_evts_get_info sport "$client_evts")
245	client_serverside=$(mptcp_lib_evts_get_info server_side "$client_evts")
246	server_token=$(mptcp_lib_evts_get_info token "$server_evts")
247	server_serverside=$(mptcp_lib_evts_get_info server_side "$server_evts")
248
249	print_test "Established IP${is_v6} MPTCP Connection ns2 => ns1"
250	if [ "$client_token" != "" ] && [ "$server_token" != "" ] && [ "$client_serverside" = 0 ] &&
251		   [ "$server_serverside" = 1 ]
252	then
253		test_pass
254		print_title "Connection info: ${client_addr}:${client_port} -> ${connect_addr}:${app_port}"
255	else
256		test_fail "Expected tokens (c:${client_token} - s:${server_token}) and server (c:${client_serverside} - s:${server_serverside})"
257		mptcp_lib_result_print_all_tap
258		exit 1
259	fi
260
261	if [ "$is_v6" = "v6" ]
262	then
263		client6_token=$client_token
264		server6_token=$server_token
265		client6_port=$client_port
266		client6_pid=$client_pid
267		server6_pid=$server_pid
268	else
269		client4_token=$client_token
270		server4_token=$server_token
271		client4_port=$client_port
272		client4_pid=$client_pid
273		server4_pid=$server_pid
274	fi
275}
276
277# $1: var name ; $2: prev ret
278check_expected_one()
279{
280	local var="${1}"
281	local exp="e_${var}"
282	local prev_ret="${2}"
283
284	if [ "${!var}" = "${!exp}" ]
285	then
286		return 0
287	fi
288
289	if [ "${prev_ret}" = "0" ]
290	then
291		test_fail
292	fi
293
294	_printf "\tExpected value for '%s': '%s', got '%s'.\n" \
295		"${var}" "${!exp}" "${!var}"
296	return 1
297}
298
299# $@: all var names to check
300check_expected()
301{
302	local rc=0
303	local var
304
305	for var in "${@}"
306	do
307		check_expected_one "${var}" "${rc}" || rc=1
308	done
309
310	if [ ${rc} -eq 0 ]
311	then
312		test_pass
313		return 0
314	fi
315
316	return 1
317}
318
319verify_announce_event()
320{
321	local evt=$1
322	local e_type=$2
323	local e_token=$3
324	local e_addr=$4
325	local e_id=$5
326	local e_dport=$6
327	local e_af=$7
328	local type
329	local token
330	local addr
331	local dport
332	local id
333
334	type=$(mptcp_lib_evts_get_info type "$evt" $e_type)
335	token=$(mptcp_lib_evts_get_info token "$evt" $e_type)
336	if [ "$e_af" = "v6" ]
337	then
338		addr=$(mptcp_lib_evts_get_info daddr6 "$evt" $e_type)
339	else
340		addr=$(mptcp_lib_evts_get_info daddr4 "$evt" $e_type)
341	fi
342	dport=$(mptcp_lib_evts_get_info dport "$evt" $e_type)
343	id=$(mptcp_lib_evts_get_info rem_id "$evt" $e_type)
344
345	check_expected "type" "token" "addr" "dport" "id"
346}
347
348test_announce()
349{
350	print_title "Announce tests"
351
352	# Capture events on the network namespace running the server
353	:>"$server_evts"
354
355	# ADD_ADDR using an invalid token should result in no action
356	local invalid_token=$(( client4_token - 1))
357	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token $invalid_token id\
358	   $client_addr_id dev ns2eth1 > /dev/null 2>&1
359
360	local type
361	type=$(mptcp_lib_evts_get_info type "$server_evts")
362	print_test "ADD_ADDR 10.0.2.2 (ns2) => ns1, invalid token"
363	if [ "$type" = "" ]
364	then
365		test_pass
366	else
367		test_fail "type defined: ${type}"
368	fi
369
370	# ADD_ADDR from the client to server machine reusing the subflow port
371	:>"$server_evts"
372	ip netns exec "$ns2"\
373	   ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id $client_addr_id dev\
374	   ns2eth1
375	print_test "ADD_ADDR id:client 10.0.2.2 (ns2) => ns1, reuse port"
376	sleep 0.5
377	verify_announce_event $server_evts $ANNOUNCED $server4_token "10.0.2.2" $client_addr_id \
378			      "$client4_port"
379
380	# ADD_ADDR6 from the client to server machine reusing the subflow port
381	:>"$server_evts"
382	ip netns exec "$ns2" ./pm_nl_ctl ann\
383	   dead:beef:2::2 token "$client6_token" id $client_addr_id dev ns2eth1
384	print_test "ADD_ADDR6 id:client dead:beef:2::2 (ns2) => ns1, reuse port"
385	sleep 0.5
386	verify_announce_event "$server_evts" "$ANNOUNCED" "$server6_token" "dead:beef:2::2"\
387			      "$client_addr_id" "$client6_port" "v6"
388
389	# ADD_ADDR from the client to server machine using a new port
390	:>"$server_evts"
391	client_addr_id=$((client_addr_id+1))
392	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
393	   $client_addr_id dev ns2eth1 port $new4_port
394	print_test "ADD_ADDR id:client+1 10.0.2.2 (ns2) => ns1, new port"
395	sleep 0.5
396	verify_announce_event "$server_evts" "$ANNOUNCED" "$server4_token" "10.0.2.2"\
397			      "$client_addr_id" "$new4_port"
398
399	# Capture events on the network namespace running the client
400	:>"$client_evts"
401
402	# ADD_ADDR from the server to client machine reusing the subflow port
403	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
404	   $server_addr_id dev ns1eth2
405	print_test "ADD_ADDR id:server 10.0.2.1 (ns1) => ns2, reuse port"
406	sleep 0.5
407	verify_announce_event "$client_evts" "$ANNOUNCED" "$client4_token" "10.0.2.1"\
408			      "$server_addr_id" "$app4_port"
409
410	# ADD_ADDR6 from the server to client machine reusing the subflow port
411	:>"$client_evts"
412	ip netns exec "$ns1" ./pm_nl_ctl ann dead:beef:2::1 token "$server6_token" id\
413	   $server_addr_id dev ns1eth2
414	print_test "ADD_ADDR6 id:server dead:beef:2::1 (ns1) => ns2, reuse port"
415	sleep 0.5
416	verify_announce_event "$client_evts" "$ANNOUNCED" "$client6_token" "dead:beef:2::1"\
417			      "$server_addr_id" "$app6_port" "v6"
418
419	# ADD_ADDR from the server to client machine using a new port
420	:>"$client_evts"
421	server_addr_id=$((server_addr_id+1))
422	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
423	   $server_addr_id dev ns1eth2 port $new4_port
424	print_test "ADD_ADDR id:server+1 10.0.2.1 (ns1) => ns2, new port"
425	sleep 0.5
426	verify_announce_event "$client_evts" "$ANNOUNCED" "$client4_token" "10.0.2.1"\
427			      "$server_addr_id" "$new4_port"
428}
429
430verify_remove_event()
431{
432	local evt=$1
433	local e_type=$2
434	local e_token=$3
435	local e_id=$4
436	local type
437	local token
438	local id
439
440	type=$(mptcp_lib_evts_get_info type "$evt" $e_type)
441	token=$(mptcp_lib_evts_get_info token "$evt" $e_type)
442	id=$(mptcp_lib_evts_get_info rem_id "$evt" $e_type)
443
444	check_expected "type" "token" "id"
445}
446
447test_remove()
448{
449	print_title "Remove tests"
450
451	# Capture events on the network namespace running the server
452	:>"$server_evts"
453
454	# RM_ADDR using an invalid token should result in no action
455	local invalid_token=$(( client4_token - 1 ))
456	ip netns exec "$ns2" ./pm_nl_ctl rem token $invalid_token id\
457	   $client_addr_id > /dev/null 2>&1
458	print_test "RM_ADDR id:client ns2 => ns1, invalid token"
459	local type
460	type=$(mptcp_lib_evts_get_info type "$server_evts")
461	if [ "$type" = "" ]
462	then
463		test_pass
464	else
465		test_fail
466	fi
467
468	# RM_ADDR using an invalid addr id should result in no action
469	local invalid_id=$(( client_addr_id + 1 ))
470	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
471	   $invalid_id > /dev/null 2>&1
472	print_test "RM_ADDR id:client+1 ns2 => ns1, invalid id"
473	type=$(mptcp_lib_evts_get_info type "$server_evts")
474	if [ "$type" = "" ]
475	then
476		test_pass
477	else
478		test_fail
479	fi
480
481	# RM_ADDR from the client to server machine
482	:>"$server_evts"
483	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
484	   $client_addr_id
485	print_test "RM_ADDR id:client ns2 => ns1"
486	sleep 0.5
487	verify_remove_event "$server_evts" "$REMOVED" "$server4_token" "$client_addr_id"
488
489	# RM_ADDR from the client to server machine
490	:>"$server_evts"
491	client_addr_id=$(( client_addr_id - 1 ))
492	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
493	   $client_addr_id
494	print_test "RM_ADDR id:client-1 ns2 => ns1"
495	sleep 0.5
496	verify_remove_event "$server_evts" "$REMOVED" "$server4_token" "$client_addr_id"
497
498	# RM_ADDR6 from the client to server machine
499	:>"$server_evts"
500	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client6_token" id\
501	   $client_addr_id
502	print_test "RM_ADDR6 id:client-1 ns2 => ns1"
503	sleep 0.5
504	verify_remove_event "$server_evts" "$REMOVED" "$server6_token" "$client_addr_id"
505
506	# Capture events on the network namespace running the client
507	:>"$client_evts"
508
509	# RM_ADDR from the server to client machine
510	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server4_token" id\
511	   $server_addr_id
512	print_test "RM_ADDR id:server ns1 => ns2"
513	sleep 0.5
514	verify_remove_event "$client_evts" "$REMOVED" "$client4_token" "$server_addr_id"
515
516	# RM_ADDR from the server to client machine
517	:>"$client_evts"
518	server_addr_id=$(( server_addr_id - 1 ))
519	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server4_token" id\
520	   $server_addr_id
521	print_test "RM_ADDR id:server-1 ns1 => ns2"
522	sleep 0.5
523	verify_remove_event "$client_evts" "$REMOVED" "$client4_token" "$server_addr_id"
524
525	# RM_ADDR6 from the server to client machine
526	:>"$client_evts"
527	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server6_token" id\
528	   $server_addr_id
529	print_test "RM_ADDR6 id:server-1 ns1 => ns2"
530	sleep 0.5
531	verify_remove_event "$client_evts" "$REMOVED" "$client6_token" "$server_addr_id"
532}
533
534verify_subflow_events()
535{
536	local evt=$1
537	local e_type=$2
538	local e_token=$3
539	local e_family=$4
540	local e_saddr=$5
541	local e_daddr=$6
542	local e_dport=$7
543	local e_locid=$8
544	local e_remid=$9
545	shift 2
546	local e_from=$8
547	local e_to=$9
548	local type
549	local token
550	local family
551	local saddr
552	local daddr
553	local dport
554	local locid
555	local remid
556	local info
557	local e_dport_txt
558
559	# only display the fixed ports
560	if [ "${e_dport}" -ge "${app4_port}" ] && [ "${e_dport}" -le "${app6_port}" ]; then
561		e_dport_txt=":${e_dport}"
562	fi
563
564	info="${e_saddr} (${e_from}) => ${e_daddr}${e_dport_txt} (${e_to})"
565
566	if [ "$e_type" = "$SUB_ESTABLISHED" ]
567	then
568		if [ "$e_family" = "$AF_INET6" ]
569		then
570			print_test "CREATE_SUBFLOW6 ${info}"
571		else
572			print_test "CREATE_SUBFLOW ${info}"
573		fi
574	else
575		if [ "$e_family" = "$AF_INET6" ]
576		then
577			print_test "DESTROY_SUBFLOW6 ${info}"
578		else
579			print_test "DESTROY_SUBFLOW ${info}"
580		fi
581	fi
582
583	type=$(mptcp_lib_evts_get_info type "$evt" $e_type)
584	token=$(mptcp_lib_evts_get_info token "$evt" $e_type)
585	family=$(mptcp_lib_evts_get_info family "$evt" $e_type)
586	dport=$(mptcp_lib_evts_get_info dport "$evt" $e_type)
587	locid=$(mptcp_lib_evts_get_info loc_id "$evt" $e_type)
588	remid=$(mptcp_lib_evts_get_info rem_id "$evt" $e_type)
589	if [ "$family" = "$AF_INET6" ]
590	then
591		saddr=$(mptcp_lib_evts_get_info saddr6 "$evt" $e_type)
592		daddr=$(mptcp_lib_evts_get_info daddr6 "$evt" $e_type)
593	else
594		saddr=$(mptcp_lib_evts_get_info saddr4 "$evt" $e_type)
595		daddr=$(mptcp_lib_evts_get_info daddr4 "$evt" $e_type)
596	fi
597
598	check_expected "type" "token" "daddr" "dport" "family" "saddr" "locid" "remid"
599}
600
601test_subflows()
602{
603	print_title "Subflows v4 or v6 only tests"
604
605	# Capture events on the network namespace running the server
606	:>"$server_evts"
607
608	# Attempt to add a listener at 10.0.2.2:<subflow-port>
609	ip netns exec "$ns2" ./pm_nl_ctl listen 10.0.2.2\
610	   "$client4_port" &
611	local listener_pid=$!
612
613	# ADD_ADDR from client to server machine reusing the subflow port
614	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
615	   $client_addr_id
616	sleep 0.5
617
618	# CREATE_SUBFLOW from server to client machine
619	:>"$server_evts"
620	ip netns exec "$ns1" ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2\
621	   rport "$client4_port" token "$server4_token"
622	sleep 0.5
623	verify_subflow_events $server_evts $SUB_ESTABLISHED $server4_token $AF_INET "10.0.2.1" \
624			      "10.0.2.2" "$client4_port" "23" "$client_addr_id" "ns1" "ns2"
625
626	# Delete the listener from the client ns, if one was created
627	mptcp_lib_kill_wait $listener_pid
628
629	local sport
630	sport=$(mptcp_lib_evts_get_info sport "$server_evts" $SUB_ESTABLISHED)
631
632	# DESTROY_SUBFLOW from server to client machine
633	:>"$server_evts"
634	ip netns exec "$ns1" ./pm_nl_ctl dsf lip 10.0.2.1 lport "$sport" rip 10.0.2.2 rport\
635	   "$client4_port" token "$server4_token"
636	sleep 0.5
637	verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server4_token" "$AF_INET" "10.0.2.1"\
638			      "10.0.2.2" "$client4_port" "23" "$client_addr_id" "ns1" "ns2"
639
640	# RM_ADDR from client to server machine
641	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
642	   "$client4_token"
643	sleep 0.5
644
645	# Attempt to add a listener at dead:beef:2::2:<subflow-port>
646	ip netns exec "$ns2" ./pm_nl_ctl listen dead:beef:2::2\
647	   "$client6_port" &
648	listener_pid=$!
649
650	# ADD_ADDR6 from client to server machine reusing the subflow port
651	:>"$server_evts"
652	ip netns exec "$ns2" ./pm_nl_ctl ann dead:beef:2::2 token "$client6_token" id\
653	   $client_addr_id
654	sleep 0.5
655
656	# CREATE_SUBFLOW6 from server to client machine
657	:>"$server_evts"
658	ip netns exec "$ns1" ./pm_nl_ctl csf lip dead:beef:2::1 lid 23 rip\
659	   dead:beef:2::2 rport "$client6_port" token "$server6_token"
660	sleep 0.5
661	verify_subflow_events "$server_evts" "$SUB_ESTABLISHED" "$server6_token" "$AF_INET6"\
662			      "dead:beef:2::1" "dead:beef:2::2" "$client6_port" "23"\
663			      "$client_addr_id" "ns1" "ns2"
664
665	# Delete the listener from the client ns, if one was created
666	mptcp_lib_kill_wait $listener_pid
667
668	sport=$(mptcp_lib_evts_get_info sport "$server_evts" $SUB_ESTABLISHED)
669
670	# DESTROY_SUBFLOW6 from server to client machine
671	:>"$server_evts"
672	ip netns exec "$ns1" ./pm_nl_ctl dsf lip dead:beef:2::1 lport "$sport" rip\
673	   dead:beef:2::2 rport "$client6_port" token "$server6_token"
674	sleep 0.5
675	verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server6_token" "$AF_INET6"\
676			      "dead:beef:2::1" "dead:beef:2::2" "$client6_port" "23"\
677			      "$client_addr_id" "ns1" "ns2"
678
679	# RM_ADDR from client to server machine
680	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
681	   "$client6_token"
682	sleep 0.5
683
684	# Attempt to add a listener at 10.0.2.2:<new-port>
685	ip netns exec "$ns2" ./pm_nl_ctl listen 10.0.2.2\
686	   $new4_port &
687	listener_pid=$!
688
689	# ADD_ADDR from client to server machine using a new port
690	:>"$server_evts"
691	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
692	   $client_addr_id port $new4_port
693	sleep 0.5
694
695	# CREATE_SUBFLOW from server to client machine
696	:>"$server_evts"
697	ip netns exec "$ns1" ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2 rport\
698	   $new4_port token "$server4_token"
699	sleep 0.5
700	verify_subflow_events "$server_evts" "$SUB_ESTABLISHED" "$server4_token" "$AF_INET"\
701			      "10.0.2.1" "10.0.2.2" "$new4_port" "23"\
702			      "$client_addr_id" "ns1" "ns2"
703
704	# Delete the listener from the client ns, if one was created
705	mptcp_lib_kill_wait $listener_pid
706
707	sport=$(mptcp_lib_evts_get_info sport "$server_evts" $SUB_ESTABLISHED)
708
709	# DESTROY_SUBFLOW from server to client machine
710	:>"$server_evts"
711	ip netns exec "$ns1" ./pm_nl_ctl dsf lip 10.0.2.1 lport "$sport" rip 10.0.2.2 rport\
712	   $new4_port token "$server4_token"
713	sleep 0.5
714	verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server4_token" "$AF_INET" "10.0.2.1"\
715			      "10.0.2.2" "$new4_port" "23" "$client_addr_id" "ns1" "ns2"
716
717	# RM_ADDR from client to server machine
718	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
719	   "$client4_token"
720
721	# Capture events on the network namespace running the client
722	:>"$client_evts"
723
724	# Attempt to add a listener at 10.0.2.1:<subflow-port>
725	ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
726	   $app4_port &
727	listener_pid=$!
728
729	# ADD_ADDR from server to client machine reusing the subflow port
730	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
731	   $server_addr_id
732	sleep 0.5
733
734	# CREATE_SUBFLOW from client to server machine
735	:>"$client_evts"
736	ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
737	   $app4_port token "$client4_token"
738	sleep 0.5
739	verify_subflow_events $client_evts $SUB_ESTABLISHED $client4_token $AF_INET "10.0.2.2"\
740			      "10.0.2.1" "$app4_port" "23" "$server_addr_id" "ns2" "ns1"
741
742	# Delete the listener from the server ns, if one was created
743	mptcp_lib_kill_wait $listener_pid
744
745	sport=$(mptcp_lib_evts_get_info sport "$client_evts" $SUB_ESTABLISHED)
746
747	# DESTROY_SUBFLOW from client to server machine
748	:>"$client_evts"
749	ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
750	   $app4_port token "$client4_token"
751	sleep 0.5
752	verify_subflow_events "$client_evts" "$SUB_CLOSED" "$client4_token" "$AF_INET" "10.0.2.2"\
753			      "10.0.2.1" "$app4_port" "23" "$server_addr_id" "ns2" "ns1"
754
755	# RM_ADDR from server to client machine
756	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
757	   "$server4_token"
758	sleep 0.5
759
760	# Attempt to add a listener at dead:beef:2::1:<subflow-port>
761	ip netns exec "$ns1" ./pm_nl_ctl listen dead:beef:2::1\
762	   $app6_port &
763	listener_pid=$!
764
765	# ADD_ADDR6 from server to client machine reusing the subflow port
766	:>"$client_evts"
767	ip netns exec "$ns1" ./pm_nl_ctl ann dead:beef:2::1 token "$server6_token" id\
768	   $server_addr_id
769	sleep 0.5
770
771	# CREATE_SUBFLOW6 from client to server machine
772	:>"$client_evts"
773	ip netns exec "$ns2" ./pm_nl_ctl csf lip dead:beef:2::2 lid 23 rip\
774	   dead:beef:2::1 rport $app6_port token "$client6_token"
775	sleep 0.5
776	verify_subflow_events "$client_evts" "$SUB_ESTABLISHED" "$client6_token"\
777			      "$AF_INET6" "dead:beef:2::2"\
778			      "dead:beef:2::1" "$app6_port" "23"\
779			      "$server_addr_id" "ns2" "ns1"
780
781	# Delete the listener from the server ns, if one was created
782	mptcp_lib_kill_wait $listener_pid
783
784	sport=$(mptcp_lib_evts_get_info sport "$client_evts" $SUB_ESTABLISHED)
785
786	# DESTROY_SUBFLOW6 from client to server machine
787	:>"$client_evts"
788	ip netns exec "$ns2" ./pm_nl_ctl dsf lip dead:beef:2::2 lport "$sport" rip\
789	   dead:beef:2::1 rport $app6_port token "$client6_token"
790	sleep 0.5
791	verify_subflow_events $client_evts $SUB_CLOSED $client6_token $AF_INET6 "dead:beef:2::2"\
792			      "dead:beef:2::1" "$app6_port" "23" "$server_addr_id" "ns2" "ns1"
793
794	# RM_ADDR6 from server to client machine
795	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
796	   "$server6_token"
797	sleep 0.5
798
799	# Attempt to add a listener at 10.0.2.1:<new-port>
800	ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
801	   $new4_port &
802	listener_pid=$!
803
804	# ADD_ADDR from server to client machine using a new port
805	:>"$client_evts"
806	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
807	   $server_addr_id port $new4_port
808	sleep 0.5
809
810	# CREATE_SUBFLOW from client to server machine
811	:>"$client_evts"
812	ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
813	   $new4_port token "$client4_token"
814	sleep 0.5
815	verify_subflow_events "$client_evts" "$SUB_ESTABLISHED" "$client4_token" "$AF_INET"\
816			      "10.0.2.2" "10.0.2.1" "$new4_port" "23" "$server_addr_id" "ns2" "ns1"
817
818	# Delete the listener from the server ns, if one was created
819	mptcp_lib_kill_wait $listener_pid
820
821	sport=$(mptcp_lib_evts_get_info sport "$client_evts" $SUB_ESTABLISHED)
822
823	# DESTROY_SUBFLOW from client to server machine
824	:>"$client_evts"
825	ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
826	   $new4_port token "$client4_token"
827	sleep 0.5
828	verify_subflow_events "$client_evts" "$SUB_CLOSED" "$client4_token" "$AF_INET" "10.0.2.2"\
829			      "10.0.2.1" "$new4_port" "23" "$server_addr_id" "ns2" "ns1"
830
831	# RM_ADDR from server to client machine
832	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
833	   "$server4_token"
834}
835
836test_subflows_v4_v6_mix()
837{
838	print_title "Subflows v4 and v6 mix tests"
839
840	# Attempt to add a listener at 10.0.2.1:<subflow-port>
841	ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
842	   $app6_port &
843	local listener_pid=$!
844
845	# ADD_ADDR4 from server to client machine reusing the subflow port on
846	# the established v6 connection
847	:>"$client_evts"
848	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server6_token" id\
849	   $server_addr_id dev ns1eth2
850	print_test "ADD_ADDR4 id:server 10.0.2.1 (ns1) => ns2, reuse port"
851	sleep 0.5
852	verify_announce_event "$client_evts" "$ANNOUNCED" "$client6_token" "10.0.2.1"\
853			      "$server_addr_id" "$app6_port"
854
855	# CREATE_SUBFLOW from client to server machine
856	:>"$client_evts"
857	ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
858	   $app6_port token "$client6_token"
859	sleep 0.5
860	verify_subflow_events "$client_evts" "$SUB_ESTABLISHED" "$client6_token"\
861			      "$AF_INET" "10.0.2.2" "10.0.2.1" "$app6_port" "23"\
862			      "$server_addr_id" "ns2" "ns1"
863
864	# Delete the listener from the server ns, if one was created
865	mptcp_lib_kill_wait $listener_pid
866
867	sport=$(mptcp_lib_evts_get_info sport "$client_evts" $SUB_ESTABLISHED)
868
869	# DESTROY_SUBFLOW from client to server machine
870	:>"$client_evts"
871	ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
872	   $app6_port token "$client6_token"
873	sleep 0.5
874	verify_subflow_events "$client_evts" "$SUB_CLOSED" "$client6_token" \
875			      "$AF_INET" "10.0.2.2" "10.0.2.1" "$app6_port" "23"\
876			      "$server_addr_id" "ns2" "ns1"
877
878	# RM_ADDR from server to client machine
879	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
880	   "$server6_token"
881	sleep 0.5
882}
883
884test_prio()
885{
886	print_title "Prio tests"
887
888	local count
889
890	# Send MP_PRIO signal from client to server machine
891	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"
892	sleep 0.5
893
894	# Check TX
895	print_test "MP_PRIO TX"
896	count=$(mptcp_lib_get_counter "$ns2" "MPTcpExtMPPrioTx")
897	if [ -z "$count" ]; then
898		test_skip
899	elif [ $count != 1 ]; then
900		test_fail "Count != 1: ${count}"
901	else
902		test_pass
903	fi
904
905	# Check RX
906	print_test "MP_PRIO RX"
907	count=$(mptcp_lib_get_counter "$ns1" "MPTcpExtMPPrioRx")
908	if [ -z "$count" ]; then
909		test_skip
910	elif [ $count != 1 ]; then
911		test_fail "Count != 1: ${count}"
912	else
913		test_pass
914	fi
915}
916
917verify_listener_events()
918{
919	local evt=$1
920	local e_type=$2
921	local e_family=$3
922	local e_saddr=$4
923	local e_sport=$5
924	local type
925	local family
926	local saddr
927	local sport
928
929	type=$(mptcp_lib_evts_get_info type $evt $e_type)
930	family=$(mptcp_lib_evts_get_info family $evt $e_type)
931	sport=$(mptcp_lib_evts_get_info sport $evt $e_type)
932	if [ $family ] && [ $family = $AF_INET6 ]; then
933		saddr=$(mptcp_lib_evts_get_info saddr6 $evt $e_type)
934	else
935		saddr=$(mptcp_lib_evts_get_info saddr4 $evt $e_type)
936	fi
937
938	check_expected "type" "family" "saddr" "sport"
939}
940
941test_listener()
942{
943	print_title "Listener tests"
944
945	if ! mptcp_lib_kallsyms_has "mptcp_event_pm_listener$"; then
946		print_test "LISTENER events"
947		test_skip
948		return
949	fi
950
951	# Capture events on the network namespace running the client
952	:>$client_evts
953
954	# Attempt to add a listener at 10.0.2.2:<subflow-port>
955	ip netns exec $ns2 ./pm_nl_ctl listen 10.0.2.2\
956		$client4_port &
957	local listener_pid=$!
958
959	sleep 0.5
960	print_test "CREATE_LISTENER 10.0.2.2 (client port)"
961	verify_listener_events $client_evts $LISTENER_CREATED $AF_INET 10.0.2.2 $client4_port
962
963	# ADD_ADDR from client to server machine reusing the subflow port
964	ip netns exec $ns2 ./pm_nl_ctl ann 10.0.2.2 token $client4_token id\
965		$client_addr_id
966	sleep 0.5
967
968	# CREATE_SUBFLOW from server to client machine
969	ip netns exec $ns1 ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2\
970		rport $client4_port token $server4_token
971	sleep 0.5
972
973	# Delete the listener from the client ns, if one was created
974	mptcp_lib_kill_wait $listener_pid
975
976	sleep 0.5
977	print_test "CLOSE_LISTENER 10.0.2.2 (client port)"
978	verify_listener_events $client_evts $LISTENER_CLOSED $AF_INET 10.0.2.2 $client4_port
979}
980
981print_title "Make connections"
982make_connection
983make_connection "v6"
984print_title "Will be using address IDs ${client_addr_id} (client) and ${server_addr_id} (server)"
985
986test_announce
987test_remove
988test_subflows
989test_subflows_v4_v6_mix
990test_prio
991test_listener
992
993mptcp_lib_result_print_all_tap
994exit ${ret}
995