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
46kill_wait()
47{
48	kill $1 > /dev/null 2>&1
49	wait $1 2>/dev/null
50}
51
52cleanup()
53{
54	echo "cleanup"
55
56	rm -rf $file $client_evts $server_evts
57
58	# Terminate the MPTCP connection and related processes
59	if [ $client4_pid -ne 0 ]; then
60		kill -SIGUSR1 $client4_pid > /dev/null 2>&1
61	fi
62	if [ $server4_pid -ne 0 ]; then
63		kill_wait $server4_pid
64	fi
65	if [ $client6_pid -ne 0 ]; then
66		kill -SIGUSR1 $client6_pid > /dev/null 2>&1
67	fi
68	if [ $server6_pid -ne 0 ]; then
69		kill_wait $server6_pid
70	fi
71	if [ $server_evts_pid -ne 0 ]; then
72		kill_wait $server_evts_pid
73	fi
74	if [ $client_evts_pid -ne 0 ]; then
75		kill_wait $client_evts_pid
76	fi
77	local netns
78	for netns in "$ns1" "$ns2" ;do
79		ip netns del "$netns"
80	done
81}
82
83trap cleanup EXIT
84
85# Create and configure network namespaces for testing
86for i in "$ns1" "$ns2" ;do
87	ip netns add "$i" || exit 1
88	ip -net "$i" link set lo up
89	ip netns exec "$i" sysctl -q net.mptcp.enabled=1
90	ip netns exec "$i" sysctl -q net.mptcp.pm_type=1
91done
92
93#  "$ns1"              ns2
94#     ns1eth2    ns2eth1
95
96ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
97
98# Add IPv4/v6 addresses to the namespaces
99ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth2
100ip -net "$ns1" addr add 10.0.2.1/24 dev ns1eth2
101ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth2 nodad
102ip -net "$ns1" addr add dead:beef:2::1/64 dev ns1eth2 nodad
103ip -net "$ns1" link set ns1eth2 up
104
105ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
106ip -net "$ns2" addr add 10.0.2.2/24 dev ns2eth1
107ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1 nodad
108ip -net "$ns2" addr add dead:beef:2::2/64 dev ns2eth1 nodad
109ip -net "$ns2" link set ns2eth1 up
110
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	if [ "$client_token" != "" ] && [ "$server_token" != "" ] && [ "$client_serverside" = 0 ] &&
197		   [ "$server_serverside" = 1 ]
198	then
199		stdbuf -o0 -e0 printf "Established IP%s MPTCP Connection ns2 => ns1    \t\t[OK]\n" $is_v6
200	else
201		exit 1
202	fi
203
204	if [ "$is_v6" = "v6" ]
205	then
206		client6_token=$client_token
207		server6_token=$server_token
208		client6_port=$client_port
209		client6_pid=$client_pid
210		server6_pid=$server_pid
211	else
212		client4_token=$client_token
213		server4_token=$server_token
214		client4_port=$client_port
215		client4_pid=$client_pid
216		server4_pid=$server_pid
217	fi
218}
219
220verify_announce_event()
221{
222	local evt=$1
223	local e_type=$2
224	local e_token=$3
225	local e_addr=$4
226	local e_id=$5
227	local e_dport=$6
228	local e_af=$7
229	local type
230	local token
231	local addr
232	local dport
233	local id
234
235	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
236	token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
237	if [ "$e_af" = "v6" ]
238	then
239		addr=$(sed --unbuffered -n 's/.*\(daddr6:\)\([0-9a-f:.]*\).*$/\2/p;q' "$evt")
240	else
241		addr=$(sed --unbuffered -n 's/.*\(daddr4:\)\([0-9.]*\).*$/\2/p;q' "$evt")
242	fi
243	dport=$(sed --unbuffered -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
244	id=$(sed --unbuffered -n 's/.*\(rem_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
245	if [ "$type" = "$e_type" ] && [ "$token" = "$e_token" ] &&
246		   [ "$addr" = "$e_addr" ] && [ "$dport" = "$e_dport" ] &&
247		   [ "$id" = "$e_id" ]
248	then
249		stdbuf -o0 -e0 printf "[OK]\n"
250		return 0
251	fi
252	stdbuf -o0 -e0 printf "[FAIL]\n"
253	exit 1
254}
255
256test_announce()
257{
258	# Capture events on the network namespace running the server
259	:>"$server_evts"
260
261	# ADD_ADDR using an invalid token should result in no action
262	local invalid_token=$(( client4_token - 1))
263	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token $invalid_token id\
264	   $client_addr_id dev ns2eth1 > /dev/null 2>&1
265
266	local type
267	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
268	stdbuf -o0 -e0 printf "ADD_ADDR 10.0.2.2 (ns2) => ns1, invalid token    \t\t"
269	if [ "$type" = "" ]
270	then
271		stdbuf -o0 -e0 printf "[OK]\n"
272	else
273		stdbuf -o0 -e0 printf "[FAIL]\n"
274		exit 1
275	fi
276
277	# ADD_ADDR from the client to server machine reusing the subflow port
278	:>"$server_evts"
279	ip netns exec "$ns2"\
280	   ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id $client_addr_id dev\
281	   ns2eth1 > /dev/null 2>&1
282	stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.2 (ns2) => ns1, reuse port \t\t" $client_addr_id
283	sleep 0.5
284	verify_announce_event $server_evts $ANNOUNCED $server4_token "10.0.2.2" $client_addr_id \
285			      "$client4_port"
286
287	# ADD_ADDR6 from the client to server machine reusing the subflow port
288	:>"$server_evts"
289	ip netns exec "$ns2" ./pm_nl_ctl ann\
290	   dead:beef:2::2 token "$client6_token" id $client_addr_id dev ns2eth1 > /dev/null 2>&1
291	stdbuf -o0 -e0 printf "ADD_ADDR6 id:%d dead:beef:2::2 (ns2) => ns1, reuse port\t\t" $client_addr_id
292	sleep 0.5
293	verify_announce_event "$server_evts" "$ANNOUNCED" "$server6_token" "dead:beef:2::2"\
294			      "$client_addr_id" "$client6_port" "v6"
295
296	# ADD_ADDR from the client to server machine using a new port
297	:>"$server_evts"
298	client_addr_id=$((client_addr_id+1))
299	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
300	   $client_addr_id dev ns2eth1 port $new4_port > /dev/null 2>&1
301	stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.2 (ns2) => ns1, new port \t\t\t" $client_addr_id
302	sleep 0.5
303	verify_announce_event "$server_evts" "$ANNOUNCED" "$server4_token" "10.0.2.2"\
304			      "$client_addr_id" "$new4_port"
305
306	# Capture events on the network namespace running the client
307	:>"$client_evts"
308
309	# ADD_ADDR from the server to client machine reusing the subflow port
310	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
311	   $server_addr_id dev ns1eth2 > /dev/null 2>&1
312	stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.1 (ns1) => ns2, reuse port \t\t" $server_addr_id
313	sleep 0.5
314	verify_announce_event "$client_evts" "$ANNOUNCED" "$client4_token" "10.0.2.1"\
315			      "$server_addr_id" "$app4_port"
316
317	# ADD_ADDR6 from the server to client machine reusing the subflow port
318	:>"$client_evts"
319	ip netns exec "$ns1" ./pm_nl_ctl ann dead:beef:2::1 token "$server6_token" id\
320	   $server_addr_id dev ns1eth2 > /dev/null 2>&1
321	stdbuf -o0 -e0 printf "ADD_ADDR6 id:%d dead:beef:2::1 (ns1) => ns2, reuse port\t\t" $server_addr_id
322	sleep 0.5
323	verify_announce_event "$client_evts" "$ANNOUNCED" "$client6_token" "dead:beef:2::1"\
324			      "$server_addr_id" "$app6_port" "v6"
325
326	# ADD_ADDR from the server to client machine using a new port
327	:>"$client_evts"
328	server_addr_id=$((server_addr_id+1))
329	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
330	   $server_addr_id dev ns1eth2 port $new4_port > /dev/null 2>&1
331	stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.1 (ns1) => ns2, new port \t\t\t" $server_addr_id
332	sleep 0.5
333	verify_announce_event "$client_evts" "$ANNOUNCED" "$client4_token" "10.0.2.1"\
334			      "$server_addr_id" "$new4_port"
335}
336
337verify_remove_event()
338{
339	local evt=$1
340	local e_type=$2
341	local e_token=$3
342	local e_id=$4
343	local type
344	local token
345	local id
346
347	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
348	token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
349	id=$(sed --unbuffered -n 's/.*\(rem_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
350	if [ "$type" = "$e_type" ] && [ "$token" = "$e_token" ] &&
351		   [ "$id" = "$e_id" ]
352	then
353		stdbuf -o0 -e0 printf "[OK]\n"
354		return 0
355	fi
356	stdbuf -o0 -e0 printf "[FAIL]\n"
357	exit 1
358}
359
360test_remove()
361{
362	# Capture events on the network namespace running the server
363	:>"$server_evts"
364
365	# RM_ADDR using an invalid token should result in no action
366	local invalid_token=$(( client4_token - 1 ))
367	ip netns exec "$ns2" ./pm_nl_ctl rem token $invalid_token id\
368	   $client_addr_id > /dev/null 2>&1
369	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1, invalid token                    \t"\
370	       $client_addr_id
371	local type
372	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
373	if [ "$type" = "" ]
374	then
375		stdbuf -o0 -e0 printf "[OK]\n"
376	else
377		stdbuf -o0 -e0 printf "[FAIL]\n"
378	fi
379
380	# RM_ADDR using an invalid addr id should result in no action
381	local invalid_id=$(( client_addr_id + 1 ))
382	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
383	   $invalid_id > /dev/null 2>&1
384	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1, invalid id                    \t"\
385	       $invalid_id
386	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
387	if [ "$type" = "" ]
388	then
389		stdbuf -o0 -e0 printf "[OK]\n"
390	else
391		stdbuf -o0 -e0 printf "[FAIL]\n"
392	fi
393
394	# RM_ADDR from the client to server machine
395	:>"$server_evts"
396	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
397	   $client_addr_id > /dev/null 2>&1
398	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1                                \t"\
399	       $client_addr_id
400	sleep 0.5
401	verify_remove_event "$server_evts" "$REMOVED" "$server4_token" "$client_addr_id"
402
403	# RM_ADDR from the client to server machine
404	:>"$server_evts"
405	client_addr_id=$(( client_addr_id - 1 ))
406	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
407	   $client_addr_id > /dev/null 2>&1
408	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1                                \t"\
409	       $client_addr_id
410	sleep 0.5
411	verify_remove_event "$server_evts" "$REMOVED" "$server4_token" "$client_addr_id"
412
413	# RM_ADDR6 from the client to server machine
414	:>"$server_evts"
415	ip netns exec "$ns2" ./pm_nl_ctl rem token "$client6_token" id\
416	   $client_addr_id > /dev/null 2>&1
417	stdbuf -o0 -e0 printf "RM_ADDR6 id:%d ns2 => ns1                               \t"\
418	       $client_addr_id
419	sleep 0.5
420	verify_remove_event "$server_evts" "$REMOVED" "$server6_token" "$client_addr_id"
421
422	# Capture events on the network namespace running the client
423	:>"$client_evts"
424
425	# RM_ADDR from the server to client machine
426	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server4_token" id\
427	   $server_addr_id > /dev/null 2>&1
428	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns1 => ns2                                \t"\
429	       $server_addr_id
430	sleep 0.5
431	verify_remove_event "$client_evts" "$REMOVED" "$client4_token" "$server_addr_id"
432
433	# RM_ADDR from the server to client machine
434	:>"$client_evts"
435	server_addr_id=$(( server_addr_id - 1 ))
436	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server4_token" id\
437	   $server_addr_id > /dev/null 2>&1
438	stdbuf -o0 -e0 printf "RM_ADDR id:%d ns1 => ns2                                \t" $server_addr_id
439	sleep 0.5
440	verify_remove_event "$client_evts" "$REMOVED" "$client4_token" "$server_addr_id"
441
442	# RM_ADDR6 from the server to client machine
443	:>"$client_evts"
444	ip netns exec "$ns1" ./pm_nl_ctl rem token "$server6_token" id\
445	   $server_addr_id > /dev/null 2>&1
446	stdbuf -o0 -e0 printf "RM_ADDR6 id:%d ns1 => ns2                               \t" $server_addr_id
447	sleep 0.5
448	verify_remove_event "$client_evts" "$REMOVED" "$client6_token" "$server_addr_id"
449}
450
451verify_subflow_events()
452{
453	local evt=$1
454	local e_type=$2
455	local e_token=$3
456	local e_family=$4
457	local e_saddr=$5
458	local e_daddr=$6
459	local e_dport=$7
460	local e_locid=$8
461	local e_remid=$9
462	shift 2
463	local e_from=$8
464	local e_to=$9
465	local type
466	local token
467	local family
468	local saddr
469	local daddr
470	local dport
471	local locid
472	local remid
473
474	if [ "$e_type" = "$SUB_ESTABLISHED" ]
475	then
476		if [ "$e_family" = "$AF_INET6" ]
477		then
478			stdbuf -o0 -e0 printf "CREATE_SUBFLOW6 %s (%s) => %s (%s)    "\
479			       "$e_saddr" "$e_from" "$e_daddr" "$e_to"
480		else
481			stdbuf -o0 -e0 printf "CREATE_SUBFLOW %s (%s) => %s (%s)         \t"\
482			       "$e_saddr" "$e_from" "$e_daddr" "$e_to"
483		fi
484	else
485		if [ "$e_family" = "$AF_INET6" ]
486		then
487			stdbuf -o0 -e0 printf "DESTROY_SUBFLOW6 %s (%s) => %s (%s)   "\
488			       "$e_saddr" "$e_from" "$e_daddr" "$e_to"
489		else
490			stdbuf -o0 -e0 printf "DESTROY_SUBFLOW %s (%s) => %s (%s)         \t"\
491			       "$e_saddr" "$e_from" "$e_daddr" "$e_to"
492		fi
493	fi
494
495	type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
496	token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
497	family=$(sed --unbuffered -n 's/.*\(family:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
498	dport=$(sed --unbuffered -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
499	locid=$(sed --unbuffered -n 's/.*\(loc_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
500	remid=$(sed --unbuffered -n 's/.*\(rem_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
501	if [ "$family" = "$AF_INET6" ]
502	then
503		saddr=$(sed --unbuffered -n 's/.*\(saddr6:\)\([0-9a-f:.]*\).*$/\2/p;q' "$evt")
504		daddr=$(sed --unbuffered -n 's/.*\(daddr6:\)\([0-9a-f:.]*\).*$/\2/p;q' "$evt")
505	else
506		saddr=$(sed --unbuffered -n 's/.*\(saddr4:\)\([0-9.]*\).*$/\2/p;q' "$evt")
507		daddr=$(sed --unbuffered -n 's/.*\(daddr4:\)\([0-9.]*\).*$/\2/p;q' "$evt")
508	fi
509
510	if [ "$type" = "$e_type" ] && [ "$token" = "$e_token" ] &&
511		   [ "$daddr" = "$e_daddr" ] && [ "$e_dport" = "$dport" ] &&
512		   [ "$family" = "$e_family" ] && [ "$saddr" = "$e_saddr" ] &&
513		   [ "$e_locid" = "$locid" ] && [ "$e_remid" = "$remid" ]
514	then
515		stdbuf -o0 -e0 printf "[OK]\n"
516		return 0
517	fi
518	stdbuf -o0 -e0 printf "[FAIL]\n"
519	exit 1
520}
521
522test_subflows()
523{
524	# Capture events on the network namespace running the server
525	:>"$server_evts"
526
527	# Attempt to add a listener at 10.0.2.2:<subflow-port>
528	ip netns exec "$ns2" ./pm_nl_ctl listen 10.0.2.2\
529	   "$client4_port" > /dev/null 2>&1 &
530	local listener_pid=$!
531
532	# ADD_ADDR from client to server machine reusing the subflow port
533	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
534	   $client_addr_id > /dev/null 2>&1
535	sleep 0.5
536
537	# CREATE_SUBFLOW from server to client machine
538	:>"$server_evts"
539	ip netns exec "$ns1" ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2\
540	   rport "$client4_port" token "$server4_token" > /dev/null 2>&1
541	sleep 0.5
542	verify_subflow_events $server_evts $SUB_ESTABLISHED $server4_token $AF_INET "10.0.2.1" \
543			      "10.0.2.2" "$client4_port" "23" "$client_addr_id" "ns1" "ns2"
544
545	# Delete the listener from the client ns, if one was created
546	kill_wait $listener_pid
547
548	local sport
549	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
550
551	# DESTROY_SUBFLOW from server to client machine
552	:>"$server_evts"
553	ip netns exec "$ns1" ./pm_nl_ctl dsf lip 10.0.2.1 lport "$sport" rip 10.0.2.2 rport\
554	   "$client4_port" token "$server4_token" > /dev/null 2>&1
555	sleep 0.5
556	verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server4_token" "$AF_INET" "10.0.2.1"\
557			      "10.0.2.2" "$client4_port" "23" "$client_addr_id" "ns1" "ns2"
558
559	# RM_ADDR from client to server machine
560	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
561	   "$client4_token" > /dev/null 2>&1
562	sleep 0.5
563
564	# Attempt to add a listener at dead:beef:2::2:<subflow-port>
565	ip netns exec "$ns2" ./pm_nl_ctl listen dead:beef:2::2\
566	   "$client6_port" > /dev/null 2>&1 &
567	listener_pid=$!
568
569	# ADD_ADDR6 from client to server machine reusing the subflow port
570	:>"$server_evts"
571	ip netns exec "$ns2" ./pm_nl_ctl ann dead:beef:2::2 token "$client6_token" id\
572	   $client_addr_id > /dev/null 2>&1
573	sleep 0.5
574
575	# CREATE_SUBFLOW6 from server to client machine
576	:>"$server_evts"
577	ip netns exec "$ns1" ./pm_nl_ctl csf lip dead:beef:2::1 lid 23 rip\
578	   dead:beef:2::2 rport "$client6_port" token "$server6_token" > /dev/null 2>&1
579	sleep 0.5
580	verify_subflow_events "$server_evts" "$SUB_ESTABLISHED" "$server6_token" "$AF_INET6"\
581			      "dead:beef:2::1" "dead:beef:2::2" "$client6_port" "23"\
582			      "$client_addr_id" "ns1" "ns2"
583
584	# Delete the listener from the client ns, if one was created
585	kill_wait $listener_pid
586
587	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
588
589	# DESTROY_SUBFLOW6 from server to client machine
590	:>"$server_evts"
591	ip netns exec "$ns1" ./pm_nl_ctl dsf lip dead:beef:2::1 lport "$sport" rip\
592	   dead:beef:2::2 rport "$client6_port" token "$server6_token" > /dev/null 2>&1
593	sleep 0.5
594	verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server6_token" "$AF_INET6"\
595			      "dead:beef:2::1" "dead:beef:2::2" "$client6_port" "23"\
596			      "$client_addr_id" "ns1" "ns2"
597
598	# RM_ADDR from client to server machine
599	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
600	   "$client6_token" > /dev/null 2>&1
601	sleep 0.5
602
603	# Attempt to add a listener at 10.0.2.2:<new-port>
604	ip netns exec "$ns2" ./pm_nl_ctl listen 10.0.2.2\
605	   $new4_port > /dev/null 2>&1 &
606	listener_pid=$!
607
608	# ADD_ADDR from client to server machine using a new port
609	:>"$server_evts"
610	ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
611	   $client_addr_id port $new4_port > /dev/null 2>&1
612	sleep 0.5
613
614	# CREATE_SUBFLOW from server to client machine
615	:>"$server_evts"
616	ip netns exec "$ns1" ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2 rport\
617	   $new4_port token "$server4_token" > /dev/null 2>&1
618	sleep 0.5
619	verify_subflow_events "$server_evts" "$SUB_ESTABLISHED" "$server4_token" "$AF_INET"\
620			      "10.0.2.1" "10.0.2.2" "$new4_port" "23"\
621			      "$client_addr_id" "ns1" "ns2"
622
623	# Delete the listener from the client ns, if one was created
624	kill_wait $listener_pid
625
626	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
627
628	# DESTROY_SUBFLOW from server to client machine
629	:>"$server_evts"
630	ip netns exec "$ns1" ./pm_nl_ctl dsf lip 10.0.2.1 lport "$sport" rip 10.0.2.2 rport\
631	   $new4_port token "$server4_token" > /dev/null 2>&1
632	sleep 0.5
633	verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server4_token" "$AF_INET" "10.0.2.1"\
634			      "10.0.2.2" "$new4_port" "23" "$client_addr_id" "ns1" "ns2"
635
636	# RM_ADDR from client to server machine
637	ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
638	   "$client4_token" > /dev/null 2>&1
639
640	# Capture events on the network namespace running the client
641	:>"$client_evts"
642
643	# Attempt to add a listener at 10.0.2.1:<subflow-port>
644	ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
645	   $app4_port > /dev/null 2>&1 &
646	listener_pid=$!
647
648	# ADD_ADDR from server to client machine reusing the subflow port
649	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
650	   $server_addr_id > /dev/null 2>&1
651	sleep 0.5
652
653	# CREATE_SUBFLOW from client to server machine
654	:>"$client_evts"
655	ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
656	   $app4_port token "$client4_token" > /dev/null 2>&1
657	sleep 0.5
658	verify_subflow_events $client_evts $SUB_ESTABLISHED $client4_token $AF_INET "10.0.2.2"\
659			      "10.0.2.1" "$app4_port" "23" "$server_addr_id" "ns2" "ns1"
660
661	# Delete the listener from the server ns, if one was created
662	kill_wait $listener_pid
663
664	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$client_evts")
665
666	# DESTROY_SUBFLOW from client to server machine
667	:>"$client_evts"
668	ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
669	   $app4_port token "$client4_token" > /dev/null 2>&1
670	sleep 0.5
671	verify_subflow_events "$client_evts" "$SUB_CLOSED" "$client4_token" "$AF_INET" "10.0.2.2"\
672			      "10.0.2.1" "$app4_port" "23" "$server_addr_id" "ns2" "ns1"
673
674	# RM_ADDR from server to client machine
675	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
676	   "$server4_token" > /dev/null 2>&1
677	sleep 0.5
678
679	# Attempt to add a listener at dead:beef:2::1:<subflow-port>
680	ip netns exec "$ns1" ./pm_nl_ctl listen dead:beef:2::1\
681	   $app6_port > /dev/null 2>&1 &
682	listener_pid=$!
683
684	# ADD_ADDR6 from server to client machine reusing the subflow port
685	:>"$client_evts"
686	ip netns exec "$ns1" ./pm_nl_ctl ann dead:beef:2::1 token "$server6_token" id\
687	   $server_addr_id > /dev/null 2>&1
688	sleep 0.5
689
690	# CREATE_SUBFLOW6 from client to server machine
691	:>"$client_evts"
692	ip netns exec "$ns2" ./pm_nl_ctl csf lip dead:beef:2::2 lid 23 rip\
693	   dead:beef:2::1 rport $app6_port token "$client6_token" > /dev/null 2>&1
694	sleep 0.5
695	verify_subflow_events "$client_evts" "$SUB_ESTABLISHED" "$client6_token"\
696			      "$AF_INET6" "dead:beef:2::2"\
697			      "dead:beef:2::1" "$app6_port" "23"\
698			      "$server_addr_id" "ns2" "ns1"
699
700	# Delete the listener from the server ns, if one was created
701	kill_wait $listener_pid
702
703	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$client_evts")
704
705	# DESTROY_SUBFLOW6 from client to server machine
706	:>"$client_evts"
707	ip netns exec "$ns2" ./pm_nl_ctl dsf lip dead:beef:2::2 lport "$sport" rip\
708	   dead:beef:2::1 rport $app6_port token "$client6_token" > /dev/null 2>&1
709	sleep 0.5
710	verify_subflow_events $client_evts $SUB_CLOSED $client6_token $AF_INET6 "dead:beef:2::2"\
711			      "dead:beef:2::1" "$app6_port" "23" "$server_addr_id" "ns2" "ns1"
712
713	# RM_ADDR6 from server to client machine
714	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
715	   "$server6_token" > /dev/null 2>&1
716	sleep 0.5
717
718	# Attempt to add a listener at 10.0.2.1:<new-port>
719	ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
720	   $new4_port > /dev/null 2>&1 &
721	listener_pid=$!
722
723	# ADD_ADDR from server to client machine using a new port
724	:>"$client_evts"
725	ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
726	   $server_addr_id port $new4_port > /dev/null 2>&1
727	sleep 0.5
728
729	# CREATE_SUBFLOW from client to server machine
730	:>"$client_evts"
731	ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
732	   $new4_port token "$client4_token" > /dev/null 2>&1
733	sleep 0.5
734	verify_subflow_events "$client_evts" "$SUB_ESTABLISHED" "$client4_token" "$AF_INET"\
735			      "10.0.2.2" "10.0.2.1" "$new4_port" "23" "$server_addr_id" "ns2" "ns1"
736
737	# Delete the listener from the server ns, if one was created
738	kill_wait $listener_pid
739
740	sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$client_evts")
741
742	# DESTROY_SUBFLOW from client to server machine
743	:>"$client_evts"
744	ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
745	   $new4_port token "$client4_token" > /dev/null 2>&1
746	sleep 0.5
747	verify_subflow_events "$client_evts" "$SUB_CLOSED" "$client4_token" "$AF_INET" "10.0.2.2"\
748			      "10.0.2.1" "$new4_port" "23" "$server_addr_id" "ns2" "ns1"
749
750	# RM_ADDR from server to client machine
751	ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
752	   "$server4_token" > /dev/null 2>&1
753}
754
755test_prio()
756{
757	local count
758
759	# Send MP_PRIO signal from client to server machine
760	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"
761	sleep 0.5
762
763	# Check TX
764	stdbuf -o0 -e0 printf "MP_PRIO TX                                                 \t"
765	count=$(ip netns exec "$ns2" nstat -as | grep MPTcpExtMPPrioTx | awk '{print $2}')
766	[ -z "$count" ] && count=0
767	if [ $count != 1 ]; then
768		stdbuf -o0 -e0 printf "[FAIL]\n"
769		exit 1
770	else
771		stdbuf -o0 -e0 printf "[OK]\n"
772	fi
773
774	# Check RX
775	stdbuf -o0 -e0 printf "MP_PRIO RX                                                 \t"
776	count=$(ip netns exec "$ns1" nstat -as | grep MPTcpExtMPPrioRx | awk '{print $2}')
777	[ -z "$count" ] && count=0
778	if [ $count != 1 ]; then
779		stdbuf -o0 -e0 printf "[FAIL]\n"
780		exit 1
781	else
782		stdbuf -o0 -e0 printf "[OK]\n"
783	fi
784}
785
786verify_listener_events()
787{
788	local evt=$1
789	local e_type=$2
790	local e_family=$3
791	local e_saddr=$4
792	local e_sport=$5
793	local type
794	local family
795	local saddr
796	local sport
797
798	if [ $e_type = $LISTENER_CREATED ]; then
799		stdbuf -o0 -e0 printf "CREATE_LISTENER %s:%s\t\t\t\t\t"\
800			$e_saddr $e_sport
801	elif [ $e_type = $LISTENER_CLOSED ]; then
802		stdbuf -o0 -e0 printf "CLOSE_LISTENER %s:%s\t\t\t\t\t"\
803			$e_saddr $e_sport
804	fi
805
806	type=$(grep "type:$e_type," $evt |
807	       sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q')
808	family=$(grep "type:$e_type," $evt |
809		 sed --unbuffered -n 's/.*\(family:\)\([[:digit:]]*\).*$/\2/p;q')
810	sport=$(grep "type:$e_type," $evt |
811		sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q')
812	if [ $family ] && [ $family = $AF_INET6 ]; then
813		saddr=$(grep "type:$e_type," $evt |
814			sed --unbuffered -n 's/.*\(saddr6:\)\([0-9a-f:.]*\).*$/\2/p;q')
815	else
816		saddr=$(grep "type:$e_type," $evt |
817			sed --unbuffered -n 's/.*\(saddr4:\)\([0-9.]*\).*$/\2/p;q')
818	fi
819
820	if [ $type ] && [ $type = $e_type ] &&
821	   [ $family ] && [ $family = $e_family ] &&
822	   [ $saddr ] && [ $saddr = $e_saddr ] &&
823	   [ $sport ] && [ $sport = $e_sport ]; then
824		stdbuf -o0 -e0 printf "[OK]\n"
825		return 0
826	fi
827	stdbuf -o0 -e0 printf "[FAIL]\n"
828	exit 1
829}
830
831test_listener()
832{
833	# Capture events on the network namespace running the client
834	:>$client_evts
835
836	# Attempt to add a listener at 10.0.2.2:<subflow-port>
837	ip netns exec $ns2 ./pm_nl_ctl listen 10.0.2.2\
838		$client4_port > /dev/null 2>&1 &
839	local listener_pid=$!
840
841	verify_listener_events $client_evts $LISTENER_CREATED $AF_INET 10.0.2.2 $client4_port
842
843	# ADD_ADDR from client to server machine reusing the subflow port
844	ip netns exec $ns2 ./pm_nl_ctl ann 10.0.2.2 token $client4_token id\
845		$client_addr_id > /dev/null 2>&1
846	sleep 0.5
847
848	# CREATE_SUBFLOW from server to client machine
849	ip netns exec $ns1 ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2\
850		rport $client4_port token $server4_token > /dev/null 2>&1
851	sleep 0.5
852
853	# Delete the listener from the client ns, if one was created
854	kill_wait $listener_pid
855
856	verify_listener_events $client_evts $LISTENER_CLOSED $AF_INET 10.0.2.2 $client4_port
857}
858
859make_connection
860make_connection "v6"
861test_announce
862test_remove
863test_subflows
864test_prio
865test_listener
866
867exit 0
868