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