2 # SPDX-License-Identifier: GPL-2.0
4 # This test is for checking IPv4 and IPv6 FIB rules API
8 PAUSE_ON_FAIL
=${PAUSE_ON_FAIL:=no}
18 DEV_ADDR6
=2001:db8
:1::1
27 if [ ${rc} -eq ${expected} ]; then
28 nsuccess
=$
((nsuccess
+1))
29 printf "\n TEST: %-50s [ OK ]\n" "${msg}"
33 printf "\n TEST: %-50s [FAIL]\n" "${msg}"
34 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
36 echo "hit enter to continue, 'q' to quit"
38 [ "$a" = "q" ] && exit 1
46 echo "######################################################################"
47 echo "TEST SECTION: $*"
48 echo "######################################################################"
55 $IP link
set dev lo up
57 $IP link add dummy0
type dummy
58 $IP link
set dev dummy0 up
59 $IP address add
$DEV_ADDR/24 dev dummy0
60 $IP -6 address add
$DEV_ADDR6/64 dev dummy0
67 $IP link del dev dummy0
&> /dev
/null
71 fib_check_iproute_support
()
73 ip rule
help 2>&1 |
grep -q $1
75 echo "SKIP: iproute2 iprule too old, missing $1 match"
79 ip route get
help 2>&1 |
grep -q $2
81 echo "SKIP: iproute2 get route too old, missing $2 match"
91 log_test $?
0 "rule6 del $1"
94 fib_rule6_del_by_pref
()
96 pref
=$
($IP -6 rule show |
grep "$1 lookup $TABLE" | cut
-d ":" -f 1)
97 $IP -6 rule del pref
$pref
100 fib_rule6_test_match_n_redirect
()
105 $IP -6 rule add
$match table
$RTABLE
106 $IP -6 route get
$GW_IP6 $getmatch |
grep -q "table $RTABLE"
107 log_test $?
0 "rule6 check: $1"
109 fib_rule6_del_by_pref
"$match"
110 log_test $?
0 "rule6 del by pref: $match"
115 # setup the fib rule redirect route
116 $IP -6 route add table
$RTABLE default via
$GW_IP6 dev
$DEV onlink
119 fib_rule6_test_match_n_redirect
"$match" "$match" "oif redirect to table"
121 match
="from $SRC_IP6 iif $DEV"
122 fib_rule6_test_match_n_redirect
"$match" "$match" "iif redirect to table"
125 fib_rule6_test_match_n_redirect
"$match" "$match" "tos redirect to table"
129 fib_rule6_test_match_n_redirect
"$match" "$getmatch" "fwmark redirect to table"
131 fib_check_iproute_support
"uidrange" "uid"
132 if [ $?
-eq 0 ]; then
133 match
="uidrange 100-100"
135 fib_rule6_test_match_n_redirect
"$match" "$getmatch" "uid redirect to table"
138 fib_check_iproute_support
"sport" "sport"
139 if [ $?
-eq 0 ]; then
140 match
="sport 666 dport 777"
141 fib_rule6_test_match_n_redirect
"$match" "$match" "sport and dport redirect to table"
144 fib_check_iproute_support
"ipproto" "ipproto"
145 if [ $?
-eq 0 ]; then
147 fib_rule6_test_match_n_redirect
"$match" "$match" "ipproto match"
150 fib_check_iproute_support
"ipproto" "ipproto"
151 if [ $?
-eq 0 ]; then
152 match
="ipproto ipv6-icmp"
153 fib_rule6_test_match_n_redirect
"$match" "$match" "ipproto ipv6-icmp match"
160 log_test $?
0 "del $1"
163 fib_rule4_del_by_pref
()
165 pref
=$
($IP rule show |
grep "$1 lookup $TABLE" | cut
-d ":" -f 1)
166 $IP rule del pref
$pref
169 fib_rule4_test_match_n_redirect
()
174 $IP rule add
$match table
$RTABLE
175 $IP route get
$GW_IP4 $getmatch |
grep -q "table $RTABLE"
176 log_test $?
0 "rule4 check: $1"
178 fib_rule4_del_by_pref
"$match"
179 log_test $?
0 "rule4 del by pref: $match"
184 # setup the fib rule redirect route
185 $IP route add table
$RTABLE default via
$GW_IP4 dev
$DEV onlink
188 fib_rule4_test_match_n_redirect
"$match" "$match" "oif redirect to table"
190 # need enable forwarding and disable rp_filter temporarily as all the
191 # addresses are in the same subnet and egress device == ingress device.
192 ip netns
exec testns sysctl
-w net.ipv4.ip_forward
=1
193 ip netns
exec testns sysctl
-w net.ipv4.conf.
$DEV.rp_filter
=0
194 match
="from $SRC_IP iif $DEV"
195 fib_rule4_test_match_n_redirect
"$match" "$match" "iif redirect to table"
196 ip netns
exec testns sysctl
-w net.ipv4.ip_forward
=0
199 fib_rule4_test_match_n_redirect
"$match" "$match" "tos redirect to table"
203 fib_rule4_test_match_n_redirect
"$match" "$getmatch" "fwmark redirect to table"
205 fib_check_iproute_support
"uidrange" "uid"
206 if [ $?
-eq 0 ]; then
207 match
="uidrange 100-100"
209 fib_rule4_test_match_n_redirect
"$match" "$getmatch" "uid redirect to table"
212 fib_check_iproute_support
"sport" "sport"
213 if [ $?
-eq 0 ]; then
214 match
="sport 666 dport 777"
215 fib_rule4_test_match_n_redirect
"$match" "$match" "sport and dport redirect to table"
218 fib_check_iproute_support
"ipproto" "ipproto"
219 if [ $?
-eq 0 ]; then
221 fib_rule4_test_match_n_redirect
"$match" "$match" "ipproto tcp match"
224 fib_check_iproute_support
"ipproto" "ipproto"
225 if [ $?
-eq 0 ]; then
227 fib_rule4_test_match_n_redirect
"$match" "$match" "ipproto icmp match"
233 log_section
"IPv4 fib rule"
235 log_section
"IPv6 fib rule"
239 if [ "$(id -u)" -ne 0 ];then
240 echo "SKIP: Need root privileges"
244 if [ ! -x "$(command -v ip)" ]; then
245 echo "SKIP: Could not run test without ip tool"
255 if [ "$TESTS" != "none" ]; then
256 printf "\nTests passed: %3d\n" ${nsuccess}
257 printf "Tests failed: %3d\n" ${nfail}