1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ip -Version > /dev/null 2>&1
5if [ $? -ne 0 ];then
6	echo "SKIP: Cannot not run test without ip tool"
7	exit 1
8fi
9
10ANNOUNCED=6        # MPTCP_EVENT_ANNOUNCED
11REMOVED=7          # MPTCP_EVENT_REMOVED
12SUB_ESTABLISHED=10 # MPTCP_EVENT_SUB_ESTABLISHED
13SUB_CLOSED=11      # MPTCP_EVENT_SUB_CLOSED
14
15AF_INET=2
16AF_INET6=10
17
18evts_pid=0
19client4_pid=0
20server4_pid=0
21client6_pid=0
22server6_pid=0
23client4_token=""
24server4_token=""
25client6_token=""
26server6_token=""
27client4_port=0;
28client6_port=0;
29app4_port=50002
30new4_port=50003
31app6_port=50004
32client_addr_id=${RANDOM:0:2}
33server_addr_id=${RANDOM:0:2}
34
35sec=$(date +%s)
36rndh=$(stdbuf -o0 -e0 printf %x "$sec")-$(mktemp -u XXXXXX)
37ns1="ns1-$rndh"
38ns2="ns2-$rndh"
39
40kill_wait()
41{
42	kill $1 > /dev/null 2>&1
43	wait $1 2>/dev/null
44}
45
46cleanup()
47{
48	echo "cleanup"
49
50	rm -rf $file
51
52	# Terminate the MPTCP connection and related processes
53	if [ $client4_pid -ne 0 ]; then
54		kill -SIGUSR1 $client4_pid > /dev/null 2>&1
55	fi
56	if [ $server4_pid -ne 0 ]; then
57		kill_wait $server4_pid
58	fi
59	if [ $client6_pid -ne 0 ]; then
60		kill -SIGUSR1 $client6_pid > /dev/null 2>&1
61	fi
62	if [ $server6_pid -ne 0 ]; then
63		kill_wait $server6_pid
64	fi
65	if [ $evts_pid -ne 0 ]; then
66		kill_wait $evts_pid
67	fi
68	local netns
69	for netns in "$ns1" "$ns2" ;do
70		ip netns del "$netns"
71	done
72}
73
74trap cleanup EXIT
75
76# Create and configure network namespaces for testing
77for i in "$ns1" "$ns2" ;do
78	ip netns add "$i" || exit 1
79	ip -net "$i" link set lo up
80	ip netns exec "$i" sysctl -q net.mptcp.enabled=1
81	ip netns exec "$i" sysctl -q net.mptcp.pm_type=1
82done
83
84#  "$ns1"              ns2
85#     ns1eth2    ns2eth1
86
87ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
88
89# Add IPv4/v6 addresses to the namespaces
90ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth2
91ip -net "$ns1" addr add 10.0.2.1/24 dev ns1eth2
92ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth2 nodad
93ip -net "$ns1" addr add dead:beef:2::1/64 dev ns1eth2 nodad
94ip -net "$ns1" link set ns1eth2 up
95
96ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
97ip -net "$ns2" addr add 10.0.2.2/24 dev ns2eth1
98ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1 nodad
99ip -net "$ns2" addr add dead:beef:2::2/64 dev ns2eth1 nodad
100ip -net "$ns2" link set ns2eth1 up
101
102stdbuf -o0 -e0 printf "Created network namespaces ns1, ns2         \t\t\t[OK]\n"
103
104make_file()
105{
106	# Store a chunk of data in a file to transmit over an MPTCP connection
107	local name=$1
108	local ksize=1
109
110	dd if=/dev/urandom of="$name" bs=2 count=$ksize 2> /dev/null
111	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
112}
113
114make_connection()
115{
116	local file
117	file=$(mktemp)
118	make_file "$file" "client"
119
120	local is_v6=$1
121	local app_port=$app4_port
122	local connect_addr="10.0.1.1"
123	local listen_addr="0.0.0.0"
124	if [ "$is_v6" = "v6" ]
125	then
126		connect_addr="dead:beef:1::1"
127		listen_addr="::"
128		app_port=$app6_port
129	else
130		is_v6="v4"
131	fi
132
133	# Capture netlink events over the two network namespaces running
134	# the MPTCP client and server
135	local client_evts
136	client_evts=$(mktemp)
137	:>"$client_evts"
138	ip netns exec "$ns2" ./pm_nl_ctl events >> "$client_evts" 2>&1 &
139	local client_evts_pid=$!
140	local server_evts
141	server_evts=$(mktemp)
142	:>"$server_evts"
143	ip netns exec "$ns1" ./pm_nl_ctl events >> "$server_evts" 2>&1 &
144	local server_evts_pid=$!
145	sleep 0.5
146
147	# Run the server
148	ip netns exec "$ns1" \
149	   ./mptcp_connect -s MPTCP -w 300 -p $app_port -l $listen_addr > /dev/null 2>&1 &
150	local server_pid=$!
151	sleep 0.5
152
153	# Run the client, transfer $file and stay connected to the server
154	# to conduct tests
155	ip netns exec "$ns2" \
156	   ./mptcp_connect -s MPTCP -w 300 -m sendfile -p $app_port $connect_addr\
157	   2>&1 > /dev/null < "$file" &
158	local client_pid=$!
159	sleep 1
160
161	# Capture client/server attributes from MPTCP connection netlink events
162	kill_wait $client_evts_pid
163
164	local client_token
165	local client_port
166	local client_serverside
167	local server_token
168	local server_serverside
169
170	client_token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$client_evts")
171	client_port=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$client_evts")
172	client_serverside=$(sed --unbuffered -n 's/.*\(server_side:\)\([[:digit:]]*\).*$/\2/p;q'\
173				      "$client_evts")
174	kill_wait $server_evts_pid
175	server_token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
176	server_serverside=$(sed --unbuffered -n 's/.*\(server_side:\)\([[:digit:]]*\).*$/\2/p;q'\
177				      "$server_evts")
178	rm -f "$client_evts" "$server_evts" "$file"
179
180	if [ "$client_token" != "" ] && [ "$server_token" != "" ] && [ "$client_serverside" = 0 ] &&
181		   [ "$server_serverside" = 1 ]
182	then
183		stdbuf -o0 -e0 printf "Established IP%s MPTCP Connection ns2 => ns1    \t\t[OK]\n" $is_v6
184	else
185		exit 1
186	fi
187
188	if [ "$is_v6" = "v6" ]
189	then
190		client6_token=$client_token
191		server6_token=$server_token
192		client6_port=$client_port
193		client6_pid=$client_pid
194		server6_pid=$server_pid
195	else
196		client4_token=$client_token
197		server4_token=$server_token
198		client4_port=$client_port
199		client4_pid=$client_pid
200		server4_pid=$server_pid
201	fi
202}
203
204verify_announce_event()
205{
206	local evt=$1
207	local e_type=$2
208	local e_token=$3
209	local e_addr=$4
210	local e_id=$5
211	local e_dport=$6
212	local e_af=$7
213	local type
214	local token
215	local addr
216	local dport
217	local id
218
219	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
220	token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
221	if [ "$e_af" = "v6" ]
222	then
223		addr=$(sed --unbuffered -n 's/.*\(daddr6:\)\([0-9a-f:.]*\).*$/\2/p;q' "$evt")
224	else
225		addr=$(sed --unbuffered -n 's/.*\(daddr4:\)\([0-9.]*\).*$/\2/p;q' "$evt")
226	fi
227	dport=$(sed --unbuffered -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
228	id=$(sed --unbuffered -n 's/.*\(rem_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
229	if [ "$type" = "$e_type" ] && [ "$token" = "$e_token" ] &&
230		   [ "$addr" = "$e_addr" ] && [ "$dport" = "$e_dport" ] &&
231		   [ "$id" = "$e_id" ]
232	then
233		stdbuf -o0 -e0 printf "[OK]\n"
234		return 0
235	fi
236	stdbuf -o0 -e0 printf "[FAIL]\n"
237	exit 1
238}
239
240test_announce()
241{
242	local evts
243	evts=$(mktemp)
244	# Capture events on the network namespace running the server
245	:>"$evts"
246	ip netns exec "$ns1" ./pm_nl_ctl events >> "$evts" 2>&1 &
247	evts_pid=$!
248	sleep 0.5
249
250	# ADD_ADDR using an invalid token should result in no action
251	local invalid_token=$(( client4_token - 1))
252	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token $invalid_token id\
253	   $client_addr_id dev ns2eth1 > /dev/null 2>&1
254
255	local type
256	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evts")
257	stdbuf -o0 -e0 printf "ADD_ADDR 10.0.2.2 (ns2) => ns1, invalid token    \t\t"
258	if [ "$type" = "" ]
259	then
260		stdbuf -o0 -e0 printf "[OK]\n"
261	else
262		stdbuf -o0 -e0 printf "[FAIL]\n"
263		exit 1
264	fi
265
266	# ADD_ADDR from the client to server machine reusing the subflow port
267	:>"$evts"
268	ip netns exec "$ns2"\
269	   ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id $client_addr_id dev\
270	   ns2eth1 > /dev/null 2>&1
271	stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.2 (ns2) => ns1, reuse port \t\t" $client_addr_id
272	sleep 0.5
273	verify_announce_event "$evts" "$ANNOUNCED" "$server4_token" "10.0.2.2" "$client_addr_id"\
274			      "$client4_port"
275
276	# ADD_ADDR6 from the client to server machine reusing the subflow port
277	:>"$evts"
278	ip netns exec "$ns2" ./pm_nl_ctl ann\
279	   dead:beef:2::2 token "$client6_token" id $client_addr_id dev ns2eth1 > /dev/null 2>&1
280	stdbuf -o0 -e0 printf "ADD_ADDR6 id:%d dead:beef:2::2 (ns2) => ns1, reuse port\t\t" $client_addr_id
281	sleep 0.5
282	verify_announce_event "$evts" "$ANNOUNCED" "$server6_token" "dead:beef:2::2"\
283			      "$client_addr_id" "$client6_port" "v6"
284
285	# ADD_ADDR from the client to server machine using a new port
286	:>"$evts"
287	client_addr_id=$((client_addr_id+1))
288	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
289	   $client_addr_id dev ns2eth1 port $new4_port > /dev/null 2>&1
290	stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.2 (ns2) => ns1, new port \t\t\t" $client_addr_id
291	sleep 0.5
292	verify_announce_event "$evts" "$ANNOUNCED" "$server4_token" "10.0.2.2"\
293			      "$client_addr_id" "$new4_port"
294
295	kill_wait $evts_pid
296
297	# Capture events on the network namespace running the client
298	:>"$evts"
299	ip netns exec "$ns2" ./pm_nl_ctl events >> "$evts" 2>&1 &
300	evts_pid=$!
301	sleep 0.5
302
303	# ADD_ADDR from the server to client machine reusing the subflow port
304	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
305	   $server_addr_id dev ns1eth2 > /dev/null 2>&1
306	stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.1 (ns1) => ns2, reuse port \t\t" $server_addr_id
307	sleep 0.5
308	verify_announce_event "$evts" "$ANNOUNCED" "$client4_token" "10.0.2.1"\
309			      "$server_addr_id" "$app4_port"
310
311	# ADD_ADDR6 from the server to client machine reusing the subflow port
312	:>"$evts"
313	ip netns exec "$ns1" ./pm_nl_ctl ann dead:beef:2::1 token "$server6_token" id\
314	   $server_addr_id dev ns1eth2 > /dev/null 2>&1
315	stdbuf -o0 -e0 printf "ADD_ADDR6 id:%d dead:beef:2::1 (ns1) => ns2, reuse port\t\t" $server_addr_id
316	sleep 0.5
317	verify_announce_event "$evts" "$ANNOUNCED" "$client6_token" "dead:beef:2::1"\
318			      "$server_addr_id" "$app6_port" "v6"
319
320	# ADD_ADDR from the server to client machine using a new port
321	:>"$evts"
322	server_addr_id=$((server_addr_id+1))
323	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
324	   $server_addr_id dev ns1eth2 port $new4_port > /dev/null 2>&1
325	stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.1 (ns1) => ns2, new port \t\t\t" $server_addr_id
326	sleep 0.5
327	verify_announce_event "$evts" "$ANNOUNCED" "$client4_token" "10.0.2.1"\
328			      "$server_addr_id" "$new4_port"
329
330	kill_wait $evts_pid
331	rm -f "$evts"
332}
333
334verify_remove_event()
335{
336	local evt=$1
337	local e_type=$2
338	local e_token=$3
339	local e_id=$4
340	local type
341	local token
342	local id
343
344	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
345	token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
346	id=$(sed --unbuffered -n 's/.*\(rem_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
347	if [ "$type" = "$e_type" ] && [ "$token" = "$e_token" ] &&
348		   [ "$id" = "$e_id" ]
349	then
350		stdbuf -o0 -e0 printf "[OK]\n"
351		return 0
352	fi
353	stdbuf -o0 -e0 printf "[FAIL]\n"
354	exit 1
355}
356
357test_remove()
358{
359	local evts
360	evts=$(mktemp)
361
362	# Capture events on the network namespace running the server
363	:>"$evts"
364	ip netns exec "$ns1" ./pm_nl_ctl events >> "$evts" 2>&1 &
365	evts_pid=$!
366	sleep 0.5
367
368	# RM_ADDR using an invalid token should result in no action
369	local invalid_token=$(( client4_token - 1 ))
370	ip netns exec "$ns2" ./pm_nl_ctl rem token $invalid_token id\
371	   $client_addr_id > /dev/null 2>&1
372	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1, invalid token                    \t"\
373	       $client_addr_id
374	local type
375	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evts")
376	if [ "$type" = "" ]
377	then
378		stdbuf -o0 -e0 printf "[OK]\n"
379	else
380		stdbuf -o0 -e0 printf "[FAIL]\n"
381	fi
382
383	# RM_ADDR using an invalid addr id should result in no action
384	local invalid_id=$(( client_addr_id + 1 ))
385	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
386	   $invalid_id > /dev/null 2>&1
387	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1, invalid id                    \t"\
388	       $invalid_id
389	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evts")
390	if [ "$type" = "" ]
391	then
392		stdbuf -o0 -e0 printf "[OK]\n"
393	else
394		stdbuf -o0 -e0 printf "[FAIL]\n"
395	fi
396
397	# RM_ADDR from the client to server machine
398	:>"$evts"
399	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
400	   $client_addr_id > /dev/null 2>&1
401	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1                                \t"\
402	       $client_addr_id
403	sleep 0.5
404	verify_remove_event "$evts" "$REMOVED" "$server4_token" "$client_addr_id"
405
406	# RM_ADDR from the client to server machine
407	:>"$evts"
408	client_addr_id=$(( client_addr_id - 1 ))
409	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
410	   $client_addr_id > /dev/null 2>&1
411	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1                                \t"\
412	       $client_addr_id
413	sleep 0.5
414	verify_remove_event "$evts" "$REMOVED" "$server4_token" "$client_addr_id"
415
416	# RM_ADDR6 from the client to server machine
417	:>"$evts"
418	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client6_token" id\
419	   $client_addr_id > /dev/null 2>&1
420	stdbuf -o0 -e0 printf "RM_ADDR6 id:%d ns2 => ns1                               \t"\
421	       $client_addr_id
422	sleep 0.5
423	verify_remove_event "$evts" "$REMOVED" "$server6_token" "$client_addr_id"
424
425	kill_wait $evts_pid
426
427	# Capture events on the network namespace running the client
428	:>"$evts"
429	ip netns exec "$ns2" ./pm_nl_ctl events >> "$evts" 2>&1 &
430	evts_pid=$!
431	sleep 0.5
432
433	# RM_ADDR from the server to client machine
434	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server4_token" id\
435	   $server_addr_id > /dev/null 2>&1
436	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns1 => ns2                                \t"\
437	       $server_addr_id
438	sleep 0.5
439	verify_remove_event "$evts" "$REMOVED" "$client4_token" "$server_addr_id"
440
441	# RM_ADDR from the server to client machine
442	:>"$evts"
443	server_addr_id=$(( server_addr_id - 1 ))
444	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server4_token" id\
445	   $server_addr_id > /dev/null 2>&1
446	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns1 => ns2                                \t" $server_addr_id
447	sleep 0.5
448	verify_remove_event "$evts" "$REMOVED" "$client4_token" "$server_addr_id"
449
450	# RM_ADDR6 from the server to client machine
451	:>"$evts"
452	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server6_token" id\
453	   $server_addr_id > /dev/null 2>&1
454	stdbuf -o0 -e0 printf "RM_ADDR6 id:%d ns1 => ns2                               \t" $server_addr_id
455	sleep 0.5
456	verify_remove_event "$evts" "$REMOVED" "$client6_token" "$server_addr_id"
457
458	kill_wait $evts_pid
459	rm -f "$evts"
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
485	if [ "$e_type" = "$SUB_ESTABLISHED" ]
486	then
487		if [ "$e_family" = "$AF_INET6" ]
488		then
489			stdbuf -o0 -e0 printf "CREATE_SUBFLOW6 %s (%s) => %s (%s)    "\
490			       "$e_saddr" "$e_from" "$e_daddr" "$e_to"
491		else
492			stdbuf -o0 -e0 printf "CREATE_SUBFLOW %s (%s) => %s (%s)         \t"\
493			       "$e_saddr" "$e_from" "$e_daddr" "$e_to"
494		fi
495	else
496		if [ "$e_family" = "$AF_INET6" ]
497		then
498			stdbuf -o0 -e0 printf "DESTROY_SUBFLOW6 %s (%s) => %s (%s)   "\
499			       "$e_saddr" "$e_from" "$e_daddr" "$e_to"
500		else
501			stdbuf -o0 -e0 printf "DESTROY_SUBFLOW %s (%s) => %s (%s)         \t"\
502			       "$e_saddr" "$e_from" "$e_daddr" "$e_to"
503		fi
504	fi
505
506	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
507	token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
508	family=$(sed --unbuffered -n 's/.*\(family:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
509	dport=$(sed --unbuffered -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
510	locid=$(sed --unbuffered -n 's/.*\(loc_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
511	remid=$(sed --unbuffered -n 's/.*\(rem_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
512	if [ "$family" = "$AF_INET6" ]
513	then
514		saddr=$(sed --unbuffered -n 's/.*\(saddr6:\)\([0-9a-f:.]*\).*$/\2/p;q' "$evt")
515		daddr=$(sed --unbuffered -n 's/.*\(daddr6:\)\([0-9a-f:.]*\).*$/\2/p;q' "$evt")
516	else
517		saddr=$(sed --unbuffered -n 's/.*\(saddr4:\)\([0-9.]*\).*$/\2/p;q' "$evt")
518		daddr=$(sed --unbuffered -n 's/.*\(daddr4:\)\([0-9.]*\).*$/\2/p;q' "$evt")
519	fi
520
521	if [ "$type" = "$e_type" ] && [ "$token" = "$e_token" ] &&
522		   [ "$daddr" = "$e_daddr" ] && [ "$e_dport" = "$dport" ] &&
523		   [ "$family" = "$e_family" ] && [ "$saddr" = "$e_saddr" ] &&
524		   [ "$e_locid" = "$locid" ] && [ "$e_remid" = "$remid" ]
525	then
526		stdbuf -o0 -e0 printf "[OK]\n"
527		return 0
528	fi
529	stdbuf -o0 -e0 printf "[FAIL]\n"
530	exit 1
531}
532
533test_subflows()
534{
535	local evts
536	evts=$(mktemp)
537	# Capture events on the network namespace running the server
538	:>"$evts"
539	ip netns exec "$ns1" ./pm_nl_ctl events >> "$evts" 2>&1 &
540	evts_pid=$!
541	sleep 0.5
542
543	# Attempt to add a listener at 10.0.2.2:<subflow-port>
544	ip netns exec "$ns2" ./pm_nl_ctl listen 10.0.2.2\
545	   "$client4_port" > /dev/null 2>&1 &
546	local listener_pid=$!
547
548	# ADD_ADDR from client to server machine reusing the subflow port
549	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
550	   $client_addr_id > /dev/null 2>&1
551	sleep 0.5
552
553	# CREATE_SUBFLOW from server to client machine
554	:>"$evts"
555	ip netns exec "$ns1" ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2\
556	   rport "$client4_port" token "$server4_token" > /dev/null 2>&1
557	sleep 0.5
558	verify_subflow_events "$evts" "$SUB_ESTABLISHED" "$server4_token" "$AF_INET" "10.0.2.1"\
559			      "10.0.2.2" "$client4_port" "23" "$client_addr_id" "ns1" "ns2"
560
561	# Delete the listener from the client ns, if one was created
562	kill_wait $listener_pid
563
564	local sport
565	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$evts")
566
567	# DESTROY_SUBFLOW from server to client machine
568	:>"$evts"
569	ip netns exec "$ns1" ./pm_nl_ctl dsf lip 10.0.2.1 lport "$sport" rip 10.0.2.2 rport\
570	   "$client4_port" token "$server4_token" > /dev/null 2>&1
571	sleep 0.5
572	verify_subflow_events "$evts" "$SUB_CLOSED" "$server4_token" "$AF_INET" "10.0.2.1"\
573			      "10.0.2.2" "$client4_port" "23" "$client_addr_id" "ns1" "ns2"
574
575	# RM_ADDR from client to server machine
576	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
577	   "$client4_token" > /dev/null 2>&1
578	sleep 0.5
579
580	# Attempt to add a listener at dead:beef:2::2:<subflow-port>
581	ip netns exec "$ns2" ./pm_nl_ctl listen dead:beef:2::2\
582	   "$client6_port" > /dev/null 2>&1 &
583	listener_pid=$!
584
585	# ADD_ADDR6 from client to server machine reusing the subflow port
586	:>"$evts"
587	ip netns exec "$ns2" ./pm_nl_ctl ann dead:beef:2::2 token "$client6_token" id\
588	   $client_addr_id > /dev/null 2>&1
589	sleep 0.5
590
591	# CREATE_SUBFLOW6 from server to client machine
592	:>"$evts"
593	ip netns exec "$ns1" ./pm_nl_ctl csf lip dead:beef:2::1 lid 23 rip\
594	   dead:beef:2::2 rport "$client6_port" token "$server6_token" > /dev/null 2>&1
595	sleep 0.5
596	verify_subflow_events "$evts" "$SUB_ESTABLISHED" "$server6_token" "$AF_INET6"\
597			      "dead:beef:2::1" "dead:beef:2::2" "$client6_port" "23"\
598			      "$client_addr_id" "ns1" "ns2"
599
600	# Delete the listener from the client ns, if one was created
601	kill_wait $listener_pid
602
603	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$evts")
604
605	# DESTROY_SUBFLOW6 from server to client machine
606	:>"$evts"
607	ip netns exec "$ns1" ./pm_nl_ctl dsf lip dead:beef:2::1 lport "$sport" rip\
608	   dead:beef:2::2 rport "$client6_port" token "$server6_token" > /dev/null 2>&1
609	sleep 0.5
610	verify_subflow_events "$evts" "$SUB_CLOSED" "$server6_token" "$AF_INET6"\
611			      "dead:beef:2::1" "dead:beef:2::2" "$client6_port" "23"\
612			      "$client_addr_id" "ns1" "ns2"
613
614	# RM_ADDR from client to server machine
615	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
616	   "$client6_token" > /dev/null 2>&1
617	sleep 0.5
618
619	# Attempt to add a listener at 10.0.2.2:<new-port>
620	ip netns exec "$ns2" ./pm_nl_ctl listen 10.0.2.2\
621	   $new4_port > /dev/null 2>&1 &
622	listener_pid=$!
623
624	# ADD_ADDR from client to server machine using a new port
625	:>"$evts"
626	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
627	   $client_addr_id port $new4_port > /dev/null 2>&1
628	sleep 0.5
629
630	# CREATE_SUBFLOW from server to client machine
631	:>"$evts"
632	ip netns exec "$ns1" ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2 rport\
633	   $new4_port token "$server4_token" > /dev/null 2>&1
634	sleep 0.5
635	verify_subflow_events "$evts" "$SUB_ESTABLISHED" "$server4_token" "$AF_INET"\
636			      "10.0.2.1" "10.0.2.2" "$new4_port" "23"\
637			      "$client_addr_id" "ns1" "ns2"
638
639	# Delete the listener from the client ns, if one was created
640	kill_wait $listener_pid
641
642	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$evts")
643
644	# DESTROY_SUBFLOW from server to client machine
645	:>"$evts"
646	ip netns exec "$ns1" ./pm_nl_ctl dsf lip 10.0.2.1 lport "$sport" rip 10.0.2.2 rport\
647	   $new4_port token "$server4_token" > /dev/null 2>&1
648	sleep 0.5
649	verify_subflow_events "$evts" "$SUB_CLOSED" "$server4_token" "$AF_INET" "10.0.2.1"\
650			      "10.0.2.2" "$new4_port" "23" "$client_addr_id" "ns1" "ns2"
651
652	# RM_ADDR from client to server machine
653	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
654	   "$client4_token" > /dev/null 2>&1
655
656	kill_wait $evts_pid
657
658	# Capture events on the network namespace running the client
659	:>"$evts"
660	ip netns exec "$ns2" ./pm_nl_ctl events >> "$evts" 2>&1 &
661	evts_pid=$!
662	sleep 0.5
663
664	# Attempt to add a listener at 10.0.2.1:<subflow-port>
665	ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
666	   $app4_port > /dev/null 2>&1 &
667	listener_pid=$!
668
669	# ADD_ADDR from server to client machine reusing the subflow port
670	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
671	   $server_addr_id > /dev/null 2>&1
672	sleep 0.5
673
674	# CREATE_SUBFLOW from client to server machine
675	:>"$evts"
676	ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
677	   $app4_port token "$client4_token" > /dev/null 2>&1
678	sleep 0.5
679	verify_subflow_events "$evts" "$SUB_ESTABLISHED" "$client4_token" "$AF_INET" "10.0.2.2"\
680			      "10.0.2.1" "$app4_port" "23" "$server_addr_id" "ns2" "ns1"
681
682	# Delete the listener from the server ns, if one was created
683	kill_wait $listener_pid
684
685	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$evts")
686
687	# DESTROY_SUBFLOW from client to server machine
688	:>"$evts"
689	ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
690	   $app4_port token "$client4_token" > /dev/null 2>&1
691	sleep 0.5
692	verify_subflow_events "$evts" "$SUB_CLOSED" "$client4_token" "$AF_INET" "10.0.2.2"\
693			      "10.0.2.1" "$app4_port" "23" "$server_addr_id" "ns2" "ns1"
694
695	# RM_ADDR from server to client machine
696	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
697	   "$server4_token" > /dev/null 2>&1
698	sleep 0.5
699
700	# Attempt to add a listener at dead:beef:2::1:<subflow-port>
701	ip netns exec "$ns1" ./pm_nl_ctl listen dead:beef:2::1\
702	   $app6_port > /dev/null 2>&1 &
703	listener_pid=$!
704
705	# ADD_ADDR6 from server to client machine reusing the subflow port
706	:>"$evts"
707	ip netns exec "$ns1" ./pm_nl_ctl ann dead:beef:2::1 token "$server6_token" id\
708	   $server_addr_id > /dev/null 2>&1
709	sleep 0.5
710
711	# CREATE_SUBFLOW6 from client to server machine
712	:>"$evts"
713	ip netns exec "$ns2" ./pm_nl_ctl csf lip dead:beef:2::2 lid 23 rip\
714	   dead:beef:2::1 rport $app6_port token "$client6_token" > /dev/null 2>&1
715	sleep 0.5
716	verify_subflow_events "$evts" "$SUB_ESTABLISHED" "$client6_token"\
717			      "$AF_INET6" "dead:beef:2::2"\
718			      "dead:beef:2::1" "$app6_port" "23"\
719			      "$server_addr_id" "ns2" "ns1"
720
721	# Delete the listener from the server ns, if one was created
722	kill_wait $listener_pid
723
724	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$evts")
725
726	# DESTROY_SUBFLOW6 from client to server machine
727	:>"$evts"
728	ip netns exec "$ns2" ./pm_nl_ctl dsf lip dead:beef:2::2 lport "$sport" rip\
729	   dead:beef:2::1 rport $app6_port token "$client6_token" > /dev/null 2>&1
730	sleep 0.5
731	verify_subflow_events "$evts" "$SUB_CLOSED" "$client6_token" "$AF_INET6" "dead:beef:2::2"\
732			      "dead:beef:2::1" "$app6_port" "23" "$server_addr_id" "ns2" "ns1"
733
734	# RM_ADDR6 from server to client machine
735	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
736	   "$server6_token" > /dev/null 2>&1
737	sleep 0.5
738
739	# Attempt to add a listener at 10.0.2.1:<new-port>
740	ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
741	   $new4_port > /dev/null 2>&1 &
742	listener_pid=$!
743
744	# ADD_ADDR from server to client machine using a new port
745	:>"$evts"
746	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
747	   $server_addr_id port $new4_port > /dev/null 2>&1
748	sleep 0.5
749
750	# CREATE_SUBFLOW from client to server machine
751	:>"$evts"
752	ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
753	   $new4_port token "$client4_token" > /dev/null 2>&1
754	sleep 0.5
755	verify_subflow_events "$evts" "$SUB_ESTABLISHED" "$client4_token" "$AF_INET"\
756			      "10.0.2.2" "10.0.2.1" "$new4_port" "23" "$server_addr_id" "ns2" "ns1"
757
758	# Delete the listener from the server ns, if one was created
759	kill_wait $listener_pid
760
761	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$evts")
762
763	# DESTROY_SUBFLOW from client to server machine
764	:>"$evts"
765	ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
766	   $new4_port token "$client4_token" > /dev/null 2>&1
767	sleep 0.5
768	verify_subflow_events "$evts" "$SUB_CLOSED" "$client4_token" "$AF_INET" "10.0.2.2"\
769			      "10.0.2.1" "$new4_port" "23" "$server_addr_id" "ns2" "ns1"
770
771	# RM_ADDR from server to client machine
772	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
773	   "$server4_token" > /dev/null 2>&1
774
775	kill_wait $evts_pid
776	rm -f "$evts"
777}
778
779test_prio()
780{
781	local count
782
783	# Send MP_PRIO signal from client to server machine
784	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 "$server4_port"
785	sleep 0.5
786
787	# Check TX
788	stdbuf -o0 -e0 printf "MP_PRIO TX                                                 \t"
789	count=$(ip netns exec "$ns2" nstat -as | grep MPTcpExtMPPrioTx | awk '{print $2}')
790	[ -z "$count" ] && count=0
791	if [ $count != 1 ]; then
792		stdbuf -o0 -e0 printf "[FAIL]\n"
793		exit 1
794	else
795		stdbuf -o0 -e0 printf "[OK]\n"
796	fi
797
798	# Check RX
799	stdbuf -o0 -e0 printf "MP_PRIO RX                                                 \t"
800	count=$(ip netns exec "$ns1" nstat -as | grep MPTcpExtMPPrioRx | awk '{print $2}')
801	[ -z "$count" ] && count=0
802	if [ $count != 1 ]; then
803		stdbuf -o0 -e0 printf "[FAIL]\n"
804		exit 1
805	else
806		stdbuf -o0 -e0 printf "[OK]\n"
807	fi
808}
809
810make_connection
811make_connection "v6"
812test_announce
813test_remove
814test_subflows
815test_prio
816
817exit 0
818