1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# ns: me               | ns: peer              | ns: remote
5#   2001:db8:91::1     |       2001:db8:91::2  |
6#   172.16.1.1         |       172.16.1.2      |
7#            veth1 <---|---> veth2             |
8#                      |              veth5 <--|--> veth6  172.16.101.1
9#            veth3 <---|---> veth4             |           2001:db8:101::1
10#   172.16.2.1         |       172.16.2.2      |
11#   2001:db8:92::1     |       2001:db8:92::2  |
12#
13# This test is for checking IPv4 and IPv6 FIB behavior with nexthop
14# objects. Device reference counts and network namespace cleanup tested
15# by use of network namespace for peer.
16
17ret=0
18# Kselftest framework requirement - SKIP code is 4.
19ksft_skip=4
20
21# all tests in this script. Can be overridden with -t option
22IPV4_TESTS="ipv4_fcnal ipv4_grp_fcnal ipv4_withv6_fcnal ipv4_fcnal_runtime"
23IPV6_TESTS="ipv6_fcnal ipv6_grp_fcnal ipv6_fcnal_runtime"
24
25ALL_TESTS="basic ${IPV4_TESTS} ${IPV6_TESTS}"
26TESTS="${ALL_TESTS}"
27VERBOSE=0
28PAUSE_ON_FAIL=no
29PAUSE=no
30
31nsid=100
32
33################################################################################
34# utilities
35
36log_test()
37{
38	local rc=$1
39	local expected=$2
40	local msg="$3"
41
42	if [ ${rc} -eq ${expected} ]; then
43		printf "TEST: %-60s  [ OK ]\n" "${msg}"
44		nsuccess=$((nsuccess+1))
45	else
46		ret=1
47		nfail=$((nfail+1))
48		printf "TEST: %-60s  [FAIL]\n" "${msg}"
49		if [ "$VERBOSE" = "1" ]; then
50			echo "    rc=$rc, expected $expected"
51		fi
52
53		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
54		echo
55			echo "hit enter to continue, 'q' to quit"
56			read a
57			[ "$a" = "q" ] && exit 1
58		fi
59	fi
60
61	if [ "${PAUSE}" = "yes" ]; then
62		echo
63		echo "hit enter to continue, 'q' to quit"
64		read a
65		[ "$a" = "q" ] && exit 1
66	fi
67
68	[ "$VERBOSE" = "1" ] && echo
69}
70
71run_cmd()
72{
73	local cmd="$1"
74	local out
75	local stderr="2>/dev/null"
76
77	if [ "$VERBOSE" = "1" ]; then
78		printf "COMMAND: $cmd\n"
79		stderr=
80	fi
81
82	out=$(eval $cmd $stderr)
83	rc=$?
84	if [ "$VERBOSE" = "1" -a -n "$out" ]; then
85		echo "    $out"
86	fi
87
88	return $rc
89}
90
91get_linklocal()
92{
93	local dev=$1
94	local ns
95	local addr
96
97	[ -n "$2" ] && ns="-netns $2"
98	addr=$(ip $ns -6 -br addr show dev ${dev} | \
99	awk '{
100		for (i = 3; i <= NF; ++i) {
101			if ($i ~ /^fe80/)
102				print $i
103		}
104	}'
105	)
106	addr=${addr/\/*}
107
108	[ -z "$addr" ] && return 1
109
110	echo $addr
111
112	return 0
113}
114
115create_ns()
116{
117	local n=${1}
118
119	ip netns del ${n} 2>/dev/null
120
121	set -e
122	ip netns add ${n}
123	ip netns set ${n} $((nsid++))
124	ip -netns ${n} addr add 127.0.0.1/8 dev lo
125	ip -netns ${n} link set lo up
126
127	ip netns exec ${n} sysctl -qw net.ipv4.ip_forward=1
128	ip netns exec ${n} sysctl -qw net.ipv4.fib_multipath_use_neigh=1
129	ip netns exec ${n} sysctl -qw net.ipv4.conf.default.ignore_routes_with_linkdown=1
130	ip netns exec ${n} sysctl -qw net.ipv6.conf.all.keep_addr_on_down=1
131	ip netns exec ${n} sysctl -qw net.ipv6.conf.all.forwarding=1
132	ip netns exec ${n} sysctl -qw net.ipv6.conf.default.forwarding=1
133	ip netns exec ${n} sysctl -qw net.ipv6.conf.default.ignore_routes_with_linkdown=1
134	ip netns exec ${n} sysctl -qw net.ipv6.conf.all.accept_dad=0
135	ip netns exec ${n} sysctl -qw net.ipv6.conf.default.accept_dad=0
136
137	set +e
138}
139
140setup()
141{
142	cleanup
143
144	create_ns me
145	create_ns peer
146	create_ns remote
147
148	IP="ip -netns me"
149	set -e
150	$IP li add veth1 type veth peer name veth2
151	$IP li set veth1 up
152	$IP addr add 172.16.1.1/24 dev veth1
153	$IP -6 addr add 2001:db8:91::1/64 dev veth1
154
155	$IP li add veth3 type veth peer name veth4
156	$IP li set veth3 up
157	$IP addr add 172.16.2.1/24 dev veth3
158	$IP -6 addr add 2001:db8:92::1/64 dev veth3
159
160	$IP li set veth2 netns peer up
161	ip -netns peer addr add 172.16.1.2/24 dev veth2
162	ip -netns peer -6 addr add 2001:db8:91::2/64 dev veth2
163
164	$IP li set veth4 netns peer up
165	ip -netns peer addr add 172.16.2.2/24 dev veth4
166	ip -netns peer -6 addr add 2001:db8:92::2/64 dev veth4
167
168	ip -netns remote li add veth5 type veth peer name veth6
169	ip -netns remote li set veth5 up
170	ip -netns remote addr add dev veth5 172.16.101.1/24
171	ip -netns remote addr add dev veth5 2001:db8:101::1/64
172	ip -netns remote ro add 172.16.0.0/22 via 172.16.101.2
173	ip -netns remote -6 ro add 2001:db8:90::/40 via 2001:db8:101::2
174
175	ip -netns remote li set veth6 netns peer up
176	ip -netns peer addr add dev veth6 172.16.101.2/24
177	ip -netns peer addr add dev veth6 2001:db8:101::2/64
178	set +e
179}
180
181cleanup()
182{
183	local ns
184
185	for ns in me peer remote; do
186		ip netns del ${ns} 2>/dev/null
187	done
188}
189
190check_output()
191{
192	local out="$1"
193	local expected="$2"
194	local rc=0
195
196	[ "${out}" = "${expected}" ] && return 0
197
198	if [ -z "${out}" ]; then
199		if [ "$VERBOSE" = "1" ]; then
200			printf "\nNo entry found\n"
201			printf "Expected:\n"
202			printf "    ${expected}\n"
203		fi
204		return 1
205	fi
206
207	out=$(echo ${out})
208	if [ "${out}" != "${expected}" ]; then
209		rc=1
210		if [ "${VERBOSE}" = "1" ]; then
211			printf "    Unexpected entry. Have:\n"
212			printf "        ${out}\n"
213			printf "    Expected:\n"
214			printf "        ${expected}\n\n"
215		else
216			echo "      WARNING: Unexpected route entry"
217		fi
218	fi
219
220	return $rc
221}
222
223check_nexthop()
224{
225	local nharg="$1"
226	local expected="$2"
227	local out
228
229	out=$($IP nexthop ls ${nharg} 2>/dev/null)
230
231	check_output "${out}" "${expected}"
232}
233
234check_route()
235{
236	local pfx="$1"
237	local expected="$2"
238	local out
239
240	out=$($IP route ls match ${pfx} 2>/dev/null)
241
242	check_output "${out}" "${expected}"
243}
244
245check_route6()
246{
247	local pfx="$1"
248	local expected="$2"
249	local out
250
251	out=$($IP -6 route ls match ${pfx} 2>/dev/null)
252
253	check_output "${out}" "${expected}"
254}
255
256################################################################################
257# basic operations (add, delete, replace) on nexthops and nexthop groups
258#
259# IPv6
260
261ipv6_fcnal()
262{
263	local rc
264
265	echo
266	echo "IPv6"
267	echo "----------------------"
268
269	run_cmd "$IP nexthop add id 52 via 2001:db8:91::2 dev veth1"
270	rc=$?
271	log_test $rc 0 "Create nexthop with id, gw, dev"
272	if [ $rc -ne 0 ]; then
273		echo "Basic IPv6 create fails; can not continue"
274		return 1
275	fi
276
277	run_cmd "$IP nexthop get id 52"
278	log_test $? 0 "Get nexthop by id"
279	check_nexthop "id 52" "id 52 via 2001:db8:91::2 dev veth1 scope link"
280
281	run_cmd "$IP nexthop del id 52"
282	log_test $? 0 "Delete nexthop by id"
283	check_nexthop "id 52" ""
284
285	#
286	# gw, device spec
287	#
288	# gw validation, no device - fails since dev required
289	run_cmd "$IP nexthop add id 52 via 2001:db8:92::3"
290	log_test $? 2 "Create nexthop - gw only"
291
292	# gw is not reachable throught given dev
293	run_cmd "$IP nexthop add id 53 via 2001:db8:3::3 dev veth1"
294	log_test $? 2 "Create nexthop - invalid gw+dev combination"
295
296	# onlink arg overrides gw+dev lookup
297	run_cmd "$IP nexthop add id 53 via 2001:db8:3::3 dev veth1 onlink"
298	log_test $? 0 "Create nexthop - gw+dev and onlink"
299
300	# admin down should delete nexthops
301	set -e
302	run_cmd "$IP -6 nexthop add id 55 via 2001:db8:91::3 dev veth1"
303	run_cmd "$IP nexthop add id 56 via 2001:db8:91::4 dev veth1"
304	run_cmd "$IP nexthop add id 57 via 2001:db8:91::5 dev veth1"
305	run_cmd "$IP li set dev veth1 down"
306	set +e
307	check_nexthop "dev veth1" ""
308	log_test $? 0 "Nexthops removed on admin down"
309}
310
311ipv6_grp_fcnal()
312{
313	local rc
314
315	echo
316	echo "IPv6 groups functional"
317	echo "----------------------"
318
319	# basic functionality: create a nexthop group, default weight
320	run_cmd "$IP nexthop add id 61 via 2001:db8:91::2 dev veth1"
321	run_cmd "$IP nexthop add id 101 group 61"
322	log_test $? 0 "Create nexthop group with single nexthop"
323
324	# get nexthop group
325	run_cmd "$IP nexthop get id 101"
326	log_test $? 0 "Get nexthop group by id"
327	check_nexthop "id 101" "id 101 group 61"
328
329	# delete nexthop group
330	run_cmd "$IP nexthop del id 101"
331	log_test $? 0 "Delete nexthop group by id"
332	check_nexthop "id 101" ""
333
334	$IP nexthop flush >/dev/null 2>&1
335	check_nexthop "id 101" ""
336
337	#
338	# create group with multiple nexthops - mix of gw and dev only
339	#
340	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
341	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
342	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
343	run_cmd "$IP nexthop add id 65 dev veth1"
344	run_cmd "$IP nexthop add id 102 group 62/63/64/65"
345	log_test $? 0 "Nexthop group with multiple nexthops"
346	check_nexthop "id 102" "id 102 group 62/63/64/65"
347
348	# Delete nexthop in a group and group is updated
349	run_cmd "$IP nexthop del id 63"
350	check_nexthop "id 102" "id 102 group 62/64/65"
351	log_test $? 0 "Nexthop group updated when entry is deleted"
352
353	# create group with multiple weighted nexthops
354	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
355	run_cmd "$IP nexthop add id 103 group 62/63,2/64,3/65,4"
356	log_test $? 0 "Nexthop group with weighted nexthops"
357	check_nexthop "id 103" "id 103 group 62/63,2/64,3/65,4"
358
359	# Delete nexthop in a weighted group and group is updated
360	run_cmd "$IP nexthop del id 63"
361	check_nexthop "id 103" "id 103 group 62/64,3/65,4"
362	log_test $? 0 "Weighted nexthop group updated when entry is deleted"
363
364	# admin down - nexthop is removed from group
365	run_cmd "$IP li set dev veth1 down"
366	check_nexthop "dev veth1" ""
367	log_test $? 0 "Nexthops in groups removed on admin down"
368
369	# expect groups to have been deleted as well
370	check_nexthop "" ""
371
372	run_cmd "$IP li set dev veth1 up"
373
374	$IP nexthop flush >/dev/null 2>&1
375
376	# group with nexthops using different devices
377	set -e
378	run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1"
379	run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1"
380	run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1"
381	run_cmd "$IP nexthop add id 65 via 2001:db8:91::5 dev veth1"
382
383	run_cmd "$IP nexthop add id 72 via 2001:db8:92::2 dev veth3"
384	run_cmd "$IP nexthop add id 73 via 2001:db8:92::3 dev veth3"
385	run_cmd "$IP nexthop add id 74 via 2001:db8:92::4 dev veth3"
386	run_cmd "$IP nexthop add id 75 via 2001:db8:92::5 dev veth3"
387	set +e
388
389	# multiple groups with same nexthop
390	run_cmd "$IP nexthop add id 104 group 62"
391	run_cmd "$IP nexthop add id 105 group 62"
392	check_nexthop "group" "id 104 group 62 id 105 group 62"
393	log_test $? 0 "Multiple groups with same nexthop"
394
395	run_cmd "$IP nexthop flush groups"
396	[ $? -ne 0 ] && return 1
397
398	# on admin down of veth1, it should be removed from the group
399	run_cmd "$IP nexthop add id 105 group 62/63/72/73/64"
400	run_cmd "$IP li set veth1 down"
401	check_nexthop "id 105" "id 105 group 72/73"
402	log_test $? 0 "Nexthops in group removed on admin down - mixed group"
403
404	run_cmd "$IP nexthop add id 106 group 105/74"
405	log_test $? 2 "Nexthop group can not have a group as an entry"
406
407	# a group can have a blackhole entry only if it is the only
408	# nexthop in the group. Needed for atomic replace with an
409	# actual nexthop group
410	run_cmd "$IP -6 nexthop add id 31 blackhole"
411	run_cmd "$IP nexthop add id 107 group 31"
412	log_test $? 0 "Nexthop group with a blackhole entry"
413
414	run_cmd "$IP nexthop add id 108 group 31/24"
415	log_test $? 2 "Nexthop group can not have a blackhole and another nexthop"
416}
417
418ipv6_fcnal_runtime()
419{
420	local rc
421
422	echo
423	echo "IPv6 functional runtime"
424	echo "-----------------------"
425
426	sleep 5
427
428	#
429	# IPv6 - the basics
430	#
431	run_cmd "$IP nexthop add id 81 via 2001:db8:91::2 dev veth1"
432	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
433	log_test $? 0 "Route add"
434
435	run_cmd "$IP ro delete 2001:db8:101::1/128 nhid 81"
436	log_test $? 0 "Route delete"
437
438	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
439	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
440	log_test $? 0 "Ping with nexthop"
441
442	run_cmd "$IP nexthop add id 82 via 2001:db8:92::2 dev veth3"
443	run_cmd "$IP nexthop add id 122 group 81/82"
444	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
445	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
446	log_test $? 0 "Ping - multipath"
447
448	#
449	# IPv6 with blackhole nexthops
450	#
451	run_cmd "$IP -6 nexthop add id 83 blackhole"
452	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 83"
453	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
454	log_test $? 2 "Ping - blackhole"
455
456	run_cmd "$IP nexthop replace id 83 via 2001:db8:91::2 dev veth1"
457	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
458	log_test $? 0 "Ping - blackhole replaced with gateway"
459
460	run_cmd "$IP -6 nexthop replace id 83 blackhole"
461	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
462	log_test $? 2 "Ping - gateway replaced by blackhole"
463
464	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
465	run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
466	if [ $? -eq 0 ]; then
467		run_cmd "$IP nexthop replace id 122 group 83"
468		run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
469		log_test $? 2 "Ping - group with blackhole"
470
471		run_cmd "$IP nexthop replace id 122 group 81/82"
472		run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1"
473		log_test $? 0 "Ping - group blackhole replaced with gateways"
474	else
475		log_test 2 0 "Ping - multipath failed"
476	fi
477
478	#
479	# device only and gw + dev only mix
480	#
481	run_cmd "$IP -6 nexthop add id 85 dev veth1"
482	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 85"
483	log_test $? 0 "IPv6 route with device only nexthop"
484	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 85 dev veth1 metric 1024 pref medium"
485
486	run_cmd "$IP nexthop add id 123 group 81/85"
487	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 123"
488	log_test $? 0 "IPv6 multipath route with nexthop mix - dev only + gw"
489	check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 123 metric 1024 nexthop via 2001:db8:91::2 dev veth1 weight 1 nexthop dev veth1 weight 1 pref medium"
490
491	#
492	# IPv6 route with v4 nexthop - not allowed
493	#
494	run_cmd "$IP ro delete 2001:db8:101::1/128"
495	run_cmd "$IP nexthop add id 84 via 172.16.1.1 dev veth1"
496	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 84"
497	log_test $? 2 "IPv6 route can not have a v4 gateway"
498
499	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 81"
500	run_cmd "$IP nexthop replace id 81 via 172.16.1.1 dev veth1"
501	log_test $? 2 "Nexthop replace - v6 route, v4 nexthop"
502
503	run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122"
504	run_cmd "$IP nexthop replace id 81 via 172.16.1.1 dev veth1"
505	log_test $? 2 "Nexthop replace of group entry - v6 route, v4 nexthop"
506
507	$IP nexthop flush >/dev/null 2>&1
508
509	#
510	# weird IPv6 cases
511	#
512	run_cmd "$IP nexthop add id 86 via 2001:db8:91::2 dev veth1"
513	run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81"
514
515	# TO-DO:
516	# existing route with old nexthop; append route with new nexthop
517	# existing route with old nexthop; replace route with new
518	# existing route with new nexthop; replace route with old
519	# route with src address and using nexthop - not allowed
520}
521
522ipv4_fcnal()
523{
524	local rc
525
526	echo
527	echo "IPv4 functional"
528	echo "----------------------"
529
530	#
531	# basic IPv4 ops - add, get, delete
532	#
533	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
534	rc=$?
535	log_test $rc 0 "Create nexthop with id, gw, dev"
536	if [ $rc -ne 0 ]; then
537		echo "Basic IPv4 create fails; can not continue"
538		return 1
539	fi
540
541	run_cmd "$IP nexthop get id 12"
542	log_test $? 0 "Get nexthop by id"
543	check_nexthop "id 12" "id 12 via 172.16.1.2 dev veth1 scope link"
544
545	run_cmd "$IP nexthop del id 12"
546	log_test $? 0 "Delete nexthop by id"
547	check_nexthop "id 52" ""
548
549	#
550	# gw, device spec
551	#
552	# gw validation, no device - fails since dev is required
553	run_cmd "$IP nexthop add id 12 via 172.16.2.3"
554	log_test $? 2 "Create nexthop - gw only"
555
556	# gw not reachable through given dev
557	run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1"
558	log_test $? 2 "Create nexthop - invalid gw+dev combination"
559
560	# onlink flag overrides gw+dev lookup
561	run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1 onlink"
562	log_test $? 0 "Create nexthop - gw+dev and onlink"
563
564	# admin down should delete nexthops
565	set -e
566	run_cmd "$IP nexthop add id 15 via 172.16.1.3 dev veth1"
567	run_cmd "$IP nexthop add id 16 via 172.16.1.4 dev veth1"
568	run_cmd "$IP nexthop add id 17 via 172.16.1.5 dev veth1"
569	run_cmd "$IP li set dev veth1 down"
570	set +e
571	check_nexthop "dev veth1" ""
572	log_test $? 0 "Nexthops removed on admin down"
573}
574
575ipv4_grp_fcnal()
576{
577	local rc
578
579	echo
580	echo "IPv4 groups functional"
581	echo "----------------------"
582
583	# basic functionality: create a nexthop group, default weight
584	run_cmd "$IP nexthop add id 11 via 172.16.1.2 dev veth1"
585	run_cmd "$IP nexthop add id 101 group 11"
586	log_test $? 0 "Create nexthop group with single nexthop"
587
588	# get nexthop group
589	run_cmd "$IP nexthop get id 101"
590	log_test $? 0 "Get nexthop group by id"
591	check_nexthop "id 101" "id 101 group 11"
592
593	# delete nexthop group
594	run_cmd "$IP nexthop del id 101"
595	log_test $? 0 "Delete nexthop group by id"
596	check_nexthop "id 101" ""
597
598	$IP nexthop flush >/dev/null 2>&1
599
600	#
601	# create group with multiple nexthops
602	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
603	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
604	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
605	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
606	run_cmd "$IP nexthop add id 102 group 12/13/14/15"
607	log_test $? 0 "Nexthop group with multiple nexthops"
608	check_nexthop "id 102" "id 102 group 12/13/14/15"
609
610	# Delete nexthop in a group and group is updated
611	run_cmd "$IP nexthop del id 13"
612	check_nexthop "id 102" "id 102 group 12/14/15"
613	log_test $? 0 "Nexthop group updated when entry is deleted"
614
615	# create group with multiple weighted nexthops
616	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
617	run_cmd "$IP nexthop add id 103 group 12/13,2/14,3/15,4"
618	log_test $? 0 "Nexthop group with weighted nexthops"
619	check_nexthop "id 103" "id 103 group 12/13,2/14,3/15,4"
620
621	# Delete nexthop in a weighted group and group is updated
622	run_cmd "$IP nexthop del id 13"
623	check_nexthop "id 103" "id 103 group 12/14,3/15,4"
624	log_test $? 0 "Weighted nexthop group updated when entry is deleted"
625
626	# admin down - nexthop is removed from group
627	run_cmd "$IP li set dev veth1 down"
628	check_nexthop "dev veth1" ""
629	log_test $? 0 "Nexthops in groups removed on admin down"
630
631	# expect groups to have been deleted as well
632	check_nexthop "" ""
633
634	run_cmd "$IP li set dev veth1 up"
635
636	$IP nexthop flush >/dev/null 2>&1
637
638	# group with nexthops using different devices
639	set -e
640	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
641	run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1"
642	run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1"
643	run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1"
644
645	run_cmd "$IP nexthop add id 22 via 172.16.2.2 dev veth3"
646	run_cmd "$IP nexthop add id 23 via 172.16.2.3 dev veth3"
647	run_cmd "$IP nexthop add id 24 via 172.16.2.4 dev veth3"
648	run_cmd "$IP nexthop add id 25 via 172.16.2.5 dev veth3"
649	set +e
650
651	# multiple groups with same nexthop
652	run_cmd "$IP nexthop add id 104 group 12"
653	run_cmd "$IP nexthop add id 105 group 12"
654	check_nexthop "group" "id 104 group 12 id 105 group 12"
655	log_test $? 0 "Multiple groups with same nexthop"
656
657	run_cmd "$IP nexthop flush groups"
658	[ $? -ne 0 ] && return 1
659
660	# on admin down of veth1, it should be removed from the group
661	run_cmd "$IP nexthop add id 105 group 12/13/22/23/14"
662	run_cmd "$IP li set veth1 down"
663	check_nexthop "id 105" "id 105 group 22/23"
664	log_test $? 0 "Nexthops in group removed on admin down - mixed group"
665
666	run_cmd "$IP nexthop add id 106 group 105/24"
667	log_test $? 2 "Nexthop group can not have a group as an entry"
668
669	# a group can have a blackhole entry only if it is the only
670	# nexthop in the group. Needed for atomic replace with an
671	# actual nexthop group
672	run_cmd "$IP nexthop add id 31 blackhole"
673	run_cmd "$IP nexthop add id 107 group 31"
674	log_test $? 0 "Nexthop group with a blackhole entry"
675
676	run_cmd "$IP nexthop add id 108 group 31/24"
677	log_test $? 2 "Nexthop group can not have a blackhole and another nexthop"
678}
679
680ipv4_withv6_fcnal()
681{
682	local lladdr
683
684	set -e
685	lladdr=$(get_linklocal veth2 peer)
686	run_cmd "$IP nexthop add id 11 via ${lladdr} dev veth1"
687	set +e
688	run_cmd "$IP ro add 172.16.101.1/32 nhid 11"
689	log_test $? 0 "IPv6 nexthop with IPv4 route"
690	check_route "172.16.101.1" "172.16.101.1 nhid 11 via inet6 ${lladdr} dev veth1"
691
692	set -e
693	run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1"
694	run_cmd "$IP nexthop add id 101 group 11/12"
695	set +e
696	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
697	log_test $? 0 "IPv6 nexthop with IPv4 route"
698
699	check_route "172.16.101.1" "172.16.101.1 nhid 101 nexthop via inet6 ${lladdr} dev veth1 weight 1 nexthop via 172.16.1.2 dev veth1 weight 1"
700
701	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
702	log_test $? 0 "IPv4 route with IPv6 gateway"
703	check_route "172.16.101.1" "172.16.101.1 via inet6 ${lladdr} dev veth1"
704
705	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 2001:db8:50::1 dev veth1"
706	log_test $? 2 "IPv4 route with invalid IPv6 gateway"
707}
708
709ipv4_fcnal_runtime()
710{
711	local lladdr
712	local rc
713
714	echo
715	echo "IPv4 functional runtime"
716	echo "-----------------------"
717
718	run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1"
719	run_cmd "$IP ro add 172.16.101.1/32 nhid 21"
720	log_test $? 0 "Route add"
721	check_route "172.16.101.1" "172.16.101.1 nhid 21 via 172.16.1.2 dev veth1"
722
723	run_cmd "$IP ro delete 172.16.101.1/32 nhid 21"
724	log_test $? 0 "Route delete"
725
726	#
727	# scope mismatch
728	#
729	run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1"
730	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
731	log_test $? 2 "Route add - scope conflict with nexthop"
732
733	run_cmd "$IP nexthop replace id 22 dev veth3"
734	run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host"
735	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
736	log_test $? 2 "Nexthop replace with invalid scope for existing route"
737
738	#
739	# add route with nexthop and check traffic
740	#
741	run_cmd "$IP nexthop replace id 21 via 172.16.1.2 dev veth1"
742	run_cmd "$IP ro replace 172.16.101.1/32 nhid 21"
743	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
744	log_test $? 0 "Basic ping"
745
746	run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3"
747	run_cmd "$IP nexthop add id 122 group 21/22"
748	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
749	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
750	log_test $? 0 "Ping - multipath"
751
752	#
753	# IPv4 with blackhole nexthops
754	#
755	run_cmd "$IP nexthop add id 23 blackhole"
756	run_cmd "$IP ro replace 172.16.101.1/32 nhid 23"
757	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
758	log_test $? 2 "Ping - blackhole"
759
760	run_cmd "$IP nexthop replace id 23 via 172.16.1.2 dev veth1"
761	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
762	log_test $? 0 "Ping - blackhole replaced with gateway"
763
764	run_cmd "$IP nexthop replace id 23 blackhole"
765	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
766	log_test $? 2 "Ping - gateway replaced by blackhole"
767
768	run_cmd "$IP ro replace 172.16.101.1/32 nhid 122"
769	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
770	if [ $? -eq 0 ]; then
771		run_cmd "$IP nexthop replace id 122 group 23"
772		run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
773		log_test $? 2 "Ping - group with blackhole"
774
775		run_cmd "$IP nexthop replace id 122 group 21/22"
776		run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
777		log_test $? 0 "Ping - group blackhole replaced with gateways"
778	else
779		log_test 2 0 "Ping - multipath failed"
780	fi
781
782	#
783	# device only and gw + dev only mix
784	#
785	run_cmd "$IP nexthop add id 85 dev veth1"
786	run_cmd "$IP ro replace 172.16.101.1/32 nhid 85"
787	log_test $? 0 "IPv4 route with device only nexthop"
788	check_route "172.16.101.1" "172.16.101.1 nhid 85 dev veth1"
789
790	run_cmd "$IP nexthop add id 123 group 21/85"
791	run_cmd "$IP ro replace 172.16.101.1/32 nhid 123"
792	log_test $? 0 "IPv4 multipath route with nexthop mix - dev only + gw"
793	check_route "172.16.101.1" "172.16.101.1 nhid 123 nexthop via 172.16.1.2 dev veth1 weight 1 nexthop dev veth1 weight 1"
794
795	#
796	# IPv4 with IPv6
797	#
798	set -e
799	lladdr=$(get_linklocal veth2 peer)
800	run_cmd "$IP nexthop add id 24 via ${lladdr} dev veth1"
801	set +e
802	run_cmd "$IP ro replace 172.16.101.1/32 nhid 24"
803	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
804	log_test $? 0 "IPv6 nexthop with IPv4 route"
805
806	$IP neigh sh | grep -q "${lladdr} dev veth1"
807	if [ $? -eq 1 ]; then
808		echo "    WARNING: Neigh entry missing for ${lladdr}"
809		$IP neigh sh | grep 'dev veth1'
810	fi
811
812	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
813	if [ $? -eq 0 ]; then
814		echo "    WARNING: Neigh entry exists for 172.16.101.1"
815		$IP neigh sh | grep 'dev veth1'
816	fi
817
818	set -e
819	run_cmd "$IP nexthop add id 25 via 172.16.1.2 dev veth1"
820	run_cmd "$IP nexthop add id 101 group 24/25"
821	set +e
822	run_cmd "$IP ro replace 172.16.101.1/32 nhid 101"
823	log_test $? 0 "IPv4 route with mixed v4-v6 multipath route"
824
825	check_route "172.16.101.1" "172.16.101.1 nhid 101 nexthop via inet6 ${lladdr} dev veth1 weight 1 nexthop via 172.16.1.2 dev veth1 weight 1"
826
827	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
828	log_test $? 0 "IPv6 nexthop with IPv4 route"
829
830	run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1"
831	run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1"
832	log_test $? 0 "IPv4 route with IPv6 gateway"
833
834	$IP neigh sh | grep -q "${lladdr} dev veth1"
835	if [ $? -eq 1 ]; then
836		echo "    WARNING: Neigh entry missing for ${lladdr}"
837		$IP neigh sh | grep 'dev veth1'
838	fi
839
840	$IP neigh sh | grep -q "172.16.101.1 dev eth1"
841	if [ $? -eq 0 ]; then
842		echo "    WARNING: Neigh entry exists for 172.16.101.1"
843		$IP neigh sh | grep 'dev veth1'
844	fi
845
846	#
847	# MPLS as an example of LWT encap
848	#
849	run_cmd "$IP nexthop add id 51 encap mpls 101 via 172.16.1.2 dev veth1"
850	log_test $? 0 "IPv4 route with MPLS encap"
851	check_nexthop "id 51" "id 51 encap mpls 101 via 172.16.1.2 dev veth1 scope link"
852	log_test $? 0 "IPv4 route with MPLS encap - check"
853
854	run_cmd "$IP nexthop add id 52 encap mpls 102 via inet6 2001:db8:91::2 dev veth1"
855	log_test $? 0 "IPv4 route with MPLS encap and v6 gateway"
856	check_nexthop "id 52" "id 52 encap mpls 102 via 2001:db8:91::2 dev veth1 scope link"
857	log_test $? 0 "IPv4 route with MPLS encap, v6 gw - check"
858}
859
860basic()
861{
862	echo
863	echo "Basic functional tests"
864	echo "----------------------"
865	run_cmd "$IP nexthop ls"
866	log_test $? 0 "List with nothing defined"
867
868	run_cmd "$IP nexthop get id 1"
869	log_test $? 2 "Nexthop get on non-existent id"
870
871	# attempt to create nh without a device or gw - fails
872	run_cmd "$IP nexthop add id 1"
873	log_test $? 2 "Nexthop with no device or gateway"
874
875	# attempt to create nh with down device - fails
876	$IP li set veth1 down
877	run_cmd "$IP nexthop add id 1 dev veth1"
878	log_test $? 2 "Nexthop with down device"
879
880	# create nh with linkdown device - fails
881	$IP li set veth1 up
882	ip -netns peer li set veth2 down
883	run_cmd "$IP nexthop add id 1 dev veth1"
884	log_test $? 2 "Nexthop with device that is linkdown"
885	ip -netns peer li set veth2 up
886
887	# device only
888	run_cmd "$IP nexthop add id 1 dev veth1"
889	log_test $? 0 "Nexthop with device only"
890
891	# create nh with duplicate id
892	run_cmd "$IP nexthop add id 1 dev veth3"
893	log_test $? 2 "Nexthop with duplicate id"
894
895	# blackhole nexthop
896	run_cmd "$IP nexthop add id 2 blackhole"
897	log_test $? 0 "Blackhole nexthop"
898
899	# blackhole nexthop can not have other specs
900	run_cmd "$IP nexthop replace id 2 blackhole dev veth1"
901	log_test $? 2 "Blackhole nexthop with other attributes"
902
903	#
904	# groups
905	#
906
907	run_cmd "$IP nexthop add id 101 group 1"
908	log_test $? 0 "Create group"
909
910	run_cmd "$IP nexthop add id 102 group 2"
911	log_test $? 0 "Create group with blackhole nexthop"
912
913	# multipath group can not have a blackhole as 1 path
914	run_cmd "$IP nexthop add id 103 group 1/2"
915	log_test $? 2 "Create multipath group where 1 path is a blackhole"
916
917	# multipath group can not have a member replaced by a blackhole
918	run_cmd "$IP nexthop replace id 2 dev veth3"
919	run_cmd "$IP nexthop replace id 102 group 1/2"
920	run_cmd "$IP nexthop replace id 2 blackhole"
921	log_test $? 2 "Multipath group can not have a member replaced by blackhole"
922
923	# attempt to create group with non-existent nexthop
924	run_cmd "$IP nexthop add id 103 group 12"
925	log_test $? 2 "Create group with non-existent nexthop"
926
927	# attempt to create group with same nexthop
928	run_cmd "$IP nexthop add id 103 group 1/1"
929	log_test $? 2 "Create group with same nexthop multiple times"
930
931	# replace nexthop with a group - fails
932	run_cmd "$IP nexthop replace id 2 group 1"
933	log_test $? 2 "Replace nexthop with nexthop group"
934
935	# replace nexthop group with a nexthop - fails
936	run_cmd "$IP nexthop replace id 101 dev veth1"
937	log_test $? 2 "Replace nexthop group with nexthop"
938
939	# nexthop group with other attributes fail
940	run_cmd "$IP nexthop add id 104 group 1 dev veth1"
941	log_test $? 2 "Nexthop group and device"
942
943	# Tests to ensure that flushing works as expected.
944	run_cmd "$IP nexthop add id 105 blackhole proto 99"
945	run_cmd "$IP nexthop add id 106 blackhole proto 100"
946	run_cmd "$IP nexthop add id 107 blackhole proto 99"
947	run_cmd "$IP nexthop flush proto 99"
948	check_nexthop "id 105" ""
949	check_nexthop "id 106" "id 106 blackhole proto 100"
950	check_nexthop "id 107" ""
951	run_cmd "$IP nexthop flush proto 100"
952	check_nexthop "id 106" ""
953
954	run_cmd "$IP nexthop flush proto 100"
955	log_test $? 0 "Test proto flush"
956
957	run_cmd "$IP nexthop add id 104 group 1 blackhole"
958	log_test $? 2 "Nexthop group and blackhole"
959
960	$IP nexthop flush >/dev/null 2>&1
961}
962
963################################################################################
964# usage
965
966usage()
967{
968	cat <<EOF
969usage: ${0##*/} OPTS
970
971        -t <test>   Test(s) to run (default: all)
972                    (options: $ALL_TESTS)
973        -4          IPv4 tests only
974        -6          IPv6 tests only
975        -p          Pause on fail
976        -P          Pause after each test before cleanup
977        -v          verbose mode (show commands and output)
978
979    Runtime test
980	-n num	    Number of nexthops to target
981	-N    	    Use new style to install routes in DUT
982
983done
984EOF
985}
986
987################################################################################
988# main
989
990while getopts :t:pP46hv o
991do
992	case $o in
993		t) TESTS=$OPTARG;;
994		4) TESTS=${IPV4_TESTS};;
995		6) TESTS=${IPV6_TESTS};;
996		p) PAUSE_ON_FAIL=yes;;
997		P) PAUSE=yes;;
998		v) VERBOSE=$(($VERBOSE + 1));;
999		h) usage; exit 0;;
1000		*) usage; exit 1;;
1001	esac
1002done
1003
1004# make sure we don't pause twice
1005[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
1006
1007if [ "$(id -u)" -ne 0 ];then
1008	echo "SKIP: Need root privileges"
1009	exit $ksft_skip;
1010fi
1011
1012if [ ! -x "$(command -v ip)" ]; then
1013	echo "SKIP: Could not run test without ip tool"
1014	exit $ksft_skip
1015fi
1016
1017ip help 2>&1 | grep -q nexthop
1018if [ $? -ne 0 ]; then
1019	echo "SKIP: iproute2 too old, missing nexthop command"
1020	exit $ksft_skip
1021fi
1022
1023out=$(ip nexthop ls 2>&1 | grep -q "Operation not supported")
1024if [ $? -eq 0 ]; then
1025	echo "SKIP: kernel lacks nexthop support"
1026	exit $ksft_skip
1027fi
1028
1029for t in $TESTS
1030do
1031	case $t in
1032	none) IP="ip -netns peer"; setup; exit 0;;
1033	*) setup; $t; cleanup;;
1034	esac
1035done
1036
1037if [ "$TESTS" != "none" ]; then
1038	printf "\nTests passed: %3d\n" ${nsuccess}
1039	printf "Tests failed: %3d\n"   ${nfail}
1040fi
1041
1042exit $ret
1043