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