1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# redirect test
5#
6#                     .253 +----+
7#                     +----| r1 |
8#                     |    +----+
9# +----+              |       |.1
10# | h1 |--------------+       |   10.1.1.0/30 2001:db8:1::0/126
11# +----+ .1           |       |.2
12#         172.16.1/24 |    +----+                   +----+
13#    2001:db8:16:1/64 +----| r2 |-------------------| h2 |
14#                     .254 +----+ .254           .2 +----+
15#                                    172.16.2/24
16#                                  2001:db8:16:2/64
17#
18# Route from h1 to h2 goes through r1, eth1 - connection between r1 and r2.
19# Route on r1 changed to go to r2 via eth0. This causes a redirect to be sent
20# from r1 to h1 telling h1 to use r2 when talking to h2.
21
22VERBOSE=0
23PAUSE_ON_FAIL=no
24
25H1_N1_IP=172.16.1.1
26R1_N1_IP=172.16.1.253
27R2_N1_IP=172.16.1.254
28
29H1_N1_IP6=2001:db8:16:1::1
30R1_N1_IP6=2001:db8:16:1::253
31R2_N1_IP6=2001:db8:16:1::254
32
33R1_R2_N1_IP=10.1.1.1
34R2_R1_N1_IP=10.1.1.2
35
36R1_R2_N1_IP6=2001:db8:1::1
37R2_R1_N1_IP6=2001:db8:1::2
38
39H2_N2=172.16.2.0/24
40H2_N2_6=2001:db8:16:2::/64
41H2_N2_IP=172.16.2.2
42R2_N2_IP=172.16.2.254
43H2_N2_IP6=2001:db8:16:2::2
44R2_N2_IP6=2001:db8:16:2::254
45
46VRF=red
47VRF_TABLE=1111
48
49################################################################################
50# helpers
51
52log_section()
53{
54	echo
55	echo "###########################################################################"
56	echo "$*"
57	echo "###########################################################################"
58	echo
59}
60
61log_test()
62{
63	local rc=$1
64	local expected=$2
65	local msg="$3"
66	local xfail=$4
67
68	if [ ${rc} -eq ${expected} ]; then
69		printf "TEST: %-60s  [ OK ]\n" "${msg}"
70		nsuccess=$((nsuccess+1))
71	elif [ ${rc} -eq ${xfail} ]; then
72		printf "TEST: %-60s  [XFAIL]\n" "${msg}"
73		nxfail=$((nxfail+1))
74	else
75		ret=1
76		nfail=$((nfail+1))
77		printf "TEST: %-60s  [FAIL]\n" "${msg}"
78		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
79			echo
80			echo "hit enter to continue, 'q' to quit"
81			read a
82			[ "$a" = "q" ] && exit 1
83		fi
84	fi
85}
86
87log_debug()
88{
89	if [ "$VERBOSE" = "1" ]; then
90		echo "$*"
91	fi
92}
93
94run_cmd()
95{
96	local cmd="$*"
97	local out
98	local rc
99
100	if [ "$VERBOSE" = "1" ]; then
101		echo "COMMAND: $cmd"
102	fi
103
104	out=$(eval $cmd 2>&1)
105	rc=$?
106	if [ "$VERBOSE" = "1" -a -n "$out" ]; then
107		echo "$out"
108	fi
109
110	[ "$VERBOSE" = "1" ] && echo
111
112	return $rc
113}
114
115get_linklocal()
116{
117	local ns=$1
118	local dev=$2
119	local addr
120
121	addr=$(ip -netns $ns -6 -br addr show dev ${dev} | \
122	awk '{
123		for (i = 3; i <= NF; ++i) {
124			if ($i ~ /^fe80/)
125				print $i
126		}
127	}'
128	)
129	addr=${addr/\/*}
130
131	[ -z "$addr" ] && return 1
132
133	echo $addr
134
135	return 0
136}
137
138################################################################################
139# setup and teardown
140
141cleanup()
142{
143	local ns
144
145	for ns in h1 h2 r1 r2; do
146		ip netns del $ns 2>/dev/null
147	done
148}
149
150create_vrf()
151{
152	local ns=$1
153
154	ip -netns ${ns} link add ${VRF} type vrf table ${VRF_TABLE}
155	ip -netns ${ns} link set ${VRF} up
156	ip -netns ${ns} route add vrf ${VRF} unreachable default metric 8192
157	ip -netns ${ns} -6 route add vrf ${VRF} unreachable default metric 8192
158
159	ip -netns ${ns} addr add 127.0.0.1/8 dev ${VRF}
160	ip -netns ${ns} -6 addr add ::1 dev ${VRF} nodad
161
162	ip -netns ${ns} ru del pref 0
163	ip -netns ${ns} ru add pref 32765 from all lookup local
164	ip -netns ${ns} -6 ru del pref 0
165	ip -netns ${ns} -6 ru add pref 32765 from all lookup local
166}
167
168setup()
169{
170	local ns
171
172	#
173	# create nodes as namespaces
174	#
175	for ns in h1 h2 r1 r2; do
176		ip netns add $ns
177		ip -netns $ns li set lo up
178
179		case "${ns}" in
180		h[12]) ip netns exec $ns sysctl -q -w net.ipv4.conf.all.accept_redirects=1
181		       ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=0
182		       ip netns exec $ns sysctl -q -w net.ipv6.conf.all.accept_redirects=1
183		       ip netns exec $ns sysctl -q -w net.ipv6.conf.all.keep_addr_on_down=1
184			;;
185		r[12]) ip netns exec $ns sysctl -q -w net.ipv4.ip_forward=1
186		       ip netns exec $ns sysctl -q -w net.ipv4.conf.all.send_redirects=1
187		       ip netns exec $ns sysctl -q -w net.ipv4.conf.default.rp_filter=0
188		       ip netns exec $ns sysctl -q -w net.ipv4.conf.all.rp_filter=0
189
190		       ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=1
191		       ip netns exec $ns sysctl -q -w net.ipv6.route.mtu_expires=10
192		esac
193	done
194
195	#
196	# create interconnects
197	#
198	ip -netns h1 li add eth0 type veth peer name r1h1
199	ip -netns h1 li set r1h1 netns r1 name eth0 up
200
201	ip -netns h1 li add eth1 type veth peer name r2h1
202	ip -netns h1 li set r2h1 netns r2 name eth0 up
203
204	ip -netns h2 li add eth0 type veth peer name r2h2
205	ip -netns h2 li set eth0 up
206	ip -netns h2 li set r2h2 netns r2 name eth2 up
207
208	ip -netns r1 li add eth1 type veth peer name r2r1
209	ip -netns r1 li set eth1 up
210	ip -netns r1 li set r2r1 netns r2 name eth1 up
211
212	#
213	# h1
214	#
215	if [ "${WITH_VRF}" = "yes" ]; then
216		create_vrf "h1"
217		H1_VRF_ARG="vrf ${VRF}"
218		H1_PING_ARG="-I ${VRF}"
219	else
220		H1_VRF_ARG=
221		H1_PING_ARG=
222	fi
223	ip -netns h1 li add br0 type bridge
224	if [ "${WITH_VRF}" = "yes" ]; then
225		ip -netns h1 li set br0 vrf ${VRF} up
226	else
227		ip -netns h1 li set br0 up
228	fi
229	ip -netns h1 addr add dev br0 ${H1_N1_IP}/24
230	ip -netns h1 -6 addr add dev br0 ${H1_N1_IP6}/64 nodad
231	ip -netns h1 li set eth0 master br0 up
232	ip -netns h1 li set eth1 master br0 up
233
234	#
235	# h2
236	#
237	ip -netns h2 addr add dev eth0 ${H2_N2_IP}/24
238	ip -netns h2 ro add default via ${R2_N2_IP} dev eth0
239	ip -netns h2 -6 addr add dev eth0 ${H2_N2_IP6}/64 nodad
240	ip -netns h2 -6 ro add default via ${R2_N2_IP6} dev eth0
241
242	#
243	# r1
244	#
245	ip -netns r1 addr add dev eth0 ${R1_N1_IP}/24
246	ip -netns r1 -6 addr add dev eth0 ${R1_N1_IP6}/64 nodad
247	ip -netns r1 addr add dev eth1 ${R1_R2_N1_IP}/30
248	ip -netns r1 -6 addr add dev eth1 ${R1_R2_N1_IP6}/126 nodad
249
250	#
251	# r2
252	#
253	ip -netns r2 addr add dev eth0 ${R2_N1_IP}/24
254	ip -netns r2 -6 addr add dev eth0 ${R2_N1_IP6}/64 nodad
255	ip -netns r2 addr add dev eth1 ${R2_R1_N1_IP}/30
256	ip -netns r2 -6 addr add dev eth1 ${R2_R1_N1_IP6}/126 nodad
257	ip -netns r2 addr add dev eth2 ${R2_N2_IP}/24
258	ip -netns r2 -6 addr add dev eth2 ${R2_N2_IP6}/64 nodad
259
260	sleep 2
261
262	R1_LLADDR=$(get_linklocal r1 eth0)
263	if [ $? -ne 0 ]; then
264		echo "Error: Failed to get link-local address of r1's eth0"
265		exit 1
266	fi
267	log_debug "initial gateway is R1's lladdr = ${R1_LLADDR}"
268
269	R2_LLADDR=$(get_linklocal r2 eth0)
270	if [ $? -ne 0 ]; then
271		echo "Error: Failed to get link-local address of r2's eth0"
272		exit 1
273	fi
274	log_debug "initial gateway is R2's lladdr = ${R2_LLADDR}"
275}
276
277change_h2_mtu()
278{
279	local mtu=$1
280
281	run_cmd ip -netns h2 li set eth0 mtu ${mtu}
282	run_cmd ip -netns r2 li set eth2 mtu ${mtu}
283}
284
285check_exception()
286{
287	local mtu="$1"
288	local with_redirect="$2"
289	local desc="$3"
290
291	# From 172.16.1.101: icmp_seq=1 Redirect Host(New nexthop: 172.16.1.102)
292	if [ "$VERBOSE" = "1" ]; then
293		echo "Commands to check for exception:"
294		run_cmd ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP}
295		run_cmd ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6}
296	fi
297
298	if [ -n "${mtu}" ]; then
299		mtu=" mtu ${mtu}"
300	fi
301	if [ "$with_redirect" = "yes" ]; then
302		ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
303		grep -q "cache <redirected> expires [0-9]*sec${mtu}"
304	elif [ -n "${mtu}" ]; then
305		ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
306		grep -q "cache expires [0-9]*sec${mtu}"
307	else
308		# want to verify that neither mtu nor redirected appears in
309		# the route get output. The -v will wipe out the cache line
310		# if either are set so the last grep -q will not find a match
311		ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
312		grep -E -v 'mtu|redirected' | grep -q "cache"
313	fi
314	log_test $? 0 "IPv4: ${desc}"
315
316	if [ "$with_redirect" = "yes" ]; then
317		ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
318		grep -q "${H2_N2_IP6} from :: via ${R2_LLADDR} dev br0.*${mtu}"
319	elif [ -n "${mtu}" ]; then
320		ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
321		grep -q "${mtu}"
322	else
323		# IPv6 is a bit harder. First strip out the match if it
324		# contains an mtu exception and then look for the first
325		# gateway - R1's lladdr
326		ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
327		grep -v "mtu" | grep -q "${R1_LLADDR}"
328	fi
329	log_test $? 0 "IPv6: ${desc}" 1
330}
331
332run_ping()
333{
334	local sz=$1
335
336	run_cmd ip netns exec h1 ping -q -M want -i 0.5 -c 10 -w 2 -s ${sz} ${H1_PING_ARG} ${H2_N2_IP}
337	run_cmd ip netns exec h1 ${ping6} -q -M want -i 0.5 -c 10 -w 2 -s ${sz} ${H1_PING_ARG} ${H2_N2_IP6}
338}
339
340replace_route_new()
341{
342	# r1 to h2 via r2 and eth0
343	run_cmd ip -netns r1 nexthop replace id 1 via ${R2_N1_IP} dev eth0
344	run_cmd ip -netns r1 nexthop replace id 2 via ${R2_LLADDR} dev eth0
345}
346
347reset_route_new()
348{
349	run_cmd ip -netns r1 nexthop flush
350	run_cmd ip -netns h1 nexthop flush
351
352	initial_route_new
353}
354
355initial_route_new()
356{
357	# r1 to h2 via r2 and eth1
358	run_cmd ip -netns r1 nexthop add id 1 via ${R2_R1_N1_IP} dev eth1
359	run_cmd ip -netns r1 ro add ${H2_N2} nhid 1
360
361	run_cmd ip -netns r1 nexthop add id 2 via ${R2_R1_N1_IP6} dev eth1
362	run_cmd ip -netns r1 -6 ro add ${H2_N2_6} nhid 2
363
364	# h1 to h2 via r1
365	run_cmd ip -netns h1 nexthop add id 1 via ${R1_N1_IP} dev br0
366	run_cmd ip -netns h1 ro add ${H1_VRF_ARG} ${H2_N2} nhid 1
367
368	run_cmd ip -netns h1 nexthop add id 2 via ${R1_LLADDR} dev br0
369	run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} nhid 2
370}
371
372replace_route_legacy()
373{
374	# r1 to h2 via r2 and eth0
375	run_cmd ip -netns r1    ro replace ${H2_N2}   via ${R2_N1_IP}  dev eth0
376	run_cmd ip -netns r1 -6 ro replace ${H2_N2_6} via ${R2_LLADDR} dev eth0
377}
378
379reset_route_legacy()
380{
381	run_cmd ip -netns r1    ro del ${H2_N2}
382	run_cmd ip -netns r1 -6 ro del ${H2_N2_6}
383
384	run_cmd ip -netns h1    ro del ${H1_VRF_ARG} ${H2_N2}
385	run_cmd ip -netns h1 -6 ro del ${H1_VRF_ARG} ${H2_N2_6}
386
387	initial_route_legacy
388}
389
390initial_route_legacy()
391{
392	# r1 to h2 via r2 and eth1
393	run_cmd ip -netns r1    ro add ${H2_N2}   via ${R2_R1_N1_IP}  dev eth1
394	run_cmd ip -netns r1 -6 ro add ${H2_N2_6} via ${R2_R1_N1_IP6} dev eth1
395
396	# h1 to h2 via r1
397	# - IPv6 redirect only works if gateway is the LLA
398	run_cmd ip -netns h1    ro add ${H1_VRF_ARG} ${H2_N2} via ${R1_N1_IP} dev br0
399	run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} via ${R1_LLADDR} dev br0
400}
401
402check_connectivity()
403{
404	local rc
405
406	run_cmd ip netns exec h1 ping -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP}
407	rc=$?
408	run_cmd ip netns exec h1 ${ping6} -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP6}
409	[ $? -ne 0 ] && rc=$?
410
411	return $rc
412}
413
414do_test()
415{
416	local ttype="$1"
417
418	eval initial_route_${ttype}
419
420	# verify connectivity
421	check_connectivity
422	if [ $? -ne 0 ]; then
423		echo "Error: Basic connectivity is broken"
424		ret=1
425		return
426	fi
427
428	# redirect exception followed by mtu
429	eval replace_route_${ttype}
430	run_ping 64
431	check_exception "" "yes" "redirect exception"
432
433	check_connectivity
434	if [ $? -ne 0 ]; then
435		echo "Error: Basic connectivity is broken after redirect"
436		ret=1
437		return
438	fi
439
440	change_h2_mtu 1300
441	run_ping 1350
442	check_exception "1300" "yes" "redirect exception plus mtu"
443
444	# remove exceptions and restore routing
445	change_h2_mtu 1500
446	eval reset_route_${ttype}
447
448	check_connectivity
449	if [ $? -ne 0 ]; then
450		echo "Error: Basic connectivity is broken after reset"
451		ret=1
452		return
453	fi
454	check_exception "" "no" "routing reset"
455
456	# MTU exception followed by redirect
457	change_h2_mtu 1300
458	run_ping 1350
459	check_exception "1300" "no" "mtu exception"
460
461	eval replace_route_${ttype}
462	run_ping 64
463	check_exception "1300" "yes" "mtu exception plus redirect"
464
465	check_connectivity
466	if [ $? -ne 0 ]; then
467		echo "Error: Basic connectivity is broken after redirect"
468		ret=1
469		return
470	fi
471}
472
473################################################################################
474# usage
475
476usage()
477{
478        cat <<EOF
479usage: ${0##*/} OPTS
480
481	-p          Pause on fail
482	-v          verbose mode (show commands and output)
483EOF
484}
485
486################################################################################
487# main
488
489# Some systems don't have a ping6 binary anymore
490which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
491
492ret=0
493nsuccess=0
494nfail=0
495nxfail=0
496
497while getopts :pv o
498do
499	case $o in
500                p) PAUSE_ON_FAIL=yes;;
501                v) VERBOSE=$(($VERBOSE + 1));;
502                *) usage; exit 1;;
503	esac
504done
505
506trap cleanup EXIT
507
508cleanup
509WITH_VRF=no
510setup
511
512log_section "Legacy routing"
513do_test "legacy"
514
515cleanup
516log_section "Legacy routing with VRF"
517WITH_VRF=yes
518setup
519do_test "legacy"
520
521cleanup
522log_section "Routing with nexthop objects"
523ip nexthop ls >/dev/null 2>&1
524if [ $? -eq 0 ]; then
525	WITH_VRF=no
526	setup
527	do_test "new"
528
529	cleanup
530	log_section "Routing with nexthop objects and VRF"
531	WITH_VRF=yes
532	setup
533	do_test "new"
534else
535	echo "Nexthop objects not supported; skipping tests"
536fi
537
538printf "\nTests passed: %3d\n" ${nsuccess}
539printf "Tests failed: %3d\n"   ${nfail}
540printf "Tests xfailed: %3d\n"  ${nxfail}
541
542exit $ret
543