xref: /linux/tools/testing/selftests/net/fib_tests.sh (revision 3e9e84e92c9c2eec396ee62a2e47b85781520c57)
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"
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 dummy0)
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	set +e
1593
1594	cleanup &> /dev/null
1595}
1596
1597# add route for a prefix, flushing any existing routes first
1598# expected to be the first step of a test
1599add_route()
1600{
1601	local pfx="$1"
1602	local nh="$2"
1603	local out
1604
1605	if [ "$VERBOSE" = "1" ]; then
1606		echo
1607		echo "    ##################################################"
1608		echo
1609	fi
1610
1611	run_cmd "$IP ro flush ${pfx}"
1612	[ $? -ne 0 ] && exit 1
1613
1614	out=$($IP ro ls match ${pfx})
1615	if [ -n "$out" ]; then
1616		echo "Failed to flush routes for prefix used for tests."
1617		exit 1
1618	fi
1619
1620	run_cmd "$IP ro add ${pfx} ${nh}"
1621	if [ $? -ne 0 ]; then
1622		echo "Failed to add initial route for test."
1623		exit 1
1624	fi
1625}
1626
1627# add initial route - used in replace route tests
1628add_initial_route()
1629{
1630	add_route "172.16.104.0/24" "$1"
1631}
1632
1633check_route()
1634{
1635	local pfx
1636	local expected="$1"
1637	local out
1638
1639	set -- $expected
1640	pfx=$1
1641	[ "${pfx}" = "unreachable" ] && pfx=$2
1642
1643	out=$($IP ro ls match ${pfx})
1644	check_expected "${out}" "${expected}"
1645}
1646
1647# assumption is that basic add of a single path route works
1648# otherwise just adding an address on an interface is broken
1649ipv4_rt_add()
1650{
1651	local rc
1652
1653	echo
1654	echo "IPv4 route add / append tests"
1655
1656	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1657	add_route "172.16.104.0/24" "via 172.16.101.2"
1658	run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.2"
1659	log_test $? 2 "Attempt to add duplicate route - gw"
1660
1661	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1662	add_route "172.16.104.0/24" "via 172.16.101.2"
1663	run_cmd "$IP ro add 172.16.104.0/24 dev veth3"
1664	log_test $? 2 "Attempt to add duplicate route - dev only"
1665
1666	# route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL
1667	add_route "172.16.104.0/24" "via 172.16.101.2"
1668	run_cmd "$IP ro add unreachable 172.16.104.0/24"
1669	log_test $? 2 "Attempt to add duplicate route - reject route"
1670
1671	# iproute2 prepend only sets NLM_F_CREATE
1672	# - adds a new route; does NOT convert existing route to ECMP
1673	add_route "172.16.104.0/24" "via 172.16.101.2"
1674	run_cmd "$IP ro prepend 172.16.104.0/24 via 172.16.103.2"
1675	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"
1676	log_test $? 0 "Add new nexthop for existing prefix"
1677
1678	# route append with same prefix adds a new route
1679	# - iproute2 sets NLM_F_CREATE | NLM_F_APPEND
1680	add_route "172.16.104.0/24" "via 172.16.101.2"
1681	run_cmd "$IP ro append 172.16.104.0/24 via 172.16.103.2"
1682	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"
1683	log_test $? 0 "Append nexthop to existing route - gw"
1684
1685	add_route "172.16.104.0/24" "via 172.16.101.2"
1686	run_cmd "$IP ro append 172.16.104.0/24 dev veth3"
1687	check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 172.16.104.0/24 dev veth3 scope link"
1688	log_test $? 0 "Append nexthop to existing route - dev only"
1689
1690	add_route "172.16.104.0/24" "via 172.16.101.2"
1691	run_cmd "$IP ro append unreachable 172.16.104.0/24"
1692	check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 unreachable 172.16.104.0/24"
1693	log_test $? 0 "Append nexthop to existing route - reject route"
1694
1695	run_cmd "$IP ro flush 172.16.104.0/24"
1696	run_cmd "$IP ro add unreachable 172.16.104.0/24"
1697	run_cmd "$IP ro append 172.16.104.0/24 via 172.16.103.2"
1698	check_route "unreachable 172.16.104.0/24 172.16.104.0/24 via 172.16.103.2 dev veth3"
1699	log_test $? 0 "Append nexthop to existing reject route - gw"
1700
1701	run_cmd "$IP ro flush 172.16.104.0/24"
1702	run_cmd "$IP ro add unreachable 172.16.104.0/24"
1703	run_cmd "$IP ro append 172.16.104.0/24 dev veth3"
1704	check_route "unreachable 172.16.104.0/24 172.16.104.0/24 dev veth3 scope link"
1705	log_test $? 0 "Append nexthop to existing reject route - dev only"
1706
1707	# insert mpath directly
1708	add_route "172.16.104.0/24" "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1709	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"
1710	log_test $? 0 "add multipath route"
1711
1712	add_route "172.16.104.0/24" "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1713	run_cmd "$IP ro add 172.16.104.0/24 nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1714	log_test $? 2 "Attempt to add duplicate multipath route"
1715
1716	# insert of a second route without append but different metric
1717	add_route "172.16.104.0/24" "via 172.16.101.2"
1718	run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.2 metric 512"
1719	rc=$?
1720	if [ $rc -eq 0 ]; then
1721		run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.3 metric 256"
1722		rc=$?
1723	fi
1724	log_test $rc 0 "Route add with different metrics"
1725
1726	run_cmd "$IP ro del 172.16.104.0/24 metric 512"
1727	rc=$?
1728	if [ $rc -eq 0 ]; then
1729		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"
1730		rc=$?
1731	fi
1732	log_test $rc 0 "Route delete with metric"
1733}
1734
1735ipv4_rt_replace_single()
1736{
1737	# single path with single path
1738	#
1739	add_initial_route "via 172.16.101.2"
1740	run_cmd "$IP ro replace 172.16.104.0/24 via 172.16.103.2"
1741	check_route "172.16.104.0/24 via 172.16.103.2 dev veth3"
1742	log_test $? 0 "Single path with single path"
1743
1744	# single path with multipath
1745	#
1746	add_initial_route "nexthop via 172.16.101.2"
1747	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.2"
1748	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"
1749	log_test $? 0 "Single path with multipath"
1750
1751	# single path with reject
1752	#
1753	add_initial_route "nexthop via 172.16.101.2"
1754	run_cmd "$IP ro replace unreachable 172.16.104.0/24"
1755	check_route "unreachable 172.16.104.0/24"
1756	log_test $? 0 "Single path with reject route"
1757
1758	# single path with single path using MULTIPATH attribute
1759	#
1760	add_initial_route "via 172.16.101.2"
1761	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.103.2"
1762	check_route "172.16.104.0/24 via 172.16.103.2 dev veth3"
1763	log_test $? 0 "Single path with single path via multipath attribute"
1764
1765	# route replace fails - invalid nexthop
1766	add_initial_route "via 172.16.101.2"
1767	run_cmd "$IP ro replace 172.16.104.0/24 via 2001:db8:104::2"
1768	if [ $? -eq 0 ]; then
1769		# previous command is expected to fail so if it returns 0
1770		# that means the test failed.
1771		log_test 0 1 "Invalid nexthop"
1772	else
1773		check_route "172.16.104.0/24 via 172.16.101.2 dev veth1"
1774		log_test $? 0 "Invalid nexthop"
1775	fi
1776
1777	# replace non-existent route
1778	# - note use of change versus replace since ip adds NLM_F_CREATE
1779	#   for replace
1780	add_initial_route "via 172.16.101.2"
1781	run_cmd "$IP ro change 172.16.105.0/24 via 172.16.101.2"
1782	log_test $? 2 "Single path - replace of non-existent route"
1783}
1784
1785ipv4_rt_replace_mpath()
1786{
1787	# multipath with multipath
1788	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1789	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.3"
1790	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"
1791	log_test $? 0 "Multipath with multipath"
1792
1793	# multipath with single
1794	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1795	run_cmd "$IP ro replace 172.16.104.0/24 via 172.16.101.3"
1796	check_route  "172.16.104.0/24 via 172.16.101.3 dev veth1"
1797	log_test $? 0 "Multipath with single path"
1798
1799	# multipath with single
1800	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1801	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3"
1802	check_route "172.16.104.0/24 via 172.16.101.3 dev veth1"
1803	log_test $? 0 "Multipath with single path via multipath attribute"
1804
1805	# multipath with reject
1806	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1807	run_cmd "$IP ro replace unreachable 172.16.104.0/24"
1808	check_route "unreachable 172.16.104.0/24"
1809	log_test $? 0 "Multipath with reject route"
1810
1811	# route replace fails - invalid nexthop 1
1812	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1813	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.111.3 nexthop via 172.16.103.3"
1814	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"
1815	log_test $? 0 "Multipath - invalid first nexthop"
1816
1817	# route replace fails - invalid nexthop 2
1818	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1819	run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.113.3"
1820	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"
1821	log_test $? 0 "Multipath - invalid second nexthop"
1822
1823	# multipath non-existent route
1824	add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2"
1825	run_cmd "$IP ro change 172.16.105.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.3"
1826	log_test $? 2 "Multipath - replace of non-existent route"
1827}
1828
1829ipv4_rt_replace()
1830{
1831	echo
1832	echo "IPv4 route replace tests"
1833
1834	ipv4_rt_replace_single
1835	ipv4_rt_replace_mpath
1836}
1837
1838# checks that cached input route on VRF port is deleted
1839# when VRF is deleted
1840ipv4_local_rt_cache()
1841{
1842	run_cmd "ip addr add 10.0.0.1/32 dev lo"
1843	run_cmd "setup_ns test-ns"
1844	run_cmd "ip link add veth-outside type veth peer name veth-inside"
1845	run_cmd "ip link add vrf-100 type vrf table 1100"
1846	run_cmd "ip link set veth-outside master vrf-100"
1847	run_cmd "ip link set veth-inside netns $test-ns"
1848	run_cmd "ip link set veth-outside up"
1849	run_cmd "ip link set vrf-100 up"
1850	run_cmd "ip route add 10.1.1.1/32 dev veth-outside table 1100"
1851	run_cmd "ip netns exec $test-ns ip link set veth-inside up"
1852	run_cmd "ip netns exec $test-ns ip addr add 10.1.1.1/32 dev veth-inside"
1853	run_cmd "ip netns exec $test-ns ip route add 10.0.0.1/32 dev veth-inside"
1854	run_cmd "ip netns exec $test-ns ip route add default via 10.0.0.1"
1855	run_cmd "ip netns exec $test-ns ping 10.0.0.1 -c 1 -i 1"
1856	run_cmd "ip link delete vrf-100"
1857
1858	# if we do not hang test is a success
1859	log_test $? 0 "Cached route removed from VRF port device"
1860}
1861
1862ipv4_rt_dsfield()
1863{
1864	echo
1865	echo "IPv4 route with dsfield tests"
1866
1867	run_cmd "$IP route flush 172.16.102.0/24"
1868
1869	# New routes should reject dsfield options that interfere with ECN
1870	run_cmd "$IP route add 172.16.102.0/24 dsfield 0x01 via 172.16.101.2"
1871	log_test $? 2 "Reject route with dsfield 0x01"
1872
1873	run_cmd "$IP route add 172.16.102.0/24 dsfield 0x02 via 172.16.101.2"
1874	log_test $? 2 "Reject route with dsfield 0x02"
1875
1876	run_cmd "$IP route add 172.16.102.0/24 dsfield 0x03 via 172.16.101.2"
1877	log_test $? 2 "Reject route with dsfield 0x03"
1878
1879	# A generic route that doesn't take DSCP into account
1880	run_cmd "$IP route add 172.16.102.0/24 via 172.16.101.2"
1881
1882	# A more specific route for DSCP 0x10
1883	run_cmd "$IP route add 172.16.102.0/24 dsfield 0x10 via 172.16.103.2"
1884
1885	# DSCP 0x10 should match the specific route, no matter the ECN bits
1886	$IP route get fibmatch 172.16.102.1 dsfield 0x10 | \
1887		grep -q "172.16.102.0/24 tos 0x10 via 172.16.103.2"
1888	log_test $? 0 "IPv4 route with DSCP and ECN:Not-ECT"
1889
1890	$IP route get fibmatch 172.16.102.1 dsfield 0x11 | \
1891		grep -q "172.16.102.0/24 tos 0x10 via 172.16.103.2"
1892	log_test $? 0 "IPv4 route with DSCP and ECN:ECT(1)"
1893
1894	$IP route get fibmatch 172.16.102.1 dsfield 0x12 | \
1895		grep -q "172.16.102.0/24 tos 0x10 via 172.16.103.2"
1896	log_test $? 0 "IPv4 route with DSCP and ECN:ECT(0)"
1897
1898	$IP route get fibmatch 172.16.102.1 dsfield 0x13 | \
1899		grep -q "172.16.102.0/24 tos 0x10 via 172.16.103.2"
1900	log_test $? 0 "IPv4 route with DSCP and ECN:CE"
1901
1902	# Unknown DSCP should match the generic route, no matter the ECN bits
1903	$IP route get fibmatch 172.16.102.1 dsfield 0x14 | \
1904		grep -q "172.16.102.0/24 via 172.16.101.2"
1905	log_test $? 0 "IPv4 route with unknown DSCP and ECN:Not-ECT"
1906
1907	$IP route get fibmatch 172.16.102.1 dsfield 0x15 | \
1908		grep -q "172.16.102.0/24 via 172.16.101.2"
1909	log_test $? 0 "IPv4 route with unknown DSCP and ECN:ECT(1)"
1910
1911	$IP route get fibmatch 172.16.102.1 dsfield 0x16 | \
1912		grep -q "172.16.102.0/24 via 172.16.101.2"
1913	log_test $? 0 "IPv4 route with unknown DSCP and ECN:ECT(0)"
1914
1915	$IP route get fibmatch 172.16.102.1 dsfield 0x17 | \
1916		grep -q "172.16.102.0/24 via 172.16.101.2"
1917	log_test $? 0 "IPv4 route with unknown DSCP and ECN:CE"
1918
1919	# Null DSCP should match the generic route, no matter the ECN bits
1920	$IP route get fibmatch 172.16.102.1 dsfield 0x00 | \
1921		grep -q "172.16.102.0/24 via 172.16.101.2"
1922	log_test $? 0 "IPv4 route with no DSCP and ECN:Not-ECT"
1923
1924	$IP route get fibmatch 172.16.102.1 dsfield 0x01 | \
1925		grep -q "172.16.102.0/24 via 172.16.101.2"
1926	log_test $? 0 "IPv4 route with no DSCP and ECN:ECT(1)"
1927
1928	$IP route get fibmatch 172.16.102.1 dsfield 0x02 | \
1929		grep -q "172.16.102.0/24 via 172.16.101.2"
1930	log_test $? 0 "IPv4 route with no DSCP and ECN:ECT(0)"
1931
1932	$IP route get fibmatch 172.16.102.1 dsfield 0x03 | \
1933		grep -q "172.16.102.0/24 via 172.16.101.2"
1934	log_test $? 0 "IPv4 route with no DSCP and ECN:CE"
1935}
1936
1937ipv4_route_test()
1938{
1939	route_setup
1940
1941	ipv4_rt_add
1942	ipv4_rt_replace
1943	ipv4_local_rt_cache
1944	ipv4_rt_dsfield
1945
1946	route_cleanup
1947}
1948
1949ipv4_addr_metric_test()
1950{
1951	local rc
1952
1953	echo
1954	echo "IPv4 prefix route tests"
1955
1956	ip_addr_metric_check || return 1
1957
1958	setup
1959
1960	set -e
1961	$IP li add dummy1 type dummy
1962	$IP li add dummy2 type dummy
1963	$IP li set dummy1 up
1964	$IP li set dummy2 up
1965
1966	# default entry is metric 256
1967	run_cmd "$IP addr add dev dummy1 172.16.104.1/24"
1968	run_cmd "$IP addr add dev dummy2 172.16.104.2/24"
1969	set +e
1970
1971	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"
1972	log_test $? 0 "Default metric"
1973
1974	set -e
1975	run_cmd "$IP addr flush dev dummy1"
1976	run_cmd "$IP addr add dev dummy1 172.16.104.1/24 metric 257"
1977	set +e
1978
1979	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"
1980	log_test $? 0 "User specified metric on first device"
1981
1982	set -e
1983	run_cmd "$IP addr flush dev dummy2"
1984	run_cmd "$IP addr add dev dummy2 172.16.104.2/24 metric 258"
1985	set +e
1986
1987	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"
1988	log_test $? 0 "User specified metric on second device"
1989
1990	run_cmd "$IP addr del dev dummy1 172.16.104.1/24 metric 257"
1991	rc=$?
1992	if [ $rc -eq 0 ]; then
1993		check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 258"
1994		rc=$?
1995	fi
1996	log_test $rc 0 "Delete of address on first device"
1997
1998	run_cmd "$IP addr change dev dummy2 172.16.104.2/24 metric 259"
1999	rc=$?
2000	if [ $rc -eq 0 ]; then
2001		check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 259"
2002		rc=$?
2003	fi
2004	log_test $rc 0 "Modify metric of address"
2005
2006	# verify prefix route removed on down
2007	run_cmd "$IP li set dev dummy2 down"
2008	rc=$?
2009	if [ $rc -eq 0 ]; then
2010		out=$($IP ro ls match 172.16.104.0/24)
2011		check_expected "${out}" ""
2012		rc=$?
2013	fi
2014	log_test $rc 0 "Prefix route removed on link down"
2015
2016	# verify prefix route re-inserted with assigned metric
2017	run_cmd "$IP li set dev dummy2 up"
2018	rc=$?
2019	if [ $rc -eq 0 ]; then
2020		check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 259"
2021		rc=$?
2022	fi
2023	log_test $rc 0 "Prefix route with metric on link up"
2024
2025	# explicitly check for metric changes on edge scenarios
2026	run_cmd "$IP addr flush dev dummy2"
2027	run_cmd "$IP addr add dev dummy2 172.16.104.0/24 metric 259"
2028	run_cmd "$IP addr change dev dummy2 172.16.104.0/24 metric 260"
2029	rc=$?
2030	if [ $rc -eq 0 ]; then
2031		check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.0 metric 260"
2032		rc=$?
2033	fi
2034	log_test $rc 0 "Modify metric of .0/24 address"
2035
2036	run_cmd "$IP addr flush dev dummy2"
2037	run_cmd "$IP addr add dev dummy2 172.16.104.1/32 peer 172.16.104.2 metric 260"
2038	rc=$?
2039	if [ $rc -eq 0 ]; then
2040		check_route "172.16.104.2 dev dummy2 proto kernel scope link src 172.16.104.1 metric 260"
2041		rc=$?
2042	fi
2043	log_test $rc 0 "Set metric of address with peer route"
2044
2045	run_cmd "$IP addr change dev dummy2 172.16.104.1/32 peer 172.16.104.3 metric 261"
2046	rc=$?
2047	if [ $rc -eq 0 ]; then
2048		check_route "172.16.104.3 dev dummy2 proto kernel scope link src 172.16.104.1 metric 261"
2049		rc=$?
2050	fi
2051	log_test $rc 0 "Modify metric and peer address for peer route"
2052
2053	$IP li del dummy1
2054	$IP li del dummy2
2055	cleanup
2056}
2057
2058ipv4_route_metrics_test()
2059{
2060	local rc
2061
2062	echo
2063	echo "IPv4 route add / append tests"
2064
2065	route_setup
2066
2067	run_cmd "$IP ro add 172.16.111.0/24 via 172.16.101.2 mtu 1400"
2068	rc=$?
2069	if [ $rc -eq 0 ]; then
2070		check_route "172.16.111.0/24 via 172.16.101.2 dev veth1 mtu 1400"
2071		rc=$?
2072	fi
2073	log_test $rc 0 "Single path route with mtu metric"
2074
2075
2076	run_cmd "$IP ro add 172.16.112.0/24 mtu 1400 nexthop via 172.16.101.2 nexthop via 172.16.103.2"
2077	rc=$?
2078	if [ $rc -eq 0 ]; then
2079		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"
2080		rc=$?
2081	fi
2082	log_test $rc 0 "Multipath route with mtu metric"
2083
2084	$IP ro add 172.16.104.0/24 via 172.16.101.2 mtu 1300
2085	run_cmd "ip netns exec $ns1 ping -w1 -c1 -s 1500 172.16.104.1"
2086	log_test $? 0 "Using route with mtu metric"
2087
2088	run_cmd "$IP ro add 172.16.111.0/24 via 172.16.101.2 congctl lock foo"
2089	log_test $? 2 "Invalid metric (fails metric_convert)"
2090
2091	route_cleanup
2092}
2093
2094ipv4_del_addr_test()
2095{
2096	echo
2097	echo "IPv4 delete address route tests"
2098
2099	setup
2100
2101	set -e
2102	$IP li add dummy1 type dummy
2103	$IP li set dummy1 up
2104	$IP li add dummy2 type dummy
2105	$IP li set dummy2 up
2106	$IP li add red type vrf table 1111
2107	$IP li set red up
2108	$IP ro add vrf red unreachable default
2109	$IP li set dummy2 vrf red
2110
2111	$IP addr add dev dummy1 172.16.104.1/24
2112	$IP addr add dev dummy1 172.16.104.11/24
2113	$IP addr add dev dummy1 172.16.104.12/24
2114	$IP addr add dev dummy1 172.16.104.13/24
2115	$IP addr add dev dummy2 172.16.104.1/24
2116	$IP addr add dev dummy2 172.16.104.11/24
2117	$IP addr add dev dummy2 172.16.104.12/24
2118	$IP route add 172.16.105.0/24 via 172.16.104.2 src 172.16.104.11
2119	$IP route add 172.16.106.0/24 dev lo src 172.16.104.12
2120	$IP route add table 0 172.16.107.0/24 via 172.16.104.2 src 172.16.104.13
2121	$IP route add vrf red 172.16.105.0/24 via 172.16.104.2 src 172.16.104.11
2122	$IP route add vrf red 172.16.106.0/24 dev lo src 172.16.104.12
2123	set +e
2124
2125	# removing address from device in vrf should only remove route from vrf table
2126	echo "    Regular FIB info"
2127
2128	$IP addr del dev dummy2 172.16.104.11/24
2129	$IP ro ls vrf red | grep -q 172.16.105.0/24
2130	log_test $? 1 "Route removed from VRF when source address deleted"
2131
2132	$IP ro ls | grep -q 172.16.105.0/24
2133	log_test $? 0 "Route in default VRF not removed"
2134
2135	$IP addr add dev dummy2 172.16.104.11/24
2136	$IP route add vrf red 172.16.105.0/24 via 172.16.104.2 src 172.16.104.11
2137
2138	$IP addr del dev dummy1 172.16.104.11/24
2139	$IP ro ls | grep -q 172.16.105.0/24
2140	log_test $? 1 "Route removed in default VRF when source address deleted"
2141
2142	$IP ro ls vrf red | grep -q 172.16.105.0/24
2143	log_test $? 0 "Route in VRF is not removed by address delete"
2144
2145	# removing address from device in vrf should only remove route from vrf
2146	# table even when the associated fib info only differs in table ID
2147	echo "    Identical FIB info with different table ID"
2148
2149	$IP addr del dev dummy2 172.16.104.12/24
2150	$IP ro ls vrf red | grep -q 172.16.106.0/24
2151	log_test $? 1 "Route removed from VRF when source address deleted"
2152
2153	$IP ro ls | grep -q 172.16.106.0/24
2154	log_test $? 0 "Route in default VRF not removed"
2155
2156	$IP addr add dev dummy2 172.16.104.12/24
2157	$IP route add vrf red 172.16.106.0/24 dev lo src 172.16.104.12
2158
2159	$IP addr del dev dummy1 172.16.104.12/24
2160	$IP ro ls | grep -q 172.16.106.0/24
2161	log_test $? 1 "Route removed in default VRF when source address deleted"
2162
2163	$IP ro ls vrf red | grep -q 172.16.106.0/24
2164	log_test $? 0 "Route in VRF is not removed by address delete"
2165
2166	# removing address from device in default vrf should remove route from
2167	# the default vrf even when route was inserted with a table ID of 0.
2168	echo "    Table ID 0"
2169
2170	$IP addr del dev dummy1 172.16.104.13/24
2171	$IP ro ls | grep -q 172.16.107.0/24
2172	log_test $? 1 "Route removed in default VRF when source address deleted"
2173
2174	$IP li del dummy1
2175	$IP li del dummy2
2176	cleanup
2177}
2178
2179ipv6_del_addr_test()
2180{
2181	echo
2182	echo "IPv6 delete address route tests"
2183
2184	setup
2185
2186	set -e
2187	for i in $(seq 6); do
2188		$IP li add dummy${i} up type dummy
2189	done
2190
2191	$IP li add red up type vrf table 1111
2192	$IP ro add vrf red unreachable default
2193	for i in $(seq 4 6); do
2194		$IP li set dummy${i} vrf red
2195	done
2196
2197	$IP addr add dev dummy1 fe80::1/128
2198	$IP addr add dev dummy1 2001:db8:101::1/64
2199	$IP addr add dev dummy1 2001:db8:101::10/64
2200	$IP addr add dev dummy1 2001:db8:101::11/64
2201	$IP addr add dev dummy1 2001:db8:101::12/64
2202	$IP addr add dev dummy1 2001:db8:101::13/64
2203	$IP addr add dev dummy1 2001:db8:101::14/64
2204	$IP addr add dev dummy1 2001:db8:101::15/64
2205	$IP addr add dev dummy2 fe80::1/128
2206	$IP addr add dev dummy2 2001:db8:101::1/64
2207	$IP addr add dev dummy2 2001:db8:101::11/64
2208	$IP addr add dev dummy3 fe80::1/128
2209
2210	$IP addr add dev dummy4 2001:db8:101::1/64
2211	$IP addr add dev dummy4 2001:db8:101::10/64
2212	$IP addr add dev dummy4 2001:db8:101::11/64
2213	$IP addr add dev dummy4 2001:db8:101::12/64
2214	$IP addr add dev dummy4 2001:db8:101::13/64
2215	$IP addr add dev dummy4 2001:db8:101::14/64
2216	$IP addr add dev dummy5 2001:db8:101::1/64
2217	$IP addr add dev dummy5 2001:db8:101::11/64
2218
2219	# Single device using src address
2220	$IP route add 2001:db8:110::/64 dev dummy3 src 2001:db8:101::10
2221	# Two devices with the same source address
2222	$IP route add 2001:db8:111::/64 dev dummy3 src 2001:db8:101::11
2223	# VRF with single device using src address
2224	$IP route add vrf red 2001:db8:110::/64 dev dummy6 src 2001:db8:101::10
2225	# VRF with two devices using src address
2226	$IP route add vrf red 2001:db8:111::/64 dev dummy6 src 2001:db8:101::11
2227	# src address and nexthop dev in same VRF
2228	$IP route add 2001:db8:112::/64 dev dummy3 src 2001:db8:101::12
2229	$IP route add vrf red 2001:db8:112::/64 dev dummy6 src 2001:db8:101::12
2230	# src address and nexthop device in different VRF
2231	$IP route add 2001:db8:113::/64 dev lo src 2001:db8:101::13
2232	$IP route add vrf red 2001:db8:113::/64 dev lo src 2001:db8:101::13
2233	# table ID 0
2234	$IP route add table 0 2001:db8:115::/64 via 2001:db8:101::2 src 2001:db8:101::15
2235	# Link local source route
2236	$IP route add 2001:db8:116::/64 dev dummy2 src fe80::1
2237	$IP route add 2001:db8:117::/64 dev dummy3 src fe80::1
2238	set +e
2239
2240	echo "    Single device using src address"
2241
2242	$IP addr del dev dummy1 2001:db8:101::10/64
2243	$IP -6 route show | grep -q "src 2001:db8:101::10 "
2244	log_test $? 1 "Prefsrc removed when src address removed on other device"
2245
2246	echo "    Two devices with the same source address"
2247
2248	$IP addr del dev dummy1 2001:db8:101::11/64
2249	$IP -6 route show | grep -q "src 2001:db8:101::11 "
2250	log_test $? 0 "Prefsrc not removed when src address exist on other device"
2251
2252	$IP addr del dev dummy2 2001:db8:101::11/64
2253	$IP -6 route show | grep -q "src 2001:db8:101::11 "
2254	log_test $? 1 "Prefsrc removed when src address removed on all devices"
2255
2256	echo "    VRF with single device using src address"
2257
2258	$IP addr del dev dummy4 2001:db8:101::10/64
2259	$IP -6 route show vrf red | grep -q "src 2001:db8:101::10 "
2260	log_test $? 1 "Prefsrc removed when src address removed on other device"
2261
2262	echo "    VRF with two devices using src address"
2263
2264	$IP addr del dev dummy4 2001:db8:101::11/64
2265	$IP -6 route show vrf red | grep -q "src 2001:db8:101::11 "
2266	log_test $? 0 "Prefsrc not removed when src address exist on other device"
2267
2268	$IP addr del dev dummy5 2001:db8:101::11/64
2269	$IP -6 route show vrf red | grep -q "src 2001:db8:101::11 "
2270	log_test $? 1 "Prefsrc removed when src address removed on all devices"
2271
2272	echo "    src address and nexthop dev in same VRF"
2273
2274	$IP addr del dev dummy4 2001:db8:101::12/64
2275	$IP -6 route show vrf red | grep -q "src 2001:db8:101::12 "
2276	log_test $? 1 "Prefsrc removed from VRF when source address deleted"
2277	$IP -6 route show | grep -q " src 2001:db8:101::12 "
2278	log_test $? 0 "Prefsrc in default VRF not removed"
2279
2280	$IP addr add dev dummy4 2001:db8:101::12/64
2281	$IP route replace vrf red 2001:db8:112::/64 dev dummy6 src 2001:db8:101::12
2282	$IP addr del dev dummy1 2001:db8:101::12/64
2283	$IP -6 route show vrf red | grep -q "src 2001:db8:101::12 "
2284	log_test $? 0 "Prefsrc not removed from VRF when source address exist"
2285	$IP -6 route show | grep -q " src 2001:db8:101::12 "
2286	log_test $? 1 "Prefsrc in default VRF removed"
2287
2288	echo "    src address and nexthop device in different VRF"
2289
2290	$IP addr del dev dummy4 2001:db8:101::13/64
2291	$IP -6 route show vrf red | grep -q "src 2001:db8:101::13 "
2292	log_test $? 0 "Prefsrc not removed from VRF when nexthop dev in diff VRF"
2293	$IP -6 route show | grep -q "src 2001:db8:101::13 "
2294	log_test $? 0 "Prefsrc not removed in default VRF"
2295
2296	$IP addr add dev dummy4 2001:db8:101::13/64
2297	$IP addr del dev dummy1 2001:db8:101::13/64
2298	$IP -6 route show vrf red | grep -q "src 2001:db8:101::13 "
2299	log_test $? 1 "Prefsrc removed from VRF when nexthop dev in diff VRF"
2300	$IP -6 route show | grep -q "src 2001:db8:101::13 "
2301	log_test $? 1 "Prefsrc removed in default VRF"
2302
2303	echo "    Table ID 0"
2304
2305	$IP addr del dev dummy1 2001:db8:101::15/64
2306	$IP -6 route show | grep -q "src 2001:db8:101::15"
2307	log_test $? 1 "Prefsrc removed from default VRF when source address deleted"
2308
2309	echo "    Link local source route"
2310	$IP addr del dev dummy1 fe80::1/128
2311	$IP -6 route show | grep -q "2001:db8:116::/64 dev dummy2 src fe80::1"
2312	log_test $? 0 "Prefsrc not removed when delete ll addr from other dev"
2313	$IP addr del dev dummy2 fe80::1/128
2314	$IP -6 route show | grep -q "2001:db8:116::/64 dev dummy2 src fe80::1"
2315	log_test $? 1 "Prefsrc removed when delete ll addr"
2316	$IP -6 route show | grep -q "2001:db8:117::/64 dev dummy3 src fe80::1"
2317	log_test $? 0 "Prefsrc not removed when delete ll addr from other dev"
2318	$IP addr add dev dummy1 fe80::1/128
2319	$IP addr del dev dummy3 fe80::1/128
2320	$IP -6 route show | grep -q "2001:db8:117::/64 dev dummy3 src fe80::1"
2321	log_test $? 1 "Prefsrc removed even ll addr still exist on other dev"
2322
2323	for i in $(seq 6); do
2324		$IP li del dummy${i}
2325	done
2326	cleanup
2327}
2328
2329ipv4_route_v6_gw_test()
2330{
2331	local rc
2332
2333	echo
2334	echo "IPv4 route with IPv6 gateway tests"
2335
2336	route_setup
2337	sleep 2
2338
2339	#
2340	# single path route
2341	#
2342	run_cmd "$IP ro add 172.16.104.0/24 via inet6 2001:db8:101::2"
2343	rc=$?
2344	log_test $rc 0 "Single path route with IPv6 gateway"
2345	if [ $rc -eq 0 ]; then
2346		check_route "172.16.104.0/24 via inet6 2001:db8:101::2 dev veth1"
2347	fi
2348
2349	run_cmd "ip netns exec $ns1 ping -w1 -c1 172.16.104.1"
2350	log_test $rc 0 "Single path route with IPv6 gateway - ping"
2351
2352	run_cmd "$IP ro del 172.16.104.0/24 via inet6 2001:db8:101::2"
2353	rc=$?
2354	log_test $rc 0 "Single path route delete"
2355	if [ $rc -eq 0 ]; then
2356		check_route "172.16.112.0/24"
2357	fi
2358
2359	#
2360	# multipath - v6 then v4
2361	#
2362	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"
2363	rc=$?
2364	log_test $rc 0 "Multipath route add - v6 nexthop then v4"
2365	if [ $rc -eq 0 ]; then
2366		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"
2367	fi
2368
2369	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"
2370	log_test $? 2 "    Multipath route delete - nexthops in wrong order"
2371
2372	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"
2373	log_test $? 0 "    Multipath route delete exact match"
2374
2375	#
2376	# multipath - v4 then v6
2377	#
2378	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"
2379	rc=$?
2380	log_test $rc 0 "Multipath route add - v4 nexthop then v6"
2381	if [ $rc -eq 0 ]; then
2382		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"
2383	fi
2384
2385	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"
2386	log_test $? 2 "    Multipath route delete - nexthops in wrong order"
2387
2388	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"
2389	log_test $? 0 "    Multipath route delete exact match"
2390
2391	route_cleanup
2392}
2393
2394socat_check()
2395{
2396	if [ ! -x "$(command -v socat)" ]; then
2397		echo "socat command not found. Skipping test"
2398		return 1
2399	fi
2400
2401	return 0
2402}
2403
2404iptables_check()
2405{
2406	iptables -t mangle -L OUTPUT &> /dev/null
2407	if [ $? -ne 0 ]; then
2408		echo "iptables configuration not supported. Skipping test"
2409		return 1
2410	fi
2411
2412	return 0
2413}
2414
2415ip6tables_check()
2416{
2417	ip6tables -t mangle -L OUTPUT &> /dev/null
2418	if [ $? -ne 0 ]; then
2419		echo "ip6tables configuration not supported. Skipping test"
2420		return 1
2421	fi
2422
2423	return 0
2424}
2425
2426ipv4_mangle_test()
2427{
2428	local rc
2429
2430	echo
2431	echo "IPv4 mangling tests"
2432
2433	socat_check || return 1
2434	iptables_check || return 1
2435
2436	route_setup
2437	sleep 2
2438
2439	local tmp_file=$(mktemp)
2440	ip netns exec $ns2 socat UDP4-LISTEN:54321,fork $tmp_file &
2441
2442	# Add a FIB rule and a route that will direct our connection to the
2443	# listening server.
2444	$IP rule add pref 100 ipproto udp sport 12345 dport 54321 table 123
2445	$IP route add table 123 172.16.101.0/24 dev veth1
2446
2447	# Add an unreachable route to the main table that will block our
2448	# connection in case the FIB rule is not hit.
2449	$IP route add unreachable 172.16.101.2/32
2450
2451	run_cmd "echo a | $NS_EXEC socat STDIN UDP4:172.16.101.2:54321,sourceport=12345"
2452	log_test $? 0 "    Connection with correct parameters"
2453
2454	run_cmd "echo a | $NS_EXEC socat STDIN UDP4:172.16.101.2:54321,sourceport=11111"
2455	log_test $? 1 "    Connection with incorrect parameters"
2456
2457	# Add a mangling rule and make sure connection is still successful.
2458	$NS_EXEC iptables -t mangle -A OUTPUT -j MARK --set-mark 1
2459
2460	run_cmd "echo a | $NS_EXEC socat STDIN UDP4:172.16.101.2:54321,sourceport=12345"
2461	log_test $? 0 "    Connection with correct parameters - mangling"
2462
2463	# Delete the mangling rule and make sure connection is still
2464	# successful.
2465	$NS_EXEC iptables -t mangle -D OUTPUT -j MARK --set-mark 1
2466
2467	run_cmd "echo a | $NS_EXEC socat STDIN UDP4:172.16.101.2:54321,sourceport=12345"
2468	log_test $? 0 "    Connection with correct parameters - no mangling"
2469
2470	# Verify connections were indeed successful on server side.
2471	[[ $(cat $tmp_file | wc -l) -eq 3 ]]
2472	log_test $? 0 "    Connection check - server side"
2473
2474	$IP route del unreachable 172.16.101.2/32
2475	$IP route del table 123 172.16.101.0/24 dev veth1
2476	$IP rule del pref 100
2477
2478	kill_process %%
2479	rm $tmp_file
2480
2481	route_cleanup
2482}
2483
2484ipv6_mangle_test()
2485{
2486	local rc
2487
2488	echo
2489	echo "IPv6 mangling tests"
2490
2491	socat_check || return 1
2492	ip6tables_check || return 1
2493
2494	route_setup
2495	sleep 2
2496
2497	local tmp_file=$(mktemp)
2498	ip netns exec $ns2 socat UDP6-LISTEN:54321,fork $tmp_file &
2499
2500	# Add a FIB rule and a route that will direct our connection to the
2501	# listening server.
2502	$IP -6 rule add pref 100 ipproto udp sport 12345 dport 54321 table 123
2503	$IP -6 route add table 123 2001:db8:101::/64 dev veth1
2504
2505	# Add an unreachable route to the main table that will block our
2506	# connection in case the FIB rule is not hit.
2507	$IP -6 route add unreachable 2001:db8:101::2/128
2508
2509	run_cmd "echo a | $NS_EXEC socat STDIN UDP6:[2001:db8:101::2]:54321,sourceport=12345"
2510	log_test $? 0 "    Connection with correct parameters"
2511
2512	run_cmd "echo a | $NS_EXEC socat STDIN UDP6:[2001:db8:101::2]:54321,sourceport=11111"
2513	log_test $? 1 "    Connection with incorrect parameters"
2514
2515	# Add a mangling rule and make sure connection is still successful.
2516	$NS_EXEC ip6tables -t mangle -A OUTPUT -j MARK --set-mark 1
2517
2518	run_cmd "echo a | $NS_EXEC socat STDIN UDP6:[2001:db8:101::2]:54321,sourceport=12345"
2519	log_test $? 0 "    Connection with correct parameters - mangling"
2520
2521	# Delete the mangling rule and make sure connection is still
2522	# successful.
2523	$NS_EXEC ip6tables -t mangle -D OUTPUT -j MARK --set-mark 1
2524
2525	run_cmd "echo a | $NS_EXEC socat STDIN UDP6:[2001:db8:101::2]:54321,sourceport=12345"
2526	log_test $? 0 "    Connection with correct parameters - no mangling"
2527
2528	# Verify connections were indeed successful on server side.
2529	[[ $(cat $tmp_file | wc -l) -eq 3 ]]
2530	log_test $? 0 "    Connection check - server side"
2531
2532	$IP -6 route del unreachable 2001:db8:101::2/128
2533	$IP -6 route del table 123 2001:db8:101::/64 dev veth1
2534	$IP -6 rule del pref 100
2535
2536	kill_process %%
2537	rm $tmp_file
2538
2539	route_cleanup
2540}
2541
2542ip_neigh_get_check()
2543{
2544	ip neigh help 2>&1 | grep -q 'ip neigh get'
2545	if [ $? -ne 0 ]; then
2546		echo "iproute2 command does not support neigh get. Skipping test"
2547		return 1
2548	fi
2549
2550	return 0
2551}
2552
2553ipv4_bcast_neigh_test()
2554{
2555	local rc
2556
2557	echo
2558	echo "IPv4 broadcast neighbour tests"
2559
2560	ip_neigh_get_check || return 1
2561
2562	setup
2563
2564	set -e
2565	run_cmd "$IP neigh add 192.0.2.111 lladdr 00:11:22:33:44:55 nud perm dev dummy0"
2566	run_cmd "$IP neigh add 192.0.2.255 lladdr 00:11:22:33:44:55 nud perm dev dummy0"
2567
2568	run_cmd "$IP neigh get 192.0.2.111 dev dummy0"
2569	run_cmd "$IP neigh get 192.0.2.255 dev dummy0"
2570
2571	run_cmd "$IP address add 192.0.2.1/24 broadcast 192.0.2.111 dev dummy0"
2572
2573	run_cmd "$IP neigh add 203.0.113.111 nud failed dev dummy0"
2574	run_cmd "$IP neigh add 203.0.113.255 nud failed dev dummy0"
2575
2576	run_cmd "$IP neigh get 203.0.113.111 dev dummy0"
2577	run_cmd "$IP neigh get 203.0.113.255 dev dummy0"
2578
2579	run_cmd "$IP address add 203.0.113.1/24 broadcast 203.0.113.111 dev dummy0"
2580	set +e
2581
2582	run_cmd "$IP neigh get 192.0.2.111 dev dummy0"
2583	log_test $? 0 "Resolved neighbour for broadcast address"
2584
2585	run_cmd "$IP neigh get 192.0.2.255 dev dummy0"
2586	log_test $? 0 "Resolved neighbour for network broadcast address"
2587
2588	run_cmd "$IP neigh get 203.0.113.111 dev dummy0"
2589	log_test $? 2 "Unresolved neighbour for broadcast address"
2590
2591	run_cmd "$IP neigh get 203.0.113.255 dev dummy0"
2592	log_test $? 2 "Unresolved neighbour for network broadcast address"
2593
2594	cleanup
2595}
2596
2597mpath_dep_check()
2598{
2599	if [ ! -x "$(command -v mausezahn)" ]; then
2600		echo "mausezahn command not found. Skipping test"
2601		return 1
2602	fi
2603
2604	if [ ! -x "$(command -v jq)" ]; then
2605		echo "jq command not found. Skipping test"
2606		return 1
2607	fi
2608
2609	if [ ! -x "$(command -v bc)" ]; then
2610		echo "bc command not found. Skipping test"
2611		return 1
2612	fi
2613
2614	if [ ! -x "$(command -v perf)" ]; then
2615		echo "perf command not found. Skipping test"
2616		return 1
2617	fi
2618
2619	perf list fib:* | grep -q fib_table_lookup
2620	if [ $? -ne 0 ]; then
2621		echo "IPv4 FIB tracepoint not found. Skipping test"
2622		return 1
2623	fi
2624
2625	perf list fib6:* | grep -q fib6_table_lookup
2626	if [ $? -ne 0 ]; then
2627		echo "IPv6 FIB tracepoint not found. Skipping test"
2628		return 1
2629	fi
2630
2631	return 0
2632}
2633
2634link_stats_get()
2635{
2636	local ns=$1; shift
2637	local dev=$1; shift
2638	local dir=$1; shift
2639	local stat=$1; shift
2640
2641	ip -n $ns -j -s link show dev $dev \
2642		| jq '.[]["stats64"]["'$dir'"]["'$stat'"]'
2643}
2644
2645list_rcv_eval()
2646{
2647	local file=$1; shift
2648	local expected=$1; shift
2649
2650	local count=$(tail -n 1 $file | jq '.["counter-value"] | tonumber | floor')
2651	local ratio=$(echo "scale=2; $count / $expected" | bc -l)
2652	local res=$(echo "$ratio >= 0.95" | bc)
2653	[[ $res -eq 1 ]]
2654	log_test $? 0 "Multipath route hit ratio ($ratio)"
2655}
2656
2657ipv4_mpath_list_test()
2658{
2659	echo
2660	echo "IPv4 multipath list receive tests"
2661
2662	mpath_dep_check || return 1
2663
2664	route_setup
2665
2666	set -e
2667	run_cmd "ip netns exec $ns1 ethtool -K veth1 tcp-segmentation-offload off"
2668
2669	run_cmd "ip netns exec $ns2 bash -c \"echo 20000 > /sys/class/net/veth2/gro_flush_timeout\""
2670	run_cmd "ip netns exec $ns2 bash -c \"echo 1 > /sys/class/net/veth2/napi_defer_hard_irqs\""
2671	run_cmd "ip netns exec $ns2 ethtool -K veth2 generic-receive-offload on"
2672	run_cmd "ip -n $ns2 link add name nh1 up type dummy"
2673	run_cmd "ip -n $ns2 link add name nh2 up type dummy"
2674	run_cmd "ip -n $ns2 address add 172.16.201.1/24 dev nh1"
2675	run_cmd "ip -n $ns2 address add 172.16.202.1/24 dev nh2"
2676	run_cmd "ip -n $ns2 neigh add 172.16.201.2 lladdr 00:11:22:33:44:55 nud perm dev nh1"
2677	run_cmd "ip -n $ns2 neigh add 172.16.202.2 lladdr 00:aa:bb:cc:dd:ee nud perm dev nh2"
2678	run_cmd "ip -n $ns2 route add 203.0.113.0/24
2679		nexthop via 172.16.201.2 nexthop via 172.16.202.2"
2680	run_cmd "ip netns exec $ns2 sysctl -qw net.ipv4.fib_multipath_hash_policy=1"
2681	set +e
2682
2683	local dmac=$(ip -n $ns2 -j link show dev veth2 | jq -r '.[]["address"]')
2684	local tmp_file=$(mktemp)
2685	local cmd="ip netns exec $ns1 mausezahn veth1 -a own -b $dmac
2686		-A 172.16.101.1 -B 203.0.113.1 -t udp 'sp=12345,dp=0-65535' -q"
2687
2688	# Packets forwarded in a list using a multipath route must not reuse a
2689	# cached result so that a flow always hits the same nexthop. In other
2690	# words, the FIB lookup tracepoint needs to be triggered for every
2691	# packet.
2692	local t0_rx_pkts=$(link_stats_get $ns2 veth2 rx packets)
2693	run_cmd "perf stat -a -e fib:fib_table_lookup --filter 'err == 0' -j -o $tmp_file -- $cmd"
2694	local t1_rx_pkts=$(link_stats_get $ns2 veth2 rx packets)
2695	local diff=$(echo $t1_rx_pkts - $t0_rx_pkts | bc -l)
2696	list_rcv_eval $tmp_file $diff
2697
2698	rm $tmp_file
2699	route_cleanup
2700}
2701
2702ipv6_mpath_list_test()
2703{
2704	echo
2705	echo "IPv6 multipath list receive tests"
2706
2707	mpath_dep_check || return 1
2708
2709	route_setup
2710
2711	set -e
2712	run_cmd "ip netns exec $ns1 ethtool -K veth1 tcp-segmentation-offload off"
2713
2714	run_cmd "ip netns exec $ns2 bash -c \"echo 20000 > /sys/class/net/veth2/gro_flush_timeout\""
2715	run_cmd "ip netns exec $ns2 bash -c \"echo 1 > /sys/class/net/veth2/napi_defer_hard_irqs\""
2716	run_cmd "ip netns exec $ns2 ethtool -K veth2 generic-receive-offload on"
2717	run_cmd "ip -n $ns2 link add name nh1 up type dummy"
2718	run_cmd "ip -n $ns2 link add name nh2 up type dummy"
2719	run_cmd "ip -n $ns2 -6 address add 2001:db8:201::1/64 dev nh1"
2720	run_cmd "ip -n $ns2 -6 address add 2001:db8:202::1/64 dev nh2"
2721	run_cmd "ip -n $ns2 -6 neigh add 2001:db8:201::2 lladdr 00:11:22:33:44:55 nud perm dev nh1"
2722	run_cmd "ip -n $ns2 -6 neigh add 2001:db8:202::2 lladdr 00:aa:bb:cc:dd:ee nud perm dev nh2"
2723	run_cmd "ip -n $ns2 -6 route add 2001:db8:301::/64
2724		nexthop via 2001:db8:201::2 nexthop via 2001:db8:202::2"
2725	run_cmd "ip netns exec $ns2 sysctl -qw net.ipv6.fib_multipath_hash_policy=1"
2726	set +e
2727
2728	local dmac=$(ip -n $ns2 -j link show dev veth2 | jq -r '.[]["address"]')
2729	local tmp_file=$(mktemp)
2730	local cmd="ip netns exec $ns1 mausezahn -6 veth1 -a own -b $dmac
2731		-A 2001:db8:101::1 -B 2001:db8:301::1 -t udp 'sp=12345,dp=0-65535' -q"
2732
2733	# Packets forwarded in a list using a multipath route must not reuse a
2734	# cached result so that a flow always hits the same nexthop. In other
2735	# words, the FIB lookup tracepoint needs to be triggered for every
2736	# packet.
2737	local t0_rx_pkts=$(link_stats_get $ns2 veth2 rx packets)
2738	run_cmd "perf stat -a -e fib6:fib6_table_lookup --filter 'err == 0' -j -o $tmp_file -- $cmd"
2739	local t1_rx_pkts=$(link_stats_get $ns2 veth2 rx packets)
2740	local diff=$(echo $t1_rx_pkts - $t0_rx_pkts | bc -l)
2741	list_rcv_eval $tmp_file $diff
2742
2743	rm $tmp_file
2744	route_cleanup
2745}
2746
2747tc_set_flower_counter__saddr_syn() {
2748	tc_set_flower_counter $1 $2 $3 "src_ip $4 ip_proto tcp tcp_flags 0x2"
2749}
2750
2751ip_mpath_balance_dep_check()
2752{
2753	if [ ! -x "$(command -v socat)" ]; then
2754		echo "socat command not found. Skipping test"
2755		return 1
2756	fi
2757
2758	if [ ! -x "$(command -v jq)" ]; then
2759		echo "jq command not found. Skipping test"
2760		return 1
2761	fi
2762}
2763
2764ip_mpath_balance() {
2765	local -r ipver=$1
2766	local -r daddr=$2
2767	local -r num_conn=20
2768
2769	for i in $(seq 1 $num_conn); do
2770		ip netns exec $ns3 socat $ipver TCP-LISTEN:8000 STDIO >/dev/null &
2771		sleep 0.02
2772		echo -n a | ip netns exec $ns1 socat $ipver STDIO TCP:$daddr:8000
2773	done
2774
2775	local -r syn0="$(tc_get_flower_counter $ns1 veth1)"
2776	local -r syn1="$(tc_get_flower_counter $ns1 veth3)"
2777	local -r syns=$((syn0+syn1))
2778
2779	[ "$VERBOSE" = "1" ] && echo "multipath: syns seen: ($syn0,$syn1)"
2780
2781	[[ $syns -ge $num_conn ]] && [[ $syn0 -gt 0 ]] && [[ $syn1 -gt 0 ]]
2782}
2783
2784ipv4_mpath_balance_test()
2785{
2786	echo
2787	echo "IPv4 multipath load balance test"
2788
2789	ip_mpath_balance_dep_check || return 1
2790	forwarding_setup
2791
2792	$IP route add 172.16.105.1 \
2793		nexthop via 172.16.101.2 \
2794		nexthop via 172.16.103.2
2795
2796	ip netns exec $ns1 \
2797		sysctl -q -w net.ipv4.fib_multipath_hash_policy=1
2798
2799	tc_set_flower_counter__saddr_syn $ns1 4 veth1 172.16.101.1
2800	tc_set_flower_counter__saddr_syn $ns1 4 veth3 172.16.103.1
2801
2802	ip_mpath_balance -4 172.16.105.1
2803
2804	log_test $? 0 "IPv4 multipath loadbalance"
2805
2806	forwarding_cleanup
2807}
2808
2809get_route_dev_src()
2810{
2811	local pfx="$1"
2812	local src="$2"
2813	local out
2814
2815	if out=$($IP -j route get "$pfx" from "$src" | jq -re ".[0].dev"); then
2816		echo "$out"
2817	fi
2818}
2819
2820ipv4_mpath_preferred()
2821{
2822	local src_ip=$1
2823	local pref_dev=$2
2824	local dev routes
2825	local route0=0
2826	local route1=0
2827	local pref_route=0
2828	num_routes=254
2829
2830	for i in $(seq 1 $num_routes) ; do
2831		dev=$(get_route_dev_src 172.16.105.$i $src_ip)
2832		if [ "$dev" = "$pref_dev" ]; then
2833			pref_route=$((pref_route+1))
2834		elif [ "$dev" = "veth1" ]; then
2835			route0=$((route0+1))
2836		elif [ "$dev" = "veth3" ]; then
2837			route1=$((route1+1))
2838		fi
2839	done
2840
2841	routes=$((route0+route1))
2842
2843	[ "$VERBOSE" = "1" ] && echo "multipath: routes seen: ($route0,$route1,$pref_route)"
2844
2845	if [ x"$pref_dev" = x"" ]; then
2846		[[ $routes -ge $num_routes ]] && [[ $route0 -gt 0 ]] && [[ $route1 -gt 0 ]]
2847	else
2848		[[ $pref_route -ge $num_routes ]]
2849	fi
2850
2851}
2852
2853ipv4_mpath_balance_preferred_test()
2854{
2855	echo
2856	echo "IPv4 multipath load balance preferred route"
2857
2858	forwarding_setup
2859
2860	$IP route add 172.16.105.0/24 \
2861		nexthop via 172.16.101.2 \
2862		nexthop via 172.16.103.2
2863
2864	ipv4_mpath_preferred 172.16.101.1 veth1
2865	log_test $? 0 "IPv4 multipath loadbalance from veth1"
2866
2867	ipv4_mpath_preferred 172.16.103.1 veth3
2868	log_test $? 0 "IPv4 multipath loadbalance from veth3"
2869
2870	ipv4_mpath_preferred 198.51.100.1
2871	log_test $? 0 "IPv4 multipath loadbalance from dummy"
2872
2873	forwarding_cleanup
2874}
2875
2876ipv6_mpath_balance_test()
2877{
2878	echo
2879	echo "IPv6 multipath load balance test"
2880
2881	ip_mpath_balance_dep_check || return 1
2882	forwarding_setup
2883
2884	$IP route add 2001:db8:105::1\
2885		nexthop via 2001:db8:101::2 \
2886		nexthop via 2001:db8:103::2
2887
2888	ip netns exec $ns1 \
2889		sysctl -q -w net.ipv6.fib_multipath_hash_policy=1
2890
2891	tc_set_flower_counter__saddr_syn $ns1 6 veth1 2001:db8:101::1
2892	tc_set_flower_counter__saddr_syn $ns1 6 veth3 2001:db8:103::1
2893
2894	ip_mpath_balance -6 "[2001:db8:105::1]"
2895
2896	log_test $? 0 "IPv6 multipath loadbalance"
2897
2898	forwarding_cleanup
2899}
2900
2901################################################################################
2902# usage
2903
2904usage()
2905{
2906	cat <<EOF
2907usage: ${0##*/} OPTS
2908
2909        -t <test>   Test(s) to run (default: all)
2910                    (options: $TESTS)
2911        -p          Pause on fail
2912        -P          Pause after each test before cleanup
2913        -v          verbose mode (show commands and output)
2914EOF
2915}
2916
2917################################################################################
2918# main
2919
2920trap cleanup EXIT
2921
2922while getopts :t:pPhv o
2923do
2924	case $o in
2925		t) TESTS=$OPTARG;;
2926		p) PAUSE_ON_FAIL=yes;;
2927		P) PAUSE=yes;;
2928		v) VERBOSE=$(($VERBOSE + 1));;
2929		h) usage; exit 0;;
2930		*) usage; exit 1;;
2931	esac
2932done
2933
2934PEER_CMD="ip netns exec ${PEER_NS}"
2935
2936# make sure we don't pause twice
2937[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
2938
2939if [ "$(id -u)" -ne 0 ];then
2940	echo "SKIP: Need root privileges"
2941	exit $ksft_skip;
2942fi
2943
2944if [ ! -x "$(command -v ip)" ]; then
2945	echo "SKIP: Could not run test without ip tool"
2946	exit $ksft_skip
2947fi
2948
2949ip route help 2>&1 | grep -q fibmatch
2950if [ $? -ne 0 ]; then
2951	echo "SKIP: iproute2 too old, missing fibmatch"
2952	exit $ksft_skip
2953fi
2954
2955# start clean
2956cleanup &> /dev/null
2957
2958for t in $TESTS
2959do
2960	case $t in
2961	fib_unreg_test|unregister)	fib_unreg_test;;
2962	fib_down_test|down)		fib_down_test;;
2963	fib_carrier_test|carrier)	fib_carrier_test;;
2964	fib_rp_filter_test|rp_filter)	fib_rp_filter_test;;
2965	fib_nexthop_test|nexthop)	fib_nexthop_test;;
2966	fib_notify_test|ipv4_notify)	fib_notify_test;;
2967	fib6_notify_test|ipv6_notify)	fib6_notify_test;;
2968	fib_suppress_test|suppress)	fib_suppress_test;;
2969	ipv6_route_test|ipv6_rt)	ipv6_route_test;;
2970	ipv4_route_test|ipv4_rt)	ipv4_route_test;;
2971	ipv6_addr_metric)		ipv6_addr_metric_test;;
2972	ipv4_addr_metric)		ipv4_addr_metric_test;;
2973	ipv4_del_addr)			ipv4_del_addr_test;;
2974	ipv6_del_addr)			ipv6_del_addr_test;;
2975	ipv6_route_metrics)		ipv6_route_metrics_test;;
2976	ipv4_route_metrics)		ipv4_route_metrics_test;;
2977	ipv4_route_v6_gw)		ipv4_route_v6_gw_test;;
2978	ipv4_mangle)			ipv4_mangle_test;;
2979	ipv6_mangle)			ipv6_mangle_test;;
2980	ipv4_bcast_neigh)		ipv4_bcast_neigh_test;;
2981	fib6_gc_test|ipv6_gc)		fib6_gc_test;;
2982	ipv4_mpath_list)		ipv4_mpath_list_test;;
2983	ipv6_mpath_list)		ipv6_mpath_list_test;;
2984	ipv4_mpath_balance)		ipv4_mpath_balance_test;;
2985	ipv6_mpath_balance)		ipv6_mpath_balance_test;;
2986	ipv4_mpath_balance_preferred)	ipv4_mpath_balance_preferred_test;;
2987	fib6_ra_to_static)		fib6_ra_to_static;;
2988
2989	help) echo "Test names: $TESTS"; exit 0;;
2990	esac
2991done
2992
2993if [ "$TESTS" != "none" ]; then
2994	printf "\nTests passed: %3d\n" ${nsuccess}
2995	printf "Tests failed: %3d\n"   ${nfail}
2996fi
2997
2998exit $ret
2999