xref: /linux/tools/testing/selftests/net/fib_tests.sh (revision dad4d4b92a9b9f0edb8c66deda049da1b62f6089)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# This test is for checking IPv4 and IPv6 FIB behavior in response to
5# different events.
6source lib.sh
7ret=0
8
9# all tests in this script. Can be overridden with -t option
10TESTS="unregister down carrier nexthop suppress ipv6_notify ipv4_notify \
11       ipv6_rt ipv4_rt ipv6_addr_metric ipv4_addr_metric ipv6_route_metrics \
12       ipv4_route_metrics ipv4_route_v6_gw rp_filter ipv4_del_addr \
13       ipv6_del_addr ipv4_mangle ipv6_mangle ipv4_bcast_neigh fib6_gc_test \
14       ipv4_mpath_list ipv6_mpath_list ipv4_mpath_balance ipv6_mpath_balance \
15       ipv4_mpath_balance_preferred fib6_ra_to_static fib6_temp_addr_renewal"
16
17VERBOSE=0
18PAUSE_ON_FAIL=no
19PAUSE=no
20
21which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
22
23log_test()
24{
25	local rc=$1
26	local expected=$2
27	local msg="$3"
28
29	if [ ${rc} -eq ${expected} ]; then
30		printf "    TEST: %-60s  [ OK ]\n" "${msg}"
31		nsuccess=$((nsuccess+1))
32	else
33		ret=1
34		nfail=$((nfail+1))
35		printf "    TEST: %-60s  [FAIL]\n" "${msg}"
36		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
37		echo
38			echo "hit enter to continue, 'q' to quit"
39			read a
40			[ "$a" = "q" ] && exit 1
41		fi
42	fi
43
44	if [ "${PAUSE}" = "yes" ]; then
45		echo
46		echo "hit enter to continue, 'q' to quit"
47		read a
48		[ "$a" = "q" ] && exit 1
49	fi
50}
51
52setup()
53{
54	set -e
55	setup_ns ns1
56	IP="$(which ip) -netns $ns1"
57	NS_EXEC="$(which ip) netns exec $ns1"
58	ip netns exec $ns1 sysctl -qw net.ipv4.ip_forward=1
59	ip netns exec $ns1 sysctl -qw net.ipv6.conf.all.forwarding=1
60
61	$IP link add dummy0 type dummy
62	$IP link set dev dummy0 up
63	$IP address add 198.51.100.1/24 dev dummy0
64	$IP -6 address add 2001:db8:1::1/64 dev dummy0
65	set +e
66
67}
68
69cleanup()
70{
71	$IP link del dev dummy0 &> /dev/null
72	cleanup_ns $ns1 $ns2
73}
74
75get_linklocal()
76{
77	local dev=$1
78	local addr
79
80	addr=$($IP -6 -br addr show dev ${dev} | \
81	awk '{
82		for (i = 3; i <= NF; ++i) {
83			if ($i ~ /^fe80/)
84				print $i
85		}
86	}'
87	)
88	addr=${addr/\/*}
89
90	[ -z "$addr" ] && return 1
91
92	echo $addr
93
94	return 0
95}
96
97fib_unreg_unicast_test()
98{
99	echo
100	echo "Single path route test"
101
102	setup
103
104	echo "    Start point"
105	$IP route get fibmatch 198.51.100.2 &> /dev/null
106	log_test $? 0 "IPv4 fibmatch"
107	$IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
108	log_test $? 0 "IPv6 fibmatch"
109
110	set -e
111	$IP link del dev dummy0
112	set +e
113
114	echo "    Nexthop device deleted"
115	$IP route get fibmatch 198.51.100.2 &> /dev/null
116	log_test $? 2 "IPv4 fibmatch - no route"
117	$IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
118	log_test $? 2 "IPv6 fibmatch - no route"
119
120	cleanup
121}
122
123fib_unreg_multipath_test()
124{
125
126	echo
127	echo "Multipath route test"
128
129	setup
130
131	set -e
132	$IP link add dummy1 type dummy
133	$IP link set dev dummy1 up
134	$IP address add 192.0.2.1/24 dev dummy1
135	$IP -6 address add 2001:db8:2::1/64 dev dummy1
136
137	$IP route add 203.0.113.0/24 \
138		nexthop via 198.51.100.2 dev dummy0 \
139		nexthop via 192.0.2.2 dev dummy1
140	$IP -6 route add 2001:db8:3::/64 \
141		nexthop via 2001:db8:1::2 dev dummy0 \
142		nexthop via 2001:db8:2::2 dev dummy1
143	set +e
144
145	echo "    Start point"
146	$IP route get fibmatch 203.0.113.1 &> /dev/null
147	log_test $? 0 "IPv4 fibmatch"
148	$IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null
149	log_test $? 0 "IPv6 fibmatch"
150
151	set -e
152	$IP link del dev dummy0
153	set +e
154
155	echo "    One nexthop device deleted"
156	$IP route get fibmatch 203.0.113.1 &> /dev/null
157	log_test $? 2 "IPv4 - multipath route removed on delete"
158
159	$IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null
160	# In IPv6 we do not flush the entire multipath route.
161	log_test $? 0 "IPv6 - multipath down to single path"
162
163	set -e
164	$IP link del dev dummy1
165	set +e
166
167	echo "    Second nexthop device deleted"
168	$IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null
169	log_test $? 2 "IPv6 - no route"
170
171	cleanup
172}
173
174fib_unreg_test()
175{
176	fib_unreg_unicast_test
177	fib_unreg_multipath_test
178}
179
180fib_down_unicast_test()
181{
182	echo
183	echo "Single path, admin down"
184
185	setup
186
187	echo "    Start point"
188	$IP route get fibmatch 198.51.100.2 &> /dev/null
189	log_test $? 0 "IPv4 fibmatch"
190	$IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
191	log_test $? 0 "IPv6 fibmatch"
192
193	set -e
194	$IP link set dev dummy0 down
195	set +e
196
197	echo "    Route deleted on down"
198	$IP route get fibmatch 198.51.100.2 &> /dev/null
199	log_test $? 2 "IPv4 fibmatch"
200	$IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
201	log_test $? 2 "IPv6 fibmatch"
202
203	cleanup
204}
205
206fib_down_multipath_test_do()
207{
208	local down_dev=$1
209	local up_dev=$2
210
211	$IP route get fibmatch 203.0.113.1 \
212		oif $down_dev &> /dev/null
213	log_test $? 2 "IPv4 fibmatch on down device"
214	$IP -6 route get fibmatch 2001:db8:3::1 \
215		oif $down_dev &> /dev/null
216	log_test $? 2 "IPv6 fibmatch on down device"
217
218	$IP route get fibmatch 203.0.113.1 \
219		oif $up_dev &> /dev/null
220	log_test $? 0 "IPv4 fibmatch on up device"
221	$IP -6 route get fibmatch 2001:db8:3::1 \
222		oif $up_dev &> /dev/null
223	log_test $? 0 "IPv6 fibmatch on up device"
224
225	$IP route get fibmatch 203.0.113.1 | \
226		grep $down_dev | grep -q "dead linkdown"
227	log_test $? 0 "IPv4 flags on down device"
228	$IP -6 route get fibmatch 2001:db8:3::1 | \
229		grep $down_dev | grep -q "dead linkdown"
230	log_test $? 0 "IPv6 flags on down device"
231
232	$IP route get fibmatch 203.0.113.1 | \
233		grep $up_dev | grep -q "dead linkdown"
234	log_test $? 1 "IPv4 flags on up device"
235	$IP -6 route get fibmatch 2001:db8:3::1 | \
236		grep $up_dev | grep -q "dead linkdown"
237	log_test $? 1 "IPv6 flags on up device"
238}
239
240fib_down_multipath_test()
241{
242	echo
243	echo "Admin down multipath"
244
245	setup
246
247	set -e
248	$IP link add dummy1 type dummy
249	$IP link set dev dummy1 up
250
251	$IP address add 192.0.2.1/24 dev dummy1
252	$IP -6 address add 2001:db8:2::1/64 dev dummy1
253
254	$IP route add 203.0.113.0/24 \
255		nexthop via 198.51.100.2 dev dummy0 \
256		nexthop via 192.0.2.2 dev dummy1
257	$IP -6 route add 2001:db8:3::/64 \
258		nexthop via 2001:db8:1::2 dev dummy0 \
259		nexthop via 2001:db8:2::2 dev dummy1
260	set +e
261
262	echo "    Verify start point"
263	$IP route get fibmatch 203.0.113.1 &> /dev/null
264	log_test $? 0 "IPv4 fibmatch"
265
266	$IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null
267	log_test $? 0 "IPv6 fibmatch"
268
269	set -e
270	$IP link set dev dummy0 down
271	set +e
272
273	echo "    One device down, one up"
274	fib_down_multipath_test_do "dummy0" "dummy1"
275
276	set -e
277	$IP link set dev dummy0 up
278	$IP link set dev dummy1 down
279	set +e
280
281	echo "    Other device down and up"
282	fib_down_multipath_test_do "dummy1" "dummy0"
283
284	set -e
285	$IP link set dev dummy0 down
286	set +e
287
288	echo "    Both devices down"
289	$IP route get fibmatch 203.0.113.1 &> /dev/null
290	log_test $? 2 "IPv4 fibmatch"
291	$IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null
292	log_test $? 2 "IPv6 fibmatch"
293
294	$IP link del dev dummy1
295	cleanup
296}
297
298fib_down_test()
299{
300	fib_down_unicast_test
301	fib_down_multipath_test
302}
303
304# Local routes should not be affected when carrier changes.
305fib_carrier_local_test()
306{
307	echo
308	echo "Local carrier tests - single path"
309
310	setup
311
312	set -e
313	$IP link set dev dummy0 carrier on
314	set +e
315
316	echo "    Start point"
317	$IP route get fibmatch 198.51.100.1 &> /dev/null
318	log_test $? 0 "IPv4 fibmatch"
319	$IP -6 route get fibmatch 2001:db8:1::1 &> /dev/null
320	log_test $? 0 "IPv6 fibmatch"
321
322	$IP route get fibmatch 198.51.100.1 | \
323		grep -q "linkdown"
324	log_test $? 1 "IPv4 - no linkdown flag"
325	$IP -6 route get fibmatch 2001:db8:1::1 | \
326		grep -q "linkdown"
327	log_test $? 1 "IPv6 - no linkdown flag"
328
329	set -e
330	$IP link set dev dummy0 carrier off
331	sleep 1
332	set +e
333
334	echo "    Carrier off on nexthop"
335	$IP route get fibmatch 198.51.100.1 &> /dev/null
336	log_test $? 0 "IPv4 fibmatch"
337	$IP -6 route get fibmatch 2001:db8:1::1 &> /dev/null
338	log_test $? 0 "IPv6 fibmatch"
339
340	$IP route get fibmatch 198.51.100.1 | \
341		grep -q "linkdown"
342	log_test $? 1 "IPv4 - linkdown flag set"
343	$IP -6 route get fibmatch 2001:db8:1::1 | \
344		grep -q "linkdown"
345	log_test $? 1 "IPv6 - linkdown flag set"
346
347	set -e
348	$IP address add 192.0.2.1/24 dev dummy0
349	$IP -6 address add 2001:db8:2::1/64 dev dummy0
350	set +e
351
352	echo "    Route to local address with carrier down"
353	$IP route get fibmatch 192.0.2.1 &> /dev/null
354	log_test $? 0 "IPv4 fibmatch"
355	$IP -6 route get fibmatch 2001:db8:2::1 &> /dev/null
356	log_test $? 0 "IPv6 fibmatch"
357
358	$IP route get fibmatch 192.0.2.1 | \
359		grep -q "linkdown"
360	log_test $? 1 "IPv4 linkdown flag set"
361	$IP -6 route get fibmatch 2001:db8:2::1 | \
362		grep -q "linkdown"
363	log_test $? 1 "IPv6 linkdown flag set"
364
365	cleanup
366}
367
368fib_carrier_unicast_test()
369{
370	ret=0
371
372	echo
373	echo "Single path route carrier test"
374
375	setup
376
377	set -e
378	$IP link set dev dummy0 carrier on
379	set +e
380
381	echo "    Start point"
382	$IP route get fibmatch 198.51.100.2 &> /dev/null
383	log_test $? 0 "IPv4 fibmatch"
384	$IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
385	log_test $? 0 "IPv6 fibmatch"
386
387	$IP route get fibmatch 198.51.100.2 | \
388		grep -q "linkdown"
389	log_test $? 1 "IPv4 no linkdown flag"
390	$IP -6 route get fibmatch 2001:db8:1::2 | \
391		grep -q "linkdown"
392	log_test $? 1 "IPv6 no linkdown flag"
393
394	set -e
395	$IP link set dev dummy0 carrier off
396	sleep 1
397	set +e
398
399	echo "    Carrier down"
400	$IP route get fibmatch 198.51.100.2 &> /dev/null
401	log_test $? 0 "IPv4 fibmatch"
402	$IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null
403	log_test $? 0 "IPv6 fibmatch"
404
405	$IP route get fibmatch 198.51.100.2 | \
406		grep -q "linkdown"
407	log_test $? 0 "IPv4 linkdown flag set"
408	$IP -6 route get fibmatch 2001:db8:1::2 | \
409		grep -q "linkdown"
410	log_test $? 0 "IPv6 linkdown flag set"
411
412	set -e
413	$IP address add 192.0.2.1/24 dev dummy0
414	$IP -6 address add 2001:db8:2::1/64 dev dummy0
415	set +e
416
417	echo "    Second address added with carrier down"
418	$IP route get fibmatch 192.0.2.2 &> /dev/null
419	log_test $? 0 "IPv4 fibmatch"
420	$IP -6 route get fibmatch 2001:db8:2::2 &> /dev/null
421	log_test $? 0 "IPv6 fibmatch"
422
423	$IP route get fibmatch 192.0.2.2 | \
424		grep -q "linkdown"
425	log_test $? 0 "IPv4 linkdown flag set"
426	$IP -6 route get fibmatch 2001:db8:2::2 | \
427		grep -q "linkdown"
428	log_test $? 0 "IPv6 linkdown flag set"
429
430	cleanup
431}
432
433fib_carrier_test()
434{
435	fib_carrier_local_test
436	fib_carrier_unicast_test
437}
438
439fib_rp_filter_test()
440{
441	echo
442	echo "IPv4 rp_filter tests"
443
444	setup
445
446	set -e
447	setup_ns ns2
448
449	$IP link add name veth1 type veth peer name veth2
450	$IP link set dev veth2 netns $ns2
451	$IP address add 192.0.2.1/24 dev veth1
452	ip -netns $ns2 address add 192.0.2.1/24 dev veth2
453	$IP link set dev veth1 up
454	ip -netns $ns2 link set dev veth2 up
455
456	$IP link set dev lo address 52:54:00:6a:c7:5e
457	$IP link set dev veth1 address 52:54:00:6a:c7:5e
458	ip -netns $ns2 link set dev lo address 52:54:00:6a:c7:5e
459	ip -netns $ns2 link set dev veth2 address 52:54:00:6a:c7:5e
460
461	# 1. (ns2) redirect lo's egress to veth2's egress
462	ip netns exec $ns2 tc qdisc add dev lo parent root handle 1: fq_codel
463	ip netns exec $ns2 tc filter add dev lo parent 1: protocol arp basic \
464		action mirred egress redirect dev veth2
465	ip netns exec $ns2 tc filter add dev lo parent 1: protocol ip basic \
466		action mirred egress redirect dev veth2
467
468	# 2. (ns1) redirect veth1's ingress to lo's ingress
469	$NS_EXEC tc qdisc add dev veth1 ingress
470	$NS_EXEC tc filter add dev veth1 ingress protocol arp basic \
471		action mirred ingress redirect dev lo
472	$NS_EXEC tc filter add dev veth1 ingress protocol ip basic \
473		action mirred ingress redirect dev lo
474
475	# 3. (ns1) redirect lo's egress to veth1's egress
476	$NS_EXEC tc qdisc add dev lo parent root handle 1: fq_codel
477	$NS_EXEC tc filter add dev lo parent 1: protocol arp basic \
478		action mirred egress redirect dev veth1
479	$NS_EXEC tc filter add dev lo parent 1: protocol ip basic \
480		action mirred egress redirect dev veth1
481
482	# 4. (ns2) redirect veth2's ingress to lo's ingress
483	ip netns exec $ns2 tc qdisc add dev veth2 ingress
484	ip netns exec $ns2 tc filter add dev veth2 ingress protocol arp basic \
485		action mirred ingress redirect dev lo
486	ip netns exec $ns2 tc filter add dev veth2 ingress protocol ip basic \
487		action mirred ingress redirect dev lo
488
489	$NS_EXEC sysctl -qw net.ipv4.conf.all.rp_filter=1
490	$NS_EXEC sysctl -qw net.ipv4.conf.all.accept_local=1
491	$NS_EXEC sysctl -qw net.ipv4.conf.all.route_localnet=1
492	ip netns exec $ns2 sysctl -qw net.ipv4.conf.all.rp_filter=1
493	ip netns exec $ns2 sysctl -qw net.ipv4.conf.all.accept_local=1
494	ip netns exec $ns2 sysctl -qw net.ipv4.conf.all.route_localnet=1
495	set +e
496
497	run_cmd "ip netns exec $ns2 ping -w1 -c1 192.0.2.1"
498	log_test $? 0 "rp_filter passes local packets"
499
500	run_cmd "ip netns exec $ns2 ping -w1 -c1 127.0.0.1"
501	log_test $? 0 "rp_filter passes loopback packets"
502
503	cleanup
504}
505
506################################################################################
507# Tests on nexthop spec
508
509# run 'ip route add' with given spec
510add_rt()
511{
512	local desc="$1"
513	local erc=$2
514	local vrf=$3
515	local pfx=$4
516	local gw=$5
517	local dev=$6
518	local cmd out rc
519
520	[ "$vrf" = "-" ] && vrf="default"
521	[ -n "$gw" ] && gw="via $gw"
522	[ -n "$dev" ] && dev="dev $dev"
523
524	cmd="$IP route add vrf $vrf $pfx $gw $dev"
525	if [ "$VERBOSE" = "1" ]; then
526		printf "\n    COMMAND: $cmd\n"
527	fi
528
529	out=$(eval $cmd 2>&1)
530	rc=$?
531	if [ "$VERBOSE" = "1" -a -n "$out" ]; then
532		echo "    $out"
533	fi
534	log_test $rc $erc "$desc"
535}
536
537fib4_nexthop()
538{
539	echo
540	echo "IPv4 nexthop tests"
541
542	echo "<<< write me >>>"
543}
544
545fib6_nexthop()
546{
547	local lldummy=$(get_linklocal dummy0)
548	local llv1=$(get_linklocal veth1)
549
550	if [ -z "$lldummy" ]; then
551		echo "Failed to get linklocal address for dummy0"
552		return 1
553	fi
554	if [ -z "$llv1" ]; then
555		echo "Failed to get linklocal address for veth1"
556		return 1
557	fi
558
559	echo
560	echo "IPv6 nexthop tests"
561
562	add_rt "Directly connected nexthop, unicast address" 0 \
563		- 2001:db8:101::/64 2001:db8:1::2
564	add_rt "Directly connected nexthop, unicast address with device" 0 \
565		- 2001:db8:102::/64 2001:db8:1::2 "dummy0"
566	add_rt "Gateway is linklocal address" 0 \
567		- 2001:db8:103::1/64 $llv1 "veth0"
568
569	# fails because LL address requires a device
570	add_rt "Gateway is linklocal address, no device" 2 \
571		- 2001:db8:104::1/64 $llv1
572
573	# local address can not be a gateway
574	add_rt "Gateway can not be local unicast address" 2 \
575		- 2001:db8:105::/64 2001:db8:1::1
576	add_rt "Gateway can not be local unicast address, with device" 2 \
577		- 2001:db8:106::/64 2001:db8:1::1 "dummy0"
578	add_rt "Gateway can not be a local linklocal address" 2 \
579		- 2001:db8:107::1/64 $lldummy "dummy0"
580
581	# VRF tests
582	add_rt "Gateway can be local address in a VRF" 0 \
583		- 2001:db8:108::/64 2001:db8:51::2
584	add_rt "Gateway can be local address in a VRF, with device" 0 \
585		- 2001:db8:109::/64 2001:db8:51::2 "veth0"
586	add_rt "Gateway can be local linklocal address in a VRF" 0 \
587		- 2001:db8:110::1/64 $llv1 "veth0"
588
589	add_rt "Redirect to VRF lookup" 0 \
590		- 2001:db8:111::/64 "" "red"
591
592	add_rt "VRF route, gateway can be local address in default VRF" 0 \
593		red 2001:db8:112::/64 2001:db8:51::1
594
595	# local address in same VRF fails
596	add_rt "VRF route, gateway can not be a local address" 2 \
597		red 2001:db8:113::1/64 2001:db8:2::1
598	add_rt "VRF route, gateway can not be a local addr with device" 2 \
599		red 2001:db8:114::1/64 2001:db8:2::1 "dummy1"
600}
601
602# Default VRF:
603#   dummy0 - 198.51.100.1/24 2001:db8:1::1/64
604#   veth0  - 192.0.2.1/24    2001:db8:51::1/64
605#
606# VRF red:
607#   dummy1 - 192.168.2.1/24 2001:db8:2::1/64
608#   veth1  - 192.0.2.2/24   2001:db8:51::2/64
609#
610#  [ dummy0   veth0 ]--[ veth1   dummy1 ]
611
612fib_nexthop_test()
613{
614	setup
615
616	set -e
617
618	$IP -4 rule add pref 32765 table local
619	$IP -4 rule del pref 0
620	$IP -6 rule add pref 32765 table local
621	$IP -6 rule del pref 0
622
623	$IP link add red type vrf table 1
624	$IP link set red up
625	$IP -4 route add vrf red unreachable default metric 4278198272
626	$IP -6 route add vrf red unreachable default metric 4278198272
627
628	$IP link add veth0 type veth peer name veth1
629	$IP link set dev veth0 up
630	$IP address add 192.0.2.1/24 dev veth0
631	$IP -6 address add 2001:db8:51::1/64 dev veth0
632
633	$IP link set dev veth1 vrf red up
634	$IP address add 192.0.2.2/24 dev veth1
635	$IP -6 address add 2001:db8:51::2/64 dev veth1
636
637	$IP link add dummy1 type dummy
638	$IP link set dev dummy1 vrf red up
639	$IP address add 192.168.2.1/24 dev dummy1
640	$IP -6 address add 2001:db8:2::1/64 dev dummy1
641	set +e
642
643	sleep 1
644	fib4_nexthop
645	fib6_nexthop
646
647	(
648	$IP link del dev dummy1
649	$IP link del veth0
650	$IP link del red
651	) 2>/dev/null
652	cleanup
653}
654
655fib6_notify_test()
656{
657	setup
658
659	echo
660	echo "Fib6 info length calculation in route notify test"
661	set -e
662
663	for i in 10 20 30 40 50 60 70;
664	do
665		$IP link add dummy_$i type dummy
666		$IP link set dev dummy_$i up
667		$IP -6 address add 2001:$i::1/64 dev dummy_$i
668	done
669
670	$NS_EXEC ip monitor route &> errors.txt &
671	sleep 2
672
673	$IP -6 route add 2001::/64 \
674                nexthop via 2001:10::2 dev dummy_10 \
675                nexthop encap ip6 dst 2002::20 via 2001:20::2 dev dummy_20 \
676                nexthop encap ip6 dst 2002::30 via 2001:30::2 dev dummy_30 \
677                nexthop encap ip6 dst 2002::40 via 2001:40::2 dev dummy_40 \
678                nexthop encap ip6 dst 2002::50 via 2001:50::2 dev dummy_50 \
679                nexthop encap ip6 dst 2002::60 via 2001:60::2 dev dummy_60 \
680                nexthop encap ip6 dst 2002::70 via 2001:70::2 dev dummy_70
681
682	set +e
683
684	err=`cat errors.txt |grep "Message too long"`
685	if [ -z "$err" ];then
686		ret=0
687	else
688		ret=1
689	fi
690
691	log_test $ret 0 "ipv6 route add notify"
692
693	kill_process %%
694
695	#rm errors.txt
696
697	cleanup &> /dev/null
698}
699
700
701fib_notify_test()
702{
703	setup
704
705	echo
706	echo "Fib4 info length calculation in route notify test"
707
708	set -e
709
710	for i in 10 20 30 40 50 60 70;
711	do
712		$IP link add dummy_$i type dummy
713		$IP link set dev dummy_$i up
714		$IP address add 20.20.$i.2/24 dev dummy_$i
715	done
716
717	$NS_EXEC ip monitor route &> errors.txt &
718	sleep 2
719
720        $IP route add 10.0.0.0/24 \
721                nexthop via 20.20.10.1 dev dummy_10 \
722                nexthop encap ip dst 192.168.10.20 via 20.20.20.1 dev dummy_20 \
723                nexthop encap ip dst 192.168.10.30 via 20.20.30.1 dev dummy_30 \
724                nexthop encap ip dst 192.168.10.40 via 20.20.40.1 dev dummy_40 \
725                nexthop encap ip dst 192.168.10.50 via 20.20.50.1 dev dummy_50 \
726                nexthop encap ip dst 192.168.10.60 via 20.20.60.1 dev dummy_60 \
727                nexthop encap ip dst 192.168.10.70 via 20.20.70.1 dev dummy_70
728
729	set +e
730
731	err=`cat errors.txt |grep "Message too long"`
732	if [ -z "$err" ];then
733		ret=0
734	else
735		ret=1
736	fi
737
738	log_test $ret 0 "ipv4 route add notify"
739
740	kill_process %%
741
742	rm  errors.txt
743
744	cleanup &> /dev/null
745}
746
747# Create a new dummy_10 to remove all associated routes.
748reset_dummy_10()
749{
750	$IP link del dev dummy_10
751
752	$IP link add dummy_10 type dummy
753	$IP link set dev dummy_10 up
754	$IP -6 address add 2001:10::1/64 dev dummy_10
755}
756
757check_rt_num()
758{
759    local expected=$1
760    local num=$2
761
762    if [ $num -ne $expected ]; then
763	echo "FAIL: Expected $expected routes, got $num"
764	ret=1
765    else
766	ret=0
767    fi
768}
769
770check_rt_num_clean()
771{
772    local expected=$1
773    local num=$2
774
775    if [ $num -ne $expected ]; then
776	log_test 1 0 "expected $expected routes, got $num"
777	set +e
778	cleanup &> /dev/null
779	return 1
780    fi
781    return 0
782}
783
784fib6_gc_test()
785{
786	setup
787
788	echo
789	echo "Fib6 garbage collection test"
790	set -e
791
792	EXPIRE=5
793	GC_WAIT_TIME=$((EXPIRE * 2 + 2))
794
795	# Check expiration of routes every $EXPIRE seconds (GC)
796	$NS_EXEC sysctl -wq net.ipv6.route.gc_interval=$EXPIRE
797
798	$IP link add dummy_10 type dummy
799	$IP link set dev dummy_10 up
800	$IP -6 address add 2001:10::1/64 dev dummy_10
801
802	$NS_EXEC sysctl -wq net.ipv6.route.flush=1
803
804	# Temporary routes
805	for i in $(seq 1 5); do
806	    # Expire route after $EXPIRE seconds
807	    $IP -6 route add 2001:20::$i \
808		via 2001:10::2 dev dummy_10 expires $EXPIRE
809	done
810	sleep $GC_WAIT_TIME
811	$NS_EXEC sysctl -wq net.ipv6.route.flush=1
812	check_rt_num 0 $($IP -6 route list |grep expires|wc -l)
813	log_test $ret 0 "ipv6 route garbage collection"
814
815	reset_dummy_10
816
817	# Permanent routes
818	for i in $(seq 1 5); do
819	    $IP -6 route add 2001:30::$i \
820		via 2001:10::2 dev dummy_10
821	done
822	# Temporary routes
823	for i in $(seq 1 5); do
824	    # Expire route after $EXPIRE seconds
825	    $IP -6 route add 2001:20::$i \
826		via 2001:10::2 dev dummy_10 expires $EXPIRE
827	done
828	# Wait for GC
829	sleep $GC_WAIT_TIME
830	check_rt_num 0 $($IP -6 route list |grep expires|wc -l)
831	log_test $ret 0 "ipv6 route garbage collection (with permanent routes)"
832
833	reset_dummy_10
834
835	# Permanent routes
836	for i in $(seq 1 5); do
837	    $IP -6 route add 2001:20::$i \
838		via 2001:10::2 dev dummy_10
839	done
840	# Replace with temporary routes
841	for i in $(seq 1 5); do
842	    # Expire route after $EXPIRE seconds
843	    $IP -6 route replace 2001:20::$i \
844		via 2001:10::2 dev dummy_10 expires $EXPIRE
845	done
846	# Wait for GC
847	sleep $GC_WAIT_TIME
848	check_rt_num 0 $($IP -6 route list |grep expires|wc -l)
849	log_test $ret 0 "ipv6 route garbage collection (replace with expires)"
850
851	reset_dummy_10
852
853	# Temporary routes
854	for i in $(seq 1 5); do
855	    # Expire route after $EXPIRE seconds
856	    $IP -6 route add 2001:20::$i \
857		via 2001:10::2 dev dummy_10 expires $EXPIRE
858	done
859	# Replace with permanent routes
860	for i in $(seq 1 5); do
861	    $IP -6 route replace 2001:20::$i \
862		via 2001:10::2 dev dummy_10
863	done
864	check_rt_num_clean 0 $($IP -6 route list |grep expires|wc -l) || return
865
866	# Wait for GC
867	sleep $GC_WAIT_TIME
868	check_rt_num 5 $($IP -6 route list |grep -v expires|grep 2001:20::|wc -l)
869	log_test $ret 0 "ipv6 route garbage collection (replace with permanent)"
870
871	# Delete dummy_10 and remove all routes
872	$IP link del dev dummy_10
873
874	# rd6 is required for the next test. (ipv6toolkit)
875	if [ ! -x "$(command -v rd6)" ]; then
876	    echo "SKIP: rd6 not found."
877	    set +e
878	    cleanup &> /dev/null
879	    return
880	fi
881
882	setup_ns ns2
883	$IP link add veth1 type veth peer veth2 netns $ns2
884	$IP link set veth1 up
885	ip -netns $ns2 link set veth2 up
886	$IP addr add fe80:dead::1/64 dev veth1
887	ip -netns $ns2 addr add fe80:dead::2/64 dev veth2
888
889	# Add NTF_ROUTER neighbour to prevent rt6_age_examine_exception()
890	# from removing not-yet-expired exceptions.
891	ip -netns $ns2 link set veth2 address 00:11:22:33:44:55
892	$IP neigh add fe80:dead::3 lladdr 00:11:22:33:44:55 dev veth1 router
893
894	$NS_EXEC sysctl -wq net.ipv6.conf.veth1.accept_redirects=1
895	$NS_EXEC sysctl -wq net.ipv6.conf.veth1.forwarding=0
896
897	# Temporary routes
898	for i in $(seq 1 5); do
899	    # Expire route after $EXPIRE seconds
900	    $IP -6 route add 2001:10::$i \
901		via fe80:dead::2 dev veth1 expires $EXPIRE
902
903	    ip netns exec $ns2 rd6 -i veth2 \
904		-s fe80:dead::2 -d fe80:dead::1 \
905		-r 2001:10::$i -t fe80:dead::3 -p ICMP6
906	done
907
908	check_rt_num 5 $($IP -6 route list | grep expires | grep 2001:10:: | wc -l)
909
910	# Promote to permanent routes by "prepend" (w/o NLM_F_EXCL and NLM_F_REPLACE)
911	for i in $(seq 1 5); do
912	    # -EEXIST, but the temporary route becomes the permanent route.
913	    $IP -6 route append 2001:10::$i \
914		via fe80:dead::2 dev veth1 2>/dev/null || true
915	done
916
917	check_rt_num 5 $($IP -6 route list | grep -v expires | grep 2001:10:: | wc -l)
918	check_rt_num 5 $($IP -6 route list cache | grep 2001:10:: | wc -l)
919
920	# Trigger GC instead of waiting $GC_WAIT_TIME.
921	# rt6_nh_dump_exceptions() just skips expired exceptions.
922	$NS_EXEC sysctl -wq net.ipv6.route.flush=1
923	check_rt_num 0 $($IP -6 route list cache | grep 2001:10:: | wc -l)
924	log_test $ret 0 "ipv6 route garbage collection (promote to permanent routes)"
925
926	$IP neigh del fe80:dead::3 lladdr 00:11:22:33:44:55 dev veth1 router
927	$IP link del veth1
928
929	# ra6 is required for the next test. (ipv6toolkit)
930	if [ ! -x "$(command -v ra6)" ]; then
931	    echo "SKIP: ra6 not found."
932	    set +e
933	    cleanup &> /dev/null
934	    return
935	fi
936
937	# Create a pair of veth devices to send a RA message from one
938	# device to another.
939	$IP link add veth1 type veth peer name veth2
940	$IP link set dev veth1 up
941	$IP link set dev veth2 up
942	$IP -6 address add 2001:10::1/64 dev veth1 nodad
943	$IP -6 address add 2001:10::2/64 dev veth2 nodad
944
945	# Make veth1 ready to receive RA messages.
946	$NS_EXEC sysctl -wq net.ipv6.conf.veth1.accept_ra=2
947
948	# Send a RA message with a route from veth2 to veth1.
949	$NS_EXEC ra6 -i veth2 -d 2001:10::1 -t $EXPIRE
950
951	# Wait for the RA message.
952	sleep 1
953
954	# systemd may mess up the test.  You syould make sure that
955	# systemd-networkd.service and systemd-networkd.socket are stopped.
956	check_rt_num_clean 1 $($IP -6 route list|grep expires|wc -l) || return
957
958	# Wait for GC
959	sleep $GC_WAIT_TIME
960	check_rt_num 0 $($IP -6 route list |grep expires|wc -l)
961	log_test $ret 0 "ipv6 route garbage collection (RA message)"
962
963	set +e
964
965	cleanup &> /dev/null
966}
967
968fib_suppress_test()
969{
970	echo
971	echo "FIB rule with suppress_prefixlength"
972	setup
973
974	$IP link add dummy1 type dummy
975	$IP link set dummy1 up
976	$IP -6 route add default dev dummy1
977	$IP -6 rule add table main suppress_prefixlength 0
978	ping -f -c 1000 -W 1 1234::1 >/dev/null 2>&1
979	$IP -6 rule del table main suppress_prefixlength 0
980	$IP link del dummy1
981
982	# If we got here without crashing, we're good.
983	log_test 0 0 "FIB rule suppress test"
984
985	cleanup
986}
987
988################################################################################
989# Tests on route add and replace
990
991run_cmd()
992{
993	local cmd="$1"
994	local out
995	local stderr="2>/dev/null"
996
997	if [ "$VERBOSE" = "1" ]; then
998		printf "    COMMAND: $cmd\n"
999		stderr=
1000	fi
1001
1002	out=$(eval $cmd $stderr)
1003	rc=$?
1004	if [ "$VERBOSE" = "1" -a -n "$out" ]; then
1005		echo "    $out"
1006	fi
1007
1008	[ "$VERBOSE" = "1" ] && echo
1009
1010	return $rc
1011}
1012
1013check_expected()
1014{
1015	local out="$1"
1016	local expected="$2"
1017	local rc=0
1018
1019	[ "${out}" = "${expected}" ] && return 0
1020
1021	if [ -z "${out}" ]; then
1022		if [ "$VERBOSE" = "1" ]; then
1023			printf "\nNo route entry found\n"
1024			printf "Expected:\n"
1025			printf "    ${expected}\n"
1026		fi
1027		return 1
1028	fi
1029
1030	# tricky way to convert output to 1-line without ip's
1031	# messy '\'; this drops all extra white space
1032	out=$(echo ${out})
1033	if [ "${out}" != "${expected}" ]; then
1034		rc=1
1035		if [ "${VERBOSE}" = "1" ]; then
1036			printf "    Unexpected route entry. Have:\n"
1037			printf "        ${out}\n"
1038			printf "    Expected:\n"
1039			printf "        ${expected}\n\n"
1040		fi
1041	fi
1042
1043	return $rc
1044}
1045
1046# add route for a prefix, flushing any existing routes first
1047# expected to be the first step of a test
1048add_route6()
1049{
1050	local pfx="$1"
1051	local nh="$2"
1052	local out
1053
1054	if [ "$VERBOSE" = "1" ]; then
1055		echo
1056		echo "    ##################################################"
1057		echo
1058	fi
1059
1060	run_cmd "$IP -6 ro flush ${pfx}"
1061	[ $? -ne 0 ] && exit 1
1062
1063	out=$($IP -6 ro ls match ${pfx})
1064	if [ -n "$out" ]; then
1065		echo "Failed to flush routes for prefix used for tests."
1066		exit 1
1067	fi
1068
1069	run_cmd "$IP -6 ro add ${pfx} ${nh}"
1070	if [ $? -ne 0 ]; then
1071		echo "Failed to add initial route for test."
1072		exit 1
1073	fi
1074}
1075
1076# add initial route - used in replace route tests
1077add_initial_route6()
1078{
1079	add_route6 "2001:db8:104::/64" "$1"
1080}
1081
1082check_route6()
1083{
1084	local pfx
1085	local expected="$1"
1086	local out
1087	local rc=0
1088
1089	set -- $expected
1090	pfx=$1
1091
1092	out=$($IP -6 ro ls match ${pfx} | sed -e 's/ pref medium//')
1093	check_expected "${out}" "${expected}"
1094}
1095
1096route_cleanup()
1097{
1098	$IP li del red 2>/dev/null
1099	$IP li del dummy1 2>/dev/null
1100	$IP li del veth1 2>/dev/null
1101	$IP li del veth3 2>/dev/null
1102
1103	cleanup &> /dev/null
1104}
1105
1106route_setup()
1107{
1108	route_cleanup
1109	setup
1110
1111	[ "${VERBOSE}" = "1" ] && set -x
1112	set -e
1113
1114	setup_ns ns2
1115	ip netns exec $ns2 sysctl -qw net.ipv4.ip_forward=1
1116	ip netns exec $ns2 sysctl -qw net.ipv6.conf.all.forwarding=1
1117
1118	$IP li add veth1 type veth peer name veth2
1119	$IP li add veth3 type veth peer name veth4
1120
1121	$IP li set veth1 up
1122	$IP li set veth3 up
1123	$IP li set veth2 netns $ns2 up
1124	$IP li set veth4 netns $ns2 up
1125	ip -netns $ns2 li add dummy1 type dummy
1126	ip -netns $ns2 li set dummy1 up
1127
1128	$IP -6 addr add 2001:db8:101::1/64 dev veth1 nodad
1129	$IP -6 addr add 2001:db8:103::1/64 dev veth3 nodad
1130	$IP addr add 172.16.101.1/24 dev veth1
1131	$IP addr add 172.16.103.1/24 dev veth3
1132
1133	ip -netns $ns2 -6 addr add 2001:db8:101::2/64 dev veth2 nodad
1134	ip -netns $ns2 -6 addr add 2001:db8:103::2/64 dev veth4 nodad
1135	ip -netns $ns2 -6 addr add 2001:db8:104::1/64 dev dummy1 nodad
1136
1137	ip -netns $ns2 addr add 172.16.101.2/24 dev veth2
1138	ip -netns $ns2 addr add 172.16.103.2/24 dev veth4
1139	ip -netns $ns2 addr add 172.16.104.1/24 dev dummy1
1140
1141	set +e
1142}
1143
1144forwarding_cleanup()
1145{
1146	cleanup_ns $ns3
1147
1148	route_cleanup
1149}
1150
1151# extend route_setup with an ns3 reachable through ns2 over both devices
1152forwarding_setup()
1153{
1154	forwarding_cleanup
1155
1156	route_setup
1157
1158	setup_ns ns3
1159
1160	ip link add veth5 netns $ns3 type veth peer name veth6 netns $ns2
1161	ip -netns $ns3 link set veth5 up
1162	ip -netns $ns2 link set veth6 up
1163
1164	ip -netns $ns3 -4 addr add dev veth5 172.16.105.1/24
1165	ip -netns $ns2 -4 addr add dev veth6 172.16.105.2/24
1166	ip -netns $ns3 -4 route add 172.16.100.0/22 via 172.16.105.2
1167
1168	ip -netns $ns3 -6 addr add dev veth5 2001:db8:105::1/64 nodad
1169	ip -netns $ns2 -6 addr add dev veth6 2001:db8:105::2/64 nodad
1170	ip -netns $ns3 -6 route add 2001:db8:101::/33 via 2001:db8:105::2
1171}
1172
1173# assumption is that basic add of a single path route works
1174# otherwise just adding an address on an interface is broken
1175ipv6_rt_add()
1176{
1177	local rc
1178
1179	echo
1180	echo "IPv6 route add / append tests"
1181
1182	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1183	add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
1184	run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::2"
1185	log_test $? 2 "Attempt to add duplicate route - gw"
1186
1187	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1188	add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
1189	run_cmd "$IP -6 ro add 2001:db8:104::/64 dev veth3"
1190	log_test $? 2 "Attempt to add duplicate route - dev only"
1191
1192	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1193	add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
1194	run_cmd "$IP -6 ro add unreachable 2001:db8:104::/64"
1195	log_test $? 2 "Attempt to add duplicate route - reject route"
1196
1197	# route append with same prefix adds a new route
1198	# - iproute2 sets NLM_F_CREATE | NLM_F_APPEND
1199	add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
1200	run_cmd "$IP -6 ro append 2001:db8:104::/64 via 2001:db8:103::2"
1201	check_route6 "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
1202	log_test $? 0 "Append nexthop to existing route - gw"
1203
1204	# insert mpath directly
1205	add_route6 "2001:db8:104::/64" "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1206	check_route6  "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
1207	log_test $? 0 "Add multipath route"
1208
1209	add_route6 "2001:db8:104::/64" "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1210	run_cmd "$IP -6 ro add 2001:db8:104::/64 nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1211	log_test $? 2 "Attempt to add duplicate multipath route"
1212
1213	# insert of a second route without append but different metric
1214	add_route6 "2001:db8:104::/64" "via 2001:db8:101::2"
1215	run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::2 metric 512"
1216	rc=$?
1217	if [ $rc -eq 0 ]; then
1218		run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::3 metric 256"
1219		rc=$?
1220	fi
1221	log_test $rc 0 "Route add with different metrics"
1222
1223	run_cmd "$IP -6 ro del 2001:db8:104::/64 metric 512"
1224	rc=$?
1225	if [ $rc -eq 0 ]; then
1226		check_route6 "2001:db8:104::/64 via 2001:db8:103::3 dev veth3 metric 256 2001:db8:104::/64 via 2001:db8:101::2 dev veth1 metric 1024"
1227		rc=$?
1228	fi
1229	log_test $rc 0 "Route delete with metric"
1230}
1231
1232ipv6_rt_replace_single()
1233{
1234	# single path with single path
1235	#
1236	add_initial_route6 "via 2001:db8:101::2"
1237	run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:103::2"
1238	check_route6 "2001:db8:104::/64 via 2001:db8:103::2 dev veth3 metric 1024"
1239	log_test $? 0 "Single path with single path"
1240
1241	# single path with multipath
1242	#
1243	add_initial_route6 "nexthop via 2001:db8:101::2"
1244	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::2"
1245	check_route6 "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::3 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
1246	log_test $? 0 "Single path with multipath"
1247
1248	# single path with single path using MULTIPATH attribute
1249	#
1250	add_initial_route6 "via 2001:db8:101::2"
1251	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:103::2"
1252	check_route6 "2001:db8:104::/64 via 2001:db8:103::2 dev veth3 metric 1024"
1253	log_test $? 0 "Single path with single path via multipath attribute"
1254
1255	# route replace fails - invalid nexthop
1256	add_initial_route6 "via 2001:db8:101::2"
1257	run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:104::2"
1258	if [ $? -eq 0 ]; then
1259		# previous command is expected to fail so if it returns 0
1260		# that means the test failed.
1261		log_test 0 1 "Invalid nexthop"
1262	else
1263		check_route6 "2001:db8:104::/64 via 2001:db8:101::2 dev veth1 metric 1024"
1264		log_test $? 0 "Invalid nexthop"
1265	fi
1266
1267	# replace non-existent route
1268	# - note use of change versus replace since ip adds NLM_F_CREATE
1269	#   for replace
1270	add_initial_route6 "via 2001:db8:101::2"
1271	run_cmd "$IP -6 ro change 2001:db8:105::/64 via 2001:db8:101::2"
1272	log_test $? 2 "Single path - replace of non-existent route"
1273}
1274
1275ipv6_rt_replace_mpath()
1276{
1277	# multipath with multipath
1278	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1279	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::3"
1280	check_route6  "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::3 dev veth1 weight 1 nexthop via 2001:db8:103::3 dev veth3 weight 1"
1281	log_test $? 0 "Multipath with multipath"
1282
1283	# multipath with single
1284	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1285	run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:101::3"
1286	check_route6  "2001:db8:104::/64 via 2001:db8:101::3 dev veth1 metric 1024"
1287	log_test $? 0 "Multipath with single path"
1288
1289	# multipath with single
1290	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1291	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3"
1292	check_route6 "2001:db8:104::/64 via 2001:db8:101::3 dev veth1 metric 1024"
1293	log_test $? 0 "Multipath with single path via multipath attribute"
1294
1295	# multipath with dev-only
1296	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1297	run_cmd "$IP -6 ro replace 2001:db8:104::/64 dev veth1"
1298	check_route6 "2001:db8:104::/64 dev veth1 metric 1024"
1299	log_test $? 0 "Multipath with dev-only"
1300
1301	# route replace fails - invalid nexthop 1
1302	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1303	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:111::3 nexthop via 2001:db8:103::3"
1304	check_route6  "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
1305	log_test $? 0 "Multipath - invalid first nexthop"
1306
1307	# route replace fails - invalid nexthop 2
1308	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1309	run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:113::3"
1310	check_route6  "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
1311	log_test $? 0 "Multipath - invalid second nexthop"
1312
1313	# multipath non-existent route
1314	add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1315	run_cmd "$IP -6 ro change 2001:db8:105::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::3"
1316	log_test $? 2 "Multipath - replace of non-existent route"
1317}
1318
1319ipv6_rt_replace()
1320{
1321	echo
1322	echo "IPv6 route replace tests"
1323
1324	ipv6_rt_replace_single
1325	ipv6_rt_replace_mpath
1326}
1327
1328ipv6_rt_dsfield()
1329{
1330	echo
1331	echo "IPv6 route with dsfield tests"
1332
1333	run_cmd "$IP -6 route flush 2001:db8:102::/64"
1334
1335	# IPv6 doesn't support routing based on dsfield
1336	run_cmd "$IP -6 route add 2001:db8:102::/64 dsfield 0x04 via 2001:db8:101::2"
1337	log_test $? 2 "Reject route with dsfield"
1338}
1339
1340ipv6_route_test()
1341{
1342	route_setup
1343
1344	ipv6_rt_add
1345	ipv6_rt_replace
1346	ipv6_rt_dsfield
1347
1348	route_cleanup
1349}
1350
1351ip_addr_metric_check()
1352{
1353	ip addr help 2>&1 | grep -q metric
1354	if [ $? -ne 0 ]; then
1355		echo "iproute2 command does not support metric for addresses. Skipping test"
1356		return 1
1357	fi
1358
1359	return 0
1360}
1361
1362ipv6_addr_metric_test()
1363{
1364	local rc
1365
1366	echo
1367	echo "IPv6 prefix route tests"
1368
1369	ip_addr_metric_check || return 1
1370
1371	setup
1372
1373	set -e
1374	$IP li add dummy1 type dummy
1375	$IP li add dummy2 type dummy
1376	$IP li set dummy1 up
1377	$IP li set dummy2 up
1378
1379	# default entry is metric 256
1380	run_cmd "$IP -6 addr add dev dummy1 2001:db8:104::1/64"
1381	run_cmd "$IP -6 addr add dev dummy2 2001:db8:104::2/64"
1382	set +e
1383
1384	check_route6 "2001:db8:104::/64 dev dummy1 proto kernel metric 256 2001:db8:104::/64 dev dummy2 proto kernel metric 256"
1385	log_test $? 0 "Default metric"
1386
1387	set -e
1388	run_cmd "$IP -6 addr flush dev dummy1"
1389	run_cmd "$IP -6 addr add dev dummy1 2001:db8:104::1/64 metric 257"
1390	set +e
1391
1392	check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 256 2001:db8:104::/64 dev dummy1 proto kernel metric 257"
1393	log_test $? 0 "User specified metric on first device"
1394
1395	set -e
1396	run_cmd "$IP -6 addr flush dev dummy2"
1397	run_cmd "$IP -6 addr add dev dummy2 2001:db8:104::2/64 metric 258"
1398	set +e
1399
1400	check_route6 "2001:db8:104::/64 dev dummy1 proto kernel metric 257 2001:db8:104::/64 dev dummy2 proto kernel metric 258"
1401	log_test $? 0 "User specified metric on second device"
1402
1403	run_cmd "$IP -6 addr del dev dummy1 2001:db8:104::1/64 metric 257"
1404	rc=$?
1405	if [ $rc -eq 0 ]; then
1406		check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 258"
1407		rc=$?
1408	fi
1409	log_test $rc 0 "Delete of address on first device"
1410
1411	run_cmd "$IP -6 addr change dev dummy2 2001:db8:104::2/64 metric 259"
1412	rc=$?
1413	if [ $rc -eq 0 ]; then
1414		check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 259"
1415		rc=$?
1416	fi
1417	log_test $rc 0 "Modify metric of address"
1418
1419	# verify prefix route removed on down
1420	run_cmd "ip netns exec $ns1 sysctl -qw net.ipv6.conf.all.keep_addr_on_down=1"
1421	run_cmd "$IP li set dev dummy2 down"
1422	rc=$?
1423	if [ $rc -eq 0 ]; then
1424		out=$($IP -6 ro ls match 2001:db8:104::/64)
1425		check_expected "${out}" ""
1426		rc=$?
1427	fi
1428	log_test $rc 0 "Prefix route removed on link down"
1429
1430	# verify prefix route re-inserted with assigned metric
1431	run_cmd "$IP li set dev dummy2 up"
1432	rc=$?
1433	if [ $rc -eq 0 ]; then
1434		check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 259"
1435		rc=$?
1436	fi
1437	log_test $rc 0 "Prefix route with metric on link up"
1438
1439	# verify peer metric added correctly
1440	set -e
1441	run_cmd "$IP -6 addr flush dev dummy2"
1442	run_cmd "$IP -6 addr add dev dummy2 2001:db8:104::1 peer 2001:db8:104::2 metric 260"
1443	set +e
1444
1445	check_route6 "2001:db8:104::1 dev dummy2 proto kernel metric 260"
1446	log_test $? 0 "Set metric with peer route on local side"
1447	check_route6 "2001:db8:104::2 dev dummy2 proto kernel metric 260"
1448	log_test $? 0 "Set metric with peer route on peer side"
1449
1450	set -e
1451	run_cmd "$IP -6 addr change dev dummy2 2001:db8:104::1 peer 2001:db8:104::3 metric 261"
1452	set +e
1453
1454	check_route6 "2001:db8:104::1 dev dummy2 proto kernel metric 261"
1455	log_test $? 0 "Modify metric and peer address on local side"
1456	check_route6 "2001:db8:104::3 dev dummy2 proto kernel metric 261"
1457	log_test $? 0 "Modify metric and peer address on peer side"
1458
1459	$IP li del dummy1
1460	$IP li del dummy2
1461	cleanup
1462}
1463
1464ipv6_route_metrics_test()
1465{
1466	local rc
1467
1468	echo
1469	echo "IPv6 routes with metrics"
1470
1471	route_setup
1472
1473	#
1474	# single path with metrics
1475	#
1476	run_cmd "$IP -6 ro add 2001:db8:111::/64 via 2001:db8:101::2 mtu 1400"
1477	rc=$?
1478	if [ $rc -eq 0 ]; then
1479		check_route6  "2001:db8:111::/64 via 2001:db8:101::2 dev veth1 metric 1024 mtu 1400"
1480		rc=$?
1481	fi
1482	log_test $rc 0 "Single path route with mtu metric"
1483
1484
1485	#
1486	# multipath via separate routes with metrics
1487	#
1488	run_cmd "$IP -6 ro add 2001:db8:112::/64 via 2001:db8:101::2 mtu 1400"
1489	run_cmd "$IP -6 ro append 2001:db8:112::/64 via 2001:db8:103::2"
1490	rc=$?
1491	if [ $rc -eq 0 ]; then
1492		check_route6 "2001:db8:112::/64 metric 1024 mtu 1400 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
1493		rc=$?
1494	fi
1495	log_test $rc 0 "Multipath route via 2 single routes with mtu metric on first"
1496
1497	# second route is coalesced to first to make a multipath route.
1498	# MTU of the second path is hidden from display!
1499	run_cmd "$IP -6 ro add 2001:db8:113::/64 via 2001:db8:101::2"
1500	run_cmd "$IP -6 ro append 2001:db8:113::/64 via 2001:db8:103::2 mtu 1400"
1501	rc=$?
1502	if [ $rc -eq 0 ]; then
1503		check_route6 "2001:db8:113::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
1504		rc=$?
1505	fi
1506	log_test $rc 0 "Multipath route via 2 single routes with mtu metric on 2nd"
1507
1508	run_cmd "$IP -6 ro del 2001:db8:113::/64 via 2001:db8:101::2"
1509	if [ $? -eq 0 ]; then
1510		check_route6 "2001:db8:113::/64 via 2001:db8:103::2 dev veth3 metric 1024 mtu 1400"
1511		log_test $? 0 "    MTU of second leg"
1512	fi
1513
1514	#
1515	# multipath with metrics
1516	#
1517	run_cmd "$IP -6 ro add 2001:db8:115::/64 mtu 1400 nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2"
1518	rc=$?
1519	if [ $rc -eq 0 ]; then
1520		check_route6  "2001:db8:115::/64 metric 1024 mtu 1400 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1"
1521		rc=$?
1522	fi
1523	log_test $rc 0 "Multipath route with mtu metric"
1524
1525	$IP -6 ro add 2001:db8:104::/64 via 2001:db8:101::2 mtu 1300
1526	run_cmd "ip netns exec $ns1 ${ping6} -w1 -c1 -s 1500 2001:db8:104::1"
1527	log_test $? 0 "Using route with mtu metric"
1528
1529	run_cmd "$IP -6 ro add 2001:db8:114::/64 via  2001:db8:101::2  congctl lock foo"
1530	log_test $? 2 "Invalid metric (fails metric_convert)"
1531
1532	route_cleanup
1533}
1534
1535fib6_ra_to_static()
1536{
1537	setup
1538
1539	echo
1540	echo "Fib6 route promotion from RA-learned to static test"
1541	set -e
1542
1543	# ra6 is required for the test. (ipv6toolkit)
1544	if [ ! -x "$(command -v ra6)" ]; then
1545	    echo "SKIP: ra6 not found."
1546	    set +e
1547	    cleanup &> /dev/null
1548	    return
1549	fi
1550
1551	# Create a pair of veth devices to send a RA message from one
1552	# device to another.
1553	$IP link add veth1 type veth peer name veth2
1554	$IP link set dev veth1 up
1555	$IP link set dev veth2 up
1556	$IP -6 address add 2001:10::1/64 dev veth1 nodad
1557	$IP -6 address add 2001:10::2/64 dev veth2 nodad
1558
1559	# Make veth1 ready to receive RA messages.
1560	$NS_EXEC sysctl -wq net.ipv6.conf.veth1.accept_ra=2
1561
1562	# Send a RA message with a prefix from veth2.
1563	$NS_EXEC ra6 -i veth2 -d 2001:10::1 -P 2001:12::/64\#LA\#120\#60
1564
1565	# Wait for the RA message.
1566	sleep 1
1567
1568	# systemd may mess up the test. Make sure that
1569	# systemd-networkd.service and systemd-networkd.socket are stopped.
1570	check_rt_num_clean 2 $($IP -6 route list|grep expires|wc -l) || return
1571
1572	# Configure static address on the same prefix
1573	$IP -6 address add 2001:12::dead/64 dev veth1 nodad
1574
1575	# On-link route won't expire anymore, default route still owned by RA
1576	check_rt_num 1 $($IP -6 route list |grep expires|wc -l)
1577
1578	# Send a second RA message with a prefix from veth2.
1579	$NS_EXEC ra6 -i veth2 -d 2001:10::1 -P 2001:12::/64\#LA\#120\#60
1580	sleep 1
1581
1582	# Expire is not back, on-link route is still static
1583	check_rt_num 1 $($IP -6 route list |grep expires|wc -l)
1584
1585	$IP -6 address del 2001:12::dead/64 dev veth1 nodad
1586
1587	# Expire is back, on-link route is now owned by RA again
1588	check_rt_num 2 $($IP -6 route list |grep expires|wc -l)
1589
1590	log_test $ret 0 "ipv6 promote RA route to static"
1591
1592	# Prepare for RA route with gateway
1593	$NS_EXEC sysctl -wq net.ipv6.conf.veth1.accept_ra_rt_info_max_plen=64
1594
1595	# Add initial route to cause ECMP merging
1596	$IP -6 route add 2001:12::/64 via fe80::dead:beef dev veth1
1597
1598	$NS_EXEC ra6 -i veth2 -d 2001:10::1 -R 2001:12::/64#1#120
1599
1600	# Routes are not merged as RA routes are not elegible for ECMP
1601	check_rt_num 2 "$($IP -6 route list | grep -c "2001:12::/64 via")"
1602
1603	$IP -6 route append 2001:12::/64 via fe80::dead:feeb dev veth1
1604
1605	check_rt_num 2 "$($IP -6 route list | grep -c "nexthop via")"
1606
1607	log_test "$ret" 0 "ipv6 RA route with nexthop do not merge into ECMP with static"
1608
1609	set +e
1610
1611	cleanup &> /dev/null
1612}
1613
1614fib6_temp_addr_renewal() {
1615	setup
1616
1617	echo
1618	echo "Fib6 temporary address renewal test"
1619	set -e
1620
1621	# ra6 is required for the test. (ipv6toolkit)
1622	if [ ! -x "$(command -v ra6)" ]; then
1623	    echo "SKIP: ra6 not found."
1624	    set +e
1625	    cleanup &> /dev/null
1626	    return
1627	fi
1628
1629	# Create a pair of veth devices to send a RA message from one
1630	# device to another.
1631	$IP link add veth1 type veth peer name veth2
1632	$IP link set dev veth1 up
1633	$IP link set dev veth2 up
1634
1635	# Make veth1 ready to receive RA messages.
1636	$NS_EXEC sysctl -wq net.ipv6.conf.veth1.accept_ra=2
1637	$NS_EXEC sysctl -wq net.ipv6.conf.veth1.use_tempaddr=2
1638	$NS_EXEC sysctl -wq net.ipv6.conf.veth1.temp_prefered_lft=15
1639	$NS_EXEC sysctl -wq net.ipv6.conf.veth1.max_desync_factor=0
1640
1641	# Send a RA message with a prefix from veth2.
1642	$NS_EXEC ra6 -i veth2 -s fe80::1 -d ff02::1 -P 2001:12::/64\#LA\#3600\#3600 -e
1643	sleep 3
1644
1645	# Deprecate it
1646	$NS_EXEC ra6 -i veth2 -s fe80::1 -d ff02::1 -P 2001:12::/64\#LA\#3600\#0 -e
1647	sleep 3
1648
1649	# Restore it
1650	$NS_EXEC ra6 -i veth2 -s fe80::1 -d ff02::1 -P 2001:12::/64\#LA\#3600\#3600 -e
1651
1652	ret=1
1653	for i in $(seq 1 25); do
1654		sleep 1
1655		num_dep="$($IP -6 addr | grep -c "temporary deprecated" || true)"
1656		num_tot="$($IP -6 addr | grep -c "temporary" || true)"
1657
1658		if [ "$num_dep" -eq 1 ] && [ "$num_tot" -ge 2 ]; then
1659			ret=0
1660			break
1661		fi
1662	done
1663	log_test "$ret" 0 "IPv6 temporary address cleanly deprecated and regenerated"
1664
1665	set +e
1666
1667	cleanup &> /dev/null
1668}
1669
1670# add route for a prefix, flushing any existing routes first
1671# expected to be the first step of a test
1672add_route()
1673{
1674	local pfx="$1"
1675	local nh="$2"
1676	local out
1677
1678	if [ "$VERBOSE" = "1" ]; then
1679		echo
1680		echo "    ##################################################"
1681		echo
1682	fi
1683
1684	run_cmd "$IP ro flush ${pfx}"
1685	[ $? -ne 0 ] && exit 1
1686
1687	out=$($IP ro ls match ${pfx})
1688	if [ -n "$out" ]; then
1689		echo "Failed to flush routes for prefix used for tests."
1690		exit 1
1691	fi
1692
1693	run_cmd "$IP ro add ${pfx} ${nh}"
1694	if [ $? -ne 0 ]; then
1695		echo "Failed to add initial route for test."
1696		exit 1
1697	fi
1698}
1699
1700# add initial route - used in replace route tests
1701add_initial_route()
1702{
1703	add_route "172.16.104.0/24" "$1"
1704}
1705
1706check_route()
1707{
1708	local pfx
1709	local expected="$1"
1710	local out
1711
1712	set -- $expected
1713	pfx=$1
1714	[ "${pfx}" = "unreachable" ] && pfx=$2
1715
1716	out=$($IP ro ls match ${pfx})
1717	check_expected "${out}" "${expected}"
1718}
1719
1720# assumption is that basic add of a single path route works
1721# otherwise just adding an address on an interface is broken
1722ipv4_rt_add()
1723{
1724	local rc
1725
1726	echo
1727	echo "IPv4 route add / append tests"
1728
1729	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1730	add_route "172.16.104.0/24" "via 172.16.101.2"
1731	run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.2"
1732	log_test $? 2 "Attempt to add duplicate route - gw"
1733
1734	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1735	add_route "172.16.104.0/24" "via 172.16.101.2"
1736	run_cmd "$IP ro add 172.16.104.0/24 dev veth3"
1737	log_test $? 2 "Attempt to add duplicate route - dev only"
1738
1739	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1740	add_route "172.16.104.0/24" "via 172.16.101.2"
1741	run_cmd "$IP ro add unreachable 172.16.104.0/24"
1742	log_test $? 2 "Attempt to add duplicate route - reject route"
1743
1744	# iproute2 prepend only sets NLM_F_CREATE
1745	# - adds a new route; does NOT convert existing route to ECMP
1746	add_route "172.16.104.0/24" "via 172.16.101.2"
1747	run_cmd "$IP ro prepend 172.16.104.0/24 via 172.16.103.2"
1748	check_route "172.16.104.0/24 via 172.16.103.2 dev veth3 172.16.104.0/24 via 172.16.101.2 dev veth1"
1749	log_test $? 0 "Add new nexthop for existing prefix"
1750
1751	# route append with same prefix adds a new route
1752	# - iproute2 sets NLM_F_CREATE | NLM_F_APPEND
1753	add_route "172.16.104.0/24" "via 172.16.101.2"
1754	run_cmd "$IP ro append 172.16.104.0/24 via 172.16.103.2"
1755	check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 172.16.104.0/24 via 172.16.103.2 dev veth3"
1756	log_test $? 0 "Append nexthop to existing route - gw"
1757
1758	add_route "172.16.104.0/24" "via 172.16.101.2"
1759	run_cmd "$IP ro append 172.16.104.0/24 dev veth3"
1760	check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 172.16.104.0/24 dev veth3 scope link"
1761	log_test $? 0 "Append nexthop to existing route - dev only"
1762
1763	add_route "172.16.104.0/24" "via 172.16.101.2"
1764	run_cmd "$IP ro append unreachable 172.16.104.0/24"
1765	check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 unreachable 172.16.104.0/24"
1766	log_test $? 0 "Append nexthop to existing route - reject route"
1767
1768	run_cmd "$IP ro flush 172.16.104.0/24"
1769	run_cmd "$IP ro add unreachable 172.16.104.0/24"
1770	run_cmd "$IP ro append 172.16.104.0/24 via 172.16.103.2"
1771	check_route "unreachable 172.16.104.0/24 172.16.104.0/24 via 172.16.103.2 dev veth3"
1772	log_test $? 0 "Append nexthop to existing reject route - gw"
1773
1774	run_cmd "$IP ro flush 172.16.104.0/24"
1775	run_cmd "$IP ro add unreachable 172.16.104.0/24"
1776	run_cmd "$IP ro append 172.16.104.0/24 dev veth3"
1777	check_route "unreachable 172.16.104.0/24 172.16.104.0/24 dev veth3 scope link"
1778	log_test $? 0 "Append nexthop to existing reject route - dev only"
1779
1780	# insert mpath directly
1781	add_route "172.16.104.0/24" "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1782	check_route  "172.16.104.0/24 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1"
1783	log_test $? 0 "add multipath route"
1784
1785	add_route "172.16.104.0/24" "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1786	run_cmd "$IP ro add 172.16.104.0/24 nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1787	log_test $? 2 "Attempt to add duplicate multipath route"
1788
1789	# insert of a second route without append but different metric
1790	add_route "172.16.104.0/24" "via 172.16.101.2"
1791	run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.2 metric 512"
1792	rc=$?
1793	if [ $rc -eq 0 ]; then
1794		run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.3 metric 256"
1795		rc=$?
1796	fi
1797	log_test $rc 0 "Route add with different metrics"
1798
1799	run_cmd "$IP ro del 172.16.104.0/24 metric 512"
1800	rc=$?
1801	if [ $rc -eq 0 ]; then
1802		check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 172.16.104.0/24 via 172.16.103.3 dev veth3 metric 256"
1803		rc=$?
1804	fi
1805	log_test $rc 0 "Route delete with metric"
1806}
1807
1808ipv4_rt_replace_single()
1809{
1810	# single path with single path
1811	#
1812	add_initial_route "via 172.16.101.2"
1813	run_cmd "$IP ro replace 172.16.104.0/24 via 172.16.103.2"
1814	check_route "172.16.104.0/24 via 172.16.103.2 dev veth3"
1815	log_test $? 0 "Single path with single path"
1816
1817	# single path with multipath
1818	#
1819	add_initial_route "nexthop via 172.16.101.2"
1820	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.2"
1821	check_route "172.16.104.0/24 nexthop via 172.16.101.3 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1"
1822	log_test $? 0 "Single path with multipath"
1823
1824	# single path with reject
1825	#
1826	add_initial_route "nexthop via 172.16.101.2"
1827	run_cmd "$IP ro replace unreachable 172.16.104.0/24"
1828	check_route "unreachable 172.16.104.0/24"
1829	log_test $? 0 "Single path with reject route"
1830
1831	# single path with single path using MULTIPATH attribute
1832	#
1833	add_initial_route "via 172.16.101.2"
1834	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.103.2"
1835	check_route "172.16.104.0/24 via 172.16.103.2 dev veth3"
1836	log_test $? 0 "Single path with single path via multipath attribute"
1837
1838	# route replace fails - invalid nexthop
1839	add_initial_route "via 172.16.101.2"
1840	run_cmd "$IP ro replace 172.16.104.0/24 via 2001:db8:104::2"
1841	if [ $? -eq 0 ]; then
1842		# previous command is expected to fail so if it returns 0
1843		# that means the test failed.
1844		log_test 0 1 "Invalid nexthop"
1845	else
1846		check_route "172.16.104.0/24 via 172.16.101.2 dev veth1"
1847		log_test $? 0 "Invalid nexthop"
1848	fi
1849
1850	# replace non-existent route
1851	# - note use of change versus replace since ip adds NLM_F_CREATE
1852	#   for replace
1853	add_initial_route "via 172.16.101.2"
1854	run_cmd "$IP ro change 172.16.105.0/24 via 172.16.101.2"
1855	log_test $? 2 "Single path - replace of non-existent route"
1856}
1857
1858ipv4_rt_replace_mpath()
1859{
1860	# multipath with multipath
1861	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1862	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.3"
1863	check_route  "172.16.104.0/24 nexthop via 172.16.101.3 dev veth1 weight 1 nexthop via 172.16.103.3 dev veth3 weight 1"
1864	log_test $? 0 "Multipath with multipath"
1865
1866	# multipath with single
1867	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1868	run_cmd "$IP ro replace 172.16.104.0/24 via 172.16.101.3"
1869	check_route  "172.16.104.0/24 via 172.16.101.3 dev veth1"
1870	log_test $? 0 "Multipath with single path"
1871
1872	# multipath with single
1873	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1874	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3"
1875	check_route "172.16.104.0/24 via 172.16.101.3 dev veth1"
1876	log_test $? 0 "Multipath with single path via multipath attribute"
1877
1878	# multipath with reject
1879	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1880	run_cmd "$IP ro replace unreachable 172.16.104.0/24"
1881	check_route "unreachable 172.16.104.0/24"
1882	log_test $? 0 "Multipath with reject route"
1883
1884	# route replace fails - invalid nexthop 1
1885	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1886	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.111.3 nexthop via 172.16.103.3"
1887	check_route  "172.16.104.0/24 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1"
1888	log_test $? 0 "Multipath - invalid first nexthop"
1889
1890	# route replace fails - invalid nexthop 2
1891	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1892	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.113.3"
1893	check_route  "172.16.104.0/24 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1"
1894	log_test $? 0 "Multipath - invalid second nexthop"
1895
1896	# multipath non-existent route
1897	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1898	run_cmd "$IP ro change 172.16.105.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.3"
1899	log_test $? 2 "Multipath - replace of non-existent route"
1900}
1901
1902ipv4_rt_replace()
1903{
1904	echo
1905	echo "IPv4 route replace tests"
1906
1907	ipv4_rt_replace_single
1908	ipv4_rt_replace_mpath
1909}
1910
1911# checks that cached input route on VRF port is deleted
1912# when VRF is deleted
1913ipv4_local_rt_cache()
1914{
1915	run_cmd "ip addr add 10.0.0.1/32 dev lo"
1916	run_cmd "setup_ns test-ns"
1917	run_cmd "ip link add veth-outside type veth peer name veth-inside"
1918	run_cmd "ip link add vrf-100 type vrf table 1100"
1919	run_cmd "ip link set veth-outside master vrf-100"
1920	run_cmd "ip link set veth-inside netns $test-ns"
1921	run_cmd "ip link set veth-outside up"
1922	run_cmd "ip link set vrf-100 up"
1923	run_cmd "ip route add 10.1.1.1/32 dev veth-outside table 1100"
1924	run_cmd "ip netns exec $test-ns ip link set veth-inside up"
1925	run_cmd "ip netns exec $test-ns ip addr add 10.1.1.1/32 dev veth-inside"
1926	run_cmd "ip netns exec $test-ns ip route add 10.0.0.1/32 dev veth-inside"
1927	run_cmd "ip netns exec $test-ns ip route add default via 10.0.0.1"
1928	run_cmd "ip netns exec $test-ns ping 10.0.0.1 -c 1 -i 1"
1929	run_cmd "ip link delete vrf-100"
1930
1931	# if we do not hang test is a success
1932	log_test $? 0 "Cached route removed from VRF port device"
1933}
1934
1935ipv4_rt_dsfield()
1936{
1937	echo
1938	echo "IPv4 route with dsfield tests"
1939
1940	run_cmd "$IP route flush 172.16.102.0/24"
1941
1942	# New routes should reject dsfield options that interfere with ECN
1943	run_cmd "$IP route add 172.16.102.0/24 dsfield 0x01 via 172.16.101.2"
1944	log_test $? 2 "Reject route with dsfield 0x01"
1945
1946	run_cmd "$IP route add 172.16.102.0/24 dsfield 0x02 via 172.16.101.2"
1947	log_test $? 2 "Reject route with dsfield 0x02"
1948
1949	run_cmd "$IP route add 172.16.102.0/24 dsfield 0x03 via 172.16.101.2"
1950	log_test $? 2 "Reject route with dsfield 0x03"
1951
1952	# A generic route that doesn't take DSCP into account
1953	run_cmd "$IP route add 172.16.102.0/24 via 172.16.101.2"
1954
1955	# A more specific route for DSCP 0x10
1956	run_cmd "$IP route add 172.16.102.0/24 dsfield 0x10 via 172.16.103.2"
1957
1958	# DSCP 0x10 should match the specific route, no matter the ECN bits
1959	$IP route get fibmatch 172.16.102.1 dsfield 0x10 | \
1960		grep -q "172.16.102.0/24 tos 0x10 via 172.16.103.2"
1961	log_test $? 0 "IPv4 route with DSCP and ECN:Not-ECT"
1962
1963	$IP route get fibmatch 172.16.102.1 dsfield 0x11 | \
1964		grep -q "172.16.102.0/24 tos 0x10 via 172.16.103.2"
1965	log_test $? 0 "IPv4 route with DSCP and ECN:ECT(1)"
1966
1967	$IP route get fibmatch 172.16.102.1 dsfield 0x12 | \
1968		grep -q "172.16.102.0/24 tos 0x10 via 172.16.103.2"
1969	log_test $? 0 "IPv4 route with DSCP and ECN:ECT(0)"
1970
1971	$IP route get fibmatch 172.16.102.1 dsfield 0x13 | \
1972		grep -q "172.16.102.0/24 tos 0x10 via 172.16.103.2"
1973	log_test $? 0 "IPv4 route with DSCP and ECN:CE"
1974
1975	# Unknown DSCP should match the generic route, no matter the ECN bits
1976	$IP route get fibmatch 172.16.102.1 dsfield 0x14 | \
1977		grep -q "172.16.102.0/24 via 172.16.101.2"
1978	log_test $? 0 "IPv4 route with unknown DSCP and ECN:Not-ECT"
1979
1980	$IP route get fibmatch 172.16.102.1 dsfield 0x15 | \
1981		grep -q "172.16.102.0/24 via 172.16.101.2"
1982	log_test $? 0 "IPv4 route with unknown DSCP and ECN:ECT(1)"
1983
1984	$IP route get fibmatch 172.16.102.1 dsfield 0x16 | \
1985		grep -q "172.16.102.0/24 via 172.16.101.2"
1986	log_test $? 0 "IPv4 route with unknown DSCP and ECN:ECT(0)"
1987
1988	$IP route get fibmatch 172.16.102.1 dsfield 0x17 | \
1989		grep -q "172.16.102.0/24 via 172.16.101.2"
1990	log_test $? 0 "IPv4 route with unknown DSCP and ECN:CE"
1991
1992	# Null DSCP should match the generic route, no matter the ECN bits
1993	$IP route get fibmatch 172.16.102.1 dsfield 0x00 | \
1994		grep -q "172.16.102.0/24 via 172.16.101.2"
1995	log_test $? 0 "IPv4 route with no DSCP and ECN:Not-ECT"
1996
1997	$IP route get fibmatch 172.16.102.1 dsfield 0x01 | \
1998		grep -q "172.16.102.0/24 via 172.16.101.2"
1999	log_test $? 0 "IPv4 route with no DSCP and ECN:ECT(1)"
2000
2001	$IP route get fibmatch 172.16.102.1 dsfield 0x02 | \
2002		grep -q "172.16.102.0/24 via 172.16.101.2"
2003	log_test $? 0 "IPv4 route with no DSCP and ECN:ECT(0)"
2004
2005	$IP route get fibmatch 172.16.102.1 dsfield 0x03 | \
2006		grep -q "172.16.102.0/24 via 172.16.101.2"
2007	log_test $? 0 "IPv4 route with no DSCP and ECN:CE"
2008}
2009
2010ipv4_route_test()
2011{
2012	route_setup
2013
2014	ipv4_rt_add
2015	ipv4_rt_replace
2016	ipv4_local_rt_cache
2017	ipv4_rt_dsfield
2018
2019	route_cleanup
2020}
2021
2022ipv4_addr_metric_test()
2023{
2024	local rc
2025
2026	echo
2027	echo "IPv4 prefix route tests"
2028
2029	ip_addr_metric_check || return 1
2030
2031	setup
2032
2033	set -e
2034	$IP li add dummy1 type dummy
2035	$IP li add dummy2 type dummy
2036	$IP li set dummy1 up
2037	$IP li set dummy2 up
2038
2039	# default entry is metric 256
2040	run_cmd "$IP addr add dev dummy1 172.16.104.1/24"
2041	run_cmd "$IP addr add dev dummy2 172.16.104.2/24"
2042	set +e
2043
2044	check_route "172.16.104.0/24 dev dummy1 proto kernel scope link src 172.16.104.1 172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2"
2045	log_test $? 0 "Default metric"
2046
2047	set -e
2048	run_cmd "$IP addr flush dev dummy1"
2049	run_cmd "$IP addr add dev dummy1 172.16.104.1/24 metric 257"
2050	set +e
2051
2052	check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 172.16.104.0/24 dev dummy1 proto kernel scope link src 172.16.104.1 metric 257"
2053	log_test $? 0 "User specified metric on first device"
2054
2055	set -e
2056	run_cmd "$IP addr flush dev dummy2"
2057	run_cmd "$IP addr add dev dummy2 172.16.104.2/24 metric 258"
2058	set +e
2059
2060	check_route "172.16.104.0/24 dev dummy1 proto kernel scope link src 172.16.104.1 metric 257 172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 258"
2061	log_test $? 0 "User specified metric on second device"
2062
2063	run_cmd "$IP addr del dev dummy1 172.16.104.1/24 metric 257"
2064	rc=$?
2065	if [ $rc -eq 0 ]; then
2066		check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 258"
2067		rc=$?
2068	fi
2069	log_test $rc 0 "Delete of address on first device"
2070
2071	run_cmd "$IP addr change dev dummy2 172.16.104.2/24 metric 259"
2072	rc=$?
2073	if [ $rc -eq 0 ]; then
2074		check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 259"
2075		rc=$?
2076	fi
2077	log_test $rc 0 "Modify metric of address"
2078
2079	# verify prefix route removed on down
2080	run_cmd "$IP li set dev dummy2 down"
2081	rc=$?
2082	if [ $rc -eq 0 ]; then
2083		out=$($IP ro ls match 172.16.104.0/24)
2084		check_expected "${out}" ""
2085		rc=$?
2086	fi
2087	log_test $rc 0 "Prefix route removed on link down"
2088
2089	# verify prefix route re-inserted with assigned metric
2090	run_cmd "$IP li set dev dummy2 up"
2091	rc=$?
2092	if [ $rc -eq 0 ]; then
2093		check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 259"
2094		rc=$?
2095	fi
2096	log_test $rc 0 "Prefix route with metric on link up"
2097
2098	# explicitly check for metric changes on edge scenarios
2099	run_cmd "$IP addr flush dev dummy2"
2100	run_cmd "$IP addr add dev dummy2 172.16.104.0/24 metric 259"
2101	run_cmd "$IP addr change dev dummy2 172.16.104.0/24 metric 260"
2102	rc=$?
2103	if [ $rc -eq 0 ]; then
2104		check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.0 metric 260"
2105		rc=$?
2106	fi
2107	log_test $rc 0 "Modify metric of .0/24 address"
2108
2109	run_cmd "$IP addr flush dev dummy2"
2110	run_cmd "$IP addr add dev dummy2 172.16.104.1/32 peer 172.16.104.2 metric 260"
2111	rc=$?
2112	if [ $rc -eq 0 ]; then
2113		check_route "172.16.104.2 dev dummy2 proto kernel scope link src 172.16.104.1 metric 260"
2114		rc=$?
2115	fi
2116	log_test $rc 0 "Set metric of address with peer route"
2117
2118	run_cmd "$IP addr change dev dummy2 172.16.104.1/32 peer 172.16.104.3 metric 261"
2119	rc=$?
2120	if [ $rc -eq 0 ]; then
2121		check_route "172.16.104.3 dev dummy2 proto kernel scope link src 172.16.104.1 metric 261"
2122		rc=$?
2123	fi
2124	log_test $rc 0 "Modify metric and peer address for peer route"
2125
2126	$IP li del dummy1
2127	$IP li del dummy2
2128	cleanup
2129}
2130
2131ipv4_route_metrics_test()
2132{
2133	local rc
2134
2135	echo
2136	echo "IPv4 route add / append tests"
2137
2138	route_setup
2139
2140	run_cmd "$IP ro add 172.16.111.0/24 via 172.16.101.2 mtu 1400"
2141	rc=$?
2142	if [ $rc -eq 0 ]; then
2143		check_route "172.16.111.0/24 via 172.16.101.2 dev veth1 mtu 1400"
2144		rc=$?
2145	fi
2146	log_test $rc 0 "Single path route with mtu metric"
2147
2148
2149	run_cmd "$IP ro add 172.16.112.0/24 mtu 1400 nexthop via 172.16.101.2 nexthop via 172.16.103.2"
2150	rc=$?
2151	if [ $rc -eq 0 ]; then
2152		check_route "172.16.112.0/24 mtu 1400 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1"
2153		rc=$?
2154	fi
2155	log_test $rc 0 "Multipath route with mtu metric"
2156
2157	$IP ro add 172.16.104.0/24 via 172.16.101.2 mtu 1300
2158	run_cmd "ip netns exec $ns1 ping -w1 -c1 -s 1500 172.16.104.1"
2159	log_test $? 0 "Using route with mtu metric"
2160
2161	run_cmd "$IP ro add 172.16.111.0/24 via 172.16.101.2 congctl lock foo"
2162	log_test $? 2 "Invalid metric (fails metric_convert)"
2163
2164	route_cleanup
2165}
2166
2167ipv4_del_addr_test()
2168{
2169	echo
2170	echo "IPv4 delete address route tests"
2171
2172	setup
2173
2174	set -e
2175	$IP li add dummy1 type dummy
2176	$IP li set dummy1 up
2177	$IP li add dummy2 type dummy
2178	$IP li set dummy2 up
2179	$IP li add red type vrf table 1111
2180	$IP li set red up
2181	$IP ro add vrf red unreachable default
2182	$IP li set dummy2 vrf red
2183
2184	$IP addr add dev dummy1 172.16.104.1/24
2185	$IP addr add dev dummy1 172.16.104.11/24
2186	$IP addr add dev dummy1 172.16.104.12/24
2187	$IP addr add dev dummy1 172.16.104.13/24
2188	$IP addr add dev dummy2 172.16.104.1/24
2189	$IP addr add dev dummy2 172.16.104.11/24
2190	$IP addr add dev dummy2 172.16.104.12/24
2191	$IP route add 172.16.105.0/24 via 172.16.104.2 src 172.16.104.11
2192	$IP route add 172.16.106.0/24 dev lo src 172.16.104.12
2193	$IP route add table 0 172.16.107.0/24 via 172.16.104.2 src 172.16.104.13
2194	$IP route add vrf red 172.16.105.0/24 via 172.16.104.2 src 172.16.104.11
2195	$IP route add vrf red 172.16.106.0/24 dev lo src 172.16.104.12
2196	set +e
2197
2198	# removing address from device in vrf should only remove route from vrf table
2199	echo "    Regular FIB info"
2200
2201	$IP addr del dev dummy2 172.16.104.11/24
2202	$IP ro ls vrf red | grep -q 172.16.105.0/24
2203	log_test $? 1 "Route removed from VRF when source address deleted"
2204
2205	$IP ro ls | grep -q 172.16.105.0/24
2206	log_test $? 0 "Route in default VRF not removed"
2207
2208	$IP addr add dev dummy2 172.16.104.11/24
2209	$IP route add vrf red 172.16.105.0/24 via 172.16.104.2 src 172.16.104.11
2210
2211	$IP addr del dev dummy1 172.16.104.11/24
2212	$IP ro ls | grep -q 172.16.105.0/24
2213	log_test $? 1 "Route removed in default VRF when source address deleted"
2214
2215	$IP ro ls vrf red | grep -q 172.16.105.0/24
2216	log_test $? 0 "Route in VRF is not removed by address delete"
2217
2218	# removing address from device in vrf should only remove route from vrf
2219	# table even when the associated fib info only differs in table ID
2220	echo "    Identical FIB info with different table ID"
2221
2222	$IP addr del dev dummy2 172.16.104.12/24
2223	$IP ro ls vrf red | grep -q 172.16.106.0/24
2224	log_test $? 1 "Route removed from VRF when source address deleted"
2225
2226	$IP ro ls | grep -q 172.16.106.0/24
2227	log_test $? 0 "Route in default VRF not removed"
2228
2229	$IP addr add dev dummy2 172.16.104.12/24
2230	$IP route add vrf red 172.16.106.0/24 dev lo src 172.16.104.12
2231
2232	$IP addr del dev dummy1 172.16.104.12/24
2233	$IP ro ls | grep -q 172.16.106.0/24
2234	log_test $? 1 "Route removed in default VRF when source address deleted"
2235
2236	$IP ro ls vrf red | grep -q 172.16.106.0/24
2237	log_test $? 0 "Route in VRF is not removed by address delete"
2238
2239	# removing address from device in default vrf should remove route from
2240	# the default vrf even when route was inserted with a table ID of 0.
2241	echo "    Table ID 0"
2242
2243	$IP addr del dev dummy1 172.16.104.13/24
2244	$IP ro ls | grep -q 172.16.107.0/24
2245	log_test $? 1 "Route removed in default VRF when source address deleted"
2246
2247	$IP li del dummy1
2248	$IP li del dummy2
2249	cleanup
2250}
2251
2252ipv6_del_addr_test()
2253{
2254	echo
2255	echo "IPv6 delete address route tests"
2256
2257	setup
2258
2259	set -e
2260	for i in $(seq 6); do
2261		$IP li add dummy${i} up type dummy
2262	done
2263
2264	$IP li add red up type vrf table 1111
2265	$IP ro add vrf red unreachable default
2266	for i in $(seq 4 6); do
2267		$IP li set dummy${i} vrf red
2268	done
2269
2270	$IP addr add dev dummy1 fe80::1/128
2271	$IP addr add dev dummy1 2001:db8:101::1/64
2272	$IP addr add dev dummy1 2001:db8:101::10/64
2273	$IP addr add dev dummy1 2001:db8:101::11/64
2274	$IP addr add dev dummy1 2001:db8:101::12/64
2275	$IP addr add dev dummy1 2001:db8:101::13/64
2276	$IP addr add dev dummy1 2001:db8:101::14/64
2277	$IP addr add dev dummy1 2001:db8:101::15/64
2278	$IP addr add dev dummy2 fe80::1/128
2279	$IP addr add dev dummy2 2001:db8:101::1/64
2280	$IP addr add dev dummy2 2001:db8:101::11/64
2281	$IP addr add dev dummy3 fe80::1/128
2282
2283	$IP addr add dev dummy4 2001:db8:101::1/64
2284	$IP addr add dev dummy4 2001:db8:101::10/64
2285	$IP addr add dev dummy4 2001:db8:101::11/64
2286	$IP addr add dev dummy4 2001:db8:101::12/64
2287	$IP addr add dev dummy4 2001:db8:101::13/64
2288	$IP addr add dev dummy4 2001:db8:101::14/64
2289	$IP addr add dev dummy5 2001:db8:101::1/64
2290	$IP addr add dev dummy5 2001:db8:101::11/64
2291
2292	# Single device using src address
2293	$IP route add 2001:db8:110::/64 dev dummy3 src 2001:db8:101::10
2294	# Two devices with the same source address
2295	$IP route add 2001:db8:111::/64 dev dummy3 src 2001:db8:101::11
2296	# VRF with single device using src address
2297	$IP route add vrf red 2001:db8:110::/64 dev dummy6 src 2001:db8:101::10
2298	# VRF with two devices using src address
2299	$IP route add vrf red 2001:db8:111::/64 dev dummy6 src 2001:db8:101::11
2300	# src address and nexthop dev in same VRF
2301	$IP route add 2001:db8:112::/64 dev dummy3 src 2001:db8:101::12
2302	$IP route add vrf red 2001:db8:112::/64 dev dummy6 src 2001:db8:101::12
2303	# src address and nexthop device in different VRF
2304	$IP route add 2001:db8:113::/64 dev lo src 2001:db8:101::13
2305	$IP route add vrf red 2001:db8:113::/64 dev lo src 2001:db8:101::13
2306	# table ID 0
2307	$IP route add table 0 2001:db8:115::/64 via 2001:db8:101::2 src 2001:db8:101::15
2308	# Link local source route
2309	$IP route add 2001:db8:116::/64 dev dummy2 src fe80::1
2310	$IP route add 2001:db8:117::/64 dev dummy3 src fe80::1
2311	set +e
2312
2313	echo "    Single device using src address"
2314
2315	$IP addr del dev dummy1 2001:db8:101::10/64
2316	$IP -6 route show | grep -q "src 2001:db8:101::10 "
2317	log_test $? 1 "Prefsrc removed when src address removed on other device"
2318
2319	echo "    Two devices with the same source address"
2320
2321	$IP addr del dev dummy1 2001:db8:101::11/64
2322	$IP -6 route show | grep -q "src 2001:db8:101::11 "
2323	log_test $? 0 "Prefsrc not removed when src address exist on other device"
2324
2325	$IP addr del dev dummy2 2001:db8:101::11/64
2326	$IP -6 route show | grep -q "src 2001:db8:101::11 "
2327	log_test $? 1 "Prefsrc removed when src address removed on all devices"
2328
2329	echo "    VRF with single device using src address"
2330
2331	$IP addr del dev dummy4 2001:db8:101::10/64
2332	$IP -6 route show vrf red | grep -q "src 2001:db8:101::10 "
2333	log_test $? 1 "Prefsrc removed when src address removed on other device"
2334
2335	echo "    VRF with two devices using src address"
2336
2337	$IP addr del dev dummy4 2001:db8:101::11/64
2338	$IP -6 route show vrf red | grep -q "src 2001:db8:101::11 "
2339	log_test $? 0 "Prefsrc not removed when src address exist on other device"
2340
2341	$IP addr del dev dummy5 2001:db8:101::11/64
2342	$IP -6 route show vrf red | grep -q "src 2001:db8:101::11 "
2343	log_test $? 1 "Prefsrc removed when src address removed on all devices"
2344
2345	echo "    src address and nexthop dev in same VRF"
2346
2347	$IP addr del dev dummy4 2001:db8:101::12/64
2348	$IP -6 route show vrf red | grep -q "src 2001:db8:101::12 "
2349	log_test $? 1 "Prefsrc removed from VRF when source address deleted"
2350	$IP -6 route show | grep -q " src 2001:db8:101::12 "
2351	log_test $? 0 "Prefsrc in default VRF not removed"
2352
2353	$IP addr add dev dummy4 2001:db8:101::12/64
2354	$IP route replace vrf red 2001:db8:112::/64 dev dummy6 src 2001:db8:101::12
2355	$IP addr del dev dummy1 2001:db8:101::12/64
2356	$IP -6 route show vrf red | grep -q "src 2001:db8:101::12 "
2357	log_test $? 0 "Prefsrc not removed from VRF when source address exist"
2358	$IP -6 route show | grep -q " src 2001:db8:101::12 "
2359	log_test $? 1 "Prefsrc in default VRF removed"
2360
2361	echo "    src address and nexthop device in different VRF"
2362
2363	$IP addr del dev dummy4 2001:db8:101::13/64
2364	$IP -6 route show vrf red | grep -q "src 2001:db8:101::13 "
2365	log_test $? 0 "Prefsrc not removed from VRF when nexthop dev in diff VRF"
2366	$IP -6 route show | grep -q "src 2001:db8:101::13 "
2367	log_test $? 0 "Prefsrc not removed in default VRF"
2368
2369	$IP addr add dev dummy4 2001:db8:101::13/64
2370	$IP addr del dev dummy1 2001:db8:101::13/64
2371	$IP -6 route show vrf red | grep -q "src 2001:db8:101::13 "
2372	log_test $? 1 "Prefsrc removed from VRF when nexthop dev in diff VRF"
2373	$IP -6 route show | grep -q "src 2001:db8:101::13 "
2374	log_test $? 1 "Prefsrc removed in default VRF"
2375
2376	echo "    Table ID 0"
2377
2378	$IP addr del dev dummy1 2001:db8:101::15/64
2379	$IP -6 route show | grep -q "src 2001:db8:101::15"
2380	log_test $? 1 "Prefsrc removed from default VRF when source address deleted"
2381
2382	echo "    Link local source route"
2383	$IP addr del dev dummy1 fe80::1/128
2384	$IP -6 route show | grep -q "2001:db8:116::/64 dev dummy2 src fe80::1"
2385	log_test $? 0 "Prefsrc not removed when delete ll addr from other dev"
2386	$IP addr del dev dummy2 fe80::1/128
2387	$IP -6 route show | grep -q "2001:db8:116::/64 dev dummy2 src fe80::1"
2388	log_test $? 1 "Prefsrc removed when delete ll addr"
2389	$IP -6 route show | grep -q "2001:db8:117::/64 dev dummy3 src fe80::1"
2390	log_test $? 0 "Prefsrc not removed when delete ll addr from other dev"
2391	$IP addr add dev dummy1 fe80::1/128
2392	$IP addr del dev dummy3 fe80::1/128
2393	$IP -6 route show | grep -q "2001:db8:117::/64 dev dummy3 src fe80::1"
2394	log_test $? 1 "Prefsrc removed even ll addr still exist on other dev"
2395
2396	for i in $(seq 6); do
2397		$IP li del dummy${i}
2398	done
2399	cleanup
2400}
2401
2402ipv4_route_v6_gw_test()
2403{
2404	local rc
2405
2406	echo
2407	echo "IPv4 route with IPv6 gateway tests"
2408
2409	route_setup
2410	sleep 2
2411
2412	#
2413	# single path route
2414	#
2415	run_cmd "$IP ro add 172.16.104.0/24 via inet6 2001:db8:101::2"
2416	rc=$?
2417	log_test $rc 0 "Single path route with IPv6 gateway"
2418	if [ $rc -eq 0 ]; then
2419		check_route "172.16.104.0/24 via inet6 2001:db8:101::2 dev veth1"
2420	fi
2421
2422	run_cmd "ip netns exec $ns1 ping -w1 -c1 172.16.104.1"
2423	log_test $rc 0 "Single path route with IPv6 gateway - ping"
2424
2425	run_cmd "$IP ro del 172.16.104.0/24 via inet6 2001:db8:101::2"
2426	rc=$?
2427	log_test $rc 0 "Single path route delete"
2428	if [ $rc -eq 0 ]; then
2429		check_route "172.16.112.0/24"
2430	fi
2431
2432	#
2433	# multipath - v6 then v4
2434	#
2435	run_cmd "$IP ro add 172.16.104.0/24 nexthop via inet6 2001:db8:101::2 dev veth1 nexthop via 172.16.103.2 dev veth3"
2436	rc=$?
2437	log_test $rc 0 "Multipath route add - v6 nexthop then v4"
2438	if [ $rc -eq 0 ]; then
2439		check_route "172.16.104.0/24 nexthop via inet6 2001:db8:101::2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1"
2440	fi
2441
2442	run_cmd "$IP ro del 172.16.104.0/24 nexthop via 172.16.103.2 dev veth3 nexthop via inet6 2001:db8:101::2 dev veth1"
2443	log_test $? 2 "    Multipath route delete - nexthops in wrong order"
2444
2445	run_cmd "$IP ro del 172.16.104.0/24 nexthop via inet6 2001:db8:101::2 dev veth1 nexthop via 172.16.103.2 dev veth3"
2446	log_test $? 0 "    Multipath route delete exact match"
2447
2448	#
2449	# multipath - v4 then v6
2450	#
2451	run_cmd "$IP ro add 172.16.104.0/24 nexthop via 172.16.103.2 dev veth3 nexthop via inet6 2001:db8:101::2 dev veth1"
2452	rc=$?
2453	log_test $rc 0 "Multipath route add - v4 nexthop then v6"
2454	if [ $rc -eq 0 ]; then
2455		check_route "172.16.104.0/24 nexthop via 172.16.103.2 dev veth3 weight 1 nexthop via inet6 2001:db8:101::2 dev veth1 weight 1"
2456	fi
2457
2458	run_cmd "$IP ro del 172.16.104.0/24 nexthop via inet6 2001:db8:101::2 dev veth1 nexthop via 172.16.103.2 dev veth3"
2459	log_test $? 2 "    Multipath route delete - nexthops in wrong order"
2460
2461	run_cmd "$IP ro del 172.16.104.0/24 nexthop via 172.16.103.2 dev veth3 nexthop via inet6 2001:db8:101::2 dev veth1"
2462	log_test $? 0 "    Multipath route delete exact match"
2463
2464	route_cleanup
2465}
2466
2467socat_check()
2468{
2469	if [ ! -x "$(command -v socat)" ]; then
2470		echo "socat command not found. Skipping test"
2471		return 1
2472	fi
2473
2474	return 0
2475}
2476
2477iptables_check()
2478{
2479	iptables -t mangle -L OUTPUT &> /dev/null
2480	if [ $? -ne 0 ]; then
2481		echo "iptables configuration not supported. Skipping test"
2482		return 1
2483	fi
2484
2485	return 0
2486}
2487
2488ip6tables_check()
2489{
2490	ip6tables -t mangle -L OUTPUT &> /dev/null
2491	if [ $? -ne 0 ]; then
2492		echo "ip6tables configuration not supported. Skipping test"
2493		return 1
2494	fi
2495
2496	return 0
2497}
2498
2499ipv4_mangle_test()
2500{
2501	local rc
2502
2503	echo
2504	echo "IPv4 mangling tests"
2505
2506	socat_check || return 1
2507	iptables_check || return 1
2508
2509	route_setup
2510	sleep 2
2511
2512	local tmp_file=$(mktemp)
2513	ip netns exec $ns2 socat UDP4-LISTEN:54321,fork $tmp_file &
2514
2515	# Add a FIB rule and a route that will direct our connection to the
2516	# listening server.
2517	$IP rule add pref 100 ipproto udp sport 12345 dport 54321 table 123
2518	$IP route add table 123 172.16.101.0/24 dev veth1
2519
2520	# Add an unreachable route to the main table that will block our
2521	# connection in case the FIB rule is not hit.
2522	$IP route add unreachable 172.16.101.2/32
2523
2524	run_cmd "echo a | $NS_EXEC socat STDIN UDP4:172.16.101.2:54321,sourceport=12345"
2525	log_test $? 0 "    Connection with correct parameters"
2526
2527	run_cmd "echo a | $NS_EXEC socat STDIN UDP4:172.16.101.2:54321,sourceport=11111"
2528	log_test $? 1 "    Connection with incorrect parameters"
2529
2530	# Add a mangling rule and make sure connection is still successful.
2531	$NS_EXEC iptables -t mangle -A OUTPUT -j MARK --set-mark 1
2532
2533	run_cmd "echo a | $NS_EXEC socat STDIN UDP4:172.16.101.2:54321,sourceport=12345"
2534	log_test $? 0 "    Connection with correct parameters - mangling"
2535
2536	# Delete the mangling rule and make sure connection is still
2537	# successful.
2538	$NS_EXEC iptables -t mangle -D OUTPUT -j MARK --set-mark 1
2539
2540	run_cmd "echo a | $NS_EXEC socat STDIN UDP4:172.16.101.2:54321,sourceport=12345"
2541	log_test $? 0 "    Connection with correct parameters - no mangling"
2542
2543	# Verify connections were indeed successful on server side.
2544	[[ $(cat $tmp_file | wc -l) -eq 3 ]]
2545	log_test $? 0 "    Connection check - server side"
2546
2547	$IP route del unreachable 172.16.101.2/32
2548	$IP route del table 123 172.16.101.0/24 dev veth1
2549	$IP rule del pref 100
2550
2551	kill_process %%
2552	rm $tmp_file
2553
2554	route_cleanup
2555}
2556
2557ipv6_mangle_test()
2558{
2559	local rc
2560
2561	echo
2562	echo "IPv6 mangling tests"
2563
2564	socat_check || return 1
2565	ip6tables_check || return 1
2566
2567	route_setup
2568	sleep 2
2569
2570	local tmp_file=$(mktemp)
2571	ip netns exec $ns2 socat UDP6-LISTEN:54321,fork $tmp_file &
2572
2573	# Add a FIB rule and a route that will direct our connection to the
2574	# listening server.
2575	$IP -6 rule add pref 100 ipproto udp sport 12345 dport 54321 table 123
2576	$IP -6 route add table 123 2001:db8:101::/64 dev veth1
2577
2578	# Add an unreachable route to the main table that will block our
2579	# connection in case the FIB rule is not hit.
2580	$IP -6 route add unreachable 2001:db8:101::2/128
2581
2582	run_cmd "echo a | $NS_EXEC socat STDIN UDP6:[2001:db8:101::2]:54321,sourceport=12345"
2583	log_test $? 0 "    Connection with correct parameters"
2584
2585	run_cmd "echo a | $NS_EXEC socat STDIN UDP6:[2001:db8:101::2]:54321,sourceport=11111"
2586	log_test $? 1 "    Connection with incorrect parameters"
2587
2588	# Add a mangling rule and make sure connection is still successful.
2589	$NS_EXEC ip6tables -t mangle -A OUTPUT -j MARK --set-mark 1
2590
2591	run_cmd "echo a | $NS_EXEC socat STDIN UDP6:[2001:db8:101::2]:54321,sourceport=12345"
2592	log_test $? 0 "    Connection with correct parameters - mangling"
2593
2594	# Delete the mangling rule and make sure connection is still
2595	# successful.
2596	$NS_EXEC ip6tables -t mangle -D OUTPUT -j MARK --set-mark 1
2597
2598	run_cmd "echo a | $NS_EXEC socat STDIN UDP6:[2001:db8:101::2]:54321,sourceport=12345"
2599	log_test $? 0 "    Connection with correct parameters - no mangling"
2600
2601	# Verify connections were indeed successful on server side.
2602	[[ $(cat $tmp_file | wc -l) -eq 3 ]]
2603	log_test $? 0 "    Connection check - server side"
2604
2605	$IP -6 route del unreachable 2001:db8:101::2/128
2606	$IP -6 route del table 123 2001:db8:101::/64 dev veth1
2607	$IP -6 rule del pref 100
2608
2609	kill_process %%
2610	rm $tmp_file
2611
2612	route_cleanup
2613}
2614
2615ip_neigh_get_check()
2616{
2617	ip neigh help 2>&1 | grep -q 'ip neigh get'
2618	if [ $? -ne 0 ]; then
2619		echo "iproute2 command does not support neigh get. Skipping test"
2620		return 1
2621	fi
2622
2623	return 0
2624}
2625
2626ipv4_bcast_neigh_test()
2627{
2628	local rc
2629
2630	echo
2631	echo "IPv4 broadcast neighbour tests"
2632
2633	ip_neigh_get_check || return 1
2634
2635	setup
2636
2637	set -e
2638	run_cmd "$IP neigh add 192.0.2.111 lladdr 00:11:22:33:44:55 nud perm dev dummy0"
2639	run_cmd "$IP neigh add 192.0.2.255 lladdr 00:11:22:33:44:55 nud perm dev dummy0"
2640
2641	run_cmd "$IP neigh get 192.0.2.111 dev dummy0"
2642	run_cmd "$IP neigh get 192.0.2.255 dev dummy0"
2643
2644	run_cmd "$IP address add 192.0.2.1/24 broadcast 192.0.2.111 dev dummy0"
2645
2646	run_cmd "$IP neigh add 203.0.113.111 nud failed dev dummy0"
2647	run_cmd "$IP neigh add 203.0.113.255 nud failed dev dummy0"
2648
2649	run_cmd "$IP neigh get 203.0.113.111 dev dummy0"
2650	run_cmd "$IP neigh get 203.0.113.255 dev dummy0"
2651
2652	run_cmd "$IP address add 203.0.113.1/24 broadcast 203.0.113.111 dev dummy0"
2653	set +e
2654
2655	run_cmd "$IP neigh get 192.0.2.111 dev dummy0"
2656	log_test $? 0 "Resolved neighbour for broadcast address"
2657
2658	run_cmd "$IP neigh get 192.0.2.255 dev dummy0"
2659	log_test $? 0 "Resolved neighbour for network broadcast address"
2660
2661	run_cmd "$IP neigh get 203.0.113.111 dev dummy0"
2662	log_test $? 2 "Unresolved neighbour for broadcast address"
2663
2664	run_cmd "$IP neigh get 203.0.113.255 dev dummy0"
2665	log_test $? 2 "Unresolved neighbour for network broadcast address"
2666
2667	cleanup
2668}
2669
2670mpath_dep_check()
2671{
2672	if [ ! -x "$(command -v mausezahn)" ]; then
2673		echo "mausezahn command not found. Skipping test"
2674		return 1
2675	fi
2676
2677	if [ ! -x "$(command -v jq)" ]; then
2678		echo "jq command not found. Skipping test"
2679		return 1
2680	fi
2681
2682	if [ ! -x "$(command -v bc)" ]; then
2683		echo "bc command not found. Skipping test"
2684		return 1
2685	fi
2686
2687	if [ ! -x "$(command -v perf)" ]; then
2688		echo "perf command not found. Skipping test"
2689		return 1
2690	fi
2691
2692	perf list fib:* | grep -q fib_table_lookup
2693	if [ $? -ne 0 ]; then
2694		echo "IPv4 FIB tracepoint not found. Skipping test"
2695		return 1
2696	fi
2697
2698	perf list fib6:* | grep -q fib6_table_lookup
2699	if [ $? -ne 0 ]; then
2700		echo "IPv6 FIB tracepoint not found. Skipping test"
2701		return 1
2702	fi
2703
2704	return 0
2705}
2706
2707link_stats_get()
2708{
2709	local ns=$1; shift
2710	local dev=$1; shift
2711	local dir=$1; shift
2712	local stat=$1; shift
2713
2714	ip -n $ns -j -s link show dev $dev \
2715		| jq '.[]["stats64"]["'$dir'"]["'$stat'"]'
2716}
2717
2718list_rcv_eval()
2719{
2720	local file=$1; shift
2721	local expected=$1; shift
2722
2723	local count=$(tail -n 1 $file | jq '.["counter-value"] | tonumber | floor')
2724	local ratio=$(echo "scale=2; $count / $expected" | bc -l)
2725	local res=$(echo "$ratio >= 0.95" | bc)
2726	[[ $res -eq 1 ]]
2727	log_test $? 0 "Multipath route hit ratio ($ratio)"
2728}
2729
2730ipv4_mpath_list_test()
2731{
2732	echo
2733	echo "IPv4 multipath list receive tests"
2734
2735	mpath_dep_check || return 1
2736
2737	route_setup
2738
2739	set -e
2740	run_cmd "ip netns exec $ns1 ethtool -K veth1 tcp-segmentation-offload off"
2741
2742	run_cmd "ip netns exec $ns2 bash -c \"echo 20000 > /sys/class/net/veth2/gro_flush_timeout\""
2743	run_cmd "ip netns exec $ns2 bash -c \"echo 1 > /sys/class/net/veth2/napi_defer_hard_irqs\""
2744	run_cmd "ip netns exec $ns2 ethtool -K veth2 generic-receive-offload on"
2745	run_cmd "ip -n $ns2 link add name nh1 up type dummy"
2746	run_cmd "ip -n $ns2 link add name nh2 up type dummy"
2747	run_cmd "ip -n $ns2 address add 172.16.201.1/24 dev nh1"
2748	run_cmd "ip -n $ns2 address add 172.16.202.1/24 dev nh2"
2749	run_cmd "ip -n $ns2 neigh add 172.16.201.2 lladdr 00:11:22:33:44:55 nud perm dev nh1"
2750	run_cmd "ip -n $ns2 neigh add 172.16.202.2 lladdr 00:aa:bb:cc:dd:ee nud perm dev nh2"
2751	run_cmd "ip -n $ns2 route add 203.0.113.0/24
2752		nexthop via 172.16.201.2 nexthop via 172.16.202.2"
2753	run_cmd "ip netns exec $ns2 sysctl -qw net.ipv4.fib_multipath_hash_policy=1"
2754	set +e
2755
2756	local dmac=$(ip -n $ns2 -j link show dev veth2 | jq -r '.[]["address"]')
2757	local tmp_file=$(mktemp)
2758	local cmd="ip netns exec $ns1 mausezahn veth1 -a own -b $dmac
2759		-A 172.16.101.1 -B 203.0.113.1 -t udp 'sp=12345,dp=0-65535' -q"
2760
2761	# Packets forwarded in a list using a multipath route must not reuse a
2762	# cached result so that a flow always hits the same nexthop. In other
2763	# words, the FIB lookup tracepoint needs to be triggered for every
2764	# packet.
2765	local t0_rx_pkts=$(link_stats_get $ns2 veth2 rx packets)
2766	run_cmd "perf stat -a -e fib:fib_table_lookup --filter 'err == 0' -j -o $tmp_file -- $cmd"
2767	local t1_rx_pkts=$(link_stats_get $ns2 veth2 rx packets)
2768	local diff=$(echo $t1_rx_pkts - $t0_rx_pkts | bc -l)
2769	list_rcv_eval $tmp_file $diff
2770
2771	rm $tmp_file
2772	route_cleanup
2773}
2774
2775ipv6_mpath_list_test()
2776{
2777	echo
2778	echo "IPv6 multipath list receive tests"
2779
2780	mpath_dep_check || return 1
2781
2782	route_setup
2783
2784	set -e
2785	run_cmd "ip netns exec $ns1 ethtool -K veth1 tcp-segmentation-offload off"
2786
2787	run_cmd "ip netns exec $ns2 bash -c \"echo 20000 > /sys/class/net/veth2/gro_flush_timeout\""
2788	run_cmd "ip netns exec $ns2 bash -c \"echo 1 > /sys/class/net/veth2/napi_defer_hard_irqs\""
2789	run_cmd "ip netns exec $ns2 ethtool -K veth2 generic-receive-offload on"
2790	run_cmd "ip -n $ns2 link add name nh1 up type dummy"
2791	run_cmd "ip -n $ns2 link add name nh2 up type dummy"
2792	run_cmd "ip -n $ns2 -6 address add 2001:db8:201::1/64 dev nh1"
2793	run_cmd "ip -n $ns2 -6 address add 2001:db8:202::1/64 dev nh2"
2794	run_cmd "ip -n $ns2 -6 neigh add 2001:db8:201::2 lladdr 00:11:22:33:44:55 nud perm dev nh1"
2795	run_cmd "ip -n $ns2 -6 neigh add 2001:db8:202::2 lladdr 00:aa:bb:cc:dd:ee nud perm dev nh2"
2796	run_cmd "ip -n $ns2 -6 route add 2001:db8:301::/64
2797		nexthop via 2001:db8:201::2 nexthop via 2001:db8:202::2"
2798	run_cmd "ip netns exec $ns2 sysctl -qw net.ipv6.fib_multipath_hash_policy=1"
2799	set +e
2800
2801	local dmac=$(ip -n $ns2 -j link show dev veth2 | jq -r '.[]["address"]')
2802	local tmp_file=$(mktemp)
2803	local cmd="ip netns exec $ns1 mausezahn -6 veth1 -a own -b $dmac
2804		-A 2001:db8:101::1 -B 2001:db8:301::1 -t udp 'sp=12345,dp=0-65535' -q"
2805
2806	# Packets forwarded in a list using a multipath route must not reuse a
2807	# cached result so that a flow always hits the same nexthop. In other
2808	# words, the FIB lookup tracepoint needs to be triggered for every
2809	# packet.
2810	local t0_rx_pkts=$(link_stats_get $ns2 veth2 rx packets)
2811	run_cmd "perf stat -a -e fib6:fib6_table_lookup --filter 'err == 0' -j -o $tmp_file -- $cmd"
2812	local t1_rx_pkts=$(link_stats_get $ns2 veth2 rx packets)
2813	local diff=$(echo $t1_rx_pkts - $t0_rx_pkts | bc -l)
2814	list_rcv_eval $tmp_file $diff
2815
2816	rm $tmp_file
2817	route_cleanup
2818}
2819
2820tc_set_flower_counter__saddr_syn() {
2821	tc_set_flower_counter $1 $2 $3 "src_ip $4 ip_proto tcp tcp_flags 0x2"
2822}
2823
2824ip_mpath_balance_dep_check()
2825{
2826	if [ ! -x "$(command -v socat)" ]; then
2827		echo "socat command not found. Skipping test"
2828		return 1
2829	fi
2830
2831	if [ ! -x "$(command -v jq)" ]; then
2832		echo "jq command not found. Skipping test"
2833		return 1
2834	fi
2835}
2836
2837ip_mpath_balance() {
2838	local -r ipver=$1
2839	local -r daddr=$2
2840	local -r num_conn=20
2841
2842	for i in $(seq 1 $num_conn); do
2843		ip netns exec $ns3 socat $ipver TCP-LISTEN:8000 STDIO >/dev/null &
2844		sleep 0.02
2845		echo -n a | ip netns exec $ns1 socat $ipver STDIO TCP:$daddr:8000
2846	done
2847
2848	local -r syn0="$(tc_get_flower_counter $ns1 veth1)"
2849	local -r syn1="$(tc_get_flower_counter $ns1 veth3)"
2850	local -r syns=$((syn0+syn1))
2851
2852	[ "$VERBOSE" = "1" ] && echo "multipath: syns seen: ($syn0,$syn1)"
2853
2854	[[ $syns -ge $num_conn ]] && [[ $syn0 -gt 0 ]] && [[ $syn1 -gt 0 ]]
2855}
2856
2857ipv4_mpath_balance_test()
2858{
2859	echo
2860	echo "IPv4 multipath load balance test"
2861
2862	ip_mpath_balance_dep_check || return 1
2863	forwarding_setup
2864
2865	$IP route add 172.16.105.1 \
2866		nexthop via 172.16.101.2 \
2867		nexthop via 172.16.103.2
2868
2869	ip netns exec $ns1 \
2870		sysctl -q -w net.ipv4.fib_multipath_hash_policy=1
2871
2872	tc_set_flower_counter__saddr_syn $ns1 4 veth1 172.16.101.1
2873	tc_set_flower_counter__saddr_syn $ns1 4 veth3 172.16.103.1
2874
2875	ip_mpath_balance -4 172.16.105.1
2876
2877	log_test $? 0 "IPv4 multipath loadbalance"
2878
2879	forwarding_cleanup
2880}
2881
2882get_route_dev_src()
2883{
2884	local pfx="$1"
2885	local src="$2"
2886	local out
2887
2888	if out=$($IP -j route get "$pfx" from "$src" | jq -re ".[0].dev"); then
2889		echo "$out"
2890	fi
2891}
2892
2893ipv4_mpath_preferred()
2894{
2895	local src_ip=$1
2896	local pref_dev=$2
2897	local dev routes
2898	local route0=0
2899	local route1=0
2900	local pref_route=0
2901	num_routes=254
2902
2903	for i in $(seq 1 $num_routes) ; do
2904		dev=$(get_route_dev_src 172.16.105.$i $src_ip)
2905		if [ "$dev" = "$pref_dev" ]; then
2906			pref_route=$((pref_route+1))
2907		elif [ "$dev" = "veth1" ]; then
2908			route0=$((route0+1))
2909		elif [ "$dev" = "veth3" ]; then
2910			route1=$((route1+1))
2911		fi
2912	done
2913
2914	routes=$((route0+route1))
2915
2916	[ "$VERBOSE" = "1" ] && echo "multipath: routes seen: ($route0,$route1,$pref_route)"
2917
2918	if [ x"$pref_dev" = x"" ]; then
2919		[[ $routes -ge $num_routes ]] && [[ $route0 -gt 0 ]] && [[ $route1 -gt 0 ]]
2920	else
2921		[[ $pref_route -ge $num_routes ]]
2922	fi
2923
2924}
2925
2926ipv4_mpath_balance_preferred_test()
2927{
2928	echo
2929	echo "IPv4 multipath load balance preferred route"
2930
2931	forwarding_setup
2932
2933	$IP route add 172.16.105.0/24 \
2934		nexthop via 172.16.101.2 \
2935		nexthop via 172.16.103.2
2936
2937	ipv4_mpath_preferred 172.16.101.1 veth1
2938	log_test $? 0 "IPv4 multipath loadbalance from veth1"
2939
2940	ipv4_mpath_preferred 172.16.103.1 veth3
2941	log_test $? 0 "IPv4 multipath loadbalance from veth3"
2942
2943	ipv4_mpath_preferred 198.51.100.1
2944	log_test $? 0 "IPv4 multipath loadbalance from dummy"
2945
2946	forwarding_cleanup
2947}
2948
2949ipv6_mpath_balance_test()
2950{
2951	echo
2952	echo "IPv6 multipath load balance test"
2953
2954	ip_mpath_balance_dep_check || return 1
2955	forwarding_setup
2956
2957	$IP route add 2001:db8:105::1\
2958		nexthop via 2001:db8:101::2 \
2959		nexthop via 2001:db8:103::2
2960
2961	ip netns exec $ns1 \
2962		sysctl -q -w net.ipv6.fib_multipath_hash_policy=1
2963
2964	tc_set_flower_counter__saddr_syn $ns1 6 veth1 2001:db8:101::1
2965	tc_set_flower_counter__saddr_syn $ns1 6 veth3 2001:db8:103::1
2966
2967	ip_mpath_balance -6 "[2001:db8:105::1]"
2968
2969	log_test $? 0 "IPv6 multipath loadbalance"
2970
2971	forwarding_cleanup
2972}
2973
2974################################################################################
2975# usage
2976
2977usage()
2978{
2979	cat <<EOF
2980usage: ${0##*/} OPTS
2981
2982        -t <test>   Test(s) to run (default: all)
2983                    (options: $TESTS)
2984        -p          Pause on fail
2985        -P          Pause after each test before cleanup
2986        -v          verbose mode (show commands and output)
2987EOF
2988}
2989
2990################################################################################
2991# main
2992
2993trap cleanup EXIT
2994
2995while getopts :t:pPhv o
2996do
2997	case $o in
2998		t) TESTS=$OPTARG;;
2999		p) PAUSE_ON_FAIL=yes;;
3000		P) PAUSE=yes;;
3001		v) VERBOSE=$(($VERBOSE + 1));;
3002		h) usage; exit 0;;
3003		*) usage; exit 1;;
3004	esac
3005done
3006
3007PEER_CMD="ip netns exec ${PEER_NS}"
3008
3009# make sure we don't pause twice
3010[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
3011
3012if [ "$(id -u)" -ne 0 ];then
3013	echo "SKIP: Need root privileges"
3014	exit $ksft_skip;
3015fi
3016
3017if [ ! -x "$(command -v ip)" ]; then
3018	echo "SKIP: Could not run test without ip tool"
3019	exit $ksft_skip
3020fi
3021
3022ip route help 2>&1 | grep -q fibmatch
3023if [ $? -ne 0 ]; then
3024	echo "SKIP: iproute2 too old, missing fibmatch"
3025	exit $ksft_skip
3026fi
3027
3028# start clean
3029cleanup &> /dev/null
3030
3031for t in $TESTS
3032do
3033	case $t in
3034	fib_unreg_test|unregister)	fib_unreg_test;;
3035	fib_down_test|down)		fib_down_test;;
3036	fib_carrier_test|carrier)	fib_carrier_test;;
3037	fib_rp_filter_test|rp_filter)	fib_rp_filter_test;;
3038	fib_nexthop_test|nexthop)	fib_nexthop_test;;
3039	fib_notify_test|ipv4_notify)	fib_notify_test;;
3040	fib6_notify_test|ipv6_notify)	fib6_notify_test;;
3041	fib_suppress_test|suppress)	fib_suppress_test;;
3042	ipv6_route_test|ipv6_rt)	ipv6_route_test;;
3043	ipv4_route_test|ipv4_rt)	ipv4_route_test;;
3044	ipv6_addr_metric)		ipv6_addr_metric_test;;
3045	ipv4_addr_metric)		ipv4_addr_metric_test;;
3046	ipv4_del_addr)			ipv4_del_addr_test;;
3047	ipv6_del_addr)			ipv6_del_addr_test;;
3048	ipv6_route_metrics)		ipv6_route_metrics_test;;
3049	ipv4_route_metrics)		ipv4_route_metrics_test;;
3050	ipv4_route_v6_gw)		ipv4_route_v6_gw_test;;
3051	ipv4_mangle)			ipv4_mangle_test;;
3052	ipv6_mangle)			ipv6_mangle_test;;
3053	ipv4_bcast_neigh)		ipv4_bcast_neigh_test;;
3054	fib6_gc_test|ipv6_gc)		fib6_gc_test;;
3055	ipv4_mpath_list)		ipv4_mpath_list_test;;
3056	ipv6_mpath_list)		ipv6_mpath_list_test;;
3057	ipv4_mpath_balance)		ipv4_mpath_balance_test;;
3058	ipv6_mpath_balance)		ipv6_mpath_balance_test;;
3059	ipv4_mpath_balance_preferred)	ipv4_mpath_balance_preferred_test;;
3060	fib6_ra_to_static)		fib6_ra_to_static;;
3061	fib6_temp_addr_renewal)		fib6_temp_addr_renewal;;
3062
3063	help) echo "Test names: $TESTS"; exit 0;;
3064	esac
3065done
3066
3067if [ "$TESTS" != "none" ]; then
3068	printf "\nTests passed: %3d\n" ${nsuccess}
3069	printf "Tests failed: %3d\n"   ${nfail}
3070fi
3071
3072exit $ret
3073