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