xref: /linux/tools/testing/selftests/net/fib_rule_tests.sh (revision ffa16b43078a90a43beebdf4fe8d4166665cf6cf)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# This test is for checking IPv4 and IPv6 FIB rules API
5
6source lib.sh
7ret=0
8PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}
9
10RTABLE=100
11RTABLE_PEER=101
12RTABLE_VRF=102
13GW_IP4=192.51.100.2
14SRC_IP=192.51.100.3
15GW_IP6=2001:db8:1::2
16SRC_IP6=2001:db8:1::3
17
18DEV_ADDR=192.51.100.1
19DEV_ADDR6=2001:db8:1::1
20DEV=dummy0
21TESTS="
22	fib_rule6
23	fib_rule4
24	fib_rule6_connect
25	fib_rule4_connect
26	fib_rule6_vrf
27	fib_rule4_vrf
28"
29
30SELFTEST_PATH=""
31
32log_test()
33{
34	log_test_expected "$1" "$2" "$3"
35}
36
37setup()
38{
39	set -e
40	setup_ns testns
41	IP="ip -netns $testns"
42
43	$IP link add dummy0 type dummy
44	$IP link set dev dummy0 up
45	$IP address add $DEV_ADDR/24 dev dummy0
46	$IP -6 address add $DEV_ADDR6/64 dev dummy0
47
48	set +e
49}
50
51cleanup()
52{
53	$IP link del dev dummy0 &> /dev/null
54	cleanup_ns $testns
55}
56
57setup_peer()
58{
59	set -e
60
61	setup_ns peerns
62	IP_PEER="ip -netns $peerns"
63	$IP_PEER link set dev lo up
64
65	ip link add name veth0 netns $testns type veth \
66		peer name veth1 netns $peerns
67	$IP link set dev veth0 up
68	$IP_PEER link set dev veth1 up
69
70	$IP address add 192.0.2.10 peer 192.0.2.11/32 dev veth0
71	$IP_PEER address add 192.0.2.11 peer 192.0.2.10/32 dev veth1
72
73	$IP address add 2001:db8::10 peer 2001:db8::11/128 dev veth0 nodad
74	$IP_PEER address add 2001:db8::11 peer 2001:db8::10/128 dev veth1 nodad
75
76	$IP_PEER address add 198.51.100.11/32 dev lo
77	$IP route add table $RTABLE_PEER 198.51.100.11/32 via 192.0.2.11
78
79	$IP_PEER address add 2001:db8::1:11/128 dev lo
80	$IP route add table $RTABLE_PEER 2001:db8::1:11/128 via 2001:db8::11
81
82	set +e
83}
84
85cleanup_peer()
86{
87	$IP link del dev veth0
88	ip netns del $peerns
89}
90
91setup_vrf()
92{
93	$IP link add name vrf0 up type vrf table $RTABLE_VRF
94	$IP link set dev $DEV master vrf0
95}
96
97cleanup_vrf()
98{
99	$IP link del dev vrf0
100}
101
102fib_check_iproute_support()
103{
104	ip rule help 2>&1 | grep -q $1
105	if [ $? -ne 0 ]; then
106		echo "SKIP: iproute2 iprule too old, missing $1 match"
107		return 1
108	fi
109
110	ip route get help 2>&1 | grep -q $2
111	if [ $? -ne 0 ]; then
112		echo "SKIP: iproute2 get route too old, missing $2 match"
113		return 1
114	fi
115
116	return 0
117}
118
119fib_rule6_del()
120{
121	$IP -6 rule del $1
122	log_test $? 0 "rule6 del $1"
123}
124
125fib_rule6_del_by_pref()
126{
127	pref=$($IP -6 rule show $1 table $RTABLE | cut -d ":" -f 1)
128	$IP -6 rule del pref $pref
129}
130
131fib_rule6_test_match_n_redirect()
132{
133	local match="$1"
134	local getmatch="$2"
135	local getnomatch="$3"
136	local description="$4"
137	local nomatch_description="$5"
138
139	$IP -6 rule add $match table $RTABLE
140	$IP -6 route get $GW_IP6 $getmatch | grep -q "table $RTABLE"
141	log_test $? 0 "rule6 check: $description"
142
143	$IP -6 route get $GW_IP6 $getnomatch 2>&1 | grep -q "table $RTABLE"
144	log_test $? 1 "rule6 check: $nomatch_description"
145
146	fib_rule6_del_by_pref "$match"
147	log_test $? 0 "rule6 del by pref: $description"
148}
149
150fib_rule6_test_reject()
151{
152	local match="$1"
153	local rc
154
155	$IP -6 rule add $match table $RTABLE 2>/dev/null
156	rc=$?
157	log_test $rc 2 "rule6 check: $match"
158
159	if [ $rc -eq 0 ]; then
160		$IP -6 rule del $match table $RTABLE
161	fi
162}
163
164fib_rule6_test()
165{
166	local ext_name=$1; shift
167	local getnomatch
168	local getmatch
169	local match
170	local cnt
171
172	echo
173	echo "IPv6 FIB rule tests $ext_name"
174
175	# setup the fib rule redirect route
176	$IP -6 route add table $RTABLE default via $GW_IP6 dev $DEV onlink
177
178	match="oif $DEV"
179	getnomatch="oif lo"
180	fib_rule6_test_match_n_redirect "$match" "$match" "$getnomatch" \
181		"oif redirect to table" "oif no redirect to table"
182
183	match="from $SRC_IP6 iif $DEV"
184	getnomatch="from $SRC_IP6 iif lo"
185	fib_rule6_test_match_n_redirect "$match" "$match" "$getnomatch" \
186		"iif redirect to table" "iif no redirect to table"
187
188	# Reject dsfield (tos) options which have ECN bits set
189	for cnt in $(seq 1 3); do
190		match="dsfield $cnt"
191		fib_rule6_test_reject "$match"
192	done
193
194	# Don't take ECN bits into account when matching on dsfield
195	match="tos 0x10"
196	for cnt in "0x10" "0x11" "0x12" "0x13"; do
197		# Using option 'tos' instead of 'dsfield' as old iproute2
198		# versions don't support 'dsfield' in ip rule show.
199		getmatch="tos $cnt"
200		getnomatch="tos 0x20"
201		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
202			"$getnomatch" "$getmatch redirect to table" \
203			"$getnomatch no redirect to table"
204	done
205
206	# Re-test TOS matching, but with input routes since they are handled
207	# differently from output routes.
208	match="tos 0x10"
209	for cnt in "0x10" "0x11" "0x12" "0x13"; do
210		getmatch="tos $cnt"
211		getnomatch="tos 0x20"
212		fib_rule6_test_match_n_redirect "$match" \
213			"from $SRC_IP6 iif $DEV $getmatch" \
214			"from $SRC_IP6 iif $DEV $getnomatch" \
215			"iif $getmatch redirect to table" \
216			"iif $getnomatch no redirect to table"
217	done
218
219	match="fwmark 0x64"
220	getmatch="mark 0x64"
221	getnomatch="mark 0x63"
222	fib_rule6_test_match_n_redirect "$match" "$getmatch" "$getnomatch" \
223		"fwmark redirect to table" "fwmark no redirect to table"
224
225	fib_check_iproute_support "uidrange" "uid"
226	if [ $? -eq 0 ]; then
227		match="uidrange 100-100"
228		getmatch="uid 100"
229		getnomatch="uid 101"
230		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
231			"$getnomatch" "uid redirect to table" \
232			"uid no redirect to table"
233	fi
234
235	fib_check_iproute_support "sport" "sport"
236	if [ $? -eq 0 ]; then
237		match="sport 666 dport 777"
238		getnomatch="sport 667 dport 778"
239		fib_rule6_test_match_n_redirect "$match" "$match" \
240			"$getnomatch" "sport and dport redirect to table" \
241			"sport and dport no redirect to table"
242
243		match="sport 100-200 dport 300-400"
244		getmatch="sport 100 dport 400"
245		getnomatch="sport 100 dport 401"
246		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
247			"$getnomatch" \
248			"sport and dport range redirect to table" \
249			"sport and dport range no redirect to table"
250	fi
251
252	ip rule help 2>&1 | grep sport | grep -q MASK
253	if [ $? -eq 0 ]; then
254		match="sport 0x0f00/0xff00 dport 0x000f/0x00ff"
255		getmatch="sport 0x0f11 dport 0x220f"
256		getnomatch="sport 0x1f11 dport 0x221f"
257		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
258			"$getnomatch" "sport and dport masked redirect to table" \
259			"sport and dport masked no redirect to table"
260	fi
261
262	fib_check_iproute_support "ipproto" "ipproto"
263	if [ $? -eq 0 ]; then
264		match="ipproto tcp"
265		getnomatch="ipproto udp"
266		fib_rule6_test_match_n_redirect "$match" "$match" \
267			"$getnomatch" "ipproto tcp match" "ipproto udp no match"
268	fi
269
270	fib_check_iproute_support "ipproto" "ipproto"
271	if [ $? -eq 0 ]; then
272		match="ipproto ipv6-icmp"
273		getnomatch="ipproto tcp"
274		fib_rule6_test_match_n_redirect "$match" "$match" \
275			"$getnomatch" "ipproto ipv6-icmp match" \
276			"ipproto ipv6-tcp no match"
277	fi
278
279	fib_check_iproute_support "dscp" "tos"
280	if [ $? -eq 0 ]; then
281		match="dscp 0x3f"
282		getmatch="tos 0xfc"
283		getnomatch="tos 0xf4"
284		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
285			"$getnomatch" "dscp redirect to table" \
286			"dscp no redirect to table"
287
288		match="dscp 0x3f"
289		getmatch="from $SRC_IP6 iif $DEV tos 0xfc"
290		getnomatch="from $SRC_IP6 iif $DEV tos 0xf4"
291		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
292			"$getnomatch" "iif dscp redirect to table" \
293			"iif dscp no redirect to table"
294	fi
295
296	ip rule help 2>&1 | grep -q "DSCP\[/MASK\]"
297	if [ $? -eq 0 ]; then
298		match="dscp 0x0f/0x0f"
299		tosmatch=$(printf 0x"%x" $((0x1f << 2)))
300		tosnomatch=$(printf 0x"%x" $((0x1e << 2)))
301		getmatch="tos $tosmatch"
302		getnomatch="tos $tosnomatch"
303		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
304			"$getnomatch" "dscp masked redirect to table" \
305			"dscp masked no redirect to table"
306
307		match="dscp 0x0f/0x0f"
308		getmatch="from $SRC_IP6 iif $DEV tos $tosmatch"
309		getnomatch="from $SRC_IP6 iif $DEV tos $tosnomatch"
310		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
311			"$getnomatch" "iif dscp masked redirect to table" \
312			"iif dscp masked no redirect to table"
313	fi
314
315	fib_check_iproute_support "flowlabel" "flowlabel"
316	if [ $? -eq 0 ]; then
317		match="flowlabel 0xfffff"
318		getmatch="flowlabel 0xfffff"
319		getnomatch="flowlabel 0xf"
320		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
321			"$getnomatch" "flowlabel redirect to table" \
322			"flowlabel no redirect to table"
323
324		match="flowlabel 0xfffff"
325		getmatch="from $SRC_IP6 iif $DEV flowlabel 0xfffff"
326		getnomatch="from $SRC_IP6 iif $DEV flowlabel 0xf"
327		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
328			"$getnomatch" "iif flowlabel redirect to table" \
329			"iif flowlabel no redirect to table"
330
331		match="flowlabel 0x08000/0x08000"
332		getmatch="flowlabel 0xfffff"
333		getnomatch="flowlabel 0xf7fff"
334		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
335			"$getnomatch" "flowlabel masked redirect to table" \
336			"flowlabel masked no redirect to table"
337
338		match="flowlabel 0x08000/0x08000"
339		getmatch="from $SRC_IP6 iif $DEV flowlabel 0xfffff"
340		getnomatch="from $SRC_IP6 iif $DEV flowlabel 0xf7fff"
341		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
342			"$getnomatch" "iif flowlabel masked redirect to table" \
343			"iif flowlabel masked no redirect to table"
344	fi
345
346	$IP link show dev $DEV | grep -q vrf0
347	if [ $? -eq 0 ]; then
348		match="oif vrf0"
349		getmatch="oif $DEV"
350		getnomatch="oif lo"
351		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
352			"$getnomatch" "VRF oif redirect to table" \
353			"VRF oif no redirect to table"
354
355		match="from $SRC_IP6 iif vrf0"
356		getmatch="from $SRC_IP6 iif $DEV"
357		getnomatch="from $SRC_IP6 iif lo"
358		fib_rule6_test_match_n_redirect "$match" "$getmatch" \
359			"$getnomatch" "VRF iif redirect to table" \
360			"VRF iif no redirect to table"
361	fi
362}
363
364fib_rule6_vrf_test()
365{
366	setup_vrf
367	fib_rule6_test "- with VRF"
368	cleanup_vrf
369}
370
371# Verify that the IPV6_TCLASS option of UDPv6 and TCPv6 sockets is properly
372# taken into account when connecting the socket and when sending packets.
373fib_rule6_connect_test()
374{
375	local dsfield
376
377	echo
378	echo "IPv6 FIB rule connect tests"
379
380	setup_peer
381	$IP -6 rule add dsfield 0x04 table $RTABLE_PEER
382
383	# Combine the base DS Field value (0x04) with all possible ECN values
384	# (Not-ECT: 0, ECT(1): 1, ECT(0): 2, CE: 3).
385	# The ECN bits shouldn't influence the result of the test.
386	for dsfield in 0x04 0x05 0x06 0x07; do
387		nettest -q -6 -B -t 5 -N $testns -O $peerns -U -D \
388			-Q "${dsfield}" -l 2001:db8::1:11 -r 2001:db8::1:11
389		log_test $? 0 "rule6 dsfield udp connect (dsfield ${dsfield})"
390
391		nettest -q -6 -B -t 5 -N $testns -O $peerns -Q "${dsfield}" \
392			-l 2001:db8::1:11 -r 2001:db8::1:11
393		log_test $? 0 "rule6 dsfield tcp connect (dsfield ${dsfield})"
394	done
395
396	# Check that UDP and TCP connections fail when using a DS Field that
397	# does not match the previously configured FIB rule.
398	nettest -q -6 -B -t 5 -N $testns -O $peerns -U -D \
399		-Q 0x20 -l 2001:db8::1:11 -r 2001:db8::1:11
400	log_test $? 1 "rule6 dsfield udp no connect (dsfield 0x20)"
401
402	nettest -q -6 -B -t 5 -N $testns -O $peerns -Q 0x20 \
403		-l 2001:db8::1:11 -r 2001:db8::1:11
404	log_test $? 1 "rule6 dsfield tcp no connect (dsfield 0x20)"
405
406	$IP -6 rule del dsfield 0x04 table $RTABLE_PEER
407
408	ip rule help 2>&1 | grep -q dscp
409	if [ $? -ne 0 ]; then
410		echo "SKIP: iproute2 iprule too old, missing dscp match"
411		cleanup_peer
412		return
413	fi
414
415	$IP -6 rule add dscp 0x3f table $RTABLE_PEER
416
417	nettest -q -6 -B -t 5 -N $testns -O $peerns -U -D -Q 0xfc \
418		-l 2001:db8::1:11 -r 2001:db8::1:11
419	log_test $? 0 "rule6 dscp udp connect"
420
421	nettest -q -6 -B -t 5 -N $testns -O $peerns -Q 0xfc \
422		-l 2001:db8::1:11 -r 2001:db8::1:11
423	log_test $? 0 "rule6 dscp tcp connect"
424
425	nettest -q -6 -B -t 5 -N $testns -O $peerns -U -D -Q 0xf4 \
426		-l 2001:db8::1:11 -r 2001:db8::1:11
427	log_test $? 1 "rule6 dscp udp no connect"
428
429	nettest -q -6 -B -t 5 -N $testns -O $peerns -Q 0xf4 \
430		-l 2001:db8::1:11 -r 2001:db8::1:11
431	log_test $? 1 "rule6 dscp tcp no connect"
432
433	$IP -6 rule del dscp 0x3f table $RTABLE_PEER
434
435	cleanup_peer
436}
437
438fib_rule4_del()
439{
440	$IP rule del $1
441	log_test $? 0 "del $1"
442}
443
444fib_rule4_del_by_pref()
445{
446	pref=$($IP rule show $1 table $RTABLE | cut -d ":" -f 1)
447	$IP rule del pref $pref
448}
449
450fib_rule4_test_match_n_redirect()
451{
452	local match="$1"
453	local getmatch="$2"
454	local getnomatch="$3"
455	local description="$4"
456	local nomatch_description="$5"
457
458	$IP rule add $match table $RTABLE
459	$IP route get $GW_IP4 $getmatch | grep -q "table $RTABLE"
460	log_test $? 0 "rule4 check: $description"
461
462	$IP route get $GW_IP4 $getnomatch 2>&1 | grep -q "table $RTABLE"
463	log_test $? 1 "rule4 check: $nomatch_description"
464
465	fib_rule4_del_by_pref "$match"
466	log_test $? 0 "rule4 del by pref: $description"
467}
468
469fib_rule4_test_reject()
470{
471	local match="$1"
472	local rc
473
474	$IP rule add $match table $RTABLE 2>/dev/null
475	rc=$?
476	log_test $rc 2 "rule4 check: $match"
477
478	if [ $rc -eq 0 ]; then
479		$IP rule del $match table $RTABLE
480	fi
481}
482
483fib_rule4_test()
484{
485	local ext_name=$1; shift
486	local getnomatch
487	local getmatch
488	local match
489	local cnt
490
491	echo
492	echo "IPv4 FIB rule tests $ext_name"
493
494	# setup the fib rule redirect route
495	$IP route add table $RTABLE default via $GW_IP4 dev $DEV onlink
496
497	match="oif $DEV"
498	getnomatch="oif lo"
499	fib_rule4_test_match_n_redirect "$match" "$match" "$getnomatch" \
500		"oif redirect to table" "oif no redirect to table"
501
502	ip netns exec $testns sysctl -qw net.ipv4.ip_forward=1
503	match="from $SRC_IP iif $DEV"
504	getnomatch="from $SRC_IP iif lo"
505	fib_rule4_test_match_n_redirect "$match" "$match" "$getnomatch" \
506		"iif redirect to table" "iif no redirect to table"
507
508	# Reject dsfield (tos) options which have ECN bits set
509	for cnt in $(seq 1 3); do
510		match="dsfield $cnt"
511		fib_rule4_test_reject "$match"
512	done
513
514	# Don't take ECN bits into account when matching on dsfield
515	match="tos 0x10"
516	for cnt in "0x10" "0x11" "0x12" "0x13"; do
517		# Using option 'tos' instead of 'dsfield' as old iproute2
518		# versions don't support 'dsfield' in ip rule show.
519		getmatch="tos $cnt"
520		getnomatch="tos 0x20"
521		fib_rule4_test_match_n_redirect "$match" "$getmatch" \
522			"$getnomatch" "$getmatch redirect to table" \
523			"$getnomatch no redirect to table"
524	done
525
526	# Re-test TOS matching, but with input routes since they are handled
527	# differently from output routes.
528	match="tos 0x10"
529	for cnt in "0x10" "0x11" "0x12" "0x13"; do
530		getmatch="tos $cnt"
531		getnomatch="tos 0x20"
532		fib_rule4_test_match_n_redirect "$match" \
533			"from $SRC_IP iif $DEV $getmatch" \
534			"from $SRC_IP iif $DEV $getnomatch" \
535			"iif $getmatch redirect to table" \
536			"iif $getnomatch no redirect to table"
537	done
538
539	match="fwmark 0x64"
540	getmatch="mark 0x64"
541	getnomatch="mark 0x63"
542	fib_rule4_test_match_n_redirect "$match" "$getmatch" "$getnomatch" \
543		"fwmark redirect to table" "fwmark no redirect to table"
544
545	fib_check_iproute_support "uidrange" "uid"
546	if [ $? -eq 0 ]; then
547		match="uidrange 100-100"
548		getmatch="uid 100"
549		getnomatch="uid 101"
550		fib_rule4_test_match_n_redirect "$match" "$getmatch" \
551			"$getnomatch" "uid redirect to table" \
552			"uid no redirect to table"
553	fi
554
555	fib_check_iproute_support "sport" "sport"
556	if [ $? -eq 0 ]; then
557		match="sport 666 dport 777"
558		getnomatch="sport 667 dport 778"
559		fib_rule4_test_match_n_redirect "$match" "$match" \
560			"$getnomatch" "sport and dport redirect to table" \
561			"sport and dport no redirect to table"
562
563		match="sport 100-200 dport 300-400"
564		getmatch="sport 100 dport 400"
565		getnomatch="sport 100 dport 401"
566		fib_rule4_test_match_n_redirect "$match" "$getmatch" \
567			"$getnomatch" \
568			"sport and dport range redirect to table" \
569			"sport and dport range no redirect to table"
570	fi
571
572	ip rule help 2>&1 | grep sport | grep -q MASK
573	if [ $? -eq 0 ]; then
574		match="sport 0x0f00/0xff00 dport 0x000f/0x00ff"
575		getmatch="sport 0x0f11 dport 0x220f"
576		getnomatch="sport 0x1f11 dport 0x221f"
577		fib_rule4_test_match_n_redirect "$match" "$getmatch" \
578			"$getnomatch" "sport and dport masked redirect to table" \
579			"sport and dport masked no redirect to table"
580	fi
581
582	fib_check_iproute_support "ipproto" "ipproto"
583	if [ $? -eq 0 ]; then
584		match="ipproto tcp"
585		getnomatch="ipproto udp"
586		fib_rule4_test_match_n_redirect "$match" "$match" \
587			"$getnomatch" "ipproto tcp match" \
588			"ipproto udp no match"
589	fi
590
591	fib_check_iproute_support "ipproto" "ipproto"
592	if [ $? -eq 0 ]; then
593		match="ipproto icmp"
594		getnomatch="ipproto tcp"
595		fib_rule4_test_match_n_redirect "$match" "$match" \
596			"$getnomatch" "ipproto icmp match" \
597			"ipproto tcp no match"
598	fi
599
600	fib_check_iproute_support "dscp" "tos"
601	if [ $? -eq 0 ]; then
602		match="dscp 0x3f"
603		getmatch="tos 0xfc"
604		getnomatch="tos 0xf4"
605		fib_rule4_test_match_n_redirect "$match" "$getmatch" \
606			"$getnomatch" "dscp redirect to table" \
607			"dscp no redirect to table"
608
609		match="dscp 0x3f"
610		getmatch="from $SRC_IP iif $DEV tos 0xfc"
611		getnomatch="from $SRC_IP iif $DEV tos 0xf4"
612		fib_rule4_test_match_n_redirect "$match" "$getmatch" \
613			"$getnomatch" "iif dscp redirect to table" \
614			"iif dscp no redirect to table"
615	fi
616
617	ip rule help 2>&1 | grep -q "DSCP\[/MASK\]"
618	if [ $? -eq 0 ]; then
619		match="dscp 0x0f/0x0f"
620		tosmatch=$(printf 0x"%x" $((0x1f << 2)))
621		tosnomatch=$(printf 0x"%x" $((0x1e << 2)))
622		getmatch="tos $tosmatch"
623		getnomatch="tos $tosnomatch"
624		fib_rule4_test_match_n_redirect "$match" "$getmatch" \
625			"$getnomatch" "dscp masked redirect to table" \
626			"dscp masked no redirect to table"
627
628		match="dscp 0x0f/0x0f"
629		getmatch="from $SRC_IP iif $DEV tos $tosmatch"
630		getnomatch="from $SRC_IP iif $DEV tos $tosnomatch"
631		fib_rule4_test_match_n_redirect "$match" "$getmatch" \
632			"$getnomatch" "iif dscp masked redirect to table" \
633			"iif dscp masked no redirect to table"
634	fi
635
636	$IP link show dev $DEV | grep -q vrf0
637	if [ $? -eq 0 ]; then
638		match="oif vrf0"
639		getmatch="oif $DEV"
640		getnomatch="oif lo"
641		fib_rule4_test_match_n_redirect "$match" "$getmatch" \
642			"$getnomatch" "VRF oif redirect to table" \
643			"VRF oif no redirect to table"
644
645		match="from $SRC_IP iif vrf0"
646		getmatch="from $SRC_IP iif $DEV"
647		getnomatch="from $SRC_IP iif lo"
648		fib_rule4_test_match_n_redirect "$match" "$getmatch" \
649			"$getnomatch" "VRF iif redirect to table" \
650			"VRF iif no redirect to table"
651	fi
652}
653
654fib_rule4_vrf_test()
655{
656	setup_vrf
657	fib_rule4_test "- with VRF"
658	cleanup_vrf
659}
660
661# Verify that the IP_TOS option of UDPv4 and TCPv4 sockets is properly taken
662# into account when connecting the socket and when sending packets.
663fib_rule4_connect_test()
664{
665	local dsfield
666
667	echo
668	echo "IPv4 FIB rule connect tests"
669
670	setup_peer
671	$IP -4 rule add dsfield 0x04 table $RTABLE_PEER
672
673	# Combine the base DS Field value (0x04) with all possible ECN values
674	# (Not-ECT: 0, ECT(1): 1, ECT(0): 2, CE: 3).
675	# The ECN bits shouldn't influence the result of the test.
676	for dsfield in 0x04 0x05 0x06 0x07; do
677		nettest -q -B -t 5 -N $testns -O $peerns -D -U -Q "${dsfield}" \
678			-l 198.51.100.11 -r 198.51.100.11
679		log_test $? 0 "rule4 dsfield udp connect (dsfield ${dsfield})"
680
681		nettest -q -B -t 5 -N $testns -O $peerns -Q "${dsfield}" \
682			-l 198.51.100.11 -r 198.51.100.11
683		log_test $? 0 "rule4 dsfield tcp connect (dsfield ${dsfield})"
684	done
685
686	# Check that UDP and TCP connections fail when using a DS Field that
687	# does not match the previously configured FIB rule.
688	nettest -q -B -t 5 -N $testns -O $peerns -D -U -Q 0x20 \
689		-l 198.51.100.11 -r 198.51.100.11
690	log_test $? 1 "rule4 dsfield udp no connect (dsfield 0x20)"
691
692	nettest -q -B -t 5 -N $testns -O $peerns -Q 0x20 \
693		-l 198.51.100.11 -r 198.51.100.11
694	log_test $? 1 "rule4 dsfield tcp no connect (dsfield 0x20)"
695
696	$IP -4 rule del dsfield 0x04 table $RTABLE_PEER
697
698	ip rule help 2>&1 | grep -q dscp
699	if [ $? -ne 0 ]; then
700		echo "SKIP: iproute2 iprule too old, missing dscp match"
701		cleanup_peer
702		return
703	fi
704
705	$IP -4 rule add dscp 0x3f table $RTABLE_PEER
706
707	nettest -q -B -t 5 -N $testns -O $peerns -D -U -Q 0xfc \
708		-l 198.51.100.11 -r 198.51.100.11
709	log_test $? 0 "rule4 dscp udp connect"
710
711	nettest -q -B -t 5 -N $testns -O $peerns -Q 0xfc \
712		-l 198.51.100.11 -r 198.51.100.11
713	log_test $? 0 "rule4 dscp tcp connect"
714
715	nettest -q -B -t 5 -N $testns -O $peerns -D -U -Q 0xf4 \
716		-l 198.51.100.11 -r 198.51.100.11
717	log_test $? 1 "rule4 dscp udp no connect"
718
719	nettest -q -B -t 5 -N $testns -O $peerns -Q 0xf4 \
720		-l 198.51.100.11 -r 198.51.100.11
721	log_test $? 1 "rule4 dscp tcp no connect"
722
723	$IP -4 rule del dscp 0x3f table $RTABLE_PEER
724
725	cleanup_peer
726}
727################################################################################
728# usage
729
730usage()
731{
732	cat <<EOF
733usage: ${0##*/} OPTS
734
735        -t <test>   Test(s) to run (default: all)
736                    (options: $TESTS)
737EOF
738}
739
740################################################################################
741# main
742
743while getopts ":t:h" opt; do
744	case $opt in
745		t) TESTS=$OPTARG;;
746		h) usage; exit 0;;
747		*) usage; exit 1;;
748	esac
749done
750
751if [ "$(id -u)" -ne 0 ];then
752	echo "SKIP: Need root privileges"
753	exit $ksft_skip
754fi
755
756if [ ! -x "$(command -v ip)" ]; then
757	echo "SKIP: Could not run test without ip tool"
758	exit $ksft_skip
759fi
760
761check_gen_prog "nettest"
762
763# start clean
764cleanup &> /dev/null
765setup
766for t in $TESTS
767do
768	case $t in
769	fib_rule6_test|fib_rule6)		fib_rule6_test;;
770	fib_rule4_test|fib_rule4)		fib_rule4_test;;
771	fib_rule6_connect_test|fib_rule6_connect)	fib_rule6_connect_test;;
772	fib_rule4_connect_test|fib_rule4_connect)	fib_rule4_connect_test;;
773	fib_rule6_vrf_test|fib_rule6_vrf)	fib_rule6_vrf_test;;
774	fib_rule4_vrf_test|fib_rule4_vrf)	fib_rule4_vrf_test;;
775
776	help) echo "Test names: $TESTS"; exit 0;;
777
778	esac
779done
780cleanup
781
782if [ "$TESTS" != "none" ]; then
783	printf "\nTests passed: %3d\n" ${nsuccess}
784	printf "Tests failed: %3d\n"   ${nfail}
785fi
786
787exit $ret
788