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