2 # SPDX-License-Identifier: GPL-2.0
4 # This test is for checking IPv4 and IPv6 FIB rules API
6 # Kselftest framework requirement - SKIP code is 4.
11 PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}
21 DEV_ADDR6=2001:db8:1::1
23 TESTS="fib_rule6 fib_rule4"
31 if [ ${rc} -eq ${expected} ]; then
32 nsuccess=$((nsuccess+1))
33 printf "\n TEST: %-50s [ OK ]\n" "${msg}"
37 printf "\n TEST: %-50s [FAIL]\n" "${msg}"
38 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
40 echo "hit enter to continue, 'q' to quit"
42 [ "$a" = "q" ] && exit 1
50 echo "######################################################################"
51 echo "TEST SECTION: $*"
52 echo "######################################################################"
59 $IP link set dev lo up
61 $IP link add dummy0 type dummy
62 $IP link set dev dummy0 up
63 $IP address add $DEV_ADDR/24 dev dummy0
64 $IP -6 address add $DEV_ADDR6/64 dev dummy0
71 $IP link del dev dummy0 &> /dev/null
75 fib_check_iproute_support()
77 ip rule help 2>&1 | grep -q $1
79 echo "SKIP: iproute2 iprule too old, missing $1 match"
83 ip route get help 2>&1 | grep -q $2
85 echo "SKIP: iproute2 get route too old, missing $2 match"
95 log_test $? 0 "rule6 del $1"
98 fib_rule6_del_by_pref()
100 pref=$($IP -6 rule show $1 table $RTABLE | cut -d ":" -f 1)
101 $IP -6 rule del pref $pref
104 fib_rule6_test_match_n_redirect()
108 local description="$3"
110 $IP -6 rule add $match table $RTABLE
111 $IP -6 route get $GW_IP6 $getmatch | grep -q "table $RTABLE"
112 log_test $? 0 "rule6 check: $description"
114 fib_rule6_del_by_pref "$match"
115 log_test $? 0 "rule6 del by pref: $description"
118 fib_rule6_test_reject()
123 $IP -6 rule add $match table $RTABLE 2>/dev/null
125 log_test $rc 2 "rule6 check: $match"
127 if [ $rc -eq 0 ]; then
128 $IP -6 rule del $match table $RTABLE
138 # setup the fib rule redirect route
139 $IP -6 route add table $RTABLE default via $GW_IP6 dev $DEV onlink
142 fib_rule6_test_match_n_redirect "$match" "$match" "oif redirect to table"
144 match="from $SRC_IP6 iif $DEV"
145 fib_rule6_test_match_n_redirect "$match" "$match" "iif redirect to table"
147 # Reject dsfield (tos) options which have ECN bits set
148 for cnt in $(seq 1 3); do
150 fib_rule6_test_reject "$match"
153 # Don't take ECN bits into account when matching on dsfield
155 for cnt in "0x10" "0x11" "0x12" "0x13"; do
156 # Using option 'tos' instead of 'dsfield' as old iproute2
157 # versions don't support 'dsfield' in ip rule show.
159 fib_rule6_test_match_n_redirect "$match" "$getmatch" \
160 "$getmatch redirect to table"
165 fib_rule6_test_match_n_redirect "$match" "$getmatch" "fwmark redirect to table"
167 fib_check_iproute_support "uidrange" "uid"
168 if [ $? -eq 0 ]; then
169 match="uidrange 100-100"
171 fib_rule6_test_match_n_redirect "$match" "$getmatch" "uid redirect to table"
174 fib_check_iproute_support "sport" "sport"
175 if [ $? -eq 0 ]; then
176 match="sport 666 dport 777"
177 fib_rule6_test_match_n_redirect "$match" "$match" "sport and dport redirect to table"
180 fib_check_iproute_support "ipproto" "ipproto"
181 if [ $? -eq 0 ]; then
183 fib_rule6_test_match_n_redirect "$match" "$match" "ipproto match"
186 fib_check_iproute_support "ipproto" "ipproto"
187 if [ $? -eq 0 ]; then
188 match="ipproto ipv6-icmp"
189 fib_rule6_test_match_n_redirect "$match" "$match" "ipproto ipv6-icmp match"
196 log_test $? 0 "del $1"
199 fib_rule4_del_by_pref()
201 pref=$($IP rule show $1 table $RTABLE | cut -d ":" -f 1)
202 $IP rule del pref $pref
205 fib_rule4_test_match_n_redirect()
209 local description="$3"
211 $IP rule add $match table $RTABLE
212 $IP route get $GW_IP4 $getmatch | grep -q "table $RTABLE"
213 log_test $? 0 "rule4 check: $description"
215 fib_rule4_del_by_pref "$match"
216 log_test $? 0 "rule4 del by pref: $description"
219 fib_rule4_test_reject()
224 $IP rule add $match table $RTABLE 2>/dev/null
226 log_test $rc 2 "rule4 check: $match"
228 if [ $rc -eq 0 ]; then
229 $IP rule del $match table $RTABLE
239 # setup the fib rule redirect route
240 $IP route add table $RTABLE default via $GW_IP4 dev $DEV onlink
243 fib_rule4_test_match_n_redirect "$match" "$match" "oif redirect to table"
245 # need enable forwarding and disable rp_filter temporarily as all the
246 # addresses are in the same subnet and egress device == ingress device.
247 ip netns exec testns sysctl -qw net.ipv4.ip_forward=1
248 ip netns exec testns sysctl -qw net.ipv4.conf.$DEV.rp_filter=0
249 match="from $SRC_IP iif $DEV"
250 fib_rule4_test_match_n_redirect "$match" "$match" "iif redirect to table"
251 ip netns exec testns sysctl -qw net.ipv4.ip_forward=0
253 # Reject dsfield (tos) options which have ECN bits set
254 for cnt in $(seq 1 3); do
256 fib_rule4_test_reject "$match"
259 # Don't take ECN bits into account when matching on dsfield
261 for cnt in "0x10" "0x11" "0x12" "0x13"; do
262 # Using option 'tos' instead of 'dsfield' as old iproute2
263 # versions don't support 'dsfield' in ip rule show.
265 fib_rule4_test_match_n_redirect "$match" "$getmatch" \
266 "$getmatch redirect to table"
271 fib_rule4_test_match_n_redirect "$match" "$getmatch" "fwmark redirect to table"
273 fib_check_iproute_support "uidrange" "uid"
274 if [ $? -eq 0 ]; then
275 match="uidrange 100-100"
277 fib_rule4_test_match_n_redirect "$match" "$getmatch" "uid redirect to table"
280 fib_check_iproute_support "sport" "sport"
281 if [ $? -eq 0 ]; then
282 match="sport 666 dport 777"
283 fib_rule4_test_match_n_redirect "$match" "$match" "sport and dport redirect to table"
286 fib_check_iproute_support "ipproto" "ipproto"
287 if [ $? -eq 0 ]; then
289 fib_rule4_test_match_n_redirect "$match" "$match" "ipproto tcp match"
292 fib_check_iproute_support "ipproto" "ipproto"
293 if [ $? -eq 0 ]; then
295 fib_rule4_test_match_n_redirect "$match" "$match" "ipproto icmp match"
301 log_section "IPv4 fib rule"
303 log_section "IPv6 fib rule"
306 ################################################################################
314 -t <test> Test(s) to run (default: all)
319 ################################################################################
322 while getopts ":t:h" opt; do
330 if [ "$(id -u)" -ne 0 ];then
331 echo "SKIP: Need root privileges"
335 if [ ! -x "$(command -v ip)" ]; then
336 echo "SKIP: Could not run test without ip tool"
346 fib_rule6_test|fib_rule6) fib_rule6_test;;
347 fib_rule4_test|fib_rule4) fib_rule4_test;;
349 help) echo "Test names: $TESTS"; exit 0;;
355 if [ "$TESTS" != "none" ]; then
356 printf "\nTests passed: %3d\n" ${nsuccess}
357 printf "Tests failed: %3d\n" ${nfail}